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

#include <dune/vtk/vtkstructuredgridwriter.hh>
#include <dune/vtk/vtkimagedatawriter.hh>
25
26
#include <dune/vtk/datacollectors/yaspdatacollector.hh>
#include <dune/vtk/datacollectors/spdatacollector.hh>
27
28
29
30
31

using namespace Dune;
using namespace Dune::experimental;
using namespace Dune::Functions;

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

35
36
37
38
39
40
template <class GridView>
void write(std::string prefix, GridView const& gridView)
{
  auto fct2 = makeAnalyticGridViewFunction([](auto const& x) {
    return std::sin(10*x[0])*std::cos(10*x[1])+std::sin(10*x[2]);
  }, gridView);
41
42

  {
43
    using Writer = VtkStructuredGridWriter<GridView>;
44
    Writer vtkWriter(gridView);
45
46
    vtkWriter.addPointData(fct2, "analytic");
    vtkWriter.write(prefix + "sg_ascii_float32.vts", Vtk::ASCII);
47
48
49
  }

  {
50
    using Writer = VtkImageDataWriter<GridView>;
51
    Writer vtkWriter(gridView);
52
53
    vtkWriter.addPointData(fct2, "analytic");
    vtkWriter.write(prefix + "id_ascii_float32.vti", Vtk::ASCII);
54
55
  }
}
56
57
58
59
60
61
62
63

template <int dim>
void write_yaspgrid(std::integral_constant<int,dim>)
{
  using GridType = YaspGrid<dim>;
  FieldVector<double,dim> upperRight; upperRight = 1.0;
  auto numElements = filledArray<dim,int>(8);
  GridType grid(upperRight,numElements,0,0);
64
  grid.globalRefine(1);
65
66
67
68
69
70
71

  write("yasp_" + std::to_string(dim) + "d_", grid.leafGridView());
}

template <int dim>
void write_spgrid(std::integral_constant<int,dim>)
{
72
#if HAVE_DUNE_SPGRID
73
74
75
76
  using GridType = SPGrid<double,dim, SPIsotropicRefinement>;
  FieldVector<double,dim> upperRight; upperRight = 1.0;
  auto numElements = filledArray<dim,int>(8);
  GridType grid(SPDomain<double,dim>::unitCube(),numElements);
77
  grid.globalRefine(1);
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

  write("sp_" + std::to_string(dim) + "d_", grid.leafGridView());
#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);
  });
}