structuredgridwriter.cc 3.79 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
namespace Impl_
33
{
34
35
  template <class GridView, class Grid>
  struct StructuredDataCollector;
36

37
#if HAVE_DUNE_SPGRID
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  template<class GridView, class ct, int dim, template< int > class Ref, class Comm>
  struct StructuredDataCollector<GridView, SPGrid<ct,dim,Ref,Comm>>
  {
    using type = SPDataCollector<GridView>;
  };
#endif

  template<class GridView, int dim, class Coordinates>
  struct StructuredDataCollector<GridView, YaspGrid<dim,Coordinates>>
  {
    using type = YaspDataCollector<GridView>;
  };
}

template <class GridView>
using StructuredDataCollector = typename Impl_::StructuredDataCollector<GridView, typename GridView::Grid>::type;
54
55


56
57
template <int dim>
using int_ = std::integral_constant<int,dim>;
58

59
60
61
template <class GridView>
void write(std::string prefix, GridView const& gridView)
{
62
63
64
65
66
67
68
69
  using namespace BasisFactory;
  auto basis = makeBasis(gridView, lagrange<1>());

  std::vector<double> p1function(basis.dimension());
  interpolate(basis, p1function, [](auto const& x) {
    return 100*x[0] + 10*x[1] + 1*x[2];
  });

70
71
72
73
  auto fct1 = makeDiscreteGlobalBasisFunction<double>(basis, p1function);
  auto fct2 = makeAnalyticGridViewFunction([](auto const& x) {
    return std::sin(10*x[0])*std::cos(10*x[1])+std::sin(10*x[2]);
  }, gridView);
74
75

  {
76
    using Writer = VtkStructuredGridWriter<GridView, StructuredDataCollector<GridView>>;
77
    Writer vtkWriter(gridView);
78
79
80
81
    vtkWriter.addPointData(fct1, "p1");
    vtkWriter.addCellData(fct1, "p0");
    vtkWriter.addPointData(fct2, "analytic");
    vtkWriter.write(prefix + "sg_ascii_float32.vts", Vtk::ASCII);
82
83
84
  }

  {
85
    using Writer = VtkImageDataWriter<GridView, StructuredDataCollector<GridView>>;
86
    Writer vtkWriter(gridView);
87
88
89
90
    vtkWriter.addPointData(fct1, "p1");
    vtkWriter.addCellData(fct1, "p0");
    vtkWriter.addPointData(fct2, "analytic");
    vtkWriter.write(prefix + "id_ascii_float32.vti", Vtk::ASCII);
91
92
  }
}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

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);

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

template <int dim>
void write_spgrid(std::integral_constant<int,dim>)
{
108
#if HAVE_DUNE_SPGRID
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  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);

  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);
  });
}