Mesh.hpp 4.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

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

#include <array>
#include <memory>

#include <dune/common/fvector.hh>

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

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

#include <dune/amdis/Initfile.hpp>
#include <dune/amdis/Log.hpp>
#include <dune/amdis/common/Filesystem.hpp>
22
23
24

namespace AMDiS
{
25
  namespace tag 
26
  {
27
28
29
30
31
32
    struct albertagrid {};
    struct uggrid {};
    struct yaspgrid {};
    struct unknowngrid {};
    
  } // end namespace tag
33
  
34
  namespace Impl
35
  {
36
37
38
39
40
41
42
43
44
45
46
47
48
    template <class Grid>
    struct MeshTag
    {
      using type = tag::unknowngrid;
    };
    
    // 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;
    };
49
#endif
50
    
51
#if HAVE_UG
52
53
54
55
56
    template <int dim>
    struct MeshTag<Dune::UGGrid<dim>>
    {
      using type = tag::uggrid;
    };
57
#endif
58
59
60
61
62
63
64
65
    
    template <int dim, class Coordinates>
    struct MeshTag<Dune::YaspGrid<dim, Coordinates>>
    {
      using type = tag::yaspgrid;
    };
    
  } // end namespace Impl
66
  
67
68
  template <class Grid>
  using MeshTag_t = typename Impl::MeshTag<Grid>::type;
69
70
  
  
71
  /// A creator class for meshes. Each mesh needs different way of initialization
72
73
74
  template <class Grid>
  class MeshCreator
  {
75
    static unique_ptr<Grid> create(std::string meshName)
76
    {
77
      AMDIS_ERROR_EXIT("Creator not yet implemented for this mesh type.");
78
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
    static unique_ptr<Grid> create(std::string meshName)
88
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
      return make_unique<Grid>(macro_filename);
96
97
98
99
100
    }
  };
#endif
  
  
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#if HAVE_UG
  template <int dim>
  struct MeshCreator<Dune::UGGrid<dim>>
  {
    using Grid = Dune::UGGrid<dim>;
    
    static unique_ptr<Grid> create(std::string meshName)
    {
      
      std::string filename = "";
      Parameters::get(meshName + "->macro file name", filename);
      
      if (!filename.empty()) {
        Path fn(filename);
        auto ext = fn.extension();

#if HAVE_ALBERTA
        if (ext == "1d" || ext == "2d" || ext == "3d") {
          Dune::GridFactory<Grid> factory;
          Dune::AlbertaReader<Grid> reader; 
          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 {
        AMDIS_ERROR_EXIT("Construction of UGGrid without filename not yet implemented!");
      }
    }
  };
#endif
  
136
137
138
139
140
  template <int dim, class T>
  struct MeshCreator<Dune::YaspGrid<dim, Dune::EquidistantCoordinates<T,dim>>>
  {
    using Grid = Dune::YaspGrid<dim, Dune::EquidistantCoordinates<T,dim>>;
    
141
    static unique_ptr<Grid> create(std::string meshName)
142
143
144
145
146
147
148
149
150
151
152
    {
      Dune::FieldVector<double, dim> L; L = 1.0;  // extension of the domain
      Parameters::get(meshName + "->dimension", L);
      
      AMDIS_MSG("L = " << L);
      
      std::array<int, dim> s; // number of cells on coarse mesh in each direction
      Parameters::get(meshName + "->num cells", s);
      
      // TODO: add more parameters for yasp-grid (see constructor)
      
153
      return make_unique<Grid>(L, s);
154
155
156
157
158
159
160
161
162
    }
  };
  
  
  template <int dim, class T>
  struct MeshCreator<Dune::YaspGrid<dim, Dune::EquidistantOffsetCoordinates<T, dim>>>
  {
    using Grid = Dune::YaspGrid<dim, Dune::EquidistantOffsetCoordinates<T, dim>>;
    
163
    static unique_ptr<Grid> create(std::string meshName)
164
165
166
167
168
169
170
171
172
173
174
    {
      Dune::FieldVector<double, dim> lowerleft;  // Lower left corner of the domain
      Dune::FieldVector<double, dim> upperright; // Upper right corner of the domain
      Parameters::get(meshName + "->min corner", lowerleft);
      Parameters::get(meshName + "->max corner", upperright);
      
      std::array<int, dim> s; // number of cells on coarse mesh in each direction
      Parameters::get(meshName + "->num cells", s);
      
      // TODO: add more parameters for yasp-grid (see constructor)
      
175
      return make_unique<Grid>(lowerleft, upperright, s);
176
177
178
179
    }
  };

} // end namespace AMDiS