Template Numerical Library version\ main:1437bf49
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
TNL::Solvers::ODE::StaticMerson< Real > Class Template Reference

Solver of ODEs with the first order of accuracy. More...

#include <TNL/Solvers/ODE/StaticMerson.h>

Inheritance diagram for TNL::Solvers::ODE::StaticMerson< Real >:
Inheritance graph
[legend]
Collaboration diagram for TNL::Solvers::ODE::StaticMerson< Real >:
Collaboration graph
[legend]

Public Types

using DofVectorType = VectorType
 Alias for type of unknown variable \( x \).
 
using IndexType = int
 Type for indexing.
 
using RealType = Real
 Type of floating-point arithemtics.
 
using VectorType = Real
 Type of unknown variable \( x \).
 
- Public Types inherited from TNL::Solvers::ODE::StaticExplicitSolver< Real, int >
using IndexType
 Indexing type.
 
using RealType
 Type of the floating-point arithmetics or static vector.
 

Public Member Functions

__cuda_callable__ StaticMerson ()=default
 Default constructor.
 
__cuda_callable__ const RealTypegetAdaptivity () const
 Getter of the parameter controlling the adaptive choice of the integration time step.
 
__cuda_callable__ void setAdaptivity (const RealType &adaptivity)
 Setter of the parameter controlling the adaptive choice of the integration time step.
 
bool setup (const Config::ParameterContainer &parameters, const std::string &prefix="")
 Method for setup of the explicit solver based on configuration parameters.
 
template<typename RHSFunction , typename... Args>
__cuda_callable__ bool solve (VectorType &u, RHSFunction &&f, Args... args)
 Solve ODE given by a lambda function.
 
template<typename RHSFunction , typename... Args>
bool __cuda_callable__ solve (VectorType &u, RHSFunction &&rhsFunction, Args... args)
 
- Public Member Functions inherited from TNL::Solvers::ODE::StaticExplicitSolver< Real, int >
__cuda_callable__ StaticExplicitSolver ()=default
 Default constructor.
 
bool __cuda_callable__ checkNextIteration ()
 Checks if the solver is allowed to do the next iteration.
 
__cuda_callable__ const RealTypegetMaxTau () const
 Getter of maximal value of the time step.
 
__cuda_callable__ const RealTypegetStopTime () const
 Getter of the time where the evolution computation shall by stopped.
 
__cuda_callable__ const RealTypegetTau () const
 Getter of the time step used for the computation.
 
__cuda_callable__ const RealTypegetTime () const
 Getter of the current time of the evolution computed by the solver.
 
__cuda_callable__ void setMaxTau (const RealType &maxTau)
 Setter of maximal value of the time step.
 
__cuda_callable__ void setStopTime (const RealType &stopTime)
 Setter of the time where the evolution computation shall by stopped.
 
__cuda_callable__ void setTau (const RealType &tau)
 Setter of the time step used for the computation.
 
__cuda_callable__ void setTestingMode (bool testingMode)
 
__cuda_callable__ void setTime (const RealType &t)
 Settter of the current time of the evolution computed by the solver.
 
bool setup (const Config::ParameterContainer &parameters, const std::string &prefix="")
 Method for setup of the iterative solver based on configuration parameters.
 
- Public Member Functions inherited from TNL::Solvers::StaticIterativeSolver< Real, Index >
__cuda_callable__ StaticIterativeSolver ()=default
 Default constructor.
 
__cuda_callable__ bool checkConvergence ()
 Checks whether the convergence occurred already.
 
__cuda_callable__ bool checkNextIteration ()
 Checks if the solver is allowed to do the next iteration.
 
__cuda_callable__ const RealgetConvergenceResidue () const
 Gets the the convergence threshold.
 
__cuda_callable__ const RealgetDivergenceResidue () const
 Gets the limit for the divergence criterion.
 
__cuda_callable__ const Index & getIterations () const
 Gets the number of iterations performed by the solver so far.
 
__cuda_callable__ const Index & getMaxIterations () const
 Gets the maximal number of iterations the solver is allowed to perform.
 
__cuda_callable__ const Index & getMinIterations () const
 Gets the minimal number of iterations the solver is supposed to do.
 
__cuda_callable__ const RealgetResidue () const
 Gets the residue reached at the current iteration.
 
__cuda_callable__ bool nextIteration ()
 Proceeds to the next iteration.
 
__cuda_callable__ void resetIterations ()
 Sets the the number of the current iterations to zero.
 
__cuda_callable__ void setConvergenceResidue (const Real &convergenceResidue)
 Sets the threshold for the convergence.
 
__cuda_callable__ void setDivergenceResidue (const Real &divergenceResidue)
 Sets the residue limit for the divergence criterion.
 
__cuda_callable__ void setMaxIterations (const Index &maxIterations)
 Sets the maximal number of iterations the solver is allowed to perform.
 
__cuda_callable__ void setMinIterations (const Index &minIterations)
 Sets the minimal number of iterations the solver is supposed to do.
 
