Nektar++
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
TestNormOps< TData > Class Template Reference

#include <TestNormOps.hpp>

Public Member Functions

 TestNormOps ()=default
 
 ~TestNormOps ()=default
 
void Configure ()
 Read the session, build the expansion list and allocate the field the test cases work on.
 
void Configure3DH1 (const unsigned int nhomo)
 As Configure, on a 3DH1 expansion list with nhomo planes.
 
void SetTestCase ()
 Fill the input field with an analytic expression and record the norms it is expected to have.
 
void RunTestCase ()
 Apply the norm operators, once on the non-interleaved storage reduced to every rank and once on the interleaved storage reduced to rank zero alone.
 
bool Compare (const TData tol)
 Compare both sets of norms against the expected ones, with absolute tolerance tol.
 

Protected Member Functions

void SetSession ()
 
void SetExpList ()
 
void SetExpList3DH1 ()
 
void SetFixture ()
 
void ExpectedSolution ()
 The oracle: the norms the expansion list computes from the same data, independently of the operators under test.
 

Protected Attributes

std::string m_meshName = ""
 
unsigned int m_nhomo = 1
 Number of planes; 1 without a homogeneous direction.
 
SessionReaderSharedPtr m_session
 
ExpListSharedPtr m_expList
 
Field< TData, FieldState::Phys > m_in
 
std::vector< TData > m_l2Test
 
std::vector< TData > m_linfTest
 
std::vector< TData > m_l2Test2
 
std::vector< TData > m_linfTest2
 
std::vector< TData > m_l2NormalisedTest
 
std::vector< TData > m_l2NormalisedTest2
 
std::vector< TData > m_l2Expected
 
std::vector< TData > m_l2NormalisedExpected
 
std::vector< TData > m_linfExpected
 

Static Protected Attributes

static constexpr double m_homoLen = 2.0
 Homogeneous length of the 3DH1 cases, other than 1 so that it shows.
 

Detailed Description

template<typename TData>
class TestNormOps< TData >

A test fixture that fills a physical-space field with an analytic expression and checks NormL2Op (plain and normalised by the mesh volume) and NormLinfOp against the norms the expansion list computes itself, both for the non-interleaved storage and for the interleaved storage the vectorised and device paths use. The 3DH1 cases give every plane its own data, so the norms depend on how the planes are combined.

The fixture constructor (destructor) is called before (after) each call to the BOOST_FIXTURE_TEST_CASE(<test name>, <fixture>) macro.

See "Single test case fixture" on the Boost.Test documentation for more details: https://www.boost.org/doc/libs/1_82_0/libs/test/doc/html/boost_test/tests_organization/fixtures/case.html

Definition at line 83 of file TestNormOps.hpp.

Constructor & Destructor Documentation

◆ TestNormOps()

template<typename TData >
TestNormOps< TData >::TestNormOps ( )
default

◆ ~TestNormOps()

template<typename TData >
TestNormOps< TData >::~TestNormOps ( )
default

Member Function Documentation

◆ Compare()

template<typename TData >
bool TestNormOps< TData >::Compare ( const TData  tol)
inline

Compare both sets of norms against the expected ones, with absolute tolerance tol.

Returns
bool

Definition at line 211 of file TestNormOps.hpp.

