Mesh.hpp 4.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
#pragma once

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

#include <array>
#include <memory>

10
#include <dune/common/filledarray.hh>
11
12
13
14
15
16
#include <dune/common/fvector.hh>

#include <dune/grid/albertagrid.hh>
#include <dune/grid/uggrid.hh>
#include <dune/grid/yaspgrid.hh>

17
18
19
20
#include <dune/grid/albertagrid/albertareader.hh>
#include <dune/grid/io/file/gmshreader.hh>

#include <dune/amdis/Initfile.hpp>
21
#include <dune/amdis/Output.hpp>
22
#include <dune/amdis/common/Filesystem.hpp>
23
24
25

namespace AMDiS
{
26
  namespace tag
27
  {
28
29
30
31
    struct albertagrid {};
    struct uggrid {};
    struct yaspgrid {};
    struct unknowngrid {};
32

33
  } // end namespace tag
34

35
  namespace Impl
36
  {
37
38
39
40
41
    template <class Grid>
    struct MeshTag
    {
      using type = tag::unknowngrid;
    };
42

43
44
45
46
47
48
49
    // specialization for some grid types from DUNE
#if HAVE_ALBERTA
    template <int dim, int dimworld>
    struct MeshTag<Dune::AlbertaGrid<dim, dimworld>>
    {
      using type = tag::albertagrid;
    };
50
#endif
51

52
#if HAVE_UG
53
54
55
56
57
    template <int dim>
    struct MeshTag<Dune::UGGrid<dim>>
    {
      using type = tag::uggrid;
    };
58
#endif
59

60
61
62
63
64
    template <int dim, class Coordinates>
    struct MeshTag<Dune::YaspGrid<dim, Coordinates>>
    {
      using type = tag::yaspgrid;
    };
65

66
  } // end namespace Impl
67

68
69
  template <class Grid>
  using MeshTag_t = typename Impl::MeshTag<Grid>::type;
70
71


72
  /// A creator class for meshes. Each mesh needs different way of initialization
73
74
75
  template <class Grid>
  class MeshCreator
  {
76
    static unique_ptr<Grid> create(std::string meshName)
77
    {
78
      error_exit("Creator not yet implemented for this mesh type.");
79
80
    }
  };
81

82
83
84
85
86
#if HAVE_ALBERTA
  template <int dim, int dimworld>
  struct MeshCreator<Dune::AlbertaGrid<dim, dimworld>>
  {
    using Grid = Dune::AlbertaGrid<dim, dimworld>;
87

88
    static unique_ptr<Grid> create(std::string meshName)
89
90
91
92
93
94
    {
      std::string macro_filename = "";
      Parameters::get(meshName + "->macro file name", macro_filename);

      // TODO: if filename_extension is ".2d" or ".3d" read it directly from file
      // otherwise use a factory method
95

96
      return make_unique<Grid>(macro_filename);
97
98
99
    }
  };
#endif
100
101


102
103
104
105
106
#if HAVE_UG
  template <int dim>
  struct MeshCreator<Dune::UGGrid<dim>>
  {
    using Grid = Dune::UGGrid<dim>;
107

108
109
    static unique_ptr<Grid> create(std::string meshName)
    {
110

111
112
      std::string filename = "";
      Parameters::get(meshName + "->macro file name", filename);
113

114
115
116
117
118
119
120
      if (!filename.empty()) {
        Path fn(filename);
        auto ext = fn.extension();

#if HAVE_ALBERTA
        if (ext == "1d" || ext == "2d" || ext == "3d") {
          Dune::GridFactory<Grid> factory;
121
          Dune::AlbertaReader<Grid> reader;
122
123
124
125
126
127
128
129
130
          reader.readGrid(filename, factory);
          return unique_ptr<Grid>{factory.createGrid()};
        }
#endif
        if (ext == "msh") {
          Dune::GmshReader<Grid> reader;
          return unique_ptr<Grid>{reader.read(filename)};
        }
      } else {
131
        error_exit("Construction of UGGrid without filename not yet implemented!");
132
      }
133
134

      error_exit("No way to construct UG-Grid found");
135
      return {};
136
137
138
    }
  };
#endif
139

140
141
142
143
  template <int dim, class T>
  struct MeshCreator<Dune::YaspGrid<dim, Dune::EquidistantCoordinates<T,dim>>>
  {
    using Grid = Dune::YaspGrid<dim, Dune::EquidistantCoordinates<T,dim>>;
144

145
    static unique_ptr<Grid> create(std::string meshName)
146
147
148
    {
      Dune::FieldVector<double, dim> L; L = 1.0;  // extension of the domain
      Parameters::get(meshName + "->dimension", L);
149
150
151

      msg("L = ", L);

152
      auto s = Dune::filledArray<std::size_t(dim)>(2); // number of cells on coarse mesh in each direction
153
      Parameters::get(meshName + "->num cells", s);
154

155
      // TODO: add more parameters for yasp-grid (see constructor)
156

157
      return make_unique<Grid>(L, s);
158
159
    }
  };
160
161


162
163
164
165
  template <int dim, class T>
  struct MeshCreator<Dune::YaspGrid<dim, Dune::EquidistantOffsetCoordinates<T, dim>>>
  {
    using Grid = Dune::YaspGrid<dim, Dune::EquidistantOffsetCoordinates<T, dim>>;
166

167
    static unique_ptr<Grid> create(std::string meshName)
168
    {
169
170
      Dune::FieldVector<double, dim> lowerleft;  lowerleft = 0.0; // Lower left corner of the domain
      Dune::FieldVector<double, dim> upperright; upperright = 1.0; // Upper right corner of the domain
171
172
      Parameters::get(meshName + "->min corner", lowerleft);
      Parameters::get(meshName + "->max corner", upperright);
173

174
      auto s = Dune::filledArray<std::size_t(dim)>(2); // number of cells on coarse mesh in each direction
175
      Parameters::get(meshName + "->num cells", s);
176

177
      // TODO: add more parameters for yasp-grid (see constructor)
178

179
      return make_unique<Grid>(lowerleft, upperright, s);
180
181
182
183
    }
  };

} // end namespace AMDiS