Nektar++
Loading...
Searching...
No Matches
TestHelmSolve.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestHelmSolve.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 HelmSolve 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/HelmSolve/HelmSolveOp.hpp>
42#include <SolverCore/PreconOps/PreconOp.hpp>
43
44using namespace Nektar::SolverCore;
45
46template <typename TData> class TestHelmSolve : public TestLinearSolver<TData>
47{
48public:
49 TestHelmSolve() = default;
50
52 {
53 // Set initial conditions.
54 const unsigned int numComp =
55 this->m_in.GetNumComponents() * this->m_in.GetNumHomoModes();
56 const size_t nphys = this->m_expList->GetTotPoints();
57 m_coordDim = this->m_expList->GetCoordim(0);
58
59 Array<OneD, TData> x(nphys, 0.0);
60 Array<OneD, TData> y(nphys, 0.0);
61 Array<OneD, TData> z(nphys, 0.0);
62 Array<OneD, TData> fce(numComp * nphys, 0.0);
63 this->m_expList->GetCoords(x, y, z);
64
65 if (this->m_session->DefinesFunction("Forcing"))
66 {
67 for (unsigned int n = 0; n < numComp; ++n)
68 {
69 auto func = this->m_session->GetFunction(
70 "Forcing", n % this->m_session->GetVariables().size());
71 Array<OneD, TData> fceVar = fce + n * nphys;
72 func->Evaluate(x, y, z, fceVar);
73 }
74 }
75
76 for (unsigned int blk = 0; blk < this->m_in.GetBlocks().size(); ++blk)
77 {
78 auto &block = this->m_in.GetBlocks()[blk];
79 auto inptr =
80 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
81 for (unsigned int n = 0; n < numComp; ++n)
82 {
83 auto xptr = x.data();
84 auto yptr = y.data();
85 auto zptr = z.data();
86 auto fceptr = fce.data() + n * nphys;
87
88 for (size_t el = 0, cnt = 0; el < block.GetNumElements(); ++el)
89 {
90 for (unsigned int phys = 0; phys < block.GetNumData();
91 ++phys, ++cnt)
92 {
93 if (this->m_session->DefinesFunction("Forcing"))
94 {
95 inptr[cnt] = *(fceptr++);
96 }
97 else
98 {
99 inptr[cnt] = 1.0 + n;
100 if (m_coordDim == 1)
101 {
102 for (unsigned int p = 1; p < 4; ++p)
103 {
104 inptr[cnt] +=
105 (n + 1) * p * std::pow(*xptr, p);
106 }
107 xptr++;
108 }
109 else if (m_coordDim == 2)
110 {
111 for (unsigned int p = 1; p < 4; ++p)
112 {
113 inptr[cnt] += (n + 1) * p *
114 std::pow(*xptr, p) *
115 std::pow(*yptr, p);
116 }
117 xptr++;
118 yptr++;
119 }
120 else
121 {
122 for (unsigned int p = 1; p < 4; ++p)
123 {
124 inptr[cnt] +=
125 (n + 1) * p * std::pow(*xptr, p) *
126 std::pow(*yptr, p) * std::pow(*zptr, p);
127 }
128 xptr++;
129 yptr++;
130 zptr++;
131 }
132 }
133 }
134 }
135
136 inptr += block.CompSize();
137 }
138 }
139
140 this->m_out.template Initialize<NektarSpaces::HostSpace>(0.0);
141
142 // Get lambda from this->m_session or default to 10.0
143 m_lambda = this->m_session->DefinesParameter("Lambda")
144 ? this->m_session->GetParameter("Lambda")
145 : 10.0;
146
147 // Set up diffusion coefficient.
148 const auto nDiffCoeff = m_coordDim * (m_coordDim + 1) / 2;
149 m_diffCoeff.resize(nDiffCoeff);
150
151 // Set up (isotropic) diffusion coefficient.
152 if (m_coordDim == 1)
153 {
154 m_diffCoeff[0] = 1.0; // D00
155 }
156 else if (m_coordDim == 2)
157 {
158 m_diffCoeff[0] = 2.0; // D00
159 m_diffCoeff[2] = 3.0; // D11
160 }
161 else
162 {
163 m_diffCoeff[0] = 2.0; // D00
164 m_diffCoeff[2] = 3.0; // D11
165 m_diffCoeff[5] = 4.0; // D22
166 }
167
168 // Compute expected solution.
170 }
171
172 void RunTestCase(const std::string &method)
173 {
174 std::string execStr(GlobalConfiguration::ExecStr());
175 std::string implStr(GlobalConfiguration::ImplStr());
176
177 // reshape this->m_in
178 if (implStr == "SumFac" || execStr == "AVX")
179 {
180 this->m_in.ReshapeStorage(
181 NektarSpaces::GetVectorWidth<TData>(execStr), execStr);
183 NektarSpaces::GetVectorWidth<TData>(execStr));
184 }
185
186 m_op = HelmSolveOp<TData>::Create(this->m_expList,
187 this->m_session->GetVariables());
188 auto precon = PreconOp<TData>::Create(
189 this->m_expList, this->m_session->GetVariables(), "Diagonal");
191 this->m_expList, this->m_session->GetVariables(), method);
192 m_linsolve->SetVerboseOutput(true);
193 m_op->SetLambda(m_lambda);
194 m_op->SetDiffCoeff(m_diffCoeff);
195 m_op->SetLinearSolver(m_linsolve);
196 m_op->SetPrecon(precon);
197 m_op->UpdatePrecon();
198 m_op->Apply(this->m_in, this->m_out);
199 }
200
201 /**
202 * @brief Solve again with the current output as the initial guess.
203 */
205 {
206 m_op->Apply(this->m_in, this->m_out);
207 }
208
209 /**
210 * @brief Set a GLOBALSYSSOLNINFO property for the first variable only,
211 * which the linear solver applies to all variables solved together.
212 */
213 void SetGlobalSysSolnInfo(const std::string &property,
214 const std::string &value)
215 {
216 this->m_session->SetGlobalSysSolnInfo(
217 this->m_session->GetVariables()[0], property, value);
218 }
219
220 unsigned int GetNiterations()
221 {
222 return m_linsolve->GetNiterations();
223 }
224
226 {
227 // Calculate expected result from Nektar++.
228 const unsigned int numComp =
229 this->m_in.GetNumComponents() * this->m_in.GetNumHomoModes();
230 const size_t nphys = this->m_expList->GetTotPoints();
231 const size_t ncoeffs = this->m_expList->GetNcoeffs();
232 const auto &variables = this->m_session->GetVariables();
233
234 // Setup input/output arrays
235 Array<OneD, TData> inphys = this->m_in.ToArray();
236 Array<OneD, TData> outcoeffs(numComp * ncoeffs, 0.0);
237
238 // Set lambda as constant coefficient
241
242 // Set up diffusion coefficient.
243 if (m_coordDim == 2)
244 {
248 }
249 else if (m_coordDim == 3)
250 {
257 }
258
259 // Solve each component separately
261 for (unsigned int n = 0; n < numComp; ++n)
262 {
264 this->m_session, graph, variables[n % variables.size()], true,
266 Array<OneD, TData> inphysVar = inphys + n * nphys;
267 Array<OneD, TData> outcoeffsVar = outcoeffs + n * ncoeffs;
268 expListVar->HelmSolve(inphysVar, outcoeffsVar, factors);
269 }
270
271 // Copy solution back
272 this->m_expected.template CopyArray<NektarSpaces::HostSpace>(outcoeffs);
273 }
274
275protected:
276 unsigned int m_coordDim;
277 TData m_lambda;
278 std::vector<TData> m_diffCoeff;
279 std::shared_ptr<HelmSolveOp<TData>> m_op;
280 std::shared_ptr<LinearSolverOp<TData>> m_linsolve;
281};
282
283// clang-format off
284#if defined(NEKTAR_ENABLE_SINGLE_PRECISION)
285#define TESTFLOAT(type, filename) \
286 class type##float : public TestHelmSolve<float> \
287 { \
288 public: \
289 type##float() \
290 { \
291 m_meshName = filename; \
292 } \
293 };
294#else
295#define TESTFLOAT(type, filename)
296#endif
297#if defined(NEKTAR_ENABLE_DOUBLE_PRECISION)
298#define TESTDOUBLE(type, filename) \
299 class type : public TestHelmSolve<double> \
300 { \
301 public: \
302 type() \
303 { \
304 m_meshName = filename; \
305 } \
306 };
307#else
308#define TESTDOUBLE(type, filename)
309#endif
310#define TEST(type, filename) \
311 TESTFLOAT(type, filename) \
312 TESTDOUBLE(type, filename)
313// clang-format on
314
315TEST(Helmholtz1D_Seg, "run/Helmholtz1D_P8.xml")
316TEST(Helmholtz1D_Seg_3C, "run/Helmholtz1D_3C.xml")
317
318TEST(Helmholtz2D_Tri_Quad, "run/Helmholtz2D_varP.xml")
319TEST(Helmholtz2D_Tri_Quad_3C, "run/Helmholtz2D_3C.xml")
320
321TEST(Helmholtz2D_AllBCs, "run/Helmholtz2D_P7_AllBCs.xml")
322
323TEST(Helmholtz3D_Hex, "run/Helmholtz3D_Hex_Heterogeneous.xml")
324TEST(Helmholtz3D_Hex_3C, "run/Helmholtz3D_Hex_3C.xml")
325
326TEST(Helmholtz3D_Prism, "run/Helmholtz3D_Prism_VarP.xml")
327
328TEST(Helmholtz3D_Pyr, "run/Helmholtz3D_Pyr_VarP.xml")
329
330TEST(Helmholtz3D_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)
unsigned int m_coordDim
void RunTestCase(const std::string &method)
void RepeatSolve()
Solve again with the current output as the initial guess.
std::shared_ptr< LinearSolverOp< TData > > m_linsolve
unsigned int GetNiterations()
void SetGlobalSysSolnInfo(const std::string &property, const std::string &value)
Set a GLOBALSYSSOLNINFO property for the first variable only, which the linear solver applies to all ...
std::vector< TData > m_diffCoeff
TestHelmSolve()=default
std::shared_ptr< HelmSolveOp< TData > > m_op
Field< TData, FieldState::Coeff > m_out
Field< TData, FieldState::Phys > m_in
ExpListSharedPtr m_expList
SessionReaderSharedPtr m_session
Field< TData, FieldState::Coeff > m_expected
std::map< ConstFactorType, NekDouble > ConstFactorMap