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

Compares a preconditioner operator against its legacy counterpart. More...

#include <TestPreconOps.hpp>

Public Member Functions

 TestPreconOps ()=default
 
 ~TestPreconOps ()=default
 
void Configure ()
 Read the session, build the continuous expansion list and allocate the fields the test cases work on.
 
void SetTestCase (const std::string &method)
 Set the input coefficients and the problem parameters, and record the result the preconditioner is expected to produce.
 
void RunTestCase (const std::string &method)
 Build the preconditioner for method, configure it against the Helmholtz operator and apply it to the assembled input.
 
void ExpectedSolution (const std::string &method)
 The oracle: the legacy MultiRegions preconditioner of the same name, built on a GlobalLinSys for the same Helmholtz problem.
 
bool Compare (TData tol)
 Compare the computed field to the expected one, with absolute tolerance tol. The two must have the same blocks and components; their interleave widths may differ.
 

Protected Member Functions

void SetSession ()
 
void SetExpList ()
 A continuous field: the preconditioners act on the assembled, globally continuous coefficients, and the oracle needs the local to global map that only ContField carries.
 
void SetFixture ()
 

Protected Attributes

std::string m_meshName = ""
 
SessionReaderSharedPtr m_session
 
ExpListSharedPtr m_expList
 
Field< TData, FieldState::Coeff > m_in
 
Field< TData, FieldState::Coeff > m_out
 
Field< TData, FieldState::Coeff > m_expected
 
TData m_lambda
 
std::vector< TData > m_diffCoeff
 
bool m_isReconfigured = false
 

Detailed Description

template<typename TData>
class TestPreconOps< TData >

Compares a preconditioner operator against its legacy counterpart.

The operator under test is built through PreconOp::Create for the requested method and configured against a Helmholtz operator; the expected result comes from the MultiRegions preconditioner of the same name, built on a legacy GlobalLinSys for the same problem. The two reach the same numbers by different routes, so the comparison checks the operator, its configuration from the element operator, and its handling of the assembled storage.

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 TestPreconOps.hpp.

Constructor & Destructor Documentation

◆ TestPreconOps()

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

◆ ~TestPreconOps()

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

Member Function Documentation

◆ Compare()

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

Compare the computed field to the expected one, with absolute tolerance tol. The two must have the same blocks and components; their interleave widths may differ.

Returns
bool

Definition at line 252 of file TestPreconOps.hpp.

