Discontinuous Galerkin Library
#include "dg/algorithm.h"
dg::Simpsons< ContainerType > Struct Template Reference

Time integration based on Simpson's rule. More...

Public Types

using value_type = get_value_type< ContainerType >
 the value type of the time variable (float or double) More...
 
using container_type = ContainerType
 the type of the vector class in use More...
 

Public Member Functions

 Simpsons (unsigned order=3)
 Set integration order without initializing values. More...
 
void set_order (unsigned order)
 Set integration order without initializing values. More...
 
unsigned get_order () const
 Access current integration order. More...
 
void init (value_type t0, const ContainerType &u0)
 Initialize the left-side boundary of the integration. More...
 
void flush ()
 Reset the integral to zero and the last (t,u) pair in the add function as the new left-side. More...
 
void add (value_type t_new, const ContainerType &u_new)
 Add a new (t,u) pair to the time integral. More...
 
const ContainerType & get_integral () const
 Access the current value of the time integral. More...
 
std::array< value_type, 2 > get_boundaries () const
 Access the left and right boundary in time. More...
 

Detailed Description

template<class ContainerType>
struct dg::Simpsons< ContainerType >

Time integration based on Simpson's rule.

The intention of this class is to provide a means to continuously integrate a sample of \(( t_i, u_i)\) pairs that become available one after the other (e.g. from the integration of an ODE) and approximate

\[ \int_{t_0}^T u(t) dt \]

Note
The algorithm simply integrates the Lagrange-polynomial through up to three data points. For equidistant Data points this equals either the Trapezoidal (linear) or the Simpson's rule (quadratic)
See also
For an explanation of Simpson's rule: https://en.wikipedia.org/wiki/Simpson%27s_rule

The class works by first calling the init function to set the left-side boundary and then adding values as they become available.

// Let us construct an artificial list of data points
unsigned N=20;
dg::Grid1d g1d( 0, M_PI/2., 3, N );
dg::HVec values = dg::evaluate( cos, g1d);
//Init the left side boundary
simpsons.init( 0., 1.);
//Now add (time, value) pairs to the integral
for ( unsigned i=0; i<g1d.size(); i++)
simpsons.add( times[i], values[i]);
//Add a last point since the dg grid is cell-centered
simpsons.add( M_PI/2., 0.);
//Ask for the integral
double integral = simpsons.get_integral();
std::cout << "Error Simpsons is "<<fabs(integral-1.)<<std::endl;
#define M_PI
M_PI is non-standard ... so MSVC complains.
Definition: functors.h:6
static DG_DEVICE double cooX1d(double x)
Definition: functions.h:38
thrust::host_vector< real_type > evaluate(UnaryOp f, const RealGrid1d< real_type > &g)
Evaluate a 1d function on grid coordinates.
Definition: evaluation.h:67
thrust::host_vector< double > HVec
Host Vector.
Definition: typedefs.h:19
Time integration based on Simpson's rule.
Definition: simpsons.h:33
void init(value_type t0, const ContainerType &u0)
Initialize the left-side boundary of the integration.
Definition: simpsons.h:59
void add(value_type t_new, const ContainerType &u_new)
Add a new (t,u) pair to the time integral.
Definition: simpsons.h:83
const ContainerType & get_integral() const
Access the current value of the time integral.
Definition: simpsons.h:121
Template Parameters
ContainerTypeAny class for which a specialization of TensorTraits exists and which fulfills the requirements of the there defined data and execution policies derived from AnyVectorTag and AnyPolicyTag. Among others
  • dg::HVec (serial), dg::DVec (cuda / omp), dg::MHVec (mpi + serial) or dg::MDVec (mpi + cuda / omp)
  • std::vector<dg::DVec> (vector of shared device vectors), std::array<double, 4> (array of 4 doubles) or std::map < std::string, dg::DVec> ( a map of named vectors)
  • double (scalar) and other primitive types ...
If there are several ContainerTypes in the argument list, then TensorTraits must exist for all of them
See also
See The dg dispatch system for a detailed explanation of our type dispatch system

Member Typedef Documentation

◆ container_type

template<class ContainerType >
using dg::Simpsons< ContainerType >::container_type = ContainerType

the type of the vector class in use

◆ value_type

template<class ContainerType >
using dg::Simpsons< ContainerType >::value_type = get_value_type<ContainerType>

the value type of the time variable (float or double)

Constructor & Destructor Documentation

◆ Simpsons()

template<class ContainerType >
dg::Simpsons< ContainerType >::Simpsons ( unsigned  order = 3)
inline

Set integration order without initializing values.

Parameters
ordernumber of vectors to use for integration. Choose 2 (linear) or 3 (parabola) integration.

Member Function Documentation

◆ add()

template<class ContainerType >
void dg::Simpsons< ContainerType >::add ( value_type  t_new,
const ContainerType &  u_new 
)
inline

Add a new (t,u) pair to the time integral.

The times in subsequent calls must strictly increase

Parameters
t_newtime (must be strictly larger than in the previous call)
u_newvalue (must have the same size as u0 in the init function)
Attention
the init function must be called before you can add values to the integral

◆ flush()

template<class ContainerType >
void dg::Simpsons< ContainerType >::flush ( )
inline

Reset the integral to zero and the last (t,u) pair in the add function as the new left-side.

◆ get_boundaries()

template<class ContainerType >
std::array< value_type, 2 > dg::Simpsons< ContainerType >::get_boundaries ( ) const
inline

Access the left and right boundary in time.

associated with the current value of the integral

Returns
the current integral boundaries

◆ get_integral()

template<class ContainerType >
const ContainerType & dg::Simpsons< ContainerType >::get_integral ( ) const
inline

Access the current value of the time integral.

Returns
the current integral

◆ get_order()

template<class ContainerType >
unsigned dg::Simpsons< ContainerType >::get_order ( ) const
inline

Access current integration order.

◆ init()

template<class ContainerType >
void dg::Simpsons< ContainerType >::init ( value_type  t0,
const ContainerType &  u0 
)
inline

Initialize the left-side boundary of the integration.

Parameters
t0left-side time
u0left-side value
Note
The integral itself is initialized to zero

◆ set_order()

template<class ContainerType >
void dg::Simpsons< ContainerType >::set_order ( unsigned  order)
inline

Set integration order without initializing values.

Parameters
ordernumber of vectors to use for integration. Choose 2 (linear) or 3 (parabola) integration.

The documentation for this struct was generated from the following file: