rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
demo_fem2.m
Go to the documentation of this file.
1 function demo_fem2()
2 %function demo_fem2()
3 % Demo of +Fem usage for finite elements simulations of linear and
4 % stationary models: (i) Poisson, (ii) Stokes and nonlinear models:
5 % (iii) Navier-Stokes.
6 
7 % IM, 09.09.2016
8 
9 % set up model:
10 fprintf('\n');
11 disp('(a) P_k-FEM on triangular grid for Poisson problem');
12 disp('==================================================');
13 params = [];
14 params.pdeg = 2;
15 params.numintervals = 2*2^4;
16 model = poisson_model(params);
18 model_data = gen_model_data(model);
19 
20 % Simulation:
21 sim_data = detailed_simulation(model, model_data);
22 
23 % plot:
24 disp(['ndofs = ',num2str(sim_data.uh.ndofs)]);
25 disp('plotting solution ...');
26 figure, plot_sim_data(model, model_data, sim_data, []);
27 title('FEM solution of -Laplace u = f for zero-boundary values')
28 
29 disp('press enter to continue');
30 pause;
31 close all
32 
33 % eoc:
34 fprintf('\n');
35 disp('error convergence table:')
36 
37 i_s = [0,1,2,3,4];
38 nis = length(i_s);
39 l2errs = zeros(nis,1);
40 h1errs = zeros(nis,1);
41 ndofs = zeros(nis,1);
42 
43 for pdeg = 1:2
44  fprintf('\n');
45  disp([' *** Convergence table for pdeg = ',num2str(pdeg),' ***']);
46  disp(' 1/numintervals | ndofs | L2-error | H1-error ')
47  disp('-------------------------------------------------------------------')
48  for i = 1:length(i_s)
49  params.numintervals = 2*2^i_s(i);
50  params.pdeg = pdeg;
51  model = poisson_model(params);
52  model_data = gen_model_data(model);
53  sim_data = detailed_simulation(model, model_data);
54 
55  % project exact solution onto higher degree polynomial fem func
56  p4_df_info = Fem.Lagrange.DefaultInfo(4, model_data.grid);
57  uexact_h = interpol_global(p4_df_info, model.solution);
58  uh = interpol_local(p4_df_info, @(grid, el, lcoord, params) sim_data.uh(el, lcoord, params));
59 
60  err = uh - uexact_h;
61  l2errs(i) = l2_norm(err);
62  h1errs(i) = h1_norm(err);
63  ndofs(i) = sim_data.uh.ndofs;
64  disp([' ',num2str(1/params.numintervals,'%10.5e'),' | ',...
65  num2str(ndofs(i),'%10.4d'),' | ',...
66  num2str(l2errs(i),'%10.5e'),' | ',...
67  num2str(h1errs(i),'%10.5e')]);
68  end
69 end
70 
71 disp('press enter to continue');
72 pause;
73 
74 % Stokes: Mini-Element
75 fprintf('\n');
76 disp('(b) Part 1: Mini-FE for Stokes problem');
77 disp('======================================');
78 
79 params = [];
80 params.mesh_number = 1;
81 model = laminar_flow_model(params);
82 model.has_nonlinearity = 0; % linear
83 model.df_type = 'stokes_mini'; % Mini Element
84 
85 % generate and plot grid:
86 model_data = gen_model_data(model);
87 disp('plotting grid ...');
88 figure, plot(model_data.grid);
89 axis tight
90 axis equal
91 title('Grid for Stokes problem');
92 
93 % simulation and plot:
94 sim_data = detailed_simulation(model, model_data);
95 disp('plotting solution ...');
96 plot_params = [];
97 plot_params.modes = {'velocity_abs'}; % for now, plot only velocity
98 plot_sim_data(model, model_data, sim_data, plot_params);
99 
100 disp('press enter to continue');
101 pause;
102 
103 % Taylor-Hood
104 fprintf('\n');
105 disp('(b) Part 2: Taylor-Hood FE for Stokes problem');
106 disp('=============================================');
107 
108 % change model, new computations
109 model.df_type = 'taylor_hood'; % P2-P1
110 model_data = gen_model_data(model);
111 sim_data = detailed_simulation(model, model_data);
112 disp('plotting solution ...');
113 plot_sim_data(model, model_data, sim_data, plot_params);
114 
115 disp('press enter to continue');
116 pause;
117 close all
118 
119 % demonstrate precomputation of operator components (Fem.OperatorsDefault
120 % class)
121 fprintf('\n');
122 disp('Operator components can be precomputed (parameter decomposition):');
123 
124 % finer mesh:
125 params.mesh_number = 4;
126 model = laminar_flow_model(params);
127 model.has_nonlinearity = 0;
128 model_data = gen_model_data(model);
129 sim_data = detailed_simulation(model, model_data);
130 disp(['Simulation time (full assembly): t = ', num2str(sim_data.total_time)]);
131 
132 fprintf('Precompute operator components in ...');
133 tic;
134 model_data.operators.assemble_components(model, model_data);
135 fprintf([' t = ', num2str(toc), '\n']);
136 
137 sim_data = detailed_simulation(model, model_data);
138 disp(['Simulation time (using stored data): t = ', num2str(sim_data.total_time)]);
139 
140 % erase components:
141 model_data.operators.clear_components();
142 
143 disp('press enter to continue');
144 pause;
145 
146 % nonlinear model:
147 fprintf('\n');
148 disp('(c): Nonlinear Stokes problem (CFD benchmark)');
149 disp('=============================================');
150 params.mesh_number = 1;
151 model = laminar_flow_model(params);
152 model_data = gen_model_data(model);
153 model.verbose = 4;
154 disp(['Simulation for RE = ', num2str(model.get_reynolds_number(model))]);
155 sim_data = detailed_simulation(model, model_data);
156 disp('plotting solution ...');
157 plot_sim_data(model, model_data, sim_data, plot_params);
158 
159 % higher reynolds number:
160 fprintf('\n');
161 model = set_mu(model, 2);
162 disp(['Simulation for RE = ', num2str(model.get_reynolds_number(model))]);
163 sim_data = detailed_simulation(model, model_data);
164 disp('plotting solution ...');
165 plot_sim_data(model, model_data, sim_data, plot_params);
166 
167 disp('press enter to continue');
168 pause;
169 
170 % finer mesh for demonstrating caching
171 fprintf('\n');
172 disp('Speed-up assembly by caching:');
173 disp('Perform simulation on finer mesh:');
174 fprintf('(without caching) ');
175 params.mesh_number = 4;
176 model = laminar_flow_model(params);
177 model = set_mu(model, 2);
178 model.verbose = 3; % less information
179 cache('limit', 2e8);
180 model_data = gen_model_data(model);
181 detailed_simulation(model, model_data);
182 
183 fprintf('(with caching) ');
184 model.disable_caching = 0;
185 sim_data = detailed_simulation(model, model_data);
186 
187 disp('press enter to continue');
188 pause;
189 close all
190 
191 % plotting functionality
192 fprintf('\n');
193 disp('Show all plot modes for Stokes problems:');
194 plot_params.modes = {...
195  'pressure', 'velocity', 'velocity_xcomp', 'velocity_ycomp', ...
196  'velocity_abs', 'velocity_vec', 'velocity_vec_plus_pressure'};
197 plot_params.vector_distance = 0.03;
198 plot_sim_data(model, model_data, sim_data, plot_params);
199 
200 disp('press enter to continue');
201 pause;
202 
203 % compute benchmark values and compare to reference
204 fprintf('\n');
205 disp('Compute drag and lift coefficients on different grids:');
206 disp('Reference values (benchmark): C_D = 5.57953523384, C_L = 0.010618948146');
207 fprintf('\n');
208 disp(' h | ndofs | C_D | C_L');
209 disp('----------------------------------------------------------');
210 
211 for i = 1:4
212  params.mesh_number = i;
213  model = laminar_flow_model(params);
214  model_data = gen_model_data(model);
215  model = set_mu(model, 1);
216  model.fp_maxiter = 17;
217  sim_data = detailed_simulation(model, model_data);
218  [C_D, C_L] = stokes_compute_drag_and_lift(model, model_data, sim_data);
219  disp([' ' ,num2str(max(max(model_data.grid.EL)), '%.3e'), ...
220  ' | ', num2str(model_data.df_info.ndofs, '%.3e'),' | ', ...
221  num2str(C_D, '%.4f'),' | ', num2str(C_L, '%.4f')]);
222 end
223 
224 disp('press enter to terminate (close all windows)');
225 pause;
226 close all
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...
Definition: verbose.m:17
Fem operators can be obtained from stored components.
function model = generic_fem_model_adapter(model)
Initializes a default linear and stationary model for more generic fem discretization by modifying mo...
function model = laminar_flow_model(params)
Model of laminar flow (steady Navier-Stokes) around cylinder in a pipe.
function [ Cd , Cl ] = stokes_compute_drag_and_lift(model, model_data, sim_data)
Drag and lift coefficients are computed via volume integrals.
function p = plot_sim_data(model, model_data, sim_data, plot_params)
function performing the plot of the simulation results as specified in model.
Definition: plot_sim_data.m:17
function demo_fem2()
Demo of +Fem usage for finite elements simulations of linear and stationary models: (i) Poisson...
Definition: demo_fem2.m:17