212 {
213 const unsigned int numComp = m_in.GetNumComponents();
214
215 if (m_l2Test.size() != m_l2Expected.size() ||
217 m_linfTest.size() != m_linfExpected.size())
218 {
219 std::cout << "Mismatch of norm vector sizes." << std::endl;
220 return false;
221 }
222
223 bool match = true;
224 std::cout << " - Non-interleaved cases: " << std::endl;
225 for (unsigned int comp = 0; comp < numComp; ++comp)
226 {
227 const auto l2Diff = std::abs(m_l2Test[comp] - m_l2Expected[comp]);
228 const auto l2NormalisedDiff = std::abs(
230 const auto linfDiff =
231 std::abs(m_linfTest[comp] - m_linfExpected[comp]);
232
233 if (l2Diff > tol)
234 {
235 std::cout << "Component " << comp
236 << " mismatch: " << "L2(actual=" << m_l2Test[comp]
237 << ", expected=" << m_l2Expected[comp]
238 << ", diff=" << l2Diff << ")" << std::endl;
239 match = false;
240 }
241
242 if (l2NormalisedDiff > tol)
243 {
244 std::cout << "Component " << comp << " mismatch: "
245 << "L2 normalised(actual=" << m_l2NormalisedTest[comp]
246 << ", expected=" << m_l2NormalisedExpected[comp]
247 << ", diff=" << l2NormalisedDiff << ")" << std::endl;
248 match = false;
249 }
250
251 if (linfDiff > tol)
252 {
253 std::cout << "Component " << comp
254 << " mismatch: " << "Linf(actual=" << m_linfTest[comp]
255 << ", expected=" << m_linfExpected[comp]
256 << ", diff=" << linfDiff << ")" << std::endl;
257 match = false;
258 }
259 }
260
261 std::cout << " - Interleaved cases: " << std::endl;
262 for (unsigned int comp = 0; comp < numComp; ++comp)
263 {
264 const auto l2Diff = std::abs(m_l2Test2[comp] - m_l2Expected[comp]);
265 const auto l2NormalisedDiff = std::abs(
267 const auto linfDiff =
268 std::abs(m_linfTest2[comp] - m_linfExpected[comp]);
269
270 if (l2Diff > tol)
271 {
272 std::cout << "Component " << comp
273 << " mismatch: " << "L2(actual=" << m_l2Test2[comp]
274 << ", expected=" << m_l2Expected[comp]
275 << ", diff=" << l2Diff << ")" << std::endl;
276 match = false;
277 }
278
279 if (l2NormalisedDiff > tol)
280 {
281 std::cout << "Component " << comp
282 << " mismatch: " << "L2 normalised(actual="
283 << m_l2NormalisedTest2[comp]
284 << ", expected=" << m_l2NormalisedExpected[comp]
285 << ", diff=" << l2NormalisedDiff << ")" << std::endl;
286 match = false;
287 }
288
289 if (linfDiff > tol)
290 {
291 std::cout << "Component " << comp << " mismatch: "
292 << "Linf(actual=" << m_linfTest2[comp]
293 << ", expected=" << m_linfExpected[comp]
294 << ", diff=" << linfDiff << ")" << std::endl;
295 match = false;
296 }
297 }
298
299 return match;
300 }
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
std::vector< TData > m_linfTest
std::vector< TData > m_l2NormalisedTest2
std::vector< TData > m_linfTest2
Field< TData, FieldState::Phys > m_in
std::vector< TData > m_l2Test
std::vector< TData > m_l2Expected
std::vector< TData > m_l2NormalisedTest
std::vector< TData > m_l2Test2
std::vector< TData > m_l2NormalisedExpected
std::vector< TData > m_linfExpected

References Nektar::LibUtilities::Field< TData, TState >::GetNumComponents(), TestNormOps< TData >::m_in, TestNormOps< TData >::m_l2Expected, TestNormOps< TData >::m_l2NormalisedExpected, TestNormOps< TData >::m_l2NormalisedTest, TestNormOps< TData >::m_l2NormalisedTest2, TestNormOps< TData >::m_l2Test, TestNormOps< TData >::m_l2Test2, TestNormOps< TData >::m_linfExpected, TestNormOps< TData >::m_linfTest, and TestNormOps< TData >::m_linfTest2.

◆ Configure()

template<typename TData >
void TestNormOps< TData >::Configure ( )
inline

Read the session, build the expansion list and allocate the field the test cases work on.

Definition at line 93 of file TestNormOps.hpp.

94 {
95 SetSession();
96 SetExpList();
97 SetFixture();
98 }
void SetFixture()
void SetExpList()
void SetSession()

References TestNormOps< TData >::SetExpList(), TestNormOps< TData >::SetFixture(), and TestNormOps< TData >::SetSession().

◆ Configure3DH1()

template<typename TData >
void TestNormOps< TData >::Configure3DH1 ( const unsigned int  nhomo)
inline

As Configure, on a 3DH1 expansion list with nhomo planes.

Definition at line 103 of file TestNormOps.hpp.

104 {
105 m_nhomo = nhomo;
106 SetSession();
108 SetFixture();
109 }
void SetExpList3DH1()
unsigned int m_nhomo
Number of planes; 1 without a homogeneous direction.

References TestNormOps< TData >::m_nhomo, TestNormOps< TData >::SetExpList3DH1(), TestNormOps< TData >::SetFixture(), and TestNormOps< TData >::SetSession().

◆ ExpectedSolution()

template<typename TData >
void TestNormOps< TData >::ExpectedSolution ( )
inlineprotected

The oracle: the norms the expansion list computes from the same data, independently of the operators under test.

Definition at line 381 of file TestNormOps.hpp.

382 {
383 const unsigned int numComp = m_in.GetNumComponents();
384 const auto nphys = m_expList->GetTotPoints();
385
386 Array<OneD, TData> lhs = m_in.template ToArray<TData>();
387
388 // Mesh volume, for the normalised L2 norm: a 3DH1 mesh is one plane
389 // extended over the homogeneous length.
390 const auto volume =
391 (m_nhomo == 1)
392 ? m_expList->Integral(Array<OneD, double>(nphys, 1.0))
393 : m_expList->GetPlane(0)->Integral(Array<OneD, double>(
394 m_expList->GetPlane(0)->GetTotPoints(), 1.0)) *
395 m_homoLen;
396
397 m_l2Expected.resize(numComp);
398 m_l2NormalisedExpected.resize(numComp);
399 m_linfExpected.resize(numComp);
400
401 for (unsigned int comp = 0; comp < numComp; ++comp)
402 {
403 m_l2Expected[comp] = m_expList->L2(lhs + comp * nphys);
405 m_l2Expected[comp] / std::sqrt(volume);
406 m_linfExpected[comp] = m_expList->Linf(lhs + comp * nphys);
407 }
408 }
static constexpr double m_homoLen
Homogeneous length of the 3DH1 cases, other than 1 so that it shows.
ExpListSharedPtr m_expList

References Nektar::LibUtilities::Field< TData, TState >::GetNumComponents(), TestNormOps< TData >::m_expList, TestNormOps< TData >::m_homoLen, TestNormOps< TData >::m_in, TestNormOps< TData >::m_l2Expected, TestNormOps< TData >::m_l2NormalisedExpected, TestNormOps< TData >::m_linfExpected, and TestNormOps< TData >::m_nhomo.

Referenced by TestNormOps< TData >::SetTestCase().

◆ RunTestCase()

template<typename TData >
void TestNormOps< TData >::RunTestCase ( )
inline

Apply the norm operators, once on the non-interleaved storage reduced to every rank and once on the interleaved storage reduced to rank zero alone.

Definition at line 174 of file TestNormOps.hpp.

175 {
176 auto l2Op =
177 NormL2Op<TData>::Create(m_expList, m_session->GetVariables());
178 auto linfOp =
179 NormLinfOp<TData>::Create(m_expList, m_session->GetVariables());
180
181 // Non-interleaved case.
182 l2Op->Apply(m_in);
183 m_l2Test = l2Op->GetNorms();
184 m_l2NormalisedTest = l2Op->GetNorms(true);
185
186 linfOp->Apply(m_in);
187 m_linfTest = linfOp->GetNorms();
188
189 // Interleaved case.
190 std::string execStr =
192 m_in.ReshapeStorage(NektarSpaces::GetVectorWidth<TData>(execStr),
193 execStr);
194
195 l2Op->SetRootOnly(true);
196 l2Op->Apply(m_in);
197 m_l2Test2 = l2Op->GetNorms();
198 m_l2NormalisedTest2 = l2Op->GetNorms(true);
199
200 linfOp->SetRootOnly(true);
201 linfOp->Apply(m_in);
202 m_linfTest2 = linfOp->GetNorms();
203 }
void ReshapeStorage(const unsigned int &interleaveWidth, const std::string &execSpace)
static std::string GetOpExecSpace(std::shared_ptr< LibUtilities::SessionReader > session, const bool warn=true)
Return the execution space name ("opExecSpace") for an operator.
Definition Operator.hpp:190
SessionReaderSharedPtr m_session

References Nektar::MultiRegions::Operator< TData >::GetOpExecSpace(), TestNormOps< TData >::m_expList, TestNormOps< TData >::m_in, TestNormOps< TData >::m_l2NormalisedTest, TestNormOps< TData >::m_l2NormalisedTest2, TestNormOps< TData >::m_l2Test, TestNormOps< TData >::m_l2Test2, TestNormOps< TData >::m_linfTest, TestNormOps< TData >::m_linfTest2, TestNormOps< TData >::m_session, and Nektar::LibUtilities::Field< TData, TState >::ReshapeStorage().

◆ SetExpList()

template<typename TData >
void TestNormOps< TData >::SetExpList ( )
inlineprotected

Definition at line 346 of file TestNormOps.hpp.

347 {
351 m_expList->SetDataWarehouse();
352 }
SpatialDomains::MeshGraphSharedPtr graph
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)

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), Nektar::Collections::eNoCollection, graph, TestNormOps< TData >::m_expList, TestNormOps< TData >::m_session, and Nektar::SpatialDomains::MeshGraphIO::Read().

