rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
demo_fem.m
Go to the documentation of this file.
1 function demo_fem;
2 % demo of a finite element simulation
3 %
4 % Script solving a laplace problem (-laplace u = 0) with suitable
5 % general dirichlet and neuman boundary conditions.
6 
7 % Part 1:
8 % direct assembly of Q1-FEM on cartesian grids
9 % Part 2:
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
14 %
15 % See also demo_fem2 for examples using
16 % the more general RBmatlab +Fem Routines
17 % (including Stokes/Navier-Stokes examples)
18 
19 % Bernard Haasdonk 5.7.2006
20 
21 disp('===========================================')
22 disp('Part A: bilinear Q1-FEM on cartesian grid')
23 
24 disp('Demonstration that solves a laplace-problem -laplace u = 0:')
25 disp('with nontrivial Dirichlet and Neumann values.');
26 
27 params = [];
28 
29 disp('setting parameters');
30 
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
35 
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
40 
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
44 
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);
52  ];
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)
59  ];
60 params.bnd_rect_corner2 = params.bnd_rect_corner2' + eps;
61 
62 params.bnd_rect_index = [-1, -2, -2, -2, -2];
63 
64 params.gridtype = 'rectgrid';
65 grid = construct_grid(params);
66 plot(grid);
67 title('FEM grid');
68 
69 % set data functions
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;
74 
75 %params.name_dirichlet_values = 'homogeneous';
76 %params.c_dir = 1.0;
77 params.name_neuman_values = 'pressure_gdl';
78 params.c_neu_max = 1000; % maximum velocity in middle of left and right side
79 
80 %params.name_neuman_values = 'homogeneous';
81 %params.c_neu = 0;
82 
83 params.show_colorbar = 1;
84 params.nolines = 1;
85 params.verbose = 10;
86 
87 disp('solving system');
88 p = fem_laplace(grid,params);
89 
90 disp('plotting result');
91 figure;
92 
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');
96 
97 disp('press key to continue')
98 pause;
99 
100 
101 
102 
103 disp('===============================================================')
104 disp('Part B: P_k-FEM on triangular grid and convergence order study')
105 
106  % simple FEM simulation and plot
107  % initialize model:
108  params = [];
109  params.pdeg = 2;
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
119  figure, plot_sim_data(model,model_data,sim_data,[]);
120  title('FEM solution of -Laplace u = f for zero-boundary values')
121  disp(['ndofs = ',num2str(sim_data.uh.df_info.ndofs)]);
122 
123  disp('===============================')
124  disp('error convergence table:')
125 
126  i_s = [0,1,2,3,4];
127  nis = length(i_s);
128  l2errs = zeros(nis,1);
129  h1errs = zeros(nis,1);
130  ndofs = zeros(nis,1);
131 
132  for pdeg = 1:2
133  disp('===================================================================')
134  disp(['Convergence table for pdeg = ',num2str(pdeg)]);
135  disp(' ');
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);
140  params.pdeg = pdeg;
141  model = poisson_model(params);
142  model_data = gen_model_data(model);
143  sim_data = detailed_simulation(model,model_data);
144 
145  % project exact solution onto higher degree polynomial fem func
146  par.pdeg = 4;
147  par.qdeg = 8;
148  par.dimrange = 1;
149  p4_df_info = feminfo(par,model_data.grid);
150 
151  uexact_h = femdiscfunc([],p4_df_info);
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);
157 
158  err = uh - uexact_h;
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')]);
166  end;
167  end;
168 
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.
Definition: femdiscfunc.m:17
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
function demo_fem()
demo of a finite element simulation
Definition: demo_fem.m:17
A cartesian rectangular grid in two dimensions with axis parallel elements.
Definition: rectgrid.m:17
structure representing the fem-space information shared by all fem-functions. Implemented as handle c...
Definition: feminfo.m:17
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