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

#include <TestTimeOps.hpp>

Public Member Functions

 TestTimeOps ()=default
 
 ~TestTimeOps ()=default
 
void Configure ()
 Read the session, build the expansion list and allocate the fields the test cases work on.
 
void SetTestCase (const TData alpha, const TData beta)
 Set the initial condition and the analytic solution it is expected to reach at the final time.
 
void RunTestCase (const std::string scheme, const std::string variant, const unsigned int order, const std::vector< TData > freeParams, const unsigned int numsteps)
 Integrate the test problem for numsteps steps of the scheme given, leaving the result in the output field.
 
void ExpectedSolution (const double final_time)
 Fill the expected field with the analytic solution at final_time.
 
bool CheckOrderOfAccuracy (const std::string scheme, const std::string variant, const unsigned int order, const std::vector< TData > freeParams={})
 Integrate the test problem over a sequence of timesteps and check the order the error converges at against the order claimed by the scheme.
 

Protected Member Functions

void SetSession ()
 Create the session from the mesh named by the derived fixture and the execution space given on the command line.
 
void SetExpList ()
 
void SetFixture ()
 
void DoLHS (Field< TData, FieldState::Phys > &in, Field< TData, FieldState::Phys > &out, const TData &time, const TData &lambda)
 Implicit solve of the stiff part of the test problem.
 
void DoExplicitRHS (Field< TData, FieldState::Phys > &in, Field< TData, FieldState::Phys > &out, const TData &time, const TData &factor)
 Right-hand side of the non-stiff part of the test problem.
 
void DoImplicitRHS (Field< TData, FieldState::Phys > &in, Field< TData, FieldState::Phys > &out, const TData &time, const TData &factor)
 Right-hand side of the stiff part of the test problem.
 
void DoProjection (Field< TData, FieldState::Phys > &in, Field< TData, FieldState::Phys > &out, const TData &time)
 Projection between timesteps, the identity for this test problem.
 

Protected Attributes

std::string m_meshName = ""
 
TData m_alpha = 0
 
TData m_beta = 0
 
Math::MathHelper m_math
 
Field< TData, FieldState::Phys > m_in
 
Field< TData, FieldState::Phys > m_out
 
Field< TData, FieldState::Phys > m_expected
 
ExpListSharedPtr m_expList
 
SessionReaderSharedPtr m_session
 

Detailed Description

template<typename TData>
class TestTimeOps< TData >

A test fixture that integrates the scalar test problem du/dt = (alpha + beta) u in time, alpha being the non-stiff factor carried by the explicit part of a scheme and beta the stiff factor carried by the implicit part. The exact solution u = e^((alpha + beta) t) with u(t=0) = 1 gives the reference the observed order of accuracy is measured against.

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 79 of file TestTimeOps.hpp.

Constructor & Destructor Documentation

◆ TestTimeOps()

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

◆ ~TestTimeOps()

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

Member Function Documentation

◆ CheckOrderOfAccuracy()

template<typename TData >
bool TestTimeOps< TData >::CheckOrderOfAccuracy ( const std::string  scheme,
const std::string  variant,
const unsigned int  order,
const std::vector< TData >  freeParams = {} 
)
inline

Integrate the test problem over a sequence of timesteps and check the order the error converges at against the order claimed by the scheme.

Definition at line 187 of file TestTimeOps.hpp.

