24#ifndef DUMUX_IO_GRID_MANAGER_SUB_HH
25#define DUMUX_IO_GRID_MANAGER_SUB_HH
29#include <dune/common/deprecated.hh>
30#include <dune/common/shared_ptr.hh>
31#include <dune/common/concept.hh>
35#include <dune/subgrid/subgrid.hh>
38#include <dune/grid/io/file/vtk.hh>
40#include <dune/grid/io/file/dgfparser/dgfwriter.hh>
43#ifndef DUMUX_IO_GRID_MANAGER_HH
58template<
class Element>
62 auto require(F&& f) ->
decltype(
63 bool(f(std::declval<const Element&>()))
72template <
class HostGr
id,
class HostGr
idManager = Gr
idManager<HostGr
id>>
73class SubGridManagerBase
76 static constexpr int dim = HostGrid::dimension;
77 using HostElement =
typename HostGrid::template Codim<0>::Entity;
80 using Grid = Dune::SubGrid<dim, HostGrid>;
86 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
87 void init(HostGrid& hostGrid,
89 const std::string& paramGroup =
"")
91 this->gridPtr() = createGrid_(hostGrid, selector, paramGroup);
99 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
100 void init(
const ES& selector,
101 const std::string& paramGroup =
"")
103 initHostGrid_(paramGroup);
104 this->gridPtr() = createGrid_(hostGridManager_->grid(), selector, paramGroup);
111 template<
class ElementSelector>
112 [[deprecated(
"Create an instance of this class and use subgridManager.init(hostGrid, selector, paramGroup)")]]
113 static std::unique_ptr<Grid> makeGrid(HostGrid& hostGrid,
114 const ElementSelector& selector,
115 const std::string& paramGroup =
"")
117 std::cerr <<
"Deprecation warning: SubGridManager::makeGrid is deprecated."
118 <<
"Create an instance of this class and use subgridManager.init(hostGrid, selector, paramGroup)." << std::endl;
120 auto subgridPtr = createGrid_(hostGrid, selector, paramGroup);
124 if (getParamFromGroup<bool>(paramGroup,
"Grid.WriteSubGridToDGF",
false))
126 std::cerr <<
"Deprecation warning: SubGridManager: Grid.WriteSubGridToDGF is deprecated."
127 <<
"Use Dune::VTKWriter to write out your grid manually." << std::endl;
129 const auto postfix = getParamFromGroup<std::string>(paramGroup,
"Problem.Name",
"");
130 const std::string name = postfix ==
"" ?
"subgrid" :
"subgrid_" + postfix;
131 Dune::DGFWriter<typename Grid::LeafGridView> writer(subgridPtr->leafGridView());
132 writer.write(name +
".dgf");
137 if (getParamFromGroup<bool>(paramGroup,
"Grid.WriteSubGridToVtk",
false))
139 std::cerr <<
"Deprecation warning: SubGridManager: Grid.WriteSubGridToVtk is deprecated."
140 <<
"Use Dune::VTKWriter to write out your grid manually." << std::endl;
142 const auto postfix = getParamFromGroup<std::string>(paramGroup,
"Problem.Name",
"");
143 const std::string name = postfix ==
"" ?
"subgrid" :
"subgrid_" + postfix;
144 Dune::VTKWriter<typename Grid::LeafGridView> vtkWriter(subgridPtr->leafGridView());
145 vtkWriter.write(name);
157 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
158 this->grid().loadBalance();
167 typename std::enable_if_t<Dune::models<Concept::ElementSelector<HostElement>, ES>(),
int> = 0>
168 static std::unique_ptr<Grid> createGrid_(HostGrid& hostGrid,
170 const std::string& paramGroup =
"")
173 auto subgridPtr = std::make_unique<Grid>(hostGrid);
176 std::set<typename HostGrid::Traits::GlobalIdSet::IdType> elementsForSubgrid;
177 const auto& globalIDset = subgridPtr->getHostGrid().globalIdSet();
180 subgridPtr->createBegin();
184 auto hostGridView = subgridPtr->getHostGrid().leafGridView();
185 for (
const auto& e : elements(hostGridView))
187 elementsForSubgrid.insert(globalIDset.template id<0>(e));
189 if (elementsForSubgrid.empty())
190 DUNE_THROW(Dune::GridError,
"No elements in subgrid");
192 subgridPtr->insertSetPartial(elementsForSubgrid);
193 subgridPtr->createEnd();
199 void initHostGrid_(
const std::string& paramGroup)
201 hostGridManager_ = std::make_unique<HostGridManager>();
202 hostGridManager_->init(paramGroup);
208 HostGrid& hostGrid_()
210 return hostGridManager_->grid();
213 std::unique_ptr<HostGridManager> hostGridManager_;
224template<
int dim,
class HostGr
id>
226:
public SubGridManagerBase<HostGrid, GridManager<HostGrid>>
239template<
int dim,
class Coordinates>
240class GridManager<Dune::SubGrid<dim, Dune::YaspGrid<dim, Coordinates>>>
241:
public SubGridManagerBase<Dune::YaspGrid<dim, Coordinates>, GridManager<Dune::YaspGrid<dim, Coordinates>>>
243 using ParentType = SubGridManagerBase<Dune::YaspGrid<dim, Coordinates>,
244 GridManager<Dune::YaspGrid<dim, Coordinates>>>;
246 using typename ParentType::Grid;
247 using ParentType::init;
254 void init(
const std::string& paramGroup =
"")
264 DUNE_THROW(Dune::GridError,
"Portable Bitmap Format only supports dim == 2");
268 createGridFromImage_(img, paramGroup);
271 DUNE_THROW(Dune::IOError,
"The SubGridManager doesn't support image files with extension: *." << ext);
275 DUNE_THROW(Dune::IOError,
"SubGridManager couldn't construct element selector. Specify Grid.Image in the input file!");
280 void createGridFromImage_(
const Img& img,
const std::string& paramGroup)
283 std::array<int, dim> cells; cells.fill(1);
286 if (img.header().nCols != cells[0])
287 DUNE_THROW(Dune::GridError,
"Host grid has wrong number of cells in x-direction. Expected "
288 << img.header().nCols <<
", got " << cells[0]);
289 if (img.header().nRows != cells[1])
290 DUNE_THROW(Dune::GridError,
"Host grid has wrong number of cells in y-direction. Expected "
291 << img.header().nRows <<
", got " << cells[1]);
295 this->initHostGrid_(paramGroup);
302 auto elementSelector = [
this, &img, &marked](
const auto& element)
304 const auto eIdx = this->hostGrid_().leafGridView().indexSet().index(element);
305 return img[eIdx] == marked;
309 this->
gridPtr() = this->createGrid_(this->hostGrid_(), elementSelector, paramGroup);
315template<
int dim,
class HostGr
id>
319 BoundaryFlag() : flag_(-1) {}
321 template<
class Intersection>
322 BoundaryFlag(
const Intersection& i) : flag_(-1) {}
327 { 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.
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition parameters.hh:438
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:454
make the local view function available whenever we use the grid geometry
Definition adapt.hh:29
std::size_t value_type
Definition boundaryflag.hh:51
value_type get() const
Definition boundaryflag.hh:53
Definition boundaryflag.hh:68
The grid manager (this is the class used by the user) for all supported grid managers that constructs...
Definition gridmanager_base.hh:312
The grid manager base interface (public) and methods common to most grid manager specializations (pro...
Definition gridmanager_base.hh:65
void loadBalance()
Definition gridmanager_base.hh:95
std::shared_ptr< Grid > & gridPtr()
Definition gridmanager_base.hh:134
void init(const std::string &modelParamGroup="")
Definition gridmanager_base.hh:73
std::string getFileExtension(const std::string &fileName) const
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:142
Convience header that includes all grid manager specializations.