Nektar++
Loading...
Searching...
No Matches
TestLinearADRSolve.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestLinearADRSolve.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 LinearADRSolve 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/LinearADRSolve/LinearADRSolveOp.hpp>
42#include <SolverCore/PreconOps/PreconOp.hpp>
43
44#include <memory>
45
46using namespace Nektar::SolverCore;
47
49{
50public:
53};
54
55template <typename TData>
57{
58public:
59 TestLinearADRSolve() = default;
60
61 // Custom configure method to avoid non-symmetric system warning in
62 // GlobalLinsSysIterative
63 void Configure(void)
64 {
65 this->SetSession();
66 // Must run before SetExpList(): AssemblyMap reads GlobalSysSoln
67 // when the ContField is constructed. The expansion lists built
68 // later for the expected solution rely on the session state set
69 // here too, so this is the only call needed.
71 this->SetExpList();
72 this->m_expList->SetDataWarehouse();
73 this->SetFixture();
74 }
75
77 {
78 // Set initial conditions.
79 const unsigned int numComp =
80 this->m_in.GetNumComponents() * this->m_in.GetNumHomoModes();
81 const size_t nphys = this->m_expList->GetTotPoints();
82 m_coordDim = this->m_expList->GetCoordim(0);
83
84 Array<OneD, TData> x(nphys, 0.0);
85 Array<OneD, TData> y(nphys, 0.0);
86 Array<OneD, TData> z(nphys, 0.0);
87 Array<OneD, TData> fce(numComp * nphys, 0.0);
88 this->m_expList->GetCoords(x, y, z);
89 this->m_in.template Initialize<NektarSpaces::HostSpace>(0.0);
90
91 if (this->m_session->DefinesFunction("Forcing"))
92 {
93 for (unsigned int n = 0; n < numComp; ++n)
94 {
95 auto func = this->m_session->GetFunction(
96 "Forcing", n % this->m_session->GetVariables().size());
97 Array<OneD, TData> fceVar = fce + n * nphys;
98 func->Evaluate(x, y, z, fceVar);
99 }
100 }
101
102 for (unsigned int blk = 0; blk < this->m_in.GetBlocks().size(); ++blk)
103 {
104 auto &block = this->m_in.GetBlocks()[blk];
105 auto inptr =
106 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
107 for (unsigned int n = 0; n < numComp; ++n)
108 {
109 auto xptr = x.data();
110 auto yptr = y.data();
111 auto zptr = z.data();
112 auto fceptr = fce.data() + n * nphys;
113
114 for (size_t el = 0, cnt = 0; el < block.GetNumElements(); ++el)
115 {
116 for (unsigned int phys = 0; phys < block.GetNumData();
117 ++phys, ++cnt)
118 {
119 if (this->m_session->DefinesFunction("Forcing"))
120 {
121 inptr[cnt] = *(fceptr++);
122 }
123 else
124 {
125 inptr[cnt] = 1.0 + n;
126 if (m_coordDim == 1)
127 {
128 for (unsigned int p = 1; p < 4; ++p)
129 {
130 inptr[cnt] +=
131 (n + 1) * p * std::pow(*xptr, p);
132 }
133 xptr++;
134 }
135 else if (m_coordDim == 2)
136 {
137 for (unsigned int p = 1; p < 4; ++p)
138 {
139 inptr[cnt] += (n + 1) * p *
140 std::pow(*xptr, p) *
141 std::pow(*yptr, p);
142 }
143 xptr++;
144 yptr++;
145 }
146 else
147 {
148 for (unsigned int p = 1; p < 4; ++p)
149 {
150 inptr[cnt] +=
151 (n + 1) * p * std::pow(*xptr, p) *
152 std::pow(*yptr, p) * std::pow(*zptr, p);
153 }
154 xptr++;
155 yptr++;
156 zptr++;
157 }
158 }
159 }
160 }
161
162 inptr += block.CompSize();
163 }
164 }
165
166 this->m_out.template Initialize<NektarSpaces::HostSpace>(0.0);
167
168 // Get lambda from this->m_session or default to 10.0
169 m_lambda = this->m_session->DefinesParameter("Lambda")
170 ? this->m_session->GetParameter("Lambda")
171 : 10.0;
172
173 // Set up diffusion coefficient.
174 const auto nDiffCoeff = m_coordDim * (m_coordDim + 1) / 2;
175 m_diffCoeff.resize(nDiffCoeff);
176
177 // Set up (isotropic) diffusion coefficient.
178 if (m_coordDim == 1)
179 {
180 m_diffCoeff[0] = 1.0; // D00
181 }
182 else if (m_coordDim == 2)
183 {
184 m_diffCoeff[0] = 1.0; // D00
185 m_diffCoeff[2] = 1.0; // D11
186 }
187 else
188 {
189 m_diffCoeff[0] = 1.0; // D00
190 m_diffCoeff[2] = 1.0; // D11
191 m_diffCoeff[5] = 1.0; // D22
192 }
193
194 // Set advection velocity
195 m_vel = Array<OneD, TData>(nphys * m_coordDim, 1.0);
197 for (unsigned int d = 1; d < m_coordDim; ++d)
198 {
199 Vmath::Fill(nphys, d + 1.0, tmp = m_vel + d * nphys, 1);
200 }
201
202 // Compute expected solution.
204 }
205
206 void RunTestCase(const std::string &method)
207 {
208 auto advelblockAttr = GetBlockAttributes<TData, FieldState::Phys>(
209 this->m_expList,
210 MultiRegions::UnitInterleaveWidths(this->m_expList));
211 auto vel = Field<TData, FieldState::Phys>("vel", advelblockAttr,
212 m_coordDim, 1);
213 vel.template CopyArray<NektarSpaces::HostSpace>(m_vel);
214
215 std::string execStr(GlobalConfiguration::ExecStr());
216 std::string implStr(GlobalConfiguration::ImplStr());
217
218 // reshape this->m_in
219 if (implStr == "SumFac" || execStr == "AVX")
220 {
221 this->m_in.ReshapeStorage(
222 NektarSpaces::GetVectorWidth<TData>(execStr), execStr);
224 NektarSpaces::GetVectorWidth<TData>(execStr));
225 }
226
227 auto op = LinearADRSolveOp<TData>::Create(
228 this->m_expList, this->m_session->GetVariables());
229 auto precon = PreconOp<TData>::Create(
230 this->m_expList, this->m_session->GetVariables(), m_preconName);
231 auto linsolve = LinearSolverOp<TData>::Create(
232 this->m_expList, this->m_session->GetVariables(), method);
233 linsolve->SetVerboseOutput(true);
234 op->SetLambda(m_lambda);
235 op->SetDiffCoeff(m_diffCoeff);
236 op->SetAdvVel(vel);
237 op->SetLinearSolver(linsolve);
238 op->SetPrecon(precon);
239 op->UpdatePrecon();
240 op->Apply(this->m_in, this->m_out);
241 }
242
244 {
245 // Calculate expected result from Nektar++.
246 const unsigned int numComp =
247 this->m_in.GetNumComponents() * this->m_in.GetNumHomoModes();
248 const size_t nphys = this->m_expList->GetTotPoints();
249 const size_t ncoeffs = this->m_expList->GetNcoeffs();
250 const auto &variables = this->m_session->GetVariables();
251
252 // Setup input/output arrays
253 Array<OneD, TData> inphys = this->m_in.ToArray();
254 Array<OneD, TData> outcoeffs(numComp * ncoeffs, 0.0);
255
256 // Set lambda as constant coefficient
259
260 // Set up diffusion coefficient.
261 if (m_coordDim == 2)
262 {
266 }
267 else if (m_coordDim == 3)
268 {
275 }
276
277 // Set advection velocities via varcoeffs
278 StdRegions::VarCoeffMap varcoeffs;
279 std::vector<StdRegions::VarCoeffType> velCoeffType = {
282 for (unsigned int d = 0; d < m_coordDim; ++d)
283 {
285 Array<OneD, TData> tmpVel(nphys);
286 Vmath::Vcopy(nphys, m_vel + d * nphys, 1, tmpVel, 1);
287 varcoeffs[velCoeffType[d]] = tmpVel;
288 }
289
291
292 // Solve each component separately.
293 for (unsigned int n = 0; n < numComp; ++n)
294 {
296 this->m_session, graph, variables[n % variables.size()], true,
298 Array<OneD, TData> inphysVar = inphys + n * nphys;
299 Array<OneD, TData> outcoeffsVar = outcoeffs + n * ncoeffs;
300 expListVar->LinearAdvectionDiffusionReactionSolve(
301 inphysVar, outcoeffsVar, factors, varcoeffs);
302 }
303
304 // Copy solution back
305 this->m_expected.template CopyArray<NektarSpaces::HostSpace>(outcoeffs);
306 }
307
309 {
312
313 if (m_coordDim == 2)
314 {
318 }
319 else if (m_coordDim == 3)
320 {
327 }
328
329 return factors;
330 }
331
333 {
334 const size_t nphys = this->m_expList->GetTotPoints();
335 StdRegions::VarCoeffMap varcoeffs;
336 std::vector<StdRegions::VarCoeffType> velCoeffType = {
339
340 for (unsigned int d = 0; d < m_coordDim; ++d)
341 {
342 Array<OneD, TData> tmpVel(nphys);
343 Vmath::Vcopy(nphys, vel + d * nphys, 1, tmpVel, 1);
344 varcoeffs[velCoeffType[d]] = tmpVel;
345 }
346
347 return varcoeffs;
348 }
349
350 std::shared_ptr<ContFieldMatrixAccessor> BuildExpectedExpList(
351 const std::string &variable)
352 {
354
356 this->m_session, graph, variable, true, false,
358 }
359
360 DNekMatSharedPtr BuildGlobalADRMatrix(const std::string &variable,
361 const Array<OneD, TData> &vel)
362 {
363 auto expListVar = BuildExpectedExpList(variable);
364 const size_t nphys = expListVar->GetTotPoints();
365 const size_t ncoeffs = expListVar->GetNcoeffs();
366 Array<OneD, TData> inphys(nphys, 0.0);
367 Array<OneD, TData> outcoeffs(ncoeffs, 0.0);
368
369 auto key = expListVar->LinearAdvectionDiffusionReactionSolve(
370 inphys, outcoeffs, BuildFactors(), BuildVarCoeffs(vel));
371
372 return expListVar->GenGlobalMatrixFull(
373 key, expListVar->GetLocalToGlobalMap());
374 }
375
376protected:
378 {
379 const auto &variables = this->m_session->GetVariables();
380 std::string iterSolver =
381 this->m_session->DefinesSolverInfo("LinSysIterSolver")
382 ? this->m_session->GetSolverInfo("LinSysIterSolver")
383 : "ConjugateGradient";
384 std::string gmresSolver =
385 iterSolver.find("Loc") != std::string::npos ? "GMRESLoc" : "GMRES";
386
387 if (this->m_session->DefinesSolverInfo("LinSysIterSolver"))
388 {
389 this->m_session->SetSolverInfo("LinSysIterSolver", gmresSolver);
390 }
391 for (const auto &var : variables)
392 {
393 this->m_session->SetGlobalSysSolnInfo(var, "GlobalSysSoln",
394 "IterativeFull");
395 this->m_session->SetGlobalSysSolnInfo(var, "LinSysIterSolver",
396 gmresSolver);
397 }
398 }
399
400 // Name of the preconditioner RunTestCase() creates.
401 std::string m_preconName = "Diagonal";
402 unsigned int m_coordDim;
403 TData m_lambda;
404 std::vector<TData> m_diffCoeff;
406};
407
408// clang-format off
409#if defined(NEKTAR_ENABLE_SINGLE_PRECISION)
410#define TESTFLOAT(type, filename) \
411 class type##float : public TestLinearADRSolve<float> \
412 { \
413 public: \
414 type##float() \
415 { \
416 m_meshName = filename; \
417 } \
418 };
419#else
420#define TESTFLOAT(type, filename)
421#endif
422#if defined(NEKTAR_ENABLE_DOUBLE_PRECISION)
423#define TESTDOUBLE(type, filename) \
424 class type : public TestLinearADRSolve<double> \
425 { \
426 public: \
427 type() \
428 { \
429 m_meshName = filename; \
430 } \
431 };
432#else
433#define TESTDOUBLE(type, filename)
434#endif
435#define TEST(type, filename) \
436 TESTFLOAT(type, filename) \
437 TESTDOUBLE(type, filename)
438// clang-format on
439
440TEST(Helmholtz1D_Seg, "run/Helmholtz1D_P8.xml")
441TEST(Helmholtz1D_Seg_3C, "run/Helmholtz1D_3C.xml")
442
443TEST(Helmholtz2D_Tri_Quad, "run/Helmholtz2D_varP.xml")
444TEST(Helmholtz2D_Tri_Quad_3C, "run/Helmholtz2D_3C.xml")
445
446TEST(Helmholtz2D_AllBCs, "run/Helmholtz2D_P7_AllBCs.xml")
447
448TEST(Helmholtz3D_Hex, "run/Helmholtz3D_Hex_Heterogeneous.xml")
449TEST(Helmholtz3D_Hex_3C, "run/Helmholtz3D_Hex_3C.xml")
450TEST(Helmholtz3D_Hex_3C_Single, "run/Helmholtz3D_Hex_3C_Single.xml")
451
452TEST(Helmholtz3D_Prism, "run/Helmholtz3D_Prism_VarP.xml")
453
454TEST(Helmholtz3D_Pyr, "run/Helmholtz3D_Pyr_VarP.xml")
455
456TEST(Helmholtz3D_Tet, "run/Helmholtz3D_Tet_VarP.xml")
SpatialDomains::MeshGraphSharedPtr graph
#define TEST(type, filename)
SpatialDomains::Geometry * el
A Field represents expansion data to be operated on.
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.
This class is the abstraction of a global continuous two- dimensional spectral/hp element expansion w...
Definition ContField.h:60
ContField()
The default constructor.
Definition ContField.cpp:87
std::shared_ptr< DNekMat > GenGlobalMatrixFull(const GlobalLinSysKey &mkey, const std::shared_ptr< AssemblyMapCG > &locToGloMap)
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
StdRegions::ConstFactorMap BuildFactors() const
std::vector< TData > m_diffCoeff
std::shared_ptr< ContFieldMatrixAccessor > BuildExpectedExpList(const std::string &variable)
Array< OneD, TData > m_vel
TestLinearADRSolve()=default
StdRegions::VarCoeffMap BuildVarCoeffs(const Array< OneD, TData > &vel) const
DNekMatSharedPtr BuildGlobalADRMatrix(const std::string &variable, const Array< OneD, TData > &vel)
void RunTestCase(const std::string &method)
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
std::map< StdRegions::VarCoeffType, VarCoeffEntry > VarCoeffMap
std::shared_ptr< DNekMat > DNekMatSharedPtr
void Fill(int n, const T alpha, T *x, const int incx)
Fill a vector with a constant value.
Definition Vmath.hpp:56
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition Vmath.hpp:828