24#ifndef DUMUX_IO_GRID_MANAGER_SUB_HH
25#define DUMUX_IO_GRID_MANAGER_SUB_HH
29#include <dune/common/shared_ptr.hh>
30#include <dune/common/concept.hh>
34#include <dune/subgrid/subgrid.hh>
38#ifndef DUMUX_IO_GRID_MANAGER_HH
53template<
class Element>
57 auto require(F&& f) ->
decltype(
58 bool(f(std::declval<const Element&>()))
67template <
class HostGr
id,
class HostGr
idManager = Gr
idManager<HostGr
id>>
68class SubGridManagerBase
69:
public GridManagerBase<Dune::SubGrid<HostGrid::dimension, HostGrid>>
71 static constexpr int dim = HostGrid::dimension;
72 using HostElement =
typename HostGrid::template Codim<0>::Entity;
75 using Grid = Dune::SubGrid<dim, HostGrid>;
81 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
82 void init(HostGrid& hostGrid,
84 const std::string& paramGroup =
"")
86 this->gridPtr() = createGrid_(hostGrid, selector, paramGroup);
94 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
95 void init(
const ES& selector,
96 const std::string& paramGroup =
"")
98 initHostGrid_(paramGroup);
99 this->gridPtr() = createGrid_(hostGridManager_->grid(), selector, paramGroup);
108 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
109 this->grid().loadBalance();
118 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
119 static std::unique_ptr<Grid> createGrid_(HostGrid& hostGrid,
121 const std::string& paramGroup =
"")
124 auto subgridPtr = std::make_unique<Grid>(hostGrid);
127 std::set<typename HostGrid::Traits::GlobalIdSet::IdType> elementsForSubgrid;
128 const auto& globalIDset = subgridPtr->getHostGrid().globalIdSet();
131 subgridPtr->createBegin();
135 auto hostGridView = subgridPtr->getHostGrid().leafGridView();
136 for (
const auto& e : elements(hostGridView))
138 elementsForSubgrid.insert(globalIDset.template id<0>(e));
140 if (elementsForSubgrid.empty())
141 DUNE_THROW(Dune::GridError,
"No elements in subgrid");
143 subgridPtr->insertSetPartial(elementsForSubgrid);
144 subgridPtr->createEnd();
150 void initHostGrid_(
const std::string& paramGroup)
152 hostGridManager_ = std::make_unique<HostGridManager>();
153 hostGridManager_->init(paramGroup);
159 HostGrid& hostGrid_()
161 return hostGridManager_->grid();
164 std::unique_ptr<HostGridManager> hostGridManager_;
175template<
int dim,
class HostGr
id>
176class GridManager<
Dune::SubGrid<dim, HostGrid>>
177:
public SubGridManagerBase<HostGrid, GridManager<HostGrid>>
190template<
int dim,
class Coordinates>
191class GridManager<
Dune::SubGrid<dim, Dune::YaspGrid<dim, Coordinates>>>
192:
public SubGridManagerBase<Dune::YaspGrid<dim, Coordinates>, GridManager<Dune::YaspGrid<dim, Coordinates>>>
194 using ParentType = SubGridManagerBase<Dune::YaspGrid<dim, Coordinates>,
195 GridManager<Dune::YaspGrid<dim, Coordinates>>>;
197 using typename ParentType::Grid;
198 using ParentType::init;
205 void init(
const std::string& paramGroup =
"")
210 const auto imgFileName = getParamFromGroup<std::string>(paramGroup,
"Grid.Image");
215 DUNE_THROW(Dune::GridError,
"Portable Bitmap Format only supports dim == 2");
219 createGridFromImage_(img, paramGroup);
222 DUNE_THROW(Dune::IOError,
"The SubGridManager doesn't support image files with extension: *." << ext);
226 DUNE_THROW(Dune::IOError,
"SubGridManager couldn't construct element selector. Specify Grid.Image in the input file!");
231 void createGridFromImage_(
const Img& img,
const std::string& paramGroup)
234 std::array<int, dim> cells; cells.fill(1);
235 cells = getParamFromGroup<std::array<int, dim>>(paramGroup,
"Grid.Cells", cells);
237 if (img.header().nCols != cells[0])
238 DUNE_THROW(Dune::GridError,
"Host grid has wrong number of cells in x-direction. Expected "
239 << img.header().nCols <<
", got " << cells[0]);
240 if (img.header().nRows != cells[1])
241 DUNE_THROW(Dune::GridError,
"Host grid has wrong number of cells in y-direction. Expected "
242 << img.header().nRows <<
", got " << cells[1]);
246 this->initHostGrid_(paramGroup);
250 const bool marked = getParamFromGroup<bool>(paramGroup,
"Grid.Marker", 0);
253 auto elementSelector = [
this, &img, &marked](
const auto& element)
255 const auto eIdx = this->hostGrid_().leafGridView().indexSet().index(element);
256 return img[eIdx] == marked;
260 this->
gridPtr() = this->createGrid_(this->hostGrid_(), elementSelector, paramGroup);
266template<
int dim,
class HostGr
id>
267class BoundaryFlag<
Dune::SubGrid<dim, HostGrid>>
270 BoundaryFlag() : flag_(-1) {}
272 template<
class Intersection>
273 BoundaryFlag(
const Intersection& i) : flag_(-1) {}
278 { DUNE_THROW(Dune::NotImplemented,
"Sub-grid doesn't implement boundary segment indices!"); }
Boundary flag to store e.g. in sub control volume faces.
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
A simple reader class for raster images.
bool hasParamInGroup(const std::string ¶mGroup, const std::string ¶m)
Check whether a key exists in the parameter tree with a model group prefix.
Definition: parameters.hh:391
Definition: common/pdesolver.hh:35
std::size_t value_type
Definition: boundaryflag.hh:51
value_type get() const
Definition: boundaryflag.hh:53
void loadBalance()
Call loadBalance() function of the grid.
Definition: gridmanager_base.hh:103
std::shared_ptr< Grid > & gridPtr()
Returns a reference to the grid pointer (std::shared_ptr<Grid>)
Definition: gridmanager_base.hh:142
void init(const std::string &modelParamGroup="")
Make the grid. Implement this method in the specialization of this class for a grid type.
Definition: gridmanager_base.hh:81
std::string getFileExtension(const std::string &fileName) const
Returns the filename extension of a given filename.
Definition: gridmanager_base.hh:164
static Result< bool > readPBM(const std::string &fileName, const bool useDuneGridOrdering=true)
Reads a *pbm (black and white) in ASCII or binary encoding. Returns a struct that contains both the p...
Definition: rasterimagereader.hh:142
Convience header that includes all grid manager specializations.