2 % demo of a finite element simulation
4 % Script solving a laplace problem (-laplace u = 0) with suitable
5 % general dirichlet and neuman boundary conditions.
8 % direct assembly of Q1-FEM on cartesian grids
10 % arbitrary degree P_k triangular elements on triangular grid
11 % via RBmatlab fem routines
12 % Part 3: Convergence order study of RBmatlab P_k finite elements.
13 % via RBmatlab fem routines
16 % the more general RBmatlab +
Fem Routines
17 % (including Stokes/Navier-Stokes examples)
19 % Bernard Haasdonk 5.7.2006
21 disp('===========================================')
22 disp('Part A: bilinear Q1-FEM on cartesian grid')
24 disp('Demonstration that solves a laplace-problem -laplace u = 0:')
25 disp('with nontrivial Dirichlet and Neumann values.');
29 disp('setting parameters');
31 params.xrange = [0,1]; % grid x-coordinate range
32 params.yrange = [0,1]; % grid y-coordinate range
33 params.xnumintervals = 100; % number of grid cells in x-direction
34 params.ynumintervals = 50; % number of grid cells in y-direction
36 % the following exceeds the size of sparse-matrices, which can be
37 % linearly indexed (180000 elements, ... sec comp time)
38 %params.nx = 600; % number of grid cells in x-direction
39 %params.ny = 300; % number of grid cells in y-direction
41 % the following is trivially solvable by linear indices <= 1.6e9
42 %params.nx = 200; % number of grid cells in x-direction
43 %params.ny = 100; % number of grid cells in y-direction
45 % set upper boundary to dirichlet, middle of upper and the lower boundary
46 % to neuman, set left and right to neuman
47 params.bnd_rect_corner1 = [params.xrange(1) , params.yrange(2) ; ...
48 params.xrange(1)+ 250e-6 , params.yrange(2); ...
49 params.xrange(1) , params.yrange(1); ...
50 params.xrange(1) , params.yrange(1); ...
51 params.xrange(2) , params.yrange(1);
53 params.bnd_rect_corner1 = params.bnd_rect_corner1' - eps;
54 params.bnd_rect_corner2 = [params.xrange(2), params.yrange(2); ...
55 params.xrange(2)-250e-6, params.yrange(2); ...
56 params.xrange(2), params.yrange(1); ...
57 params.xrange(1), params.yrange(2); ...
58 params.xrange(2), params.yrange(2)
60 params.bnd_rect_corner2 = params.bnd_rect_corner2' + eps;
62 params.bnd_rect_index = [-1, -2, -2, -2, -2];
65 grid = construct_grid(params);
70 params.name_dirichlet_values = 'leftright';
71 params.c_dir_left = 1.0; % pressure left
72 params.c_dir_right = 0.5; % pressure right
73 params.dir_middle = params.xrange(2)/2;
75 %params.name_dirichlet_values = 'homogeneous';
77 params.name_neuman_values = 'pressure_gdl';
78 params.c_neu_max = 1000; % maximum velocity in middle of left and right side
80 %params.name_neuman_values = 'homogeneous';
83 params.show_colorbar = 1;
87 disp('solving system');
88 p = fem_laplace(grid,params);
90 disp('plotting result');
93 plot_vertex_data(grid,p,params);
94 %plot_p1_data(p,params);
95 title('FEM solution of -Laplace u = 0 with nontrivial boundary values');
97 disp('press key to continue')
103 disp('===============================================================')
104 disp('Part B: P_k-FEM on triangular grid and convergence order study')
106 % simple FEM simulation and plot
110 params.numintervals = 2*2^4;
111 model = poisson_model(params);
112 % generate grid and fem matrices:
113 model_data = gen_model_data(model);
114 figure, plot(model_data.grid);
115 axis equal; axis tight; title('FEM grid')
116 % perform full simulation
117 sim_data = detailed_simulation(model, model_data);
118 % plot results and provide slider
120 title('FEM solution of -Laplace u = f for zero-boundary values')
121 disp(['ndofs = ',num2str(sim_data.uh.df_info.ndofs)]);
123 disp('===============================')
124 disp('error convergence table:')
128 l2errs = zeros(nis,1);
129 h1errs = zeros(nis,1);
130 ndofs = zeros(nis,1);
133 disp('===================================================================')
134 disp(['Convergence table for pdeg = ',num2str(pdeg)]);
136 disp(' 1/numintervals | ndofs | L2-error | H1-error ')
137 disp('-------------------------------------------------------------------')
138 for i = 1:length(i_s)
139 params.numintervals = 2*2^i_s(i);
141 model = poisson_model(params);
142 model_data = gen_model_data(model);
143 sim_data = detailed_simulation(model,model_data);
145 % project exact solution onto higher degree polynomial fem func
149 p4_df_info =
feminfo(par,model_data.grid);
152 uexact_h = fem_interpol_global(model.solution,uexact_h);
153 uh = femdiscfunc([],p4_df_info);
154 u_local_eval = @(grid,elids,lcoord,params) ...
155 my_uh_local_eval(grid,elids,lcoord,params,sim_data.uh);
156 uh = fem_interpol_local(u_local_eval,uh);
159 l2errs(i) = fem_l2_norm(err);
160 h1errs(i) = fem_h1_norm(err);
161 ndofs(i) = sim_data.uh.df_info.ndofs;
162 disp([' ',num2str(1/params.numintervals,'%10.5e'),' | ',...
163 num2str(ndofs(i),'%10.4d'),' | ',...
164 num2str(l2errs(i),'%10.5e'),' | ',...
165 num2str(h1errs(i),'%10.5e')]);
169 function res = my_uh_local_eval(grid,elids,lcoord,params,df)
170 % dummy function used for evaluating a discrete function at finer
171 % lagrange-grid nodes
172 res = fem_evaluate(df,elids,lcoord,[],[]);
class representing a continous piecewise polynomial function of arbitrary dimension. DOFS correspond to the values of Lagrange-nodes.
function r = verbose(level, message, messageId)
This function displays messages depending on a message-id and/or a level. Aditionally you can set/res...
function demo_fem()
demo of a finite element simulation
A cartesian rectangular grid in two dimensions with axis parallel elements.
structure representing the fem-space information shared by all fem-functions. Implemented as handle c...
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...