3.4
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
gridmanager_alu.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef DUMUX_IO_GRID_MANAGER_ALU_HH
25#define DUMUX_IO_GRID_MANAGER_ALU_HH
26
27// ALUGrid specific includes
28#if HAVE_DUNE_ALUGRID
29#include <dune/alugrid/grid.hh>
30#include <dune/alugrid/dgf.hh>
31#endif
32
33#ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
35#endif
36
38
39namespace Dumux {
40
41#if HAVE_DUNE_ALUGRID
42
60template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
61class GridManager<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
62: public GridManagerBase<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
63{
64public:
65 using Grid = Dune::ALUGrid<dim, dimworld, elType, refinementType>;
66 using ParentType = GridManagerBase<Grid>;
67
71 void init(const std::string& modelParamGroup = "", bool adaptiveRestart = false)
72 {
73 // restarting an adaptive grid using Dune's BackupRestoreFacility
74 // TODO: the part after first || is backward compatibilty with old sequential models remove once sequential adpative restart is replaced
75 if (adaptiveRestart || hasParam("Restart") || hasParam("TimeManager.Restart"))
76 {
77 auto restartTime = getParamFromGroup<double>(modelParamGroup, "TimeLoop.Restart", 0.0);
78 // TODO: backward compatibilty with old sequential models remove once sequential adpative restart is replaced
79 if (hasParam("Restart") || hasParam("TimeManager.Restart"))
80 {
81 restartTime = getParamFromGroup<double>("TimeManager", "Restart");
82 std::cerr << "Warning: You are using a deprecated restart mechanism. The usage will change in the future.\n";
83 }
84
85 const int rank = Dune::MPIHelper::getCollectiveCommunication().rank();
86 const std::string name = getParamFromGroup<std::string>(modelParamGroup, "Problem.Name");
87 std::ostringstream oss;
88 oss << name << "_time=" << restartTime << "_rank=" << rank << ".grs";
89 std::cout << "Restoring an ALUGrid from " << oss.str() << std::endl;
90 ParentType::gridPtr() = std::shared_ptr<Grid>(Dune::BackupRestoreFacility<Grid>::restore(oss.str()));
91 ParentType::loadBalance();
92 return;
93 }
94
95 // try to create it from a DGF or msh file in GridParameterGroup.File
96 else if (hasParamInGroup(modelParamGroup, "Grid.File"))
97 {
98 makeGridFromFile(getParamFromGroup<std::string>(modelParamGroup, "Grid.File"), modelParamGroup);
99 ParentType::maybeRefineGrid(modelParamGroup);
100 ParentType::loadBalance();
101 return;
102 }
103
104 // Then look for the necessary keys to construct from the input file
105 else if (hasParamInGroup(modelParamGroup, "Grid.UpperRight"))
106 {
107 if (elType == Dune::cube)
108 makeStructuredGrid<dim, dimworld>(ParentType::CellType::Cube, modelParamGroup);
109 else if (elType == Dune::simplex)
110 makeStructuredGrid<dim, dimworld>(ParentType::CellType::Simplex, modelParamGroup);
111 else
112 DUNE_THROW(Dune::IOError, "ALUGrid only supports Dune::cube or Dune::simplex as cell type!");
113
114 ParentType::maybeRefineGrid(modelParamGroup);
115 ParentType::loadBalance();
116 }
117
118 // Didn't find a way to construct the grid
119 else
120 {
121 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup + ".";
122 DUNE_THROW(ParameterException, "Please supply one of the parameters "
123 << prefix + "Grid.UpperRight"
124 << ", or a grid file in " << prefix + "Grid.File");
125
126 }
127 }
128
132 void makeGridFromFile(const std::string& fileName,
133 const std::string& modelParamGroup)
134 {
135 // We found a file in the input file...does it have a supported extension?
136 const std::string extension = ParentType::getFileExtension(fileName);
137 if(extension != "dgf" && extension != "msh")
138 DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " only supports DGF (*.dgf) and Gmsh (*.msh) grid files but the specified filename has extension: *."<< extension);
139
140 // make the grid
141 if (extension == "dgf")
142 {
143 ParentType::enableDgfGridPointer_ = true;
144 ParentType::dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
145 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::dgfGridPtr());
146 }
147 if (extension == "msh")
148 {
149 // get some optional parameters
150 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
151 const bool boundarySegments = getParamFromGroup<bool>(modelParamGroup, "Grid.BoundarySegments", false);
152 const bool domainMarkers = getParamFromGroup<bool>(modelParamGroup, "Grid.DomainMarkers", false);
153
154 if (domainMarkers)
155 ParentType::enableGmshDomainMarkers_ = true;
156
157 // only filll the factory for rank 0
158 if (domainMarkers)
159 {
160 std::vector<int> boundaryMarkersInsertionIndex, boundaryMarkers, faceMarkers, elementMarkers;
161 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
162
163 Dune::GmshReader<Grid>::read(*gridFactory, fileName, boundaryMarkersInsertionIndex, elementMarkers, verbose, boundarySegments);
164 ParentType::gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
165
166 // reorder boundary markers according to boundarySegmentIndex
167 boundaryMarkers.resize(ParentType::gridPtr()->numBoundarySegments(), 0);
168 faceMarkers.resize(ParentType::gridPtr()->leafGridView().size(1), 0);
169 const auto& indexSet = ParentType::gridPtr()->leafGridView().indexSet();
170 for (const auto& element : elements(ParentType::gridPtr()->leafGridView()))
171 {
172 for (const auto& intersection : intersections(ParentType::gridPtr()->leafGridView(), element))
173 {
174 if (intersection.boundary() && gridFactory->wasInserted(intersection))
175 {
176 auto marker = boundaryMarkersInsertionIndex[gridFactory->insertionIndex(intersection)];
177 boundaryMarkers[intersection.boundarySegmentIndex()] = marker;
178 faceMarkers[indexSet.index(element.template subEntity<1>(intersection.indexInInside()))] = marker;
179 }
180 }
181 }
182
183 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::gridPtr(), std::move(gridFactory),
184 std::move(elementMarkers), std::move(boundaryMarkers), std::move(faceMarkers));
185 }
186 else
187 {
188 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
189
190 if (Dune::MPIHelper::getCollectiveCommunication().rank() == 0)
191 Dune::GmshReader<Grid>::read(*gridFactory, fileName, verbose, boundarySegments);
192
193 ParentType::gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
194 }
195 }
196 }
197
201 template <int dimension, int dimensionworld, std::enable_if_t<dimension != dimensionworld, int> = 0>
202 void makeStructuredGrid(typename ParentType::CellType cellType,
203 const std::string& modelParamGroup)
204 {
205 DUNE_THROW(Dune::IOError, "ALUGrid currently only supports the creation of structured grids with dimension == dimensionworld. Consider reading in a grid file instead.");
206 }
207
211 template <int dimension, int dimensionworld, std::enable_if_t<dimension == dimensionworld, int> = 0>
212 void makeStructuredGrid(typename ParentType::CellType cellType,
213 const std::string& modelParamGroup)
214 {
215 // make a structured grid
216 if (elType == Dune::cube)
217 ParentType::template makeStructuredGrid<dimension, dimensionworld>(ParentType::CellType::Cube, modelParamGroup);
218 else if (elType == Dune::simplex)
219 ParentType::template makeStructuredGrid<dimension, dimensionworld>(ParentType::CellType::Simplex, modelParamGroup);
220 else
221 DUNE_THROW(Dune::IOError, "ALUGrid only supports Dune::cube or Dune::simplex as cell type!");
222 }
223};
224
230template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
231class BoundaryFlag<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
232{
233public:
234 BoundaryFlag() : flag_(-1) {}
235
236 template<class Intersection>
237 BoundaryFlag(const Intersection& i) : flag_(-1)
238 {
239 if (i.boundary())
240 flag_ = i.impl().boundaryId();
241 }
242
243 using value_type = int;
244
245 value_type get() const { return flag_; }
246
247private:
248 int flag_;
249};
250
251#endif // HAVE_DUNE_ALUGRID
252
253} // end namespace Dumux
254
255#endif
Boundary flag to store e.g. in sub control volume faces.
Provides a grid manager for all supported grid managers with input file interfaces....
bool hasParamInGroup(const std::string &paramGroup, const std::string &param)
Check whether a key exists in the parameter tree with a model group prefix.
Definition: parameters.hh:374
bool hasParam(const std::string &param)
Check whether a key exists in the parameter tree.
Definition: parameters.hh:366
Definition: adapt.hh:29
Definition: common/pdesolver.hh:36
std::size_t value_type
Definition: boundaryflag.hh:51
value_type get() const
Definition: boundaryflag.hh:53
Grid Grid
Definition: gridmanager_base.hh:68
void makeGridFromFile(const std::string &fileName, const std::string &modelParamGroup)
Makes a grid from a file. We currently support.
Definition: gridmanager_base.hh:186
void makeStructuredGrid(CellType cellType, const std::string &modelParamGroup)
Makes a structured cube grid using the structured grid factory.
Definition: gridmanager_base.hh:269
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:74