253 {
254 auto rank = m_session->GetComm()->GetRank();
255
257 {
258 std::cout << "Mismatch of number of components." << std::endl;
259 return false;
260 }
261
263 {
264 std::cout << "Mismatch of number of homogeneous modes."
265 << std::endl;
266 return false;
267 }
268
269 if (m_expected.GetBlocks().size() != m_out.GetBlocks().size())
270 {
271 std::cout << "Mismatch of block size." << std::endl;
272 return false;
273 }
274
275 // ToVector() returns every component element by element, whatever
276 // the interleave width of each block, and leaves the fields as they
277 // are.
278 const std::vector<TData> out = m_out.template ToVector<TData>();
279 const std::vector<TData> expected =
280 m_expected.template ToVector<TData>();
281 const size_t compSize =
283
284 bool isMatch = true;
285
286 if (rank == 0)
287 {
288 printf("#elm #pts output expected "
289 "difference\n");
290 }
291 size_t blkOffset = 0;
292 for (unsigned int blk = 0; blk < m_out.GetBlocks().size(); ++blk)
293 {
294 const auto &outBlock = m_out.GetBlocks()[blk];
295 const auto &expBlock = m_expected.GetBlocks()[blk];
296 if ((outBlock.GetNumElements() != expBlock.GetNumElements()) ||
297 (outBlock.GetNumData() != expBlock.GetNumData()))
298 {
299 std::cout << "Mismatch of block structure." << std::endl;
300 return false;
301 }
302 const size_t nElmts = outBlock.GetNumElements();
303 const unsigned int nPts = outBlock.GetNumData();
304
305 for (unsigned int n = 0;
307 {
308 const TData *outptr = out.data() + n * compSize + blkOffset;
309 const TData *expptr =
310 expected.data() + n * compSize + blkOffset;
311 size_t MisMatchcnt = 0, total = 0;
312
313 for (size_t el = 0, cnt = 0; el < nElmts; ++el)
314 {
315 for (unsigned int pts = 0; pts < nPts; ++pts, ++cnt)
316 {
317 if (std::isnan(outptr[cnt]) ||
318 std::isinf(outptr[cnt]) ||
319 std::abs(outptr[cnt] - expptr[cnt]) > tol)
320 {
321 printf("%04lu %04u %20.16f %20.16f %20.16f\n", el,
322 pts, outptr[cnt], expptr[cnt],
323 std::abs(outptr[cnt] - expptr[cnt]));
324 MisMatchcnt++;
325 }
326 total++;
327 }
328 }
329
330 if (MisMatchcnt)
331 {
332 std::cout << "Number of mismatches in component " << n
333 << " on block " << blk << " is " << MisMatchcnt
334 << " out of " << total << " on rank: " << rank
335 << std::endl;
336 isMatch = false;
337 }
338 }
339 blkOffset += nElmts * nPts;
340 }
341
342 return isMatch;
343 }
SpatialDomains::Geometry * el
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.
SessionReaderSharedPtr m_session
Field< TData, FieldState::Coeff > m_out
Field< TData, FieldState::Coeff > m_expected
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 TData *NEK_RESTRICT out

References el, Nektar::LibUtilities::Field< TData, TState >::GetBlocks(), Nektar::LibUtilities::Field< TData, TState >::GetNumComponents(), Nektar::LibUtilities::Field< TData, TState >::GetNumHomoModes(), TestPreconOps< TData >::m_expected, TestPreconOps< TData >::m_out, TestPreconOps< TData >::m_session, and rank.

◆ Configure()

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

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

Definition at line 100 of file TestPreconOps.hpp.

101 {
102 SetSession();
103 SetExpList();
104 SetFixture();
105 }
void SetExpList()
A continuous field: the preconditioners act on the assembled, globally continuous coefficients,...

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

◆ ExpectedSolution()

template<typename TData >
void TestPreconOps< TData >::ExpectedSolution ( const std::string &  method)
inline

The oracle: the legacy MultiRegions preconditioner of the same name, built on a GlobalLinSys for the same Helmholtz problem.

Definition at line 216 of file TestPreconOps.hpp.

217 {
218 auto nin = m_session->GetVariables().size();
219 auto ncoeff = m_expList->GetNcoeffs();
220
221 // Calculate expected result from Nektar++
222 Array<OneD, TData> incoeffs = m_in.ToArray();
223 Array<OneD, TData> outcoeffs(nin * ncoeff, 0.0);
224
227
228 auto map = std::dynamic_pointer_cast<ContField>(m_expList)
229 ->GetLocalToGlobalMap();
231 auto globalSys = GetGlobalLinSysFactory().CreateInstance(
232 "IterativeFull", key, m_expList, map);
233 auto precond =
234 GetPreconFactory().CreateInstance(method, globalSys, map);
235 precond->BuildPreconditioner();
236 for (unsigned int n = 0; n < nin; n++)
237 {
239 precond->DoPreconditioner(incoeffs + n * ncoeff,
240 tmp = outcoeffs + n * ncoeff, true);
241 }
242 m_expected.template CopyArray<NektarSpaces::HostSpace>(outcoeffs);
243 }
Nektar::Array< Nektar::OneD, TDataOut > ToArray()
Copy the data to a Nektar::Array, component by component and element by element, without padding,...
tBasePtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
ExpListSharedPtr m_expList
Field< TData, FieldState::Coeff > m_in
GlobalLinSysFactory & GetGlobalLinSysFactory()
PreconFactory & GetPreconFactory()
std::map< ConstFactorType, NekDouble > ConstFactorMap
StdRegions::ConstFactorMap factors

References Nektar::LibUtilities::NekFactoryBase< tKey, tBase, tBasePtr, tParam >::CreateInstance(), Nektar::StdRegions::eFactorLambda, Nektar::StdRegions::eHelmholtz, Nektar::MultiRegions::GetGlobalLinSysFactory(), Nektar::MultiRegions::GetPreconFactory(), TestPreconOps< TData >::m_expected, TestPreconOps< TData >::m_expList, TestPreconOps< TData >::m_in, TestPreconOps< TData >::m_lambda, TestPreconOps< TData >::m_session, and Nektar::LibUtilities::Field< TData, TState >::ToArray().

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

◆ RunTestCase()

template<typename TData >
void TestPreconOps< TData >::RunTestCase ( const std::string &  method)
inline

Build the preconditioner for method, configure it against the Helmholtz operator and apply it to the assembled input.

Definition at line 163 of file TestPreconOps.hpp.

164 {
165 std::string execStr = Operator<TData>::GetOpExecSpace(m_session);
166
168 m_expList, m_session->GetVariables());
169 auto op =
171 auto precon = PreconOp<TData>::Create(
172 m_expList, m_session->GetVariables(), method);
173 op->SetLambda(m_lambda);
174 op->SetDiffCoeff(m_diffCoeff);
175 auto lhs = std::make_shared<LHSOp<TData>>(m_expList,
176 m_session->GetVariables());
177 lhs->SetElmtOp(op);
179 {
180 // Configure and apply for another operator first, so that the
181 // configuration below refills what this one left.
182 const unsigned int ncomp = m_session->GetVariables().size();
184 "f_first",
185 GetBlockAttributes<TData, FieldState::Coeff>(
186 m_expList, MultiRegions::UnitInterleaveWidths(m_expList)),
187 ncomp, 1);
188 op->SetLambda(2.0 * m_lambda + 1.0);
189 precon->Configure(lhs);
190 assmb->Apply(m_in, first);
191 if (execStr == "AVX")
192 {
193 first.ReshapeStorage(
194 NektarSpaces::GetVectorWidth<TData>(execStr), execStr);
195 }
196 precon->Apply(first, first);
197 op->SetLambda(m_lambda);
198 }
199 precon->Configure(lhs);
200 assmb->Apply(m_in, m_out);
201
202 // reshape m_out
203 if (execStr == "AVX")
204 {
205 m_out.ReshapeStorage(NektarSpaces::GetVectorWidth<TData>(execStr),
206 execStr);
207 }
208
209 precon->Apply(m_out, m_out);
210 }
A Field represents expansion data to be operated on.
void ReshapeStorage(const unsigned int &interleaveWidth, const std::string &execSpace)
static std::shared_ptr< AssmbScatrZeroDirOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="")
Create an implementation of this operator through the operator factory.
static std::shared_ptr< HelmholtzOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Create a weak-Helmholtz operator through the operator and block-operator factories.
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
std::vector< TData > m_diffCoeff