190 {})
191 {
192 // The timestep ladder is test configuration, not field data: keeping
193 // it double makes final_time/dt land exactly on an integer step count
194 // whatever TData is. At float, 0.5f/0.001f is 499.99997, which
195 // truncates to 499 and stops the integration a step short.
196 const double final_time = 0.5;
197
198 std::vector<double> timesteps;
199
200 if (order <= 2)
201 {
202 timesteps = {0.1, 0.05, 0.025, 0.01, 0.005, 0.001, 0.0005};
203 }
204 else if (order <= 3)
205 {
206 timesteps = {0.1, 0.05, 0.025, 0.01, 0.005, 0.001};
207 }
208 else if (order == 4)
209 {
210 timesteps = {0.1, 0.05, 0.02, 0.01, 0.005};
211 }
212 else if (order == 5)
213 {
214 if (scheme == "ImplicitSDC" || scheme == "IMEXSDC" ||
215 scheme == "ImplicitGEM" || scheme == "IMEXGEM")
216 {
217 timesteps = {0.1, 0.05, 0.02, 0.01, 0.005};
218 }
219 else
220 {
221 timesteps = {0.2, 0.1, 0.05, 0.025};
222 }
223 }
224 else
225 {
226 if (scheme == "IMEXSDC")
227 {
228 timesteps = {0.1, 0.05, 0.02, 0.01, 0.005};
229 }
230 else
231 {
232 timesteps = {0.4, 0.2, 0.1};
233 }
234 }
235
236 std::vector<TData> errors;
237
238 for (double dt : timesteps)
239 {
240 // Change timestep in m_session to propagate to TimeOp at runtime
241 m_session->SetParameter("TimeStep", dt);
242
243 // Run simulation
244 RunTestCase(scheme, variant, order, freeParams, final_time / dt);
245
246 // Compute error at final time (L2 norm; adapt as needed)
248 TData error = std::sqrt(m_math.l2norm(m_out) / m_math.l2norm(m_in));
249 errors.push_back(error);
250 }
251
252 // Compute observed order of accuracy from (dt, error) pairs
253 std::vector<TData> orders;
254 for (size_t i = 0; i < timesteps.size() - 1; ++i)
255 {
256 TData order = log(errors[i] / errors[i + 1]) /
257 log(timesteps[i] / timesteps[i + 1]);
258 orders.push_back(order);
259 }
260
261 // Use last observed order
262 TData observedOrder = orders.back();
263
264 // Check if observed order is close to expected order (within tolerance)
265 if (std::isnan(observedOrder) || std::isinf(observedOrder) ||
266 observedOrder < 0.9 * order) // Choose tolerance
267 {
268 std::cerr << "Order of accuracy test failed! Observed: "
269 << observedOrder << ", Expected: " << order << std::endl;
270 return false;
271 }
272 else
273 {
274 std::cout << "Order of accuracy verified: " << observedOrder
275 << std::endl;
276 return true;
277 }
278 }
LIB_MATH_EXPORT T::value_type l2norm(T &, const std::string &execSpace="")
LIB_MATH_EXPORT void sub(T &x, T &y, T &z, const std::string &execSpace="")
SessionReaderSharedPtr m_session
Math::MathHelper m_math
Field< TData, FieldState::Phys > m_expected
Field< TData, FieldState::Phys > m_in
void RunTestCase(const std::string scheme, const std::string variant, const unsigned int order, const std::vector< TData > freeParams, const unsigned int numsteps)
Integrate the test problem for numsteps steps of the scheme given, leaving the result in the output f...
Field< TData, FieldState::Phys > m_out
scalarT< T > log(scalarT< T > in)
Definition scalar.hpp:309

◆ Configure()

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

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

Definition at line 89 of file TestTimeOps.hpp.

90 {
91 SetSession();
92 SetExpList();
93 SetFixture();
94 }
void SetExpList()
void SetSession()
Create the session from the mesh named by the derived fixture and the execution space given on the co...
void SetFixture()

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

◆ DoExplicitRHS()

template<typename TData >
void TestTimeOps< TData >::DoExplicitRHS ( Field< TData, FieldState::Phys > &  in,
Field< TData, FieldState::Phys > &  out,
const TData &  time,
const TData &  factor 
)
inlineprotected

Right-hand side of the non-stiff part of the test problem.

Definition at line 357 of file TestTimeOps.hpp.

360 {
361 // Multiply solution by factor
362 m_math.mul(m_alpha * factor, in, out);
363 }
LIB_MATH_EXPORT void mul(const typename T::value_type alpha, T &x, T &y, const std::string &execSpace="")

References TestTimeOps< TData >::m_alpha, TestTimeOps< TData >::m_math, and Nektar::Math::MathHelper::mul().

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

◆ DoImplicitRHS()

template<typename TData >
void TestTimeOps< TData >::DoImplicitRHS ( Field< TData, FieldState::Phys > &  in,
Field< TData, FieldState::Phys > &  out,
const TData &  time,
const TData &  factor 
)
inlineprotected

Right-hand side of the stiff part of the test problem.

Definition at line 368 of file TestTimeOps.hpp.

371 {
372 // Multiply solution by factor
373 m_math.mul(m_beta * factor, in, out);
374 }

References TestTimeOps< TData >::m_beta, TestTimeOps< TData >::m_math, and Nektar::Math::MathHelper::mul().

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

◆ DoLHS()

template<typename TData >
void TestTimeOps< TData >::DoLHS ( Field< TData, FieldState::Phys > &  in,
Field< TData, FieldState::Phys > &  out,
const TData &  time,
const TData &  lambda 
)
inlineprotected

Implicit solve of the stiff part of the test problem.

Definition at line 343 of file TestTimeOps.hpp.

