LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
Quick Reference - Assembly

Edit on GitHub

Attention
The contents of this page are discussed in Lecture Document Subsection 2.7.4. Please read this section before using the quick reference.

Overview

Assembly in the context of LehrFEM++ refers to computing entries of the stiffness matrix and the right-hand side vector (load vector) for finite elements. Lecture Document Subsection 2.7.4 provides more detailed information.

A key part of efficient assembly is cell-local computations, where the indexing is handled by DOFHandlers. To solve a PDE using the Galerkin method, we assemble the matrix \(\textbf{A}\) and the right-hand side vector \(\vec{b}\) to obtain a linear system \(\textbf{A}\vec{x} = \vec{b}\).

LehrFEM++ provides a number of helper functions for assembly, which are detailed in Lecture Document Subsection 2.7.4.3.

Stiffness/Galerkin Matrix

LehrFEM++ provides the lf::assemble::AssembleMatrixLocally function for assembling the stiffness matrix \(\textbf{A}\). The following example shows how to assemble the stiffness matrix for a Poisson bilinear form

\[a(u, v) = \int_{\Omega} \textbf{grad} u \cdot \textbf{grad} v \, dx \]

using the entity matrix provider lf::uscalfe::LinearFELaplaceElementMatrix.

// initialize triangular 2d mesh somehow
std::shared_ptr<lf::mesh::Mesh> mesh;
// create a dof handler which assigns one global dof to every node of the mesh
lf::assemble::UniformFEDofHandler dofh(mesh,
// initialize EntityMatrixProvider that defines the local Laplace element
// matrix (only for triangular meshes):
lf::uscalfe::LinearFELaplaceElementMatrix entity_matrix_provider;
// setup a COOMatrix into which the global matrix will be assembled:
lf::assemble::COOMatrix<double> lhs(dofh.NumDofs(), dofh.NumDofs());
// increase logging level for AssembleMatrixLocally():
lf::assemble::AssembleMatrixLogger()->set_level(spdlog::level::debug);
// assemble the global Laplace matrix (iterate over all entities with
// codim=2):
2, dofh, dofh, entity_matrix_provider, lhs);

Right-Hand Side (RHS)/Load Vector

LehrFEM++ also provides the lf::assemble::AssembleVectorLocally function for assembling the right-hand side vector \(\vec{b}\). The following example shows how to assemble a right-hand side vector with a custom entity vector provider, where \(b\) is assembled from volume contributions from local cells.

// initialize a 2d mesh somehow
std::shared_ptr<lf::mesh::Mesh> mesh;
// define a EntityVectorProvider which returns the volume for every mesh cell
struct VolumeEntityVectorProvider {
bool isActive(const lf::mesh::Entity& e) const { return e.Codim() == 0; }
Eigen::VectorXd Eval(const lf::mesh::Entity& e) const {
return Eigen::VectorXd::Constant(1, lf::geometry::Volume(*e.Geometry()));
}
} entity_vector_provider;
// define a dof handler which assigns a dof to every mesh cell
lf::assemble::UniformFEDofHandler dofh(
// initialize the output vector:
Eigen::VectorXd x(dofh.NumDofs());
// assemble the global vector over entities with codim=0:
lf::assemble::AssembleVectorLocally(0, dofh, entity_vector_provider, x);
// now x[dofh.GlobalDofIndices(e)[0]] will contain the volume of mesh cell e