References Nektar::MultiRegions::AssmbScatrZeroDirOp< TData >::Create(), Nektar::MultiRegions::HelmholtzOp< TData >::Create(), Nektar::MultiRegions::Operator< TData >::GetOpExecSpace(), TestPreconOps< TData >::m_diffCoeff, TestPreconOps< TData >::m_expList, TestPreconOps< TData >::m_in, TestPreconOps< TData >::m_isReconfigured, TestPreconOps< TData >::m_lambda, TestPreconOps< TData >::m_out, TestPreconOps< TData >::m_session, and Nektar::LibUtilities::Field< TData, TState >::ReshapeStorage().

◆ SetExpList()

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

A continuous field: the preconditioners act on the assembled, globally continuous coefficients, and the oracle needs the local to global map that only ContField carries.

Definition at line 391 of file TestPreconOps.hpp.

392 {
395 m_session, graph, "u", true, false, Collections::eNoCollection);
396 m_expList->SetDataWarehouse();
397 }
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, TestPreconOps< TData >::m_expList, TestPreconOps< TData >::m_session, and Nektar::SpatialDomains::MeshGraphIO::Read().

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

◆ SetFixture()

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

Definition at line 399 of file TestPreconOps.hpp.

400 {
401 const unsigned int ncomp = m_session->GetVariables().size();
402 auto blockAttr = GetBlockAttributes<TData, FieldState::Coeff>(
403 m_expList, MultiRegions::UnitInterleaveWidths(m_expList));
404
405 m_in = Field<TData, FieldState::Coeff>("f_in", blockAttr, ncomp, 1);
406 m_out = Field<TData, FieldState::Coeff>("f_out", blockAttr, ncomp, 1);
407 m_expected =
408 Field<TData, FieldState::Coeff>("f_expected", blockAttr, ncomp, 1);
409 }

References TestPreconOps< TData >::m_expected, TestPreconOps< TData >::m_expList, TestPreconOps< TData >::m_in, TestPreconOps< TData >::m_out, and TestPreconOps< TData >::m_session.

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

