3.6-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
porenetwork/griddata.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_PORENETWORKGRID_DATA_HH
25#define DUMUX_IO_PORENETWORKGRID_DATA_HH
26
27#include <algorithm>
28#include <memory>
29#include <string>
30#include <type_traits>
31#include <unordered_map>
32#include <vector>
33#include <fstream>
34
35#include <dune/common/exceptions.hh>
36#include <dune/grid/common/gridfactory.hh>
37#include <dune/grid/common/mcmgmapper.hh>
38#include <dune/grid/io/file/dgfparser/gridptr.hh>
39#include <dune/grid/io/file/dgfparser/parser.hh>
40#include <dune/grid/utility/persistentcontainer.hh>
41#include <dune/geometry/axisalignedcubegeometry.hh>
42
45
47
48namespace Dumux::PoreNetwork {
49
54template <class Grid>
56{
57 static constexpr int dim = Grid::dimension;
58 static constexpr int dimWorld = Grid::dimensionworld;
59 using Intersection = typename Grid::LeafIntersection;
60 using Element = typename Grid::template Codim<0>::Entity;
61 using Vertex = typename Grid::template Codim<dim>::Entity;
62 using GridView = typename Grid::LeafGridView;
63 using GlobalPosition = typename Element::Geometry::GlobalCoordinate;
64 using SmallLocalIndex = typename IndexTraits<GridView>::SmallLocalIndex;
65 using StringVector = std::vector<std::string>;
66
67 using Scalar = typename Grid::ctype;
68 using PersistentParameterContainer = Dune::PersistentContainer<Grid, std::vector<typename Grid::ctype>>;
70
71public:
72
74 GridData(Dune::GridPtr<Grid> grid, const std::string& paramGroup)
75 : dgfGrid_(grid)
76 , isDgfData_(true)
77 , paramGroup_(paramGroup)
78 , numSubregions_(0)
79 {
80 setParameterIndices_();
81 }
82
84 GridData(std::shared_ptr<Grid> grid, const std::string& paramGroup)
85 : factoryGrid_(grid)
86 , isDgfData_(false)
87 , paramGroup_(paramGroup)
88 {
89 numSubregions_ = getParamFromGroup<std::size_t>(paramGroup_, "Grid.NumSubregions", 0);
90 setParameterIndices_();
91 parametersForGeneratedGrid_ = std::make_unique<ParametersForGeneratedGrid>(gridView_(), paramGroup);
92 }
93
98 const std::vector<double>& parameters(const Vertex& vertex) const
99 {
100 if (isDgfData_ && !useCopiedDgfData_)
101 return dgfGrid_.parameters(vertex);
102 else
103 {
104 assert(!(*vertexParameters_)[vertex].empty() && "No parameters available.");
105 return (*vertexParameters_)[vertex];
106 }
107 }
108
112 const std::vector<double>& parameters(const Element& element) const
113 {
114 if (isDgfData_ && !useCopiedDgfData_)
115 {
116 if (element.hasFather())
117 {
118 auto level0Element = element;
119 while(level0Element.hasFather())
120 level0Element = level0Element.father();
121
122 return dgfGrid_.parameters(level0Element);
123 }
124 else
125 {
126 return dgfGrid_.parameters(element);
127 }
128 }
129 else
130 {
131 assert(!(*elementParameters_)[element].empty() && "No parameters available.");
132 return (*elementParameters_)[element];
133 }
134 }
135
139 template <class GridImp, class IntersectionImp>
140 const Dune::DGFBoundaryParameter::type& parameters(const Dune::Intersection<GridImp, IntersectionImp>& intersection) const
141 {
142 if (isDgfData_)
143 return dgfGrid_.parameters(intersection);
144 else
145 DUNE_THROW(Dune::InvalidStateException, "The parameters method is only available if the grid was constructed with a DGF file.");
146 }
147
151 std::vector<SmallLocalIndex> getCoordinationNumbers() const
152 {
153 std::vector<SmallLocalIndex> coordinationNumbers(gridView_().size(dim), 0);
154
155 for (const auto &element : elements(gridView_()))
156 {
157 for (SmallLocalIndex vIdxLocal = 0; vIdxLocal < 2; ++vIdxLocal)
158 {
159 const auto vIdxGlobal = gridView_().indexSet().subIndex(element, vIdxLocal, dim);
160 coordinationNumbers[vIdxGlobal] += 1;
161 }
162 }
163
164 if (std::any_of(coordinationNumbers.begin(), coordinationNumbers.end(), [](auto i){ return i == 0; }))
165 DUNE_THROW(Dune::InvalidStateException, "One of the pores is not connected to another pore. SanitizeGrid will not help in this case. Check your grid file");
166
167 return coordinationNumbers;
168 }
169
174 {
175 if (isDgfData_)
176 DUNE_THROW(Dune::InvalidStateException, "Assigning parameter not possible for dgf gids");
177
178 const auto numVertexParams = vertexParameterNames_.size();
179 const auto numElementParams = elementParameterNames_.size();
180 vertexParameters_ = makeParamContainer_(*factoryGrid_, numVertexParams, 1/*codim*/);
181 elementParameters_ = makeParamContainer_(*factoryGrid_, numElementParams, 0/*codim*/);
182
183 auto setParamHelper = [&](const auto& entity, const std::string& param, const Scalar value)
184 {
185 setParameter_(entity, param, value);
186 };
187
188 auto getParamHelper = [&](const auto& entity, const std::string& param)
189 {
190 return getParameter(entity, param);
191 };
192
193 parametersForGeneratedGrid_->assignParameters(setParamHelper, getParamHelper, numSubregions_);
194 }
195
197 {
198 // resize the parameters
199 vertexParameters_->resize();
200 elementParameters_->resize();
201 vertexParameters_->shrinkToFit();
202 elementParameters_->shrinkToFit();
203 }
204
206 {
207 if (!isDgfData_)
208 DUNE_THROW(Dune::InvalidStateException, "copying dgf data only works when a dgf grid is actually used");
209
210 useCopiedDgfData_ = true;
211 const auto someVertex = *(vertices(gridView_()).begin());
212 const auto someElement = *(elements(gridView_()).begin());
213 const auto numVertexParams = dgfGrid_.parameters(someVertex).size();
214 const auto numElementParams = dgfGrid_.parameters(someElement).size();
215 vertexParameters_ = makeParamContainer_(*dgfGrid_, numVertexParams, 1);
216 elementParameters_ = makeParamContainer_(*dgfGrid_, numElementParams, 0);
217
218 for (const auto& element : elements(gridView_()))
219 {
220 for (int i = 0; i < numElementParams; ++i)
221 (*elementParameters_)[element][i] = dgfGrid_.parameters(element)[i];
222 }
223
224 for (const auto& vertex : vertices(gridView_()))
225 {
226 for (int i = 0; i < numVertexParams; ++i)
227 (*vertexParameters_)[vertex][i] = dgfGrid_.parameters(vertex)[i];
228 }
229 }
230
234 int parameterIndex(const std::string& paramName) const
235 {
236 // make sure the string is present in the map, throw a Dumux exception otherwise (and not a std one)
237 // the [] operator can't be used here due to const correctness
238 if (static_cast<bool>(parameterIndex_.count(paramName)))
239 return parameterIndex_.at(paramName);
240 else
241 {
242 std::stringstream list;
243 list << "List of parameter indices:\n";
244 for (const auto& entry : parameterIndex_)
245 list << entry.first << " " << entry.second << "\n";
246
247 std::string msg;
248 if (paramName.find("Throat") != std::string::npos)
249 msg = "Make sure to include it in the vector of parameter names as a comment in the DGF file: Element Parameters = ... " + paramName + " ...";
250 else if (paramName.find("Pore") != std::string::npos)
251 msg = "Make sure to include it in the vector of parameter names as a comment in the DGF file: Vertex Parameters = ... " + paramName + " ...";
252
253 DUNE_THROW(Dumux::ParameterException, paramName << " not set in the grid file. \n" << msg << "\n" << list.str());
254 }
255 }
256
260 const std::string& paramGroup() const
261 { return paramGroup_; }
262
266 bool gridHasElementParameter(const std::string& param) const
267 {
268 return std::any_of(elementParameterNames_.begin(), elementParameterNames_.end(), [&param]( const auto& i ){ return (i == param); });
269 }
270
274 bool gridHasVertexParameter(const std::string& param) const
275 {
276 return std::any_of(vertexParameterNames_.begin(), vertexParameterNames_.end(), [&param]( const auto& i ){ return (i == param); });
277 }
278
282 Scalar getParameter(const Element& element, const std::string& param) const
283 { return parameters(element)[parameterIndex(param)]; }
284
288 Scalar getParameter(const Vertex& vertex, const std::string& param) const
289 { return parameters(vertex)[parameterIndex(param)]; }
290
295 int poreLabelAtPosForGenericGrid(const GlobalPosition& pos) const
296 { return parametersForGeneratedGrid_->boundaryFaceMarkerAtPos(pos); }
297
301 const std::vector<std::string>& vertexParameterNames() const
302 { return vertexParameterNames_; }
303
307 const std::vector<std::string>& elementParameterNames() const
308 { return elementParameterNames_; }
309
310private:
311
312 void setParameter_(const Element& element, const std::string& param, const Scalar value)
313 { (*elementParameters_)[element][parameterIndex(param)] = value; }
314
315 void setParameter_(const Vertex& vertex, const std::string& param, const Scalar value)
316 { (*vertexParameters_)[vertex][parameterIndex(param)] = value; }
317
318 void setParameterIndices_()
319 {
320 if (!isDgfData_)
321 {
322 vertexParameterNames_ = StringVector{"PoreInscribedRadius", "PoreVolume", "PoreLabel"};
323 elementParameterNames_ = StringVector{"ThroatInscribedRadius", "ThroatLength", "ThroatLabel"};
324 if (numSubregions_ > 0)
325 {
326 vertexParameterNames_.push_back("PoreRegionId");
327 elementParameterNames_.push_back("ThroatRegionId");
328 }
329 }
330 else // DGF grid data
331 {
332 // treat vertex parameter names
333 vertexParameterNames_ = dgfFileParameterNames_("Vertex");
334 if (vertexParameterNames_.empty())
335 DUNE_THROW(Dune::InvalidStateException, "No vertex parameter names specified in dgf file. Set '% Vertex parameters: Param1 Param2 ...'");
336
337 // treat element parameter names
338 elementParameterNames_ = dgfFileParameterNames_("Element");
339 if (elementParameterNames_.empty())
340 DUNE_THROW(Dune::InvalidStateException, "No element parameter names specified in dgf file. Set '% Element parameters: Param1 Param2 ...'");
341
342 // make sure that the number of specified parameters matches with the dgf file
343 if (const auto& someElement = *(elements(gridView_()).begin()); elementParameterNames_.size() != dgfGrid_.nofParameters(someElement))
344 DUNE_THROW(Dune::InvalidStateException, "Number of user-specified element parameters (" << elementParameterNames_.size()
345 << ") does not match number of element parameters in dgf file (" << dgfGrid_.nofParameters(someElement) << ")");
346
347 if (const auto& someVertex = *(vertices(gridView_()).begin()); vertexParameterNames_.size() != dgfGrid_.nofParameters(someVertex))
348 DUNE_THROW(Dune::InvalidStateException, "Number of user-specified vertex parameters (" << vertexParameterNames_.size()
349 << ") does not match number of vertex parameters in dgf file (" << dgfGrid_.nofParameters(someVertex) << ")");
350 }
351
352 for (int i = 0; i < vertexParameterNames_.size(); ++i)
353 {
354 std::cout << vertexParameterNames_[i] << " is vertex parameter " << i << std::endl;
355 parameterIndex_[vertexParameterNames_[i]] = i;
356 }
357
358 for (int i = 0; i < elementParameterNames_.size(); ++i)
359 {
360 std::cout << elementParameterNames_[i] << " is element parameter " << i << std::endl;
361 parameterIndex_[elementParameterNames_[i]] = i;
362 }
363 }
364
372 auto makeParamContainer_(const Grid& grid, int numParams, int codim) const
373 {
374 auto parameters = std::make_unique<PersistentParameterContainer>(grid, codim);
375 (*parameters).resize();
376 for (auto&& v : (*parameters))
377 v.resize(numParams);
378 return parameters;
379 }
380
381 StringVector dgfFileParameterNames_(const std::string& entity) const
382 {
383 StringVector paramNames;
384 {
385 std::ifstream gridFile(getParamFromGroup<std::string>(paramGroup_, "Grid.File"));
386 for (std::string line; std::getline(gridFile, line); )
387 {
388 // handle Windows file endings (if any)
389 line.erase( std::remove(line.begin(), line.end(), '\r'), line.end() );
390
391 // extract parameter names
392 if (line.find(entity + " parameters:", 0) != std::string::npos)
393 {
394 std::istringstream iss(line.substr(line.find(":")+1, std::string::npos));
395 for (std::string item; std::getline(iss, item, ' '); )
396 if (!item.empty())
397 paramNames.push_back(item);
398 }
399 }
400 }
401 return paramNames;
402 }
403
407 const GridView gridView_() const
408 { return isDgfData_ ? dgfGrid_->leafGridView() : factoryGrid_->leafGridView(); }
409
410 // dgf grid data
411 Dune::GridPtr<Grid> dgfGrid_;
412
413 std::shared_ptr<Grid> factoryGrid_;
414
415 bool isDgfData_ = false;
416 bool useCopiedDgfData_ = false;
417 std::string paramGroup_;
418
419 std::unique_ptr<ParametersForGeneratedGrid> parametersForGeneratedGrid_;
420
421 std::vector<std::string> vertexParameterNames_;
422 std::vector<std::string> elementParameterNames_;
423
424 std::unique_ptr<PersistentParameterContainer> vertexParameters_;
425 std::unique_ptr<PersistentParameterContainer> elementParameters_;
426
427 std::size_t numSubregions_;
428
429 std::unordered_map<std::string, int> parameterIndex_;
430};
431
432} // namespace Dumux::PoreNetwork
433
434#endif
Defines the index types used for grid and local indices.
Helper class to assign parameters to a generated grid.
This file contains functions related to calculate pore-throat properties.
Definition: discretization/porenetwork/fvelementgeometry.hh:34
constexpr Line line
Definition: couplingmanager1d3d_line.hh:43
Exception thrown if a run-time parameter is not specified correctly.
Definition: exceptions.hh:60
std::uint_least8_t SmallLocalIndex
Definition: indextraits.hh:41
Class for grid data attached to dgf or gmsh grid files.
Definition: porenetwork/griddata.hh:56
bool gridHasElementParameter(const std::string &param) const
Return if a given element parameter is provided by the grid.
Definition: porenetwork/griddata.hh:266
const std::vector< std::string > & vertexParameterNames() const
Returns the names of the vertex parameters.
Definition: porenetwork/griddata.hh:301
void resizeParameterContainers()
Definition: porenetwork/griddata.hh:196
GridData(Dune::GridPtr< Grid > grid, const std::string &paramGroup)
constructor for dgf grid data
Definition: porenetwork/griddata.hh:74
const std::vector< double > & parameters(const Element &element) const
Call the parameters function of the DGF grid pointer if available for element data.
Definition: porenetwork/griddata.hh:112
const Dune::DGFBoundaryParameter::type & parameters(const Dune::Intersection< GridImp, IntersectionImp > &intersection) const
Call the parameters function of the DGF grid pointer if available.
Definition: porenetwork/griddata.hh:140
std::vector< SmallLocalIndex > getCoordinationNumbers() const
Returns the coordination numbers for all pore bodies.
Definition: porenetwork/griddata.hh:151
Scalar getParameter(const Element &element, const std::string &param) const
Returns the value of an element parameter.
Definition: porenetwork/griddata.hh:282
const std::vector< std::string > & elementParameterNames() const
Returns the names of the element parameters.
Definition: porenetwork/griddata.hh:307
void copyDgfData()
Definition: porenetwork/griddata.hh:205
int parameterIndex(const std::string &paramName) const
Return the index for a given parameter name.
Definition: porenetwork/griddata.hh:234
int poreLabelAtPosForGenericGrid(const GlobalPosition &pos) const
Returns the pore label at a given position for a generic grid. This is needed by the grid creator in ...
Definition: porenetwork/griddata.hh:295
const std::string & paramGroup() const
Return the parameter group.
Definition: porenetwork/griddata.hh:260
GridData(std::shared_ptr< Grid > grid, const std::string &paramGroup)
constructor for non-dgf grid data
Definition: porenetwork/griddata.hh:84
Scalar getParameter(const Vertex &vertex, const std::string &param) const
Returns the value of an vertex parameter.
Definition: porenetwork/griddata.hh:288
void assignParameters()
Assign parameters for generically created grids.
Definition: porenetwork/griddata.hh:173
const std::vector< double > & parameters(const Vertex &vertex) const
Call the parameters function of the DGF grid pointer if available for vertex data.
Definition: porenetwork/griddata.hh:98
bool gridHasVertexParameter(const std::string &param) const
Return if a given vertex parameter is provided by the grid.
Definition: porenetwork/griddata.hh:274
Helper class to assign parameters to a generated grid.
Definition: parametersforgeneratedgrid.hh:53