datacollector.cc 3.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
// -*- 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>
14
#include <dune/common/version.hh>
15
16
17
18
19
20

#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>
21

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

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

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

using namespace Dune;
using namespace Dune::Functions;

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

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

45
46
47
template <class GridView>
void write (std::string prefix, GridView const& gridView)
{
48
49
50
#if ! DUNE_VERSION_NEWER(DUNE_FUNCTIONS, 2, 6)
  using namespace BasisBuilder;
#else
51
  using namespace BasisFactory;
52
#endif
53
54
  auto basis = makeBasis(gridView, lagrange<1>());

Praetorius, Simon's avatar
Praetorius, Simon committed
55
56
57
58
  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;
59

60
61
  std::vector<double> vec(basis.dimension());
  interpolate(basis, vec, [&c](auto const& x) { return c.dot(x); });
62
63

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

66
  // write analytic function
67
68
69
70
71
  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);
72
73
}

74
75
76
template <int I>
using int_ = std::integral_constant<int,I>;

77
78
79
80
int main(int argc, char** argv)
{
  Dune::MPIHelper::instance(argc, argv);

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