◆ SetSession()

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

Definition at line 360 of file TestPreconOps.hpp.

361 {
362 std::string execStr(GlobalConfiguration::ExecStr());
363 std::string implStr(GlobalConfiguration::ImplStr());
364
365 BOOST_TEST_MESSAGE("Creating input and output fields");
366
367 // Construct a fake command-line argument array to be fed to
368 // Session::Reader::CreateInstance. The first element stands for
369 // the name of the executable which, in our case, doesn't matter.
370 int argc = 4;
371 char **argv = new char *[argc];
372 argv[0] = strdup("exe_name");
373 argv[1] = strdup(m_meshName.data());
374 argv[2] = strdup(("--opExecSpace=" + execStr).c_str());
375 argv[3] = strdup(("--opImpl=" + implStr).c_str());
376
378
379 for (int i = 0; i < argc; ++i)
380 {
381 free(argv[i]);
382 }
383 delete[] argv;
384 }
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
std::string m_meshName

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

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

◆ SetTestCase()

template<typename TData >
void TestPreconOps< TData >::SetTestCase ( const std::string &  method)
inline

Set the input coefficients and the problem parameters, and record the result the preconditioner is expected to produce.

Definition at line 111 of file TestPreconOps.hpp.

112 {
113 // Set initial conditions.
114 for (unsigned int blk = 0; blk < m_in.GetBlocks().size(); ++blk)
115 {
116 auto &block = m_in.GetBlocks()[blk];
117 auto inptr =
118 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
119 for (size_t el = 0, cnt = 0; el < block.GetNumElements(); ++el)
120 {
121 for (unsigned int coeff = 0; coeff < block.GetNumData();
122 ++coeff, ++cnt)
123 {
124 inptr[cnt] = 1.0;
125 }
126 }
127 }
128
129 // Get lambda from m_session or default to 10.0
130 m_lambda = m_session->DefinesParameter("Lambda")
131 ? m_session->GetParameter("Lambda")
132 : 10.0;
133
134 // Set up diffusion coefficient.
135 const auto coordDim = m_expList->GetCoordim(0);
136 const auto nDiffCoeff = coordDim * (coordDim + 1) / 2;
137 m_diffCoeff.resize(nDiffCoeff);
138
139 if (coordDim == 1)
140 {
141 m_diffCoeff[0] = 1.0; // D00
142 }
143 else if (coordDim == 2)
144 {
145 m_diffCoeff[0] = 1.0; // D00
146 m_diffCoeff[2] = 1.0; // D11
147 }
148 else
149 {
150 m_diffCoeff[0] = 1.0; // D00
151 m_diffCoeff[2] = 1.0; // D11
152 m_diffCoeff[5] = 1.0; // D22
153 }
154
155 // Compute expected solution.
156 ExpectedSolution(method);
157 }
void ExpectedSolution(const std::string &method)
The oracle: the legacy MultiRegions preconditioner of the same name, built on a GlobalLinSys for the ...

References el, TestPreconOps< TData >::ExpectedSolution(), Nektar::LibUtilities::Field< TData, TState >::GetBlocks(), TestPreconOps< TData >::m_diffCoeff, TestPreconOps< TData >::m_expList, TestPreconOps< TData >::m_in, TestPreconOps< TData >::m_lambda, and TestPreconOps< TData >::m_session.

Member Data Documentation

◆ m_diffCoeff

template<typename TData >
std::vector<TData> TestPreconOps< TData >::m_diffCoeff
protected

◆ m_expected

template<typename TData >
Field<TData, FieldState::Coeff> TestPreconOps< TData >::m_expected
protected

◆ m_expList

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

◆ m_in

template<typename TData >
Field<TData, FieldState::Coeff> TestPreconOps< TData >::m_in
protected

◆ m_isReconfigured

template<typename TData >
bool TestPreconOps< TData >::m_isReconfigured = false
protected

Definition at line 358 of file TestPreconOps.hpp.

Referenced by TestPreconOps< TData >::RunTestCase().

◆ m_lambda

template<typename TData >
TData TestPreconOps< TData >::m_lambda
protected

◆ m_meshName

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

Definition at line 346 of file TestPreconOps.hpp.

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

◆ m_out

template<typename TData >
Field<TData, FieldState::Coeff> TestPreconOps< TData >::m_out
protected

◆ m_session

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