Discontinuous Galerkin Library
#include "dg/algorithm.h"
Loading...
Searching...
No Matches

\( f_i = f(\vec x_i) \) More...

Collaboration diagram for Evaluate, integrate, weights:

Functions

template<class Functor , class Topology >
auto dg::evaluate (Functor &&f, const Topology &g)
 Evaluate a function on grid coordinates
 
template<class real_type >
thrust::host_vector< real_type > dg::integrate (const thrust::host_vector< real_type > &in, const RealGrid< real_type, 1 > &g, dg::direction dir=dg::forward)
 Indefinite integral of a function on a grid.
 
template<class UnaryOp , class real_type >
thrust::host_vector< real_type > dg::integrate (UnaryOp f, const RealGrid< real_type, 1 > &g, dg::direction dir=dg::forward)
 Untility shortcut.
 
template<class Topology >
auto dg::create::weights (const Topology &g)
 Nodal weight coefficients.
 
template<class Topology >
auto dg::create::weights (const Topology &g, std::array< bool, Topology::ndim()> remains)
 Nodal weight coefficients on a subset of dimensions.
 
template<class Topology >
auto dg::create::inv_weights (const Topology &g)
 Inverse nodal weight coefficients.
 

Detailed Description

\( f_i = f(\vec x_i) \)

Function Documentation

◆ evaluate()

template<class Functor , class Topology >
auto dg::evaluate ( Functor && f,
const Topology & g )

Evaluate a function on grid coordinates

Evaluate is equivalent to the following:

  1. from the given Nd dimensional grid generate Nd one-dimensional lists of grid coordinates x_i = g.abscissas( i) representing the given computational space discretization in each dimension
  2. evaluate the given function or functor at these coordinates and store the result in the output vector v = dg::kronecker( f, x_0, x_1, ...) The dimension number i is thus mapped to the argument number of the function f. The 0 dimension is the contiguous dimension in the return vector v e.g. in 2D the first element of the resulting vector lies in the grid corner \( (x_0,y_0)\), the second is \((x_1, y_0)\) and so on.

For example for a 2d grid the implementation is equivalent to

return dg::kronecker( f, g.abscissas(0), g.abscissas(1));
auto kronecker(Functor &&f, const ContainerType &x0, const ContainerTypes &... xs)
Memory allocating version of dg::blas1::kronecker
Definition blas1.h:857

See here an application example

// This code snippet demonstrates how to discretize and compute the norm of a
// function on both shared and distributed memory systems
#ifdef WITH_MPI
// In an MPI environment define a 2d Cartesian communicator
MPI_Comm comm2d = dg::mpi_cart_create( MPI_COMM_WORLD, {0,0}, {1,1});
#endif
// create a grid of the domain [0,2]x[0,2] with 20 cells in x and y and
// 3 polynomial coefficients
dg::x::Grid2d g2d( 0, 2, 0, 2, 3, 20, 20
#ifdef WITH_MPI
, comm2d // in MPI distribute among processes
#endif
);
// define the function to integrate
const double amp = 2.;
auto function = [=] (double x, double y){
return amp*exp(x)*exp(y);
};
// create the Gaussian weights (volume form) for the integration
const dg::x::DVec w2d = dg::create::weights( g2d);
// discretize the function on the grid
const dg::x::DVec vec = dg::evaluate( function, g2d);
// now compute the scalar product (the L2 norm)
double square_norm = dg::blas2::dot( vec, w2d, vec);
// norm is now (or at least converges to): (e^4-1)^2
CHECK( fabs( square_norm - (exp(4)-1)*(exp(4)-1)) < 1e-6);
Template Parameters
TopologyA fixed sized grid type with member functions static constexpr size_t Topology::ndim() giving the number of dimensions and vector_type Topology::abscissas( unsigned dim) giving the abscissas in dimension dim
FunctorCallable as return_type f(real_type, ...). Functor needs to be callable with Topology::ndim arguments.
Parameters
fThe function to evaluate, see A large collection for a host of predefined functors to evaluate
gThe grid that defines the computational space on which to evaluate f
Returns
The output vector v as a host vector. Its value type is determined by the return type of Functor
Note
Use the elementary function \( f(x) = x \) (dg::cooX1d ) to generate the list of grid coordinates
See also
Introduction to dg methods
dg::pullback if you want to evaluate a function in physical space
dg::kronecker
Note
In the MPI version all processes in the grid communicator need to call this function. Each process evaluates the function f only on the grid coordinates that it owns i.e. the local part of the given grid

◆ integrate() [1/2]

template<class real_type >
thrust::host_vector< real_type > dg::integrate ( const thrust::host_vector< real_type > & in,
const RealGrid< real_type, 1 > & g,
dg::direction dir = dg::forward )

Indefinite integral of a function on a grid.

