Dune Core Modules (unstable)

recipe-integration.cc

See explanation at Integrate a function on a grid

// always include the config file
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
// C++ includes
#include<math.h>
#include<iostream>
// dune-common includes
// dune-geometry includes
#include<dune/geometry/referenceelements.hh>
// dune-grid includes
int main(int argc, char** argv)
{
// Maybe initialize Mpi
// [set up grid]
const int dim = 4;
using Grid = Dune::YaspGrid<dim>;
Dune::FieldVector<double,dim> len; for (auto& l:len) l=1.0;
std::array<int,dim> cells; for (auto& c : cells) c=5;
Grid grid(len,cells);
// [small vectors and matrices]
Dune::FieldVector<double,4> x({1,2,3,4}); // make a vector
auto y(x); // copy constructor
y *= 1.0/3.0; // scaling
auto s = x*y; // scalar product
auto norm = x.two_norm(); // Euclidean norm
Dune::FieldMatrix<double,4,4> A({{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}); // make a matrix
A.mv(x,y); // matvec: y = Ax
A.usmv(0.5,x,y); // axpy: y += 0.5*Ax
// [a function to integrate]
auto u = [](const auto& x){return std::exp(x.two_norm());};
// [integration with midpoint rule]
double integral=0.0;
auto gv = grid.leafGridView(); // extract the grid view
for (const auto& e : elements(gv))
integral += u(e.geometry().center())*e.geometry().volume();
std::cout << "integral = " << integral << std::endl;
// [integration with quadrature rule]
double integral2 = 0.0;
for (const auto& e : elements(gv))
{
auto geo = e.geometry();
auto quadrature = QR::rule(geo.type(),5);
for (const auto& qp : quadrature)
integral2 += u(geo.global(qp.position()))
*geo.integrationElement(qp.position())*qp.weight();
}
std::cout << "integral2 = " << integral2 << std::endl;
// [integrating a flux]
auto f = [](const auto& x){return x;};
double divergence=0.0;
for (const auto& i : elements(gv)) {
for (const auto& I : intersections(gv,i))
if (!I.neighbor())
{
auto geoI = I.geometry();
divergence += f(geoI.center())*I.centerUnitOuterNormal()*geoI.volume();
}
}
std::cout << "divergence = " << divergence << std::endl;
}
[ provides Dune::Grid ]
Definition: yaspgrid.hh:164
vector space out of a tensor product of fields.
Definition: fvector.hh:96
Helpers for dealing with MPI.
IteratorRange<... > intersections(const GV &gv, const Entity &e)
Iterates over all Intersections of an Entity with respect to the given GridView.
A dense n x m matrix.
Definition: fmatrix.hh:69
Various parser methods to get data into a ParameterTree object.
A container for all quadrature rules of dimension dim
Definition: quadraturerules.hh:198
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:138
A simple timing class.
A fake mpi helper.
Definition: mpihelper.hh:70
IteratorRange<... > elements(const GV &gv)
Iterates over all elements / cells (entities with codimension 0) of a GridView.
Creative Commons License   |  Legal Statements / Impressum  |  generated with Hugo v0.69.0 (Mar 1, 23:27, 2021)