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

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

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

Inheritance diagram for TNL::Solvers::ODE::StaticEuler< Real >:
Inheritance graph
[legend]
Collaboration diagram for TNL::Solvers::ODE::StaticEuler< 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 = int
 Indexing type.
 
using RealType = Real
 Type of the floating-point arithmetics or static vector.
 

Public Member Functions

__cuda_callable__ StaticEuler ()=default
 Default constructor.
 
__cuda_callable__ const RealTypegetCourantNumber () const
 Getter for the Courant number. More...
 
__cuda_callable__ void setCourantNumber (const RealType &c)
 This method sets the Courant number in the CFL condition. More...
 
bool setup (const Config::ParameterContainer &parameters, const String &prefix="")
 Method for setup of the explicit solver based on configuration parameters. More...
 
template<typename RHSFunction , typename... Args>
__cuda_callable__ bool solve (VectorType &u, RHSFunction &&f, Args... args)
 Solve ODE given by a lambda function. More...
 
- 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. More...
 
__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. More...
 
__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. More...
 
__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 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. More...
 
__cuda_callable__ bool checkNextIteration ()
 Checks if the solver is allowed to do the next iteration. More...
 
__cuda_callable__ const RealgetConvergenceResidue () const
 Gets the the convergence threshold. More...
 
__cuda_callable__ const RealgetDivergenceResidue () const
 Gets the limit for the divergence criterion. More...
 
__cuda_callable__ const Index & getIterations () const
 Gets the number of iterations performed by the solver so far. More...
 
__cuda_callable__ const Index & getMaxIterations () const
 Gets the maximal number of iterations the solver is allowed to perform. More...
 
__cuda_callable__ const Index & getMinIterations () const
 Gets the minimal number of iterations the solver is supposed to do. More...
 
__cuda_callable__ const RealgetResidue () const
 Gets the residue reached at the current iteration. More...
 
__cuda_callable__ bool nextIteration ()
 Proceeds to the next iteration. More...
 
__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. More...
 
__cuda_callable__ void setDivergenceResidue (const Real &divergenceResidue)
 Sets the residue limit for the divergence criterion. More...
 
__cuda_callable__ void setMaxIterations (const Index &maxIterations)
 Sets the maximal number of iterations the solver is allowed to perform. More...
 
__cuda_callable__ void setMinIterations (const Index &minIterations)
 Sets the minimal number of iterations the solver is supposed to do. More...
 
__cuda_callable__ void setResidue (const Real &residue)
 Sets the residue reached at the current iteration. More...
 
bool setup (const Config::ParameterContainer &parameters, const String &prefix="")
 Method for setup of the iterative solver based on configuration parameters. More...
 

Static Public Member Functions

static void configSetup (Config::ConfigDescription &config, const String &prefix="")
 Static method for setup of configuration parameters. More...
 
- Static Public Member Functions inherited from TNL::Solvers::ODE::StaticExplicitSolver< Real, int >
static void configSetup (Config::ConfigDescription &config, const 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 String &prefix="")
 This method defines configuration entries for setup of the iterative solver. More...
 

Protected Attributes

RealType courantNumber = 0.0
 
DofVectorType k1
 
- 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<typename Real>
class TNL::Solvers::ODE::StaticEuler< Real >

Solver of ODEs with the first order of accuracy.

This solver is based on the Euler method 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::StaticEuler<Containers::StaticVector<Size_,Real>>. For problems where \( \vec x\) is represented by TNL::Containers::Vector, or TNL::Containers::VectorView, see TNL::Solvers::ODE::Euler.

The following example demonstrates the use the solvers:

