rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
newton.m
Go to the documentation of this file.
1 function [descr, rdescr, dmodel, rmodel] = newton(steps, combined, M_by_N_ratio, noof_ei_extensions, use_laplacian, model_size, random, num_cpus, Mstrich)
2 % function [descr, rdescr, dmodel, rmodel] = newton(steps, combined, M_by_N_ratio, noof_ei_extensions, use_laplacian, model_size, random, num_cpus, Mstrich)
3 % small script demonstrating a buckley leverett problem
4 % discretization and RB model reduction
5 
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 %%%%%% Select here, what is to be performed with the richards data %%%%%%%%
8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9 %step = 1 % single detailed simulation with given data and plot. Run
10  % this with varying parameters mu until sure that scheme
11  % is stable. Modify dt or the data-functions accordingly,
12  % until a nice parameter-domain with uniformly stable
13  % detailed scheme is obtained.
14 %step = 2 % generate colateral reduced basis of L_E operator
15 %step = 3 % compare single detailed simulation with and without
16  % interpolated space operator
17 %step = 4 % generate dummy reduced basis from single trajectory and check, if
18  % ei_interpolation with projection on this space maintains
19  % result. A simple reduced simulation can also be
20  % performed. All results should be visually identical
21 %step = 5 % generate reduced basis
22 %step = 6 % time measurement of reduced simulation and
23  % use reduced basis in rb_demo_gui
24 
25 %steps = [0,2,3,4,5,6]
26 if nargin == 0
27  steps = [0,5,7,8];
28 end
29 
30 if nargin <= 1
31  combined = true;
32  M_by_N_ratio = 0;
33  noof_ei_extensions = 1;
34  use_laplacian = 1;
35  model_size = 'normal';
36  random = false;
37  num_cpus = 4;
38 end
39 
40 if nargin <= 8
41  Mstrich = 1;
42 end
43 
44 warning('off', 'RBMatlab:rb_simulation:runtime');
45 warning('off', 'MATLAB:cholinc:ArgInfToBeRemoved');
46 
47 imsavepath = fullfile(rbmatlabresult, 'images');
48 
49 rmodel = [];
50 
51 for si=1:length(steps)
52 
53 step = steps(si);
54 
55 params.use_laplacian = use_laplacian;
56 %params.model_type = 'buckley_leverett';
57 params.model_type = 'exponential_diffusion';
58 extrapar.Mstrich = 20;
59 
60 params.model_size = model_size;
61 %params.model_type = 'eoc_nonlinear';
62 %params.model_size = 'big';
63 %params.model_size = 'medium';
64 % params.model_size = 'small';
65 % params.model_type = 'nonlinear';
66 
67 params.step7_outputfile = fullfile(rbmatlabresult, ['newton_', params.model_type, '_step7.mat']);
68 params.step8_outputfile = fullfile(rbmatlabresult, ['newton_', params.model_type, '_step8.mat']);
69 
70 % test parameters
71 
72 params.verbose = 6;
73 params.debug = 0;
74 
75 % email to which status messages about processing mu vectors is sent
76 % params.email = 'mdrohmann@uni-muenster.de';
77 
78 %% parameters for visualization
79 plot_params.show_colorbar = 1;
80 plot_params.colorbar_mode = 'EastOutside';
81 plot_params.no_lines = true;
82 plot_params.plot_type = 'contour';
83 % plot_params.clim = [0, 0.5];
84 % plot_params.clim_tight = true;
85 plot_params.plot = @fv_plot;
86 plot_params.bind_to_model = true;
87 plot_params.yscale_uicontrols = 0.6;
88 
89 % output-filenames in rbmatlabresult
90 detailedfname = ['newton_', params.model_type, '_', num2str(Mstrich), '_detailed_interpol.mat'];
91 
92 switch step
93  case 0 % initialize model data
94  if ~isempty(getenv('MACHINEFILE'))
95  machinefile = getenv('MACHINEFILE');
96  FakeMPI.MPI_Init(machinefile, fullfile(rbmatlabhome, 'fakeMPI', 'run_parCompErrs.sh'));
97  FakeMPI.MPI_Run;
98  end
99 
100  descr = newton_oo_model(params);
101  rdescr.rb_problem_type = descr.rb_problem_type;
102  if random
103  rdescr.train_sample_mode = 'random';
104  rdescr.train_num_intervals = 800;
105  rdescr.stop_max_val_train_ratio = inf;
106  end
107 
108  rdescr.stop_timeout = Inf;
109  rdescr.stop_Nmax = 300;
110  rdescr.stop_Mmax = 600;
111  rdescr.stop_epsilon = 1e-4;
112  rdescr.val_size = 20;
113  rdescr.stop_max_val_train_ratio = 1.10;
114  rdescr.ei_minimum_residual = 1e-10;
115  rdescr.extension_M_by_N_r = M_by_N_ratio;
116  rdescr.noof_ei_extensions = noof_ei_extensions;
117  rdescr.maximum_temporary_error_growth_factor = 1e3;
118  rdescr.train_num_intervals = [4,1,2];
119  rdescr.max_refinement_level = 8;
120  rdescr.Mmax_small = 30;
121  rdescr.ei_epsilon_small = 1e-3;
122  if combined
123  if Mstrich >= 1
124  rdescr.stop_Nmax = 111;
125  rdescr.Mmax_small = rdescr.Mmax_small + Mstrich;
126  end
127  else
128  rdescr.train_num_intervals = [7,2,4];
129  rdescr.Mmax_small = 0;
130  if Mstrich == 0
131  rdescr.indicator_mode = 'error';
132  rdescr.stop_Nmax = 100;
133  rdescr.stop_Mmax = 426;
134  rdescr.stop_epsilon = 0;
135  end
136  if Mstrich > 1
137  rdescr.stop_Nmax = 100;
138  end
139  end
140 
141 
142  rdescr.refinement_theta = 0.10;
143 
144  [dmodel, rmodel] = gen_models(descr, rdescr);
145 
146  rmodel.Mstrich = Mstrich;
147  if Mstrich == 0
148  rmodel.enable_error_estimator = false;
149  end
150 
151  dmodel.num_cpus = num_cpus;
152  rmodel.num_cpus = num_cpus;
153 
154  params.mu_ranges = rmodel.mu_ranges;
155  params.mu_names = rmodel.mu_names;
156 
157  model_data = gen_model_data(dmodel);
158  case 1 % single detailed simulation and plot
160  case 1.5
161  tmppar.model_type = 'eoc';
162 
163  model = newton_model(tmppar);
164  model.debug = 1;
165  model.newton_solver = 0;
166  model.T = 1;
167  model.dt = model.T/model.nt;
168  model.verbose = 21;
169  err = zeros(model.nt+1,7);
170  eoc = zeros(4, 1);
171  for i = 1:7
172  model_data = gen_model_data(model);
173 
174  sim_data = detailed_simulation(model, model_data);
175 
176  ffe = @exact_function_heat_equation;
177 
178  exact_data.U = zeros(model_data.grid.nelements, model.nt+1);
179 
180  for tt = 1:model.nt+1
181  model.t = (tt-1) * model.dt;
182  exact_data.U(:,tt) = ffe([model_data.grid.CX(:), model_data.grid.CY(:)], model);
183  end
184  model.t = 0;
185 
186  err(:,i) = fv_l2_error(sim_data.U, exact_data.U, model_data.grid, model);
187  if(i>1)
188  eoc(i-1) = log(max(err(:,i-1))/max(err(:,i)))/log(2);
189  disp(['Error: ', num2str(err(end,i)), 'EOC: ', num2str(eoc(i-1))]);
190  pause;
191  end
192 
193  model.xnumintervals = round(model.xnumintervals.*2);
194  model.ynumintervals = round(model.ynumintervals.*2);
195  end
196  case 1.75
197  tmppar.model_type = 'eoc_nonlinear';
198  tmppar.local_mass_matrix = @fv_local_mass_matrix_rect;
199  tmppar.element_quadrature = @cubequadrature;
200  tmppar.evaluate_basis = @fv_evaluate_basis;
201  tmppar.pdeg = 0;
202 
203  model = newton_model(tmppar);
204  %model.debug = 1;
205  % model.newton_regularisation = 0;
206  %model.verbose = 21;
207  model.dt = model.T/model.nt;
208  model.ynumintervals = 1;
209  err = zeros(model.nt+1,5);
210  eoc = zeros(4, 1);
211  for i = 1:6
212  disp(['Computing EOC step no. ', num2str(i)]);
213  model_data = gen_model_data(model);
214 
215  sim_data = detailed_simulation(model, model_data);
216 
218 
219  width = model_data.grid.X(2) - model_data.grid.X(1);
220 
221  Fe = @(einds, loc, grid, params) ffe([grid.X(einds) + loc(:,1)*width,...
222  grid.Y(einds) + loc(:,2)*width], ...
223  params);
224  tmppar.diff_k0 = model.diff_k0;
225  exact_data.U = zeros(model_data.grid.nelements, model.nt+1);
226  for tt = 1:model.nt+1
227  model.t = (tt-1) * model.dt;
228  exact_data.U(:,tt) = ffe([model_data.grid.CX(:), ...
229  model_data.grid.CY(:)], model);
230 % exact_data.U(:,tt) = l2project(Fe, 2, model_data.grid, tmppar);
231  end
232  model.t = 0;
233 
234  err(:,i) = fv_l2_error(sim_data.U, exact_data.U, model_data.grid, model);
235  if(i>1)
236  eoc(i-1) = log(max(err(:,i-1))/max(err(:,i)))/log(2);
237  disp(['Error: ', num2str(err(end,i)), ' EOC: ', num2str(eoc(i-1))]);
238  end
239 
240  model.xnumintervals = model.xnumintervals.*2;
241  model.nt = model.nt;
242  model.dt = model.T / model.nt;
243  %model.ynumintervals = model.ynumintervals.*2;
244  end
245 
246  case 2 % empirical interpolation of space operator
248  case 3 % compare detailed simulation with and without interpolation
249  model.detailed_ei_simulation = @nonlin_evol_detailed_simulation;
251  case 4 % construct dummy reduced basis by single trajectory and simulate
252  model.detailed_ei_simulation = @nonlin_evol_detailed_simulation;
254  case 5 % reduced basis
255 % load(fullfile(rbmatlabresult,CRBfname));
256 
257  detailed_data = gen_detailed_data(rmodel, model_data);
258  save(fullfile(rbmatlabresult, detailedfname), 'detailed_data', 'rmodel');
259  case 6
260  tmp=load(fullfile(rbmatlabresult,detailedfname));
261  detailed_data=tmp.detailed_data;
262 
264  case 7 % training-error landscape & time comparisons
265  load(fullfile(rbmatlabresult,detailedfname));
266 
267  basis_gen.Mstrich = 0;
268  % number of reduced basis sizes to be tested.
269  Nsize = 7;
270  % number of collateral reduced basis sizes to be tested.
271  Msize = 10;
272  % sample of values for the coupling variable 'c' for which errors are
273  % computed.
274  csample = 0.1:0.1:1;
275 
276  M_by_N_ratio = 1.0;
277  % axis description for plots with coupling variable 'c'
278  cdescr = ['(N,M) = c * (Nmax, ',num2str(M_by_N_ratio), '*Mmax)'];
279  % size of mu vector test set.
280  mu_set_size = 20;
281 
282  error_plots = { 'train_set_coupled', ...
283  'error_coupled' };
284 
285 
287 
288 case 7.5
289  load(params.step7_outputfile);
290  for i = 1:length(output)
291  plot_error_landscape(output{i});
292  end
293 case 8
294  disp('warning: takes a few hours!');
295  load(fullfile(rbmatlabresult,detailedfname));
296 
297  M_by_N_ratio = 1.0;
298 
299  % number of reduced basis sizes to be tested.
300  Nsize = 7;
301  % number of collateral reduced basis sizes to be tested.
302  Msize = 12;
303  % Mstrich values to be tested
304  Mstrich_samples = [1,2,3,5,8,10,15,20];
305  % sample of values for the coupling variable 'c' for which
306  % estimators are computed.
307  csample = 0.1:0.1:1;
308  % size of mu vector test set
309  mu_set_size = 30;
310  % axis description for plots with coupling variable 'c'
311  cdescr = '(N,M) = c * (Nmax, Mmax)';
312 
314 case 8.5
315  load(params.step8_outputfile);
316  for i = 1:length(output)
317  output{i}.bound = 1500;
318  output{i}.stab_limit = 10000;
319  plot_error_landscape(output{i});
320  end
321 case 9
322  load(fullfile(rbmatlabresult,detailedfname));
323  model_data = detailed_data;
324  testdir = 'richards_test_10';
325  dirnames = testdir;
326  model.Nmax = size(detailed_data.RB,2);
327 
328  % generate test data if not existent
329  rand('state',123456);
330  M = rand_uniform(10,params.mu_ranges);
331  save_detailed_simulations(model,model_data,M,testdir)
332 
333  % load demo_nonlin_evol_detailed_data_LE_on_RB;
334  reduced_data = gen_reduced_data(model,detailed_data);
335 
336  settings = load(fullfile(rbmatlabtemp,...
337  testdir,'settings.mat'));
338 
339  Msamples = 12;
340  Nsamples = 7;
341  % storage for maximum l2-errors (i.e. linfty([0,T],l2)-norm)
342  errs = zeros(Msamples,Nsamples);
343  model.RB_error_indicator='ei_estimator_test';
344 
345  Mstrich_set = 1:10:100;%[1,2,3,4,5,10,15,20];
346 
347  deltas = zeros(size(M,2),length(Mstrich_set));
348  for mu_ind = 1:size(M,2);
349  disp(['processing parameter vector ',num2str(mu_ind),...
350  '/',num2str(size(M,2))]);
351  for msi = 1:length(Mstrich_set)
352 
353  model = model.set_mu(model,settings.M(:,mu_ind));
354  sim_data = load_detailed_simulation(mu_ind,settings.savepath,model);
355  U_H = sim_data.U;
356 
357  mtemp = model;
358 
359  mtemp.M = extrapar.M;
360  mtemp.Mstrich = Mstrich_set(msi);
361  mtemp.N = extrapar.N;
362  simulation_data = rb_simulation(mtemp,reduced_data);
363  disp('.');
364 
365  deltas(mu_ind,msi) = simulation_data.Delta(end);
366  simulation_data = rb_reconstruction(mtemp,detailed_data,simulation_data);
367  error = model.error_algorithm(simulation_data.U, sim_data.U, ...
368  detailed_data.grid, mtemp);
369  disp(['true error: ', num2str(max(error))]);
370 
371 % plot_sim_data(model,detailed_data,simulation_data,plot_params);
372 
373  end
374  plot(Mstrich_set,deltas(mu_ind,:));
375  keyboard;
376  end
377 
378 case 9.8
379 % tmp = load(fullfile(rbmatlabresult, 'exponential_laplace', 'newton_exponential_diffusion_detailed_interpol_new'));
380  tmp = load(fullfile(rbmatlabresult, detailedfname));
381  detailed_data = tmp.detailed_data;
382  mu_set = {[1,0.01,0.2],[1,0.01,0.0],[4,0.01,0.2],[4,0.01,0.0]};
383  clim = [0.0, 0.6];
384  plot_params.plot_type = 'contour';
385  plot_params.contour_lines = 7;
386  plot_params.line_width = 2;
387  timeinstants = [0.1,1.0]*model.T;
388  boxed_snapshots = true;
390 case 10
391  %tmp = load(fullfile(rbmatlabresult,'strange.mat'));
392  %Cmap = tmp.Cmap;
393  tmp = load(fullfile(rbmatlabresult,detailedfname));
394  detailed_data = tmp.detailed_data;
395  model = tmp.model;
396  plot_params.no_lines = 1;
397 
398  plot_params.no_axes = 1;
399  plot_params.transparent_background = 1;
400  plot_params.show_colorbar = 0;
401  %plot_params.shrink_factor = 1.13;
402  %plot_params.colormap = Cmap;
403  plot_params.clim = [0.1,0.6];
404 
405  reduced_data = gen_reduced_data(model, detailed_data);
406  model.N = size(detailed_data.RB,2);
407  model.M = size(detailed_data.QM,2);
408  reduced_data = extract_reduced_data_subset(model, reduced_data);
409 
410  cparams.range = cell(length(model.mu_ranges)+1,1);
411  cparams.range{1} = [1, model.nt];
412  cparams.range(2:end) = model.mu_ranges(:);
413  cparams.numintervals = 2*ones(length(model.mu_ranges)+1,1);
414  cgrid = cubegrid(cparams);
415 
416  parammat = cgrid.vertex;
417  parammat(:,1) = round(parammat(:,1));
418 
419 
420  for i=1:length(parammat)
421  tstep = parammat(i,1);
422  newmodel = model.set_mu(model, parammat(i,2:end));
423  rb_sim_data = rb_simulation(newmodel, reduced_data);
424  rb_sim_data = rb_reconstruction(model, detailed_data, rb_sim_data);
425 
426  % transformed version
427  plot_element_data(model_data.grid, rb_sim_data.U(:,tstep), plot_params);
428  Uline = rb_sim_data.U(subline,tstep);
429  mustring = strrep(strrep(strrep(strrep(mat2str(parammat(i,2:end)),' ', '_'),'[',''),']',''),'.','p');
430 
431  fp = fullfile(imsavepath, params.model_type);
432  if ~exist(fp,'dir')
433  mkdir(fp);
434  end
435  fp = fullfile(fp, ['t_', num2str(tstep)]);
436  if ~exist(fp,'dir')
437  mkdir(fp);
438  end
439  fn = ['sample_mu_', mustring];
440  disp(['Processing ', fn, ' ...']);
441 
442  tikzparams.filename = fn;
443  tikzparams.filepath = fp;
444  tikzparams.save_colorbar = 1;
445 
446  plot_as_tikzfile(model, tikzparams);
447 
448  close(gcf);
449  end
450 
451  datfn = 'sublines';
452  print_datatable(fullfile(fp, datafn), subline_title, subline_values);
453 
454 case 11
455  load('richards_affine_detailed_interpol');
456 
457  rdata = gen_reduced_data(model, detailed_data);
458 
459  model.N = size(detailed_data,2);
460  model.M = 10;
461  model.Mstrich = 1;
462  rdata2 = extract_reduced_data_subset(model, rdata);
463  model.Mstrich = 10;
464  rdata1 = extract_reduced_data_subset(model, rdata);
465 
466  TM1 = rdata1.TM_local{1}(1:10);
467  TM2 = rdata2.TM_local{1}(1:10);
468 
469  gl1 = rdata1.grid_local_ext{1};
470  gl2 = rdata2.grid_local_ext{1};
471 
472  if(any(gl1.CX(TM1) ~= gl2.CX(TM2)))
473  disp('CX differs');
474  end
475  if(any(gl1.CY(TM1) ~= gl2.CY(TM2)))
476  disp('CY differs');
477  end
478  if(any(any(gl1.X(gl1.VI(TM1,:)) ~= gl2.X(gl2.VI(TM2,:)))))
479  disp('X differs');
480  end
481  if(any(any(gl1.Y(gl1.VI(TM1,:)) ~= gl2.Y(gl2.VI(TM2,:)))))
482  disp('Y differs');
483  end
484  if(any(any(gl1.SX(TM1,:) ~= gl2.SX(TM2,:))))
485  disp('SX differs');
486  end
487  if(any(any(gl1.SY(TM1,:) ~= gl2.SY(TM2,:))))
488  disp('SY differs');
489  end
490  if(any(gl1.EL(TM1,:) ~= gl2.EL(TM2,:)))
491  disp('EL differs');
492  end
493  if(any(gl1.DC(TM1,:) ~= gl2.DC(TM2,:)))
494  disp('DC differs');
495  end
496  if(any(gl1.NX(TM1,:) ~= gl2.NX(TM2,:)))
497  disp('NX differs');
498  end
499  if(any(gl1.NY(TM1,:) ~= gl2.NY(TM2,:)))
500  disp('NY differs');
501  end
502  if(any(gl1.ECX(TM1,:) ~= gl2.ECX(TM2,:)))
503  disp('ECX differs');
504  end
505  if(any(gl1.ECY(TM1,:) ~= gl2.ECY(TM2,:)))
506  disp('ECY differs');
507  end
508  %% neighbours
509  NBI1 = gl1.NBI(TM1,:);
510  NBI2 = gl2.NBI(TM2,:);
511  if(any(gl1.CX(NBI1) ~= gl2.CX(NBI2)))
512  disp('CX differs');
513  end
514  if(any(gl1.CY(NBI1) ~= gl2.CY(NBI2)))
515  disp('CY differs');
516  end
517  if(any(any(gl1.X(gl1.VI(NBI1,:)) ~= gl2.X(gl2.VI(NBI2,:)))))
518  disp('X differs');
519  end
520  if(any(any(gl1.Y(gl1.VI(NBI1,:)) ~= gl2.Y(gl2.VI(NBI2,:)))))
521  disp('Y differs');
522  end
523  if(any(any(gl1.SX(NBI1,:) ~= gl2.SX(NBI2,:))))
524  disp('SX differs');
525  end
526  if(any(any(gl1.SY(NBI1,:) ~= gl2.SY(NBI2,:))))
527  disp('SY differs');
528  end
529  for i = 1:length(TM1)
530  end
531 
532 case 12 % plot some reduced basis functions
533  tmp = load(fullfile(rbmatlabresult,detailedfname));
534  detailed_data = tmp.detailed_data;
535  model = tmp.model;
536 
537  plot_params.no_lines = 1;
538 
539  plot_params.axis_equal = true;
540  plot_params.plot_type = 'patch';
541  plot_params.no_axes = 1;
542  plot_params.transparent_background = 1;
543  plot_params.show_colorbar = 0;
544 
545  subline = find(detailed_data.grid.CY > 0.6 ...
546  & detailed_data.grid.CY < 0.6+detailed_data.grid.hmin);
547  subline_title = {'X'};
548  subline_values = grid.CX(subline)';
549 
550  for i=1:10
551  plot_element_data(detailed_data.grid, detailed_data.RB(:,i), plot_params);
552  Uline = detailed_data.RB(subline,i);
553  subline_title = [ subline_title, ['rb',num2str(i) ] ];
554  subline_values = [ subline_values; reshape(Uline, 1, length(Uline)) ];
555 
556  fp = fullfile(imsavepath, params.model_type);
557  if ~exist(fp, 'dir')
558  mkdir(fp);
559  end
560  fn = ['rb_function_', num2str(i)];
561  disp(['Processing ', fn, ' ...']);
562 
563  tikzparams.filename = fn;
564  tikzparams.filepath = fp;
565  tikzparams.save_colorbar = 1;
566  tikzparams.width = 3.5;
567  tikzparams.scaleaxis = '';
568 
569  plot_as_tikzfile(model, tikzparams);
570 
571  close(gcf);
572  end
573  datafn = 'rb_sublines.dat';
574  print_datatable(fullfile(fp, datafn), subline_title, subline_values);
575 
576 
577 otherwise
578  error('step-number is unknown!');
579 end
580 
581 end
582 
583 %| \docupdate
584