-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFEA_RepairSims.m
158 lines (141 loc) · 5.98 KB
/
FEA_RepairSims.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
% Automatically repair FEBio .feb files that wont run with an alternate version
% This assumes files have been generated by FEA_GenerateSims.m already, and
% have been run at least once
%% SET UP PARAMETERS
model = 'defect_model_';
if strcmp(model,'defect_model_')
% patient-specific defect model
n_pts_1 = 1; % number of simulations to run the first parameter set
n_pts_2 = 23; % for the 2nd parameter set
fac = 1;
range_E = [0.3:0.1:1.5].*(0.85*fac);
range_v = [0.3:0.1:1.5].*0.30;
range_s = [1e-9 1e-8 5e-8 1e-7 5e-7 1e-6 5e-6 1e-5 1e-4 1e-3 1e-2 1e-1 1 10 100 1000 1e4 5e4 1e5 5e5 1e6 5e6 1e7];
range_s = sort(range_s);
E_ind = 46;
v_ind = 47;
s_ind = 679508;
root = 'C:\Users\Sameer\OneDrive - Rensselaer Polytechnic Institute\Documents\FEA\Hex_Mesh_Knee_Cartilage\9932809_00mo\smoothed\models - febio\';
% lines to replace only for broken files
r_ind = 17;
r_vals = [1 0]; % 2 values to swap between
% the two halves of the line, the value fits between them when concatenated horizontally
r_format_1 = [' <qnmethod>'];
r_format_2 = ['</qnmethod>'];
end
%% DETERMINE WHICH FILES SHOULD BE CORRECTED
if ~exist('to_use','var')
to_use = ones(n_pts_1,n_pts_2);
disp('The variable "to_use" will now be generated.')
parfor i = 1:n_pts_1
for j = 1:n_pts_2
% generate the filename
path = [root 'E' num2str(round(range_E(i)/fac,2)) '\'];
filename = [model num2str(round(range_E(i)/fac,2)) 'e6_' num2str(range_s(j)) '.log'];
target = [path filename];
disp(['Assessing file: ' extract_filename(target)]);
disp('--------------------------------------------------');
% make sure it exists
if ~exist(target,'file')
disp('This file does not exist! Marking it as not run...')
to_use(i,j) = 0;
continue;
end
% determine if that file terminated properly
[~,~,~,~,~,finish] = FEA_FileOptimizer(target,'log');
to_use(i,j) = finish;
disp(['---------------------------------------------' newline 'Finished assessing file!']);
end
end
disp(newline);
disp('--------------------------------------------------');
disp(' F I L E A S S E S S M E N T C O M P L E T E! ');
disp('--------------------------------------------------');
end
%% RUN THROUGH AND REPAIR THOSE FILES
parfor i = 1:n_pts_1
for j = 1:n_pts_2
if ~to_use(i,j)
% generate the appropriate filename
path = [root 'E' num2str(round(range_E(i)/fac,2)) '\'];
filename = [model num2str(round(range_E(i)/fac,2)) 'e6_' num2str(range_s(j)) '.feb'];
target = [path filename];
% make sure that file exists at all
if ~exist(target,'file')
disp('This file to be corrected: ')
disp(extract_filename(target))
disp('does not exist! Skipping...')
continue;
end
% read in that file
disp('Reading file: ')
disp(extract_filename(target))
base_file = cell(1e7,1);
file_ind = 1;
readfile = fopen(target,'r');
readline = fgetl(readfile);
while(~strcmp(readline,'</febio_spec>'))
base_file{file_ind} = readline;
file_ind = file_ind + 1;
readline = fgetl(readfile);
end
base_file{file_ind} = '</febio_spec>';
n_lines = file_ind;
disp('File read!')
fclose(readfile);
% now fix that line to be the opposite of what it was
r_line = base_file{r_ind};
if contains(r_line,num2str(r_vals(1)))
r_line = [r_format_1 num2str(r_vals(2)) r_format_2];
elseif contains(r_line,num2str(r_vals(2)))
r_line = [r_format_1 num2str(r_vals(1)) r_format_2];
end
% write the file with the corrected line
disp('Writing file: ');
disp(filename);
% start writing the file
writefile = fopen(target,'w');
for k = 1:n_lines
if k == r_ind
to_write = r_line;
else
to_write = base_file{k};
end
fprintf(writefile,'%s\n',to_write);
end
fclose(writefile);
disp('Finished writing!')
end
end
end
%% GENERATE BATCH FILES TO RUN *ONLY* THE MODELS IN NEED OF REPAIR
% write the master file - calls the other batch files per folder
masterfile = ['run_all_repair.bat'];
target = [root masterfile];
writefile = fopen(target,'w');
fprintf(writefile,'%s\n','@echo on');
fprintf(writefile,'%s\n',['cd ' root]);
for i = 1:n_pts_1
folder = ['E' num2str(round(range_E(i)/fac,2))];
to_write = ['start "' folder '" /wait cmd /c CALL ' folder '/run_' folder '_repair.bat'];
fprintf(writefile,'%s\n',to_write);
end
%fprintf(writefile,'%s\n','call run_intact.bat');
fclose(writefile);
% write each of the individual files per folder
for i = 1:n_pts_1
folder = ['E' num2str(round(range_E(i)/fac,2))];
subfile = ['run_' folder '_repair.bat'];
target = [root folder '\' subfile];
writefile = fopen(target,'w');
fprintf(writefile,'%s\n','@echo off');
fprintf(writefile,'%s\n','cd C:\Program Files\FEBio2.9.1\bin');
for j = 1:n_pts_2
% write only the needed files
if ~to_use(i,j)
to_write = ['FEBio2 -i "' root folder '\' model num2str(round(range_E(i)/fac,2)) 'e6_' num2str(range_s(j)) '.feb"'];
fprintf(writefile,'%s\n',to_write);
end
end
fclose(writefile);
end