LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
piecewise_const_element_matrix_provider.cc
1#include "piecewise_const_element_matrix_provider.h"
2
3#include <lf/uscalfe/lagr_fe.h>
4#include <utils.h>
5
6#include <cmath>
7#include <iomanip>
8#include <iostream>
9
11
13 double sigma, const lf::mesh::utils::MeshDataSet<bool> &boundary,
14 bool modified)
15 : sigma_(sigma), boundary_(boundary), modified_(modified) {}
16
18 const lf::mesh::Entity &entity) const {
19 // Get the geometry of the entity
20 const auto *geom = entity.Geometry();
21 // Compute the global vertex coordinates
22 Eigen::MatrixXd vertices = geom->Global(entity.RefEl().NodeCoords());
23 // Use the vertex coordinates to compute the local normals on the edges
24 const Eigen::Matrix<double, 2, 3> normals =
26 // Construct the basis functions from the curl of the standard hat functions
28 const Eigen::MatrixXd ref_grads =
29 hat_func.GradientsReferenceShapeFunctions(Eigen::VectorXd::Zero(2))
30 .transpose();
31 const Eigen::MatrixXd J_inv_trans =
32 geom->JacobianInverseGramian(Eigen::VectorXd::Zero(2));
33 const Eigen::Matrix<double, 2, 3> grads = J_inv_trans * ref_grads;
34 Eigen::Matrix<double, 2, 3> basis_funct;
35 basis_funct << grads.row(1), -grads.row(0);
36 // Fill the element matrix
37 Eigen::MatrixXd elem_mat = Eigen::MatrixXd::Zero(6, 6);
38 // Upper left block
39 elem_mat.block(0, 0, 3, 3).setZero();
40 // Lower left and upper right blocks
41 const auto edges = entity.SubEntities(1);
42 for (int basis_func_idx = 0; basis_func_idx < 3; ++basis_func_idx) {
43 for (int edge_idx = 0; edge_idx < 3; ++edge_idx) {
44 Eigen::Vector2d t;
45 t << -normals(1, edge_idx), normals(0, edge_idx);
46 elem_mat(edge_idx + 3, basis_func_idx) =
47 basis_funct.col(basis_func_idx).transpose() * t;
48 }
49 }
50 elem_mat.block(0, 3, 3, 3) = elem_mat.block(3, 0, 3, 3).transpose();
51 // Lower right block
52 for (int edge_idx = 0; edge_idx < 3; ++edge_idx) {
53 const auto &edge = *edges[edge_idx];
54 elem_mat(edge_idx + 3, edge_idx + 3) = -1 / sigma_;
55 if (modified_) {
56 elem_mat(edge_idx + 3, edge_idx + 3) /=
57 lf::geometry::Volume(*(edge.Geometry()));
58 }
59 // If the edge is not on the boundary, the entry in the full system matrix
60 // will be the sum of two entries of element matrices but we want it still
61 // to be equal to 1/sigma. For this reason, we divide the local contribution
62 // by 2
63 if (!boundary_(edge)) {
64 elem_mat(edge_idx + 3, edge_idx + 3) /= 2;
65 }
66 }
67 return elem_mat;
68}
69
70} // end namespace projects::ipdg_stokes::assemble
const Eigen::MatrixXd & NodeCoords() const
Get the coordinates of the nodes of this reference element.
Definition: ref_el.h:238
virtual Eigen::MatrixXd Global(const Eigen::MatrixXd &local) const =0
Map a number of points in local coordinates into the global coordinate system.
Interface class representing a topological entity in a cellular complex
Definition: entity.h:39
virtual const geometry::Geometry * Geometry() const =0
Describes the geometry of this entity.
virtual nonstd::span< const Entity *const > SubEntities(unsigned rel_codim) const =0
Return all sub entities of this entity that have the given codimension (w.r.t. this entity!...
virtual base::RefEl RefEl() const =0
Describes the reference element type of this entity.
Linear Lagrange finite element on triangular reference element.
Definition: lagr_fe.h:58
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > GradientsReferenceShapeFunctions(const Eigen::MatrixXd &refcoords) const override
Computation of the gradients of all reference shape functions in a number of points.
Definition: lagr_fe.h:117
PiecewiseConstElementMatrixProvider(double sigma, const lf::mesh::utils::MeshDataSet< bool > &boundary, bool modified=false)
Constructor.
Eigen::MatrixXd Eval(const lf::mesh::Entity &entity) const
Compute the element matrix for some entity of a mesh.
double Volume(const Geometry &geo)
Compute the (approximate) volume (area) of a shape.
Eigen::Matrix< double, 2, 3 > computeOutwardNormals(const lf::mesh::Entity &entity)
Compute the outward pointing normals of a triangle.
Definition: utils.cc:7