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
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.
19 % Bernard Haasdonk 28.1.2009
21 % quadrature degree for integration:
27 disp(
'initialization of zero ldg function');
29 % initialize basic data
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
42 % these variables are sufficient to perform most effective operations
43 %
for ldg functions, i.e. data and parameters are kept disjoint.
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:
53 df = ldgdiscfunc(dofs,df_info); % setting of data: persistent!
56 % most of the following does not access df, but work with dofs and params
58 disp(
'----------------------------------------------------------');
59 disp([
'dof-access and plot of scalar component with subsampling']);
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);
66 sparams.plot_subsampling_level = ssl;
68 ldg_plot(dofs_scalar1,grid,sparams);
69 title([
'dofnumbers, subsampling level ',num2str(ssl)]);
71 disp(
'press key to continue');
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);
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);
101 disp('press key to continue');
105 disp('----------------------------------------------------------');
106 disp('test of orthogonality of element basis functions: gram matrix');
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)
114 disp('press key to continue');
118 disp('----------------------------------------------------------');
119 disp('l2 projection of analytic function: hor/vert sinus waves');
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);
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');
134 disp('press key to continue');
138 disp('----------------------------------------------------------');
139 disp('l2-error of zero ldg function with unity (should be 1)');
144 params = ldg_params(grid.nelements,pdeg,dimrange);
145 df_info =
ldginfo(params, grid);
147 % initialize zero function:
149 df.dofs = ldg_zero(params);
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);
156 df1 = ldgdiscfunc(dofs1,df_info);
159 % compute errors with analytical and discrete function:
160 % identical syntax by using ldg class!!!
161 params.evaluate = @ldg_evaluate;
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)]);
170 disp('press key to continue');
174 disp('----------------------------------------------------------');
175 disp(['Table of l2-errors of projection with increasing p, should' ...
177 % l2-difference between ldgdiscfunc and function with
178 % entity-local-coord evaluation possibility.
182 params = ldg_params(grid.nelements,p,dimrange);
183 df_info =
ldginfo(params, grid);
185 dofs = ldg_l2project(f,qdeg,df_info);
186 df = ldgdiscfunc(dofs,df_info);
188 l2err = ldg_l2error(df,f,qdeg);
189 disp(['pol-deg = ',num2str(p),', l2error(df,f) = ',num2str(l2err)]);
192 disp('press key to continue');
197 % l2-difference between two ldgdiscfuncs works identical
198 disp(['l2error(df,df) =',...
199 num2str(l2error(df,df,qdeg,grid,params))]);
201 % arithmetics of ldg functions: sum, diff, scal-mtimes,
202 % in particular scalar product of vectorial localdgs
206 % right hand side assembly of LGS
218 % div0 projection operator
226 % concatenate for complete space operator
231 % implicit or explicit solution of time step
236 % stiffness-matrix assembly into sparse matrix
243 % stop routine for inspection of workspace variables
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 function F = f_local(einds,loc,grid,params)
248 %function F = f_local(einds,loc,grid,params)
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
257 F = zeros(size(einds,2),dimrange);
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
272 F = zeros(size(glob,1),dimrange);
273 F(:,1) = sin(pi*glob(:,1));
274 F(:,2) = cos(pi*glob(:,2));
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%