__cuda_callable__ void setResidue (const Real &residue)
 Sets the residue reached at the current iteration.
 
bool setup (const Config::ParameterContainer &parameters, const std::string &prefix="")
 Method for setup of the iterative solver based on configuration parameters.
 

Static Public Member Functions

static void configSetup (Config::ConfigDescription &config, const std::string &prefix="")
 Static method for setup of configuration parameters.
 
- Static Public Member Functions inherited from TNL::Solvers::ODE::StaticExplicitSolver< Real, int >
static void configSetup (Config::ConfigDescription &config, const std::string &prefix="")
 This method defines configuration entries for setup of the iterative solver.
 
- Static Public Member Functions inherited from TNL::Solvers::StaticIterativeSolver< Real, Index >
static void configSetup (Config::ConfigDescription &config, const std::string &prefix="")
 This method defines configuration entries for setup of the iterative solver.
 

Protected Attributes

RealType adaptivity = 0.00001
 
DofVectorType k1
 
DofVectorType k2
 
DofVectorType k3
 
DofVectorType k4
 
DofVectorType k5
 
DofVectorType kAux
 
- Protected Attributes inherited from TNL::Solvers::ODE::StaticExplicitSolver< Real, int >
RealType maxTau
 
bool stopOnSteadyState
 
RealType stopTime
 
RealType tau
 
bool testingMode
 
RealType time
 
- Protected Attributes inherited from TNL::Solvers::StaticIterativeSolver< Real, Index >
Real convergenceResidue = 1e-6
 
Index currentIteration = 0
 
Real currentResidue = 0
 
Real divergenceResidue = std::numeric_limits< Real >::max()
 
Index maxIterations = 1000000000
 
Index minIterations = 0
 
Index refreshRate = 1
 

Detailed Description

template<class Real>
class TNL::Solvers::ODE::StaticMerson< Real >

Solver of ODEs with the first order of accuracy.

This solver is based on the Runge-Kutta-Merson method with adaptive choice of the integration time step for solving of ordinary differential equations having the following form:

\( \frac{d u}{dt} = f( t, u) \text{ on } (0,T) \)

\( u( 0 ) = u_{ini} \).

It is supposed to be used when the unknown \( x \in R \) is expressed by a scalar, i.e. by a numeric type like double or float.

For problems where \( \vec x\) is represented by TNL::Containers::StaticVector, see TNL::Solvers::ODE::StaticMerson<Containers::StaticVector<Size_,Real>>. For problems where \( \vec x\) is represented by TNL::Containers::Vector, or TNL::Containers::VectorView, see TNL::Solvers::ODE::Merson.

The following example demonstrates the use the solvers:

1#include <iostream>
2#include <TNL/Solvers/ODE/StaticEuler.h>
3
4using Real = double;
5
6int
7main( int argc, char* argv[] )
8{
10 const Real final_t = 10.0;
11 const Real tau = 0.001;
12 const Real output_time_step = 0.25;
13
14 ODESolver solver;
15 solver.setTau( tau );
16 solver.setTime( 0.0 );
17 Real u = 0.0;
18 while( solver.getTime() < final_t ) {
19 solver.setStopTime( TNL::min( solver.getTime() + output_time_step, final_t ) );
20 auto f = []( const Real& t, const Real& tau, const Real& u, Real& fu )
21 {
22 fu = t * sin( t );
23 };
24 solver.solve( u, f );
25 std::cout << solver.getTime() << " " << u << std::endl;
26 }
27}
Definition Real.h:14
Solver of ODEs with the first order of accuracy.
Definition StaticEuler.h:52
T endl(T... args)

The result looks as follows:

0.001 0
0.002 1e-09
0.252 0.00526916
0.502 0.0409948
0.752 0.13364
1.002 0.302432
1.252 0.556612
1.502 0.893635
1.752 1.2985
2.002 1.74432
2.252 2.19409
2.502 2.60357
2.752 2.92506
3.002 3.11173
3.252 3.1222
3.502 2.92497
3.752 2.50232
4.002 1.85323
4.252 0.995174
4.502 -0.0355494
4.752 -1.18502
5.002 -2.38443
5.252 -3.55415
5.502 -4.60904
5.752 -5.46451
6.002 -6.04295
6.252 -6.28004
6.502 -6.1306
6.752 -5.57319
7.002 -4.61342
7.252 -3.28541
7.502 -1.65121
7.752 0.201757
8.002 2.16523
8.252 4.11644
8.502 5.92576
8.752 7.46532
9.002 8.61785
9.252 9.28537
9.502 9.3969
9.752 8.9147
10 7.84941

Since this variant of the Euler solver is static, it can be used even inside of GPU kernels and so combined with TNL::Algorithms::parallelFor as demonstrated by the following example:

