LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
primal_dpg.h
1
10#ifndef PROJECTS_DPG_CS_PRIMAL_DPG_H
11#define PROJECTS_DPG_CS_PRIMAL_DPG_H
12
13#include <lf/base/base.h>
14#include <lf/fe/fe.h>
15#include <lf/io/io.h>
16#include <lf/mesh/test_utils/test_meshes.h>
17#include <lf/mesh/utils/utils.h>
18#include <lf/refinement/refinement.h>
19#include <lf/uscalfe/uscalfe.h>
20
21#include "../dpg_element_matrix_provider.h"
22#include "../dpg_element_vector_provider.h"
23#include "../product_element_matrix_provider_builder.h"
24#include "../product_element_vector_provider_builder.h"
25#include "../product_fe_space.h"
26#include "../product_fe_space_factory.h"
27#include "../test/convection_diffusion_ell_bvp.h"
28
29// utility type to return the reported energy norms.
30//[number of dofs, H1-error, error estimator].
31using energy_vector = std::vector<std::tuple<int, double, double>>;
32
34
56template <typename SOLFUNC, typename SOLGRAD, typename ALPHAFUNC,
57 typename BETAFUNC, typename FFUNC, typename GFUNC>
59 size_type reflevels, SOLFUNC solution, SOLGRAD sol_grad, ALPHAFUNC alpha,
60 BETAFUNC beta, FFUNC f, GFUNC g, int degree_p, int enrichement,
61 lf::base::RefEl ref_el) {
62 std::vector<std::tuple<int, double, double>> errnorms{};
63 std::cout << "Approximation order: " << degree_p
64 << ", Enrichement: " << enrichement << std::endl;
65
66 // calculate degree for enriched spaces
67 int degree_r = degree_p + enrichement;
68
69 // generate a tensor product mesh based on the provided reference element.
70 std::shared_ptr<lf::mesh::Mesh> top_mesh;
71 std::unique_ptr<lf::mesh::MeshFactory> mesh_factory_ptr =
72 std::make_unique<lf::mesh::hybrid2d::MeshFactory>(2);
73 switch (ref_el) {
75 // construct a triangular tensor product mesh
76 lf::mesh::utils::TPTriagMeshBuilder builder(std::move(mesh_factory_ptr));
77 builder.setBottomLeftCorner(Eigen::Vector2d{0.0, 0.0})
78 .setTopRightCorner(Eigen::Vector2d{1.0, 1.0})
79 .setNumXCells(2)
80 .setNumYCells(2);
81 top_mesh = builder.Build();
82 break;
83 }
84
86 lf::mesh::utils::TPQuadMeshBuilder builder(std::move(mesh_factory_ptr));
87 builder.setBottomLeftCorner(Eigen::Vector2d{0.0, 0.0})
88 .setTopRightCorner(Eigen::Vector2d{1.0, 1.0})
89 .setNumXCells(2)
90 .setNumYCells(2);
91 top_mesh = builder.Build();
92 break;
93 }
94 default: {
95 LF_ASSERT_MSG(false, "unsupported reference element:" << ref_el);
96 break;
97 }
98 }
99
100 // Generate a hierarchy of test meshes by uniform refinement
101 std::shared_ptr<lf::refinement::MeshHierarchy> multi_mesh_p =
103 reflevels);
104 lf::refinement::MeshHierarchy& multi_mesh{*multi_mesh_p};
105 multi_mesh.PrintInfo(std::cout);
106
107 // get number of levels:
108 size_type L = multi_mesh.NumLevels();
109
110 // perform computations on all levels:
111 for (size_type l = 0; l < L; ++l) {
112 // retrive mesh for current level
113 std::shared_ptr<const lf::mesh::Mesh> mesh_p{multi_mesh.getMesh(l)};
114
115 // construct trial space:
116 ProductUniformFESpaceFactory<double> factory_trial(mesh_p);
117 auto u = factory_trial.AddH1Component(degree_p);
118 auto q_n = factory_trial.AddFluxComponent(degree_p - 1);
119 auto fe_space_trial = factory_trial.Build();
120
121 // construct test space:
122 ProductUniformFESpaceFactory<double> factory_test(mesh_p);
123 auto v = factory_test.AddL2Component(degree_r);
124 auto fe_space_test = factory_test.Build();
125
126 // wrap functions into a mesh function:
127 auto alpha_mf = lf::mesh::utils::MeshFunctionGlobal(alpha);
128 auto beta_mf = lf::mesh::utils::MeshFunctionGlobal(beta);
130 auto one_mf = lf::mesh::utils::MeshFunctionConstant(1.0);
131 auto zero_mf =
132 lf::mesh::utils::MeshFunctionConstant(Eigen::Vector2d(0.0, 0.0));
133
134 // construct extended stiffness provider
135 ProductElementMatrixProviderBuilder stiffness_builder(fe_space_trial,
136 fe_space_test);
137 stiffness_builder.AddDiffusionElementMatrixProvider(u, v, alpha_mf);
138 stiffness_builder.AddFluxElementMatrixProvider(q_n, v, -one_mf);
139 stiffness_builder.AddConvectionElementMatrixProvider(u, v, zero_mf,
140 beta_mf);
141 auto stiffness_provider = stiffness_builder.Build();
142
143 // construct gram matrix provider
144 ProductElementMatrixProviderBuilder gramian_builder(fe_space_test,
145 fe_space_test);
146 gramian_builder.AddDiffusionElementMatrixProvider(v, v, one_mf);
147 gramian_builder.AddReactionElementMatrixProvider(v, v, one_mf);
148 auto gramian_provider = gramian_builder.Build();
149
150 // construct the rhs provider:
151 ProductElementVectorProviderBuilder rhs_builder(fe_space_test);
152 rhs_builder.AddLoadElementVectorProvider(v, f_mf);
153 auto rhs_provider = rhs_builder.Build();
154
155 // initialize the dpg providers:
156 auto element_matrix_provider =
157 std::make_shared<DpgElementMatrixProvider<double>>(stiffness_provider,
158 gramian_provider);
159 auto element_vector_provider =
160 std::make_shared<DpgElementVectorProvider<double>>(
161 rhs_provider, stiffness_provider, gramian_provider);
162
163 // initialize the boundary value problem
164 auto h = [](const Eigen::Vector2d & /*x*/) -> double { return 0.0; };
165 auto dirichlet_selector = lf::mesh::utils::flagEntitiesOnBoundary(mesh_p);
166 auto bvp = std::make_shared<FullConvectionDiffusionBVP<
167 decltype(alpha), decltype(beta), decltype(f), decltype(g), decltype(h),
168 decltype(dirichlet_selector)>>(
169 FullConvectionDiffusionBVP(alpha, beta, f, g, h,
170 std::move(dirichlet_selector)));
171
172 // Assemble full system:
173 auto [A, phi] = ConvectionDiffusionDPGLinSys<double>(
174 fe_space_trial, element_matrix_provider, element_vector_provider, bvp,
175 u, q_n);
176
177 // calculate full solution
178 Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> solver;
179 solver.compute(A);
180 Eigen::VectorXd sol_vec = solver.solve(phi);
181
182 // extract u component:
183 auto& dofh = fe_space_trial->LocGlobMap();
184 auto fe_space_u = fe_space_trial->ComponentFESpace(u);
185 Eigen::VectorXd sol_vec_u = sol_vec.head(dofh.NumDofs(u));
186
187 // wrap finite element solution and exact solution into a mesh function:
188 auto mf_fe_u = lf::fe::MeshFunctionFE(fe_space_u, sol_vec_u);
189 auto mf_fe_grad_u = lf::fe::MeshFunctionGradFE(fe_space_u, sol_vec_u);
190 auto mf_solution = lf::mesh::utils::MeshFunctionGlobal(solution);
191 auto mf_solution_grad = lf::mesh::utils::MeshFunctionGlobal(sol_grad);
192
193 // calculate the error in the H1 norm
194 double H1_err = std::sqrt(lf::fe::IntegrateMeshFunction(
195 *mesh_p,
196 lf::mesh::utils::squaredNorm(mf_fe_u - mf_solution) +
197 lf::mesh::utils::squaredNorm(mf_fe_grad_u - mf_solution_grad),
198 10));
199
200 // evaluate error estimators:
201 auto local_errors =
202 ElementErrorEstimators(fe_space_trial->LocGlobMap(), stiffness_provider,
203 gramian_provider, rhs_provider, sol_vec);
204 double posteriorError = EvalPosteriorError(mesh_p, local_errors);
205 std::cout << "Level " << l << " ( " << dofh.NumDofs() << ")"
206 << ", H1 error: " << H1_err << ", post:" << posteriorError
207 << std::endl;
208 errnorms.emplace_back(dofh.NumDofs(), H1_err, posteriorError);
209 }
210 return errnorms;
211}
212} // namespace projects::dpg::test
213
214#endif // PROJECTS_DPG_CS_PRIMAL_DPG_H
Represents a reference element with all its properties.
Definition: ref_el.h:106
static constexpr RefEl kTria()
Returns the reference triangle.
Definition: ref_el.h:158
static constexpr RefEl kQuad()
Returns the reference quadrilateral.
Definition: ref_el.h:166
A MeshFunction which takes the same constant value on the whole mesh.
MeshFunction wrapper for a simple function of physical coordinates.
StructuredMeshBuilder & setBottomLeftCorner(const VECTOR &blc)
StructuredMeshBuilder & setNumYCells(size_type nyc)
Implements a Builder for a tensor product grid (with rectangular cells)
std::shared_ptr< mesh::Mesh > Build() override
actual construction of the mesh
Implements a MeshBuilder that generates a triangular structured mesh.
std::shared_ptr< mesh::Mesh > Build() override
actual construction of the mesh
A hierarchy of nested 2D hybrid meshes created by refinement.
std::ostream & PrintInfo(std::ostream &o, unsigned ctrl=0) const
Output of information about the mesh hierarchy.
Builder class to build a ProductElementMatrixProvider.
ProductElementMatrixProviderBuilder & AddReactionElementMatrixProvider(size_type trial_component, size_type test_component, REACTION_COEFF gamma)
std::shared_ptr< ProductElementMatrixProvider< SCALAR > > Build()
Build the ProdcutElementMatrixProvider for the bilinear form based on the provided bilinear forms .
ProductElementMatrixProviderBuilder & AddFluxElementMatrixProvider(size_type trial_component, size_type test_component, DIFF_COEFF alpha)
ProductElementMatrixProviderBuilder & AddConvectionElementMatrixProvider(size_type trial_component, size_type test_component, CONVECTION_COEFF_1 beta_1, CONVECTION_COEFF_2 beta_2)
ProductElementMatrixProviderBuilder & AddDiffusionElementMatrixProvider(size_type trial_component, size_type test_component, DIFF_COEFF alpha)
Builder class to build a ProductElementVectorProvider.
ProductElementVectorProviderBuilder & AddLoadElementVectorProvider(size_type test_component, FUNCTOR f)
Adds a linear form to representing a simple load linear form.
std::shared_ptr< ProductElementVectorProvider< SCALAR > > Build()
Build the ProductElementVectorProvider based on the provided linear forms.
Factory class to build a ProductUniformFESpace.
size_type AddFluxComponent(size_type degree)
Adds a -conforming finite element to the space, that represents a flux on the mesh skeleton.
size_type AddH1Component(size_type degree)
Adds a -conforming finite element to the space.
size_type AddL2Component(size_type degree)
Adds a -conforming finite element to the space.
std::shared_ptr< ProductUniformFESpace< SCALAR > > Build()
Build the ProductUniformFespace based on the provided specifications.
MeshFunctionGradFE(std::shared_ptr< T >, const Eigen::Matrix< SCALAR_COEFF, Eigen::Dynamic, 1 > &) -> MeshFunctionGradFE< typename T::Scalar, SCALAR_COEFF >
MeshFunctionFE(std::shared_ptr< T >, const Eigen::Matrix< SCALAR_COEFF, Eigen::Dynamic, 1 > &) -> MeshFunctionFE< typename T::Scalar, SCALAR_COEFF >
auto IntegrateMeshFunction(const lf::mesh::Mesh &mesh, const MF &mf, const QR_SELECTOR &qr_selector, const ENTITY_PREDICATE &ep=base::PredicateTrue{}, int codim=0) -> mesh::utils::MeshFunctionReturnType< MF >
Integrate a MeshFunction over a mesh (with quadrature rules)
Definition: fe_tools.h:111
CodimMeshDataSet< bool > flagEntitiesOnBoundary(const std::shared_ptr< const Mesh > &mesh_p, lf::base::dim_t codim)
flag entities of a specific co-dimension located on the boundary
std::shared_ptr< MeshHierarchy > GenerateMeshHierarchyByUniformRefinemnt(const std::shared_ptr< lf::mesh::Mesh > &mesh_p, lf::base::size_type ref_lev, RefPat ref_pat)
Generated a sequence of nested 2D hybrid mehes by regular or barycentric refinement.
energy_vector TestConververgencePrimalDPGConvectionDiffusionDirichletBVP(size_type reflevels, SOLFUNC solution, SOLGRAD sol_grad, ALPHAFUNC alpha, BETAFUNC beta, FFUNC f, GFUNC g, int degree_p, int enrichement, lf::base::RefEl ref_el)
Examines the convergence behaviour of the primal DPG method for a pure Dirichlet convection-diffusion...
Definition: primal_dpg.h:58
lf::mesh::utils::CodimMeshDataSet< SCALAR > ElementErrorEstimators(const ProductUniformFEDofHandler &trial_dofh, std::shared_ptr< ProductElementMatrixProvider< SCALAR > > stiffness_provider, std::shared_ptr< ProductElementMatrixProvider< SCALAR > > gramian_provider, std::shared_ptr< ProductElementVectorProvider< SCALAR > > load_provider, const Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > &sol_vec)
Evaluation of the local DPG error estimators on a mesh.
Definition: dpg_tools.h:327
SCALAR EvalPosteriorError(const std::shared_ptr< const lf::mesh::Mesh > &mesh_p, const lf::mesh::utils::CodimMeshDataSet< SCALAR > &local_errors)
Evaluation of the DPG error estimator.
Definition: dpg_tools.h:374
lf::uscalfe::size_type size_type
Type for vector length/matrix sizes.
Definition: dpg.h:17