LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
utils.cc
1#include "utils.h"
2
3#include <lf/geometry/geometry.h>
4
6
7Eigen::Matrix<double, 2, 3> computeOutwardNormals(
8 const lf::mesh::Entity &entity) {
9 // Get the geometry of the entity
10 const auto *const geom = entity.Geometry();
11 // Compute the global vertex coordinates
12 Eigen::MatrixXd vertices = geom->Global(entity.RefEl().NodeCoords());
13 // Use the vertex coordinates to compute the local normals on the edges
14 Eigen::Matrix<double, 2, 3> normals(vertices.rows(), vertices.cols());
15 for (long vert = 0; vert < vertices.cols() - 1; ++vert) {
16 normals(0, vert) = vertices(1, vert + 1) - vertices(1, vert + 0);
17 normals(1, vert) = vertices(0, vert + 0) - vertices(0, vert + 1);
18 }
19 normals(0, vertices.cols() - 1) =
20 vertices(1, 0) - vertices(1, vertices.cols() - 1);
21 normals(1, vertices.cols() - 1) =
22 vertices(0, vertices.cols() - 1) - vertices(0, 0);
23 // Compute test vectors to test whether the normal faces inward or outward
24 Eigen::Matrix<double, 2, 3> test;
25 test << vertices.col(2) - vertices.col(0), vertices.col(0) - vertices.col(1),
26 vertices.col(1) - vertices.col(2);
27 // Compute the sign to flip the normals
28 Eigen::Matrix<double, 1, 3> flip =
29 (normals.array() * test.array()).matrix().colwise().sum();
30 normals.array().row(0) *= flip.array();
31 normals.array().row(1) *= flip.array();
32 normals = -normals.colwise().normalized();
33 return normals;
34}
35
36Eigen::Matrix<double, 2, 3> computeTangentials(const lf::mesh::Entity &entity) {
37 // Compute the tangentials from the rotated normals
38 const auto normals = computeOutwardNormals(entity);
39 Eigen::Matrix<double, 2, 3> tang;
40 tang << -normals.row(1), normals.row(0);
41 return tang;
42}
43
44} // end namespace projects::ipdg_stokes::mesh
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 base::RefEl RefEl() const =0
Describes the reference element type of this entity.
Eigen::Matrix< double, 2, 3 > computeOutwardNormals(const lf::mesh::Entity &entity)
Compute the outward pointing normals of a triangle.
Definition: utils.cc:7
Eigen::Matrix< double, 2, 3 > computeTangentials(const lf::mesh::Entity &entity)
Compute the tangentials onto a triangle.
Definition: utils.cc:36