rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
burgers_fv.m
Go to the documentation of this file.
1 % small script demonstrating the burgers equation with explicit fv
2 % discretization and RB model reduction
3 
4 % Bernard Haasdonk 14.8.2007
5 
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 %%%%%% Select here, what is to be performed with the burgers 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 %step = 7 % generate error-landscape over varying N and M
25  % can take several hours!!!
26 
27 % output-filenames in rbmatlabresult
28 CRBfname = 'burgers_fv_CRB_interpol.mat';
29 detailedfname = 'burgers_fv_detailed_interpol.mat';
30 
31 % grid parameters
32 params = [];
33 % rectangular or triangular
34 params.gridtype = 'triagrid';
35 params.grid_initfile = '2dsquaretriang';
36 
37 %params.gridtype = 'rectgrid';
38 %params.xnumintervals = 20;
39 %params.ynumintervals = 20;
40 %params.xrange = [0,1];
41 %params.yrange = [0,1];
42 
43 % set all to neuman-boundary by specifying "rectangles", all
44 % boundary within is set to boundary type
45 params.bnd_rect_corner1 = [-1; ...
46  -1];
47 params.bnd_rect_corner2 = [ 2; ...
48  2];
49 
50 % -1 means dirichlet, -2 means neumann
51 params.bnd_rect_index = [-2]; % first is dirichlet, second neuman
52 
53 % time discretization
54 %params.T = 1.0;
55 %params.nt = 200; % guess and adjust later
56 
57 params.T = 0.4;
58 params.nt = 80; % guess and adjust later
59 %params.nt = 100; % guess and adjust later
60 %params.T = 0.1;
61 %params.nt = 50; % guess and adjust later
62 %params.400 => non-bounded u
63 
64 % output settings
65 params.verbose = 10;
66 params.axis_equal = 1;
67 params.clim = [-1,1];
68 
69 % data functions
70 %params.name_init_values = 'blobs';
71 %params.radius = 0.1;
72 %params.gamma = 100;
73 %params.beta = 1; % init data weight between 0 and 1
74 
75 % name of function in RBmatlab/datafunc/init_values
76 params.name_init_values = 'gradient_box';
77 % parameters for data functions
78 params.c_init_up = 1.0;
79 %params.c_init_lo = 1.0;
80 params.c_init_lo = 0.0;
81 %params.c_init_lo = -1.0;
82 
83 % name of function in RBmatlab/datafunc/dirichlet_values
84 %params.name_dirichlet_values = 'homogeneous';
85 %params.c_dir = 0;
86 
87 % name of function in RBmatlab/datafunc/neuman_values
88 params.name_neuman_values = 'homogeneous';
89 params.c_neu = 0;
90 
91 %params.name_neuman_values = 'homogeneous';
92 %params.c_neu = 0;
93 
94 % convective flux
95 params.name_flux = 'burgers_parabola';
96 % for simplifying computation in case of linear flux:
97 params.flux_linear = 0;
98 %params.flux_quad_degree = 2;
99 
100 % parameter of chosen conv-flux
101 params.vmax = 0.30;
102 %params.vrot_angle = 0;
103 params.vrot_angle = -pi/4;
104 %params.vrot_angle = -pi/10;
105 params.flux_pdeg = 2; % burgers exponent
106 
107 % rb-formulation
108 params.mu_names = {'c_init_lo','vrot_angle'};
109 params.mu_ranges = {[-1,1],[-pi/4, 0]};
110 params.rb_problem_type = 'nonlin_evol';
111 %params.problem_type = 'lin_evol';
112 
113 % finite volume settings
114 params.name_diffusive_num_flux = 'none';
115 params.name_convective_num_flux = 'lax-friedrichs';
116 params.lxf_lambda = 1.66
117 %params.lxf_lambda = 1.25;
118 %params.lxf_lambda = fv_search_max_lxf_lambda([],params)
119 %params.name_convective_num_flux = 'enquist-osher';
120 
121 % projection of analytical initial data to discrete function
122 params.init_values_algorithm = 'fv_init_values';
123 params.implicit_operators_algorithm = 'fv_operators_implicit';
124 % get mass matrix for inner product computation from DOF-vectors
125 params.inner_product_matrix_algorithm = 'fv_inner_product_matrix';
126 params.data_const_in_time = 1;
127 
128 % settings for empirical interpolation
129 params.L_E_local_name = 'fv_conv_explicit_space';
130 par.range = params.mu_ranges;
131 params.ei_numintervals = [4,4]; % 8x8 parameter grid
132 %params.Mmax = 300;
133 params.Mmax = 150;
134 params.ei_detailed_savepath = 'burgers_fv_5x5_detailed';
135 params.ei_operator_savepath = 'burgers_fv_5x5_ei_operator';
136 params.ei_time_indices = 1:params.nt;
137 params.CRB_generation_mode = 'param-time-space-grid';
138 params.ei_target_error = 'interpol';
139 
140 % settings for reduced basis generation
141 params.detailed_simulation_algorithm = 'detailed_simulation';
142 %params.operators_algorithm = 'fv_operators_implicit_explicit';
143 %params.init_values_algorithm = 'fv_init_values';
144 %params.inner_product_matrix_algorithm = 'fv_inner_product_matrix';
145 params.error_algorithm = 'fv_error';
146 %params.lxf_lambda = 1.0194e+003;
147 %params.data_const_in_time = 1;
148 params.error_norm = 'l2';
149 params.RB_extension_algorithm = 'RB_extension_PCA_fixspace';
150 % 'RB_extension_max_error_snapshot'};
151 params.RB_stop_timeout = 2*60*60; % 2 hours
152 params.RB_stop_epsilon = 1e-5;
153 params.RB_error_indicator = 'error'; % true error
154 %params.RB_error_indicator = 'estimator'; % Delta from rb_simulation
155 params.RB_stop_Nmax = 100;
156 params.RB_generation_mode = 'uniform_fixed';
157 params.RB_numintervals = params.ei_numintervals;
158 params.RB_detailed_train_savepath = params.ei_detailed_savepath;
159 
160 switch step
161  case 1 % single detailed simulation and plot
162  disp('performing single detailed simulation')
163  grid = construct_grid(params);
164  U = detailed_simulation(grid, params);
165  plot_element_data_sequence(grid,U,params);
166  case 2 % empirical interpolation of space operator
167  disp('constructing colateral reduced basis')
168  params.RB_generation_mode = 'none';
169  detailed_data = rb_detailed_prep(params);
170  save(fullfile(rbmatlabresult,CRBfname),...
171  'detailed_data','params');
172  detailed_data.RB = zeros(detailed_data.grid.nelements,1);
173  rb_plot_detailed_data(detailed_data,params);
174  close(gcf-2);
175  case 3 % compare detailed simulation with and without interpolation
176  disp(['comparison between detailed simulation with and without', ...
177  ' interpolation']);
178  tmp = load(fullfile(rbmatlabresult,CRBfname));
179  detailed_data = tmp.detailed_data;
180  U = detailed_simulation(detailed_data.grid, params);
181  params.title = 'detailed simulation without interpolation';
182  plot_element_data_sequence(detailed_data.grid,U,params);
183  params.M = size(detailed_data.QM{1},2);
184 % params.M = 400;
185  Ui = detailed_ei_nonlin_evol_simulation(detailed_data,params);
186  params.title = 'detailed simulation with empirical interpolation';
187  plot_element_data_sequence(detailed_data.grid,Ui,params);
188  params.title = 'error';
189  plot_element_data_sequence(detailed_data.grid,abs(Ui-U),params);
190  disp(['maximum l-infty error:',num2str(max(max(abs(Ui-U))))]);
191  case 4 % construct dummy reduced basis by single trajectory and simulate
192  load(fullfile(rbmatlabresult,CRBfname));
193  disp('detailed interpolated simulation for basis construction:')
194  params.M = size(detailed_data.QM{1},2);
195  Ui = detailed_ei_nonlin_evol_simulation(detailed_data,params);
196  A = feval(params.inner_product_matrix_algorithm,detailed_data.grid,params);
197  UON = orthonormalize(Ui,A);
198  detailed_data.RB = UON;
199  disp('reduced simulation:')
200  offline_data = rb_offline_prep(detailed_data,params);
201  params.N = size(detailed_data.RB,2);
202  params.M = size(detailed_data.QM{1},2);
203  reduced_data = rb_online_prep(offline_data, params);
204  simulation_data = rb_simulation(reduced_data,params);
205  Uappr = rb_reconstruction(detailed_data,simulation_data);
206 
207  disp('detailed interpolated and rb-projected simulation:')
208  params.M = size(detailed_data.QM{1},2);
209  params.N = size(detailed_data.RB,2);
210  Uirb = detailed_ei_rb_proj_nonlin_evol_simulation(detailed_data,params);
211 
212  params.title = 'reduced simulation result';
213  plot_element_data_sequence(detailed_data.grid,Uappr,params);
214  params.title = 'detailed ei_interpol simulation result';
215  plot_element_data_sequence(detailed_data.grid,Ui,params);
216  params.title = 'detailed ei_interpol and rb_projected simulation result';
217  plot_element_data_sequence(detailed_data.grid,Uirb,params);
218 
219  case 5 % reduced basis
220  disp('constructing reduced basis')
221  tmp = load(fullfile(rbmatlabresult,CRBfname));
222  detailed_data = tmp.detailed_data;
223  detailed_data = rb_basis_generation(detailed_data, ...
224  params);
225  save(fullfile(rbmatlabresult,detailedfname),...
226  'detailed_data','params');
227  plot(detailed_data.RB_info.max_err_sequence);
228  set(gca,'Yscale','log');
229  title('RB-generation error convergence');
230  case 6
231  load(fullfile(rbmatlabresult,detailedfname));
232  disp('reduced simulation:')
233  offline_data = rb_offline_prep(detailed_data,params);
234  params.N = size(detailed_data.RB,2);
235  params.M = size(detailed_data.QM,2);
236  reduced_data = rb_online_prep(offline_data, params);
237  tic;
238  simulation_data = rb_simulation(reduced_data,params);
239  t = toc;
240  disp(['time for online phase: t = ',num2str(t)]);
241 
242  disp('full simulation:')
243  tic;
244  U = detailed_simulation(detailed_data.grid, params);
245  t = toc;
246  disp(['time for detailed simulation: t = ',num2str(t)]);
247 
248  demo_rb_gui(detailed_data,[],params);
249 
250  case 7 % training-error landscape
251  disp('warning: takes a few hours!');
252  load(fullfile(rbmatlabresult,detailedfname));
253 
254  % runs = {'train_linRB','test_linRB'};
255  % dirnames = {testdir};
256  % testdir = 'chemnitz_test_100';
257 
258  % runs = {'train','test'};
259  % dirnames = {params.RB_detailed_train_savepath, testdir};
260 
261  runs = {'train'};
262  dirnames = {params.RB_detailed_train_savepath};
263 
264  %runs = {'test'};
265  %dirnames = {testdir};
266 
267  % generate test data if not existent
268  if ismember('test',runs)
269  % generate test data if not existent
270  rand('state',123456);
271  M = rand_uniform(100,params.mu_ranges);
272  save_detailed_simulations(M,testdir,detailed_data.grid,params)
273  end;
274 
275  % load demo_nonlin_evol_detailed_data_LE_on_RB;
276  offline_data = rb_offline_prep(detailed_data,params);
277 % Nsamples = 3;
278  Nsamples = 20;
279  Nmax = size(detailed_data.RB,2);
280  %Nmax = 150;
281  Ns = round((0:(Nsamples-1)) * ...
282  (Nmax-1)/(Nsamples-1)+1);
283 
284  %Msamples = 3;
285  Msamples = 25;
286  Mmax = size(detailed_data.BM,2);
287  %Mmax = 300; % size(detailed_data.BM,2);
288  Ms = round((0:(Msamples-1)) * ...
289  (Mmax-1)/(Msamples-1)+1);
290 
291  for ru = 1:length(runs);
292  disp(['starting run ',runs{ru}]);
293  % storage for maximum l2-errors (i.e. linfty([0,T],l2)-norm)
294  errs = zeros(Msamples,Nsamples);
295  inds = zeros(Msamples,Nsamples);
296  mu_inds = zeros(Msamples,Nsamples);
297  % assuming, that training data is in the following directory
298  settings = load(fullfile(rbmatlabtemp,...
299  dirnames{ru},'settings.mat'));
300 
301  for mu_ind = 1:size(settings.M,2);
302  disp(['processing parameter vector ',num2str(mu_ind),...
303  '/',num2str(size(settings.M,2))]);
304  params = set_mu(settings.M(:,mu_ind),params);
305  sim_data = load_detailed_simulation(mu_ind,settings.savepath,params);
306  U_H = sim_data.U;
307  for Nind = 1:Nsamples
308  N = Ns(Nind);
309  for Mind = 1:Msamples
310  fprintf('.');
311  M = Ms(Mind);
312 
313  % nonlinear simulation
314  params.M = M;
315  params.N = N;
316  reduced_data = rb_online_prep(offline_data,params);
317  try
318  simulation_data = rb_simulation(reduced_data,params);
319  U = rb_reconstruction(detailed_data,simulation_data);
320  l2_errors = fv_l2_error(U_H,U,detailed_data.W);
321  [err,ind] = max(l2_errors);
322  if err(1)>errs(Mind,Nind)
323  errs(Mind,Nind) = err(1);
324  inds(Mind,Nind) = ind(1);
325  mu_inds(Mind,Nind) = mu_ind;
326  end;
327  catch
328  errs(Mind,Nind) = NaN;
329  inds(Mind,Nind) = NaN;
330  mu_inds(Mind,Nind) = mu_ind;
331  end
332  end;
333  end;
334  fprintf('.');
335  disp('saving temporary workspace in burgers_fv_step7_*_tmp.mat');
336  save(fullfile(rbmatlabtemp,...
337  ['burgers_fv_step7_result_',...
338  runs{ru},'_tmp.mat']));
339  end;
340  % define stability-region as error being smaller than
341  % sqrt(diffmax * area), e.g. diffmax = 4, area = 2e-7
342 
343  stab_limit = 1e-2;
344  C = ones(size(errs));
345  i = find(errs<stab_limit);
346  C(i) = 2;
347 
348  if isempty(find(C==1)); % i.e. all stable
349  f1 = figure, surf(Ms, Ns,errs');
350  else % some not stable
351  f1 = figure, surf(Ms, Ns,errs',C');
352  shading interp;
353  end;
354  % figure, pcolor(Ms, Ns,C);
355  title([runs{ru},' L-infty([0,T],L2) error']);
356  set(gca,'Zscale','log');
357  xlabel('M');
358  ylabel('N');
359  zlabel('error');
360 
361  f2 = figure, surf(Ms, Ns,inds');
362  title([runs{ru},' time index of maximum l2-error']);
363  %set(gca,'Zscale','log');
364  xlabel('M');
365  ylabel('N');
366  zlabel('time index');
367 
368  save(fullfile(rbmatlabresult,...
369  ['burgers_fv_step7_result_',...
370  runs{ru},'.mat']));
371  end;
372 
373  otherwise
374  error('step-number is unknown!');
375 end;
376 
377 
378 % TO BE ADJUSTED TO NEW SYNTAX
379 %| \docupdate