datacollector.cc 3.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <iostream>
#include <vector>

#include <dune/common/parallel/mpihelper.hh> // An initializer of MPI
#include <dune/common/exceptions.hh> // We use exceptions
#include <dune/common/filledarray.hh>

#include <dune/functions/functionspacebases/defaultglobalbasis.hh>
#include <dune/functions/functionspacebases/lagrangebasis.hh>
#include <dune/functions/functionspacebases/interpolate.hh>
#include <dune/functions/gridfunctions/analyticgridviewfunction.hh>
#include <dune/functions/gridfunctions/discreteglobalbasisfunction.hh>
20

21
22
#include <dune/grid/yaspgrid.hh>

23
#include <dune/vtk/writers/vtkunstructuredgridwriter.hh>
24
25

#include <dune/vtk/datacollectors/continuousdatacollector.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
26
#include <dune/vtk/datacollectors/discontinuousdatacollector.hh>
27
#include <dune/vtk/datacollectors/quadraticdatacollector.hh>
28
29
30
31

using namespace Dune;
using namespace Dune::Functions;

32
33
template <class DataCollector, class GridView, class Fct1, class Fct2>
void write_dc (std::string prefix, GridView const& gridView, Fct1 const& fct1, Fct2 const& fct2)
34
{
35
  VtkUnstructuredGridWriter<GridView, DataCollector> vtkWriter(gridView, Vtk::ASCII, Vtk::FLOAT32);
36
37
38
39
40
  vtkWriter.addPointData(fct1, "p1");
  vtkWriter.addCellData(fct1, "p0");
  vtkWriter.addPointData(fct2, "q1");
  vtkWriter.addCellData(fct2, "q0");

Praetorius, Simon's avatar
Praetorius, Simon committed
41
  vtkWriter.write(prefix + "_" + std::to_string(GridView::dimensionworld) + "d_ascii.vtu");
42
}
43

44
45
46
template <class GridView>
void write (std::string prefix, GridView const& gridView)
{
47
48
49
  using namespace BasisFactory;
  auto basis = makeBasis(gridView, lagrange<1>());

Praetorius, Simon's avatar
Praetorius, Simon committed
50
51
52
53
  FieldVector<double,GridView::dimensionworld> c;
  if (GridView::dimensionworld > 0) c[0] = 11.0;
  if (GridView::dimensionworld > 1) c[1] = 7.0;
  if (GridView::dimensionworld > 2) c[2] = 3.0;
54

55
56
  std::vector<double> vec(basis.dimension());
  interpolate(basis, vec, [&c](auto const& x) { return c.dot(x); });
57
58

  // write discrete global-basis function
59
60
  auto p1Interpol = makeDiscreteGlobalBasisFunction<double>(basis, vec);

61
  // write analytic function
62
63
64
65
66
  auto p1Analytic = makeAnalyticGridViewFunction([&c](auto const& x) { return c.dot(x); }, gridView);

  write_dc<ContinuousDataCollector<GridView>>(prefix + "_continuous", gridView, p1Interpol, p1Analytic);
  write_dc<DiscontinuousDataCollector<GridView>>(prefix + "_discontinuous", gridView, p1Interpol, p1Analytic);
  write_dc<QuadraticDataCollector<GridView>>(prefix + "_quadratic", gridView, p1Interpol, p1Analytic);
67
68
}

69
70
71
template <int I>
using int_ = std::integral_constant<int,I>;

72
73
74
75
int main(int argc, char** argv)
{
  Dune::MPIHelper::instance(argc, argv);

76
  Hybrid::forEach(std::make_tuple(int_<1>{}, int_<2>{}, int_<3>{}), [](auto dim)
77
  {
78
79
    using GridType = YaspGrid<dim.value>;
    FieldVector<double,dim.value> upperRight; upperRight = 1.0;
Praetorius, Simon's avatar
Praetorius, Simon committed
80
81
    auto numElements = filledArray<dim.value,int>(8);
    GridType grid(upperRight, numElements, 0, 0);
Praetorius, Simon's avatar
Praetorius, Simon committed
82
    write("datacollector_yasp", grid.leafGridView());
83
  });
84
}