rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
save_detailed_simulations.m
Go to the documentation of this file.
1 function save_detailed_simulations(model,model_data,M,savepath)
2 % function save_detailed_simulations(model,model_data,M,savepaths)
3 % perform loop over detailed simulations and save results or check
4 % consistency with existing saved results.
5 %
6 % -# If the given path does not exist, it is generated and a set of
7 % detailed simulations for all parameters mu (columns of M).
8 % is computed and the results are stored in the path specified in
9 % 'savepath'.
10 % Files to be generated in the target path are:
11 % - 'settings.mat' :containing 'model', 'grid', 'M', 'path', ... such that
12 % the directory can be regenerated with this file and
13 % the current function
14 % - 'detail1.mat' :containing the simulation data for first `mu`
15 % vector in 'M'
16 % - 'detail1.mat' :containing the simulation data for second `mu`
17 % vector in 'M'
18 % - ....
19 % - 'detail123.mat' : ...
20 % .
21 % -# If the path exists, a check on consistency is performed by checking the
22 % 'model' and 'Mtrain' fields and checking whether the previous computation has
23 % been finished. If it is not, and the current settings are consistent with the
24 % old ones, the computations are resumed.
25 %
26 % optional fields of model:
27 % force_delete : if this option is set to true (the default), directories
28 % with data inconsistent with the current settings are
29 % deleted.
30 %
31 % Parameters:
32 % M : is a matrix of `mu` vectors (column vectors) for which detailed
33 % simulations shall be computed and stored.
34 % savepath: is the directory name relative to 'RBMATLABTEMP' where the
35 % detailed simulations shall be stored.
36 %
37 
38 % Bernard Haasdonk 29.3.2007
39 
40 sp = fullfile(rbmatlabtemp,savepath);
41 
42 if ~isfield(model, 'force_delete')
43  model.force_delete = 1;
44 end
45 
46 % generate directory if not available
47 if ~exist(sp,'dir')
48  disp('data directory does not exist, creating .... ');
49  [p,s,e] = fileparts(sp);
50  mkdir(p,[s,e]);
51  % now it must exist
52  if ~exist(sp,'dir')
53  error('error in generating savepath!');
54  end;
55 elseif exist(fullfile(sp, 'settings.mat'), 'file')
56  % check consistency of file
57  tmp = load(fullfile(sp,'settings.mat'));
58  % fields to skip in comparison
59  ignorelist = {'Mmax','MM','Mstrich','ei_Mmax','k','Mmax','M','N','Nmax',...
60  'RB_error_indicator','RB_stop_Nmax',...
61  'RB_stop_timeout','RB_detailed_test_savepath',...
62  'RB_generation_mode','RB_train_rand_seed','RB_train_size',...
63  'test_N_samples','RB_stop_epsilon','RB_M_val_size',...
64  'RB_numintervals','RB_refinement_mode',...
65  'RB_refinement_theta','RB_max_refinement_level',...
66  'RB_stop_max_val_train_ratio','RB_val_rand_seed',...
67  'ei_stop_on_Mmax','CRB_generation_mode',...
68  'extend_crb', ...
69  'CRB_basis_filename','ei_space_operators',...
70  'Msamples','ei_operator_savepath','ei_target_error',...
71  'ei_numintervals', 'ei_detailed_savepath','verbose','debug', ...
72  'velocity_coefficients_ptr','enable_error_estimator',...
73  'force_delete', 'ei_time_splits', 'adaptive_time_split_mode', ...
74  'ei_stop_epsilon', 'ei_stop_epsilon_first', 'minimum_time_slice', ...
75  'time_split_Mmax'};
76  ignorelist = [ignorelist, model.mu_names,...
77  model.filecache_ignore_fields_in_model];
78 
79  [iseq, a,b,c] = structcmp(model,tmp.model,ignorelist);
80  if ~iseq
81  disp('fields of model and stored model differs:')
82  disp('differing fields:');
83  cellfun(@disp,a);
84  disp('additional fields in model:');
85  cellfun(@disp,b);
86  disp('additional fields in stored model:');
87  cellfun(@disp,c);
88  if ~model.debug && model.force_delete
89  disp(['parameters in precomputed data and current are inconsistent!',...
90  ' I am deleting the old data now!']);
91  delete(fullfile(sp,'*.mat'))
92  ls(sp);
93  else
94  error(['parameters of precomputed data and current',...
95  ' simulation are inconsistent! Please delete files in path ', ...
96  sp, ' and restart!']);
97  end
98  elseif ~isequal(M,tmp.M)
99  if ~model.debug && model.force_delete
100  disp(['M of precomputed and current data is inconsistent!',...
101  ' I am deleting the old data now!']);
102  delete(fullfile(sp,'*.mat'))
103  ls(sp);
104  else
105  error(['M of precomputed data and current',...
106  ' simulation are inconsistent! Please delete and restart!']);
107  end
108  else
109  if exist(fullfile(sp,'UNFINISHED.lock'),'file')
110  if ~model.debug
111  disp(['detected UNFINISHED.lock in target directory, ',...
112  'resuming previous computations...']);
113  else
114  error(['Previous computation of detailed simulations seems not', ...
115  ' to be finished!!! please delete and restart!', ...
116  ' savepath=', sp]);
117  end
118  else
119  disp('skipping detailed computations and using stored results');
120  return;
121  end
122  end
123 else
124  disp(['stray directory found! I am cleaning this one, now!']);
125  delete(fullfile(sp,'*.mat'))
126  ls(sp);
127 end;
128 
129 save(fullfile(sp,'UNFINISHED.lock'),'model');
130 
131 % save general settings
132 save(fullfile(sp,'settings.mat'),'M','model','savepath');
133 
134 if ~isfield(model, 'num_cpus')
135  model.num_cpus = 4;
136 end
137 num_cpus = model.num_cpus;
138 
139 % save simulation_data
140 npar = size(M,2);
141 for mout = 0:floor((npar-1)/num_cpus)
142  sim_data_c = cell(1, npar);
143  tictoc_c = cell(1, npar);
144  parfor mu = (mout*num_cpus+1):min(npar,(mout+1)*num_cpus)
145 
146  filename = fullfile(sp,['detail',num2str(mu),'.mat']);
147  if ~exist(filename, 'file')
148  tmp_model = model;
149  disp(['processing parameter vector ',num2str(mu),'/',num2str(npar)]);
150  %tmp_model = tmp_model.set_mu( model,M(:,mu) );
151  tmp_model = set_mu( tmp_model,M(:,mu) );
152  tic;
153  sim_data_c{mu} = detailed_simulation(tmp_model, model_data);
154  tictoc_c{mu} = toc;
155  else
156  disp(['file exists: skipping parameter vector ', ...
157  num2str(mu), '/', num2str(npar)]);
158  end
159  end;
160  for mu=(mout*num_cpus+1):min(npar,(mout+1)*num_cpus)
161 
162  sim_data = sim_data_c{mu};
163  tictoc = tictoc_c{mu};
164  filename = fullfile(sp,['detail',num2str(mu),'.mat']);
165  if ~exist(filename, 'file')
166  save(filename,'sim_data','tictoc');
167  end
168  end
169 end
170 
171 delete(fullfile(sp,'UNFINISHED.lock'));
172 
173 
function save_detailed_simulations(model, model_data, M, savepath)
perform loop over detailed simulations and save results or check consistency with existing saved resu...