forked from MWSL-UnB/HERMES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combineHermesParameters.m
392 lines (303 loc) · 13.8 KB
/
combineHermesParameters.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
function combineHermesParameters( FILE, PARAMETER, AVOIDED_COMBINATION )
%COMBINEHERMESPARAMETERS combines parameters in grid scripts
% This function combines all provided parameters, removes combinations
% that are not wanted and writes the grid scripts.
%
% Syntax: combineHermesParameters( FILE, PARAMETER, AVOIDED_COMBINATION )
%
% Inputs:
% FILE < struct > - contains the labels provided by the user to the
% results folder and grid files. It contains the following fields.
% folderName < string > - contains the name of the folder
% where the input parameters are located
%
% parametersStruct < string cell > - each element
% contains the Struct of the technology that will be
% simulated in this campaign
%
% simulationPrefix < string > - is the label you will
% give to your simulation folders and jobs
%
% simulationSuffix < string > - this is the tag you will
% give to your simulation campaign. The suffix is used
% when you will merge your simulation results.
%
% scriptSuffix < string > - this is the tag you will give
% to you script files
%
% numberOfDropsPerJob < integer > - this is the number of
% drops in each simulation job. The total number of drops
% in your simulation is the numberOfDropsPerJob
% multiplied by the numberOfJobs.
%
% numberOfJobs < integer > - this is the number of
% parallel simulations.
%
% PARAMETER < 1 x NumberOfParametersToBeChanged cell > - each cell
% contains a struct with the following fields:
% name < string >: contains the parameter to be changed,
% the exact string used in the parameters file.
%
% value < 1 x 1 cell >: the element contains an array of
% strings. Each string represents the value of the
% parameter to be changed.
%
% label < 1 x 1 cell >: the element contains an array of
% strings. Each string representes the label of the
% parameter to be changed.
%
% AVOIDED_COMBINATION < cell 1 x numberOfcombinationsToBeAvoided > is
% struct with the following fields:
% name < 1 x numberOfParametersToBeAvoided cell > this
% cell contains in each position the name of the
% parameter that can't be combined with the other...
%
% label < 1 x numberOfcombinationsToBeAvoided cell > is a
% cell that contains in each position the label of the
% value that can't be combined with the others...
%
% Author: Erika Portela Lopes de Almeida
% Work Address: INDT Brasília
% E-mail: [email protected]
% History:
% v2.0 9 Jul 2015 (EA) - created
%
% Copyright (c) 2015 INDT - Institute of Technology Development.
%
% The program may be used and/or copied only with the written
% permission of INDT, or in accordance with the terms and conditions
% stipulated in the agreement/contract under which the program has been
% supplied.
% =========================================================================
% COMBINE PARAMETERS
% =========================================================================
numberOfCombinations = length( AVOIDED_COMBINATION );
numberOfParameters = length( PARAMETER );
avoidedCombinations = zeros( numberOfParameters, numberOfCombinations );
numParameter = cell( 1, numberOfCombinations );
parameterValue = cell( 1, numberOfCombinations );
for combination = 1 : numberOfCombinations
paramValueComb = [];
paramNameComb = [];
combinationSize = length( AVOIDED_COMBINATION{ combination }.name );
for index = 1 : combinationSize
parameterName = AVOIDED_COMBINATION{ combination }.name{ index };
for param = 1 : numberOfParameters
thisParameter = PARAMETER{ param }.name;
if strcmp( parameterName,thisParameter )
% get this index
paramNameComb = [ paramNameComb; param ];
% test Value
numberOfValues = length( PARAMETER{ param }.label );
for paramValues = 1 : numberOfValues
if strcmp( PARAMETER{ param }.label{ paramValues },...
AVOIDED_COMBINATION{ combination }.label{ index } )
paramValueComb = [ paramValueComb; paramValues ];
end
end
end
end
end
numParameter{ combination } = paramNameComb;
parameterValue{ combination } = paramValueComb;
end
for avoidedComb = 1 : numberOfCombinations
lineIndexes = numParameter{ avoidedComb };
columnValues = parameterValue{ avoidedComb };
if ~all( size( lineIndexes ) == size( columnValues ) )
error('Please check the names and values chosen for AVOIDED_COMBINATIONS');
end
avoidedCombinations( lineIndexes, avoidedComb ) = columnValues;
end
% get the number of parameters to be varied
numberOfLevels = size( PARAMETER, 2 );
numberOfValues = zeros(1,numberOfLevels);
for level = 1 : numberOfLevels
numberOfValues( level ) = length( PARAMETER{ level }.value );
end
% combine the indexes of parameters to be combined :D
command = 'combvec(';
for i = 1: numberOfLevels
command = [command '1:' num2str( numberOfValues( i ) ) ', '];
end
command = command(1:length(command)-2);
command = [command ');'];
parameterCombinations = eval(command);
columnToBeDeleted = [];
% check if there are any impossible combinations
if ~isempty( avoidedCombinations )
% get the parameter indexes that cannot be combined
getIndexes = sum( avoidedCombinations, 2 );
paramIndexes = find( getIndexes > 0 );
numberOfAvoidedCombinations = length( paramIndexes );
% look for the impossible combinations
for avoidedCombination = 1: numberOfAvoidedCombinations
% get the parameters values
getValueIndexes = avoidedCombinations( :, avoidedCombination );
% get the number of parameters to be checked
paramsToBeChecked = find( getValueIndexes ~= 0 );
columnOld = [];
for i = 1: length( paramsToBeChecked )
columnNew = find( parameterCombinations( paramsToBeChecked( i ), : ) == getValueIndexes( i ) );
if i == 1
columnOld = columnNew;
else
columnOld = intersect( columnOld, columnNew );
end
end
columnToBeDeleted = [ columnToBeDeleted, columnOld ];
end
end
% Delete from parameterCombinations the impossible Combinations
columnIndexes = 1 : size( parameterCombinations, 2 );
columnToMaintain = setdiff( columnIndexes, columnToBeDeleted );
% updated parameterCombinations
parameterCombinations = parameterCombinations( :, columnToMaintain );
% =========================================================================
% Get the base parameter files
% =========================================================================
% get the name of this folder
thisFolder = pwd;
% write the name of the folder where the parameters are located.
parametersFolder = [ thisFolder, filesep, FILE.folderName ];
eval( ['cd ', parametersFolder] );
fileList = dir( parametersFolder );
% look for input parameters files
fileIndex = [];
for index = 1 : length( fileList )
if size( fileList( index ).name, 2 ) > 2
fileIndex = [ fileIndex index ];
end
end
% initialize a cell with input parameters file names
inputFiles = cell( 1, length(fileIndex) );
inputParameters = cell( 1, length( fileIndex ) );
for index = 1 : length( fileIndex )
inputFiles{index} = [ fileList( fileIndex(index) ).name ];
fileName = inputFiles{index};
inputParameters{ index } = textread( strcat( fileName(1:end-2),'.m' ), ...
'%s', 'delimiter', '\n' );
end
eval( 'cd ..' )
% =========================================================================
% Overwrite the parameters to be changed and create results directory
% =========================================================================
resultsPath = pwd;
resultsFolderName = 'deployed';
% loop over parameters combination
for combinationID = 1 : size( parameterCombinations, 2 )
fileIdentification = '';
for k = 1 : size( PARAMETER,2 )
fileIdentification = sprintf( '%s%s%s', fileIdentification,'_',...
char(PARAMETER{k}.label{parameterCombinations(k,combinationID)}));
end
fileIdentification = sprintf( '%s%s%s%s', FILE.simulationPrefix, ...
fileIdentification, '_',FILE.simulationSuffix );
% copy the parameters
inputParameters_tmp = inputParameters;
% loop over parameter files
for inputs = 1 : size( inputParameters, 2)
for k = 1 : size( PARAMETER, 2 )
inputParameters_tmp{ inputs } = strrep( inputParameters_tmp{ inputs }, ...
'SETTINGS.DROPS.MAX_NUMBER_OF_DROPS = x;',...
sprintf(['SETTINGS.DROPS.MAX_NUMBER_OF_DROPS = ',...
num2str(FILE.numberOfDropsPerJob),';'] ) );
inputParameters_tmp{ inputs } = strrep( inputParameters_tmp{ inputs },...
[ char( PARAMETER{k}.name) ' = x;' ],...
sprintf('%s%s', [char(PARAMETER{k}.name) ' = ' ...
char(PARAMETER{k}.value(parameterCombinations(k,combinationID))),';'] ) );
end
end
consolidatedInputParameters = [];
for inputs = 1 : size( inputParameters, 2 )
consolidatedInputParameters = [ consolidatedInputParameters; ...
inputParameters_tmp{ inputs } ];
end
% combine all input parameters
AllParameters{ combinationID } = consolidatedInputParameters;
AllNames{ combinationID } = fileIdentification;
AllDirectoryNames{ combinationID } = fileIdentification;
test = dir([resultsPath, filesep, resultsFolderName, filesep, ...
AllDirectoryNames{ combinationID }]);
% check if the results folder exists
if isempty( test )
mkdir([ resultsPath, filesep, resultsFolderName, filesep, ...
AllDirectoryNames{ combinationID }]);
end
% go to results folder
cd([ resultsPath, filesep, resultsFolderName, filesep, ...
AllDirectoryNames{ combinationID } ]);
% Write the parameters file
for pdi = size( AllParameters, 2 )
% get the parameters
temporaryParameters = AllParameters{ combinationID };
% open and create a new parameter file
fileName = fopen( sprintf('%s.m', AllNames{ combinationID } ),'w');
% write the parameters into a new file
for parameterLine = 1 : length( temporaryParameters )
fprintf( fileName, '%s\n',...
cell2mat( temporaryParameters( parameterLine ) ) );
end
fclose( fileName );
end
eval( 'cd ..' );
end
% =========================================================================
% Create shell files
% =========================================================================
% create shell file
shellFileJMSGrid = ['runJMSsimulations_', FILE.scriptSuffix, '.sh'];
shellFileQSUBGrid = ['runQsubSimulations_',FILE.scriptSuffix, '.sh'];
% open files
fileID_jmsGrid = fopen( shellFileJMSGrid, 'w' );
fileID_qsubGrid = fopen( shellFileQSUBGrid, 'w' );
if isunix()
fileattrib(shellFileJMSGrid,'+x', 'a');
fileattrib(shellFileQSUBGrid,'+x', 'a');
end
fid_list = fopen('SimList','w');
for combinationID = 1 : length( AllNames )
display(['Creating file: ' AllNames{ combinationID } '...']);
% create results directory
resultsDirectory = AllDirectoryNames{ combinationID };
resultsDirectoryComplete = [ resultsPath filesep resultsFolderName filesep resultsDirectory ];
test = dir( resultsDirectoryComplete );
if isempty( test )
mkdir( resultsDirectoryComplete );
end
% load parameters
fileName_tmp =[ AllNames{combinationID}];
numberOfOutputs = length( FILE.parameterStructs );
filePath = [ resultsDirectoryComplete filesep fileName_tmp ];
run(filePath);
command = [ ' save ', filePath,'.mat'];
for i = 1 : numberOfOutputs
command = [ command, ' ',char(FILE.parameterStructs(i)) ];
end
eval( command );
% creat the grid scripts
for job = 1 : FILE.numberOfJobs
fprintf( fileID_jmsGrid, 'jms -b -log %s -t %s hermesStart %s %s %s \n', ...
[ resultsDirectoryComplete filesep fileName_tmp '_JOB' num2str(job) '.log' ], ... % log file
[ fileName_tmp '_JOB' num2str(job) ], ... % grid tag
[ resultsDirectoryComplete ], ... inputParametersDir
resultsDirectoryComplete, ...
char( num2str( job ) ) ...
);
end
for job = 1 : FILE.numberOfJobs
fprintf( fileID_qsubGrid, 'qsub -b -log %s -t %s hermesStart %s %s %s \n', ...
[ resultsDirectoryComplete filesep fileName_tmp '_JOB' num2str(job) '.log' ], ... % log file
[ fileName_tmp '_JOB' num2str(job) ], ... % grid tag
[ resultsDirectoryComplete ], ... inputParametersDir
resultsDirectoryComplete, ...
char( num2str( job ) ) ...
);
end
fprintf( fid_list, '%s\n', resultsDirectoryComplete );
end
fclose( fid_list );
fclose( fileID_qsubGrid );
fclose( fileID_jmsGrid );
eval( 'cd ..' );
end