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::Euler< Vector, SolverMonitor > Class Template Reference

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

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

Inheritance diagram for TNL::Solvers::ODE::Euler< Vector, SolverMonitor >:
Inheritance graph
[legend]
Collaboration diagram for TNL::Solvers::ODE::Euler< Vector, SolverMonitor >:
Collaboration graph
[legend]

Public Types

using DeviceType = typename Vector::DeviceType
 Device where the solver is supposed to be executed.
 
using DofVectorType = TNL::Containers::Vector< RealType, DeviceType, IndexType >
 Alias for type of unknown variable \( \vec x \). More...
 
using IndexType = typename Vector::IndexType
 Type for indexing.
 
using RealType = typename Vector::RealType
 Type of floating-point arithemtics.
 
using SolverMonitorType = SolverMonitor
 Type of object used for monitoring the convergence. More...
 
using VectorType = Vector
 Type of unknown variable \( \vec x \).
 
- Public Types inherited from TNL::Solvers::ODE::ExplicitSolver< Real, Index, SolverMonitor >
using IndexType = Index
 Indexing type.
 
using RealType = Real
 Type of the floating-point arithmetics.
 
using SolverMonitorType = SolverMonitor
 Type of the monitor of the convergence of the solver.
 
- Public Types inherited from TNL::Solvers::IterativeSolver< Real, Index, SolverMonitor >
using SolverMonitorType = SolverMonitor
 Type of an object used for monitoring of the convergence.
 

Public Member Functions

 Euler ()=default
 Default constructor.
 
const RealTypegetCourantNumber () const
 Getter for the Courant number. More...
 
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 >
bool solve (VectorType &u, RHSFunction &&f)
 Solve ODE given by a lambda function. More...
 
- Public Member Functions inherited from TNL::Solvers::ODE::ExplicitSolver< Real, Index, SolverMonitor >
 ExplicitSolver ()=default
 Default constructor.
 
bool checkNextIteration ()
 Checks if the solver is allowed to do the next iteration. More...
 
const RealTypegetMaxTau () const
 Getter of maximal value of the time step.
 
const RealTypegetStopTime () const
 Getter of the time where the evolution computation shall by stopped.
 
const RealTypegetTau () const
 Getter of the time step used for the computation.
 
const RealTypegetTime () const
 Getter of the current time of the evolution computed by the solver.
 
void refreshSolverMonitor (bool force=false)
 This method refreshes the solver monitor. More...
 
void setMaxTau (const RealType &maxTau)
 Setter of maximal value of the time step. More...
 
void setStopTime (const RealType &stopTime)
 Setter of the time where the evolution computation shall by stopped.
 
void setTau (const RealType &tau)
 Setter of the time step used for the computation. More...
 
void setTestingMode (bool testingMode)
 
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. More...
 
- Public Member Functions inherited from TNL::Solvers::IterativeSolver< Real, Index, SolverMonitor >
 IterativeSolver ()=default
 Default constructor.
 
bool checkConvergence ()
 Checks whether the convergence occurred already. More...
 
bool checkNextIteration ()
 Checks if the solver is allowed to do the next iteration. More...
 
const RealgetConvergenceResidue () const
 Gets the the convergence threshold. More...
 
const RealgetDivergenceResidue () const
 Gets the limit for the divergence criterion. More...
 
const Index & getIterations () const
 Gets the number of iterations performed by the solver so far. More...
 
const Index & getMaxIterations () const
 Gets the maximal number of iterations the solver is allowed to perform. More...
 
const Index & getMinIterations () const
 Gets the minimal number of iterations the solver is supposed to do. More...
 
const RealgetResidue () const
 Gets the residue reached at the current iteration. More...
 
bool nextIteration ()
 Proceeds to the next iteration. More...
 
void resetIterations ()
 Sets the the number of the current iterations to zero.
 
void setConvergenceResidue (const Real &convergenceResidue)
 Sets the threshold for the convergence. More...
 
void setDivergenceResidue (const Real &divergenceResidue)
 Sets the residue limit for the divergence criterion. More...
 
void setMaxIterations (const Index &maxIterations)
 Sets the maximal number of iterations the solver is allowed to perform. More...
 
void setMinIterations (const Index &minIterations)
 Sets the minimal number of iterations the solver is supposed to do. More...
 
void setRefreshRate (const Index &refreshRate)
 Sets the refresh rate (in milliseconds) for the solver monitor. More...
 
void setResidue (const Real &residue)
 Sets the residue reached at the current iteration. More...
 
void setSolverMonitor (SolverMonitorType &solverMonitor)
 Sets the solver monitor object. 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::ExplicitSolver< Real, Index, SolverMonitor >
static void configSetup (Config::ConfigDescription &config, const String &prefix="")
 This method defines configuration entries for setup of the iterative solver. More...
 
- Static Public Member Functions inherited from TNL::Solvers::IterativeSolver< Real, Index, SolverMonitor >
static void configSetup (Config::ConfigDescription &config, const String &prefix="")
 This method defines configuration entries for setup of the iterative solver. More...
 

Protected Attributes

DofVectorType _k1
 
RealType courantNumber = 0.0
 
- Protected Attributes inherited from TNL::Solvers::ODE::ExplicitSolver< Real, Index, SolverMonitor >
RealType maxTau = std::numeric_limits< RealType >::max()
 
bool stopOnSteadyState = false
 
RealType stopTime
 
RealType tau = 0.0
 
bool testingMode = false
 
RealType time = 0.0
 
- Protected Attributes inherited from TNL::Solvers::IterativeSolver< Real, Index, SolverMonitor >
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
 
