AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
MacroGridFactory.hpp
1 #pragma once
2 
3 #include <dune/common/typeutilities.hh>
4 #include <dune/common/version.hh>
5 #include <dune/grid/utility/structuredgridfactory.hh>
6 #include <dune/grid/yaspgrid.hh>
7 
8 namespace Dune
9 {
10  template <class GridType>
11  struct CubedWrapper : public GridType {};
12 
13 
14  template <class GridType>
15  class GridFactory<CubedWrapper<GridType>>
16  : public GridFactoryInterface<GridType>
17  {
18  using Self = GridFactory;
19  using Super = GridFactoryInterface<GridType>;
20  using ctype = typename GridType::ctype;
21 
22  enum { dim = GridType::dimension };
23  enum { dimworld = GridType::dimensionworld };
24 
25  public:
26  template <class... Args, disableCopyMove<Self, Args...> = 0>
27  GridFactory (Args&&... args)
28  : factory_(std::make_shared<GridFactory<GridType>>(std::forward<Args>(args)...))
29  {}
30 
31  GridFactory (GridFactory<GridType>& factory)
32  : factory_(stackobject_to_shared_ptr(factory))
33  {}
34 
36  void insertVertex (const FieldVector<ctype,dimworld>& pos) override
37  {
38  factory_->insertVertex(pos);
39  }
40 
42 
48  void insertElement (const GeometryType& type,
49  const std::vector<unsigned int>& vertices) override
50  {
51  // triangulation of reference cube
52  static const auto reference_cubes = std::make_tuple(
53  std::array<std::array<int,2>, 1>{std::array<int,2>{0,1}},
54  std::array<std::array<int,3>, 2>{std::array<int,3>{3,0,1}, std::array<int,3>{0,3,2}},
55  std::array<std::array<int,4>, 6>{std::array<int,4>{0,7,3,1}, std::array<int,4>{0,7,5,1},
56  std::array<int,4>{0,7,5,4}, std::array<int,4>{0,7,3,2},
57  std::array<int,4>{0,7,6,2}, std::array<int,4>{0,7,6,4}} );
58 
59  assert(type == GeometryTypes::cube(dim));
60 
61  auto const& simplices = std::get<dim-1>(reference_cubes);
62  thread_local std::vector<unsigned int> corners(dim+1);
63  for (auto const& simplex : simplices) {
64  for (std::size_t i = 0; i < simplex.size(); ++i)
65  corners[i] = vertices[simplex[i]];
66 
67  factory_->insertElement(GeometryTypes::simplex(dim), corners);
68  }
69  }
70 
71  using Super::insertElement;
72 
74  // TODO: maybe split boundary segment in simplices
75  void insertBoundarySegment (const std::vector<unsigned int>& vertices) override
76  {
77  factory_->insertBoundarySegment(vertices);
78  }
79 
80  using Super::insertBoundarySegment;
81 
83  GridType* createGrid () override
84  {
85  return factory_->createGrid();
86  }
87 
88  private:
89  std::shared_ptr<GridFactory<GridType>> factory_;
90  };
91 
92 } // end namespace Dune
93 
94 
95 namespace AMDiS
96 {
97  template <class GridType>
99  {
100  using ctype = typename GridType::ctype;
101 
102  enum { dim = GridType::dimension };
103  enum { dimworld = GridType::dimensionworld };
104 
105  public:
106 
108  static std::unique_ptr<GridType> createSimplexGrid (const Dune::FieldVector<ctype,dimworld>& lowerLeft,
109  const Dune::FieldVector<ctype,dimworld>& upperRight,
110  const std::array<unsigned int,dim>& numElements)
111  {
112  Dune::GridFactory<Dune::CubedWrapper<GridType>> factory;
113 
114  // fallback implementation using temporary YaspGrid
115  using TempGrid = Dune::YaspGrid<dim,Dune::EquidistantOffsetCoordinates<double,dim>>;
116  auto grid = Dune::StructuredGridFactory<TempGrid>::createCubeGrid(lowerLeft, upperRight, numElements);
117  for (auto const& v : vertices(grid->leafGridView()))
118  factory.insertVertex(v.geometry().corner(0));
119 
120  auto const& indexSet = grid->leafIndexSet();
121  for (auto const& e : elements(grid->leafGridView()))
122  {
123  thread_local std::vector<unsigned int> vertices;
124  vertices.resize(e.subEntities(dim));
125  for (unsigned int i = 0; i < e.subEntities(dim); ++i)
126  vertices[i] = indexSet.subIndex(e,i,dim);
127  factory.insertElement(Dune::GeometryTypes::cube(dim), vertices);
128  }
129  return std::unique_ptr<GridType>(factory.createGrid());
130  }
131  };
132 
133 } // end namespace AMDiS
Definition: AdaptiveGrid.hpp:373
Definition: FieldMatVec.hpp:12
Definition: AdaptBase.hpp:6
static std::unique_ptr< GridType > createSimplexGrid(const Dune::FieldVector< ctype, dimworld > &lowerLeft, const Dune::FieldVector< ctype, dimworld > &upperRight, const std::array< unsigned int, dim > &numElements)
Create a structured simplex grid.
Definition: MacroGridFactory.hpp:108
Definition: MacroGridFactory.hpp:98