24#ifndef DUMUX_IO_GRID_MANAGER_SUB_HH
25#define DUMUX_IO_GRID_MANAGER_SUB_HH
30#include <dune/common/shared_ptr.hh>
31#include <dune/common/concept.hh>
35#include <dune/subgrid/subgrid.hh>
39#ifndef DUMUX_IO_GRID_MANAGER_HH
54template<
class Element>
58 auto require(F&& f) ->
decltype(
59 bool(f(std::declval<const Element&>()))
68template <
class HostGr
id,
class HostGr
idManager = Gr
idManager<HostGr
id>>
69class SubGridManagerBase
70:
public GridManagerBase<Dune::SubGrid<HostGrid::dimension, HostGrid>>
72 static constexpr int dim = HostGrid::dimension;
73 using HostElement =
typename HostGrid::template Codim<0>::Entity;
74 using GlobalPosition =
typename HostElement::Geometry::GlobalCoordinate;
83 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
84 void init(HostGrid& hostGrid,
86 const std::string& paramGroup =
"")
88 this->gridPtr() = createGrid_(hostGrid, selector, paramGroup);
96 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
97 void init(
const ES& selector,
98 const std::string& paramGroup =
"")
100 initHostGrid_(paramGroup);
101 this->gridPtr() = createGrid_(hostGridManager_->grid(), selector, paramGroup);
110 if (Dune::MPIHelper::getCommunication().size() > 1)
111 this->grid().loadBalance();
120 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
121 static std::unique_ptr<Grid> createGrid_(HostGrid& hostGrid,
123 const std::string& paramGroup =
"")
126 auto subgridPtr = std::make_unique<Grid>(hostGrid);
129 std::set<typename HostGrid::Traits::GlobalIdSet::IdType> elementsForSubgrid;
130 const auto& globalIDset = subgridPtr->getHostGrid().globalIdSet();
133 subgridPtr->createBegin();
137 auto hostGridView = subgridPtr->getHostGrid().leafGridView();
138 for (
const auto& e : elements(hostGridView))
140 elementsForSubgrid.insert(globalIDset.template id<0>(e));
142 if (elementsForSubgrid.empty())
143 DUNE_THROW(Dune::GridError,
"No elements in subgrid");
145 subgridPtr->insertSetPartial(elementsForSubgrid);
146 subgridPtr->createEnd();
152 void initHostGrid_(
const std::string& paramGroup)
154 hostGridManager_ = std::make_unique<HostGridManager>();
155 hostGridManager_->init(paramGroup);
158 void initHostGrid_(
const GlobalPosition& lowerLeft,
159 const GlobalPosition& upperRight,
160 const std::array<int, dim>& cells,
161 const std::string& paramGroup,
162 const int overlap = 1)
164 hostGridManager_ = std::make_unique<HostGridManager>();
165 hostGridManager_->init(lowerLeft, upperRight, cells, paramGroup, overlap);
171 HostGrid& hostGrid_()
173 return hostGridManager_->grid();
176 std::unique_ptr<HostGridManager> hostGridManager_;
187template<
int dim,
class HostGr
id>
188class GridManager<
Dune::SubGrid<dim, HostGrid>>
189:
public SubGridManagerBase<HostGrid, GridManager<HostGrid>>
202template<
int dim,
class Coordinates>
203class GridManager<
Dune::SubGrid<dim, Dune::YaspGrid<dim, Coordinates>>>
204:
public SubGridManagerBase<Dune::YaspGrid<dim, Coordinates>, GridManager<Dune::YaspGrid<dim, Coordinates>>>
206 using ParentType = SubGridManagerBase<Dune::YaspGrid<dim, Coordinates>,
207 GridManager<Dune::YaspGrid<dim, Coordinates>>>;
209 using typename ParentType::Grid;
210 using ParentType::init;
217 void init(
const std::string& paramGroup =
"")
222 const auto imgFileName = getParamFromGroup<std::string>(paramGroup,
"Grid.Image");
227 DUNE_THROW(Dune::GridError,
"Portable Bitmap Format only supports dim == 2");
231 createGridFromImage_(img, paramGroup);
234 DUNE_THROW(Dune::IOError,
"The SubGridManager doesn't support image files with extension: *." << ext);
238 DUNE_THROW(Dune::IOError,
"SubGridManager couldn't construct element selector. Specify Grid.Image in the input file!");
243 void createGridFromImage_(
const Img& img,
const std::string& paramGroup)
245 using GlobalPosition =
typename ParentType::Grid::template Codim<0>::Geometry::GlobalCoordinate;
249 const std::array<int, dim> imageDimensions{
static_cast<int>(img.header().nCols),
static_cast<int>(img.header().nRows)};
250 std::array<int, dim> cells{imageDimensions[0], imageDimensions[1]};
252 std::array<int, dim> repeatsDefault; repeatsDefault.fill(1);
253 const auto numRepeats = getParamFromGroup<std::array<int, dim>>(paramGroup,
"Grid.Repeat", repeatsDefault);
254 for (
int i = 0; i < dim; i++)
255 cells[i] = cells[i] * numRepeats[i];
258 const auto [lowerLeft, upperRight] = [&]()
260 const auto lowerLeft = getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
263 auto upperRight = getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.PixelDimensions");
264 for (
int i = 0; i < upperRight.size(); ++i)
265 upperRight[i] *= cells[i];
266 upperRight += lowerLeft;
267 return std::make_pair(lowerLeft, upperRight);
270 return std::make_pair(lowerLeft, getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.UpperRight"));
274 this->initHostGrid_(lowerLeft, upperRight, cells, paramGroup);
278 const bool marked = getParamFromGroup<bool>(paramGroup,
"Grid.Marker",
false);
281 const auto elementSelector = [&](
const auto&
element)
283 auto eIdx = this->hostGrid_().leafGridView().indexSet().index(element);
289 auto level0Element =
element.father();
290 while (level0Element.hasFather())
291 level0Element = level0Element.father();
293 assert(level0Element.level() == 0);
294 eIdx = this->hostGrid_().levelGridView(0).indexSet().index(level0Element);
296 return img[eIdx] == marked;
300 const auto repeatedElementSelector = [&](
const auto&
element)
302 auto eIdx = this->hostGrid_().leafGridView().indexSet().index(element);
308 auto level0Element =
element.father();
309 while (level0Element.hasFather())
310 level0Element = level0Element.father();
312 assert(level0Element.level() == 0);
313 eIdx = this->hostGrid_().levelGridView(0).indexSet().index(level0Element);
317 const int numCols = imageDimensions[0];
318 const int numRows = imageDimensions[1];
321 const int repeatUnitIndex = eIdx % (numCols * numRepeats[0] * numRows);
322 const int imgI = repeatUnitIndex % numCols;
323 const int imgJ = repeatUnitIndex / (numCols * numRepeats[0]);
324 return img[ (imgJ * numCols + imgI) ] == marked;
329 this->
gridPtr() = this->createGrid_(this->hostGrid_(), repeatedElementSelector, paramGroup);
331 this->
gridPtr() = this->createGrid_(this->hostGrid_(), elementSelector, paramGroup);
338template<
int dim,
class HostGr
id>
339class BoundaryFlag<
Dune::SubGrid<dim, HostGrid>>
342 BoundaryFlag() : flag_(-1) {}
344 template<
class Intersection>
345 BoundaryFlag(
const Intersection& i) : flag_(-1) {}
350 { DUNE_THROW(Dune::NotImplemented,
"Sub-grid doesn't implement boundary segment indices!"); }
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Boundary flag to store e.g. in sub control volume faces.
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:177
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
Definition: deprecated.hh:149
std::size_t value_type
Definition: boundaryflag.hh:51
value_type get() const
Definition: boundaryflag.hh:53
Definition: consistentlyorientedgrid.hh:35
void loadBalance()
Call loadBalance() function of the grid.
Definition: gridmanager_base.hh:98
std::shared_ptr< Grid > & gridPtr()
Returns a reference to the grid pointer (std::shared_ptr<Grid>)
Definition: gridmanager_base.hh:146
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:75
std::string getFileExtension(const std::string &fileName) const
Returns the filename extension of a given filename.
Definition: gridmanager_base.hh:168
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:145
Convenience header that includes all grid manager specializations.