Nektar++
Loading...
Searching...
No Matches
TestPoissonSolve.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestPoissonSolve.hpp
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: Fixture for the PoissonSolve operator. Builds on the shared
32// TestLinearSolver base in TestLinearSolver.hpp.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#pragma once
37
39
40#include <SolverCore/GlobalLinSysOps/LinearSolvers/LinearSolverOp.hpp>
41#include <SolverCore/GlobalLinSysOps/LinearSystems/PoissonSolve/PoissonSolveOp.hpp>
42#include <SolverCore/PreconOps/PreconOp.hpp>
43
44using namespace Nektar::SolverCore;
45
46template <typename TData>
48{
49public:
50 TestPoissonSolve() = default;
51
53 {
54 // Set initial conditions.
55 const unsigned int numComp =
56 this->m_in.GetNumComponents() * this->m_in.GetNumHomoModes();
57 const size_t nphys = this->m_expList->GetTotPoints();
58 m_coordDim = this->m_expList->GetCoordim(0);
59
60 Array<OneD, TData> x(nphys, 0.0);
61 Array<OneD, TData> y(nphys, 0.0);
62 Array<OneD, TData> z(nphys, 0.0);
63 Array<OneD, TData> fce(numComp * nphys, 0.0);
64 this->m_expList->GetCoords(x, y, z);
65
66 if (this->m_session->DefinesFunction("Forcing"))
67 {
68 for (unsigned int n = 0; n < numComp; ++n)
69 {
70 auto func = this->m_session->GetFunction(
71 "Forcing", n % this->m_session->GetVariables().size());
72 Array<OneD, TData> fceVar = fce + n * nphys;
73 func->Evaluate(x, y, z, fceVar);
74 }
75 }
76
77 for (unsigned int blk = 0; blk < this->m_in.GetBlocks().size(); ++blk)
78 {
79 auto &block = this->m_in.GetBlocks()[blk];
80 auto inptr =
81 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
82 for (unsigned int n = 0; n < numComp; ++n)
83 {
84 auto xptr = x.data();
85 auto yptr = y.data();
86 auto zptr = z.data();
87 auto fceptr = fce.data() + n * nphys;
88
89 for (size_t el = 0, cnt = 0; el < block.GetNumElements(); ++el)
90 {
91 for (unsigned int phys = 0; phys < block.GetNumData();
92 ++phys, ++cnt)
93 {
94 if (this->m_session->DefinesFunction("Forcing"))
95 {
96 inptr[cnt] = *(fceptr++);
97 }
98 else
99 {
100 inptr[cnt] = 1.0 + n;
101 if (m_coordDim == 1)
102 {
103 for (unsigned int p = 1; p < 4; ++p)
104 {
105 inptr[cnt] +=
106 (n + 1) * p * std::pow(*xptr, p);
107 }
108 xptr++;
109 }
110 else if (m_coordDim == 2)
111 {
112 for (unsigned int p = 1; p < 4; ++p)
113 {
114 inptr[cnt] += (n + 1) * p *
115 std::pow(*xptr, p) *
116 std::pow(*yptr, p);
117 }
118 xptr++;
119 yptr++;
120 }
121 else
122 {
123 for (unsigned int p = 1; p < 4; ++p)
124 {
125 inptr[cnt] +=
126 (n + 1) * p * std::pow(*xptr, p) *
127 std::pow(*yptr, p) * std::pow(*zptr, p);
128 }
129 xptr++;
130 yptr++;
131 zptr++;
132 }
133 }
134 }
135 }
136
137 inptr += block.CompSize();
138 }
139 }
140
141 this->m_out.template Initialize<NektarSpaces::HostSpace>(0.0);
142
143 // Set up diffusion coefficient.
144 const auto nDiffCoeff = m_coordDim * (m_coordDim + 1) / 2;
145 m_diffCoeff.resize(nDiffCoeff);
146
147 // Set up (isotropic) diffusion coefficient.
148 if (m_coordDim == 1)
149 {
150 m_diffCoeff[0] = 1.0; // D00
151 }
152 else if (m_coordDim == 2)
153 {
154 m_diffCoeff[0] = 2.0; // D00
155 m_diffCoeff[2] = 3.0; // D11
156 }
157 else
158 {
159 m_diffCoeff[0] = 2.0; // D00
160 m_diffCoeff[2] = 3.0; // D11
161 m_diffCoeff[5] = 4.0; // D22
162 }
163
164 // Compute expected solution.
166 }
167
168 void RunTestCase(const std::string &method)
169 {
170 std::string execStr(GlobalConfiguration::ExecStr());
171 std::string implStr(GlobalConfiguration::ImplStr());
172
173 // reshape this->m_in
174 if (implStr == "SumFac" || execStr == "AVX")
175 {
176 this->m_in.ReshapeStorage(
177 NektarSpaces::GetVectorWidth<TData>(execStr), execStr);
179 NektarSpaces::GetVectorWidth<TData>(execStr));
180 }
181
183 this->m_expList, this->m_session->GetVariables());
184 auto precon = PreconOp<TData>::Create(
185 this->m_expList, this->m_session->GetVariables(), "Diagonal");
186 auto linsolve = LinearSolverOp<TData>::Create(
187 this->m_expList, this->m_session->GetVariables(), method);
188 linsolve->SetVerboseOutput(true);
189 op->SetDiffCoeff(m_diffCoeff);
190 op->SetLinearSolver(linsolve);
191 op->SetPrecon(precon);
192 op->UpdatePrecon();
193 op->Apply(this->m_in, this->m_out);
194 }
195
197 {
198 // Calculate expected result from Nektar++.
199 const unsigned int numComp =
200 this->m_in.GetNumComponents() * this->m_in.GetNumHomoModes();
201 const size_t nphys = this->m_expList->GetTotPoints();
202 const size_t ncoeffs = this->m_expList->GetNcoeffs();
203 const auto &variables = this->m_session->GetVariables();
204
205 // Setup input/output arrays
206 Array<OneD, TData> inphys = this->m_in.ToArray();
207 Array<OneD, TData> outcoeffs(numComp * ncoeffs, 0.0);
208
209 // Set lambda as constant coefficient
211 factors[StdRegions::eFactorLambda] = 0.0;
212
213 // Set up diffusion coefficient.
214 if (m_coordDim == 2)
215 {
219 }
220 else if (m_coordDim == 3)
221 {
228 }
229
230 // Solve each component separately
232 for (unsigned int n = 0; n < numComp; ++n)
233 {
235 this->m_session, graph, variables[n % variables.size()], true,
237 Array<OneD, TData> inphysVar = inphys + n * nphys;
238 Array<OneD, TData> outcoeffsVar = outcoeffs + n * ncoeffs;
239 expListVar->HelmSolve(inphysVar, outcoeffsVar, factors);
240 }
241
242 // Copy solution back
243 this->m_expected.template CopyArray<NektarSpaces::HostSpace>(outcoeffs);
244 }
245
246protected:
247 unsigned int m_coordDim;
248 std::vector<TData> m_diffCoeff;
249};
250
251// clang-format off
252#if defined(NEKTAR_ENABLE_SINGLE_PRECISION)
253#define TESTFLOAT(type, filename) \
254 class type##float : public TestPoissonSolve<float> \
255 { \
256 public: \
257 type##float() \
258 { \
259 m_meshName = filename; \
260 } \
261 };
262#else
263#define TESTFLOAT(type, filename)
264#endif
265#if defined(NEKTAR_ENABLE_DOUBLE_PRECISION)
266#define TESTDOUBLE(type, filename) \
267 class type : public TestPoissonSolve<double> \
268 { \
269 public: \
270 type() \
271 { \
272 m_meshName = filename; \
273 } \
274 };
275#else
276#define TESTDOUBLE(type, filename)
277#endif
278#define TEST(type, filename) \
279 TESTFLOAT(type, filename) \
280 TESTDOUBLE(type, filename)
281// clang-format on
282
283TEST(Poisson1D_Seg, "run/Helmholtz1D_P8.xml")
284
285TEST(Poisson2D_Tri_Quad, "run/Helmholtz2D_varP.xml")
286TEST(Poisson2D_Tri_Quad_3C, "run/Helmholtz2D_3C.xml")
287
288TEST(Poisson2D_AllBCs, "run/Helmholtz2D_P7_AllBCs.xml")
289
290TEST(Poisson3D_Hex, "run/Helmholtz3D_Hex_Heterogeneous.xml")
291TEST(Poisson3D_Hex_3C, "run/Helmholtz3D_Hex_3C.xml")
292
293TEST(Poisson3D_Prism, "run/Helmholtz3D_Prism_VarP.xml")
294
295TEST(Poisson3D_Pyr, "run/Helmholtz3D_Pyr_VarP.xml")
296
297TEST(Poisson3D_Tet, "run/Helmholtz3D_Tet_VarP.xml")
SpatialDomains::MeshGraphSharedPtr graph
#define TEST(type, filename)
SpatialDomains::Geometry * el
void ReshapeStorage(const unsigned int &interleaveWidth, const std::string &execSpace)
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
Nektar::Array< Nektar::OneD, TDataOut > ToArray()
Copy the data to a Nektar::Array, component by component and element by element, without padding,...
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
void SetInterleaveWidth(const unsigned int &interleaveWidth)
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
Field< TData, FieldState::Coeff > m_out
Field< TData, FieldState::Phys > m_in
ExpListSharedPtr m_expList
SessionReaderSharedPtr m_session
Field< TData, FieldState::Coeff > m_expected
void RunTestCase(const std::string &method)
std::vector< TData > m_diffCoeff
TestPoissonSolve()=default
std::map< ConstFactorType, NekDouble > ConstFactorMap