346 {
347 // Factor for implicit/stiff part of analytic test problem
348 auto factor = 1.0 / (1.0 - lambda * m_beta);
349
350 // Multiply extrapolated rhs
351 m_math.mul(factor, in, out);
352 }

References TestTimeOps< TData >::m_beta, TestTimeOps< TData >::m_math, and Nektar::Math::MathHelper::mul().

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

◆ DoProjection()

template<typename TData >
void TestTimeOps< TData >::DoProjection ( Field< TData, FieldState::Phys > &  in,
Field< TData, FieldState::Phys > &  out,
const TData &  time 
)
inlineprotected

Projection between timesteps, the identity for this test problem.

Definition at line 379 of file TestTimeOps.hpp.

382 {
383 // Multiply solution by factor
384 m_math.mul(1.0, in, out);
385 }

References TestTimeOps< TData >::m_math, and Nektar::Math::MathHelper::mul().

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

◆ ExpectedSolution()

template<typename TData >
void TestTimeOps< TData >::ExpectedSolution ( const double  final_time)
inline

Fill the expected field with the analytic solution at final_time.

Definition at line 171 of file TestTimeOps.hpp.

172 {
173 // We solve the analytic problem du/dt = \alpha u + \beta u,
174 // where \alpha is mild parameter leading to the explicit part
175 // and \beta is a stiff parameter leading to the implicit part.
176 // The solution is u = e^((\alpha + \beta)*t) and u(t=0) = 1.0
177 m_expected.template Copy<NektarSpaces::HostSpace>(m_in);
178 m_math.mul(exp((m_alpha + m_beta) * final_time), m_expected,
179 m_expected);
180 }

References TestTimeOps< TData >::m_alpha, TestTimeOps< TData >::m_beta, TestTimeOps< TData >::m_expected, TestTimeOps< TData >::m_in, TestTimeOps< TData >::m_math, and Nektar::Math::MathHelper::mul().

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

◆ RunTestCase()

template<typename TData >
void TestTimeOps< TData >::RunTestCase ( const std::string  scheme,
const std::string  variant,
const unsigned int  order,
const std::vector< TData >  freeParams,
const unsigned int  numsteps 
)
inline

Integrate the test problem for numsteps steps of the scheme given, leaving the result in the output field.

Definition at line 142 of file TestTimeOps.hpp.

146 {
147 // Copy m_in to m_out since operator uses apply with inout type
148 m_out.template Copy<NektarSpaces::HostSpace>(m_in);
149
150 // Initialise Time-stepping operator
151 auto op = TimeOp<TData>::Create(m_expList, m_session->GetVariables(),
152 scheme, order, variant, freeParams);
153 op->DefineExplicitRhs(&TestTimeOps::DoExplicitRHS, this);
154 op->DefineImplicitRhs(&TestTimeOps::DoImplicitRHS, this);
155 op->DefineImplicit(&TestTimeOps::DoLHS, this);
156 op->DefineProjection(&TestTimeOps::DoProjection, this);
157
158 // Initialise timestepping operator
159 // Loop all steps
160 while (op->GetStep() < numsteps)
161 {
162 // Evolve PDE for one timestep
163 op->Apply(m_out);
164 }
165 }
void DoExplicitRHS(Field< TData, FieldState::Phys > &in, Field< TData, FieldState::Phys > &out, const TData &time, const TData &factor)
Right-hand side of the non-stiff part of the test problem.
void DoImplicitRHS(Field< TData, FieldState::Phys > &in, Field< TData, FieldState::Phys > &out, const TData &time, const TData &factor)
Right-hand side of the stiff part of the test problem.
ExpListSharedPtr m_expList
void DoProjection(Field< TData, FieldState::Phys > &in, Field< TData, FieldState::Phys > &out, const TData &time)
Projection between timesteps, the identity for this test problem.
void DoLHS(Field< TData, FieldState::Phys > &in, Field< TData, FieldState::Phys > &out, const TData &time, const TData &lambda)
Implicit solve of the stiff part of the test problem.

References TestTimeOps< TData >::DoExplicitRHS(), TestTimeOps< TData >::DoImplicitRHS(), TestTimeOps< TData >::DoLHS(), TestTimeOps< TData >::DoProjection(), TestTimeOps< TData >::m_expList, TestTimeOps< TData >::m_in, TestTimeOps< TData >::m_out, and TestTimeOps< TData >::m_session.

◆ SetExpList()

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

Definition at line 321 of file TestTimeOps.hpp.

322 {
326 m_expList->SetDataWarehouse();
327 }
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, TestTimeOps< TData >::m_expList, TestTimeOps< TData >::m_session, and Nektar::SpatialDomains::MeshGraphIO::Read().

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

