8 #include <dune/common/filledarray.hh> 9 #include <dune/common/fvector.hh> 10 #include <dune/common/typeutilities.hh> 13 #include <dune/grid/albertagrid/albertareader.hh> 16 #include <dune/grid/io/file/gmshreader.hh> 17 #include <dune/grid/utility/structuredgridfactory.hh> 20 #include <dune/vtk/vtkreader.hh> 21 #include <dune/vtk/gridcreators/lagrangegridcreator.hh> 25 #include <dune/gmsh4/gmsh4reader.hh> 26 #include <dune/gmsh4/gridcreators/lagrangegridcreator.hh> 27 #include <dune/gmsh4/utility/version.hh> 30 #include <amdis/AdaptiveGrid.hpp> 31 #include <amdis/Initfile.hpp> 32 #include <amdis/Output.hpp> 33 #include <amdis/common/Concepts.hpp> 34 #include <amdis/common/Filesystem.hpp> 35 #include <amdis/common/TypeTraits.hpp> 36 #include <amdis/utility/MacroGridFactory.hpp> 41 template <
int dim,
class Coordinates>
class YaspGrid;
42 template <
class ct,
int dim>
class EquidistantCoordinates;
43 template <
class ct,
int dim>
class EquidistantOffsetCoordinates;
44 template <
class ct,
int dim>
class TensorProductCoordinates;
54 enum { dimension = G::dimension };
55 enum { dimworld = G::dimensionworld };
57 using Grid = AdaptiveGrid_t<G>;
58 using HostGrid =
typename Grid::HostGrid;
60 using ctype =
typename Grid::ctype;
71 static std::shared_ptr<Grid>
create(std::string name)
90 auto filename = Parameters::get<std::string>(name_ +
"->macro file name");
91 auto structured = Parameters::get<std::string>(name_ +
"->structured");
92 std::unique_ptr<HostGrid> gridPtr;
95 gridPtr = create_unstructured_grid(filename.value());
96 }
else if (structured) {
98 if (structured.value() ==
"cube") {
99 gridPtr = create_cube_grid();
100 }
else if (structured && structured.value() ==
"simplex") {
101 gridPtr = create_simplex_grid();
103 error_exit(
"Unknown structured grid type. Must be either `cube` or `simplex` in parameter [meshName]->structured.");
107 gridPtr = create_by_gridtype<HostGrid>(Dune::PriorityTag<42>{});
111 auto globalRefinements = Parameters::get<int>(name_ +
"->global refinements");
112 if (globalRefinements)
113 gridPtr->globalRefine(globalRefinements.value());
114 auto loadBalance = Parameters::get<bool>(name_ +
"->load balance");
115 if (loadBalance && loadBalance.value())
116 gridPtr->loadBalance();
118 return construct(std::move(gridPtr));
124 return create_structured_grid([](
auto&& lower,
auto&& upper,
auto&& numCells)
126 return Dune::StructuredGridFactory<HostGrid>::createCubeGrid(lower, upper, numCells);
133 return create_structured_grid([](
auto&& lower,
auto&& upper,
auto&& numCells)
135 return Dune::StructuredGridFactory<HostGrid>::createSimplexGrid(lower, upper, numCells);
152 static std::unique_ptr<Grid> construct(std::unique_ptr<Grid> hostGrid)
154 return std::move(hostGrid);
158 static std::unique_ptr<Grid> construct(std::unique_ptr<HG> hostGrid)
160 return std::make_unique<Grid>(std::move(hostGrid));
164 template <
class Size =
unsigned int,
class Factory>
165 std::unique_ptr<HostGrid> create_structured_grid(Factory factory)
const 167 if constexpr (
int(Grid::dimension) ==
int(Grid::dimensionworld)) {
169 Dune::FieldVector<ctype,int(dimworld)> lower(0);
171 Dune::FieldVector<ctype,int(dimworld)> upper(1);
173 auto numCells = Dune::filledArray<std::size_t(dimension),Size>(1);
175 Parameters::get(name_ +
"->min corner", lower);
176 Parameters::get(name_ +
"->max corner", upper);
177 Parameters::get(name_ +
"->num cells", numCells);
178 return factory(lower, upper, numCells);
180 error_exit(
"Structured grids can be created for dim == dow only.");
186 std::unique_ptr<HostGrid> create_unstructured_grid(std::string
const& filename)
const 193 if (Dune::Gmsh4::fileVersion(filename)[0] >= 4)
194 return Dune::Gmsh4Reader<HostGrid, Dune::Gmsh4::LagrangeGridCreator<HostGrid>>::createGridFromFile(filename);
197 return read_gmsh_file<HostGrid>(filename, Dune::PriorityTag<42>{});
200 else if (ext ==
".vtu") {
201 return Dune::VtkReader<HostGrid, Dune::Vtk::LagrangeGridCreator<HostGrid>>::createGridFromFile(filename);
204 else if (ext ==
".1d" || ext ==
".2d" || ext ==
".3d" || ext ==
".amc") {
205 return read_alberta_file<HostGrid>(filename, Dune::PriorityTag<42>{});
208 error_exit(
"Cannot read grid file. Unknown file extension.");
213 template <
class Gr
idType>
214 using SupportsGmshReader = decltype(std::declval<Dune::GridFactory<GridType>>().insertBoundarySegment(
215 std::declval<std::vector<unsigned int>>(),
216 std::declval<std::shared_ptr<Dune::BoundarySegment<GridType::dimension, GridType::dimensionworld> >>()) );
218 template <
class Gr
idType,
class LC>
219 using SupportsInsertionIndex = decltype(std::declval<Dune::GridFactory<GridType>>().insertionIndex(std::declval<LC>()));
222 template <
class GridType = HostGrid,
223 REQUIRES(Dune::Std::is_detected<SupportsGmshReader, GridType>::value)>
224 std::unique_ptr<GridType> read_gmsh_file(std::string
const& filename, Dune::PriorityTag<1>)
const 226 Dune::GmshReader<GridType> reader;
227 Dune::GridFactory<GridType> factory;
228 std::vector<int> boundaryIds, elementIds;
229 reader.read(factory, filename, boundaryIds, elementIds);
231 using HasInsertionIndexEntity
232 = Dune::Std::is_detected<SupportsInsertionIndex, GridType, typename GridType::template Codim<0>::Entity>;
233 using HasInsertionIndexIntersection
234 = Dune::Std::is_detected<SupportsInsertionIndex, GridType, typename GridType::LeafIntersection>;
236 auto gridPtr = factory.createGrid();
237 if ((boundaryIds.empty() && elementIds.empty()) ||
238 (!HasInsertionIndexEntity::value && !HasInsertionIndexIntersection::value))
239 return std::unique_ptr<GridType>(std::move(gridPtr));
242 if (!boundaryIds.empty() && HasInsertionIndexIntersection::value)
243 boundaryIds_.resize(gridPtr->numBoundarySegments());
244 if (!elementIds.empty() && HasInsertionIndexEntity::value)
245 elementIds_.resize(gridPtr->size(0));
247 auto const& indexSet = gridPtr->leafIndexSet();
248 for (
auto const& e : elements(gridPtr->leafGridView())) {
249 if constexpr(HasInsertionIndexEntity::value) {
250 if (!elementIds.empty())
251 elementIds_[indexSet.index(e)] = elementIds[factory.insertionIndex(e)];
254 if (boundaryIds.empty() || !e.hasBoundaryIntersections())
257 for (
auto const& it : intersections(gridPtr->leafGridView(), e)) {
258 if constexpr(HasInsertionIndexIntersection::value) {
260 boundaryIds_[it.boundarySegmentIndex()]
261 = boundaryIds[factory.insertionIndex(it)];
266 return std::unique_ptr<GridType>(std::move(gridPtr));
270 template <
class Gr
idType = HostGr
id>
271 std::unique_ptr<GridType> read_gmsh_file(std::string
const&, Dune::PriorityTag<0>)
const 273 error_exit(
"Gmsh reader not supported for this grid type!");
280 template <
class Gr
idType>
281 using IsAlbertaGrid = decltype(std::declval<GridType>().alberta2dune(0,0));
284 template <
class GridType = HostGrid,
285 REQUIRES(GridType::dimensionworld == DIM_OF_WORLD),
286 REQUIRES(Dune::Std::is_detected<IsAlbertaGrid, GridType>::value)>
287 std::unique_ptr<GridType> read_alberta_file(std::string
const& filename, Dune::PriorityTag<3>)
const 289 return std::make_unique<GridType>(filename);
293 template <
class GridType = HostGrid,
294 REQUIRES(GridType::dimensionworld == DIM_OF_WORLD)>
295 std::unique_ptr<GridType> read_alberta_file(std::string
const& filename, Dune::PriorityTag<2>)
const 297 Dune::GridFactory<GridType> factory;
298 if (Environment::mpiRank() == 0) {
299 Dune::AlbertaReader<GridType> reader;
300 reader.readGrid(filename, factory);
302 return std::unique_ptr<GridType>{factory.createGrid()};
306 template <
class GridType = HostGrid,
307 REQUIRES(GridType::dimensionworld != DIM_OF_WORLD)>
308 std::unique_ptr<GridType> read_alberta_file(std::string
const& filename, Dune::PriorityTag<1>)
const 310 error_exit(
"AlbertaGrid (and AlbertaReader) require WORLDDIM == Grid::dimensionworld. Change the cmake parameters!");
315 template <
class Gr
idType>
316 std::unique_ptr<GridType> read_alberta_file(std::string
const&, Dune::PriorityTag<0>)
const 318 error_exit(
"AlbertaGrid (and AlbertaReader) not available. Set AlbertaFlags to your executable in cmake!");
326 template <
class GridType = HostGrid,
327 REQUIRES(Dune::Std::is_detected<IsAlbertaGrid, GridType>::value)>
328 std::unique_ptr<GridType> create_by_gridtype(Dune::PriorityTag<3>)
const 330 return create_structured_grid([](
auto&& lower,
auto&& upper,
auto&& numCells)
338 template <
class GridType = HostGrid,
339 class =
typename GridType::YGrid>
340 std::unique_ptr<GridType> create_by_gridtype(Dune::PriorityTag<2>)
const 343 Parameters::get(name_ +
"->overlap", overlap);
344 std::string periodic(dimension,
'0');
345 Parameters::get(name_ +
"->periodic", periodic);
347 return create_yaspgrid(
Types<GridType>{}, overlap, std::bitset<dimension>(periodic));
350 template <
int dim,
class ct>
351 std::unique_ptr<HostGrid> create_yaspgrid(
Types<Dune::YaspGrid<dim,Dune::EquidistantCoordinates<ct,dim>>>,
352 int overlap, std::bitset<dimension>
const& per)
const 354 return create_structured_grid<int>([&](
auto&& ,
auto&& upper, std::array<int,dimension>
const& numCells)
356 return std::make_unique<HostGrid>(upper, numCells, per, overlap);
360 template <
int dim,
class ct>
361 std::unique_ptr<HostGrid> create_yaspgrid(
Types<Dune::YaspGrid<dim,Dune::EquidistantOffsetCoordinates<ct,dim>>>,
362 int overlap, std::bitset<dimension>
const& per)
const 364 return create_structured_grid<int>([&](
auto&& lower,
auto&& upper, std::array<int,dimension>
const& numCells)
366 return std::make_unique<HostGrid>(lower, upper, numCells, per, overlap);
370 template <
int dim,
class ct>
371 std::unique_ptr<HostGrid> create_yaspgrid(
Types<Dune::YaspGrid<dim,Dune::TensorProductCoordinates<ct,dim>>>,
372 int, std::bitset<dimension>
const&)
const 374 error_exit(
"MeshCreator cannot create YaspGrid with TensorProductCoordinates.");
381 template <
class GridType = HostGrid,
382 class =
typename GridType::ReferenceCube,
384 std::unique_ptr<GridType> create_by_gridtype(Dune::PriorityTag<1>)
const 386 return create_structured_grid<int>([](
auto&& lower,
auto&& upper, std::array<int,dimension>
const& numCells)
389 return std::make_unique<GridType>(lower, upper, multiIndex);
395 template <
class Gr
idType = HostGr
id>
396 std::unique_ptr<GridType> create_by_gridtype(Dune::PriorityTag<0>)
const 398 error_exit(
"Don't know how to create the grid.");
404 mutable std::vector<int> boundaryIds_;
405 mutable std::vector<int> elementIds_;
constexpr bool MultiIndex
A multi-index type.
Definition: Concepts.hpp:150
path extension() const
Returns the file extension path component.
Definition: Filesystem.cpp:83
Definition: AdaptiveGrid.hpp:373
A variadic type list.
Definition: TypeTraits.hpp:128
Definition: AdaptBase.hpp:6
std::unique_ptr< HostGrid > create_simplex_grid() const
Create a structured simplex grid.
Definition: MeshCreator.hpp:131
std::vector< int > const & elementIds() const
Return the filled vector of element ids. NOTE: not all creators support reading this.
Definition: MeshCreator.hpp:146
A creator class for dune grids.
Definition: MeshCreator.hpp:52
static std::shared_ptr< Grid > create(std::string name)
Static create mthod. See create()
Definition: MeshCreator.hpp:71
std::unique_ptr< HostGrid > create_cube_grid() const
Create a structured cube grid.
Definition: MeshCreator.hpp:122
Definition: Filesystem.hpp:11
MeshCreator(std::string const &name)
Construct a new MeshCreator.
Definition: MeshCreator.hpp:66
std::unique_ptr< Grid > create() const
Create a new grid by inspecting the intifile parameter group [meshName]
Definition: MeshCreator.hpp:88
std::vector< int > const & boundaryIds() const
Return the filled vector of boundary ids. NOTE: not all creators support reading this.
Definition: MeshCreator.hpp:140
Definition: MacroGridFactory.hpp:97