rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules 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
83  dmodel = model;
85  case 2 % empirical interpolation of space operator
87  case 3 % compare detailed simulation with and without interpolation
88  load(fullfile(rbmatlabresult,CRBfname));
90  case 4 % construct dummy reduced basis by single trajectory and simulate
91  tmp=load(fullfile(rbmatlabresult,CRBfname));
92  detailed_data=tmp.detailed_data;
94  case 5 % reduced basis
95  load(fullfile(rbmatlabresult,CRBfname));
97  case 6
98  load(fullfile(rbmatlabresult,detailedfname));
100  case 7 % training-error landscape & time comparisons
101  load(fullfile(rbmatlabresult,detailedfname));
102 
103  model.Mstrich = 0;
104  % maximum number of reduced basis functions
105  model.Nmax = size(detailed_data.RB,2);
106  % maximum number of collateral reduced basis functions
107  model.Mmax = size(detailed_data.BM{1},2);
108  % number of reduced basis sizes to be tested.
109  Nsize = 7;
110  % number of collateral reduced basis sizes to be tested.
111  Msize = 12;
112  % sample of values for the coupling variable 'c' for which errors are
113  % computed.
114  csample = 0.1:0.1:1;
115  % axis description for plots with coupling variable 'c'
116  cdescr = '(N,M) = c * (Nmax, Mmax)';
117  % size of mu vector test set.
118  mu_set_size = 20;
119 
121 
122 case 7.5
123  load(params.step7_outputfile);
124  for i = 1:length(output)
125  plot_error_landscape(output{i});
126  end
127 case 8
128  disp('warning: takes a few hours!');
129  load(fullfile(rbmatlabresult,detailedfname));
130 
131  % maximum number of reduced basis vectors
132  model.Nmax = model.RB_stop_Nmax;
133  % maximum number of collateral reduced basis vectors used for
134  model.Mmax = size( detailed_data.BM{1}, 2 ) - extrapar.Mstrich;
135 
136  % number of reduced basis sizes to be tested.
137  Nsize = 7;
138  % number of collateral reduced basis sizes to be tested.
139  Msize = 12;
140  % Mstrich values to be tested
141  Mstrich_samples = 1:extrapar.Mstrich;
142  % sample of values for the coupling variable 'c' for which
143  % estimators are computed.
144  csamples = 0.1:0.1:1;
145  % size of mu vector test set
146  mu_set_size = 20;
147  % axis description for plots with coupling variable 'c'
148  cdescr = '(N,M) = c * (Nmax, Mmax)';
149 
151 case 8.5
152  load(params.step8_outputfile);
153  for i = 1:length(output)
154  output{i}.bound = 1500;
155  output{i}.stab_limit = 10000;
156  plot_error_landscape(output{i});
157  end
158 case 9
159  load(fullfile(rbmatlabresult,detailedfname));
160  model_data = detailed_data;
161  testdir = 'richards_test_10';
162  dirnames = testdir;
163  model.Nmax = size(detailed_data.RB,2);
164 
165  % generate test data if not existent
166  rand('state',123456);
167  M = rand_uniform(10,params.mu_ranges);
168  save_detailed_simulations(model,model_data,M,testdir)
169 
170  % load demo_nonlin_evol_detailed_data_LE_on_RB;
171  reduced_data = gen_reduced_data(model,detailed_data);
172 
173  settings = load(fullfile(rbmatlabtemp,...
174  testdir,'settings.mat'));
175 
176  Msamples = 12;
177  Nsamples = 7;
178  % storage for maximum l2-errors (i.e. linfty([0,T],l2)-norm)
179  errs = zeros(Msamples,Nsamples);
180  model.RB_error_indicator='ei_estimator_test';
181 
182  Mstrich_set = 1:10:100;%[1,2,3,4,5,10,15,20];
183 
184  deltas = zeros(size(M,2),length(Mstrich_set));
185  for mu_ind = 1:size(M,2);
186  disp(['processing parameter vector ',num2str(mu_ind),...
187  '/',num2str(size(M,2))]);
188  for msi = 1:length(Mstrich_set)
189 
190  model = model.set_mu(model,settings.M(:,mu_ind));
191  sim_data = load_detailed_simulation(mu_ind,settings.savepath,model);
192  U_H = sim_data.U;
193 
194  mtemp = model;
195 
196  mtemp.M = extrapar.M;
197  mtemp.Mstrich = Mstrich_set(msi);
198  mtemp.N = extrapar.N;
199  simulation_data = rb_simulation(mtemp,reduced_data);
200  disp('.');
201 
202  deltas(mu_ind,msi) = simulation_data.Delta(end);
203  simulation_data = rb_reconstruction(mtemp,detailed_data,simulation_data);
204  error = model.error_algorithm(simulation_data.U, sim_data.U, ...
205  detailed_data.W, mtemp);
206  disp(['true error: ', num2str(max(error))]);
207 
208 % plot_sim_data(model,detailed_data,simulation_data,plot_params);
209 
210  end
211  plot(Mstrich_set,deltas(mu_ind,:));
212  keyboard;
213  end
214 case 10
215  load('datafiles/strange.mat');
216 
217  tmp = load(fullfile(rbmatlabtemp,detailedfname));
218  detailed_data = tmp.detailed_data;
219  plot_params.no_lines = 1;
220 
221  plot_params.no_axes = 1;
222  plot_params.transparent_background = 1;
223  plot_params.show_colorbar = 0;
224  %plot_params.shrink_factor = 1.13;
225  plot_params.colormap = Cmap;
226  plot_params.clim = [0.0,0.35];
227 
228  reduced_data = gen_reduced_data(model, detailed_data);
229  params.N = 14;%size(detailed_data.RB,2);
230  params.M = 100;%size(detailed_data.QM,2);
231  reduced_data = extract_reduced_data_subset(model, reduced_data, params);
232 
233  model.hill_height = 0;
234 
235  cparams.range = cell(length(model.mu_ranges)+1,1);
236  cparams.range{1} = [1, model.nt];
237  cparams.range(2:end) = model.mu_ranges(:);
238  cparams.numintervals = 2*ones(length(model.mu_ranges)+1,1);
239  cgrid = cubegrid(cparams);
240 
241  parammat = cgrid.vertex;
242  parammat(:,1) = round(parammat(:,1));
243 
244  for i=1:length(parammat)
245  tstep = parammat(i,1);
246  newmodel = newmodel.set_mu(model, parammat(i,2:end));
247  rb_sim_data = rb_simulation(newmodel, reduced_data, params);
248  rb_sim_data = rb_reconstruction(model, detailed_data, rb_sim_data);
249 
250  % transformed version
251  plot_element_data(newmodel, model_data.grid, rb_sim_data.U(:,tstep), plot_params);
252 
253  mustring = strrep(strrep(strrep(strrep(mat2str(parammat(i,2:end)),' ', '_'),'[',''),']',''),'.','p');
254  fp = fullfile(imsavepath, params.model_type);
255  if ~exist(fp,'dir')
256  mkdir(fp);
257  end
258  fp = fullfile(fp, ['t_', num2str(tstep)]);
259  if ~exist(fp,'dir')
260  mkdir(fp);
261  end
262  fn = ['sample_mu_', mustring];
263  disp(['Processing ', fn, ' ...']);
264 
265  set(gcf,'Color','none');
266  set(gca,'Color','none');
267  set(gcf,'InvertHardCopy','off');
268  showaxes('hide');
269  ranges=cell(2,1);
270  ranges{1} = get(gca,'XLim');
271  ranges{2} = get(gca,'YLim');
272 
273  export_fig(fullfile(fp, fn), '-pdf','-eps','-png');
274  im = export_fig;
275 
276  colorbarfn = fullfile(imsavepath, params.model_type, 'colorbar');
277  if ~exist([colorbarfn, '.pdf'], 'file')
278  colorh = colorbar;
279  yticks = get(colorh, 'YTick');
280  set(colorh, 'YTick',[]);
281  set(colorh, 'YTickLabel',[]);
282 
283  export_fig(colorbarfn, '-pdf', '-eps', '-png', colorh);
284  cbim = export_fig(colorh);
285  size(cbim)
286  end
287 
288  tikzfile(newmodel, fp, fn, ranges, size(im), 5);
289  close(gcf);
290 
291  % untransformed version
292  fpu = fullfile(imsavepath, params.model_type, 'untransformed');
293  if ~exist(fpu,'dir')
294  mkdir(fpu);
295  end
296  fpu = fullfile(fpu, ['t_', num2str(tstep)]);
297  if ~exist(fpu,'dir')
298  mkdir(fpu);
299  end
300 
301  plot_element_data(model, model_data.grid, rb_sim_data.U(:,tstep), plot_params);
302 
303  set(gcf,'Color','none');
304  set(gca,'Color','none');
305  set(gcf,'InvertHardCopy','off');
306  showaxes('hide');
307  ranges{1} = get(gca,'XLim');
308  ranges{2} = get(gca,'YLim');
309 
310  export_fig(fullfile(fpu, fn), '-pdf', '-eps', '-png');
311  im = export_fig;
312  tikzfile(newmodel, fpu, fn, ranges, size(im), 5);
313  close(gcf);
314  end
315 
316 case 11
317  load('richards_affine_detailed_interpol');
318 
319  rdata = gen_reduced_data(model, detailed_data);
320 
321  model.N = size(detailed_data,2);
322  model.M = 10;
323  model.Mstrich = 1;
324  rdata2 = extract_reduced_data_subset(model, rdata);
325  model.Mstrich = 10;
326  rdata1 = extract_reduced_data_subset(model, rdata);
327 
328  TM1 = rdata1.TM_local{1}(1:10);
329  TM2 = rdata2.TM_local{1}(1:10);
330 
331  gl1 = rdata1.grid_local_ext{1};
332  gl2 = rdata2.grid_local_ext{1};
333 
334  if(any(gl1.CX(TM1) ~= gl2.CX(TM2)))
335  disp('CX differs');
336  end
337  if(any(gl1.CY(TM1) ~= gl2.CY(TM2)))
338  disp('CY differs');
339  end
340  if(any(any(gl1.X(gl1.VI(TM1,:)) ~= gl2.X(gl2.VI(TM2,:)))))
341  disp('X differs');
342  end
343  if(any(any(gl1.Y(gl1.VI(TM1,:)) ~= gl2.Y(gl2.VI(TM2,:)))))
344  disp('Y differs');
345  end
346  if(any(any(gl1.SX(TM1,:) ~= gl2.SX(TM2,:))))
347  disp('SX differs');
348  end
349  if(any(any(gl1.SY(TM1,:) ~= gl2.SY(TM2,:))))
350  disp('SY differs');
351  end
352  if(any(gl1.EL(TM1,:) ~= gl2.EL(TM2,:)))
353  disp('EL differs');
354  end
355  if(any(gl1.DC(TM1,:) ~= gl2.DC(TM2,:)))
356  disp('DC differs');
357  end
358  if(any(gl1.NX(TM1,:) ~= gl2.NX(TM2,:)))
359  disp('NX differs');
360  end
361  if(any(gl1.NY(TM1,:) ~= gl2.NY(TM2,:)))
362  disp('NY differs');
363  end
364  if(any(gl1.ECX(TM1,:) ~= gl2.ECX(TM2,:)))
365  disp('ECX differs');
366  end
367  if(any(gl1.ECY(TM1,:) ~= gl2.ECY(TM2,:)))
368  disp('ECY differs');
369  end
370  %% neighbours
371  NBI1 = gl1.NBI(TM1,:);
372  NBI2 = gl2.NBI(TM2,:);
373  if(any(gl1.CX(NBI1) ~= gl2.CX(NBI2)))
374  disp('CX differs');
375  end
376  if(any(gl1.CY(NBI1) ~= gl2.CY(NBI2)))
377  disp('CY differs');
378  end
379  if(any(any(gl1.X(gl1.VI(NBI1,:)) ~= gl2.X(gl2.VI(NBI2,:)))))
380  disp('X differs');
381  end
382  if(any(any(gl1.Y(gl1.VI(NBI1,:)) ~= gl2.Y(gl2.VI(NBI2,:)))))
383  disp('Y differs');
384  end
385  if(any(any(gl1.SX(NBI1,:) ~= gl2.SX(NBI2,:))))
386  disp('SX differs');
387  end
388  if(any(any(gl1.SY(NBI1,:) ~= gl2.SY(NBI2,:))))
389  disp('SY differs');
390  end
391  for i = 1:length(TM1)
392  end
393 
394 otherwise
395  error('step-number is unknown!');
396 end
397 
398 end
399 
400 %| \docupdate
401 
function [ sim_data , tictoc ] = load_detailed_simulation(m, savepath, params)
load single trajectory of previously saved results.
function step1_detailed_simulation()
script performing a single detailed simulation and plotting it.
function plot_error_landscape(output)
plots an output structure generated by stochastic_error_estimation()
A hierarchical cubegrid of arbitrary dimension.
Definition: cubegrid.m:17
function p = fv_plot(gridbase grid, dofs, params)
routine plotting a single fv function of fv_functions.
Definition: fv_plot.m:17
function step2_empirical_interpolation()
script constructing a collateral reduced basis space for localized space operators.
function step4_dummy_reduced_basis()
script constructing a reduced basis from a single trajectory and performing a detailed simulation whe...
function step7_error_landscape()
script generating error landscapes by computing the true error of reduced simulations vs...
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 step8_estimator_landscape()
script generating landscapes plot data by computing the error estimator of reduced simulations for gi...
function step6_demo_rb_gui()
script comparing time for a reduced and a detailed simulation and starting the demonstration GUI ...
function step3_detailed_ei_simulation()
script performing a detailed simulation with empirical interpolated operators comparing the result wi...
function p = plot_sim_data(model, model_data, sim_data, plot_params)
function performing the plot of the simulation results as specified in model.
Definition: plot_sim_data.m:17
function step5_rb_generation()
script constructing a reduced basis space