◆ SetFixture()

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

Definition at line 329 of file TestTimeOps.hpp.

330 {
331 const unsigned int ncomp = m_session->GetVariables().size();
332 auto blockAttr = GetBlockAttributes<TData, FieldState::Phys>(m_expList);
333
334 m_in = Field<TData, FieldState::Phys>("f_in", blockAttr, ncomp, 1);
335 m_out = Field<TData, FieldState::Phys>("f_out", blockAttr, ncomp, 1);
336 m_expected =
337 Field<TData, FieldState::Phys>("f_expected", blockAttr, ncomp, 1);
338 }
A Field represents expansion data to be operated on.

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

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

◆ SetSession()

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

Create the session from the mesh named by the derived fixture and the execution space given on the command line.

Definition at line 297 of file TestTimeOps.hpp.

298 {
299 std::string execStr(GlobalConfiguration::ExecStr());
300
301 BOOST_TEST_MESSAGE("Creating input and output fields");
302
303 // Construct a fake command-line argument array to be fed to
304 // Session::Reader::CreateInstance. The first element stands for
305 // the name of the executable which, in our case, doesn't matter.
306 int argc = 3;
307 char **argv = new char *[argc];
308 argv[0] = strdup("exe_name");
309 argv[1] = strdup(m_meshName.data());
310 argv[2] = strdup(("--opExecSpace=" + execStr).c_str());
311
313
314 for (int i = 0; i < argc; ++i)
315 {
316 free(argv[i]);
317 }
318 delete[] argv;
319 }
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
std::string m_meshName

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

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

◆ SetTestCase()

template<typename TData >
void TestTimeOps< TData >::SetTestCase ( const TData  alpha,
const TData  beta 
)
inline

Set the initial condition and the analytic solution it is expected to reach at the final time.

Parameters
alphanon-stiff factor, integrated by the explicit part
betastiff factor, integrated by the implicit part

Definition at line 103 of file TestTimeOps.hpp.

104 {
105 // Initialise math kernel
106 std::string execName = Operator<TData>::GetOpExecSpace(m_session);
107 m_math = Math::MathHelper(execName);
108
109 // Set initial value, laid out as ToVector() returns it so that
110 // CopyVector() interleaves it and leaves the padding zero.
111 std::vector<TData> in;
112 for (unsigned int nc = 0; nc < m_in.GetNumComponents(); ++nc)
113 {
114 for (auto &block : m_in.GetBlocks())
115 {
116 for (size_t el = 0; el < block.GetNumElements(); ++el)
117 {
118 for (unsigned int phys = 0; phys < block.GetNumData();
119 ++phys)
120 {
121 in.push_back(1.0 + phys);
122 }
123 }
124 }
125 }
126 m_in.template CopyVector<NektarSpaces::HostSpace>(in);
127
128 // Parameters for analytic solution
129 m_alpha = alpha; // non-stiff factor
130 m_beta = beta; // stiff factor
131
132 const double final_time = 0.5;
133
134 // Compute expected solution
135 ExpectedSolution(final_time);
136 }
SpatialDomains::Geometry * el
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
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
void ExpectedSolution(const double final_time)
Fill the expected field with the analytic solution at final_time.
@ beta
Gauss Radau pinned at x=-1,.
Definition PointsType.h:59
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

References Nektar::LibUtilities::beta, el, TestTimeOps< TData >::ExpectedSolution(), Nektar::LibUtilities::Field< TData, TState >::GetBlocks(), Nektar::LibUtilities::Field< TData, TState >::GetNumComponents(), Nektar::MultiRegions::Operator< TData >::GetOpExecSpace(), TestTimeOps< TData >::m_alpha, TestTimeOps< TData >::m_beta, TestTimeOps< TData >::m_in, TestTimeOps< TData >::m_math, and TestTimeOps< TData >::m_session.

Member Data Documentation

◆ m_alpha

template<typename TData >
TData TestTimeOps< TData >::m_alpha = 0
protected

◆ m_beta

template<typename TData >
TData TestTimeOps< TData >::m_beta = 0
protected

◆ m_expected

template<typename TData >
Field<TData, FieldState::Phys> TestTimeOps< TData >::m_expected
protected

◆ m_expList

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

◆ m_in

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

◆ m_math

template<typename TData >
Math::MathHelper TestTimeOps< TData >::m_math
protected

◆ m_meshName

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

Definition at line 281 of file TestTimeOps.hpp.

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

◆ m_out

template<typename TData >
Field<TData, FieldState::Phys> TestTimeOps< TData >::m_out
protected

◆ m_session

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