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

Compares MaxStdVelocityOp against the legacy formula. More...

#include <TestMaxStdVelocity.hpp>

Public Member Functions

 TestMaxStdVelocity ()=default
 
 ~TestMaxStdVelocity ()=default
 
void Configure ()
 Read the session, build the expansion list and allocate the field the test cases work on.
 
void SetTestCase ()
 Fill the velocity components and the wave speed, and record the value the operator is expected to return.
 
void RunTestCase ()
 Apply the operator with and without the wave speed, 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 weightings and both storage layouts against the expected values, with relative tolerance tol.
 

Protected Member Functions

void SetSession ()
 
void SetExpList ()
 
void SetFixture ()
 
void ExpectedSolution ()
 The oracle: the legacy computation, element by element on the host.
 

Protected Attributes

std::string m_meshName = ""
 
SessionReaderSharedPtr m_session
 
ExpListSharedPtr m_expList
 
Field< TData, FieldState::Phys > m_in
 
TData m_testNoSound = 0
 
TData m_testWithSound = 0
 
TData m_testNoSound2 = 0
 
TData m_testWithSound2 = 0
 
TData m_expectedNoSound = 0
 
TData m_expectedWithSound = 0
 

Detailed Description

template<typename TData>
class TestMaxStdVelocity< TData >

Compares MaxStdVelocityOp against the legacy formula.

The expected value is computed here from the legacy expansion interface - GetGeomFactors()->GetDerivFactors() element by element - rather than from the packed factors the operator reads out of the data warehouse. The two reach the same numbers by different routes, so the comparison checks the packing and its indexing, the deformed and regular layouts, the exclusion of padded elements and the per block order weighting, on every mesh the fixture offers.

The field carries one velocity component per coordinate direction followed by a wave speed, and the operator is asked for both weightings: zero, the incompressible and scalar advection case, and one, where the wave speed contributes as a sound speed does.

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 90 of file TestMaxStdVelocity.hpp.

Constructor & Destructor Documentation

◆ TestMaxStdVelocity()

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

◆ ~TestMaxStdVelocity()

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

Member Function Documentation

◆ Compare()

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

Compare both weightings and both storage layouts against the expected values, with relative tolerance tol.

Returns
bool

Definition at line 203 of file TestMaxStdVelocity.hpp.

