rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
duneconvdiff.m
Go to the documentation of this file.
1 % small script demonstrating the convdiff example from the M2AN Paper, that is
2 % also implemented in Dune. Later it will be possible to use the Dune
3 % implementation through a mex interface.
4 % Martin Drohmann 06.05.2009
5 % based on burgers_fv.m by
6 % Bernard Haasdonk 14.8.2007
7 
8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9 %%%%%% Select here, what is to be performed %%%%%%%%
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11 %step = 0 % initialize the model_data structure. This step needs to be executed
12  % once per Matlab session only
13 %step = 1 % single detailed simulation with given data and plot. Run
14  % this with varying parameters mu until sure that scheme
15  % is stable. Modify dt or the data-functions accordingly,
16  % until a nice parameter-domain with uniformly stable
17  % detailed scheme is obtained.
18 %step = 2 % generate reduced basis
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20 %old steps:
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 = 9 % test one simulation
26 %step = 10 % generate dummy reduced basis from single trajectory.
27  % A simple reduced simulation can also be
28  % performed. All results should be visually identical
29 
30 %steps = {2,5,7,8}
31 steps = [0,1,2];
32 
33 for si=1:length(steps)
34 
35 step = steps(si);
36 
37 params.model_type = 'default';
38 
39 detailedfname = ['duneconvdiff_', params.model_type, '_detailed_interpol.mat'];
40 % $$$ params.verbose = 5;
41 % $$$
42 % $$$ %% parameters for visualization
43 % $$$ plot_params.show_colorbar = 1;
44 % $$$ plot_params.colorbar_mode = 'EastOutside';
45 
46 switch step
47  case 0 % initialize model data
48  descr = convdiff_dune_descr('init_model');
49  descr.detailedfname = fullfile(rbmatlabtemp, detailedfname);
50  params.mu_ranges = descr.mu_ranges;
51  params.mu_names = descr.mu_names;
52 
53  [dmodel, rmodel] = gen_models(descr);
54 
55  model_data = gen_model_data(dmodel);
56 % $$$ grid = model_data.grid;
57  case 1 % single detailed simulation and plot
58  disp('performing single detailed simulation')
59  tic
60 % mu_test = cellfun(@mean, model.mu_ranges);
61 % mu_test = [0,1,0.2]
62 % model = model.set_mu(model, mu_test);
63  sim_data = detailed_simulation(dmodel, model_data);
64  toc
65 % plot_sim_data(model, model_data, sim_data, plot_params);
66 % Uname = [getenv('RBMATLABTEMP'),'/U',num2str(temp)];
67 % temp = temp + 1;
68 % disp(['save solution U in ', Uname]);
69 % save(Uname, 'U');
70 % end
71 
72  case 2 % reduced basis
73  disp('constructing reduced basis')
74 % $$$ mu_test = cellfun(@mean, params.mu_ranges);
75 % $$$ params.mu = mu_test;
76 % $$$ sim_data = detailed_simulation(model, model_data, params);
77 
78  tic;
79  detailed_data = gen_detailed_data(rmodel, model_data);
80  t = toc;
81 % detailed_data = rb_basis_generation(detailed_data, ...
82 % params);
83  % reconstruct
84  reduced_data = gen_reduced_data(rmodel, detailed_data);
85 
86  rmodel.N = get_rb_size(detailed_data, rmodel);
87  reduced_data = extract_reduced_data_subset(rmodel, reduced_data);
88 
89  %dunerbconvdiff('dune_keyboard');
90  params.mu = [0.001, 0.5, 0.5];
91  set_mu(rmodel, params.mu);
92  rb_sim_data = rb_simulation(rmodel, reduced_data);
93  % reconstruct_and_compare(model, rb_sim_data);
94  set_mu(dmodel, params.mu);
95  dmodel.descr.mexptr('reconstruct_and_compare', rb_sim_data.a);
96 
97  save(fullfile(rbmatlabresult, detailedfname),...
98  'detailed_data','params');
99  plot(detailed_data.datatree.get_field_on_active_child('max_err_sequence', rmodel));
100  set(gca,'Yscale','log');
101  title('RB-generation error convergence');
102 
103  case 4
104  load(fullfile(rbmatlabresult,detailedfname));
105  disp('reduced simulation:')
106  reduced_data = gen_reduced_data(model, detailed_data);
107  params.N = size(detailed_data.RB,2);
108  params.M = params.N;
109  reduced_data = extract_reduced_data_subset(model, reduced_data);
110  tic;
111  params.mu = mu_test;
112  rb_sim_data = rb_simulation(model,reduced_data,params);
113  t = toc;
114  disp(['time for online phase: t = ',num2str(t)]);
115 
116  disp('full simulation:')
117  tic;
118  sim_data = detailed_simulation(model,model_data,params);
119  t = toc;
120  disp(['time for detailed simulation: t = ',num2str(t)]);
121 
122  dune_demo_rb_gui(model,detailed_data,[],params,plot_params);
123 
124  case 7 % training-error landscape
125  disp('warning: takes a few hours!');
126  load(fullfile(rbmatlabresult,detailedfname));
127 
128  model.N = model.RB_stop_Nmax;
129 
130  offline_data = gen_reduced_data(model,detailed_data);
131  range_params.plot_fields = { 'N', 'M' };
132  range_params.max = [ size(detailed_data.RB,2), ...
133  size(detailed_data.BM{1},2) ];
134  range_params.sample_size = [ 10, 12 ];
135  range_params.Msamples = 12;
136  range_params.mu_set_size = 100;
137 
138  testdir = [ 'duneconvdiff_test_', range_params.mu_set_size ];
139 
140  params.run_name = ['_', testdir, '_step7'];
141  params.tictoctable = true;
142 
143  output=stochastic_error_estimation(model, detailed_data, offline_data,...
144  testdir, range_params, params);
145 
146  case 9
147  tmp=load(fullfile(rbmatlabresult,detailedfname));
148  detailed_data = tmp.detailed_data;
149 
150  reduced_data = gen_reduced_data(model, detailed_data, params);
151 
152  params.N = get_rb_size(model, detailed_data);
153  reduced_data = extract_reduced_data_subset(model, reduced_data, ...
154  params);
155 
156  params.mu = [0.5, 0.5, 0.5];
157  rb_sim_data = rb_simulation(model, reduced_data, params);
158 
159  plot(rb_sim_data.a);
160 
161  case 10 % construct dummy reduced basis by single trajectory and simulate
162  disp('old, to be adjusted:')
163  disp('detailed interpolated simulation for basis construction:')
164  mu_test = cellfun(@mean, params.mu_ranges);
165 % mu_test2 = cellfun(@min, params.mu_ranges);
166  params.mu = mu_test;
167  sim_data = detailed_simulation(model, model_data);
168  UON = model.orthonormalize(model, model_data, sim_data.U);
169  detailed_data.grid = model_data.grid;
170  detailed_data.W = model_data.W;
171  detailed_data.RB = UON;
172  disp('reduced simulation:')
173  reduced_data = gen_reduced_data(model, detailed_data);
174  params.N = size(detailed_data.RB,2);
175  reduced_data = extract_reduced_data_subset(reduced_data);
176  params.mu = mu_test;
177  rb_sim_data = rb_simulation(model, reduced_data);
178  rb_sim_data = rb_reconstruction(model, detailed_data, rb_sim_data);
179 
180  plot_params.title = 'reduced simulation result';
181  plot_sim_data(model, model_data, rb_sim_data, plot_params);
182  plot_params.title = 'detailed simulation result';
183  plot_sim_data(model, model_data, sim_data, plot_params);
184 
185 
186  otherwise
187  error('step-number is unknown!');
188 end;
189 
190 end;
191 
192 %| \docupdate