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)
246 const std::array<int, dim> cells{
static_cast<int>(img.header().nCols),
static_cast<int>(img.header().nRows)};
249 const auto [lowerLeft, upperRight] = [&]()
251 using GlobalPosition =
typename ParentType::Grid::template Codim<0>::Geometry::GlobalCoordinate;
252 const auto lowerLeft = getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.LowerLeft", GlobalPosition(0.0));
255 auto upperRight = getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.PixelDimensions");
256 for (
int i = 0; i < upperRight.size(); ++i)
257 upperRight[i] *= cells[i];
258 upperRight += lowerLeft;
259 return std::make_pair(lowerLeft, upperRight);
262 return std::make_pair(lowerLeft, getParamFromGroup<GlobalPosition>(paramGroup,
"Grid.UpperRight"));
266 this->initHostGrid_(lowerLeft, upperRight, cells, paramGroup);
270 const bool marked = getParamFromGroup<bool>(paramGroup,
"Grid.Marker",
false);
273 auto elementSelector = [
this, &img, &marked](
const auto&
element)
275 auto eIdx = this->hostGrid_().leafGridView().indexSet().index(element);
281 auto level0Element =
element.father();
282 while (level0Element.hasFather())
283 level0Element = level0Element.father();
285 assert(level0Element.level() == 0);
286 eIdx = this->hostGrid_().levelGridView(0).indexSet().index(level0Element);
289 return img[eIdx] == marked;
293 this->
gridPtr() = this->createGrid_(this->hostGrid_(), elementSelector, paramGroup);
300template<
int dim,
class HostGr
id>
301class BoundaryFlag<
Dune::SubGrid<dim, HostGrid>>
304 BoundaryFlag() : flag_(-1) {}
306 template<
class Intersection>
307 BoundaryFlag(
const Intersection& i) : flag_(-1) {}
312 { 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:177
Definition: common/pdesolver.hh:36
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
Convience header that includes all grid manager specializations.