-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunCurve.m
493 lines (345 loc) · 10.1 KB
/
funCurve.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
function y = funCurve(x,poolSize,forceRepeat,multi)
tic
%% Input tests
if nargin < 2 || poolSize < 1
poolSize = 1;
end
if nargin < 3
forceRepeat = 0;
end
if nargin < 4
multi = 0;
end
t = getCurrentTask();
if isempty(t) % First evaluation is always performed in serial
pID = 1;
else
pID = t.ID;
end
%% Calculate parameters
% Defaults
geoInput(1:12) = [1.115;... % 1 bottomRad 1.115
0.072;... % 2 openingRad 0.072
1.018;... % 3 topRad 1.018
1.109;... % 4 cornerHeight 1.109
-0.269;... % 5 freeHeightOffset -0.269
0;... % 6 commisureDeg 0
7;... % 7 sinusDeg 7
0.331;... % 8 freeDeg 0.331
6.735;... % 9 fixedDeg 6.735
0.471;... % 10 fixedBlend 0.471
0.615;... % 11 centerBlend 0.615
0.818]; % 12 interiorBlend 0.818
thickness = 0.0386;
if ~isempty(x)
geoInput([5,8,9,11]) = x(1:(end-1));
thickness = x(end);
else
x(1:4) = geoInput([5,8,9,11]);
x(5) = thickness;
end
%% Check if repeat parameters
[repeat,output] = RepeatCheck(x);
if forceRepeat
repeat = 0;
end
%% Evaluate
if ~repeat % Continue with analysis
csvwrite(['sync/ready.' num2str(pID) '.dat'],1); % pID: "Hey, I'm ready for geometry!"
currentID = 0;
testID = currentID; % To track if currentID is updated
tWait = toc;
while pID ~= currentID % Wait for turn
if(toc - tWait > 10) % Increment if no change for 10 seconds
currentID = IncrementID(poolSize);
if isempty(currentID) % Appoint current pID if problem
currentID = pID;
elseif currentID == 0
currentID = pID;
end
csvwrite('sync/current.dat', currentID); % Write new ID
AnnounceID(currentID)
tWait = toc;
end
pause(0.1)
try % Attempt currentID update
currentID = csvread('sync/current.dat');
catch
end
if testID ~= currentID % Restart timer if currentID is changed
testID = currentID;
tWait = toc;
end
end
GHflag = GenerateGeometry(geoInput,thickness,pID);
delete(['sync/ready.' num2str(pID) '.dat'])
currentID = IncrementID(poolSize);
if isempty(currentID) || currentID == 0
currentID = mod(pID+1,poolSize);
end
csvwrite('sync/current.dat', currentID);
AnnounceID(currentID)
if GHflag == 1
RunAnalysis(pID)
if ~exist(['setup-opt-' num2str(pID) '/step.dat'], 'file')
GHflag = 2;
end
end
output(1:13) = [-Inf,Inf,Inf,Inf,Inf,0,0,Inf,Inf,Inf,Inf,Inf,Inf];
if GHflag == 1
output(1:13) = ReadResults(output(1:13),pID,geoInput);
end
output(6) = GHflag;
end
output(5) = toc;
output(7) = repeat;
y = CalculateObjectives(output,thickness,multi);
%% Log results
LogResults(geoInput,thickness,output,pID);
end
%% Subfunctions
function [repeat,output] = RepeatCheck(x)
repeat = 0;
output = [];
results = [];
while isempty(results) % Try until the file can be read
try
results = csvread('results/results.txt'); % Read results file
catch
pause(0.01); % Delay before retry
end
end
resNorm = 0.0001;
for i = 1:size(results,1) % Parse results
if sqrt(sum( ( results(i,[5,8,9,11,13]) - x ).^2 )) <= resNorm
output = results(i,14:26);
repeat = 1;
break
end
end
end
function GHflag = GenerateGeometry(geoInput,thickness,pID)
ClearOldResults(); % Clear results files
WriteInputs(geoInput,thickness);
pause(0.1); % Let data transfer to Windows
SetGeoFlag();
pause(0.5);
GHflag = csvread('geometry/GHflag.txt');
if GHflag == 1
MoveGeoFiles(pID);
end
end
function nextID = IncrementID(poolSize)
nextID = [];
for i = 1:poolSize % Increment processor
if exist(['sync/ready.' num2str(i) '.dat'],'file')
nextID = i;
break
end
end
end
function AnnounceID(currentID)
disp(currentID)
end
function RunAnalysis(pID)
while exist(['setup-opt-' num2str(pID) '/bashComplete.txt'],'file')
try
delete(['setup-opt-' num2str(pID) '/bashComplete.txt'])
catch
end
end
system(['./setup-opt-' num2str(pID) '/prepAnalysis.sh &']);
bashTry = 1;
bashComplete = 0;
while bashComplete ~= 1 && bashTry <= 2 % Max number of analysis attempts
system(['./setup-opt-' num2str(pID) '/runAnalysis.sh &']);
bashStart = toc;
while bashComplete ~= 1 && toc - bashStart < 60*20 % Max allowable time for analysis
pause(0.1)
if exist(['setup-opt-' num2str(pID) '/bashComplete.txt'],'file')
bashComplete = 1;
break
end
end
if bashComplete ~= 1 % Get bash PID and kill process
bashPID = csvread(['setup-opt-' num2str(pID) '/bashPID.txt']);
if ~isempty(bashPID)
system(['kill -9 ' num2str(bashPID) ' &']);
end
bashTry = bashTry + 1;
end
end
while exist(['setup-opt-' num2str(pID) '/bashComplete.txt'],'file')
try
delete(['setup-opt-' num2str(pID) '/bashComplete.txt'])
catch
end
end
end
function output = ReadResults(output,pID,geoInput)
try
results = csvread(['setup-opt-' num2str(pID) '/results.dat']);
catch
results = [];
end
if length(results) >= 8
output(8) = results(end-7); % Total Area
output(1) = results(end-6); % Contact Area
output(2) = results(end-5); % Strain
output(9) = results(end-4); % Strain Area
output(10) = results(end-3); % Bending
output(11) = results(end-2); % Bending Area
output(12) = results(end-1); % Tensile
output(13) = results(end); % Tensile Area
end
try
[output(3), output(4)] = compute_center_area(pID, max(geoInput([3,3]))); % Open Area, Max Radius
catch
end
end
function y = CalculateObjectives(output,thickness,multi)
y = output(11)/output(8); % Bending Area Ratio
%y = output(9)/output(8); % Strain Area Ratio
%y = output(2); % Strain
%y = -output(1); % Coaptation Area
%y = output(3); % Open Area
%y = [output(2),-output(1)]; % Multi-objective
% CONSTRAINTS
flexFactor = 1.01; % How much more can the best solution have?
if output(2) > 0.613*flexFactor % Peak Green Strain
y = Inf;
end
if output(12) > 0.168*flexFactor % Peak Tensile Strain
y = Inf;
end
if output(11) > 10.11*flexFactor % Peak Bending (delta_K)
y = Inf;
end
if output(3) > 0.05*flexFactor % Regurgitation EOA
y = Inf;
end
if (output(1)/output(8) < 0.1/flexFactor) || (output(1)/output(8) > 0.25*flexFactor) % Coaptation Ratio
y = Inf;
end
if multi
if y ~= Inf
y(2) = thickness;
else
y(2) = Inf;
end
end
end
function LogResults(geoInput,thickness,output,pID)
while exist('results/writing.dat','file')
pause(0.01)
end
csvwrite('results/writing.dat',1);
resultsLOG = csvread('results/results.txt');
if resultsLOG(1,1) == 0
resultsLOG(1,1:26) = [geoInput,thickness,output];
else
resultsLOG(end+1,1:26) = [geoInput,thickness,output];
end
csvwrite('results/results.txt',resultsLOG);
delete('results/writing.dat');
if exist(['setup-opt-' num2str(pID) '/output.txt'],'file')
% Move output log
try
outString = ['results/output-' num2str(size(resultsLOG,1)) '.txt'];
movefile(['setup-opt-' num2str(pID) '/output.txt'], outString)
catch
end
end
end
%% Geometry Subfunctions
function ClearOldResults()
if exist('geometry/smesh.1.dat','file')
try
delete('geometry/smesh.1.dat')
catch
end
end
if exist('geometry/smesh.2.dat','file')
try
delete('geometry/smesh.2.dat')
catch
end
end
if exist('geometry/smesh.3.dat','file')
try
delete('geometry/smesh.3.dat')
catch
end
end
if exist('geometry/thickness.dat','file')
try
delete('geometry/thickness.dat')
catch
end
end
if exist('geometry/GHflag.txt','file')
try
delete('geometry/GHflag.txt')
catch
end
end
ReadyFlag = 0;
while ReadyFlag == 0 % Wait for geometry to be deleted on Windows
if exist('geometry/input.geo.dat','file')
try
delete('geometry/input.geo.dat')
catch
end
end
pause(0.1);
try
ReadyFlag = csvread('geometry/ReadyFlag.txt');
catch
ReadyFlag = 0;
end
end
end
function WriteInputs(geoInput,thickness)
csvwrite('geometry/thickness.dat',thickness);
ReadyFlag = 1;
while ReadyFlag == 1 % Wait for geometry to be deleted on Windows
try
csvwrite('geometry/input.geo.dat',geoInput');
catch
end
pause(0.1);
try
ReadyFlag = csvread('geometry/ReadyFlag.txt');
catch
ReadyFlag = 1;
end
end
end
function SetGeoFlag()
csvwrite('geometry/GeoGenFlag.txt',1);
GeoGenFlag = 1;
while GeoGenFlag == 1
pause(0.1);
try
GeoGenFlag = csvread('geometry/GeoGenFlag.txt');
catch
GeoGenFlag = 1;
end
end
end
function MoveGeoFiles(pID)
smeshFlag = [0,0,0];
while min(smeshFlag) == 0
if exist('geometry/smesh.1.dat','file') % Wait for results files to be written
smeshFlag(1) = 1;
end
if exist('geometry/smesh.2.dat','file')
smeshFlag(2) = 1;
end
if exist('geometry/smesh.3.dat','file')
smeshFlag(3) = 1;
end
end
pause(0.1) % Extra time for files to be released
system(['./setup-opt-' num2str(pID) '/prepAnalysis.sh']); % Copy files, etc.
end