Nektar++
Loading...
Searching...
No Matches
TestOp.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestOp.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
35#pragma once
36
47
51
54
55#include <string>
56#include <type_traits>
57#include <vector>
58
59#if defined(_MSC_VER)
60#undef max
61#undef min
62#endif
63
64using namespace Nektar;
65using namespace Nektar::LibUtilities;
66using namespace Nektar::MultiRegions;
67
69
70/**
71 * @class TestOp
72 *
73 * A test fixture responsible for making available input and output
74 * Field objects. The structure constructor (destructor) is called
75 * before (after) each call to BOOST_FIXTURE_TEST_CASE(<test name>,
76 * TestOp) macro.
77 *
78 * See "Single test case fixture" on the Boost.Test documentation for more
79 * details:
80 * https://www.boost.org/doc/libs/1_82_0/libs/test/doc/html/boost_test/tests_organization/fixtures/case.html
81 */
82
83template <typename TData, FieldState stateIn = FieldState::Coeff,
84 FieldState stateOut = FieldState::Phys,
85 typename TExpList = MultiRegions::ExpList>
86class TestOp
87{
88public:
89 TestOp() = default;
90
91 virtual ~TestOp()
92 {
93 BOOST_TEST_MESSAGE("teardown fixture");
94
95 if (this->fixt_in)
96 {
97 delete this->fixt_in;
98 }
99 if (this->fixt_out)
100 {
101 delete this->fixt_out;
102 }
103 if (this->fixt_out_2)
104 {
105 delete this->fixt_out_2;
106 }
107 if (this->fixt_expected)
108 {
109 delete this->fixt_expected;
110 }
111 }
112
113 void Configure(void)
114 {
115 this->SetSession();
116 this->SetExpList();
117 this->fixt_explist->SetDataWarehouse();
118 this->SetFixture(1);
119 }
120
121 void Configure3DH1(const unsigned int nhomo = 1)
122 {
123 this->SetSession();
124 this->SetExpList3DH1(nhomo);
125 this->fixt_explist->SetDataWarehouse();
126 this->SetFixture(nhomo);
127 }
128
129 void Configure3DH2(const unsigned int nhomoY = 1,
130 const unsigned int nhomoZ = 1)
131 {
132 this->SetSession();
133 this->SetExpList3DH2(nhomoY, nhomoZ);
134 this->fixt_explist->SetDataWarehouse();
135 this->SetFixture(nhomoY * nhomoZ);
136 }
137
138 void SetSession(void)
139 {
140 // Captured once before MPI_Init - see GlobalConfiguration.
141 std::string execStr(GlobalConfiguration::ExecStr());
142 std::string implStr(GlobalConfiguration::ImplStr());
143
144 BOOST_TEST_MESSAGE("Creating input and output fields");
145
146 // Construct a fake command-line argument array to be fed to
147 // Session::Reader::CreateInstance. The first element stands for
148 // the name of the executable which, in our case, doesn't matter.
149 int argc = 4;
150 char **argv = new char *[argc];
151 argv[0] = strdup("exe_name");
152 argv[1] = strdup(meshName.data());
153 argv[2] = strdup(("--opExecSpace=" + execStr).c_str());
154 argv[3] = strdup(("--opImpl=" + implStr).c_str());
155
157
158 for (int i = 0; i < argc; ++i)
159 {
160 free(argv[i]);
161 }
162 delete[] argv;
163 }
164
165 void SetExpList(void)
166 {
168 if constexpr (std::is_same_v<TExpList, MultiRegions::ContField>)
169 {
170 this->fixt_explist =
172 this->session, graph, "u", true, false,
174 }
175 else if constexpr (std::is_same_v<TExpList, MultiRegions::DisContField>)
176 {
177 const auto &vars = this->session->GetVariables();
178 const std::string var = vars.empty() ? "DefaultVar" : vars[0];
179
180 this->fixt_explist =
182 this->session, graph, var, true, true,
184 }
185 else if constexpr (std::is_same_v<TExpList, MultiRegions::ExpList>)
186 {
187 this->fixt_explist =
189 this->session, graph, true, "u",
191 }
192 }
193
194 void SetExpList3DH1(const unsigned int nhomo = 1)
195 {
197 if constexpr (std::is_same_v<TExpList, MultiRegions::ContField>)
198 {
202 Nektar::LibUtilities::eFourier, nhomo, pkey);
203 this->fixt_explist =
205 AllocateSharedPtr(this->session, bkey, 1.0, false, false,
206 graph, "u", false,
208 }
209 else if constexpr (std::is_same_v<TExpList, MultiRegions::DisContField>)
210 {
214 Nektar::LibUtilities::eFourier, nhomo, pkey);
215 this->fixt_explist =
217 AllocateSharedPtr(this->session, bkey, 1.0, false, false,
219 }
220 else if constexpr (std::is_same_v<TExpList, MultiRegions::ExpList>)
221 {
225 Nektar::LibUtilities::eFourier, nhomo, pkey);
226 this->fixt_explist =
228 AllocateSharedPtr(this->session, bkey, 1.0, false, false,
230 }
231 this->fixt_explist->SetWaveSpace(true);
232 }
233
234 void SetExpList3DH2(const unsigned int nhomoY = 1,
235 const unsigned int nhomoZ = 1)
236 {
238 if constexpr (std::is_same_v<TExpList, MultiRegions::ContField>)
239 {
243 Nektar::LibUtilities::eFourier, nhomoY, pkey1);
247 Nektar::LibUtilities::eFourier, nhomoZ, pkey2);
248 this->fixt_explist =
250 AllocateSharedPtr(this->session, bkey1, bkey2, 1.0, 1.0,
251 false, false, graph, "u", false,
253 }
254 else if constexpr (std::is_same_v<TExpList, MultiRegions::DisContField>)
255 {
259 Nektar::LibUtilities::eFourier, nhomoY, pkey1);
263 Nektar::LibUtilities::eFourier, nhomoZ, pkey2);
264 this->fixt_explist =
266 AllocateSharedPtr(this->session, bkey1, bkey2, 1.0, 1.0,
267 false, false, graph, "u",
269 }
270 else if constexpr (std::is_same_v<TExpList, MultiRegions::ExpList>)
271 {
275 Nektar::LibUtilities::eFourier, nhomoY, pkey1);
279 Nektar::LibUtilities::eFourier, nhomoZ, pkey2);
280 this->fixt_explist =
282 AllocateSharedPtr(this->session, bkey1, bkey2, 1.0, 1.0,
283 false, false, graph,
285 }
286 this->fixt_explist->SetWaveSpace(true);
287 }
288
289 virtual void SetFixture(const unsigned int nhomo)
290 {
291 auto nin = this->session->GetVariables().size();
292 auto nout = this->session->GetVariables().size();
293 auto inblockAttr = GetBlockAttributes<TData, stateIn>(
294 this->fixt_explist,
295 MultiRegions::UnitInterleaveWidths(this->fixt_explist));
296 auto outblockAttr = GetBlockAttributes<TData, stateOut>(
297 this->fixt_explist,
298 MultiRegions::UnitInterleaveWidths(this->fixt_explist));
299
300 auto f_in = Field<TData, stateIn>("f_in", inblockAttr, nin, nhomo);
301 auto f_out = Field<TData, stateOut>("f_out", outblockAttr, nout, nhomo);
302 auto f_expected =
303 Field<TData, stateOut>("f_expected", outblockAttr, nout, nhomo);
304 this->fixt_in = new Field<TData, stateIn>(std::move(f_in));
305 this->fixt_out = new Field<TData, stateOut>(std::move(f_out));
306 this->fixt_expected = new Field<TData, stateOut>(std::move(f_expected));
307 }
308
309 /**
310 * @brief Put the blocks of @p field at alternating interleave widths,
311 * the execution space's vector width on even blocks and 1 on odd ones,
312 * reshaping the data in place: a field whose blocks differ in width, for
313 * an operator that must read any mix, such as the trace flux, which
314 * addresses each trace at the width of the element block it lies in. On
315 * Serial every width is 1 and nothing changes.
316 */
317 template <typename TField> static void MixInterleaveWidths(TField &field)
318 {
319 const std::string execStr(GlobalConfiguration::ExecStr());
320 const unsigned vecWidth = NektarSpaces::GetVectorWidth<TData>(execStr);
321 for (unsigned blk = 0; blk < field.GetBlocks().size(); ++blk)
322 {
323 field.GetBlocks()[blk].ReshapeStorage(blk % 2 ? 1u : vecWidth,
324 execStr, blk + 1);
325 }
326 }
327
328 /**
329 * @brief Compare this field to another field, with absolute
330 * tolerance tol. The two must have the same blocks and components; their
331 * interleave widths may differ.
332 *
333 * @return bool
334 */
335 virtual bool Compare(TData tol)
336 {
337 auto rank = this->session->GetComm()->GetRank();
338
339 if (this->fixt_expected->GetNumComponents() !=
340 this->fixt_out->GetNumComponents())
341 {
342 std::cout << "Mismatch of number of components." << std::endl;
343 return false;
344 }
345
346 if (this->fixt_expected->GetNumHomoModes() !=
347 this->fixt_out->GetNumHomoModes())
348 {
349 std::cout << "Mismatch of number of homogeneous modes."
350 << std::endl;
351 return false;
352 }
353
354 if (this->fixt_expected->GetBlocks().size() !=
355 this->fixt_out->GetBlocks().size())
356 {
357 std::cout << "Mismatch of block size." << std::endl;
358 return false;
359 }
360
361 // ToVector() returns every component element by element, whatever
362 // the interleave width of each block, and leaves the fields as they
363 // are.
364 const std::vector<TData> out =
365 this->fixt_out->template ToVector<TData>();
366 const std::vector<TData> expected =
367 this->fixt_expected->template ToVector<TData>();
368 const unsigned int nCompHomo = this->fixt_out->GetNumComponents() *
369 this->fixt_out->GetNumHomoModes();
370 const size_t compSize = out.size() / nCompHomo;
371
372 bool isMatch = true;
373
374 if (rank == 0)
375 {
376 printf("#elm #pts output expected "
377 "difference\n");
378 }
379 size_t blkOffset = 0;
380 for (unsigned int blk = 0; blk < this->fixt_out->GetBlocks().size();
381 ++blk)
382 {
383 const auto &outBlock = this->fixt_out->GetBlocks()[blk];
384 const auto &expBlock = this->fixt_expected->GetBlocks()[blk];
385 if ((outBlock.GetNumElements() != expBlock.GetNumElements()) ||
386 (outBlock.GetNumData() != expBlock.GetNumData()))
387 {
388 std::cout << "Mismatch of block structure." << std::endl;
389 return false;
390 }
391 const size_t nElmts = outBlock.GetNumElements();
392 const unsigned int nPts = outBlock.GetNumData();
393
394 for (unsigned int n = 0; n < nCompHomo; ++n)
395 {
396 const TData *outptr = out.data() + n * compSize + blkOffset;
397 const TData *expptr =
398 expected.data() + n * compSize + blkOffset;
399 size_t MisMatchcnt = 0, total = 0;
400
401 for (size_t el = 0, cnt = 0; el < nElmts; ++el)
402 {
403 for (unsigned int pts = 0; pts < nPts; ++pts, ++cnt)
404 {
405 if (std::isnan(outptr[cnt]) ||
406 std::isinf(outptr[cnt]) ||
407 std::abs(outptr[cnt] - expptr[cnt]) > tol)
408 {
409 printf("%04lu %04u %20.16f %20.16f %20.16f\n", el,
410 pts, outptr[cnt], expptr[cnt],
411 std::abs(outptr[cnt] - expptr[cnt]));
412 MisMatchcnt++;
413 }
414 total++;
415 }
416 }
417
418 if (MisMatchcnt)
419 {
420 std::cout << "Number of mismatches in component " << n
421 << " on block " << blk << " is " << MisMatchcnt
422 << " out of " << total << " on rank: " << rank
423 << std::endl;
424 isMatch = false;
425 }
426 }
427 blkOffset += nElmts * nPts;
428 }
429
430 return isMatch;
431 }
432
433protected:
434 std::string meshName = "";
439 std::shared_ptr<MultiRegions::ExpList> fixt_explist;
441};
442
SpatialDomains::MeshGraphSharedPtr graph
#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args)
SpatialDomains::Geometry * el
Describes the specification for a Basis.
Definition Basis.h:45
A Field represents expansion data to be operated on.
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
Defines a specification for a set of points.
Definition Points.h:50
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
Base class for all multi-elemental spectral/hp expansions.
Definition ExpList.h:160
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
std::string meshName
Definition TestOp.hpp:434
virtual bool Compare(TData tol)
Compare this field to another field, with absolute tolerance tol. The two must have the same blocks a...
Definition TestOp.hpp:335
void SetSession(void)
Definition TestOp.hpp:138
void SetExpList3DH1(const unsigned int nhomo=1)
Definition TestOp.hpp:194
std::shared_ptr< MultiRegions::ExpList > fixt_explist
Definition TestOp.hpp:439
TestOp()=default
Field< TData, stateOut > * fixt_out
Definition TestOp.hpp:436
Field< TData, stateIn > * fixt_out_2
Definition TestOp.hpp:437
Field< TData, stateIn > * fixt_in
Definition TestOp.hpp:435
void Configure3DH1(const unsigned int nhomo=1)
Definition TestOp.hpp:121
void SetExpList3DH2(const unsigned int nhomoY=1, const unsigned int nhomoZ=1)
Definition TestOp.hpp:234
void Configure3DH2(const unsigned int nhomoY=1, const unsigned int nhomoZ=1)
Definition TestOp.hpp:129
void SetExpList(void)
Definition TestOp.hpp:165
virtual ~TestOp()
Definition TestOp.hpp:91
virtual void SetFixture(const unsigned int nhomo)
Definition TestOp.hpp:289
Field< TData, stateOut > * fixt_expected
Definition TestOp.hpp:438
void Configure(void)
Definition TestOp.hpp:113
LibUtilities::SessionReaderSharedPtr session
Definition TestOp.hpp:440
static void MixInterleaveWidths(TField &field)
Put the blocks of field at alternating interleave widths, the execution space's vector width on even ...
Definition TestOp.hpp:317
std::shared_ptr< SessionReader > SessionReaderSharedPtr
@ eFourierEvenlySpaced
1D Evenly-spaced points using Fourier Fit
Definition PointsType.h:74
@ eFourier
Fourier Expansion .
Definition BasisType.h:55
@ ExecAndImpl
exe – ExecName ImplName
FieldState
Possible states for Field data.