rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
ei_operator_collect_files.m
Go to the documentation of this file.
1 function [LU_fnames] = ei_operator_collect_files(model, model_data, Mtrain, params)
2 %function [LU_fnames] = ei_operator_collect_files(model,model_data,Mtrain,params)
3 % collects operator evaluations on a sample of snapshots
4 %
5 % This function collects operator evaluations applied to sets of snapshots.
6 % Given a list of operators parameter dependent operators `L_q(\mu) \in {\cal
7 % W}_h, q=1,\ldots,Q` which are `H`-independent Dof dependent as described in
8 % [DOH10] and a set of parameters `M \subset {\cal M} \subset {\cal R}^p`, this
9 % function computes operator evaluations `L_q(\mu)[u_h(\mu;t^k)]` for all `\mu
10 % \in M` and `t^k=k\Delta t`, and stores the results in matlab files.
11 %
12 % parameters:
13 % Mtrain : a matrix specifying the parameter set `M` for which
14 % detailed simulations shall be computed. The column vectors of the
15 % matrix each contain a parameter vector.
16 % params : a structure controlling the behaviour of this function
17 %
18 % required fields of model:
19 % collect_newton_steps : boolean flag, indicating whether the intermediate
20 % newton steps computed by a detailed simulaton with a Newton scheme
21 % shall also be considered when applying the operators
22 % ei_detailed_savepath : path, where the trajectories computed as results
23 % of detailed simulations are stored. This files must contain a
24 % structure named 'sim_data' with a field 'U' of dimesion
25 % '\#DOFs x model.nt +1' or a field 'Unewton' of dimension
26 % '\#DOFs x model.nt + 1 + \#intermediate Newton steps' in case of a
27 % Newton scheme and 'model.collect_newton_steps==true'. The files are
28 % created by save_detailed_simulations() for all parameters given by
29 % 'Mtrain'
30 % ei_operator_savepath : path, where the LU-files are stored. These files
31 % each contain an array named 'LU' of the same dimension as 'sim_data.U'
32 % respectively 'sim_data.Unewton' from the files stored in
33 % 'ei_detailed_savepath'.
34 % separate_CRBs : boolean flag, indicating whether, the operator
35 % evaluations shall be stored in different directories for the later
36 % computation of separate collateral reduced basis spaces for each
37 % operator, or combined a single directory.
38 %
39 % optional fields of model:
40 % force_delete : if this option is set to true (the default), directories
41 % with data inconsistent with the current settings are
42 % deleted.
43 %
44 % required fields of params:
45 % ei_space_operators: cell array of function pointers to spacial operators
46 % `L_q, q=1,\ldots,Q` for which evaluations on snapshots shall be
47 % computed and stored.
48 %
49 % optional fields of params:
50 % num_cpus: number of cpu cores that can be used for parallel
51 % computations. This parameter has only an effect, when Matlab's
52 % parallel computation capabilities are started by initiating a
53 % matlabpool. (default == 4)
54 
55 % paths are to be understood relative to $RBMATLABTEMP
56 
57 % Bernard Haasdonk 15.8.2007
58 
59 % for each mu in Mtrain compute exact detailed trajectory u_H^k(mu)
60 npar = size(Mtrain,2);
61 
62 if (model.verbose > 0)
63  disp(['generating detailed data in ',...
64  fullfile(rbmatlabtemp,model.ei_detailed_savepath)]);
65 end;
66 
67 save_detailed_simulations(model, model_data, Mtrain, model.ei_detailed_savepath);
68 
69 skip_computation = 0;
70 savepath = fullfile(getenv('RBMATLABTEMP'),...
71  model.ei_operator_savepath);
72 
73 if (model.verbose > 0)
74  disp(['generating ei_operator snapshot data in ',...
75  fullfile(rbmatlabtemp,model.ei_operator_savepath)]);
76 end;
77 
78 if ~isfield(model, 'force_delete')
79  model.force_delete = 1;
80 end
81 
82 if ~exist(savepath,'dir');
83  [s,m,id] =mkdir(getenv('RBMATLABTEMP'), ...
84  model.ei_operator_savepath);
85  if ~s
86  error('problem in creating directory!');
87  end;
88 elseif exist(fullfile(savepath,'settings.mat'), 'file')
89  % checking consistency of existing
90  disp('directory exists, checking for correct data.');
91  tmp = load(fullfile(savepath,'settings.mat'));
92 
93  if ~isequal(Mtrain, tmp.Mtrain)
94  if ~model.debug && model.force_delete
95  disp(['Mtrain in precomputed data and current are inconsistent!',...
96  ' I am deleting the old data now!']);
97  delete(fullfile(savepath,'*.mat'))
98  ls(savepath);
99  else
100  error(['Mtrain in precomputed data and current',...
101  ' simulation are inconsistent! Please delete and restart!']);
102  end
103  else
104  % fields to skip in comparison
105  ignorelist = {'Mmax','ei_Mmax','RB_stop_epsilon','RB_stop_Nmax',...
106  'ei_target_error', 'velocity_coefficients_ptr','Mstrich',...
107  model.mu_names{:},'force_delete','RB_error_indicator',...
108  'ei_time_splits','verbose', 'adaptive_time_split_mode', ...
109  'extend_crb', ...
110  'ei_stop_epsilon', 'ei_stop_epsilon_first', ...
111  'minimum_time_slice', 'time_split_Mmax'};
112  ignorelist = [ignorelist, tmp.model.filecache_ignore_fields_in_model];
113  [iseq, a,b,c] = structcmp(model,tmp.model,ignorelist);
114  if ~iseq
115  disp('fields of params and stored params differs:')
116  disp('differing fields:');
117  cellfun(@disp,a);
118  disp('additional fields in params:');
119  cellfun(@disp,b);
120  disp('additional fields in stored params:');
121  cellfun(@disp,c);
122  if ~model.debug && model.force_delete
123  disp(['Mtrain in precomputed data and current are inconsistent!',...
124  ' I am deleting the old data now!']);
125  delete(fullfile(savepath,'*.mat'))
126  ls(savepath);
127  else
128  error(['parameters of precomputed data and current',...
129  ' simulation are inconsistent! Please delete files at path ', ...
130  savepath, ' and restart!']);
131  end
132 
133  else
134  if exist(fullfile(savepath,'UNFINISHED.lock'),'file');
135  disp(['detected UNFINISHED.lock in target directory, ',...
136  'resuming previous computations...']);
137  else
138  disp('skipping ei_operator evaluations on snapshots, as results existing')
139  skip_computation = 1;
140  end
141  end
142  end
143 else
144  disp('stray directory found! I am cleaning this one, now!');
145  delete(fullfile(savepath,'*.mat'))
146  ls(savepath);
147 end
148 
149 save(fullfile(savepath,'UNFINISHED.lock'),'savepath');
150 save(fullfile(savepath,'settings.mat'),'Mtrain','model');
151 
152 LU_fnames = cell(length(params.ei_space_operators),npar);
153 %BU_fnames = cell(length(params.ei_space_operators),npar);
154 
155 % for each of these trajectories,
156 % select time-instants and apply tobe-interpolated operator
157 
158 model.dt = model.T / model.nt;
159 
160 if ~iscell(params.ei_space_operators)
161  params.ei_space_operators = { params.ei_space_operators };
162 end
163 
164 if ~isfield(params, 'num_cpus')
165  params.num_cpus = 4;
166 end
167 num_cpus = params.num_cpus;
168 
169 %if ~isfield(params, 'separate_CRBs')
170 % params.separate_CRBs = false;
171 %end
172 %
173 
174 for oi = 1:length(params.ei_space_operators)
175  for mout = 0:floor((npar-1)/num_cpus)
176  LUc = cell(1, npar);
177  parfor m = (mout*num_cpus+1):min(npar,(mout+1)*num_cpus)
178 % mm = m - mout*num_cpus;
179  tmp_params = params;
180  opname = tmp_params.ei_space_operators{oi};
181  tmp_model_data = model_data;
182  tmp_model = model;
183  oiIdent = num2str(oi);
184  disp(['processing parameter vector ',num2str(m),'/',num2str(npar)]);
185  LU_fnames{oi,m} = fullfile(tmp_model.ei_operator_savepath,...
186  ['LU_',oiIdent,'_',num2str(m),'.mat']);
187 % BU_fnames{oi,m} = fullfile(tmp_model.ei_operator_savepath,...
188 % ['BU_',oiIdent,'_',num2str(m),'.mat']);
189  skip_file = exist(LU_fnames{oi,m},'file');% && exist(BU_fnames{oi,m},'file')
190  if ~skip_computation && ~skip_file
191  % determine initial size of vectors:
192  sim_data = load_detailed_simulation(m,model.ei_detailed_savepath,model);
193  if model.newton_solver && model.collect_newton_steps
194  U = sim_data.Unewton;
195  else
196  U = sim_data.U;
197  end
198  TLU = zeros(size(U,1),0);
199  %TBU = zeros(size(U,1),0);
200  newmodel = model.set_mu(model, Mtrain(:,m));
201  if isfield(model,'model_type') && isequal(newmodel.model_type, 'implicit_nonaffine_linear')
202  clear_all_caches;
203  [tmp_model_data.implicit_operator, tmp_model_data.implicit_constant] = ...
204  model.operators_diff_implicit(newmodel, tmp_model_data, []);
205  clear_all_caches;
206  end
207  if newmodel.newton_solver
208  for tn = 1:size(U,2);
209  fprintf('.');
210  if newmodel.debug && ~newmodel.data_const_in_time
211  error('time dependent data is not yet implemented for newton schemes');
212  end
213  [nu, bu] = opname(newmodel, tmp_model_data, U(:,tn), []);
214  if ~isempty(bu) && tn==1
215  TLU = [TLU, bu];
216  end;
217  TLU = [TLU, nu(:)];
218  end
219  else
220  for tn = 1:length(newmodel.ei_time_indices);
221  fprintf('.');
222  ti = newmodel.ei_time_indices(tn);
223  % note the time-shift by 1 in the following (ti=1 is initial data)
224  newmodel.t = (ti-1)*newmodel.dt;
225  newmodel.tstep = tn;
226  [nu, bu] = opname(newmodel, tmp_model_data, U(:,ti), []);
227  if ~isempty(bu) && tn==1
228  % TBU = [TBU, bu];
229  TLU = [TLU, bu];
230  % LU = TBU;
231  % save(fullfile(rbmatlabtemp,BU_fnames{oi,m}),'LU');
232  end;
233  TLU = [TLU, nu(:)];
234  end
235  end
236  fprintf('\n');
237  LUc{m} = TLU;
238  end
239  end
240  if ~skip_computation
241  for m = (mout*num_cpus+1):min(npar,(mout+1)*num_cpus)
242  skip_file = exist(LU_fnames{oi,m},'file');% && exist(BU_fnames{oi,m},'file')
243  if ~skip_file
244  % mm = m - mout*num_cpus;
245  LU = LUc{m};
246  save(fullfile(rbmatlabtemp,LU_fnames{oi,m}),'LU');
247  end
248  end
249  end
250  end
251 end
252 
253 if ~model.separate_CRBs
254  LU_fnames = reshape(LU_fnames, 1, size(LU_fnames,1)*size(LU_fnames,2));
255 end
256 
257 if exist(fullfile(savepath,'UNFINISHED.lock'),'file');
258  delete(fullfile(savepath,'UNFINISHED.lock'));
259 end;
260 
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...
Definition: verbose.m:17
function save_detailed_simulations(model, model_data, M, savepath)
perform loop over detailed simulations and save results or check consistency with existing saved resu...
function LU_fnames = ei_operator_collect_files(model, model_data, Mtrain, params)
collects operator evaluations on a sample of snapshots
function [ descr , rdescr , dmodel , rmodel ] = newton(steps, combined, M_by_N_ratio, noof_ei_extensions, use_laplacian, model_size, random, num_cpus, Mstrich)
small script demonstrating a buckley leverett problem discretization and RB model reduction ...
Definition: newton.m:17