Nektar++
Loading...
Searching...
No Matches
TestFwdTrans.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestFwdTrans.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 FwdTrans 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/FwdTrans/FwdTransOp.hpp>
42#include <SolverCore/PreconOps/PreconOp.hpp>
43
44using namespace Nektar::SolverCore;
45
46template <typename TData> class TestFwdTrans : public TestLinearSolver<TData>
47{
48public:
49 TestFwdTrans() = 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 // Compute expected solution.
144 }
145
146 void RunTestCase(const std::string &method)
147 {
148 std::string execStr(GlobalConfiguration::ExecStr());
149 std::string implStr(GlobalConfiguration::ImplStr());
150
151 // reshape this->m_in
152 if (implStr == "SumFac" || execStr == "AVX")
153 {
154 this->m_in.ReshapeStorage(
155 NektarSpaces::GetVectorWidth<TData>(execStr), execStr);
157 NektarSpaces::GetVectorWidth<TData>(execStr));
158 }
159
160 auto op = FwdTransOp<TData>::Create(this->m_expList,
161 this->m_session->GetVariables());
162 auto precon = PreconOp<TData>::Create(
163 this->m_expList, this->m_session->GetVariables(), "Diagonal");
164 auto linsolve = LinearSolverOp<TData>::Create(
165 this->m_expList, this->m_session->GetVariables(), method);
166 linsolve->SetVerboseOutput(true);
167 op->SetLinearSolver(linsolve);
168 op->SetPrecon(precon);
169 op->UpdatePrecon();
170 op->Apply(this->m_in, this->m_out);
171 }
172
174 {
175 // Calculate expected result from Nektar++.
176 const unsigned int numComp =
177 this->m_in.GetNumComponents() * this->m_in.GetNumHomoModes();
178 const size_t nphys = this->m_expList->GetTotPoints();
179 const size_t ncoeffs = this->m_expList->GetNcoeffs();
180 const auto &variables = this->m_session->GetVariables();
181
182 // Setup input/output arrays
183 Array<OneD, TData> inphys = this->m_in.ToArray();
184 Array<OneD, TData> outcoeffs(numComp * ncoeffs, 0.0);
185
186 // Solve each component separately
188 for (unsigned int n = 0; n < numComp; ++n)
189 {
191 this->m_session, graph, variables[n % variables.size()], true,
193 Array<OneD, TData> inphysVar = inphys + n * nphys;
194 Array<OneD, TData> outcoeffsVar = outcoeffs + n * ncoeffs;
195 expListVar->FwdTrans(inphysVar, outcoeffsVar);
196 }
197
198 // Copy solution back
199 this->m_expected.template CopyArray<NektarSpaces::HostSpace>(outcoeffs);
200 }
201
202protected:
203 unsigned int m_coordDim;
204};
205
206// clang-format off
207#if defined(NEKTAR_ENABLE_SINGLE_PRECISION)
208#define TESTFLOAT(type, filename) \
209 class type##float : public TestFwdTrans<float> \
210 { \
211 public: \
212 type##float() \
213 { \
214 m_meshName = filename; \
215 } \
216 };
217#else
218#define TESTFLOAT(type, filename)
219#endif
220#if defined(NEKTAR_ENABLE_DOUBLE_PRECISION)
221#define TESTDOUBLE(type, filename) \
222 class type : public TestFwdTrans<double> \
223 { \
224 public: \
225 type() \
226 { \
227 m_meshName = filename; \
228 } \
229 };
230#else
231#define TESTDOUBLE(type, filename)
232#endif
233#define TEST(type, filename) \
234 TESTFLOAT(type, filename) \
235 TESTDOUBLE(type, filename)
236// clang-format on
237
238TEST(Helmholtz1D_Seg, "run/Helmholtz1D_P8.xml")
239
240TEST(Helmholtz2D_Tri_Quad, "run/Helmholtz2D_varP.xml")
241TEST(Helmholtz2D_Tri_Quad_3C, "run/Helmholtz2D_3C.xml")
242
243TEST(Helmholtz2D_AllBCs, "run/Helmholtz2D_P7_AllBCs.xml")
244
245TEST(Helmholtz3D_Hex, "run/Helmholtz3D_Hex_Heterogeneous.xml")
246TEST(Helmholtz3D_Hex_3C, "run/Helmholtz3D_Hex_3C.xml")
247
248TEST(Helmholtz3D_Prism, "run/Helmholtz3D_Prism_VarP.xml")
249TEST(Helmholtz3D_Prism_3C, "run/Helmholtz3D_Prism_3C.xml")
250
251TEST(Helmholtz3D_Pyr, "run/Helmholtz3D_Pyr_VarP.xml")
252
253TEST(Helmholtz3D_Tet, "run/Helmholtz3D_Tet_VarP.xml")
254TEST(Helmholtz3D_Tet_3C, "run/Helmholtz3D_Tet_3C.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
TestFwdTrans()=default
void RunTestCase(const std::string &method)
void ExpectedSolution()
Field< TData, FieldState::Coeff > m_out
Field< TData, FieldState::Phys > m_in
ExpListSharedPtr m_expList
SessionReaderSharedPtr m_session
Field< TData, FieldState::Coeff > m_expected