rbmatlab 0.10.01
demos/demo_ldgfunc.m
00001 function demo_ldgfunc
00002 %function demo_ldgfunc
00003 %
00004 % demo for showing ldgfunc capabilities
00005 % idea is mainly to keep dofs and size information separate in
00006 % order to easily work with vectors and matrices of dofs for
00007 % efficiency.
00008 %
00009 % A slower alternative can be beneficial: by using the \\@ldg function
00010 % class, a dof-vector can be stored in the object. And hence an
00011 % evaluation be performed by the subsref method as for analytical functions. 
00012 % So ldg-objects can be used identically as any
00013 % analytical function in integration, matrix assembly, etc.
00014 % As classes/methods are slower than
00015 % structs, this should only be used if necessary. 
00016 % So most ldg operations will accept separate dof vectors/matrices
00017 % and size information.
00018 
00019 % Bernard Haasdonk 28.1.2009
00020 
00021 % quadrature degree for integration:
00022 qdeg = 4;
00023 % initialize grid
00024 grid = triagrid();
00025 
00026 disp('');
00027 disp('initialization of zero ldg function');
00028 
00029 % initialize basic data
00030 pdeg = 1;
00031 dimrange = 2;
00032 params = ldg_params(grid.nelements,pdeg,dimrange);
00033 %params.nelements = grid.nelements; 
00034 %params.pdeg = 1;
00035 %params.dimrange = 2; % vectorial function
00036 %params.ndofs = ldg_ndofs(params);
00037 %params.ndofs_per_element = ldg_ndofs_per_element(params);
00038 dofs = ldg_zero(params); % simple zero vector of dofs
00039 
00040 % these variables are sufficient to perform most effective operations
00041 % for ldg functions, i.e. data and parameters are kept disjoint.
00042 %
00043 % A slower alternative can be beneficial: by using the @ldg function
00044 % class, a dof-vector can be stored in the object. And hence an
00045 % evaluation be performed by the subsref method as for analytical functions. 
00046 % So ldg-objects can be used identically as any
00047 % analytical function in integration, matrix assembly, etc.
00048 % As classes/methods are slower than
00049 % structs, this should only be used if necessary:
00050 
00051 df = ldgdiscfunc(dofs,params); % setting of data: persistent!
00052 display(df);
00053 
00054 % most of the following does not access df, but work with dofs and params
00055 
00056 disp('----------------------------------------------------------');
00057 disp(['dof-access and plot of scalar component with subsampling']);
00058 
00059 % manipulate/read dofs and visualize
00060 dofs = 1:params.ndofs;
00061 [dofs_scalar1,sparams] = ldg_scalar_component(dofs,1,params);
00062 for ssl = 0:2
00063   sparams.plot_subsampling_level = ssl;
00064   figure
00065   ldg_plot(dofs_scalar1,grid,sparams);
00066   title(['dofnumbers, subsampling level ',num2str(ssl)]);
00067 end;
00068 disp('press key to continue');
00069 pause();
00070 
00071 disp('');
00072 disp('----------------------------------------------------------');
00073 disp(['local evaluation of ldgfunc and analytical functions in point ',...
00074       ' lcoord=[1/3,1/3] in first 10 elements:']);
00075 % define analytical function inline, e.g. constant 1
00076 f1 = @(einds,loc,grid,params) ones(length(einds),1);
00077 % define analytical function by pointer to matlab function 
00078 f2 = @(einds,loc,grid,params) f_local(einds,loc,grid,params);
00079 % define analytical function by pointer to matlab function with 
00080 % internal conversion of local to global coordinates:
00081 f3 = @(einds,loc,grid,params) f_global(...
00082     local2global(grid,einds,loc,params),params); 
00083 % evaluate all functions
00084 disp('f1 (scalar function constant 1):');
00085 f1(1:10, [1/3,1/3], grid, params)
00086 disp('f2: (function f_local defined in local coordinates)');
00087 f2(1:10, [1/3,1/3], grid, params)
00088 disp('f3: (function f_global defined in global coordinates)');
00089 f3(1:10, [1/3,1/3], grid, params)
00090 % note here a ldg function is used identically as the analytical functions!
00091 disp('df: (@ldgdiscfunc dimrange=2, pdeg=1, zero-function)');
00092 df.grid = grid;
00093 df(1:10, [1/3,1/3], params) % identical call as above!
00094 % this is equivalent to
00095 % ldg_evaluate(dofs,1:10, [1/3,1/3], grid, params);
00096 
00097 disp('press key to continue');
00098 pause();
00099 
00100 disp('');
00101 disp('----------------------------------------------------------');
00102 disp('test of orthogonality of element basis functions: gram matrix');
00103 
00104 % test orthogonality of basis functions:
00105 disp('gram matrix of basis on reference element should be identity:')
00106 K = ldg_local_mass_matrix(qdeg,params)
00107 %f = @(lcoord) gram_matrix(ldg_evaluate_basis(lcoord,params));
00108 %triaquadrature(qdeg,f)
00109 
00110 disp('press key to continue');
00111 pause();
00112 
00113 disp('');
00114 disp('----------------------------------------------------------');
00115 disp('l2 projection of analytic function: hor/vert sinus waves');
00116 
00117 % l2 projection of an analytical function and plot
00118 f = @(einds,loc,grid,params) f_global(...
00119     local2global(grid,einds,loc,params),params); 
00120 dofs = ldg_l2project(f,qdeg,grid,params);
00121 %dofs = l2project(f,qdeg,grid,params);
00122 
00123 [dofs_scalar1,sparams] = ldg_scalar_component(dofs,1,params);
00124 dofs_scalar2 = ldg_scalar_component(dofs,2,params);
00125 sparams.plot_subsampling_level = 2;
00126 figure,ldg_plot(dofs_scalar1,grid,sparams);title('component1');
00127 figure,ldg_plot(dofs_scalar2,grid,sparams);title('component2');
00128 
00129 disp('press key to continue');
00130 pause();
00131 
00132 disp('');
00133 disp('----------------------------------------------------------');
00134 disp('l2-error of zero ldg function with unity (should be 1)');
00135 
00136 qdeg = 5;
00137 pdeg = 3;
00138 dimrange = 1;
00139 params = ldg_params(grid.nelements,pdeg,dimrange)
00140 
00141 % initialize zero function:
00142 dofs0 = ldg_zero(params);
00143 % initialize const-one function analytically 
00144 f1 = @(einds,loc,grid,params) ones(length(einds),1);
00145 % and project to ldgfunc
00146 dofs1 = ldg_l2project(f1,qdeg,grid,params);
00147 
00148 df1 = ldgdiscfunc(dofs1,params);
00149 % compute errors with analytical and discrete function: 
00150 % identical syntax by using ldg class!!!
00151 params.evaluate = @ldg_evaluate;
00152 
00153 err_df0_f1 = ldg_l2error(dofs0,f1,qdeg,grid,params);
00154 disp(['l2 error (df0,f1) =',num2str(err_df0_f1)]);
00155 err_df0_df1 = ldg_l2error(dofs0,df1,qdeg,grid,params);
00156 disp(['l2 error (df0,df1) =',num2str(err_df0_df1)]);
00157 
00158 disp('press key to continue');
00159 pause();
00160 
00161 disp('');
00162 disp('----------------------------------------------------------');
00163 disp(['Table of l2-errors of projection with increasing p, should' ...
00164       ' converge to 0']);
00165 % l2-difference between ldgdiscfunc and function with 
00166 % entity-local-coord evaluation possibility.
00167 params.dimrange = 2;
00168 params.nelements = grid.nelements;
00169 qdeg = 10;
00170 for p = 1:4
00171   params.pdeg = p;
00172   params.ndofs = ldg_ndofs(params);
00173   params.ndofs_per_element = ldg_ndofs_per_element(params);
00174   dofs = ldg_l2project(f,qdeg,grid,params);
00175 %  dofs = l2project(f,qdeg,grid,params);
00176   l2err = ldg_l2error(dofs,f,qdeg,grid,params);
00177   disp(['pol-deg = ',num2str(p),', l2error(df,f) = ',num2str(l2err)]);
00178 end;
00179 
00180 disp('press key to continue');
00181 pause();
00182 
00183 return;
00184 
00185 % l2-difference between two ldgdiscfuncs works identical
00186 disp(['l2error(df,df) =',...
00187       num2str(l2error(df,df,qdeg,grid,params))]);
00188 
00189 % arithmetics of ldg functions: sum, diff, scal-mtimes,
00190 % in particular scalar product of vectorial localdgs
00191 
00192 
00193 
00194 % right hand side assembly of LGS
00195 
00196 
00197 
00198 % edge quadratures
00199 
00200 
00201 
00202 % gradient operator
00203 
00204 
00205 
00206 % div0 projection operator
00207 
00208 
00209 
00210 % second operator
00211 
00212 
00213 
00214 % concatenate for complete space operator
00215 
00216 
00217 
00218 
00219 % implicit or explicit solution of time step
00220 
00221 
00222 
00223 
00224 % stiffness-matrix assembly into sparse matrix
00225 
00226 
00227 
00228 
00229 % solve FEM problem
00230 
00231 % stop routine for inspection of workspace variables
00232 keyboard;
00233 
00234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00235 function F = f_local(einds,loc,grid,params)
00236 %function F = f_local(einds,loc,grid,params)
00237 %
00238 % function to be evaluated in local coordinates, i.e. 
00239 % glob = [X, Y] with X and Y column vectors of global coordinates, 
00240 % producing a corresponding
00241 % result-matrix in F (each row one result)
00242 % such functions can also be used with 3-dim coordinates without
00243 % any change!
00244 dimrange = 1;
00245 F = zeros(size(einds,2),dimrange);
00246 F(:,1) = einds;
00247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00248 
00249 
00250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00251 function F = f_global(glob,params)
00252 %function F = f_global(glob,params)
00253 % function to be evaluated in gobal coordinates, i.e. 
00254 % glob = [X, Y] with X and Y column vectors of global coordinates, 
00255 % producing a corresponding
00256 % result-matrix in F (each row one result)
00257 % such functions can also be used with 3-dim coordinates without
00258 % any change!
00259 dimrange = 2;
00260 F = zeros(size(glob,1),dimrange);
00261 F(:,1) = sin(pi*glob(:,1));
00262 F(:,2) = cos(pi*glob(:,2));
00263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00264 
00265 %| \docupdate 
 All Classes Namespaces Files Functions Variables