Loading [MathJax]/extensions/tex2jax.js
3.6-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
39
40namespace Dumux {
41
42#if HAVE_DUNE_ALUGRID
43
61template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
62class GridManager<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
63: public GridManagerBase<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
64{
65public:
66 using Grid = Dune::ALUGrid<dim, dimworld, elType, refinementType>;
67 using ParentType = GridManagerBase<Grid>;
68
72 void init(const std::string& modelParamGroup = "", bool adaptiveRestart = false)
73 {
74 // restarting an adaptive grid using Dune's BackupRestoreFacility
75 // TODO: the part after first || is backward compatibility with old sequential models remove once sequential adaptive restart is replaced
76 if (adaptiveRestart || hasParam("Restart") || hasParam("TimeManager.Restart"))
77 {
78 auto restartTime = getParamFromGroup<double>(modelParamGroup, "TimeLoop.Restart", 0.0);
79 // TODO: backward compatibility with old sequential models remove once sequential adaptive restart is replaced
80 if (hasParam("Restart") || hasParam("TimeManager.Restart"))
81 {
82 restartTime = getParamFromGroup<double>("TimeManager", "Restart");
83 std::cerr << "Warning: You are using a deprecated restart mechanism. The usage will change in the future.\n";
84 }
85
86 const int rank = Dune::MPIHelper::getCommunication().rank();
87 const std::string name = getParamFromGroup<std::string>(modelParamGroup, "Problem.Name");
88 std::ostringstream oss;
89 oss << name << "_time=" << restartTime << "_rank=" << rank << ".grs";
90 std::cout << "Restoring an ALUGrid from " << oss.str() << std::endl;
91 ParentType::gridPtr() = std::shared_ptr<Grid>(Dune::BackupRestoreFacility<Grid>::restore(oss.str()));
92 ParentType::loadBalance();
93 return;
94 }
95
96 // try to create it from a DGF or msh file in GridParameterGroup.File
97 else if (hasParamInGroup(modelParamGroup, "Grid.File"))
98 {
99 makeGridFromFile(getParamFromGroup<std::string>(modelParamGroup, "Grid.File"), modelParamGroup);
100 ParentType::maybeRefineGrid(modelParamGroup);
101 ParentType::loadBalance();
102 return;
103 }
104
105 // Then look for the necessary keys to construct from the input file
106 else if (hasParamInGroup(modelParamGroup, "Grid.UpperRight"))
107 {
108 if (elType == Dune::cube)
109 makeStructuredGrid<dim, dimworld>(ParentType::CellType::Cube, modelParamGroup);
110 else if (elType == Dune::simplex)
111 makeStructuredGrid<dim, dimworld>(ParentType::CellType::Simplex, modelParamGroup);
112 else
113 DUNE_THROW(Dune::IOError, "ALUGrid only supports Dune::cube or Dune::simplex as cell type!");
114
115 ParentType::maybeRefineGrid(modelParamGroup);
116 ParentType::loadBalance();
117 }
118
119 // Didn't find a way to construct the grid
120 else
121 {
122 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup + ".";
123 DUNE_THROW(ParameterException, "Please supply one of the parameters "
124 << prefix + "Grid.UpperRight"
125 << ", or a grid file in " << prefix + "Grid.File");
126
127 }
128 }
129
133 void makeGridFromFile(const std::string& fileName,
134 const std::string& modelParamGroup)
135 {
136 // We found a file in the input file...does it have a supported extension?
137 const std::string extension = ParentType::getFileExtension(fileName);
138 if(extension != "dgf" && extension != "msh")
139 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);
140
141 // make the grid
142 if (extension == "dgf")
143 {
144 ParentType::enableDgfGridPointer_ = true;
145 ParentType::dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
146 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::dgfGridPtr());
147 }
148 if (extension == "msh")
149 {
150 // get some optional parameters
151 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
152 const bool boundarySegments = getParamFromGroup<bool>(modelParamGroup, "Grid.BoundarySegments", false);
153 const bool domainMarkers = getParamFromGroup<bool>(modelParamGroup, "Grid.DomainMarkers", false);
154
155 if (domainMarkers)
156 ParentType::enableGmshDomainMarkers_ = true;
157
158 // only fill the factory for rank 0
159 if (domainMarkers)
160 {
161 std::vector<int> boundaryMarkersInsertionIndex, boundaryMarkers, faceMarkers, elementMarkers;
162 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
163
164 Dune::GmshReader<Grid>::read(*gridFactory, fileName, boundaryMarkersInsertionIndex, elementMarkers, verbose, boundarySegments);
165 ParentType::gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
166
167 // reorder boundary markers according to boundarySegmentIndex
168 boundaryMarkers.resize(ParentType::gridPtr()->numBoundarySegments(), 0);
169 faceMarkers.resize(ParentType::gridPtr()->leafGridView().size(1), 0);
170 const auto& indexSet = ParentType::gridPtr()->leafGridView().indexSet();
171 for (const auto& element : elements(ParentType::gridPtr()->leafGridView()))
172 {
173 for (const auto& intersection : intersections(ParentType::gridPtr()->leafGridView(), element))
174 {
175 if (intersection.boundary() && gridFactory->wasInserted(intersection))
176 {
177 auto marker = boundaryMarkersInsertionIndex[gridFactory->insertionIndex(intersection)];
178 boundaryMarkers[intersection.boundarySegmentIndex()] = marker;
179 faceMarkers[indexSet.index(element.template subEntity<1>(intersection.indexInInside()))] = marker;
180 }
181 }
182 }
183
184 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::gridPtr(), std::move(gridFactory),
185 std::move(elementMarkers), std::move(boundaryMarkers), std::move(faceMarkers));
186 }
187 else
188 {
189 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
190
191 if (Dune::MPIHelper::getCommunication().rank() == 0)
192 Dune::GmshReader<Grid>::read(*gridFactory, fileName, verbose, boundarySegments);
193
194 ParentType::gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
195 }
196 }
197 }
198
202 template <int dimension, int dimensionworld, std::enable_if_t<dimension != dimensionworld, int> = 0>
203 void makeStructuredGrid(typename ParentType::CellType cellType,
204 const std::string& modelParamGroup)
205 {
206 DUNE_THROW(Dune::IOError, "ALUGrid currently only supports the creation of structured grids with dimension == dimensionworld. Consider reading in a grid file instead.");
207 }
208
212 template <int dimension, int dimensionworld, std::enable_if_t<dimension == dimensionworld, int> = 0>
213 void makeStructuredGrid(typename ParentType::CellType cellType,
214 const std::string& modelParamGroup)
215 {
216 // make a structured grid
217 if (elType == Dune::cube)
218 ParentType::template makeStructuredGrid<dimension, dimensionworld>(ParentType::CellType::Cube, modelParamGroup);
219 else if (elType == Dune::simplex)
220 ParentType::template makeStructuredGrid<dimension, dimensionworld>(ParentType::CellType::Simplex, modelParamGroup);
221 else
222 DUNE_THROW(Dune::IOError, "ALUGrid only supports Dune::cube or Dune::simplex as cell type!");
223 }
224};
225
231template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
232class BoundaryFlag<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
233{
234public:
235 BoundaryFlag() : flag_(-1) {}
236
237 template<class Intersection>
238 BoundaryFlag(const Intersection& i) : flag_(-1)
239 {
240 if (i.boundary())
241 flag_ = i.impl().boundaryId();
242 }
243
244 using value_type = int;
245
246 value_type get() const { return flag_; }
247
248private:
249 int flag_;
250};
251
252namespace Grid::Capabilities {
253
254// To the best of our knowledge ALUGrid is view thread-safe
255// This specialization can be removed after we depend on Dune release 2.9 in which this is guaranteed by ALUGrid itself
256template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
257struct MultithreadingSupported<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
258{
259 template<class GV>
260 static bool eval(const GV&) // default is independent of the grid view
261 { return true; }
262};
263
264} // end namespace Grid::Capabilities
265
266#endif // HAVE_DUNE_ALUGRID
267
268} // end namespace Dumux
269
270#endif
dune-grid capabilities compatibility layer
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:177
bool hasParam(const std::string &param)
Check whether a key exists in the parameter tree.
Definition: parameters.hh:169
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
static bool eval(const GV &)
Definition: gridcapabilities.hh:81
Grid Grid
Definition: gridmanager_base.hh:69
void makeGridFromFile(const std::string &fileName, const std::string &modelParamGroup)
Makes a grid from a file. We currently support.
Definition: gridmanager_base.hh:188
void makeStructuredGrid(CellType cellType, const std::string &modelParamGroup)
Makes a structured cube grid using the structured grid factory.
Definition: gridmanager_base.hh:271
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