rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
demo_ldgfunc.m
1 function demo_ldgfunc
2 %function demo_ldgfunc
3 %
4 % demo for showing ldgfunc capabilities
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 sparams = struct(sparams);
65 for ssl = 0:2
66  sparams.plot_subsampling_level = ssl;
67  figure
68  ldg_plot(dofs_scalar1,grid,sparams);
69  title(['dofnumbers, subsampling level ',num2str(ssl)]);
70 end;
71 disp('press key to continue');
72 pause();
73 
74 disp('');
75 disp('----------------------------------------------------------');
76 disp(['local evaluation of ldgfunc and analytical functions in point ',...
77  ' lcoord=[1/3,1/3] in first 10 elements:']);
78 % define analytical function inline, e.g. constant 1
79 f1 = @(einds,loc,grid,params) ones(length(einds),1);
80 % define analytical function by pointer to matlab function
81 f2 = @(einds,loc,grid,params) f_local(einds,loc,grid,params);
82 % define analytical function by pointer to matlab function with
83 % internal conversion of local to global coordinates:
84 f3 = @(einds,loc,grid,params) f_global(...
85  local2global(grid,einds,loc,params),params);
86 % evaluate all functions
87 disp('f1 (scalar function constant 1):');
88 f1(1:10, [1/3,1/3], grid, params)
89 disp('f2: (function f_local defined in local coordinates)');
90 f2(1:10, [1/3,1/3], grid, params)
91 disp('f3: (function f_global defined in global coordinates)');
92 f3(1:10, [1/3,1/3], grid, params)
93 % note here a ldg function is used identically as the analytical functions!
94 disp('df: (@ldgdiscfunc dimrange=2, pdeg=1, zero-function)');
95 df.dofs = ldg_zero(df_info);
96 df.grid = grid;
97 df(1:10, [1/3,1/3]) % identical call as above!
98 % this is equivalent to
99 % ldg_evaluate(dofs,1:10, [1/3,1/3], grid, params);
100 
101 disp('press key to continue');
102 pause();
103 
104 disp('');
105 disp('----------------------------------------------------------');
106 disp('test of orthogonality of element basis functions: gram matrix');
107 
108 % test orthogonality of basis functions:
109 disp('gram matrix of basis on reference element should be identity:')
110 K = ldg_local_mass_matrix(qdeg,params)
111 %f = @(lcoord) gram_matrix(ldg_evaluate_basis(lcoord,params));
112 %triaquadrature(qdeg,f)
113 
114 disp('press key to continue');
115 pause();
116 
117 disp('');
118 disp('----------------------------------------------------------');
119 disp('l2 projection of analytic function: hor/vert sinus waves');
120 
121 % l2 projection of an analytical function and plot
122 f = @(einds,loc,grid) f_global(...
123  local2global(grid,einds,loc,df_info),df_info);
124 df.dofs = ldg_l2project(f,qdeg,df_info);
125 %dofs = l2project(f,qdeg,grid,params);
126 
127 [dofs_scalar1,sparams] = ldg_scalar_component(df,1);
128 dofs_scalar2 = ldg_scalar_component(df,2);
129 sparams = struct(sparams);
130 sparams.plot_subsampling_level = 2;
131 figure,ldg_plot(dofs_scalar1,grid,sparams);title('component1');
132 figure,ldg_plot(dofs_scalar2,grid,sparams);title('component2');
133 
134 disp('press key to continue');
135 pause();
136 
137 disp('');
138 disp('----------------------------------------------------------');
139 disp('l2-error of zero ldg function with unity (should be 1)');
140 
141 qdeg = 5;
142 pdeg = 3;
143 dimrange = 1;
144 params = ldg_params(grid.nelements,pdeg,dimrange);
145 df_info = ldginfo(params, grid);
146 
147 % initialize zero function:
148 df0 = ldgdiscfunc([],df_info);
149 df.dofs = ldg_zero(params);
150 
151 % initialize const-one function analytically
152 f1 = @(einds,loc,grid) ones(length(einds),1);
153 % and project to ldgfunc
154 dofs1 = ldg_l2project(f1,qdeg,df_info);
155 
156 df1 = ldgdiscfunc(dofs1,df_info);
157 
158 
159 % compute errors with analytical and discrete function:
160 % identical syntax by using ldg class!!!
161 params.evaluate = @ldg_evaluate;
162 
163 
164 
165 err_df0_f1 = ldg_l2error(df0,f1,qdeg);
166 disp(['l2 error (df0,f1) =',num2str(err_df0_f1)]);
167 err_df0_df1 = ldg_l2error(df0,df1,qdeg);
168 disp(['l2 error (df0,df1) =',num2str(err_df0_df1)]);
169 
170 disp('press key to continue');
171 pause();
172 
173 disp('');
174 disp('----------------------------------------------------------');
175 disp(['Table of l2-errors of projection with increasing p, should' ...
176  ' converge to 0']);
177 % l2-difference between ldgdiscfunc and function with
178 % entity-local-coord evaluation possibility.
179 dimrange = 2;
180 qdeg = 10;
181 for p = 1:4
182  params = ldg_params(grid.nelements,p,dimrange);
183  df_info = ldginfo(params, grid);
184 
185  dofs = ldg_l2project(f,qdeg,df_info);
186  df = ldgdiscfunc(dofs,df_info);
187 
188  l2err = ldg_l2error(df,f,qdeg);
189  disp(['pol-deg = ',num2str(p),', l2error(df,f) = ',num2str(l2err)]);
190 end;
191 
192 disp('press key to continue');
193 pause();
194 
195 return;
196 
197 % l2-difference between two ldgdiscfuncs works identical
198 disp(['l2error(df,df) =',...
199  num2str(l2error(df,df,qdeg,grid,params))]);
200 
201 % arithmetics of ldg functions: sum, diff, scal-mtimes,
202 % in particular scalar product of vectorial localdgs
203 
204 
205 
206 % right hand side assembly of LGS
207 
208 
209 
210 % edge quadratures
211 
212 
213 
214 % gradient operator
215 
216 
217 
218 % div0 projection operator
219 
220 
221 
222 % second operator
223 
224 
225 
226 % concatenate for complete space operator
227 
228 
229 
230 
231 % implicit or explicit solution of time step
232 
233 
234 
235 
236 % stiffness-matrix assembly into sparse matrix
237 
238 
239 
240 
241 % solve FEM problem
242 
243 % stop routine for inspection of workspace variables
244 keyboard;
245 
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 function F = f_local(einds,loc,grid,params)
248 %function F = f_local(einds,loc,grid,params)
249 %
250 % function to be evaluated in local coordinates, i.e.
251 % glob = [X, Y] with X and Y column vectors of global coordinates,
252 % producing a corresponding
253 % result-matrix in F (each row one result)
254 % such functions can also be used with 3-dim coordinates without
255 % any change!
256 dimrange = 1;
257 F = zeros(size(einds,2),dimrange);
258 F(:,1) = einds;
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 
261 
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 function F = f_global(glob,params)
264 %function F = f_global(glob,params)
265 % function to be evaluated in gobal coordinates, i.e.
266 % glob = [X, Y] with X and Y column vectors of global coordinates,
267 % producing a corresponding
268 % result-matrix in F (each row one result)
269 % such functions can also be used with 3-dim coordinates without
270 % any change!
271 dimrange = 2;
272 F = zeros(size(glob,1),dimrange);
273 F(:,1) = sin(pi*glob(:,1));
274 F(:,2) = cos(pi*glob(:,2));
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 
277 %| \docupdate