1#include <iostream>
2#include <TNL/Solvers/ODE/StaticEuler.h>
3
4using Real = double;
5
6int main( int argc, char* argv[] )
7{
9 const Real final_t = 10.0;
10 const Real tau = 0.001;
11 const Real output_time_step = 0.25;
12
13 ODESolver solver;
14 solver.setTau( tau );
15 solver.setTime( 0.0 );
16 Real u = 0.0;
17 while( solver.getTime() < final_t )
18 {
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 fu = t * sin( t );
22 };
23 solver.solve( u, f );
24 std::cout << solver.getTime() << " " << u << std::endl;
25 }
26}
Definition: Real.h:17
Solver of ODEs with the first order of accuracy.
Definition: StaticEuler.h:56
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 solveParallelODEs( const char* file_name )
11{
14 const Real final_t = 10.0;
15 const Real tau = 0.001;
16 const Real output_time_step = 0.1;
17 const Real c_min = 1.0;
18 const Real c_max = 5.0;
19 const int c_vals = 5.0;
20 const Real c_step = ( c_max - c_min ) / ( c_vals - 1 );
21 const int output_time_steps = ceil( final_t / output_time_step ) + 1;
22
23 Vector results( output_time_steps * c_vals, 0.0 );
24 auto results_view = results.getView();
25 auto f = [=] __cuda_callable__ ( const Real& t, const Real& tau, const Real& u, Real& fu, const Real& c ) {
26 fu = t * sin( c * t );
27 };
28 auto solve = [=] __cuda_callable__ ( int idx ) mutable {
29 const Real c = c_min + idx * c_step;
30 ODESolver solver;
31 solver.setTau( tau );
32 solver.setTime( 0.0 );
33 Real u = 0.0;
34 int time_step( 1 );
35 results_view[ idx ] = u;
36 while( time_step < output_time_steps )
37 {
38 solver.setStopTime( TNL::min( solver.getTime() + output_time_step, final_t ) );
39 solver.solve( u, f, c );
40 results_view[ time_step++ * c_vals + idx ] = u;
41 }
42 };
44
45 std::fstream file;
46 file.open( file_name, std::ios::out );
47 for( int i = 0; i < c_vals; i++ )
48 {
49 file << "# c = " << c_min + i * c_step << std::endl;
50 for( int k = 0; k < output_time_steps;k++ )
51 file << k * output_time_step << " " << results.getElement( k * c_vals + i ) << std::endl;
52 file << std::endl;
53 }
54}
55
56int main( int argc, char* argv[] )
57{
58 TNL::String file_name( argv[ 1 ] );
59 file_name += "/StaticODESolver-SineParallelExample-result.out";
60 solveParallelODEs< TNL::Devices::Host >( file_name.getString() );
61#ifdef __CUDACC__
62 solveParallelODEs< TNL::Devices::Cuda >( file_name.getString() );
63#endif
64}
#define __cuda_callable__
Definition: CudaCallable.h:22
Vector extends Array with algebraic operations.
Definition: Vector.h:40
__cuda_callable__ bool solve(VectorType &u, RHSFunction &&f, Args... args)
Solve ODE given by a lambda function.
Definition: StaticEuler.hpp:54
Class for managing strings.
Definition: String.h:33
T open(T... args)
static void exec(Index start, Index end, Function f, FunctionArgs... args)
Static method for the execution of the loop.
Definition: ParallelFor.h:70
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::StaticEuler< Real >::configSetup ( Config::ConfigDescription config,
const 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.

◆ getCourantNumber()

template<typename Real >
__cuda_callable__ auto TNL::Solvers::ODE::StaticEuler< Real >::getCourantNumber

Getter for the Courant number.

Returns
the Courant number.

◆ setCourantNumber()

template<typename Real >
__cuda_callable__ void TNL::Solvers::ODE::StaticEuler< Real >::setCourantNumber ( const RealType c)

This method sets the Courant number in the CFL condition.

This method sets the constant \( C \) in the Courant–Friedrichs–Lewy condition. It means that

\[ \Delta t = \frac{C}{\| f( t,x )\|}, \]

.

if \( C > 0\). If \( C = 0 \) the time step stays fixed.

Parameters
cis the Courant number.

◆ setup()

template<typename Real >
bool TNL::Solvers::ODE::StaticEuler< Real >::setup ( const Config::ParameterContainer parameters,
const 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<typename Real >
template<typename RHSFunction , typename... Args>
__cuda_callable__ bool TNL::Solvers::ODE::StaticEuler< 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: