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 
7 namespace Dune
8 {
9  template <class GridType>
10  struct CubedWrapper : public GridType {};
11 
12 
13  template <class GridType>
14  class GridFactory<CubedWrapper<GridType>>
15  : public GridFactoryInterface<GridType>
16  {
17  using Self = GridFactory;
18  using Super = GridFactoryInterface<GridType>;
19  using ctype = typename GridType::ctype;
20 
21  enum { dim = GridType::dimension };
22  enum { dimworld = GridType::dimensionworld };
23 
24  public:
25  template <class... Args, disableCopyMove<Self, Args...> = 0>
26  GridFactory (Args&&... args)
27  : factory_(std::make_shared<GridFactory<GridType>>(std::forward<Args>(args)...))
28  {}
29 
30  GridFactory (GridFactory<GridType>& factory)
31  : factory_(stackobject_to_shared_ptr(factory))
32  {}
33 
35  void insertVertex (const FieldVector<ctype,dimworld>& pos) override
36  {
37  factory_->insertVertex(pos);
38  }
39 
41 
47  void insertElement (const GeometryType& type,
48  const std::vector<unsigned int>& vertices) override
49  {
50  // triangulation of reference cube
51  static const auto reference_cubes = std::make_tuple(
52  std::array<std::array<int,2>, 1>{std::array<int,2>{0,1}},
53  std::array<std::array<int,3>, 2>{std::array<int,3>{3,0,1}, std::array<int,3>{0,3,2}},
54  std::array<std::array<int,4>, 6>{std::array<int,4>{0,7,3,1}, std::array<int,4>{0,7,5,1},
55  std::array<int,4>{0,7,5,4}, std::array<int,4>{0,7,3,2},
56  std::array<int,4>{0,7,6,2}, std::array<int,4>{0,7,6,4}} );
57 
58  assert(type == GeometryTypes::cube(dim));
59 
60  auto const& simplices = std::get<dim-1>(reference_cubes);
61  thread_local std::vector<unsigned int> corners(dim+1);
62  for (auto const& simplex : simplices) {
63  for (std::size_t i = 0; i < simplex.size(); ++i)
64  corners[i] = vertices[simplex[i]];
65 
66  factory_->insertElement(GeometryTypes::simplex(dim), corners);
67  }
68  }
69 
70  using Super::insertElement;
71 
73  // TODO: maybe split boundary segment in simplices
74  void insertBoundarySegment (const std::vector<unsigned int>& vertices) override
75  {
76  factory_->insertBoundarySegment(vertices);
77  }
78 
79  using Super::insertBoundarySegment;
80 
82  std::unique_ptr<GridType> createGrid () override
83  {
84  return factory_->createGrid();
85  }
86 
87  private:
88  std::shared_ptr<GridFactory<GridType>> factory_;
89  };
90 
91 } // end namespace Dune
92 
93 
94 namespace AMDiS
95 {
96  template <class GridType>
98  {
99  using ctype = typename GridType::ctype;
100 
101  enum { dim = GridType::dimension };
102  enum { dimworld = GridType::dimensionworld };
103 
104  public:
106  static std::unique_ptr<GridType> createSimplexGrid (Dune::GridFactory<GridType>& originalFactory,
107  const Dune::FieldVector<ctype,dimworld>& lowerLeft,
108  const Dune::FieldVector<ctype,dimworld>& upperRight,
109  const std::array<unsigned int,dim>& numElements)
110  {
111  Dune::GridFactory<Dune::CubedWrapper<GridType>> factory(originalFactory);
112  Dune::StructuredGridFactory<Dune::CubedWrapper<GridType>>::createCubeGrid(factory, lowerLeft, upperRight, numElements);
113  return std::unique_ptr<GridType>(factory.createGrid());
114  }
115 
117  static std::unique_ptr<GridType> createSimplexGrid (const Dune::FieldVector<ctype,dimworld>& lowerLeft,
118  const Dune::FieldVector<ctype,dimworld>& upperRight,
119  const std::array<unsigned int,dim>& numElements)
120  {
121  Dune::GridFactory<Dune::CubedWrapper<GridType>> factory;
122  Dune::StructuredGridFactory<Dune::CubedWrapper<GridType>>::createCubeGrid(factory, lowerLeft, upperRight, numElements);
123  return std::unique_ptr<GridType>(factory.createGrid());
124  }
125  };
126 
127 } // end namespace AMDiS
Definition: AdaptiveGrid.hpp:373
Definition: FieldMatVec.hpp:12
Definition: AdaptBase.hpp:6
static std::unique_ptr< GridType > createSimplexGrid(Dune::GridFactory< GridType > &originalFactory, const Dune::FieldVector< ctype, dimworld > &lowerLeft, const Dune::FieldVector< ctype, dimworld > &upperRight, const std::array< unsigned int, dim > &numElements)
insert structured simplex grid into grid factory
Definition: MacroGridFactory.hpp:106
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:117
Definition: MacroGridFactory.hpp:97