Nektar++
Loading...
Searching...
No Matches
StdRegions/TestReduceOrderCoeffs.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestReduceOrderCoeffs.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: Unit tests for StdExpansion::ReduceOrderCoeffs.
32//
33///////////////////////////////////////////////////////////////////////////////
34
40#include <boost/test/unit_test.hpp>
41
42#include <algorithm>
43#include <cmath>
44#include <vector>
45
47{
48
49/**
50 * @brief Check ReduceOrderCoeffs mode by mode.
51 *
52 * Each mode of the orthogonal expansion @p ortho, of the same shape, order
53 * and points as @p exp, is expressed in @p exp's basis and reduced to
54 * @p numMin. A mode whose degree, in the sense the shape truncates, is below
55 * numMin must come back unchanged; every other mode must vanish.
56 */
59 const std::vector<int> &degree, const int numMin)
60{
61 const int nm = exp->GetNcoeffs();
62 const int nq = exp->GetTotPoints();
63
64 BOOST_REQUIRE_EQUAL(ortho->GetNcoeffs(), nm);
65 BOOST_REQUIRE_EQUAL(ortho->GetTotPoints(), nq);
66 BOOST_REQUIRE_EQUAL(static_cast<int>(degree.size()), nm);
67
68 const NekDouble tol = 1.0e-8;
69
70 for (int mode = 0; mode < nm; ++mode)
71 {
72 Array<OneD, NekDouble> orthoCoeffs(nm, 0.0);
73 orthoCoeffs[mode] = 1.0;
74
75 Array<OneD, NekDouble> phys(nq), coeffs(nm), reduced(nm), back(nq);
76 ortho->BwdTrans(orthoCoeffs, phys);
77 exp->FwdTrans(phys, coeffs);
78 exp->ReduceOrderCoeffs(numMin, coeffs, reduced);
79 exp->BwdTrans(reduced, back);
80
81 const bool kept = degree[mode] < numMin;
82 NekDouble diff = 0.0;
83 for (int q = 0; q < nq; ++q)
84 {
85 const NekDouble expected = kept ? phys[q] : 0.0;
86 diff = std::max(diff, std::abs(back[q] - expected));
87 }
88 BOOST_CHECK_MESSAGE(diff < tol,
89 "mode " << mode << " of degree " << degree[mode]
90 << (kept ? " changed by " : " left ")
91 << diff << " with numMin " << numMin);
92 }
93}
94
95BOOST_AUTO_TEST_CASE(TestSegReduceOrderCoeffs)
96{
97 using namespace LibUtilities;
98 const int nm = 5;
99 const PointsKey pkey(nm + 1, eGaussLobattoLegendre);
100 const BasisKey bkey(eModified_A, nm, pkey);
101 const BasisKey okey(eOrtho_A, nm, pkey);
102
105
106 std::vector<int> degree(nm);
107 for (int p = 0; p < nm; ++p)
108 {
109 degree[p] = p;
110 }
111 CheckReduceOrderCoeffs(exp, ortho, degree, nm - 1);
112 CheckReduceOrderCoeffs(exp, ortho, degree, nm - 2);
113}
114
115void CheckQuad(const int nm0, const int nm1, const int numMin)
116{
117 using namespace LibUtilities;
118 const PointsKey pkey0(nm0 + 1, eGaussLobattoLegendre);
119 const PointsKey pkey1(nm1 + 1, eGaussLobattoLegendre);
120 const BasisKey b0(eModified_A, nm0, pkey0), b1(eModified_A, nm1, pkey1);
121 const BasisKey o0(eOrtho_A, nm0, pkey0), o1(eOrtho_A, nm1, pkey1);
122
124 auto ortho =
126
127 // Mode (p, q) is stored with p fastest; its degree is the larger index.
128 std::vector<int> degree(nm0 * nm1);
129 for (int q = 0; q < nm1; ++q)
130 {
131 for (int p = 0; p < nm0; ++p)
132 {
133 degree[q * nm0 + p] = std::max(p, q);
134 }
135 }
136 CheckReduceOrderCoeffs(exp, ortho, degree, numMin);
137}
138
139BOOST_AUTO_TEST_CASE(TestQuadReduceOrderCoeffs)
140{
141 CheckQuad(4, 4, 3);
142 CheckQuad(4, 4, 2);
143 // Anisotropic: numMin above the smaller mode count, and below it.
144 CheckQuad(4, 6, 5);
145 CheckQuad(6, 4, 3);
146}
147
148void CheckTri(const int nm, const int numMin)
149{
150 using namespace LibUtilities;
151 const PointsKey pkey0(nm + 1, eGaussLobattoLegendre);
152 const PointsKey pkey1(nm, eGaussRadauMAlpha1Beta0);
153 const BasisKey b0(eModified_A, nm, pkey0), b1(eModified_B, nm, pkey1);
154 const BasisKey o0(eOrtho_A, nm, pkey0), o1(eOrtho_B, nm, pkey1);
155
157 auto ortho =
159
160 // Modes are stored with p outermost and q < nm - p; the degree is p + q.
161 std::vector<int> degree;
162 for (int p = 0; p < nm; ++p)
163 {
164 for (int q = 0; q < nm - p; ++q)
165 {
166 degree.push_back(p + q);
167 }
168 }
169 CheckReduceOrderCoeffs(exp, ortho, degree, numMin);
170}
171
172BOOST_AUTO_TEST_CASE(TestTriReduceOrderCoeffs)
173{
174 CheckTri(4, 3);
175 // More than one order removed.
176 CheckTri(5, 3);
177}
178
179void CheckTet(const int nm, const int numMin)
180{
181 using namespace LibUtilities;
182 const PointsKey pkey0(nm + 1, eGaussLobattoLegendre);
183 const PointsKey pkey1(nm, eGaussRadauMAlpha1Beta0);
184 const PointsKey pkey2(nm, eGaussRadauMAlpha2Beta0);
185 const BasisKey b0(eModified_A, nm, pkey0), b1(eModified_B, nm, pkey1),
186 b2(eModified_C, nm, pkey2);
187 const BasisKey o0(eOrtho_A, nm, pkey0), o1(eOrtho_B, nm, pkey1),
188 o2(eOrtho_C, nm, pkey2);
189
190 auto exp =
192 auto ortho =
194
195 // Modes are stored with p outermost, then q < nm - p, then r < nm - p - q;
196 // the degree is p + q + r.
197 std::vector<int> degree;
198 for (int p = 0; p < nm; ++p)
199 {
200 for (int q = 0; q < nm - p; ++q)
201 {
202 for (int r = 0; r < nm - p - q; ++r)
203 {
204 degree.push_back(p + q + r);
205 }
206 }
207 }
208 CheckReduceOrderCoeffs(exp, ortho, degree, numMin);
209}
210
211BOOST_AUTO_TEST_CASE(TestTetReduceOrderCoeffs)
212{
213 CheckTet(4, 3);
214 CheckTet(5, 3);
215}
216
217BOOST_AUTO_TEST_CASE(TestPrismReduceOrderCoeffs)
218{
219 using namespace LibUtilities;
220 const int nm = 4;
221 const PointsKey pkey0(nm + 1, eGaussLobattoLegendre);
222 const PointsKey pkey2(nm, eGaussRadauMAlpha1Beta0);
223 const BasisKey b0(eModified_A, nm, pkey0), b2(eModified_B, nm, pkey2);
224 const BasisKey o0(eOrtho_A, nm, pkey0), o2(eOrtho_B, nm, pkey2);
225
226 auto exp =
228 auto ortho =
230
231 // Modes are stored with p outermost, then q, then r < nm - p; the degree
232 // is the larger of q and the collapsed pair's total p + r.
233 std::vector<int> degree;
234 for (int p = 0; p < nm; ++p)
235 {
236 for (int q = 0; q < nm; ++q)
237 {
238 for (int r = 0; r < nm - p; ++r)
239 {
240 degree.push_back(std::max(q, p + r));
241 }
242 }
243 }
244 CheckReduceOrderCoeffs(exp, ortho, degree, nm - 1);
245}
246
247} // namespace Nektar::StdRegionsUnitTests
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.
@ eGaussLobattoLegendre
1D Gauss-Lobatto-Legendre quadrature points
Definition PointsType.h:51
@ eModified_B
Principle Modified Functions .
Definition BasisType.h:49
@ eOrtho_A
Principle Orthogonal Functions .
Definition BasisType.h:42
@ eModified_C
Principle Modified Functions .
Definition BasisType.h:50
@ eOrtho_C
Principle Orthogonal Functions .
Definition BasisType.h:46
@ eOrtho_B
Principle Orthogonal Functions .
Definition BasisType.h:44
@ eModified_A
Principle Modified Functions .
Definition BasisType.h:48
std::shared_ptr< StdExpansion > StdExpansionSharedPtr
void CheckQuad(const int nm0, const int nm1, const int numMin)
void CheckTet(const int nm, const int numMin)
void CheckTri(const int nm, const int numMin)
BOOST_AUTO_TEST_CASE(TestQuadInvBwdTransPseudoInverse)
void CheckReduceOrderCoeffs(const StdRegions::StdExpansionSharedPtr &exp, const StdRegions::StdExpansionSharedPtr &ortho, const std::vector< int > &degree, const int numMin)
Check ReduceOrderCoeffs mode by mode.