std::ofstream residualHistoryFile
 
String residualHistoryFileName = ""
 
SolverMonitorsolverMonitor = nullptr
 

Detailed Description

template<typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType >>
class TNL::Solvers::ODE::Euler< Vector, SolverMonitor >

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 \vec u}{dt} = \vec f( t, \vec u) \text{ on } (0,T) \)

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

It is supposed to be used when the unknown \( \vec x \in R^n \) is expressed by a Containers::Vector.

For problems where \( \vec x\) is represented by TNL::Containers::StaticVector, see TNL::Solvers::ODE::StaticEuler<Containers::StaticVector<Size_,Real>>. For problems where \( x\) is represented by floating-point number, see TNL::Solvers::ODE::StaticEuler.

The following example demonstrates the use the solvers:

1#include <iostream>
2#include <TNL/FileName.h>
3#include <TNL/Containers/Vector.h>
4#include <TNL/Solvers/ODE/Euler.h>
5#include "write.h"
6
7using Real = double;
8using Index = int;
9
10template< typename Device >
11void solveHeatEquation( const char* file_name )
12{
14 using VectorView = typename Vector::ViewType;
15 using ODESolver = TNL::Solvers::ODE::Euler< Vector >;
16
17 /***
18 * Parameters of the discretisation
19 */
20 const Real final_t = 0.05;
21 const Real output_time_step = 0.005;
22 const Index n = 41;
23 const Real h = 1.0 / ( n - 1 );
24 const Real tau = 0.1 * h * h;
25 const Real h_sqr_inv = 1.0 / ( h * h );
26
27 /***
28 * Initial condition
29 */
30 Vector u( n );
31 u.forAllElements( [=] __cuda_callable__ ( Index i, Real& value ) {
32 const Real x = i * h;
33 if( x >= 0.4 && x <= 0.6 )
34 value = 1.0;
35 else value = 0.0;
36 } );
37 std::fstream file;
38 file.open( file_name, std::ios::out );
39 write( file, u, n, h, ( Real ) 0.0 );
40
41 /***
42 * Setup of the solver
43 */
44 ODESolver solver;
45 solver.setTau( tau );
46 solver.setTime( 0.0 );
47
48 /***
49 * Time loop
50 */
51 while( solver.getTime() < final_t )
52 {
53 solver.setStopTime( TNL::min( solver.getTime() + output_time_step, final_t ) );
54 auto f = [=] __cuda_callable__ ( Index i, const VectorView& u, VectorView& fu ) mutable {
55 if( i == 0 || i == n-1 ) // boundary nodes -> boundary conditions
56 fu[ i ] = 0.0;
57 else // interior nodes -> approximation of the second derivative
58 fu[ i ] = h_sqr_inv * ( u[ i - 1 ] - 2.0 * u[ i ] + u[ i + 1 ] );
59 };
60 auto time_stepping = [=] ( const Real& t, const Real& tau, const VectorView& u, VectorView& fu ) {
62 solver.solve( u, time_stepping );
63 write( file, u, n, h, solver.getTime() ); // write the current state to a file
64 }
65}
66
67int main( int argc, char* argv[] )
68{
69 TNL::String file_name( argv[ 1 ] );
70 file_name += "/ODESolver-HeatEquationExample-result.out";
71
72 solveHeatEquation< TNL::Devices::Host >( file_name.getString() );
73#ifdef __CUDACC__
74 solveHeatEquation< TNL::Devices::Cuda >( file_name.getString() );
75#endif
76}
#define __cuda_callable__
Definition: CudaCallable.h:22
Vector extends Array with algebraic operations.
Definition: Vector.h:40
Definition: Real.h:17
Solver of ODEs with the first order of accuracy.
Definition: Euler.h:46
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
Vectoris type of vector storing \( \vec x \in R^n \), mostly TNL::Containers::Vector or TNL::Containers::VectorView.

Member Typedef Documentation

◆ DofVectorType

template<typename Vector , typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType >>
using TNL::Solvers::ODE::Euler< Vector, SolverMonitor >::DofVectorType = TNL::Containers::Vector< RealType, DeviceType, IndexType >

Alias for type of unknown variable \( \vec x \).

Note that VectorType can be TNL::Containers::VectorView but DofVectorType is always TNL::Containers::Vector.

◆ SolverMonitorType

template<typename Vector , typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType >>
using TNL::Solvers::ODE::Euler< Vector, SolverMonitor >::SolverMonitorType = SolverMonitor

Type of object used for monitoring the convergence.

Can be TNL::Solvers::IterativeSolverMonitor.

Member Function Documentation

◆ configSetup()

template<typename Vector , typename SolverMonitor >
void TNL::Solvers::ODE::Euler< Vector, SolverMonitor >::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 Vector , typename SolverMonitor >
auto TNL::Solvers::ODE::Euler< Vector, SolverMonitor >::getCourantNumber

Getter for the Courant number.

Returns
the Courant number.

◆ setCourantNumber()

template<typename Vector , typename SolverMonitor >
void TNL::Solvers::ODE::Euler< Vector, SolverMonitor >::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 Vector , typename SolverMonitor >
bool TNL::Solvers::ODE::Euler< Vector, SolverMonitor >::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 Vector , typename SolverMonitor >
template<typename RHSFunction >
bool TNL::Solvers::ODE::Euler< Vector, SolverMonitor >::solve ( VectorType u,
RHSFunction &&  f 
)

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 VectorType& u, VectorType& fu ) {...}
Vector VectorType
Type of unknown variable .
Definition: Euler.h:66
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.
Returns
true if steady state solution has been reached, false otherwise.

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