204 {
205 bool match = true;
206
207 const auto report = [&match, tol](const std::string &desc,
208 const TData actual,
209 const TData expected) {
210 const auto diff = std::abs(actual - expected);
211 const auto rel =
212 diff / (std::max)(std::abs(expected), static_cast<TData>(1));
213 if (rel > tol)
214 {
215 std::cout << desc << " mismatch: actual=" << actual
216 << ", expected=" << expected << ", rel diff=" << rel
217 << std::endl;
218 match = false;
219 }
220 };
221
222 report("Non-interleaved case without wave speed", m_testNoSound,
224 report("Non-interleaved case with wave speed", m_testWithSound,
226 report("Interleaved case without wave speed", m_testNoSound2,
228 report("Interleaved case with wave speed", m_testWithSound2,
230
231 // The wave speed can only add to the transport rate, and the test
232 // velocity is not uniform, so the two must actually differ - a
233 // factor that was silently ignored would otherwise pass.
235 {
236 std::cout << "Wave speed made no difference: with="
237 << m_testWithSound << ", without=" << m_testNoSound
238 << std::endl;
239 match = false;
240 }
241
242 return match;
243 }

References TestMaxStdVelocity< TData >::m_expectedNoSound, TestMaxStdVelocity< TData >::m_expectedWithSound, TestMaxStdVelocity< TData >::m_testNoSound, TestMaxStdVelocity< TData >::m_testNoSound2, TestMaxStdVelocity< TData >::m_testWithSound, and TestMaxStdVelocity< TData >::m_testWithSound2.

◆ Configure()

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

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

Definition at line 100 of file TestMaxStdVelocity.hpp.

101 {
102 SetSession();
103 SetExpList();
104 m_expList->SetDataWarehouse();
105 SetFixture();
106 }
ExpListSharedPtr m_expList

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

◆ ExpectedSolution()

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

The oracle: the legacy computation, element by element on the host.

Mirrors CompressibleFlowSystem::v_GetMaxStdVelocity, including its indexing of the derivative factors and its treatment of the wave speed as acting equally in every direction, then applies the order weight the operator folds in per block.

Definition at line 310 of file TestMaxStdVelocity.hpp.

311 {
312 const auto coordim = m_expList->GetCoordim(0);
313 const auto expdim = m_expList->GetExp(0)->GetShapeDimension();
314 const auto nElmts = m_expList->GetExpSize();
315
316 Array<OneD, TData> in = m_in.template ToArray<TData>();
317 const auto nphys = m_expList->GetTotPoints();
318
321
322 for (size_t el = 0; el < nElmts; ++el)
323 {
324 auto exp = m_expList->GetExp(el);
325 const auto offset = m_expList->GetPhys_Offset(el);
326 const auto nqTot = exp->GetTotPoints();
327 const auto &gmat = exp->GetGeomFactors()->GetDerivFactors();
328 const bool deformed =
329 exp->GetGeomFactors()->GetGtype() == SpatialDomains::eDeformed;
330
331 const TData order =
332 (std::max)(static_cast<TData>(exp->EvalBasisNumModesMax()) -
333 static_cast<TData>(1),
334 static_cast<TData>(1));
335 const TData weight = order * order;
336
337 for (unsigned int q = 0; q < nqTot; ++q)
338 {
339 TData sqNoSound = 0;
340 TData sqWithSound = 0;
341
342 for (unsigned int i = 0; i < expdim; ++i)
343 {
344 TData stdVel = 0;
345 TData stdSound = 0;
346 for (unsigned int j = 0; j < coordim; ++j)
347 {
348 const TData g = static_cast<TData>(
349 gmat[expdim * j + i][deformed ? q : 0]);
350 stdVel += g * in[j * nphys + offset + q];
351 stdSound += g;
352 }
353
354 const TData c = in[coordim * nphys + offset + q];
355
356 const TData velNoSound = std::abs(stdVel);
357 const TData velSound =
358 std::abs(stdVel) + std::abs(stdSound * c);
359
360 sqNoSound += velNoSound * velNoSound;
361 sqWithSound += velSound * velSound;
362 }
363
365 std::sqrt(sqNoSound) * weight);
366 m_expectedWithSound = (std::max)(
367 m_expectedWithSound, std::sqrt(sqWithSound) * weight);
368 }
369 }
370 }
SpatialDomains::Geometry * el
Field< TData, FieldState::Phys > m_in
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT in
@ eDeformed
Geometry is curved or has non-constant factors.

References Nektar::SpatialDomains::eDeformed, el, TestMaxStdVelocity< TData >::m_expectedNoSound, TestMaxStdVelocity< TData >::m_expectedWithSound, TestMaxStdVelocity< TData >::m_expList, and TestMaxStdVelocity< TData >::m_in.

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

◆ RunTestCase()

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

Apply the operator with and without the wave speed, once on the non-interleaved storage reduced to every rank and once on the interleaved storage reduced to rank zero alone.

Definition at line 172 of file TestMaxStdVelocity.hpp.

173 {
174 auto op = MaxStdVelocityOp<TData>::Create(m_expList,
175 m_session->GetVariables());
176
177 // Non-interleaved case.
178 op->SetSoundSpeedFactor(TData(0));
179 m_testNoSound = op->Apply(m_in);
180
181 op->SetSoundSpeedFactor(TData(1));
182 m_testWithSound = op->Apply(m_in);
183
184 // Interleaved case.
185 std::string execStr = Operator<TData>::GetOpExecSpace(m_session);
186 m_in.ReshapeStorage(NektarSpaces::GetVectorWidth<TData>(execStr),
187 execStr);
188
189 op->SetRootOnly(true);
190 op->SetSoundSpeedFactor(TData(0));
191 m_testNoSound2 = op->Apply(m_in);
192
193 op->SetSoundSpeedFactor(TData(1));
194 m_testWithSound2 = op->Apply(m_in);
195 }
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(), TestMaxStdVelocity< TData >::m_expList, TestMaxStdVelocity< TData >::m_in, TestMaxStdVelocity< TData >::m_session, TestMaxStdVelocity< TData >::m_testNoSound, TestMaxStdVelocity< TData >::m_testNoSound2, TestMaxStdVelocity< TData >::m_testWithSound, TestMaxStdVelocity< TData >::m_testWithSound2, and Nektar::LibUtilities::Field< TData, TState >::ReshapeStorage().

◆ SetExpList()

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

Definition at line 282 of file TestMaxStdVelocity.hpp.

283 {
287 }
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, TestMaxStdVelocity< TData >::m_expList, TestMaxStdVelocity< TData >::m_session, and Nektar::SpatialDomains::MeshGraphIO::Read().

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

◆ SetFixture()

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

Definition at line 289 of file TestMaxStdVelocity.hpp.

290 {
291 // One component per coordinate direction, plus the wave speed. This
292 // is the operator's input contract, and is unrelated to the number of
293 // variables the session happens to declare.
294 const unsigned int ncomp = m_expList->GetCoordim(0) + 1;
295 auto blockAttr = GetBlockAttributes<TData, FieldState::Phys>(
296 m_expList, MultiRegions::UnitInterleaveWidths(m_expList));
297
298 m_in = Field<TData, FieldState::Phys>("f_in", blockAttr, ncomp, 1);
299 }
A Field represents expansion data to be operated on.

References TestMaxStdVelocity< TData >::m_expList, and TestMaxStdVelocity< TData >::m_in.

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

◆ SetSession()

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

Definition at line 258 of file TestMaxStdVelocity.hpp.

259 {
260 std::string execStr(GlobalConfiguration::ExecStr());
261
262 BOOST_TEST_MESSAGE("Creating input field");
263
264 // Construct a fake command-line argument array to be fed to
265 // Session::Reader::CreateInstance. The first element stands for
266 // the name of the executable which, in our case, doesn't matter.
267 int argc = 3;
268 char **argv = new char *[argc];
269 argv[0] = strdup("exe_name");
270 argv[1] = strdup(m_meshName.data());
271 argv[2] = strdup(("--opExecSpace=" + execStr).c_str());
272
274
275 for (int i = 0; i < argc; ++i)
276 {
277 free(argv[i]);
278 }
279 delete[] argv;
280 }
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.

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

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

◆ SetTestCase()

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

Fill the velocity components and the wave speed, and record the value the operator is expected to return.

Definition at line 112 of file TestMaxStdVelocity.hpp.

113 {
114 const auto nphys = m_expList->GetTotPoints();
115 const auto coordim = m_expList->GetCoordim(0);
116
117 Array<OneD, double> x(nphys);
118 Array<OneD, double> y(nphys, 0.0);
119 Array<OneD, double> z(nphys, 0.0);
120 m_expList->GetCoords(x, y, z);
121
122 // A velocity that varies over the mesh, so the maximum is not
123 // attained everywhere and a mistake in which point or element is
124 // examined shows up. The wave speed is kept strictly positive, as a
125 // physical sound speed is.
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
132 for (unsigned int n = 0; n < m_in.GetNumComponents(); ++n)
133 {
134 for (size_t el = 0, cnt = 0; el < inblock.GetNumElements();
135 ++el)
136 {
137 for (unsigned int phys = 0; phys < inblock.GetNumData();
138 ++phys, ++cnt)
139 {
140 const auto xc = static_cast<TData>(x[cnt]);
141 const auto yc = static_cast<TData>(y[cnt]);
142 const auto zc = static_cast<TData>(z[cnt]);
143
144 if (n < coordim)
145 {
146 inptr[cnt] = static_cast<TData>(0.7 + 0.3 * n) +
147 static_cast<TData>(0.4) * xc -
148 static_cast<TData>(0.2) * yc +
149 static_cast<TData>(0.1) * zc * zc;
150 }
151 else
152 {
153 inptr[cnt] = static_cast<TData>(1.3) +
154 static_cast<TData>(0.2) * xc * xc +
155 static_cast<TData>(0.05) * yc;
156 }
157 }
158 }
159 inptr += inblock.CompSize();
160 }
161 }
162
163 // Compute expected solution.
165 }
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
void ExpectedSolution()
The oracle: the legacy computation, element by element on the host.

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

Member Data Documentation

◆ m_expectedNoSound

template<typename TData >
TData TestMaxStdVelocity< TData >::m_expectedNoSound = 0
protected

◆ m_expectedWithSound

template<typename TData >
TData TestMaxStdVelocity< TData >::m_expectedWithSound = 0
protected

◆ m_expList

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

◆ m_in

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

◆ m_meshName

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

Definition at line 246 of file TestMaxStdVelocity.hpp.

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

◆ m_session

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

◆ m_testNoSound

template<typename TData >
TData TestMaxStdVelocity< TData >::m_testNoSound = 0
protected

◆ m_testNoSound2

template<typename TData >
TData TestMaxStdVelocity< TData >::m_testNoSound2 = 0
protected

◆ m_testWithSound

template<typename TData >
TData TestMaxStdVelocity< TData >::m_testWithSound = 0
protected

◆ m_testWithSound2

template<typename TData >
TData TestMaxStdVelocity< TData >::m_testWithSound2 = 0
protected