12#ifndef DUMUX_IO_GRID_MANAGER_SUB_HH
13#define DUMUX_IO_GRID_MANAGER_SUB_HH
18#include <dune/common/shared_ptr.hh>
19#include <dune/common/concept.hh>
20#include <dune/grid/yaspgrid.hh>
24#include <dune/subgrid/subgrid.hh>
42template<
class Element>
46 auto require(F&& f) ->
decltype(
47 bool(f(std::declval<const Element&>()))
56template <
class HostGr
id,
class HostGr
idManager = Gr
idManager<HostGr
id>>
57class SubGridManagerBase
58:
public GridManagerBase<Dune::SubGrid<HostGrid::dimension, HostGrid>>
60 static constexpr int dim = HostGrid::dimension;
61 using HostElement =
typename HostGrid::template Codim<0>::Entity;
62 using GlobalPosition =
typename HostElement::Geometry::GlobalCoordinate;
71 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
72 void init(HostGrid& hostGrid,
74 const std::string& paramGroup =
"")
76 this->gridPtr() = createGrid_(hostGrid, selector, paramGroup);
84 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
85 void init(
const ES& selector,
86 const std::string& paramGroup =
"")
88 initHostGrid_(paramGroup);
89 this->gridPtr() = createGrid_(hostGridManager_->grid(), selector, paramGroup);
98 if (Dune::MPIHelper::getCommunication().size() > 1)
99 this->grid().loadBalance();
108 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
109 static std::unique_ptr<Grid> createGrid_(HostGrid& hostGrid,
111 const std::string& paramGroup =
"")
114 auto subgridPtr = std::make_unique<Grid>(hostGrid);
117 std::set<typename HostGrid::Traits::GlobalIdSet::IdType> elementsForSubgrid;
118 const auto& globalIDset = subgridPtr->getHostGrid().globalIdSet();
121 subgridPtr->createBegin();
125 auto hostGridView = subgridPtr->getHostGrid().leafGridView();
126 for (
const auto& e : elements(hostGridView))
128 elementsForSubgrid.insert(globalIDset.template id<0>(e));
130 if (elementsForSubgrid.empty())
131 DUNE_THROW(Dune::GridError,
"No elements in subgrid");
133 subgridPtr->insertSetPartial(elementsForSubgrid);
134 subgridPtr->createEnd();
140 void initHostGrid_(
const std::string& paramGroup)
142 hostGridManager_ = std::make_unique<HostGridManager>();
143 hostGridManager_->init(paramGroup);
146 void initHostGrid_(
const GlobalPosition& lowerLeft,
147 const GlobalPosition& upperRight,
148 const std::array<int, dim>& cells,
149 const std::string& paramGroup,
150 const int overlap = 1)
152 hostGridManager_ = std::make_unique<HostGridManager>();
153 hostGridManager_->init(lowerLeft, upperRight, cells, paramGroup, overlap);
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)
233 using GlobalPosition =
typename ParentType::Grid::template Codim<0>::Geometry::GlobalCoordinate;
237 const std::array<int, dim> imageDimensions{
static_cast<int>(img.header().nCols),
static_cast<int>(img.header().nRows)};
238 std::array<int, dim> cells{imageDimensions[0], imageDimensions[1]};
240 std::array<int, dim> repeatsDefault; repeatsDefault.fill(1);
241 const auto numRepeats = getParamFromGroup<std::array<int, dim>>(paramGroup,
"Grid.Repeat", repeatsDefault);
242 for (
int i = 0; i < dim; i++)
243 cells[i] = cells[i] * numRepeats[i];
246 const auto [lowerLeft, upperRight] = [&]()
248 const auto lowerLeft = getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
251 auto upperRight = getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.PixelDimensions");
252 for (
int i = 0; i < upperRight.size(); ++i)
253 upperRight[i] *= cells[i];
254 upperRight += lowerLeft;
255 return std::make_pair(lowerLeft, upperRight);
258 return std::make_pair(lowerLeft, getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.UpperRight"));
262 this->initHostGrid_(lowerLeft, upperRight, cells, paramGroup);
266 const bool marked = getParamFromGroup<bool>(paramGroup,
"Grid.Marker",
false);
269 const auto elementSelector = [&](
const auto&
element)
271 auto eIdx = this->hostGrid_().leafGridView().indexSet().index(element);
277 auto level0Element =
element.father();
278 while (level0Element.hasFather())
279 level0Element = level0Element.father();
281 assert(level0Element.level() == 0);
282 eIdx = this->hostGrid_().levelGridView(0).indexSet().index(level0Element);
284 return img[eIdx] == marked;
288 const auto repeatedElementSelector = [&](
const auto&
element)
290 auto eIdx = this->hostGrid_().leafGridView().indexSet().index(element);
296 auto level0Element =
element.father();
297 while (level0Element.hasFather())
298 level0Element = level0Element.father();
300 assert(level0Element.level() == 0);
301 eIdx = this->hostGrid_().levelGridView(0).indexSet().index(level0Element);
305 const int numCols = imageDimensions[0];
306 const int numRows = imageDimensions[1];
309 const int repeatUnitIndex = eIdx % (numCols * numRepeats[0] * numRows);
310 const int imgI = repeatUnitIndex % numCols;
311 const int imgJ = repeatUnitIndex / (numCols * numRepeats[0]);
312 return img[ (imgJ * numCols + imgI) ] == marked;
317 this->
gridPtr() = this->createGrid_(this->hostGrid_(), repeatedElementSelector, paramGroup);
319 this->
gridPtr() = this->createGrid_(this->hostGrid_(), elementSelector, paramGroup);
326template<
int dim,
class HostGr
id>
327class BoundaryFlag<
Dune::SubGrid<dim, HostGrid>>
330 BoundaryFlag() : flag_(-1) {}
332 template<
class Intersection>
333 BoundaryFlag(
const Intersection& i) : flag_(-1) {}
338 { DUNE_THROW(Dune::NotImplemented,
"Sub-grid doesn't implement boundary segment indices!"); }
Boundary flag to store e.g. in sub control volume faces.
std::size_t value_type
Definition: boundaryflag.hh:39
value_type get() const
Definition: boundaryflag.hh:41
void loadBalance()
Call loadBalance() function of the grid.
Definition: gridmanager_base.hh:86
std::shared_ptr< Grid > & gridPtr()
Returns a reference to the grid pointer (std::shared_ptr<Grid>)
Definition: gridmanager_base.hh:134
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:63
std::string getFileExtension(const std::string &fileName) const
Returns the filename extension of a given filename.
Definition: gridmanager_base.hh:156
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:81
Definition: consistentlyorientedgrid.hh:23
Provides a grid manager for all supported grid managers with input file interfaces....
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:165
Definition: common/pdesolver.hh:24
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
A simple reader class for raster images.
A simple writer class for raster images.