2 % small script demonstrating a buckley leverett problem
3 % discretization and RB model reduction
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
24 warning('off', 'RBMatlab:rb_simulation:runtime');
26 imsavepath = fullfile(rbmatlabresult,
'images');
28 params.model_type =
'buckley_leverett';
29 %params.model_type =
'exponential_diffusion';
30 %params.model_type =
'eoc_nonlinear';
31 %params.model_size =
'big';
32 %params.model_size =
'medium';
33 % params.model_size =
'small';
34 % params.model_type =
'nonlinear';
35 params.separate_CRBs =
false;
38 params.RB_detailed_test_savepath =
'test_data_100';
39 params.RB_test_size = 100;
43 extrapar.Mstrich = 20;
47 % email to which status messages about processing mu vectors is sent
48 % params.email =
'mdrohmann@uni-muenster.de';
50 %% parameters
for visualization
51 plot_params.show_colorbar = 1;
52 plot_params.colorbar_mode =
'EastOutside';
53 plot_params.no_lines =
true;
54 plot_params.plot_type =
'patch';
55 % plot_params.clim = [0, 0.5];
56 % plot_params.clim_tight =
true;
58 plot_params.bind_to_model =
true;
59 plot_params.yscale_uicontrols = 0.6;
61 % output-filenames in rbmatlabresult
62 CRBfname = ['newton_', params.model_type, '_CRB_interpol.mat'];
63 if exist(
'model',
'var') && isfield(model,
'RB_error_indicator') ...
64 && ~strcmp(model.RB_error_indicator,
'error')
65 infix = model.RB_error_indicator;
69 detailedfname = ['newton_', params.model_type, infix, '_detailed_interpol.mat'];
70 params.step7_outputfile = fullfile(rbmatlabresult, [params.model_type, infix, '_step7_output']);
71 params.step8_outputfile = fullfile(rbmatlabresult, [params.model_type, infix, '_step8_output']);
72 params.step0file = [params.model_type, infix, 'step0'];
73 params.step1file = [params.model_type, infix, 'step1'];
78 case 0 % initialize model data
79 basis_gen = newton_oo_model(params);
81 model_data = gen_model_data(basis_gen);
82 save(fullfile(rbmatlabtemp, params.step0file), 'model','model_data')
83 outfile = fullfile(rbmatlabtemp, params.step0file);
84 case 1 % single detailed simulation and plot
86 outfile = fullfile(rbmatlabtemp, params.step1file);
88 load(fullfile(rbmatlabtemp, params.step0file));
89 tmppar.model_type = 'eoc';
91 model = newton_model(tmppar);
93 model.newton_solver = 0;
95 model.dt = model.T/model.nt;
97 err = zeros(model.nt+1,7);
100 model_data = gen_model_data(model);
102 sim_data = detailed_simulation(model, model_data);
104 ffe = @exact_function_heat_equation;
106 exact_data.U = zeros(model_data.grid.nelements, model.nt+1);
108 for tt = 1:model.nt+1
109 model.t = (tt-1) * model.dt;
110 exact_data.U(:,tt) = ffe([model_data.grid.CX(:), model_data.grid.CY(:)], model);
114 err(:,i) =
fv_l2_error(sim_data.U, exact_data.U, model_data.W);
116 eoc(i-1) = log(max(err(:,i-1))/max(err(:,i)))/log(2);
117 disp(['Error: ', num2str(err(end,i)), 'EOC: ', num2str(eoc(i-1))]);
121 model.xnumintervals = round(model.xnumintervals.*2);
122 model.ynumintervals = round(model.ynumintervals.*2);
124 outfile = fullfile(rbmatlabtemp, [params.model_type, '_step1_5']);
125 save(outfile, 'err', 'eoc');
127 load(fullfile(rbmatlabtemp, params.step0file));
128 tmppar.model_type = 'eoc_nonlinear';
129 tmppar.local_mass_matrix = @fv_local_mass_matrix_rect;
131 tmppar.evaluate_basis = @fv_evaluate_basis;
134 model = newton_model(tmppar);
136 % model.newton_regularisation = 0;
138 model.dt = model.T/model.nt;
139 model.ynumintervals = 1;
140 err = zeros(model.nt+1,5);
143 disp(['Computing EOC step no. ', num2str(i)]);
144 model_data = gen_model_data(model);
146 sim_data = detailed_simulation(model, model_data);
150 width = model_data.grid.X(2) - model_data.grid.X(1);
152 Fe = @(einds, loc, grid, params) ffe([grid.X(einds) + loc(:,1)*width,...
153 grid.Y(einds) + loc(:,2)*width], ...
155 tmppar.diff_k0 = model.diff_k0;
156 exact_data.U = zeros(model_data.grid.nelements, model.nt+1);
157 for tt = 1:model.nt+1
158 model.t = (tt-1) * model.dt;
159 exact_data.U(:,tt) = ffe([model_data.grid.CX(:), ...
160 model_data.grid.CY(:)], model);
161 % exact_data.U(:,tt) = l2project(Fe, 2, model_data.grid, tmppar);
165 err(:,i) =
fv_l2_error(sim_data.U, exact_data.U, model_data.W);
167 eoc(i-1) = log(max(err(:,i-1))/max(err(:,i)))/log(2);
168 disp(['Error: ', num2str(err(end,i)), ' EOC: ', num2str(eoc(i-1))]);
171 model.xnumintervals = model.xnumintervals.*2;
173 model.dt = model.T / model.nt;
174 %model.ynumintervals = model.ynumintervals.*2;
177 outfile = fullfile(rbmatlabtemp, [params.model_type, '_step1_75']);
178 save(outfile, 'err', 'eoc');
179 case 2 % empirical interpolation of space operator
180 load(fullfile(rbmatlabresult, params.step0file));
182 outfile = fullfile(rbmatlabresult, CRBfname);
183 case 3 % compare detailed simulation with and without interpolation
184 model.detailed_ei_simulation = @nonlin_evol_detailed_simulation;
186 case 4 % construct dummy reduced basis by single trajectory and simulate
187 model.detailed_ei_simulation = @nonlin_evol_detailed_simulation;
188 detailed_data = gen_detailed_data(basis_gen, model_data);
189 save(fullfile(rbmatlabresult, detailedfname), 'detailed_data', 'basis_gen');
191 case 5 % reduced basis
192 % tmp=load(fullfile(rbmatlabresult,CRBfname));
193 % detailed_data = tmp.detailed_data;
195 outfile = fullfile(rbmatlabresult, detailedfname);
197 % load(fullfile(rbmatlabresult,detailedfname));
199 case 7 % training-error landscape & time comparisons
200 tmp=load(fullfile(rbmatlabresult,detailedfname));
201 detailed_data = tmp.detailed_data;
205 % maximum number of reduced basis functions
206 model.Nmax = size(detailed_data.RB,2);
207 % maximum number of collateral reduced basis functions
208 model.Mmax = size(detailed_data.BM{1},2);
209 % number of reduced basis sizes to be tested.
211 % number of collateral reduced basis sizes to be tested.
213 % sample of values
for the coupling variable
'c' for which errors are
216 % axis description
for plots with coupling variable
'c'
217 cdescr =
'(N,M) = c * (Nmax, Mmax)';
218 % size of mu vector test
set.
221 error_plots = {
'train_set',
'train_set_coupled', ...
222 'error',
'error_coupled' };
224 model.M_by_N_ratio = model.Mmax/model.Nmax;
227 outfile = params.step7_outputfile;
230 load(params.step7_outputfile);
231 for i = 1:length(output)
235 disp(
'warning: takes a few hours!');
236 tmp = load(fullfile(rbmatlabresult,detailedfname));
237 detailed_data = tmp.detailed_data;
239 % maximum number of reduced basis vectors
240 model.Nmax = model.RB_stop_Nmax;
241 % maximum number of collateral reduced basis vectors used
for
242 model.Mmax = size( detailed_data.BM{1}, 2 ) - extrapar.Mstrich;
244 model.M_by_N_ratio = model.Mmax/model.Nmax;
246 % number of reduced basis sizes to be tested.
248 % number of collateral reduced basis sizes to be tested.
250 % Mstrich values to be tested
251 Mstrich_samples = 1:extrapar.Mstrich;
252 % sample of values
for the coupling variable
'c' for which
253 % estimators are computed.
255 % size of mu vector test
set
257 % axis description
for plots with coupling variable
'c'
258 cdescr =
'(N,M) = c * (Nmax, Mmax)';
261 outfile = params.step7_outputfile;
263 load(params.step8_outputfile);
264 for i = 1:length(output)
265 output{i}.bound = 1500;
266 output{i}.stab_limit = 10000;
270 tmp = load(fullfile(rbmatlabresult,detailedfname));
271 detailed_data = tmp.detailed_data;
272 model_data = detailed_data;
274 testdir = [params.model_type, '_', num2str(num_samples), '_test'];
276 model.Nmax = size(detailed_data.RB,2);
278 % generate test data
if not existent
279 rand(
'state',123456);
280 M = rand_uniform(num_samples,model.mu_ranges);
283 % load demo_nonlin_evol_detailed_data_LE_on_RB;
284 reduced_data = gen_reduced_data(model,detailed_data);
286 settings = load(fullfile(rbmatlabtemp,...
287 testdir,
'settings.mat'));
291 % storage
for maximum l2-errors (i.e. linfty([0,T],l2)-norm)
292 errs = zeros(Msamples,Nsamples);
293 model.RB_error_indicator=
'ei_estimator_test';
295 Mstrich_set = floor(1:9.9:100);%[1,2,3,4,5,10,15,20];
297 deltas = cell(size(M,2),length(Mstrich_set));
298 lambda = zeros(size(M,2),length(Mstrich_set));
299 lambdas = cell(size(M,2),length(Mstrich_set));
300 errors = cell(size(M,2),length(Mstrich_set));
301 parfor mu_ind = 1:size(M,2);
302 disp([
'processing parameter vector ',num2str(mu_ind),...
303 '/',num2str(size(M,2))]);
304 deltas_tmp = cell(1, length(Mstrich_set));
305 errors_tmp = cell(1, length(Mstrich_set));
306 lambdas_tmp = cell(1, length(Mstrich_set));
307 lambda_tmp = zeros(1, length(Mstrich_set));
308 tsettings = settings;
309 textrapar = extrapar;
311 for msi = 1:length(Mstrich_set)
313 mtemp = tmodel.set_mu(tmodel,tsettings.M(:,mu_ind));
317 mtemp.M = textrapar.M;
318 mtemp.Mstrich = Mstrich_set(msi);
319 mtemp.N = textrapar.N;
320 disp(['with CRB size ',num2str(mtemp.M),...
321 ' Mstrich'' = ',num2str(mtemp.Mstrich),...
322 ' RB size ', num2str(mtemp.N)]);
323 mtemp.C_I = 1 - mtemp.diff_m;
324 simulation_data = rb_simulation(mtemp,reduced_data);
327 deltas_tmp{msi} = simulation_data.Delta;
328 simulation_data = rb_reconstruction(mtemp,detailed_data,simulation_data);
329 errors_tmp{msi} = tmodel.error_algorithm(simulation_data.U, ...
331 detailed_data.W, mtemp);
332 lambdas_tmp{msi} = deltas_tmp{msi} ./ errors_tmp{msi};
333 lambda_tmp(msi) = max(deltas_tmp{msi}) / max(errors_tmp{msi});
334 disp([
'true error: ', num2str(max(errors_tmp{msi})), ...
335 ' estimator: ', num2str(max(deltas_tmp{msi}))]);
336 disp([
'effectivity: ', num2str(lambda_tmp(msi))]);
338 %
plot_sim_data(model,detailed_data,simulation_data,plot_params);
341 deltas(mu_ind,:) = deltas_tmp;
342 errors(mu_ind,:) = errors_tmp;
343 lambdas(mu_ind,:) = lambdas_tmp;
344 lambda(mu_ind,:) = lambda_tmp;
345 % plot(Mstrich_set,deltas(mu_ind,:));
348 save(fullfile(rbmatlabresult,
'step9'),
'deltas',
'errors',
'lambdas',...
349 'lambda',
'M',
'Mstrich_set');
351 outfile = fullfile(rbmatlabresult,
'step9');
353 tmp = load(fullfile(rbmatlabresult, detailedfname));
354 detailed_data = tmp.detailed_data;
356 %% initial data plot (as patch)
357 mu_set = {[1,0.00,0.2]};
358 plot_params.plot_type =
'patch';
359 plot_params.line_width = 1e-10;
360 plot_params.no_lines =
true;
364 %% some sample plots (as contour plot)
365 mu_set = {[1,0.01,0.2], [2,0.01,0.2], [4,0.01,0.2], [1,0.01,0], [2,0.01,0],[4,0.02,0]};
366 % mu_set = {[1,0.01,0], [1,0.01,0.2]};
367 % mu_set = {[0,0.01], [0.1,0.01],[0.2,1.3]};
369 plot_params.plot_type =
'contour';
370 plot_params.contour_lines = 5;
371 plot_params.line_width = 2;
372 timeinstants = [0:1/4:1]*model.T;
373 boxed_snapshots =
true;
376 %% interpolation dofs
377 plot_params.plot_type =
'patch';
378 plot_params.colormap = 1/length(detailed_data.TM{1})*repmat((length(detailed_data.TM{1}):-1:1)
',1,3);
379 plot_params.show_colorbar = 0;
380 u = zeros(detailed_data.grid.nelements,1);
381 u(detailed_data.TM{1}) = 1:length(detailed_data.TM{1});
384 plot_params.plot(detailed_data.grid, u, plot_params);
385 title(['Interpolation DOFS
for implicit and
explicit operator']);
386 set(gcf, 'Color
', 'none
');
387 set(gcf, 'InvertHardCopy
', 'off
');
389 tikzparams.filepath = fullfile(imsavepath, params.model_type);
390 export_fig(fullfile(tikzparams.filepath, 'ei_dofs
'),'-pdf
','-a0
',gcf);
391 system(['convert -density 1200x1200 -trim
', ...
392 fullfile(tikzparams.filepath, 'ei_dofs.pdf
'), ...
393 fullfile(tikzparams.filepath, 'ei_dofs.png
')]);
394 system(['rm
', fullfile(tikzparams.filepath, 'ei_dofs.pdf
')]);
397 %% error convergence of crb generation
398 plot(detailed_data.ei_info{1}.max_err_sequence);
399 set(gca,'Yscale
','log
');
400 title('EI-interpolation error decrease
');
401 matlab2tikz(fullfile(tikzparams.filepath, 'ei_error_decrease.tikz
'));
403 %% error convergence of POD greedy
404 plot(detailed_data.RB_info.max_err_sequence);
405 set(gca,'Yscale
','log
');
406 title('POD greedy error decrease
');
407 matlab2tikz(fullfile(tikzparams.filepath, 'pod_greedy_error_decrease.tikz
'));
411 Msize = size(detailed_data.QM{1},2);
412 % colorm = colormap(hot(Msize));
413 % colorm = colorm(Msize:-1:1,:);
414 emus = detailed_data.ei_info{1}.extension_mus([1,3],1:Msize);
415 midmu = detailed_data.ei_info{1}.extension_mus(2,1:Msize);
416 coord1 = emus(:,midmu==0.01);
417 coord2 = emus(:,midmu==0.005);
418 [coord1u,coord1I,coordJ] = unique(sortrows(coord1'),
'rows');
419 count1 = [coord1I(1); coord1I(2:end) - coord1I(1:end-1)]*10;
420 [coord2u,coord2I,coordJ] = unique(sortrows(coord2
'),'rows
');
421 count2 = [coord2I(1); coord2I(2:end) - coord2I(1:end-1)]*10;
423 scatter(coord1u(:,1),coord1u(:,2), count1, 'black
', 'filled
');
425 scatter(coord2u(:,1),coord2u(:,2), count2, [0.7 0.7 0.7], 'd
','filled
');
428 set(gcf, 'Color
', 'none
');
429 set(gcf, 'InvertHardCopy
', 'off
');
433 im = export_fig(fullfile(tikzparams.filepath, 'ei_mu_snapshots
'), '-png
','-a3
',gcf);
437 Nsize = size(detailed_data.RB,2);
438 % colorm = colormap(hot(Msize));
439 % colorm = colorm(Msize:-1:1,:);
440 emus = detailed_data.RB_info.mu_sequence([1,3],1:Nsize);
441 midmu = detailed_data.RB_info.mu_sequence(2,1:Nsize);
442 coord1 = emus(:,midmu==0.01);
443 coord2 = emus(:,midmu==0.005);
444 [coord1u,coord1I,coordJ] = unique(sortrows(coord1'),
'rows');
445 count1 = [coord1I(1); coord1I(2:end) - coord1I(1:end-1)]*100;
446 [coord2u,coord2I,coordJ] = unique(sortrows(coord2
'),'rows
');
447 count2 = [coord2I(1); coord2I(2:end) - coord2I(1:end-1)]*100;
449 scatter(coord1u(:,1),coord1u(:,2), count1, 'black
', 'filled
');
451 scatter(coord2u(:,1),coord2u(:,2), count2, [0.7 0.7 0.7], 'd
','filled
');
454 set(gcf, 'Color
', 'none
');
455 set(gcf, 'InvertHardCopy
', 'off
');
459 im = export_fig(fullfile(tikzparams.filepath, 'rb_mu_snapshots
'), '-png
','-a3
',gcf);
462 % error convergence of random samples
465 % scatter3(coord(1,:),coord(2,:),coord(3,:),20,colorm,'filled
');
466 % xlabel(['\mu_1 =
',model.mu_names{1}]);
467 % ylabel(['\mu_3 =
',model.mu_names{3}]);
468 %ylabel(['\mu_2 = c_{init}
']);%,model.mu_names{2}]);
469 % zlabel('time index k
');
470 % title(['snapshots selected
for collateral basis
for operator no.
'])
471 % set(gca,'Box
','on
');
472 % save(fullfile(tikzparams.filepath, 'ei_mu_snapshots.dat
'), 'coord
', '-ascii', '-double', '-tabs');
473 % matlab2tikz(fullfile(tikzparams.filepath,
'ei_mu_snapshots.tikz'));
478 %tmp = load(fullfile(rbmatlabresult,
'strange.mat'));
480 tmp = load(fullfile(rbmatlabresult,detailedfname));
481 detailed_data = tmp.detailed_data;
483 plot_params.no_lines = 1;
485 plot_params.no_axes = 1;
486 plot_params.transparent_background = 1;
487 plot_params.show_colorbar = 0;
488 %plot_params.shrink_factor = 1.13;
489 %plot_params.colormap = Cmap;
490 plot_params.clim = [0.1,0.6];
492 reduced_data = gen_reduced_data(model, detailed_data);
493 model.N = size(detailed_data.RB,2);
494 model.M = size(detailed_data.QM,2);
495 reduced_data = extract_reduced_data_subset(model, reduced_data);
497 cparams.range = cell(length(model.mu_ranges)+1,1);
498 cparams.range{1} = [1, model.nt];
499 cparams.range(2:end) = model.mu_ranges(:);
500 cparams.numintervals = 2*ones(length(model.mu_ranges)+1,1);
503 parammat = cgrid.vertex;
504 parammat(:,1) = round(parammat(:,1));
506 for i=1:length(parammat)
507 tstep = parammat(i,1);
508 newmodel = model.set_mu(model, parammat(i,2:end));
509 rb_sim_data = rb_simulation(newmodel, reduced_data);
510 rb_sim_data = rb_reconstruction(model, detailed_data, rb_sim_data);
512 % transformed version
513 plot_element_data(model_data.grid, rb_sim_data.U(:,tstep), plot_params);
515 mustring = strrep(strrep(strrep(strrep(mat2str(parammat(i,2:end)),' ', '_'),'[',''),']',''),'.','p');
516 fp = fullfile(imsavepath, params.model_type);
520 fp = fullfile(fp, ['t_', num2str(tstep)]);
524 fn = ['sample_mu_', mustring];
525 disp(['Processing ', fn, ' ...']);
527 tikzparams.filename = fn;
528 tikzparams.filepath = fp;
529 tikzparams.save_colorbar = 1;
537 load('richards_affine_detailed_interpol');
539 rdata = gen_reduced_data(model, detailed_data);
541 model.N = size(detailed_data,2);
544 rdata2 = extract_reduced_data_subset(model, rdata);
546 rdata1 = extract_reduced_data_subset(model, rdata);
548 TM1 = rdata1.TM_local{1}(1:10);
549 TM2 = rdata2.TM_local{1}(1:10);
551 gl1 = rdata1.grid_local_ext{1};
552 gl2 = rdata2.grid_local_ext{1};
554 if(any(gl1.CX(TM1) ~= gl2.CX(TM2)))
557 if(any(gl1.CY(TM1) ~= gl2.CY(TM2)))
560 if(any(any(gl1.X(gl1.VI(TM1,:)) ~= gl2.X(gl2.VI(TM2,:)))))
563 if(any(any(gl1.Y(gl1.VI(TM1,:)) ~= gl2.Y(gl2.VI(TM2,:)))))
566 if(any(any(gl1.SX(TM1,:) ~= gl2.SX(TM2,:))))
569 if(any(any(gl1.SY(TM1,:) ~= gl2.SY(TM2,:))))
572 if(any(gl1.EL(TM1,:) ~= gl2.EL(TM2,:)))
575 if(any(gl1.DC(TM1,:) ~= gl2.DC(TM2,:)))
578 if(any(gl1.NX(TM1,:) ~= gl2.NX(TM2,:)))
581 if(any(gl1.NY(TM1,:) ~= gl2.NY(TM2,:)))
584 if(any(gl1.ECX(TM1,:) ~= gl2.ECX(TM2,:)))
587 if(any(gl1.ECY(TM1,:) ~= gl2.ECY(TM2,:)))
591 NBI1 = gl1.NBI(TM1,:);
592 NBI2 = gl2.NBI(TM2,:);
593 if(any(gl1.CX(NBI1) ~= gl2.CX(NBI2)))
596 if(any(gl1.CY(NBI1) ~= gl2.CY(NBI2)))
599 if(any(any(gl1.X(gl1.VI(NBI1,:)) ~= gl2.X(gl2.VI(NBI2,:)))))
602 if(any(any(gl1.Y(gl1.VI(NBI1,:)) ~= gl2.Y(gl2.VI(NBI2,:)))))
605 if(any(any(gl1.SX(NBI1,:) ~= gl2.SX(NBI2,:))))
608 if(any(any(gl1.SY(NBI1,:) ~= gl2.SY(NBI2,:))))
611 for i = 1:length(TM1)
615 error('step-number is unknown!');