*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit ef31ad4c authored by Innerberger, Michael's avatar Innerberger, Michael
Browse files

Clean up examples

parent 68b4d5fb
No related branches found
No related tags found
No related merge requests found
function leveldata = storingLevelOrientedData(doPlots) % Example of an adaptive FEM algorithm with storing and displaying
%%MAINLEVELDATA Example of an adaptive FEM algorithm with storing and % level-oriented data
%displaying level-oriented dataa
%% proceed optional input function leveldata = storingLevelOrientedData(doPlots, doStore)
if nargin < 1 arguments
doPlots = true; doPlots (1,1) logical = true
doStore (1,1) logical = false
end end
%% paramters %% paramters
...@@ -15,7 +15,7 @@ function leveldata = storingLevelOrientedData(doPlots) ...@@ -15,7 +15,7 @@ function leveldata = storingLevelOrientedData(doPlots)
mesh = Mesh.loadFromGeometry('Lshape'); mesh = Mesh.loadFromGeometry('Lshape');
fes = FeSpace(mesh, LowestOrderH1Fe()); fes = FeSpace(mesh, LowestOrderH1Fe());
u = FeFunction(fes); u = FeFunction(fes);
%% initialize output data structure %% initialize output data structure
pathToStorage = 'results'; pathToStorage = 'results';
leveldata = LevelData(pathToStorage); leveldata = LevelData(pathToStorage);
...@@ -23,69 +23,68 @@ function leveldata = storingLevelOrientedData(doPlots) ...@@ -23,69 +23,68 @@ function leveldata = storingLevelOrientedData(doPlots)
leveldata.metaData('domain') = 'Lshape'; leveldata.metaData('domain') = 'Lshape';
leveldata.metaData('method') = 'S1'; leveldata.metaData('method') = 'S1';
leveldata.metaData('identifier') = 'example'; leveldata.metaData('identifier') = 'example';
%% problem data %% problem data
blf = BilinearForm(); blf = BilinearForm();
lf = LinearForm(); lf = LinearForm();
blf.a = Constant(mesh, [1;0;0;1]); blf.a = Constant(mesh, 1);
blf.b = Constant(mesh, [0;0]);
blf.c = Constant(mesh, 0);
lf.f = Constant(mesh, 1); lf.f = Constant(mesh, 1);
lf.fvec = Constant(mesh, [0;0]);
% reference value for energy norm of exact solution % reference value for energy norm of exact solution
normExactSquared = 0.2140750232; normExactSquared = 0.2140750232;
%% adaptive loop %% adaptive loop
meshSufficientlyFine = false; meshSufficientlyFine = false;
while ~meshSufficientlyFine while ~meshSufficientlyFine
%% compute data for rhs coefficient and assemble forms %% compute data for rhs coefficient and assemble forms
A = assemble(blf, fes); A = assemble(blf, fes);
F = assemble(lf, fes); F = assemble(lf, fes);
%% solve FEM system %% solve FEM system
freeDofs = getFreeDofs(fes); freeDofs = getFreeDofs(fes);
tic; tic;
u.setFreeData(A(freeDofs,freeDofs) \ F(freeDofs)); u.setFreeData(A(freeDofs,freeDofs) \ F(freeDofs));
runtimeSolve = toc; runtimeSolve = toc;
%% approximate error %% approximate error
err = sqrt(normExactSquared - u.data * A * u.data'); err = sqrt(normExactSquared - u.data * A * u.data');
%% compute refinement indicators %% compute refinement indicators
tic; tic;
eta2 = estimate(blf, lf, u); eta2 = estimate(blf, lf, u);
eta = sqrt(sum(eta2)); eta = sqrt(sum(eta2));
runtimeEstimate = toc; runtimeEstimate = toc;
%% compute efficiency index %% compute efficiency index
eff = err / eta; eff = err / eta;
%% storing error quantities and general data %% storing error quantities and general data
leveldata.append('ndof', uint32(length(freeDofs)), ... leveldata.append('ndof', uint32(length(freeDofs)), ...
'eta', eta, ... 'eta', eta, ...
'err', err, ... 'err', err, ...
'coordinates', mesh.coordinates, ... 'coordinates', mesh.coordinates, ...
'elements', mesh.elements); 'elements', mesh.elements);
%% storing results of timing %% storing results of timing
leveldata.setTime(leveldata.nLevel, ... leveldata.setTime(leveldata.nLevel, ...
'runtimeSolve', runtimeSolve, ... 'runtimeSolve', runtimeSolve, ...
'runtimeEstimate', runtimeEstimate); 'runtimeEstimate', runtimeEstimate);
%% storing absolute values %% storing absolute values
leveldata.setAbsolute(leveldata.nLevel, 'eff', eff); leveldata.setAbsolute(leveldata.nLevel, 'eff', eff);
%% print information on current level to command line %% print information on current level to command line
leveldata.printLevel(); leveldata.printLevel();
%% save intermediate results to file %% save intermediate results to file
% (prevents data loss in case of crashes) % (prevents data loss in case of crashes)
leveldata.saveToFile(); if doStore
leveldata.saveToFile();
end
%% stoping criterion %% stoping criterion
meshSufficientlyFine = (mesh.nElements > nEmax); meshSufficientlyFine = (mesh.nElements > nEmax);
%% refine mesh %% refine mesh
if ~meshSufficientlyFine if ~meshSufficientlyFine
marked = markDoerflerBinning(eta2, theta); marked = markDoerflerBinning(eta2, theta);
...@@ -95,8 +94,8 @@ function leveldata = storingLevelOrientedData(doPlots) ...@@ -95,8 +94,8 @@ function leveldata = storingLevelOrientedData(doPlots)
%% post-processing variables %% post-processing variables
leveldata.setTime(':', 'runtimeTotal', ... leveldata.setTime(':', 'runtimeTotal', ...
sum(leveldata.get(':', 'runtimeSolve', 'runtimeEstimate'), 2)); sum(leveldata.get(':', 'runtimeSolve', 'runtimeEstimate'), 2));
%% plot results %% plot results
if doPlots if doPlots
% plot error quantities (in general, converging to zeros) % plot error quantities (in general, converging to zeros)
...@@ -110,54 +109,41 @@ function leveldata = storingLevelOrientedData(doPlots) ...@@ -110,54 +109,41 @@ function leveldata = storingLevelOrientedData(doPlots)
% plot absolute values in semi-logarithmic plot % plot absolute values in semi-logarithmic plot
figure(); figure();
leveldata.plotAbsolute('ndof'); leveldata.plotAbsolute('ndof');
end
% plot of all triangulations
% figure(); %% additional visualization/storage capabilities
% leveldata.plotTriangulation(); if doStore
% export error plot to file
%% export error plot to file
leveldata.plotToFile('ndof'); leveldata.plotToFile('ndof');
%% export refinement video % save all values to comma-separated table
% (generates video of about 110 MB) leveldata.saveToTable();
%leveldata.plotTriangulationToFile();
end
%% save all values to comma-separated table % plot of all triangulations one after another
leveldata.saveToTable(); figure();
leveldata.plotTriangulation();
% export refinement video (generates video of about 110 MB)
leveldata.plotTriangulationToFile();
end
end end
%% local function for residual a posteriori error estimation %% local function for residual a posteriori error estimation
% This function shows how to build a residual error estimator with the provided % \eta(T)^2 = h_T^2 * || \Delta u + f ||_{L^2(T)}^2 + h_T * || [Du] ||_{L^2(E)}^2
% tools. Not hiding this in its own class has the advantage that estimation is function indicators = estimate(~, ~, u)
% very flexible with respect to the involved terms.
% \eta(T)^2 = h_T^2 * || -div(a*Du) + b*Du + c*u - f + div(fvec) ||_{L^2(T)}^2
% + h_T * || [ a*Du - fvec ] ||_{L^2(E)}^2
function indicators = estimate(blf, lf, u)
fes = u.fes; fes = u.fes;
mesh = fes.mesh; mesh = fes.mesh;
%% compute volume residual element-wise %% compute volume residual element-wise
% Here, one can optimize the estimator for the given situation. In the case f = CompositeFunction(@(Du) vectorProduct(Du, Du), Gradient(u));
% p=1 with constant diffusion, the diffusion term vanishes in the residual. qrTri = QuadratureRule.ofOrder(1);
% Also div(fvec) vanishes if fvec is element-wise constant.
f = CompositeFunction(...
@(b, c, f, u, Du) (vectorProduct(b, Du) + c .* u - f).^2, ...
blf.b, blf.c, lf.f, u, Gradient(u));
qrTri = QuadratureRule.ofOrder(4);
volumeRes = integrateElement(f, qrTri); volumeRes = integrateElement(f, qrTri);
%% compute edge residual edge-wise %% compute edge residual edge-wise
% Here, none of the terms vanish, so they must be computed. This can be done
% with a lower order than the element-residuals.
f = CompositeFunction(...
@(a, fvec, Du) vectorProduct(a, Du, [2,2], [2,1]) - fvec, ...
blf.a, lf.fvec, Gradient(u));
qrEdge = QuadratureRule.ofOrder(1, '1D'); qrEdge = QuadratureRule.ofOrder(1, '1D');
edgeRes = integrateNormalJump(f, qrEdge, @(j) j.^2, {}, ':'); edgeRes = integrateNormalJump(Gradient(u), qrEdge, @(j) j.^2, {}, ':');
dirichlet = getCombinedBndEdges(mesh, fes.bnd.dirichlet); dirichlet = getCombinedBndEdges(mesh, fes.bnd.dirichlet);
edgeRes(dirichlet) = 0; edgeRes(dirichlet) = 0;
......
...@@ -8,7 +8,7 @@ nRuns = 10; ...@@ -8,7 +8,7 @@ nRuns = 10;
%% run time measurements %% run time measurements
leveldatacollection = ... leveldatacollection = ...
TimeIt('debugTiming', nRuns, 'storingLevelOrientedData', false); TimeIt('debugTiming', nRuns, 'storingLevelOrientedData', false, false);
%% print statistical analysis of timing results %% print statistical analysis of timing results
leveldatacollection.printStatistics(); leveldatacollection.printStatistics();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment