Nektar++
Loading...
Searching...
No Matches
TestFwdTransBC.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestFwdTransBC.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:
32//
33///////////////////////////////////////////////////////////////////////////////
34
36
38
40
41#include <cmath>
42#include <cstdlib>
43
44using namespace Nektar;
45using namespace Nektar::LibUtilities;
46using namespace Nektar::MultiRegions;
47
48template <typename TData>
49class TestFwdTransBC : public TestOp<TData, FieldState::Phys, FieldState::Coeff>
50{
51public:
52 TestFwdTransBC() = default;
53
54 void SetFixture(const unsigned int nhomo) override
55 {
56 const auto &components = this->session->GetVariables();
57 auto inblockAttr = GetBoundaryBlockAttributes<FieldState::Phys>();
58 auto outblockAttr = GetBoundaryBlockAttributes<FieldState::Coeff>();
59
60 auto f_in = Field<TData, FieldState::Phys>("f_in", inblockAttr,
61 components, nhomo);
62 auto f_out = Field<TData, FieldState::Coeff>("f_out", outblockAttr,
63 components, nhomo);
64 auto f_expected = Field<TData, FieldState::Coeff>(
65 "f_expected", outblockAttr, components, nhomo);
66 this->fixt_in = new Field<TData, FieldState::Phys>(std::move(f_in));
67 this->fixt_out = new Field<TData, FieldState::Coeff>(std::move(f_out));
68 this->fixt_expected =
69 new Field<TData, FieldState::Coeff>(std::move(f_expected));
70 }
71
73 {
74 this->fixt_in->template CopyVector<NektarSpaces::HostSpace>(
76
77 this->fixt_out->template Initialize<NektarSpaces::HostSpace>(0.0);
78
79 // Compute expected solution.
81 }
82
84 {
85 const auto &components = this->session->GetVariables();
86 const auto nComp = components.size();
87 const auto boundaryExp = GetBoundaryConditionExpansions();
88 const auto totalPhys = GetTotalBoundaryPoints(boundaryExp);
89 const auto totalCoeffs = GetTotalBoundaryCoeffs(boundaryExp);
90 auto inPhys = this->fixt_in->template ToArray<TData>();
91 std::vector<TData> result(totalCoeffs * nComp, 0.0);
92 size_t physOffset = 0;
93 size_t coeffOffset = 0;
94
95 for (const auto &expList : boundaryExp)
96 {
97 expList->SetDataWarehouse();
98
99 auto physBlocks = GetBlockAttributes<TData, FieldState::Phys>(
100 expList, MultiRegions::UnitInterleaveWidths(expList));
101 auto coeffBlocks = GetBlockAttributes<TData, FieldState::Coeff>(
102 expList, MultiRegions::UnitInterleaveWidths(expList));
103 Field<TData, FieldState::Phys> in("boundary phys", physBlocks,
104 nComp, 1);
105 Field<TData, FieldState::Coeff> out("boundary coeff", coeffBlocks,
106 nComp, 1);
107
108 const auto nphys = expList->GetTotPoints();
109 const auto ncoeffs = expList->GetNcoeffs();
110 Array<OneD, TData> localIn(nComp * nphys, 0.0);
111
112 for (unsigned int i = 0; i < nComp; ++i)
113 {
114 std::copy(inPhys.data() + i * totalPhys + physOffset,
115 inPhys.data() + i * totalPhys + physOffset + nphys,
116 localIn.data() + i * nphys);
117 }
118
119 in.template CopyArray<NektarSpaces::HostSpace>(localIn);
120 out.template Initialize<NektarSpaces::HostSpace>(0.0);
121
122 auto op = FwdTransBCOp<TData>::Create(expList, components);
123 op->Apply(in, out);
124
125 const auto outVec = out.template ToVector<TData>();
126 for (unsigned int i = 0; i < nComp; ++i)
127 {
128 std::copy(outVec.begin() + i * ncoeffs,
129 outVec.begin() + (i + 1) * ncoeffs,
130 result.begin() + i * totalCoeffs + coeffOffset);
131 }
132
133 physOffset += nphys;
134 coeffOffset += ncoeffs;
135 }
136
137 this->fixt_out->template CopyVector<NektarSpaces::HostSpace>(result);
138 }
139
141 {
142 const auto &components = this->session->GetVariables();
143 const auto nComp = components.size();
144 const auto boundaryExp = GetBoundaryConditionExpansions();
145 const auto totalPhys = GetTotalBoundaryPoints(boundaryExp);
146 const auto totalCoeffs = GetTotalBoundaryCoeffs(boundaryExp);
147 auto inPhys = this->fixt_in->template ToArray<double>();
148 Array<OneD, double> result(totalCoeffs * nComp, 0.0);
149 size_t physOffset = 0;
150 size_t coeffOffset = 0;
151
152 for (const auto &expList : boundaryExp)
153 {
154 const auto nphys = expList->GetTotPoints();
155 const auto ncoeffs = expList->GetNcoeffs();
157
158 for (unsigned int i = 0; i < nComp; ++i)
159 {
160 if (expList->GetExpType() == MultiRegions::e0D)
161 {
162 std::copy(inPhys.data() + i * totalPhys + physOffset,
163 inPhys.data() + i * totalPhys + physOffset +
164 ncoeffs,
165 result.data() + i * totalCoeffs + coeffOffset);
166 }
167 else
168 {
169 expList->FwdTransBndConstrained(
170 inPhys + i * totalPhys + physOffset,
171 tmp = result + i * totalCoeffs + coeffOffset);
172 }
173 }
174
175 physOffset += nphys;
176 coeffOffset += ncoeffs;
177 }
178
179 this->fixt_expected->template CopyArray<NektarSpaces::HostSpace>(
180 result);
181 }
182
183private:
184 std::shared_ptr<MultiRegions::ContField> CreateBoundaryField(
185 const std::string &variable) const
186 {
188 auto bcfield =
190 this->session, graph, variable, true, false,
192 bcfield->EvaluateBoundaryConditions(0.0);
193
194 return bcfield;
195 }
196
197 std::vector<MultiRegions::ExpListSharedPtr> GetBoundaryConditionExpansions()
198 const
199 {
200 auto bcfield = CreateBoundaryField(this->session->GetVariables()[0]);
201 const auto &bndExp = bcfield->GetBndCondExpansions();
202
203 std::vector<MultiRegions::ExpListSharedPtr> result;
204 result.reserve(bndExp.size());
205 for (size_t i = 0; i < bndExp.size(); ++i)
206 {
207 result.push_back(bndExp[i]);
208 }
209
210 return result;
211 }
212
213 std::vector<TData> GetBoundaryPhysValues() const
214 {
215 const auto &components = this->session->GetVariables();
216 const auto nComp = components.size();
217 const auto boundaryExp = GetBoundaryConditionExpansions();
218 const auto totalPhys = GetTotalBoundaryPoints(boundaryExp);
219 const auto expType = this->fixt_explist->GetExpType();
220 std::vector<TData> result(totalPhys * nComp, 0.0);
221
222 for (unsigned int i = 0; i < nComp; ++i)
223 {
224 auto bcfield = CreateBoundaryField(components[i]);
225 const auto &bndExp = bcfield->GetBndCondExpansions();
226 size_t physOffset = 0;
227
228 for (size_t j = 0; j < bndExp.size(); ++j)
229 {
230 const auto &expList = bndExp[j];
231 const auto nphys = expList->GetTotPoints();
232
233 if (expType == MultiRegions::e1D)
234 {
235 const auto &coeffs = expList->GetCoeffs();
236 std::copy(coeffs.begin(), coeffs.begin() + nphys,
237 result.begin() + i * totalPhys + physOffset);
238 }
239 else
240 {
241 const auto &phys = expList->GetPhys();
242 std::copy(phys.begin(), phys.begin() + nphys,
243 result.begin() + i * totalPhys + physOffset);
244 }
245
246 physOffset += nphys;
247 }
248 }
249
250 return result;
251 }
252
254 const std::vector<MultiRegions::ExpListSharedPtr> &boundaryExp) const
255 {
256 size_t totalPhys = 0;
257 for (const auto &expList : boundaryExp)
258 {
259 totalPhys += expList->GetTotPoints();
260 }
261
262 return totalPhys;
263 }
264
266 const std::vector<MultiRegions::ExpListSharedPtr> &boundaryExp) const
267 {
268 size_t totalCoeffs = 0;
269 for (const auto &expList : boundaryExp)
270 {
271 totalCoeffs += expList->GetNcoeffs();
272 }
273
274 return totalCoeffs;
275 }
276
277 template <FieldState TState>
278 std::vector<BlockAttributes<TState>> GetBoundaryBlockAttributes() const
279 {
280 const auto boundaryExp = GetBoundaryConditionExpansions();
281 std::vector<BlockAttributes<TState>> blockAttr;
282 size_t nBlocks = 0;
283 for (const auto &expList : boundaryExp)
284 {
285 nBlocks += GetBlockAttributes<TData, TState>(
286 expList, MultiRegions::UnitInterleaveWidths(expList))
287 .size();
288 }
289
290 blockAttr.reserve(nBlocks);
291 for (const auto &expList : boundaryExp)
292 {
293 auto bcBlockAttr = GetBlockAttributes<TData, TState>(
294 expList, MultiRegions::UnitInterleaveWidths(expList));
295 for (const auto &attr : bcBlockAttr)
296 {
297 blockAttr.emplace_back(attr);
298 }
299 }
300
301 return blockAttr;
302 }
303};
304
305// clang-format off
306#if defined(NEKTAR_ENABLE_SINGLE_PRECISION)
307#define TESTFLOAT(type, filename) \
308 class type##float : public TestFwdTransBC<float> \
309 { \
310 public: \
311 type##float() \
312 { \
313 meshName = filename; \
314 } \
315 };
316#else
317#define TESTFLOAT(type, filename)
318#endif
319#if defined(NEKTAR_ENABLE_DOUBLE_PRECISION)
320#define TESTDOUBLE(type, filename) \
321 class type : public TestFwdTransBC<double> \
322 { \
323 public: \
324 type() \
325 { \
326 meshName = filename; \
327 } \
328 };
329#else
330#define TESTDOUBLE(type, filename)
331#endif
332#define TEST(type, filename) \
333 TESTFLOAT(type, filename) \
334 TESTDOUBLE(type, filename)
335// clang-format on
336
337TEST(Helmholtz1D_Seg, "run/Helmholtz1D_P8.xml")
338TEST(Helmholtz1D_Seg_3C, "run/Helmholtz1D_3C_mixedBC.xml")
339TEST(Helmholtz2D_Tri, "run/Helmholtz2D_Tri.xml")
340TEST(Helmholtz2D_Quad, "run/Helmholtz2D_Quad.xml")
341TEST(Helmholtz2D_Tri_Quad, "run/Helmholtz2D_varP.xml")
342TEST(Helmholtz2D_Tri_Quad_3C, "run/Helmholtz2D_3C.xml")
343TEST(Helmholtz2D_AllBCs, "run/Helmholtz2D_P7_AllBCs.xml")
344TEST(Helmholtz3D_Hex, "run/Helmholtz3D_Hex_Heterogeneous.xml")
345TEST(Helmholtz3D_Hex_3C, "run/Helmholtz3D_Hex_3C.xml")
346TEST(Helmholtz3D_Prism, "run/Helmholtz3D_Prism_VarP.xml")
347TEST(Helmholtz3D_Pyr, "run/Helmholtz3D_Pyr_VarP.xml")
348TEST(Helmholtz3D_Tet, "run/Helmholtz3D_Tet_VarP.xml")
SpatialDomains::MeshGraphSharedPtr graph
#define TEST(type, filename)
A Field represents expansion data to be operated on.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static std::shared_ptr< FwdTransBCOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="")
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
void SetFixture(const unsigned int nhomo) override
size_t GetTotalBoundaryCoeffs(const std::vector< MultiRegions::ExpListSharedPtr > &boundaryExp) const
std::vector< MultiRegions::ExpListSharedPtr > GetBoundaryConditionExpansions() const
size_t GetTotalBoundaryPoints(const std::vector< MultiRegions::ExpListSharedPtr > &boundaryExp) const
TestFwdTransBC()=default
std::shared_ptr< MultiRegions::ContField > CreateBoundaryField(const std::string &variable) const
std::vector< TData > GetBoundaryPhysValues() const
std::vector< BlockAttributes< TState > > GetBoundaryBlockAttributes() const
std::shared_ptr< MultiRegions::ExpList > fixt_explist
Definition TestOp.hpp:439
LibUtilities::SessionReaderSharedPtr session
Definition TestOp.hpp:440