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

22
23
24
#include <dune/vtk/writers/vtkimagedatawriter.hh>
#include <dune/vtk/writers/vtkrectilineargridwriter.hh>
#include <dune/vtk/writers/vtkstructuredgridwriter.hh>
25
#include <dune/vtk/writers/vtkunstructuredgridwriter.hh>
26
27
#include <dune/vtk/datacollectors/yaspdatacollector.hh>
#include <dune/vtk/datacollectors/spdatacollector.hh>
28
29
30
31

using namespace Dune;
using namespace Dune::Functions;

32
33
template <int dim>
using int_ = std::integral_constant<int,dim>;
34

35
36
37
template <class GridView>
void write(std::string prefix, GridView const& gridView)
{
Praetorius, Simon's avatar
Praetorius, Simon committed
38
39
40
41
  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;
42
43

  auto fct2 = makeAnalyticGridViewFunction([&c](auto const& x) -> float { return c.dot(x); }, gridView);
44
45

  {
46
    using Writer = VtkImageDataWriter<GridView>;
47
    Writer vtkWriter(gridView, Vtk::ASCII, Vtk::FLOAT32);
48
    vtkWriter.addPointData(fct2, "analytic");
49
    vtkWriter.write(prefix + "id_ascii_float32.vti");
50
51
52
  }

  {
53
    using Writer = VtkRectilinearGridWriter<GridView>;
54
    Writer vtkWriter(gridView, Vtk::ASCII, Vtk::FLOAT32);
55
    vtkWriter.addPointData(fct2, "analytic");
56
    vtkWriter.write(prefix + "rg_ascii_float32.vtr");
57
58
59
60
  }

  {
    using Writer = VtkStructuredGridWriter<GridView>;
61
    Writer vtkWriter(gridView, Vtk::ASCII, Vtk::FLOAT32);
62
    vtkWriter.addPointData(fct2, "analytic");
63
    vtkWriter.write(prefix + "sg_ascii_float32.vts");
64
65
66
67
  }

  {
    using Writer = VtkUnstructuredGridWriter<GridView>;
68
    Writer vtkWriter(gridView, Vtk::ASCII, Vtk::FLOAT32);
69
    vtkWriter.addPointData(fct2, "analytic");
70
    vtkWriter.write(prefix + "ug_ascii_float32.vts");
71
72
  }
}
73
74
75
76
77
78

template <int dim>
void write_yaspgrid(std::integral_constant<int,dim>)
{
  using GridType = YaspGrid<dim>;
  FieldVector<double,dim> upperRight; upperRight = 1.0;
Praetorius, Simon's avatar
Praetorius, Simon committed
79
  auto numElements = filledArray<dim,int>(8);
80
  GridType grid(upperRight,numElements,0,0);
81
  grid.globalRefine(1);
82

Praetorius, Simon's avatar
Praetorius, Simon committed
83
  write("structuredgridwriter_yasp_" + std::to_string(dim) + "d_", grid.leafGridView());
84
85
86
87
88
}

template <int dim>
void write_spgrid(std::integral_constant<int,dim>)
{
89
#if HAVE_DUNE_SPGRID
90
91
  using GridType = SPGrid<double,dim, SPIsotropicRefinement>;
  FieldVector<double,dim> upperRight; upperRight = 1.0;
Praetorius, Simon's avatar
Praetorius, Simon committed
92
  auto numElements = filledArray<dim,int>(8);
93
  GridType grid(SPDomain<double,dim>::unitCube(),numElements);
94
  // grid.globalRefine(1);
95

Praetorius, Simon's avatar
Praetorius, Simon committed
96
  write("structuredgridwriter_sp_" + std::to_string(dim) + "d_", grid.leafGridView());
97
98
99
100
101
102
103
104
105
106
107
108
109
#endif
}

int main(int argc, char** argv)
{
  Dune::MPIHelper::instance(argc, argv);

  Hybrid::forEach(std::make_tuple(int_<1>{}, int_<2>{}, int_<3>{}), [](auto const dim)
  {
    write_yaspgrid(dim);
    write_spgrid(dim);
  });
}