dune-vtk  0.2
gridcreatorinterface.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <string>
5 #include <vector>
6 
7 #include <dune/common/version.hh>
8 #include <dune/common/parallel/mpihelper.hh>
9 #include <dune/grid/common/gridfactory.hh>
10 
11 namespace Dune
12 {
13  namespace Vtk
14  {
16 
23  template <class GridType, class DerivedType>
25  {
26  public:
27  using Grid = GridType;
28  using GlobalCoordinate = typename Grid::template Codim<0>::Entity::Geometry::GlobalCoordinate;
29  using Derived = DerivedType;
30 
31  public:
33  GridCreatorInterface (GridFactory<Grid>& factory)
34  : factory_(stackobject_to_shared_ptr(factory))
35  {}
36 
38  GridCreatorInterface (std::shared_ptr<GridFactory<Grid>> factory)
39  : factory_(std::move(factory))
40  {}
41 
43  template <class... Args,
44  std::enable_if_t<std::is_constructible<GridFactory<Grid>, Args...>::value,int> = 0>
45  GridCreatorInterface (Args&&... args)
46  : factory_(std::make_shared<GridFactory<Grid>>(std::forward<Args>(args)...))
47  {}
48 
50  void insertVertices (std::vector<GlobalCoordinate> const& points,
51  std::vector<std::uint64_t> const& point_ids)
52  {
53  asDerived().insertVerticesImpl(points, point_ids);
54  }
55 
57  void insertElements (std::vector<std::uint8_t> const& types,
58  std::vector<std::int64_t> const& offsets,
59  std::vector<std::int64_t> const& connectivity)
60  {
61  asDerived().insertElementsImpl(types, offsets, connectivity);
62  }
63 
65  void insertPieces (std::vector<std::string> const& pieces)
66  {
67  asDerived().insertPiecesImpl(pieces);
68  }
69 
71  std::unique_ptr<Grid> createGrid () const
72  {
73  return std::unique_ptr<Grid>(factory_->createGrid());
74  }
75 
77  GridFactory<Grid>& factory ()
78  {
79  return *factory_;
80  }
81 
83  GridFactory<Grid> const& factory () const
84  {
85  return *factory_;
86  }
87 
89  auto comm () const
90  {
91  return MPIHelper::getCollectiveCommunication();
92  }
93 
94  protected: // cast to derived type
95 
97  {
98  return static_cast<Derived&>(*this);
99  }
100 
101  const Derived& asDerived () const
102  {
103  return static_cast<const Derived&>(*this);
104  }
105 
106  public: // default implementations
107 
108  void insertVerticesImpl (std::vector<GlobalCoordinate> const&,
109  std::vector<std::uint64_t> const&)
110  {
111  /* do nothing */
112  }
113 
114  void insertElementsImpl (std::vector<std::uint8_t> const&,
115  std::vector<std::int64_t> const&,
116  std::vector<std::int64_t> const&)
117  {
118  /* do nothing */
119  }
120 
121  void insertPiecesImpl (std::vector<std::string> const&)
122  {
123  /* do nothing */;
124  }
125 
126  protected:
127  std::shared_ptr<GridFactory<Grid>> factory_;
128  };
129 
130  } // end namespace Vtk
131 } // end namespace Dune
Definition: datacollectorinterface.hh:9
Base class for grid creators in a CRTP style.
Definition: gridcreatorinterface.hh:25
const Derived & asDerived() const
Definition: gridcreatorinterface.hh:101
void insertElementsImpl(std::vector< std::uint8_t > const &, std::vector< std::int64_t > const &, std::vector< std::int64_t > const &)
Definition: gridcreatorinterface.hh:114
GridCreatorInterface(Args &&... args)
Constructor. Construct a new GridFactory from the passed arguments.
Definition: gridcreatorinterface.hh:45
typename Grid::template Codim< 0 >::Entity::Geometry::GlobalCoordinate GlobalCoordinate
Definition: gridcreatorinterface.hh:28
void insertPiecesImpl(std::vector< std::string > const &)
Definition: gridcreatorinterface.hh:121
std::unique_ptr< Grid > createGrid() const
Construct the actual grid using the GridFactory.
Definition: gridcreatorinterface.hh:71
GridCreatorInterface(std::shared_ptr< GridFactory< Grid >> factory)
Constructor. Store the shared_ptr to the GridFactory.
Definition: gridcreatorinterface.hh:38
std::shared_ptr< GridFactory< Grid > > factory_
Definition: gridcreatorinterface.hh:127
auto comm() const
Return the mpi collective communicator.
Definition: gridcreatorinterface.hh:89
void insertPieces(std::vector< std::string > const &pieces)
Insert part of a grid stored in file into factory.
Definition: gridcreatorinterface.hh:65
GridCreator::Grid Grid
Definition: gridcreatorinterface.hh:27
GridCreatorInterface(GridFactory< Grid > &factory)
Constructor. Stores a reference to the passed GridFactory.
Definition: gridcreatorinterface.hh:33
void insertVerticesImpl(std::vector< GlobalCoordinate > const &, std::vector< std::uint64_t > const &)
Definition: gridcreatorinterface.hh:108
GridFactory< Grid > const & factory() const
Return the associated (const) GridFactory.
Definition: gridcreatorinterface.hh:83
GridFactory< Grid > & factory()
Return the associated GridFactory.
Definition: gridcreatorinterface.hh:77
Derived & asDerived()
Definition: gridcreatorinterface.hh:96
void insertVertices(std::vector< GlobalCoordinate > const &points, std::vector< std::uint64_t > const &point_ids)
Insert all points as vertices into the factory.
Definition: gridcreatorinterface.hh:50
void insertElements(std::vector< std::uint8_t > const &types, std::vector< std::int64_t > const &offsets, std::vector< std::int64_t > const &connectivity)
Create elements based on type and connectivity description.
Definition: gridcreatorinterface.hh:57
Definition: parallelgridcreator.hh:21