\[ F_h(x) = \int_a^x f_h(x') dx' \]

This function computes the indefinite integral of a given input

Parameters
inHost vector discretized on g
gThe grid
dirIf dg::backward then the integral starts at the right boundary (i.e. goes in the reverse direction)

\[ F_h(x) = \int_b^x f_h(x') dx' = \int_a^x f_h(x') dx' - \int_a^b f_h(x') dx' \]

Returns
integral of in on the grid g
See also
Introduction to dg methods

◆ integrate() [2/2]

template<class UnaryOp , class real_type >
thrust::host_vector< real_type > dg::integrate ( UnaryOp f,
const RealGrid< real_type, 1 > & g,
dg::direction dir = dg::forward )

Untility shortcut.

for

thrust::host_vector<real_type> vector = evaluate( f, g);
return integrate<real_type>(vector, g, dir);
thrust::host_vector< real_type > integrate(const thrust::host_vector< real_type > &in, const RealGrid< real_type, 1 > &g, dg::direction dir=dg::forward)
Indefinite integral of a function on a grid.
Definition evaluation.h:126
auto evaluate(Functor &&f, const Topology &g)
Evaluate a function on grid coordinates
Definition evaluation.h:74

◆ inv_weights()

template<class Topology >
auto dg::create::inv_weights ( const Topology & g)

Inverse nodal weight coefficients.

Short for

auto v = weights( g);
return v;
void transform(const ContainerType1 &x, ContainerType &y, UnaryOp op)
Definition blas1.h:585
auto weights(const Topology &g)
Nodal weight coefficients.
Definition weights.h:62
Definition functors.h:93

◆ weights() [1/2]

template<class Topology >
auto dg::create::weights ( const Topology & g)

Nodal weight coefficients.

Equivalent to the following:

  1. from the given Nd dimensional grid generate Nd one-dimensional lists of integraion weights w_i = g.weights( i)
  2. take the kronecker product of the 1d weights and store the result in the output vector w = dg::kronecker( dg::Product(), w_0, w_1, ...) The 0 dimension is the contiguous dimension in the return vector w

For example

// This code snippet demonstrates how to discretize and compute the norm of a
// function on both shared and distributed memory systems
#ifdef WITH_MPI
// In an MPI environment define a 2d Cartesian communicator
MPI_Comm comm2d = dg::mpi_cart_create( MPI_COMM_WORLD, {0,0}, {1,1});
#endif
// create a grid of the domain [0,2]x[0,2] with 20 cells in x and y and
// 3 polynomial coefficients
dg::x::Grid2d g2d( 0, 2, 0, 2, 3, 20, 20
#ifdef WITH_MPI
, comm2d // in MPI distribute among processes
#endif
);
// define the function to integrate
const double amp = 2.;
auto function = [=] (double x, double y){
return amp*exp(x)*exp(y);
};
// create the Gaussian weights (volume form) for the integration
const dg::x::DVec w2d = dg::create::weights( g2d);
// discretize the function on the grid
const dg::x::DVec vec = dg::evaluate( function, g2d);
// now compute the scalar product (the L2 norm)
double square_norm = dg::blas2::dot( vec, w2d, vec);
// norm is now (or at least converges to): (e^4-1)^2
CHECK( fabs( square_norm - (exp(4)-1)*(exp(4)-1)) < 1e-6);
Template Parameters
TopologyA fixed sized grid type with member functions static constexpr size_t Topology::ndim() giving the number of dimensions and vector_type Topology::weights( unsigned dim) giving the integration weights in dimension dim
Parameters
gThe grid
Returns
The output vector w as a host vector. Its value type is the same as the grid value type
See also
Introduction to dg methods

◆ weights() [2/2]

template<class Topology >
auto dg::create::weights ( const Topology & g,
std::array< bool, Topology::ndim()> remains )

Nodal weight coefficients on a subset of dimensions.

Equivalent to the following:

  1. from the given Nd dimensional grid generate Nd one-dimensional lists of integraion weights w_i = remains[i] ? g.weights( i) : 1
  2. take the kronecker product of the 1d weights and store the result in the output vector w = dg::kronecker( dg::Product(), w_0, w_1, ...) The 0 dimension is the contiguous dimension in the return vector w
Template Parameters
TopologyA fixed sized grid type with member functions static constexpr size_t Topology::ndim() giving the number of dimensions and vector_type Topology::weights( unsigned dim) giving the integration weights in dimension dim
Parameters
gThe grid
remainsFor each dimension determine whether to use weights or a vector of 1s
Returns
Host Vector with full grid size
Note
If you want the weights of the sub-grid that consists of the remaining dimensions you need to manually create that sub-grid and use dg::create::weights( sub_grid) The difference is the size of the resulting vector or the result of this function is to prolongate the sub-grid weights to the full grid again
See also
Introduction to dg methods