1#include <iostream>
2#include <fstream>
3#include <TNL/Solvers/ODE/StaticEuler.h>
4#include <TNL/Containers/Vector.h>
5#include <TNL/Algorithms/parallelFor.h>
6
7using Real = double;
8
9template< typename Device >
10void
11solveParallelODEs( const char* file_name )
12{
15 const Real final_t = 10.0;
16 const Real tau = 0.001;
17 const Real output_time_step = 0.1;
18 const Real c_min = 1.0;
19 const Real c_max = 5.0;
20 const int c_vals = 5.0;
21 const Real c_step = ( c_max - c_min ) / ( c_vals - 1 );
22 const int output_time_steps = ceil( final_t / output_time_step ) + 1;
23
24 Vector results( output_time_steps * c_vals, 0.0 );
25 auto results_view = results.getView();
26 auto f = [ = ] __cuda_callable__( const Real& t, const Real& tau, const Real& u, Real& fu, const Real& c )
27 {
28 fu = t * sin( c * t );
29 };
30 auto solve = [ = ] __cuda_callable__( int idx ) mutable
31 {
32 const Real c = c_min + idx * c_step;
33 ODESolver solver;
34 solver.setTau( tau );
35 solver.setTime( 0.0 );
36 Real u = 0.0;
37 int time_step( 1 );
38 results_view[ idx ] = u;
39 while( time_step < output_time_steps ) {
40 solver.setStopTime( TNL::min( solver.getTime() + output_time_step, final_t ) );
41 solver.solve( u, f, c );
42 results_view[ time_step++ * c_vals + idx ] = u;
43 }
44 };
45 TNL::Algorithms::parallelFor< Device >( 0, c_vals, solve );
46
47 std::fstream file;
48 file.open( file_name, std::ios::out );
49 for( int i = 0; i < c_vals; i++ ) {
50 file << "# c = " << c_min + i * c_step << std::endl;
51 for( int k = 0; k < output_time_steps; k++ )
52 file << k * output_time_step << " " << results.getElement( k * c_vals + i ) << std::endl;
53 file << std::endl;
54 }
55}
56
57int
58main( int argc, char* argv[] )
59{
60 TNL::String file_name( argv[ 1 ] );
61 file_name += "/StaticODESolver-SineParallelExample-result.out";
62 solveParallelODEs< TNL::Devices::Host >( file_name.getString() );
63#ifdef __CUDACC__
64 solveParallelODEs< TNL::Devices::Cuda >( file_name.getString() );
65#endif
66}
#define __cuda_callable__
Definition Macros.h:49
Vector extends Array with algebraic operations.
Definition Vector.h:36
Class for managing strings.
Definition String.h:30
T open(T... args)
Template Parameters
Realis floating point number type, it is type of \( x \) in this case.

Member Function Documentation

◆ configSetup()

template<typename Real >
void TNL::Solvers::ODE::StaticMerson< Real >::configSetup ( Config::ConfigDescription & config,
const std::string & prefix = "" )
static

Static method for setup of configuration parameters.

Parameters
configis the config description.
prefixis the prefix of the configuration parameters for this solver.

◆ getAdaptivity()

template<typename Real >
__cuda_callable__ const Real & TNL::Solvers::ODE::StaticMerson< Real >::getAdaptivity ( ) const

Getter of the parameter controlling the adaptive choice of the integration time step.

Returns
the current value of the parameter controlling the adaptive choice of integration time step.

◆ setAdaptivity()

template<typename Real >
void __cuda_callable__ TNL::Solvers::ODE::StaticMerson< Real >::setAdaptivity ( const RealType & adaptivity)

Setter of the parameter controlling the adaptive choice of the integration time step.

The smaller the parammeter is the smaller the intergation time step tends to be. Reasonable values for this parameters are approximately from interval \( [10^{-12},10^{-2}] \).

Parameters
adaptivitynew value of the parameter controlling the adaptive choice of integration time step.

◆ setup()

template<typename Real >
bool TNL::Solvers::ODE::StaticMerson< Real >::setup ( const Config::ParameterContainer & parameters,
const std::string & prefix = "" )

Method for setup of the explicit solver based on configuration parameters.

Parameters
parametersis the container for configuration parameters.
prefixis the prefix of the configuration parameters for this solver.
Returns
true if the parameters where parsed successfully.
false if the method did not succeed to read the configuration parameters.

◆ solve()

template<class Real >
template<typename RHSFunction , typename... Args>
__cuda_callable__ bool TNL::Solvers::ODE::StaticMerson< Real >::solve ( VectorType & u,
RHSFunction && f,
Args... args )

Solve ODE given by a lambda function.

Template Parameters
RHSFunctionis type of a lambda function representing the right-hand side of the ODE system. The definition of the lambda function reads as:
auto f = [=] ( const Real& t, const Real& tau, const Real& u, Real& fu, Args... args ) {...}
where t is the current time of the evolution, tau is the current time step, u is the solution at the current time, fu is variable/static vector into which the lambda function is suppsed to evaluate the function \( f(t, \vec x) \) at the current time \( t \).
Parameters
uis a variable/static vector representing the solution of the ODE system at current time.
fis the lambda function representing the right-hand side of the ODE system.
argsare user define arguments which are passed to the lambda function f.
Returns
true if steady state solution has been reached, false otherwise.

The documentation for this class was generated from the following files: