3 % Demo of +
Fem usage
for finite elements simulations of linear and
4 % stationary models: (i) Poisson, (ii) Stokes and nonlinear models:
11 disp(
'(a) P_k-FEM on triangular grid for Poisson problem');
12 disp(
'==================================================');
15 params.numintervals = 2*2^4;
16 model = poisson_model(params);
18 model_data = gen_model_data(model);
21 sim_data = detailed_simulation(model, model_data);
24 disp([
'ndofs = ',num2str(sim_data.uh.ndofs)]);
25 disp(
'plotting solution ...');
27 title(
'FEM solution of -Laplace u = f for zero-boundary values')
29 disp('press enter to continue');
35 disp('error convergence table:')
39 l2errs = zeros(nis,1);
40 h1errs = zeros(nis,1);
45 disp([' *** Convergence table for pdeg = ',num2str(pdeg),' ***']);
46 disp(' 1/numintervals | ndofs | L2-error | H1-error ')
47 disp('-------------------------------------------------------------------')
49 params.numintervals = 2*2^i_s(i);
51 model = poisson_model(params);
52 model_data = gen_model_data(model);
53 sim_data = detailed_simulation(model, model_data);
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));
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')]);
71 disp('press enter to continue');
74 % Stokes: Mini-Element
76 disp('(b) Part 1: Mini-FE for Stokes problem');
77 disp('======================================');
80 params.mesh_number = 1;
82 model.has_nonlinearity = 0; % linear
83 model.df_type = 'stokes_mini'; % Mini Element
85 % generate and plot grid:
86 model_data = gen_model_data(model);
87 disp('plotting grid ...');
88 figure, plot(model_data.grid);
91 title('Grid for Stokes problem');
93 % simulation and plot:
94 sim_data = detailed_simulation(model, model_data);
95 disp('plotting solution ...');
97 plot_params.modes = {
'velocity_abs'}; %
for now, plot only velocity
100 disp(
'press enter to continue');
105 disp(
'(b) Part 2: Taylor-Hood FE for Stokes problem');
106 disp(
'=============================================');
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 ...');
115 disp(
'press enter to continue');
122 disp('Operator components can be precomputed (parameter decomposition):');
125 params.mesh_number = 4;
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)]);
132 fprintf('Precompute operator components in ...');
134 model_data.operators.assemble_components(model, model_data);
135 fprintf([' t = ', num2str(toc), '\n']);
137 sim_data = detailed_simulation(model, model_data);
138 disp(['Simulation time (using stored data): t = ', num2str(sim_data.total_time)]);
141 model_data.operators.clear_components();
143 disp('press enter to continue');
148 disp('(c): Nonlinear Stokes problem (CFD benchmark)');
149 disp('=============================================');
150 params.mesh_number = 1;
152 model_data = gen_model_data(model);
154 disp(['Simulation for RE = ', num2str(model.get_reynolds_number(model))]);
155 sim_data = detailed_simulation(model, model_data);
156 disp('plotting solution ...');
159 % higher reynolds number:
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 ...');
167 disp('press enter to continue');
170 % finer mesh for demonstrating caching
172 disp('Speed-up assembly by caching:');
173 disp('Perform simulation on finer mesh:');
174 fprintf('(without caching) ');
175 params.mesh_number = 4;
177 model = set_mu(model, 2);
178 model.verbose = 3; % less information
180 model_data = gen_model_data(model);
181 detailed_simulation(model, model_data);
183 fprintf('(with caching) ');
184 model.disable_caching = 0;
185 sim_data = detailed_simulation(model, model_data);
187 disp('press enter to continue');
191 % plotting functionality
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;
200 disp(
'press enter to continue');
203 % compute benchmark values and compare to reference
205 disp(
'Compute drag and lift coefficients on different grids:');
206 disp(
'Reference values (benchmark): C_D = 5.57953523384, C_L = 0.010618948146');
208 disp(
' h | ndofs | C_D | C_L');
209 disp(
'----------------------------------------------------------');
212 params.mesh_number = i;
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);
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')]);
224 disp(
'press enter to terminate (close all windows)');
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...
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.
function demo_fem2()
Demo of +Fem usage for finite elements simulations of linear and stationary models: (i) Poisson...