MFEM v4.8.0
Finite element discretization library
Loading...
Searching...
No Matches
eltrans_basis.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_ELTRANS_BASIS
13#define MFEM_ELTRANS_BASIS
14
16
17#include "../geom.hpp"
18
19// this file contains utilities for computing nodal basis functions and their
20// derivatives in device kernels
21
22namespace mfem
23{
24
25namespace eltrans
26{
27
28/// Various utilities for working with different element geometries
29template <int GeomType> struct GeometryUtils;
30
31template <> struct GeometryUtils<Geometry::SEGMENT>
32{
33 static constexpr MFEM_HOST_DEVICE int Dimension() { return 1; }
34
35 /// @b true if the given point x in ref space is inside the element
36 static bool MFEM_HOST_DEVICE inside(real_t x) { return x >= 0 && x <= 1; }
37
38 /// @b Bound the reference coordinate @a x += dx to be inside the segment.
39 /// @a dx is updated to be dx = project(x+dx) - x
40 /// @return true if x + dx hit a boundary
41 static bool MFEM_HOST_DEVICE project(real_t &x, real_t &dx)
42 {
43 real_t tmp = x;
44 x += dx;
45 if (x < 0)
46 {
47 x = 0;
48 dx = x - tmp;
49 return true;
50 }
51 if (x > 1)
52 {
53 x = 1;
54 dx = x - tmp;
55 return true;
56 }
57 return false;
58 }
59};
60
61template <> struct GeometryUtils<Geometry::SQUARE>
62{
63 static constexpr MFEM_HOST_DEVICE int Dimension() { return 2; }
64 /// @b true if the given point (x,y) in ref space is inside the element
65 static bool MFEM_HOST_DEVICE inside(real_t x, real_t y)
66 {
67 return (x >= 0) && (x <= 1) && (y >= 0) && (y <= 1);
68 }
69
70 /// @b Bound the reference coordinate @a (x,y) += (dx,dy) to be inside the
71 /// square.
72 /// @a dx and @a dy are updated to be (dx,dy) = project(x+dx,y+dy) - (x,y)
73 /// @return true if (x,y) + (dx,dy) hit a boundary
74 static bool MFEM_HOST_DEVICE project(real_t &x, real_t &y, real_t &dx,
75 real_t &dy)
76 {
79 return x_cond || y_cond;
80 }
81};
82
83template <> struct GeometryUtils<Geometry::CUBE>
84{
85 static constexpr MFEM_HOST_DEVICE int Dimension() { return 3; }
86 /// @b true if the given point (x,y,z) in ref space is inside the element
87 static bool MFEM_HOST_DEVICE inside(real_t x, real_t y, real_t z)
88 {
89 return (x >= 0) && (x <= 1) && (y >= 0) && (y <= 1) && (z >= 0) && (z <= 1);
90 }
91
92 /// @b Bound the reference coordinate @a (x,y,z) += (dx,dy,dz) to be inside
93 /// the cube.
94 /// @a dx, @a dy, and @ dz are updated to be
95 /// (dx,dy,dz) = project(x+dx,y+dy,z+dz) - (x,y,z)
96 /// @return true if (x,y,z) + (dx,dy,dz) hit a boundary
97 static bool MFEM_HOST_DEVICE project(real_t &x, real_t &y, real_t &z,
98 real_t &dx, real_t &dy, real_t &dz)
99 {
103 return x_cond || y_cond || z_cond;
104 }
105};
106
107/// 1D Lagrange basis from [0, 1]
109{
110public:
111 /// interpolant node locations, in reference space
112 const real_t *z;
113
114 /// number of points
115 int pN;
116
117 /// @b Evaluates the @a i'th Lagrange polynomial at @a x
118 real_t MFEM_HOST_DEVICE eval(real_t x, int i) const
119 {
120 real_t u0 = 1;
121 real_t den = 1;
122 for (int j = 0; j < pN; ++j)
123 {
124 if (i != j)
125 {
126 real_t d_j = (x - z[j]);
127 u0 = d_j * u0;
128 den *= (z[i] - z[j]);
129 }
130 }
131 den = 1 / den;
132 return u0 * den;
133 }
134
135 /// @b Evaluates the @a i'th Lagrange polynomial and its first derivative at
136 /// @a x
137 void MFEM_HOST_DEVICE eval_d1(real_t &p, real_t &d1, real_t x, int i) const
138 {
139 real_t u0 = 1;
140 real_t u1 = 0;
141 real_t den = 1;
142 for (int j = 0; j < pN; ++j)
143 {
144 if (i != j)
145 {
146 real_t d_j = (x - z[j]);
147 u1 = d_j * u1 + u0;
148 u0 = d_j * u0;
149 den *= (z[i] - z[j]);
150 }
151 }
152 den = 1 / den;
153 p = u0 * den;
154 d1 = u1 * den;
155 }
156
157 /// @b Evaluates the @a i'th Lagrange polynomial and its first and second
158 /// derivatives at @a x
159 void MFEM_HOST_DEVICE eval_d2(real_t &p, real_t &d1, real_t &d2, real_t x,
160 int i) const
161 {
162 real_t u0 = 1;
163 real_t u1 = 0;
164 real_t u2 = 0;
165 real_t den = 1;
166 for (int j = 0; j < pN; ++j)
167 {
168 if (i != j)
169 {
170 real_t d_j = (x - z[j]);
171 u2 = d_j * u2 + u1;
172 u1 = d_j * u1 + u0;
173 u0 = d_j * u0;
174 den *= (z[i] - z[j]);
175 }
176 }
177 den = 1 / den;
178 p = den * u0;
179 d1 = den * u1;
180 d2 = 2 * den * u2;
181 }
182};
183
184} // namespace eltrans
185} // namespace mfem
186
187#endif
1D Lagrange basis from [0, 1]
real_t MFEM_HOST_DEVICE eval(real_t x, int i) const
Evaluates the i'th Lagrange polynomial at x
const real_t * z
interpolant node locations, in reference space
void MFEM_HOST_DEVICE eval_d2(real_t &p, real_t &d1, real_t &d2, real_t x, int i) const
void MFEM_HOST_DEVICE eval_d1(real_t &p, real_t &d1, real_t x, int i) const
int pN
number of points
float real_t
Definition config.hpp:43
real_t p(const Vector &x, real_t t)
static bool MFEM_HOST_DEVICE inside(real_t x, real_t y, real_t z)
true if the given point (x,y,z) in ref space is inside the element
static constexpr MFEM_HOST_DEVICE int Dimension()
static bool MFEM_HOST_DEVICE project(real_t &x, real_t &y, real_t &z, real_t &dx, real_t &dy, real_t &dz)
static constexpr MFEM_HOST_DEVICE int Dimension()
static bool MFEM_HOST_DEVICE project(real_t &x, real_t &dx)
static bool MFEM_HOST_DEVICE inside(real_t x)
true if the given point x in ref space is inside the element
static constexpr MFEM_HOST_DEVICE int Dimension()
static bool MFEM_HOST_DEVICE inside(real_t x, real_t y)
true if the given point (x,y) in ref space is inside the element
static bool MFEM_HOST_DEVICE project(real_t &x, real_t &y, real_t &dx, real_t &dy)
Various utilities for working with different element geometries.