LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
tria_o2.cc
1
9#include "tria_o2.h"
10
11#include <Eigen/Eigen>
12
13#include "point.h"
14
15namespace lf::geometry {
16
17TriaO2::TriaO2(Eigen::Matrix<double, Eigen::Dynamic, 6> coords)
18 : coords_(std::move(coords)),
19 alpha_(coords_.rows()),
20 beta_(coords_.rows(), 2),
21 gamma_(coords_.rows(), 2),
22 delta_(coords_.rows()),
23 gamma_x_2_(coords_.rows(), 2) {
24 /*
25 * 2 C
26 * | \ | \
27 * 5 4 -> F E
28 * | \ | \
29 * 0 - 3 - 1 A - D - B
30 */
31 const Eigen::VectorXd& A = coords_.col(0);
32 const Eigen::VectorXd& B = coords_.col(1);
33 const Eigen::VectorXd& C = coords_.col(2);
34 const Eigen::VectorXd& D = coords_.col(3);
35 const Eigen::VectorXd& E = coords_.col(4);
36 const Eigen::VectorXd& F = coords_.col(5);
37
38 // Compute monomial coeffcients of componentwise quadratic
39 // polynomial
40 alpha_ << A;
41 beta_ << 4. * D - 3. * A - B, 4. * F - 3. * A - C;
42 gamma_ << 2. * (A + B) - 4. * D, 2. * (A + C) - 4. * F;
43 delta_ << 4. * (A + E - D - F);
44 // coefficient for Jacobian()
45 gamma_x_2_ << 2. * gamma_;
46}
47
48/* SAM_LISTING_BEGIN_1 */
49Eigen::MatrixXd TriaO2::Global(const Eigen::MatrixXd& local) const {
50 LF_VERIFY_MSG((0. <= local.array()).all() && (local.array() <= 1.).all(),
51 "local coordinates out of bounds for reference element");
52 // Direct vectorized evaluation of componentwise quadratic mapping
53 // given through its monomial coefficients.
54 return ((beta_ * local) + (gamma_ * local.array().square().matrix()) +
55 (delta_ * local.row(0).cwiseProduct(local.row(1))))
56 .colwise() +
57 alpha_;
58}
59
60/* SAM_LISTING_END_1 */
61
62/* SAM_LISTING_BEGIN_2 */
63Eigen::MatrixXd TriaO2::Jacobian(const Eigen::MatrixXd& local) const {
64 LF_VERIFY_MSG((0. <= local.array()).all() && (local.array() <= 1.).all(),
65 "local coordinates out of bounds for reference element");
66
67 Eigen::MatrixXd tmp(gamma_.rows(), 2 * local.cols());
68
69 for (long i = 0; i < local.cols(); ++i) {
70 tmp.block(0, 2 * i, tmp.rows(), 2) =
71 gamma_x_2_.array().rowwise() * local.col(i).transpose().array();
72 }
73
74 Eigen::MatrixXd local_reversed = local.colwise().reverse();
75 local_reversed.resize(1, local.size());
76
77 return beta_.replicate(1, local.cols()) + tmp +
78 (local_reversed.replicate(delta_.rows(), 1).array().colwise() *
79 delta_.array())
80 .matrix();
81}
82
83/* SAM_LISTING_END_2 */
84
85/* SAM_LISTING_BEGIN_3 */
87 const Eigen::MatrixXd& local) const {
88 LF_VERIFY_MSG((0. <= local.array()).all() && (local.array() <= 1.).all(),
89 "local coordinates out of bounds for reference element");
90
91 Eigen::MatrixXd jac = Jacobian(local);
92 Eigen::MatrixXd jacInvGram(jac.rows(), jac.cols());
93
94 if (DimGlobal() == 2) {
95 for (long i = 0; i < local.cols(); ++i) {
96 auto jacobian = jac.block(0, 2 * i, 2, 2);
97 jacInvGram.block(0, 2 * i, 2, 2) << jacobian(1, 1), -jacobian(1, 0),
98 -jacobian(0, 1), jacobian(0, 0);
99 jacInvGram.block(0, 2 * i, 2, 2) /= jacobian.determinant();
100 }
101 } else {
102 for (long i = 0; i < local.cols(); ++i) {
103 auto jacobian = jac.block(0, 2 * i, jac.rows(), 2);
104
105 auto A = jacobian.col(0);
106 auto B = jacobian.col(1);
107 auto AB = A.dot(B);
108
109 Eigen::MatrixXd tmp(2, 2);
110 tmp << B.dot(B), -AB, -AB, A.dot(A);
111
112 jacInvGram.block(0, 2 * i, jac.rows(), 2) =
113 jacobian * tmp / tmp.determinant();
114 }
115 }
116
117 return jacInvGram;
118}
119/* SAM_LISTING_END_3 */
120
121/* SAM_LISTING_BEGIN_4 */
122Eigen::VectorXd TriaO2::IntegrationElement(const Eigen::MatrixXd& local) const {
123 LF_VERIFY_MSG((0. <= local.array()).all() && (local.array() <= 1.).all(),
124 "local coordinates out of bounds for reference element");
125
126 Eigen::MatrixXd jac = Jacobian(local);
127 Eigen::VectorXd intElem(local.cols());
128
129 if (DimGlobal() == 2) {
130 for (long i = 0; i < local.cols(); ++i) {
131 intElem(i) = std::abs(jac.block(0, 2 * i, 2, 2).determinant());
132 }
133 } else {
134 for (long i = 0; i < local.cols(); ++i) {
135 auto jacobian = jac.block(0, 2 * i, jac.rows(), 2);
136
137 auto A = jacobian.col(0);
138 auto B = jacobian.col(1);
139 auto AB = A.dot(B);
140
141 intElem(i) = std::sqrt(std::abs(A.dot(A) * B.dot(B) - AB * AB));
142 }
143 }
144 return intElem;
145}
146/* SAM_LISTING_END_4 */
147
148std::unique_ptr<Geometry> TriaO2::SubGeometry(dim_t codim, dim_t i) const {
149 switch (codim) {
150 case 0: {
151 LF_ASSERT_MSG(i == 0, "i is out of bounds");
152 return std::make_unique<TriaO2>(coords_);
153 }
154 case 1: {
155 LF_ASSERT_MSG(0 <= i && i <= 2, "i is out of bounds");
156 return std::make_unique<SegmentO2>(
157 (Eigen::Matrix<double, Eigen::Dynamic, 3>(DimGlobal(), 3)
158 << coords_.col(i),
159 coords_.col((i + 1) % 3), coords_.col(i + 3))
160 .finished());
161 }
162 case 2: {
163 LF_ASSERT_MSG(0 <= i && i <= 5, "i is out of bounds");
164 return std::make_unique<Point>(coords_.col(i));
165 }
166 default: {
167 LF_VERIFY_MSG(false, "codim is out of bounds")
168 }
169 }
170}
171
172std::vector<std::unique_ptr<Geometry>> TriaO2::ChildGeometry(
173 const RefinementPattern& ref_pat, lf::base::dim_t codim) const {
174 LF_VERIFY_MSG(ref_pat.RefEl() == lf::base::RefEl::kTria(),
175 "Refinement pattern for " << ref_pat.RefEl().ToString());
176 LF_VERIFY_MSG(codim < 3, "Illegal codim " << codim);
177
178 const double hLattice = 1. / static_cast<double>(ref_pat.LatticeConst());
179 std::vector<std::unique_ptr<Geometry>> childGeoPtrs = {};
180
181 // get coordinates of childGeometries
182 std::vector<Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic>> childPolygons(
183 ref_pat.ChildPolygons(codim));
184
185 const int noChildren = childPolygons.size();
186 LF_VERIFY_MSG(
187 noChildren == ref_pat.NumChildren(codim),
188 "NumChildren " << noChildren << " <-> " << ref_pat.NumChildren(codim));
189
190 // create a geometry object for each child
191 for (size_t child = 0; child < noChildren; ++child) {
192 // codim == 0: a child triangle is described by a lattice polygon with six
193 // vertices
194 // codim == 1: a child segment is described by a polygon with three vertices
195 // codim == 2: a child point by a single point ("polygon with one corner")
196 LF_VERIFY_MSG(
197 childPolygons[child].rows() == 2,
198 "childPolygons[child].rows() = " << childPolygons[child].rows());
199 LF_VERIFY_MSG(
200 childPolygons[child].cols() == (3 - codim),
201 "childPolygons[child].cols() = " << childPolygons[child].cols());
202
203 const Eigen::MatrixXd locChildPolygonCoords(
204 hLattice * childPolygons[child].cast<double>());
205
206 switch (codim) {
207 case 0: {
208 Eigen::MatrixXd locChildCoords(locChildPolygonCoords.rows(), 6);
209 locChildCoords << locChildPolygonCoords,
210 (locChildPolygonCoords.col(0) + locChildPolygonCoords.col(1)) / 2.,
211 (locChildPolygonCoords.col(1) + locChildPolygonCoords.col(2)) / 2.,
212 (locChildPolygonCoords.col(2) + locChildPolygonCoords.col(0)) / 2.;
213
214 childGeoPtrs.push_back(
215 std::make_unique<TriaO2>(Global(locChildCoords)));
216
217 break;
218 }
219 case 1: {
220 Eigen::MatrixXd locChildCoords(locChildPolygonCoords.rows(), 3);
221 locChildCoords << locChildPolygonCoords,
222 (locChildPolygonCoords.col(0) + locChildPolygonCoords.col(1)) / 2.;
223
224 childGeoPtrs.push_back(
225 std::make_unique<SegmentO2>(Global(locChildCoords)));
226
227 break;
228 }
229 case 2: {
230 childGeoPtrs.push_back(
231 std::make_unique<Point>(Global(locChildPolygonCoords)));
232
233 break;
234 }
235 default: {
236 LF_VERIFY_MSG(false, "Illegal co-dimension");
237 }
238 }
239 }
240
241 return childGeoPtrs;
242}
243
244} // namespace lf::geometry
static constexpr RefEl kTria()
Returns the reference triangle.
Definition: ref_el.h:158
std::string ToString() const
Return a string representation of this Reference element.
Definition: ref_el.h:455
base::RefEl::dim_t dim_t
Abstract interface class for encoding topological local refinement
lf::base::size_type LatticeConst() const
Provides information about lattice constant used.
virtual lf::base::size_type NumChildren(lf::base::dim_t codim) const =0
provide number of child entities of a given co-dimension to be created by refinement
virtual std::vector< Eigen::Matrix< int, Eigen::Dynamic, Eigen::Dynamic > > ChildPolygons(lf::base::dim_t codim) const =0
provide lattice reference coordinates of vertices of child polygons
lf::base::RefEl RefEl() const
Returns topological type of entity for which the current object is set up.
Eigen::MatrixXd Global(const Eigen::MatrixXd &local) const override
Map a number of points in local coordinates into the global coordinate system.
Definition: tria_o2.cc:49
dim_t DimGlobal() const override
Dimension of the image of this mapping.
Definition: tria_o2.h:42
Eigen::Matrix< double, Eigen::Dynamic, 6 > coords_
Coordinates of the 6 vertices/midpoints, stored in matrix columns.
Definition: tria_o2.h:66
Eigen::Matrix< double, Eigen::Dynamic, 2 > beta_
Definition: tria_o2.h:73
Eigen::MatrixXd JacobianInverseGramian(const Eigen::MatrixXd &local) const override
Evaluate the Jacobian * Inverse Gramian ( ) simultaneously at numPoints.
Definition: tria_o2.cc:86
std::unique_ptr< Geometry > SubGeometry(dim_t codim, dim_t i) const override
Construct a new Geometry() object that describes the geometry of the i-th sub-entity with codimension...
Definition: tria_o2.cc:148
Eigen::Matrix< double, Eigen::Dynamic, 1 > alpha_
Definition: tria_o2.h:72
Eigen::Matrix< double, Eigen::Dynamic, 2 > gamma_
Definition: tria_o2.h:74
std::vector< std::unique_ptr< Geometry > > ChildGeometry(const RefinementPattern &ref_pat, lf::base::dim_t codim) const override
Generate geometry objects for child entities created in the course of refinement.
Definition: tria_o2.cc:172
Eigen::Matrix< double, Eigen::Dynamic, 1 > delta_
Definition: tria_o2.h:75
Eigen::VectorXd IntegrationElement(const Eigen::MatrixXd &local) const override
The integration element (factor appearing in integral transformation formula, see below) at number of...
Definition: tria_o2.cc:122
Eigen::Matrix< double, Eigen::Dynamic, 2 > gamma_x_2_
Definition: tria_o2.h:78
TriaO2(Eigen::Matrix< double, Eigen::Dynamic, 6 > coords)
Constructor building triangle from vertex/midpoint coordinates.
Definition: tria_o2.cc:17
Eigen::MatrixXd Jacobian(const Eigen::MatrixXd &local) const override
Evaluate the jacobian of the mapping simultaneously at numPoints points.
Definition: tria_o2.cc:63
unsigned int dim_t
type for dimensions and co-dimensions and numbers derived from them
Definition: base.h:36
Defines the Geometry interface and provides a number of classes that implement this interface + addit...
Definition: compose.h:13