Nektar++
Loading...
Searching...
No Matches
TestPyrExactMass.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestPyrExactMass.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: The orthogonal pyramid on its default rule: a diagonal mass
32// matrix, the polynomials of total degree P and the modified pyramid in its
33// span, and polynomial traces of degree P on its triangular faces.
34//
35///////////////////////////////////////////////////////////////////////////////
36
38#include <array>
39#include <boost/test/tools/floating_point_comparison.hpp>
40#include <boost/test/unit_test.hpp>
41#include <cmath>
42#include <vector>
43
45{
46
47using namespace LibUtilities;
48
49/// The orthogonal pyramid of order @p P on @p np points per direction of
50/// the default orthogonal rule: Gauss-Legendre, Gauss-Legendre and
51/// Gauss-Jacobi(1,0) in the collapsed direction.
53 const unsigned int np)
54{
55 const BasisKey b0(eOrtho_A, P + 1, PointsKey(np, eGaussGaussLegendre));
56 const BasisKey b2(eOrthoPyr_C, P + 1,
59}
60
61/// Solve the dense system @p A x = @p b in place by Gaussian elimination
62/// with partial pivoting; @p A is row major, @p n by @p n.
63static std::vector<NekDouble> Solve(std::vector<NekDouble> A,
64 std::vector<NekDouble> b,
65 const unsigned int n)
66{
67 for (unsigned int k = 0; k < n; ++k)
68 {
69 unsigned int piv = k;
70 for (unsigned int i = k + 1; i < n; ++i)
71 {
72 if (std::abs(A[i * n + k]) > std::abs(A[piv * n + k]))
73 {
74 piv = i;
75 }
76 }
77 for (unsigned int j = 0; j < n; ++j)
78 {
79 std::swap(A[k * n + j], A[piv * n + j]);
80 }
81 std::swap(b[k], b[piv]);
82 for (unsigned int i = k + 1; i < n; ++i)
83 {
84 const NekDouble f = A[i * n + k] / A[k * n + k];
85 for (unsigned int j = k; j < n; ++j)
86 {
87 A[i * n + j] -= f * A[k * n + j];
88 }
89 b[i] -= f * b[k];
90 }
91 }
92 for (unsigned int k = n; k-- > 0;)
93 {
94 for (unsigned int j = k + 1; j < n; ++j)
95 {
96 b[k] -= A[k * n + j] * b[j];
97 }
98 b[k] /= A[k * n + k];
99 }
100 return b;
101}
102
104{
105 return e->GetStdMatrix(
107}
108
109/**
110 * With the default NUMPOINTS = NUMMODES the mass matrix is diagonal, and its
111 * diagonal is that of the mass matrix on an over-integrated rule: every mode
112 * has degree at most P in the collapsed direction, so the products the rule
113 * integrates have degree at most 2P + 1, its exactness.
114 */
115BOOST_AUTO_TEST_CASE(TestPyrOrthoMassDiagonal)
116{
117 for (unsigned int P = 1; P <= 8; ++P)
118 {
119 const auto m = Mass(MakeOrthoPyr(P, P + 1));
120 const auto ref = Mass(MakeOrthoPyr(P, 3 * P + 6));
121 const unsigned int n = m->GetRows();
122 NekDouble dmax = 0.0;
123 for (unsigned int i = 0; i < n; ++i)
124 {
125 dmax = std::max(dmax, std::abs(ref->GetValue(i, i)));
126 }
127 NekDouble off = 0.0, diag = 0.0;
128 for (unsigned int i = 0; i < n; ++i)
129 {
130 for (unsigned int j = 0; j < n; ++j)
131 {
132 if (i == j)
133 {
134 diag = std::max(diag, std::abs(m->GetValue(i, i) -
135 ref->GetValue(i, i)));
136 }
137 else
138 {
139 off = std::max(off, std::abs(m->GetValue(i, j)));
140 }
141 }
142 }
143 BOOST_TEST_INFO("P = " << P);
144 BOOST_CHECK_SMALL(off / dmax, 1.0e-13);
145 BOOST_CHECK_SMALL(diag / dmax, 1.0e-13);
146 }
147}
148
149/// Every monomial x^a y^b z^c of total degree at most P is in the span: its
150/// projection reproduces it at the quadrature points.
151BOOST_AUTO_TEST_CASE(TestPyrOrthoPolynomials)
152{
153 for (unsigned int P = 1; P <= 6; ++P)
154 {
155 const auto e = MakeOrthoPyr(P, P + 3);
156 const unsigned int nq = e->GetTotPoints();
157 Array<OneD, NekDouble> x(nq), y(nq), z(nq), f(nq), c(e->GetNcoeffs()),
158 g(nq);
159 e->GetCoords(x, y, z);
160 for (unsigned int a = 0; a <= P; ++a)
161 {
162 for (unsigned int b = 0; a + b <= P; ++b)
163 {
164 for (unsigned int k = 0; a + b + k <= P; ++k)
165 {
166 for (unsigned int i = 0; i < nq; ++i)
167 {
168 f[i] = std::pow(x[i], a) * std::pow(y[i], b) *
169 std::pow(z[i], k);
170 }
171 e->FwdTrans(f, c);
172 e->BwdTrans(c, g);
173 NekDouble err = 0.0;
174 for (unsigned int i = 0; i < nq; ++i)
175 {
176 err = std::max(err, std::abs(g[i] - f[i]));
177 }
178 BOOST_TEST_INFO("P = " << P << ", x^" << a << " y^" << b
179 << " z^" << k);
180 BOOST_CHECK_SMALL(err, 1.0e-11);
181 }
182 }
183 }
184 }
185}
186
187/**
188 * On the triangular face y = -1 every mode is a polynomial of total degree at
189 * most P in (x, z), as on a tetrahedron's face. Each mode is interpolated
190 * from a degree-P fit on a lattice of the face and checked at other face
191 * points.
192 */
193BOOST_AUTO_TEST_CASE(TestPyrOrthoTriFaceTrace)
194{
195 for (unsigned int P = 1; P <= 6; ++P)
196 {
197 // Enough points that evaluating a mode from its values is exact.
198 const auto e = MakeOrthoPyr(P, 2 * P + 3);
199 const unsigned int nq = e->GetTotPoints();
200 const unsigned int nm = e->GetNcoeffs();
201
202 // A lattice of the face shrunk about its centroid, so the apex is
203 // not sampled, and the degree-P monomials in (x, z).
204 const NekDouble shrink = 0.8, cx = -1.0 / 3.0, cz = -1.0 / 3.0;
205 std::vector<std::array<NekDouble, 2>> lattice, check;
206 for (unsigned int i = 0; i <= P; ++i)
207 {
208 for (unsigned int k = 0; i + k <= P; ++k)
209 {
210 const NekDouble lx = -1.0 + 2.0 * i / P;
211 const NekDouble lz = -1.0 + 2.0 * k / P;
212 lattice.push_back(
213 {cx + shrink * (lx - cx), cz + shrink * (lz - cz)});
214 }
215 }
216 for (const auto &st : std::vector<std::array<NekDouble, 2>>{
217 {0.13, 0.21}, {0.37, 0.11}, {0.05, 0.60}, {0.41, 0.33}})
218 {
219 check.push_back({-1.0 + 2.0 * st[0], -1.0 + 2.0 * st[1]});
220 }
221 const unsigned int nl = lattice.size();
222 auto monomial = [P](const std::array<NekDouble, 2> &pt,
223 const unsigned int j) {
224 unsigned int cnt = 0;
225 for (unsigned int a = 0; a <= P; ++a)
226 {
227 for (unsigned int k = 0; a + k <= P; ++k, ++cnt)
228 {
229 if (cnt == j)
230 {
231 return std::pow(pt[0], a) * std::pow(pt[1], k);
232 }
233 }
234 }
235 return 0.0;
236 };
237 std::vector<NekDouble> V(nl * nl);
238 for (unsigned int i = 0; i < nl; ++i)
239 {
240 for (unsigned int j = 0; j < nl; ++j)
241 {
242 V[i * nl + j] = monomial(lattice[i], j);
243 }
244 }
245
246 Array<OneD, NekDouble> phys(nq), coord(3);
247 for (unsigned int m = 0; m < nm; ++m)
248 {
249 e->FillMode(m, phys);
250 auto eval = [&](const std::array<NekDouble, 2> &pt) {
251 coord[0] = pt[0];
252 coord[1] = -1.0;
253 coord[2] = pt[1];
254 return e->PhysEvaluate(coord, phys);
255 };
256 std::vector<NekDouble> vals(nl);
257 for (unsigned int i = 0; i < nl; ++i)
258 {
259 vals[i] = eval(lattice[i]);
260 }
261 const std::vector<NekDouble> coef = Solve(V, vals, nl);
262 NekDouble err = 0.0, scale = 1.0;
263 for (const auto &pt : check)
264 {
265 NekDouble fit = 0.0;
266 for (unsigned int j = 0; j < nl; ++j)
267 {
268 fit += coef[j] * monomial(pt, j);
269 }
270 const NekDouble v = eval(pt);
271 err = std::max(err, std::abs(fit - v));
272 scale = std::max(scale, std::abs(v));
273 }
274 BOOST_TEST_INFO("P = " << P << ", mode " << m);
275 BOOST_CHECK_SMALL(err / scale, 1.0e-9);
276 }
277 }
278}
279
280/**
281 * The modified (C0) pyramid against the orthogonal span, which spectral
282 * vanishing viscosity and other modal operations transform to. Its five
283 * vertex modes, rational functions of (x, y, z), lie in the span for every
284 * order, where with the old p+q exponent four of them did not even at P = 1.
285 * Up to P = 2 every modified mode lies in it. From P = 3 the modified
286 * quadrilateral-face and interior modes whose exponent p+q-2 exceeds
287 * max(p,q) do not; they carry the same mismatch the orthogonal pyramid had.
288 */
289BOOST_AUTO_TEST_CASE(TestPyrModifiedInOrthoSpan)
290{
291 for (unsigned int P = 1; P <= 6; ++P)
292 {
293 // One grid for both, rich enough for either space.
294 const unsigned int np = P + 3;
295 const PointsKey gll(np, eGaussLobattoLegendre);
296 const PointsKey grj(np, eGaussRadauMAlpha2Beta0);
297 const auto mod =
299 BasisKey(eModified_A, P + 1, gll),
300 BasisKey(eModified_A, P + 1, gll),
301 BasisKey(eModifiedPyr_C, P + 1, grj));
302 const auto orth =
304 BasisKey(eOrtho_A, P + 1, gll), BasisKey(eOrtho_A, P + 1, gll),
305 BasisKey(eOrthoPyr_C, P + 1, grj));
306 BOOST_REQUIRE_EQUAL(mod->GetNcoeffs(), orth->GetNcoeffs());
307
308 std::vector<unsigned int> modes;
309 if (P <= 2)
310 {
311 for (unsigned int m = 0; m < mod->GetNcoeffs(); ++m)
312 {
313 modes.push_back(m);
314 }
315 }
316 else
317 {
318 for (unsigned int v = 0; v < 5; ++v)
319 {
320 modes.push_back(mod->GetVertexMap(v));
321 }
322 }
323
324 const unsigned int nq = mod->GetTotPoints();
325 Array<OneD, NekDouble> f(nq), c(orth->GetNcoeffs()), g(nq);
326 for (const unsigned int m : modes)
327 {
328 mod->FillMode(m, f);
329 orth->FwdTrans(f, c);
330 orth->BwdTrans(c, g);
331 NekDouble err = 0.0, scale = 0.0;
332 for (unsigned int i = 0; i < nq; ++i)
333 {
334 err = std::max(err, std::abs(g[i] - f[i]));
335 scale = std::max(scale, std::abs(f[i]));
336 }
337 BOOST_TEST_INFO("P = " << P << ", modified mode " << m);
338 BOOST_CHECK_SMALL(err / scale, 1.0e-11);
339 }
340 }
341}
342
343} // namespace Nektar::StdRegionsUnitTests
#define V(k, j)
Describes the specification for a Basis.
Definition Basis.h:45
Defines a specification for a set of points.
Definition Points.h:50
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
@ eGaussGaussAlpha1Beta0
1D Gauss-Jacobi quadrature points,
Definition PointsType.h:100
@ eGaussLobattoLegendre
1D Gauss-Lobatto-Legendre quadrature points
Definition PointsType.h:51
@ eGaussGaussLegendre
1D Gauss-Gauss-Legendre quadrature points
Definition PointsType.h:46
@ eOrtho_A
Principle Orthogonal Functions .
Definition BasisType.h:42
@ eModifiedPyr_C
Principle Modified Functions.
Definition BasisType.h:53
@ eModified_A
Principle Modified Functions .
Definition BasisType.h:48
@ eOrthoPyr_C
Principle Orthogonal Functions .
Definition BasisType.h:51
std::shared_ptr< StdPyrExp > StdPyrExpSharedPtr
Definition StdPyrExp.h:179
static DNekMatSharedPtr Mass(const StdRegions::StdPyrExpSharedPtr &e)
BOOST_AUTO_TEST_CASE(TestQuadInvBwdTransPseudoInverse)
static StdRegions::StdPyrExpSharedPtr MakeOrthoPyr(const unsigned int P, const unsigned int np)
The orthogonal pyramid of order P on np points per direction of the default orthogonal rule: Gauss-Le...
static std::vector< NekDouble > Solve(std::vector< NekDouble > A, std::vector< NekDouble > b, const unsigned int n)
Solve the dense system A x = b in place by Gaussian elimination with partial pivoting; A is row major...
std::shared_ptr< DNekMat > DNekMatSharedPtr