Referenced by TestNormOps< TData >::Configure().

◆ SetExpList3DH1()

template<typename TData >
void TestNormOps< TData >::SetExpList3DH1 ( )
inlineprotected

◆ SetFixture()

template<typename TData >
void TestNormOps< TData >::SetFixture ( )
inlineprotected

Definition at line 367 of file TestNormOps.hpp.

368 {
369 const unsigned int ncomp = m_session->GetVariables().size();
370 auto blockAttr = GetBlockAttributes<TData, FieldState::Phys>(
371 m_expList, MultiRegions::UnitInterleaveWidths(m_expList));
372
373 m_in =
374 Field<TData, FieldState::Phys>("f_in", blockAttr, ncomp, m_nhomo);
375 }
A Field represents expansion data to be operated on.

References TestNormOps< TData >::m_expList, TestNormOps< TData >::m_in, TestNormOps< TData >::m_nhomo, and TestNormOps< TData >::m_session.

Referenced by TestNormOps< TData >::Configure(), and TestNormOps< TData >::Configure3DH1().

◆ SetSession()

template<typename TData >
void TestNormOps< TData >::SetSession ( )
inlineprotected

Definition at line 322 of file TestNormOps.hpp.

323 {
324 std::string execStr(GlobalConfiguration::ExecStr());
325
326 BOOST_TEST_MESSAGE("Creating input field");
327
328 // Construct a fake command-line argument array to be fed to
329 // Session::Reader::CreateInstance. The first element stands for
330 // the name of the executable which, in our case, doesn't matter.
331 int argc = 3;
332 char **argv = new char *[argc];
333 argv[0] = strdup("exe_name");
334 argv[1] = strdup(m_meshName.data());
335 argv[2] = strdup(("--opExecSpace=" + execStr).c_str());
336
338
339 for (int i = 0; i < argc; ++i)
340 {
341 free(argv[i]);
342 }
343 delete[] argv;
344 }
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
std::string m_meshName

