2 % quadratic poisson equation on unit interval
6 % - laplace (k u) + m u^2 = 1
11 % discretization with Finite differences
13 % inner product: l2-inner product, piecewise linear elements
15 % B. Haasdonk 6.12.2011
17 %
for detailed_simulation
18 model.mu_names = {
'k',
'm'};
19 model.mu_ranges ={[0.01,1],[0,10]};
21 model.xnumintervals = 100;
22 model.dx = (model.xrange(2)-model.xrange(1))/model.xnumintervals;
24 %
for detailed simulation
25 model.gen_model_data = @my_gen_model_data;
26 model.detailed_simulation = @my_detailed_simulation;
27 model.plot_sim_data = @my_plot_sim_data;
28 model.set_mu = @set_mu_default;
29 model.b_coefficients = @my_b_coefficients;
30 model.b_components = @my_b_components;
31 model.f_coefficients = @my_f_coefficients;
32 model.f_components = @my_f_components;
33 model.a_coefficients = @my_a_coefficients;
34 model.au_components = @my_au_components;
37 model.gen_detailed_data = @my_gen_detailed_data;
39 %%%
for real rb simulation:
40 model.rb_reconstruction = @my_rb_reconstruction;
41 model.gen_reduced_data = @my_gen_reduced_data;
42 model.rb_simulation = @my_rb_simulation;
44 model.get_rb_size = @(model,detailed_data) model.xnumintervals-1;
45 model.get_mu = @get_mu_default;
46 model.is_stationary = 1;
47 % currently no reduction of N possible, dummy subset routine:
48 model.reduced_data_subset = @(model, reduced_data) reduced_data;
49 model.get_dofs_from_sim_data = @(sim_data) sim_data.u;
50 model.gridtype =
'onedgrid';
53 model.inner_product_matrix = @my_inner_product_matrix;
54 model.RB_Mtrain_size = 1000;
55 model.Newton_eps = 1e-6;
56 model.enable_error_estimator = 0;
58 %%%%%%%%% auxiliary rb functions
60 function model_data = my_gen_model_data(model)
61 params.xrange = model.xrange;
62 params.xnumintervals = model.xnumintervals;
65 function sim_data = my_detailed_simulation(model, model_data)
66 uk = zeros(length(model_data.grid.X)-2,1);
67 K = model.inner_product_matrix(model_data);
68 b_comp = model.b_components(model);
69 b_coeff = model.b_coefficients(model);
70 b = lincomb_sequence(b_comp, b_coeff);
71 f_comp = model.f_components(model);
72 f_coeff = model.f_coefficients(model);
73 f = lincomb_sequence(f_comp,f_coeff);
74 a_coeff = model.a_coefficients(model);
78 while errsqr>model.Newton_eps;
79 au_comp = model.au_components(model,uk);
80 au = lincomb_sequence(au_comp,a_coeff);
82 F = (uk' * (au + b))' - f;
85 errsqr = min_hk'*K*min_hk;
89 sim_data.iterations = k;
90 disp(['finished detailed_simulation with ',num2str(k),' iterations'])
92 function p = my_plot_sim_data(model, detailed_data, sim_data, ...
94 uplot = [zeros(1,size(sim_data.u,2));sim_data.u;zeros(1,size(sim_data.u,2))];
95 p = plot(detailed_data.grid.X,uplot','Linewidth',2);
96 set(gca,'xlim',model.xrange);
97 set(gca,'ylim',[-1.5,1.5]);
98 if isfield(plot_params,'title')
99 title(plot_params.title);
101 if isfield(plot_params,'legend')
102 legend(plot_params.legend);
105 function detailed_data = my_gen_detailed_data(model, model_data)
106 % reduced basis by POD of some trajectories
107 detailed_data = model_data;
109 M = rand_uniform(model.RB_Mtrain_size,model.mu_ranges);
111 %[M1,M2] = meshgrid();
112 %M = [M1(:)'; M2(:)'];
113 %model.mu_ranges ={[0.01,1],[0,10]};
115 U = zeros(length(model_data.grid.X)-2,0);
117 model = set_mu(model,M(:,m));
118 sim_data = detailed_simulation(model,model_data);
122 detailed_data.K = model.inner_product_matrix(model_data);
123 detailed_data.RB = orthonormalize_gram_schmidt(U,detailed_data.K,1e-8);
124 detailed_data.b_comp = model.b_components(model);
125 detailed_data.f_comp = model.f_components(model);
126 N = size(detailed_data.RB,2);
128 model.au_components(model,detailed_data.RB(:,1));
129 Qa = length(aPhiN_comp);
130 detailed_data.aPhiN_comp = cell(Qa,N);
132 aPhiN_comp = model.au_components(model,detailed_data.RB(:,n));
134 detailed_data.aPhiN_comp{q,n} = aPhiN_comp{q};
138 function reduced_data = my_gen_reduced_data(model, detailed_data)
139 N = size(detailed_data.RB,2);
140 reduced_data = detailed_data;
141 reduced_data.KN = detailed_data.RB' * detailed_data.K * detailed_data.RB;
142 Qb = length(detailed_data.b_comp);
143 reduced_data.bN_comp = zeros(N,N,Qb);
145 reduced_data.bN_comp(:,:,q) = ...
146 detailed_data.RB' * ...
147 detailed_data.b_comp{q} ...
150 Qf = length(detailed_data.f_comp);
151 reduced_data.fN_comp = zeros(N,Qf);
153 reduced_data.fN_comp(:,q) = detailed_data.RB
' * detailed_data.f_comp{q};
155 Qa = size(detailed_data.aPhiN_comp,1);
156 reduced_data.aN_comp = zeros(N,N,N,Qa);
159 reduced_data.aN_comp(n1,:,:,q) = ...
160 detailed_data.RB' * (detailed_data.aPhiN_comp{q,n1}(:,:)) ...
165 function rb_sim_data = my_rb_simulation(model, reduced_data)
166 %rb_sim_data = detailed_simulation(model, reduced_data);
168 N = size(reduced_data.KN,1);
170 KN = reduced_data.KN;
174 b_coeff = model.b_coefficients(model);
175 bN = reduced_data.bN_comp * b_coeff;
176 f_coeff = model.f_coefficients(model);
177 fN = reduced_data.fN_comp * f_coeff;
178 a_coeff = model.a_coefficients(model);
179 Qa = length(a_coeff);
182 AN = AN + reduced_data.aN_comp(:,:,:,q) * a_coeff(q);
184 while errsqr>model.Newton_eps;
187 AuN = AuN + reshape(AN(n,:,:),[N,N]) *uNk(n);
190 FN = AuN
' * uNk + bN * uNk - fN;
193 errsqr = min_hNk'*KN*min_hNk;
196 rb_sim_data.uN = uNk;
197 rb_sim_data.iterations = k;
198 disp([
'finished rb_simulation with ',num2str(k),
' iterations'])
200 function rb_sim_data = my_rb_reconstruction(model, detailed_data, ...
202 rb_sim_data.u = detailed_data.RB * rb_sim_data.uN;
204 function K = my_inner_product_matrix(model_data);
205 % L2 inner product matrix
206 K = eye(length(model_data.grid.X)-2);
207 K = K / model_data.grid.nelements;
209 %%%%%%%%%%%%%%%%%%%%%% data functions:
211 function b_comp = my_b_components(model)
212 % matrix b(xi_i,xi_j) of detailed model
213 % single component consisting of triangular matrix
214 dx = model.k * (model.dx)^(-2);
215 d = 2* dx * ones(model.xnumintervals-1,1);
216 d1 = ones(model.xnumintervals-2,1)*(-dx);
217 b_comp = {diag(d) + diag(d1,1) + diag(d1,-1)};
219 function b_coeff = my_b_coefficients(model)
220 % matrix b(xi_i,xi_j) of detailed model
223 function f_comp = my_f_components(model);
224 % right hand side vector f(xi_i): single cell array
225 f_comp = {ones(model.xnumintervals-1,1)};
227 function f_coeff = my_f_coefficients(model)
228 % right hand side vector f(xi_i)
229 f_coeff = [1]; % always 1
231 function au_comp = my_au_components(model, u)
232 % matrix a(u,xi_i,xi_j) decomposed
235 function a_coeff = my_a_coefficients(model)
236 % matrix a(u,xi_i,xi_j)
function demo_rb_gui(varargin)
reduced basis demo with sliders
a one dimensional grid implementation
function model = quadratic_poisson_model()
quadratic poisson equation on unit interval
function [ descr , rdescr , dmodel , rmodel ] = newton(steps, combined, M_by_N_ratio, noof_ei_extensions, use_laplacian, model_size, random, num_cpus, Mstrich)
small script demonstrating a buckley leverett problem discretization and RB model reduction ...