Nektar++
Loading...
Searching...
No Matches
TestPhysTraceExtractBndCond.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestPhysTraceExtractBndCond.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 tests of detail::PhysTraceExtractBndCond: a
32// linear polynomial, exact at every order, is extracted from the volume onto
33// a boundary region and compared with its values at the region's points.
34//
35///////////////////////////////////////////////////////////////////////////////
36
37#pragma once
38
40
42
44
45#include <algorithm>
46
47using namespace Nektar::MultiRegions;
48using namespace Nektar::LibUtilities;
49using namespace Nektar;
50
51// A field exactly representable at any polynomial order >= 1: u = x + 2y + 3z.
52template <typename TData>
53static inline TData PhysTraceExtractBndCondTestFn(TData x, TData y, TData z)
54{
55 return x + TData(2.0) * y + TData(3.0) * z;
56}
57
58template <typename TData>
60 : public TestOp<TData, FieldState::Phys, FieldState::Phys>
61{
62public:
67
68 void Configure(unsigned int boundaryRegionId)
69 {
70 this->SetSession();
71 this->SetExpList();
72 this->fixt_explist->SetDataWarehouse();
73
74 auto graph = this->fixt_explist->GetGraph();
76 const auto &bregions = bcs.GetBoundaryRegions();
77 const auto regionIter = bregions.find(boundaryRegionId);
78 ASSERTL0(regionIter != bregions.end(),
79 "Boundary region id not found in this mesh.");
80
81 const auto &vars = this->session->GetVariables();
83 this->session, *(regionIter->second), graph, true, vars[0], false,
84 this->session->GetComm(), Collections::eNoImpType);
85 m_bcExpList->SetDataWarehouse();
86
87 SetFixture(1);
88 }
89
90 void SetFixture(const unsigned int nhomo) override
91 {
92 auto nvars = this->session->GetVariables().size();
93 auto inblockAttr = GetBlockAttributes<TData, FieldState::Phys>(
94 this->fixt_explist,
95 MultiRegions::UnitInterleaveWidths(this->fixt_explist));
96 auto outblockAttr = GetBlockAttributes<TData, FieldState::Phys>(
97 m_bcExpList, MultiRegions::UnitInterleaveWidths(m_bcExpList));
98
99 auto f_in =
100 Field<TData, FieldState::Phys>("f_in", inblockAttr, nvars, nhomo);
101 auto f_out =
102 Field<TData, FieldState::Phys>("f_out", outblockAttr, nvars, nhomo);
103 auto f_expected = Field<TData, FieldState::Phys>(
104 "f_expected", outblockAttr, nvars, nhomo);
105 this->fixt_in = new Field<TData, FieldState::Phys>(std::move(f_in));
106 this->fixt_out = new Field<TData, FieldState::Phys>(std::move(f_out));
107 this->fixt_expected =
108 new Field<TData, FieldState::Phys>(std::move(f_expected));
109 }
110
112 {
113 // Seed the volume field with u = x + 2y + 3z at every quadrature
114 // point, for every component.
115 for (unsigned blk = 0; blk < this->fixt_in->GetBlocks().size(); ++blk)
116 {
117 auto &block = this->fixt_in->GetBlocks()[blk];
118 auto collection =
120 const auto &expansions = collection.GetExpVector();
121
122 auto inptr =
123 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
124 for (unsigned n = 0; n < this->fixt_in->GetNumComponents() *
125 this->fixt_in->GetNumHomoModes();
126 ++n)
127 {
128 size_t cnt = 0;
129 for (size_t el = 0; el < expansions.size(); ++el)
130 {
131 const auto npts = expansions[el]->GetTotPoints();
132 // GetCoords() leaves z unset on a two-dimensional mesh.
133 Array<OneD, NekDouble> x(npts), y(npts), z(npts, 0.0);
134 expansions[el]->GetCoords(x, y, z);
135 for (int q = 0; q < npts; ++q, ++cnt)
136 {
137 inptr[cnt] = PhysTraceExtractBndCondTestFn<TData>(
138 static_cast<TData>(x[q]), static_cast<TData>(y[q]),
139 static_cast<TData>(z[q]));
140 }
141 }
142 inptr += block.CompSize();
143 }
144 }
145
146 // Expected: evaluate the same polynomial directly at the boundary
147 // region's own coordinates.
148 for (unsigned blk = 0; blk < this->fixt_expected->GetBlocks().size();
149 ++blk)
150 {
151 auto &block = this->fixt_expected->GetBlocks()[blk];
152 auto collection = MultiRegions::GetCollection(m_bcExpList, blk);
153 const auto &expansions = collection.GetExpVector();
154
155 auto expptr =
156 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
157 for (unsigned n = 0; n < this->fixt_expected->GetNumComponents() *
159 ++n)
160 {
161 size_t cnt = 0;
162 for (size_t el = 0; el < expansions.size(); ++el)
163 {
164 const auto npts = expansions[el]->GetTotPoints();
165 // GetCoords() leaves z unset on a two-dimensional mesh.
166 Array<OneD, NekDouble> x(npts), y(npts), z(npts, 0.0);
167 expansions[el]->GetCoords(x, y, z);
168 for (int q = 0; q < npts; ++q, ++cnt)
169 {
170 expptr[cnt] = PhysTraceExtractBndCondTestFn<TData>(
171 static_cast<TData>(x[q]), static_cast<TData>(y[q]),
172 static_cast<TData>(z[q]));
173 }
174 }
175 expptr += block.CompSize();
176 }
177 }
178 }
179
180 // The helper is header-only, and its Device kernels compile only in a
181 // translation unit built for the device, which this test is not. It is
182 // therefore tested here on Serial and AVX.
184 {
185 const std::string execStr(GlobalConfiguration::ExecStr());
186 if (execStr == NektarSpaces::Serial::name)
187 {
188 RunTestCase<NektarSpaces::Serial>();
189 }
190 else if (execStr == NektarSpaces::AVX::name)
191 {
192 RunTestCase<NektarSpaces::AVX>();
193 }
194 else
195 {
197 "TestPhysTraceExtractBndCond runs on Serial and AVX, "
198 "not on " +
199 execStr);
200 }
201 }
202
203 template <typename ExecSpace> void RunTestCase()
204 {
206 this->fixt_explist, this->session->GetVariables(), m_bcExpList,
207 ExecSpace::name);
208
209 if (m_inputInterleaveWidth == 1)
210 {
211 op.Apply(*this->fixt_in, *this->fixt_out);
212 return;
213 }
214
215 // Extract at the wider interleave width, then again at width 1 into
216 // a cleared output, so that the gather index is built for both.
217 this->fixt_in->ReshapeStorage(m_inputInterleaveWidth, ExecSpace::name);
218 op.Apply(*this->fixt_in, *this->fixt_out);
219 BOOST_TEST(this->Compare(1.0E-10));
220
221 this->fixt_in->ReshapeStorage(1, ExecSpace::name);
222 for (auto &block : this->fixt_out->GetBlocks())
223 {
224 auto outptr =
225 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
226 std::fill(outptr,
227 outptr + block.CompSize() * block.GetNumComponents() *
228 block.GetNumHomoModes(),
229 TData(0));
230 }
231 op.Apply(*this->fixt_in, *this->fixt_out);
232 }
233
234protected:
236 /// Interleave width the volume field is given to Apply() at.
237 unsigned int m_inputInterleaveWidth = 1;
238};
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
SpatialDomains::MeshGraphSharedPtr graph
static TData PhysTraceExtractBndCondTestFn(TData x, TData y, TData z)
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.
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
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.
Extracts a volume field onto the points of a boundary region of a continuous field.
void Apply(LibUtilities::Field< TData, FieldState::Phys > &in, LibUtilities::Field< TData, FieldState::Phys > &out)
Extract the first components of in, as many as out has, onto out.
const BoundaryRegionCollection & GetBoundaryRegions(void) const
Definition Conditions.h:273
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
std::shared_ptr< MultiRegions::ExpList > fixt_explist
Definition TestOp.hpp:439
LibUtilities::SessionReaderSharedPtr session
Definition TestOp.hpp:440
void Configure(unsigned int boundaryRegionId)
void SetFixture(const unsigned int nhomo) override
MultiRegions::ExpListSharedPtr m_bcExpList
unsigned int m_inputInterleaveWidth
Interleave width the volume field is given to Apply() at.
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
Collections::Collection GetCollection(MultiRegions::ExpListSharedPtr expansionList, unsigned int block_idx)
FieldState
Possible states for Field data.
static const std::string name
Definition Backends.hpp:107
static const std::string name
Definition Backends.hpp:101