References Nektar::LibUtilities::SessionReader::CreateInstance(), TestNormOps< TData >::m_meshName, and TestNormOps< TData >::m_session.

Referenced by TestNormOps< TData >::Configure(), and TestNormOps< TData >::Configure3DH1().

◆ SetTestCase()

template<typename TData >
void TestNormOps< TData >::SetTestCase ( )
inline

Fill the input field with an analytic expression and record the norms it is expected to have.

Definition at line 115 of file TestNormOps.hpp.

116 {
117 const auto nphys = m_expList->GetTotPoints();
118
119 // Get coordinates
120 Array<OneD, double> x(nphys);
121 Array<OneD, double> y(nphys, 0.0);
122 Array<OneD, double> z(nphys, 0.0);
123 m_expList->GetCoords(x, y, z);
124
125 // Set initial conditions.
126 for (unsigned int blk = 0; blk < m_in.GetBlocks().size(); ++blk)
127 {
128 auto &inblock = m_in.GetBlocks()[blk];
129 auto inptr =
130 inblock.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
131 for (unsigned int n = 0;
133 {
134 const auto scale = static_cast<TData>(n + 1);
135 for (size_t el = 0, cnt = 0; el < inblock.GetNumElements();
136 ++el)
137 {
138 for (unsigned int phys = 0; phys < inblock.GetNumData();
139 ++phys, ++cnt)
140 {
141 const auto xc = static_cast<TData>(x[cnt]);
142 const auto yc = static_cast<TData>(y[cnt]);
143 const auto zc = static_cast<TData>(z[cnt]);
144
145 const auto base =
146 static_cast<TData>(0.3) * scale +
147 static_cast<TData>(0.2) * scale * xc +
148 static_cast<TData>(0.1) * (n + 2) * yc * yc -
149 static_cast<TData>(0.05) * scale * zc;
150
151 const auto base_plus_error =
152 static_cast<TData>(0.02) * scale *
153 (static_cast<TData>(1.0) + xc * xc +
154 static_cast<TData>(0.5) * yc +
155 static_cast<TData>(0.25) * zc * zc) +
156 static_cast<TData>(0.003) * (n + 1) *
157 ((cnt % 5) + 1);
158 inptr[cnt] = base - base_plus_error;
159 }
160 }
161 inptr += inblock.CompSize();
162 }
163 }
164
165 // Compute expected solution.
167 }
SpatialDomains::Geometry * el
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
void ExpectedSolution()
The oracle: the norms the expansion list computes from the same data, independently of the operators ...
const Array< OneD, NekDouble > base(3, 0.0)

References el, TestNormOps< TData >::ExpectedSolution(), Nektar::LibUtilities::Field< TData, TState >::GetBlocks(), Nektar::LibUtilities::Field< TData, TState >::GetNumComponents(), Nektar::LibUtilities::Field< TData, TState >::GetNumHomoModes(), TestNormOps< TData >::m_expList, and TestNormOps< TData >::m_in.

Member Data Documentation

◆ m_expList

template<typename TData >
ExpListSharedPtr TestNormOps< TData >::m_expList
protected

◆ m_homoLen

template<typename TData >
constexpr double TestNormOps< TData >::m_homoLen = 2.0
staticconstexprprotected

Homogeneous length of the 3DH1 cases, other than 1 so that it shows.

Definition at line 307 of file TestNormOps.hpp.

Referenced by TestNormOps< TData >::ExpectedSolution(), and TestNormOps< TData >::SetExpList3DH1().

◆ m_in

template<typename TData >
Field<TData, FieldState::Phys> TestNormOps< TData >::m_in
protected

◆ m_l2Expected

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_l2Expected
protected

◆ m_l2NormalisedExpected

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_l2NormalisedExpected
protected

◆ m_l2NormalisedTest

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_l2NormalisedTest
protected

◆ m_l2NormalisedTest2

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_l2NormalisedTest2
protected

◆ m_l2Test

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_l2Test
protected

◆ m_l2Test2

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_l2Test2
protected

◆ m_linfExpected

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_linfExpected
protected

◆ m_linfTest

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_linfTest
protected

◆ m_linfTest2

template<typename TData >
std::vector<TData> TestNormOps< TData >::m_linfTest2
protected

◆ m_meshName

template<typename TData >
std::string TestNormOps< TData >::m_meshName = ""
protected

Definition at line 303 of file TestNormOps.hpp.

Referenced by TestNormOps< TData >::SetSession().

◆ m_nhomo

template<typename TData >
unsigned int TestNormOps< TData >::m_nhomo = 1
protected

◆ m_session

template<typename TData >
SessionReaderSharedPtr TestNormOps< TData >::m_session
protected