rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
demo_ldgfunc.m
Go to the documentation of this file.
1 function demo_ldgfunc
2 %function demo_ldgfunc
3 % demo for showing ldgfunc capabilities
4 %
5 % idea is mainly to keep dofs and size information separate in
6 % order to easily work with vectors and matrices of dofs for
7 % efficiency.
8 %
9 % A slower alternative can be beneficial: by using the \\@ldg function
10 % class, a dof-vector can be stored in the object. And hence an
11 % evaluation be performed by the subsref method as for analytical functions.
12 % So ldg-objects can be used identically as any
13 % analytical function in integration, matrix assembly, etc.
14 % As classes/methods are slower than
15 % structs, this should only be used if necessary.
16 % So most ldg operations will accept separate dof vectors/matrices
17 % and size information.
18 
19 % Bernard Haasdonk 28.1.2009
20 
21 % quadrature degree for integration:
22 qdeg = 4;
23 % initialize grid
24 grid = triagrid();
25 
26 disp('');
27 disp('initialization of zero ldg function');
28 
29 % initialize basic data
30 
31 params = [];
32 params.pdeg = 1;
33 params.dimrange = 2;
34 % vectorial function
35 
36 df_info = ldginfo(params, grid);
37 %params.nelements = grid.nelements;
38 %params.ndofs = ldg_ndofs(params);
39 %params.ndofs_per_element = ldg_ndofs_per_element(params);
40 dofs = ldg_zero(df_info); % simple zero vector of dofs
41 
42 % these variables are sufficient to perform most effective operations
43 % for ldg functions, i.e. data and parameters are kept disjoint.
44 %
45 % A slower alternative can be beneficial: by using the @ldg function
46 % class, a dof-vector can be stored in the object. And hence an
47 % evaluation be performed by the subsref method as for analytical functions.
48 % So ldg-objects can be used identically as any
49 % analytical function in integration, matrix assembly, etc.
50 % As classes/methods are slower than
51 % structs, this should only be used if necessary:
52 
53 df = ldgdiscfunc(dofs,df_info); % setting of data: persistent!
54 display(df);
55 
56 % most of the following does not access df, but work with dofs and params
57 
58 disp('----------------------------------------------------------');
59 disp(['dof-access and plot of scalar component with subsampling']);
60 
61 % manipulate/read dofs and visualize
62 df.dofs = 1:df_info.ndofs;
63 [dofs_scalar1,sparams] = ldg_scalar_component(df,1);
64 df_scalar = ldgdiscfunc(dofs_scalar1, sparams);
65 plot_params = [];
66 for ssl = 0:2
67  plot_params.plot_subsampling_level = ssl;
68  figure
69  ldg_plot(df_scalar,grid,plot_params);
70  title(['dofnumbers, subsampling level ',num2str(ssl)]);
71 end;
72 disp('press key to continue');
73 pause();
74 
75 disp('');
76 disp('----------------------------------------------------------');
77 disp(['local evaluation of ldgfunc and analytical functions in point ',...
78  ' lcoord=[1/3,1/3] in first 10 elements:']);
79 % define analytical function inline, e.g. constant 1
80 f1 = @(einds,loc,grid,params) ones(length(einds),1);
81 % define analytical function by pointer to matlab function
82 f2 = @(einds,loc,grid,params) f_local(einds,loc,grid,params);
83 % define analytical function by pointer to matlab function with
84 % internal conversion of local to global coordinates:
85 f3 = @(einds,loc,grid,params) f_global(...
86  local2global(grid,einds,loc,params),params);
87 % evaluate all functions
88 disp('f1 (scalar function constant 1):');
89 f1(1:10, [1/3,1/3], grid, params)
90 disp('f2: (function f_local defined in local coordinates)');
91 f2(1:10, [1/3,1/3], grid, params)
92 disp('f3: (function f_global defined in global coordinates)');
93 f3(1:10, [1/3,1/3], grid, params)
94 % note here a ldg function is used identically as the analytical functions!
95 disp('df: (@ldgdiscfunc dimrange=2, pdeg=1, zero-function)');
96 df.dofs = ldg_zero(df_info);
97 df.grid = grid;
98 df(1:10, [1/3,1/3]) % identical call as above!
99 % this is equivalent to
100 % ldg_evaluate(dofs,1:10, [1/3,1/3], grid, params);
101 
102 disp('press key to continue');
103 pause();
104 
105 disp('');
106 disp('----------------------------------------------------------');
107 disp('test of orthogonality of element basis functions: gram matrix');
108 
109 % test orthogonality of basis functions:
110 disp('gram matrix of basis on reference element should be identity:')
111 K = ldg_local_mass_matrix(qdeg,params)
112 %f = @(lcoord) gram_matrix(ldg_evaluate_basis(lcoord,params));
113 %triaquadrature(qdeg,f)
114 
115 disp('press key to continue');
116 pause();
117 
118 disp('');
119 disp('----------------------------------------------------------');
120 disp('l2 projection of analytic function: hor/vert sinus waves');
121 
122 % l2 projection of an analytical function and plot
123 f = @(einds,loc,grid,varargin) f_global(...
124  local2global(grid,einds,loc,df_info),df_info);
125 df.dofs = ldg_l2project(f,qdeg,df_info);
126 %dofs = l2project(f,qdeg,grid,params);
127 
128 [dofs_scalar1,sparams] = ldg_scalar_component(df,1);
129 dofs_scalar2 = ldg_scalar_component(df,2);
130 df_scalar1 = ldgdiscfunc(dofs_scalar1, sparams);
131 df_scalar2 = ldgdiscfunc(dofs_scalar2, sparams);
132 plot_params.plot_subsampling_level = 2;
133 figure,ldg_plot(df_scalar1,grid,plot_params);title('component1');
134 figure,ldg_plot(df_scalar2,grid,plot_params);title('component2');
135 
136 disp('press key to continue');
137 pause();
138 
139 disp('');
140 disp('----------------------------------------------------------');
141 disp('l2-error of zero ldg function with unity (should be 1)');
142 
143 qdeg = 5;
144 pdeg = 3;
145 dimrange = 1;
146 params = ldg_params(grid.nelements,pdeg,dimrange);
147 df_info = ldginfo(params, grid);
148 
149 % initialize zero function:
150 df0 = ldgdiscfunc([],df_info);
151 df.dofs = ldg_zero(params);
152 
153 % initialize const-one function analytically
154 f1 = @(einds,loc,grid,params) ones(length(einds),1);
155 % and project to ldgfunc
156 dofs1 = ldg_l2project(f1,qdeg,df_info);
157 
158 df1 = ldgdiscfunc(dofs1,df_info);
159 
160 
161 % compute errors with analytical and discrete function:
162 % identical syntax by using ldg class!!!
163 params.evaluate = @ldg_evaluate;
164 
165 
166 
167 err_df0_f1 = ldg_l2error(df0,f1,qdeg);
168 disp(['l2 error (df0,f1) =',num2str(err_df0_f1)]);
169 err_df0_df1 = ldg_l2error(df0,df1,qdeg);
170 disp(['l2 error (df0,df1) =',num2str(err_df0_df1)]);
171 
172 disp('press key to continue');
173 pause();
174 
175 disp('');
176 disp('----------------------------------------------------------');
177 disp(['Table of l2-errors of projection with increasing p, should' ...
178  ' converge to 0']);
179 % l2-difference between ldgdiscfunc and function with
180 % entity-local-coord evaluation possibility.
181 dimrange = 2;
182 qdeg = 10;
183 for p = 1:4
184  params = ldg_params(grid.nelements,p,dimrange);
185  df_info = ldginfo(params, grid);
186 
187  dofs = ldg_l2project(f,qdeg,df_info);
188  df = ldgdiscfunc(dofs,df_info);
189 
190  l2err = ldg_l2error(df,f,qdeg);
191  disp(['pol-deg = ',num2str(p),', l2error(df,f) = ',num2str(l2err)]);
192 end;
193 
194 disp('press key to continue');
195 pause();
196 
197 return;
198 
199 % l2-difference between two ldgdiscfuncs works identical
200 disp(['l2error(df,df) =',...
201  num2str(l2error(df,df,qdeg,grid,params))]);
202 
203 % arithmetics of ldg functions: sum, diff, scal-mtimes,
204 % in particular scalar product of vectorial localdgs
205 
206 
207 
208 % right hand side assembly of LGS
209 
210 
211 
212 % edge quadratures
213 
214 
215 
216 % gradient operator
217 
218 
219 
220 % div0 projection operator
221 
222 
223 
224 % second operator
225 
226 
227 
228 % concatenate for complete space operator
229 
230 
231 
232 
233 % implicit or explicit solution of time step
234 
235 
236 
237 
238 % stiffness-matrix assembly into sparse matrix
239 
240 
241 
242 
243 % solve FEM problem
244 
245 % stop routine for inspection of workspace variables
246 keyboard;
247 
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 function F = f_local(einds,loc,grid,params)
250 %function F = f_local(einds,loc,grid,params)
251 %
252 % function to be evaluated in local coordinates, i.e.
253 % glob = [X, Y] with X and Y column vectors of global coordinates,
254 % producing a corresponding
255 % result-matrix in F (each row one result)
256 % such functions can also be used with 3-dim coordinates without
257 % any change!
258 dimrange = 1;
259 F = zeros(size(einds,2),dimrange);
260 F(:,1) = einds;
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 
263 
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 function F = f_global(glob,params)
266 %function F = f_global(glob,params)
267 % function to be evaluated in gobal coordinates, i.e.
268 % glob = [X, Y] with X and Y column vectors of global coordinates,
269 % producing a corresponding
270 % result-matrix in F (each row one result)
271 % such functions can also be used with 3-dim coordinates without
272 % any change!
273 dimrange = 2;
274 F = zeros(size(glob,1),dimrange);
275 F(:,1) = sin(pi*glob(:,1));
276 F(:,2) = cos(pi*glob(:,2));
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 
279 %| \docupdate
A triangular conforming grid in two dimensions.
Definition: triagrid.m:17
structure for the information of a ldg-function.
Definition: ldginfo.m:17
an ldg shape functions implementation
Definition: ldgdiscfunc.m:17
function demo_ldgfunc()
demo for showing ldgfunc capabilities
Definition: demo_ldgfunc.m:17