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
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
25 %steps = [0,2,3,4,5,6]
33 noof_ei_extensions = 1;
35 model_size =
'normal';
44 warning(
'off',
'RBMatlab:rb_simulation:runtime');
45 warning(
'off',
'MATLAB:cholinc:ArgInfToBeRemoved');
47 imsavepath = fullfile(rbmatlabresult,
'images');
51 for si=1:length(steps)
55 params.use_laplacian = use_laplacian;
57 params.model_type = 'exponential_diffusion';
58 extrapar.Mstrich = 20;
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';
67 params.step7_outputfile = fullfile(rbmatlabresult, ['newton_', params.model_type, '_step7.mat']);
68 params.step8_outputfile = fullfile(rbmatlabresult, ['newton_', params.model_type, '_step8.mat']);
75 % email to which status messages about processing mu vectors is sent
76 % params.email = 'mdrohmann@uni-muenster.de';
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;
86 plot_params.bind_to_model = true;
87 plot_params.yscale_uicontrols = 0.6;
89 % output-filenames in rbmatlabresult
90 detailedfname = ['newton_', params.model_type, '_', num2str(Mstrich), '_detailed_interpol.mat'];
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'));
100 descr = newton_oo_model(params);
101 rdescr.rb_problem_type = descr.rb_problem_type;
103 rdescr.train_sample_mode = 'random';
104 rdescr.train_num_intervals = 800;
105 rdescr.stop_max_val_train_ratio = inf;
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;
124 rdescr.stop_Nmax = 111;
125 rdescr.Mmax_small = rdescr.Mmax_small + Mstrich;
128 rdescr.train_num_intervals = [7,2,4];
129 rdescr.Mmax_small = 0;
131 rdescr.indicator_mode = 'error';
132 rdescr.stop_Nmax = 100;
133 rdescr.stop_Mmax = 426;
134 rdescr.stop_epsilon = 0;
137 rdescr.stop_Nmax = 100;
142 rdescr.refinement_theta = 0.10;
146 rmodel.Mstrich = Mstrich;
148 rmodel.enable_error_estimator = false;
151 dmodel.num_cpus = num_cpus;
152 rmodel.num_cpus = num_cpus;
154 params.mu_ranges = rmodel.mu_ranges;
155 params.mu_names = rmodel.mu_names;
157 model_data = gen_model_data(dmodel);
158 case 1 % single detailed simulation and plot
161 tmppar.model_type = 'eoc';
163 model = newton_model(tmppar);
165 model.newton_solver = 0;
167 model.dt = model.T/model.nt;
169 err = zeros(model.nt+1,7);
172 model_data = gen_model_data(model);
174 sim_data = detailed_simulation(model, model_data);
176 ffe = @exact_function_heat_equation;
178 exact_data.U = zeros(model_data.grid.nelements, model.nt+1);
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);
186 err(:,i) =
fv_l2_error(sim_data.U, exact_data.U, model_data.grid, model);
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))]);
193 model.xnumintervals = round(model.xnumintervals.*2);
194 model.ynumintervals = round(model.ynumintervals.*2);
197 tmppar.model_type = 'eoc_nonlinear';
198 tmppar.local_mass_matrix = @fv_local_mass_matrix_rect;
200 tmppar.evaluate_basis = @fv_evaluate_basis;
203 model = newton_model(tmppar);
205 % model.newton_regularisation = 0;
207 model.dt = model.T/model.nt;
208 model.ynumintervals = 1;
209 err = zeros(model.nt+1,5);
212 disp(['Computing EOC step no. ', num2str(i)]);
213 model_data = gen_model_data(model);
215 sim_data = detailed_simulation(model, model_data);
219 width = model_data.grid.X(2) - model_data.grid.X(1);
221 Fe = @(einds, loc, grid, params) ffe([grid.X(einds) + loc(:,1)*width,...
222 grid.Y(einds) + loc(:,2)*width], ...
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);
234 err(:,i) =
fv_l2_error(sim_data.U, exact_data.U, model_data.grid, model);
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))]);
240 model.xnumintervals = model.xnumintervals.*2;
242 model.dt = model.T / model.nt;
243 %model.ynumintervals = model.ynumintervals.*2;
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));
257 detailed_data = gen_detailed_data(rmodel, model_data);
258 save(fullfile(rbmatlabresult, detailedfname), 'detailed_data', 'rmodel');
260 tmp=load(fullfile(rbmatlabresult,detailedfname));
261 detailed_data=tmp.detailed_data;
264 case 7 % training-error landscape & time comparisons
265 load(fullfile(rbmatlabresult,detailedfname));
267 basis_gen.Mstrich = 0;
268 % number of reduced basis sizes to be tested.
270 % number of collateral reduced basis sizes to be tested.
272 % sample of values for the coupling variable 'c' for which errors are
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.
282 error_plots = {
'train_set_coupled', ...
289 load(params.step7_outputfile);
290 for i = 1:length(output)
294 disp(
'warning: takes a few hours!');
295 load(fullfile(rbmatlabresult,detailedfname));
299 % number of reduced basis sizes to be tested.
301 % number of collateral reduced basis sizes to be tested.
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.
308 % size of mu vector test
set
310 % axis description
for plots with coupling variable
'c'
311 cdescr =
'(N,M) = c * (Nmax, Mmax)';
315 load(params.step8_outputfile);
316 for i = 1:length(output)
317 output{i}.bound = 1500;
318 output{i}.stab_limit = 10000;
322 load(fullfile(rbmatlabresult,detailedfname));
323 model_data = detailed_data;
324 testdir =
'richards_test_10';
326 model.Nmax = size(detailed_data.RB,2);
328 % generate test data
if not existent
329 rand(
'state',123456);
330 M = rand_uniform(10,params.mu_ranges);
333 % load demo_nonlin_evol_detailed_data_LE_on_RB;
334 reduced_data = gen_reduced_data(model,detailed_data);
336 settings = load(fullfile(rbmatlabtemp,...
337 testdir,
'settings.mat'));
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';
345 Mstrich_set = 1:10:100;%[1,2,3,4,5,10,15,20];
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)
353 model = model.set_mu(model,settings.M(:,mu_ind));
359 mtemp.M = extrapar.M;
360 mtemp.Mstrich = Mstrich_set(msi);
361 mtemp.N = extrapar.N;
362 simulation_data = rb_simulation(mtemp,reduced_data);
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))]);
371 %
plot_sim_data(model,detailed_data,simulation_data,plot_params);
374 plot(Mstrich_set,deltas(mu_ind,:));
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]};
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;
391 %tmp = load(fullfile(rbmatlabresult,
'strange.mat'));
393 tmp = load(fullfile(rbmatlabresult,detailedfname));
394 detailed_data = tmp.detailed_data;
396 plot_params.no_lines = 1;
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];
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);
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);
416 parammat = cgrid.vertex;
417 parammat(:,1) = round(parammat(:,1));
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);
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');
431 fp = fullfile(imsavepath, params.model_type);
435 fp = fullfile(fp, ['t_', num2str(tstep)]);
439 fn = ['sample_mu_', mustring];
440 disp(['Processing ', fn, ' ...']);
442 tikzparams.filename = fn;
443 tikzparams.filepath = fp;
444 tikzparams.save_colorbar = 1;
452 print_datatable(fullfile(fp, datafn), subline_title, subline_values);
455 load('richards_affine_detailed_interpol');
457 rdata = gen_reduced_data(model, detailed_data);
459 model.N = size(detailed_data,2);
462 rdata2 = extract_reduced_data_subset(model, rdata);
464 rdata1 = extract_reduced_data_subset(model, rdata);
466 TM1 = rdata1.TM_local{1}(1:10);
467 TM2 = rdata2.TM_local{1}(1:10);
469 gl1 = rdata1.grid_local_ext{1};
470 gl2 = rdata2.grid_local_ext{1};
472 if(any(gl1.CX(TM1) ~= gl2.CX(TM2)))
475 if(any(gl1.CY(TM1) ~= gl2.CY(TM2)))
478 if(any(any(gl1.X(gl1.VI(TM1,:)) ~= gl2.X(gl2.VI(TM2,:)))))
481 if(any(any(gl1.Y(gl1.VI(TM1,:)) ~= gl2.Y(gl2.VI(TM2,:)))))
484 if(any(any(gl1.SX(TM1,:) ~= gl2.SX(TM2,:))))
487 if(any(any(gl1.SY(TM1,:) ~= gl2.SY(TM2,:))))
490 if(any(gl1.EL(TM1,:) ~= gl2.EL(TM2,:)))
493 if(any(gl1.DC(TM1,:) ~= gl2.DC(TM2,:)))
496 if(any(gl1.NX(TM1,:) ~= gl2.NX(TM2,:)))
499 if(any(gl1.NY(TM1,:) ~= gl2.NY(TM2,:)))
502 if(any(gl1.ECX(TM1,:) ~= gl2.ECX(TM2,:)))
505 if(any(gl1.ECY(TM1,:) ~= gl2.ECY(TM2,:)))
509 NBI1 = gl1.NBI(TM1,:);
510 NBI2 = gl2.NBI(TM2,:);
511 if(any(gl1.CX(NBI1) ~= gl2.CX(NBI2)))
514 if(any(gl1.CY(NBI1) ~= gl2.CY(NBI2)))
517 if(any(any(gl1.X(gl1.VI(NBI1,:)) ~= gl2.X(gl2.VI(NBI2,:)))))
520 if(any(any(gl1.Y(gl1.VI(NBI1,:)) ~= gl2.Y(gl2.VI(NBI2,:)))))
523 if(any(any(gl1.SX(NBI1,:) ~= gl2.SX(NBI2,:))))
526 if(any(any(gl1.SY(NBI1,:) ~= gl2.SY(NBI2,:))))
529 for i = 1:length(TM1)
532 case 12 % plot some reduced basis functions
533 tmp = load(fullfile(rbmatlabresult,detailedfname));
534 detailed_data = tmp.detailed_data;
537 plot_params.no_lines = 1;
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;
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)
';
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)) ];
556 fp = fullfile(imsavepath, params.model_type);
560 fn = ['rb_function_
', num2str(i)];
561 disp(['Processing
', fn, ' ...
']);
563 tikzparams.filename = fn;
564 tikzparams.filepath = fp;
565 tikzparams.save_colorbar = 1;
566 tikzparams.width = 3.5;
567 tikzparams.scaleaxis = '';
569 plot_as_tikzfile(model, tikzparams);
573 datafn = 'rb_sublines.dat
';
574 print_datatable(fullfile(fp, datafn), subline_title, subline_values);
578 error('step-number is unknown!');