rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
quadratic_poisson_model.m
Go to the documentation of this file.
1 function model = quadratic_poisson_model;
2 % quadratic poisson equation on unit interval
3 %
4 % u on [0,1] such that
5 %
6 % - laplace (k u) + m u^2 = 1
7 % u(0) = u(1) = 0
8 %
9 % parameters k and m
10 %
11 % discretization with Finite differences
12 %
13 % inner product: l2-inner product, piecewise linear elements
14 
15 % B. Haasdonk 6.12.2011
16 
17 % for detailed_simulation
18 model.mu_names = {'k','m'};
19 model.mu_ranges ={[0.01,1],[0,10]};
20 model.xrange = [0,1];
21 model.xnumintervals = 100;
22 model.dx = (model.xrange(2)-model.xrange(1))/model.xnumintervals;
23 
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;
35 
36 % for dummy demo_rb_gui
37 model.gen_detailed_data = @my_gen_detailed_data;
38 
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;
43 
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';
51 model.no_lines = 0;
52 model.axis_equal = 0;
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;
57 
58 %%%%%%%%% auxiliary rb functions
59 
60 function model_data = my_gen_model_data(model)
61 params.xrange = model.xrange;
62 params.xnumintervals = model.xnumintervals;
63 model_data.grid = onedgrid(params);
64 
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);
75 % Newton loop
76 errsqr = 1e10;
77 k = 0;
78 while errsqr>model.Newton_eps;
79  au_comp = model.au_components(model,uk);
80  au = lincomb_sequence(au_comp,a_coeff);
81  DFu = 2*au + b;
82  F = (uk' * (au + b))' - f;
83  min_hk = DFu\F;
84  uk = uk - min_hk;
85  errsqr = min_hk'*K*min_hk;
86  k = k+1;
87 end;
88 sim_data.u = uk;
89 sim_data.iterations = k;
90 disp(['finished detailed_simulation with ',num2str(k),' iterations'])
91 
92 function p = my_plot_sim_data(model, detailed_data, sim_data, ...
93  plot_params)
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);
100 end;
101 if isfield(plot_params,'legend')
102  legend(plot_params.legend);
103 end;
104 
105 function detailed_data = my_gen_detailed_data(model, model_data)
106 % reduced basis by POD of some trajectories
107 detailed_data = model_data;
108 % random point set:
109 M = rand_uniform(model.RB_Mtrain_size,model.mu_ranges);
110 % uniform mesh:
111 %[M1,M2] = meshgrid();
112 %M = [M1(:)'; M2(:)'];
113 %model.mu_ranges ={[0.01,1],[0,10]};
114 ntrain = size(M,2);
115 U = zeros(length(model_data.grid.X)-2,0);
116 for m = 1:ntrain
117  model = set_mu(model,M(:,m));
118  sim_data = detailed_simulation(model,model_data);
119  U = [U,sim_data.u];
120 end;
121 % orthonormalize U:
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);
127 aPhiN_comp = ...
128  model.au_components(model,detailed_data.RB(:,1));
129 Qa = length(aPhiN_comp);
130 detailed_data.aPhiN_comp = cell(Qa,N);
131 for n = 1:N
132  aPhiN_comp = model.au_components(model,detailed_data.RB(:,n));
133  for q = 1:Qa
134  detailed_data.aPhiN_comp{q,n} = aPhiN_comp{q};
135  end;
136 end;
137 
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);
144 for q = 1:Qb
145  reduced_data.bN_comp(:,:,q) = ...
146  detailed_data.RB' * ...
147  detailed_data.b_comp{q} ...
148  * detailed_data.RB;
149 end;
150 Qf = length(detailed_data.f_comp);
151 reduced_data.fN_comp = zeros(N,Qf);
152 for q = 1:Qf
153  reduced_data.fN_comp(:,q) = detailed_data.RB' * detailed_data.f_comp{q};
154 end;
155 Qa = size(detailed_data.aPhiN_comp,1);
156 reduced_data.aN_comp = zeros(N,N,N,Qa);
157 for n1 = 1:N
158  for q = 1:Qa
159  reduced_data.aN_comp(n1,:,:,q) = ...
160  detailed_data.RB' * (detailed_data.aPhiN_comp{q,n1}(:,:)) ...
161  * detailed_data.RB;
162  end;
163 end;
164 
165 function rb_sim_data = my_rb_simulation(model, reduced_data)
166 %rb_sim_data = detailed_simulation(model, reduced_data);
167 %N = model.N;
168 N = size(reduced_data.KN,1);
169 uNk = zeros(N,1);
170 KN = reduced_data.KN;
171 % newton loop
172 errsqr = 1e10;
173 k = 0;
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);
180 AN = zeros(N,N,N);
181 for q = 1:Qa
182  AN = AN + reduced_data.aN_comp(:,:,:,q) * a_coeff(q);
183 end;
184 while errsqr>model.Newton_eps;
185  AuN = zeros(N,N);
186  for n = 1:N
187  AuN = AuN + reshape(AN(n,:,:),[N,N]) *uNk(n);
188  end;
189  DFuN = 2* AuN + bN;
190  FN = AuN' * uNk + bN * uNk - fN;
191  min_hNk = DFuN\FN;
192  uNk = uNk - min_hNk;
193  errsqr = min_hNk'*KN*min_hNk;
194  k = k+1;
195 end;
196 rb_sim_data.uN = uNk;
197 rb_sim_data.iterations = k;
198 disp(['finished rb_simulation with ',num2str(k),' iterations'])
199 
200 function rb_sim_data = my_rb_reconstruction(model, detailed_data, ...
201  rb_sim_data)
202 rb_sim_data.u = detailed_data.RB * rb_sim_data.uN;
203 
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;
208 
209 %%%%%%%%%%%%%%%%%%%%%% data functions:
210 
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)};
218 
219 function b_coeff = my_b_coefficients(model)
220 % matrix b(xi_i,xi_j) of detailed model
221 b_coeff = [model.k];
222 
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)};
226 
227 function f_coeff = my_f_coefficients(model)
228 % right hand side vector f(xi_i)
229 f_coeff = [1]; % always 1
230 
231 function au_comp = my_au_components(model, u)
232 % matrix a(u,xi_i,xi_j) decomposed
233 au_comp = {diag(u)};
234 
235 function a_coeff = my_a_coefficients(model)
236 % matrix a(u,xi_i,xi_j)
237 a_coeff = [model.m];
238 
239 
240 
241 
function demo_rb_gui(varargin)
reduced basis demo with sliders
Definition: demo_rb_gui.m:17
a one dimensional grid implementation
Definition: onedgrid.m:17
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 ...
Definition: newton.m:17