rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
richards_fv.m
Go to the documentation of this file.
1 % small script demonstrating the richards equation with explicit fv
2 % discretization and RB model reduction
3 % Bernard Haasdonk 14.8.2007
4 
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 %%%%%% Select here, what is to be performed with the richards data %%%%%%%%
7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 %step = 1 % single detailed simulation with given data and plot. Run
9  % this with varying parameters mu until sure that scheme
10  % is stable. Modify dt or the data-functions accordingly,
11  % until a nice parameter-domain with uniformly stable
12  % detailed scheme is obtained.
13 %step = 2 % generate colateral reduced basis of L_E operator
14 %step = 3 % compare single detailed simulation with and without
15  % interpolated space operator
16 %step = 4 % generate dummy reduced basis from single trajectory and check, if
17  % ei_interpolation with projection on this space maintains
18  % result. A simple reduced simulation can also be
19  % performed. All results should be visually identical
20 %step = 5 % generate reduced basis
21 %step = 6 % time measurement of reduced simulation and
22  % use reduced basis in rb_demo_gui
23 %step = 7 % generate error-landscape over varying N and M
24  % can take several hours!!!
25 %step = 8 % do runtime comparisons between detailed and reduced simulations
26 %step = 10 % write out some nice pictures
27 
28 steps = {0,1}
29 %steps = {0,1}
30 
31 imsavepath = '/data/dune_work/private/m_droh01/images';
32 
33 for si=1:length(steps)
34 
35 step = steps{si};
36 
37 % params.model_type = 'implicit_nonaffine_linear';
38 params.model_type = 'linear_heat_trapezoidal';
39 %params.model_size = 'big';
40 params.model_size = 'small';
41 % params.model_size = 'small';
42 % params.model_type = 'nonlinear';
43 params.separate_CRBs = false;
44 
45 % test parameters
46 params.RB_detailed_test_savepath = 'test_data_100';
47 params.RB_test_size = 100;
48 
49 params.verbose = 5;
50 params.debug = 0;
51 params.step7_outputfile = fullfile(rbmatlabresult, [params.model_type, '_step7_output']);
52 params.step8_outputfile = fullfile(rbmatlabresult, [params.model_type, '_step8_output']);
53 extrapar.Mstrich = 20;
54 extrapar.N = 25;
55 extrapar.M = 50;
56 
57 % email to which status messages about processing mu vectors is sent
58 % params.email = 'mdrohmann@uni-muenster.de';
59 
60 %% parameters for visualization
61 plot_params.show_colorbar = 1;
62 plot_params.colorbar_mode = 'EastOutside';
63 plot_params.no_lines = true;
64 % plot_params.clim = [0, 0.5];
65 % plot_params.clim_tight = true;
66 plot_params.plot = @fv_plot;
67 plot_params.bind_to_model = true;
68 plot_params.yscale_uicontrols = 0.6;
69 
70 % output-filenames in rbmatlabresult
71 CRBfname = ['richards_fv_', params.model_type, '_CRB_interpol.mat'];
72 detailedfname = ['richards_fv_', params.model_type, '_detailed_interpol.mat'];
73 
74 switch step
75  case 0 % initialize model data
76  model = richards_fv_model(params);
77  params.mu_ranges = model.mu_ranges;
78  params.mu_names = model.mu_names;
79  mu_default = cellfun(@mean, model.mu_ranges);
80 
81  model_data = gen_model_data(model);
82  case 1 % single detailed simulation and plot
84  case 2 % empirical interpolation of space operator
86  case 3 % compare detailed simulation with and without interpolation
87  load(fullfile(rbmatlabresult,CRBfname));
89  case 4 % construct dummy reduced basis by single trajectory and simulate
90  tmp=load(fullfile(rbmatlabresult,CRBfname));
91  detailed_data=tmp.detailed_data;
93  case 5 % reduced basis
94  load(fullfile(rbmatlabresult,CRBfname));
96  case 6
97  load(fullfile(rbmatlabresult,detailedfname));
99  case 7 % training-error landscape & time comparisons
100  load(fullfile(rbmatlabresult,detailedfname));
101 
102  model.Mstrich = 0;
103  % maximum number of reduced basis functions
104  model.Nmax = size(detailed_data.RB,2);
105  % maximum number of collateral reduced basis functions
106  model.Mmax = size(detailed_data.BM{1},2);
107  % number of reduced basis sizes to be tested.
108  Nsize = 7;
109  % number of collateral reduced basis sizes to be tested.
110  Msize = 12;
111  % sample of values for the coupling variable 'c' for which errors are
112  % computed.
113  csample = 0.1:0.1:1;
114  % axis description for plots with coupling variable 'c'
115  cdescr = '(N,M) = c * (Nmax, Mmax)';
116  % size of mu vector test set.
117  mu_set_size = 20;
118 
120 
121 case 7.5
122  load(params.step7_outputfile);
123  for i = 1:length(output)
124  plot_error_landscape(output{i});
125  end
126 case 8
127  disp('warning: takes a few hours!');
128  load(fullfile(rbmatlabresult,detailedfname));
129 
130  % maximum number of reduced basis vectors
131  model.Nmax = model.RB_stop_Nmax;
132  % maximum number of collateral reduced basis vectors used for
133  model.Mmax = size( detailed_data.BM{1}, 2 ) - extrapar.Mstrich;
134 
135  % number of reduced basis sizes to be tested.
136  Nsize = 7;
137  % number of collateral reduced basis sizes to be tested.
138  Msize = 12;
139  % Mstrich values to be tested
140  Mstrich_samples = 1:extrapar.Mstrich;
141  % sample of values for the coupling variable 'c' for which
142  % estimators are computed.
143  csamples = 0.1:0.1:1;
144  % size of mu vector test set
145  mu_set_size = 20;
146  % axis description for plots with coupling variable 'c'
147  cdescr = '(N,M) = c * (Nmax, Mmax)';
148 
150 case 8.5
151  load(params.step8_outputfile);
152  for i = 1:length(output)
153  output{i}.bound = 1500;
154  output{i}.stab_limit = 10000;
155  plot_error_landscape(output{i});
156  end
157 case 9
158  load(fullfile(rbmatlabresult,detailedfname));
159  model_data = detailed_data;
160  testdir = 'richards_test_10';
161  dirnames = testdir;
162  model.Nmax = size(detailed_data.RB,2);
163 
164  % generate test data if not existent
165  rand('state',123456);
166  M = rand_uniform(10,params.mu_ranges);
167  save_detailed_simulations(model,model_data,M,testdir)
168 
169  % load demo_nonlin_evol_detailed_data_LE_on_RB;
170  reduced_data = gen_reduced_data(model,detailed_data);
171 
172  settings = load(fullfile(rbmatlabtemp,...
173  testdir,'settings.mat'));
174 
175  Msamples = 12;
176  Nsamples = 7;
177  % storage for maximum l2-errors (i.e. linfty([0,T],l2)-norm)
178  errs = zeros(Msamples,Nsamples);
179  model.RB_error_indicator='ei_estimator_test';
180 
181  Mstrich_set = 1:10:100;%[1,2,3,4,5,10,15,20];
182 
183  deltas = zeros(size(M,2),length(Mstrich_set));
184  for mu_ind = 1:size(M,2);
185  disp(['processing parameter vector ',num2str(mu_ind),...
186  '/',num2str(size(M,2))]);
187  for msi = 1:length(Mstrich_set)
188 
189  model = model.set_mu(model,settings.M(:,mu_ind));
190  sim_data = load_detailed_simulation(mu_ind,settings.savepath,model);
191  U_H = sim_data.U;
192 
193  mtemp = model;
194 
195  mtemp.M = extrapar.M;
196  mtemp.Mstrich = Mstrich_set(msi);
197  mtemp.N = extrapar.N;
198  simulation_data = rb_simulation(mtemp,reduced_data);
199  disp('.');
200 
201  deltas(mu_ind,msi) = simulation_data.Delta(end);
202  simulation_data = rb_reconstruction(mtemp,detailed_data,simulation_data);
203  error = model.error_algorithm(simulation_data.U, sim_data.U, ...
204  detailed_data.W, mtemp);
205  disp(['true error: ', num2str(max(error))]);
206 
207 % plot_sim_data(model,detailed_data,simulation_data,plot_params);
208 
209  end
210  plot(Mstrich_set,deltas(mu_ind,:));
211  keyboard;
212  end
213 case 10
214  load('datafiles/strange.mat');
215 
216  tmp = load(fullfile(rbmatlabtemp,detailedfname));
217  detailed_data = tmp.detailed_data;
218  plot_params.no_lines = 1;
219 
220  plot_params.no_axes = 1;
221  plot_params.transparent_background = 1;
222  plot_params.show_colorbar = 0;
223  %plot_params.shrink_factor = 1.13;
224  plot_params.colormap = Cmap;
225  plot_params.clim = [0.0,0.35];
226 
227  reduced_data = gen_reduced_data(model, detailed_data);
228  params.N = 14;%size(detailed_data.RB,2);
229  params.M = 100;%size(detailed_data.QM,2);
230  reduced_data = extract_reduced_data_subset(model, reduced_data, params);
231 
232  model.hill_height = 0;
233 
234  cparams.range = cell(length(model.mu_ranges)+1,1);
235  cparams.range{1} = [1, model.nt];
236  cparams.range(2:end) = model.mu_ranges(:);
237  cparams.numintervals = 2*ones(length(model.mu_ranges)+1,1);
238  cgrid = cubegrid(cparams);
239 
240  parammat = cgrid.vertex;
241  parammat(:,1) = round(parammat(:,1));
242 
243  for i=1:length(parammat)
244  tstep = parammat(i,1);
245  newmodel = newmodel.set_mu(model, parammat(i,2:end));
246  rb_sim_data = rb_simulation(newmodel, reduced_data, params);
247  rb_sim_data = rb_reconstruction(model, detailed_data, rb_sim_data);
248 
249  % transformed version
250  plot_element_data(newmodel, model_data.grid, rb_sim_data.U(:,tstep), plot_params);
251 
252  mustring = strrep(strrep(strrep(strrep(mat2str(parammat(i,2:end)),' ', '_'),'[',''),']',''),'.','p');
253  fp = fullfile(imsavepath, params.model_type);
254  if ~exist(fp,'dir')
255  mkdir(fp);
256  end
257  fp = fullfile(fp, ['t_', num2str(tstep)]);
258  if ~exist(fp,'dir')
259  mkdir(fp);
260  end
261  fn = ['sample_mu_', mustring];
262  disp(['Processing ', fn, ' ...']);
263 
264  set(gcf,'Color','none');
265  set(gca,'Color','none');
266  set(gcf,'InvertHardCopy','off');
267  showaxes('hide');
268  ranges=cell(2,1);
269  ranges{1} = get(gca,'XLim');
270  ranges{2} = get(gca,'YLim');
271 
272  export_fig(fullfile(fp, fn), '-pdf','-eps','-png');
273  im = export_fig;
274 
275  colorbarfn = fullfile(imsavepath, params.model_type, 'colorbar');
276  if ~exist([colorbarfn, '.pdf'], 'file')
277  colorh = colorbar;
278  yticks = get(colorh, 'YTick');
279  set(colorh, 'YTick',[]);
280  set(colorh, 'YTickLabel',[]);
281 
282  export_fig(colorbarfn, '-pdf', '-eps', '-png', colorh);
283  cbim = export_fig(colorh);
284  size(cbim)
285  end
286 
287  tikzfile(newmodel, fp, fn, ranges, size(im), 5);
288  close(gcf);
289 
290  % untransformed version
291  fpu = fullfile(imsavepath, params.model_type, 'untransformed');
292  if ~exist(fpu,'dir')
293  mkdir(fpu);
294  end
295  fpu = fullfile(fpu, ['t_', num2str(tstep)]);
296  if ~exist(fpu,'dir')
297  mkdir(fpu);
298  end
299 
300  plot_element_data(model, model_data.grid, rb_sim_data.U(:,tstep), plot_params);
301 
302  set(gcf,'Color','none');
303  set(gca,'Color','none');
304  set(gcf,'InvertHardCopy','off');
305  showaxes('hide');
306  ranges{1} = get(gca,'XLim');
307  ranges{2} = get(gca,'YLim');
308 
309  export_fig(fullfile(fpu, fn), '-pdf', '-eps', '-png');
310  im = export_fig;
311  tikzfile(newmodel, fpu, fn, ranges, size(im), 5);
312  close(gcf);
313  end
314 
315 case 11
316  load('richards_affine_detailed_interpol');
317 
318  rdata = gen_reduced_data(model, detailed_data);
319 
320  model.N = size(detailed_data,2);
321  model.M = 10;
322  model.Mstrich = 1;
323  rdata2 = extract_reduced_data_subset(model, rdata);
324  model.Mstrich = 10;
325  rdata1 = extract_reduced_data_subset(model, rdata);
326 
327  TM1 = rdata1.TM_local{1}(1:10);
328  TM2 = rdata2.TM_local{1}(1:10);
329 
330  gl1 = rdata1.grid_local_ext{1};
331  gl2 = rdata2.grid_local_ext{1};
332 
333  if(any(gl1.CX(TM1) ~= gl2.CX(TM2)))
334  disp('CX differs');
335  end
336  if(any(gl1.CY(TM1) ~= gl2.CY(TM2)))
337  disp('CY differs');
338  end
339  if(any(any(gl1.X(gl1.VI(TM1,:)) ~= gl2.X(gl2.VI(TM2,:)))))
340  disp('X differs');
341  end
342  if(any(any(gl1.Y(gl1.VI(TM1,:)) ~= gl2.Y(gl2.VI(TM2,:)))))
343  disp('Y differs');
344  end
345  if(any(any(gl1.SX(TM1,:) ~= gl2.SX(TM2,:))))
346  disp('SX differs');
347  end
348  if(any(any(gl1.SY(TM1,:) ~= gl2.SY(TM2,:))))
349  disp('SY differs');
350  end
351  if(any(gl1.EL(TM1,:) ~= gl2.EL(TM2,:)))
352  disp('EL differs');
353  end
354  if(any(gl1.DC(TM1,:) ~= gl2.DC(TM2,:)))
355  disp('DC differs');
356  end
357  if(any(gl1.NX(TM1,:) ~= gl2.NX(TM2,:)))
358  disp('NX differs');
359  end
360  if(any(gl1.NY(TM1,:) ~= gl2.NY(TM2,:)))
361  disp('NY differs');
362  end
363  if(any(gl1.ECX(TM1,:) ~= gl2.ECX(TM2,:)))
364  disp('ECX differs');
365  end
366  if(any(gl1.ECY(TM1,:) ~= gl2.ECY(TM2,:)))
367  disp('ECY differs');
368  end
369  %% neighbours
370  NBI1 = gl1.NBI(TM1,:);
371  NBI2 = gl2.NBI(TM2,:);
372  if(any(gl1.CX(NBI1) ~= gl2.CX(NBI2)))
373  disp('CX differs');
374  end
375  if(any(gl1.CY(NBI1) ~= gl2.CY(NBI2)))
376  disp('CY differs');
377  end
378  if(any(any(gl1.X(gl1.VI(NBI1,:)) ~= gl2.X(gl2.VI(NBI2,:)))))
379  disp('X differs');
380  end
381  if(any(any(gl1.Y(gl1.VI(NBI1,:)) ~= gl2.Y(gl2.VI(NBI2,:)))))
382  disp('Y differs');
383  end
384  if(any(any(gl1.SX(NBI1,:) ~= gl2.SX(NBI2,:))))
385  disp('SX differs');
386  end
387  if(any(any(gl1.SY(NBI1,:) ~= gl2.SY(NBI2,:))))
388  disp('SY differs');
389  end
390  for i = 1:length(TM1)
391  end
392 
393 otherwise
394  error('step-number is unknown!');
395 end
396 
397 end
398 
399 %| \docupdate
400