GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/io/grid/gridmanager_alu.hh
Date: 2024-05-04 19:09:25
Exec Total Coverage
Lines: 54 73 74.0%
Functions: 14 24 58.3%
Branches: 148 442 33.5%

Line Branch Exec Source
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 // SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup InputOutput
10 * \brief Grid manager specialization for ALUGrid
11 */
12 #ifndef DUMUX_IO_GRID_MANAGER_ALU_HH
13 #define DUMUX_IO_GRID_MANAGER_ALU_HH
14
15 // ALUGrid specific includes
16 #if HAVE_DUNE_ALUGRID
17 #include <dune/alugrid/grid.hh>
18 #include <dune/alugrid/dgf.hh>
19 #endif
20
21 #ifndef DUMUX_IO_GRID_MANAGER_BASE_HH
22 #include <dumux/io/grid/gridmanager_base.hh>
23 #endif
24
25 #include <dumux/common/boundaryflag.hh>
26 #include <dumux/common/gridcapabilities.hh>
27
28 namespace Dumux {
29
30 #if HAVE_DUNE_ALUGRID
31
32 /*!
33 * \ingroup InputOutput
34 * \brief Provides a grid manager for Dune ALUGrids
35 * from information in the input file
36 *
37 * All keys are expected to be in group GridParameterGroup.
38
39 * The following keys are recognized:
40 * - File : A DGF or gmsh file to load from, type detection by file extension
41 * - LowerLeft : lowerLeft corner of a structured grid
42 * - UpperRight : upperright corner of a structured grid
43 * - Cells : number of elements in a structured grid
44 * - Refinement : the number of global refines to perform
45 * - Verbosity : whether the grid construction should output to standard out
46 * - BoundarySegments : whether to insert boundary segments into the grid
47 *
48 */
49 template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
50
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
125 class GridManager<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
51 : public GridManagerBase<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
52 {
53 public:
54 using Grid = Dune::ALUGrid<dim, dimworld, elType, refinementType>;
55 using ParentType = GridManagerBase<Grid>;
56
57 /*!
58 * \brief Make the grid. This is implemented by specializations of this method.
59 */
60 63 void init(const std::string& modelParamGroup = "", bool adaptiveRestart = false)
61 {
62 // restarting an adaptive grid using Dune's BackupRestoreFacility
63 // TODO: the part after first || is backward compatibility with old sequential models remove once sequential adaptive restart is replaced
64
14/36
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 63 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 63 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 63 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 63 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 63 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 63 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 63 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 63 times.
✓ Branch 24 taken 63 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 63 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 63 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 63 times.
✗ Branch 32 not taken.
✓ Branch 33 taken 63 times.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
315 if (adaptiveRestart || hasParam("Restart") || hasParam("TimeManager.Restart"))
65 {
66 auto restartTime = getParamFromGroup<double>(modelParamGroup, "TimeLoop.Restart", 0.0);
67 // TODO: backward compatibility with old sequential models remove once sequential adaptive restart is replaced
68 if (hasParam("Restart") || hasParam("TimeManager.Restart"))
69 {
70 restartTime = getParamFromGroup<double>("TimeManager", "Restart");
71 std::cerr << "Warning: You are using a deprecated restart mechanism. The usage will change in the future.\n";
72 }
73
74 const int rank = Dune::MPIHelper::getCommunication().rank();
75 const std::string name = getParamFromGroup<std::string>(modelParamGroup, "Problem.Name");
76 std::ostringstream oss;
77 oss << name << "_time=" << restartTime << "_rank=" << rank << ".grs";
78 std::cout << "Restoring an ALUGrid from " << oss.str() << std::endl;
79 ParentType::gridPtr() = std::shared_ptr<Grid>(Dune::BackupRestoreFacility<Grid>::restore(oss.str()));
80 ParentType::loadBalance();
81 return;
82 }
83
84 // try to create it from a DGF or msh file in GridParameterGroup.File
85
8/14
✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 63 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 63 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 63 times.
✓ Branch 11 taken 36 times.
✓ Branch 12 taken 27 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 27 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
189 else if (hasParamInGroup(modelParamGroup, "Grid.File"))
86 {
87
3/6
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✓ Branch 5 taken 5 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
36 makeGridFromFile(getParamFromGroup<std::string>(modelParamGroup, "Grid.File"), modelParamGroup);
88 36 ParentType::maybeRefineGrid(modelParamGroup);
89 36 ParentType::loadBalance();
90 36 return;
91 }
92
93 // Then look for the necessary keys to construct from the input file
94
6/14
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 27 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 27 times.
✓ Branch 11 taken 27 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 27 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
81 else if (hasParamInGroup(modelParamGroup, "Grid.UpperRight"))
95 {
96 if (elType == Dune::cube)
97 36 makeStructuredGrid<dim, dimworld>(ParentType::CellType::Cube, modelParamGroup);
98 else if (elType == Dune::simplex)
99 18 makeStructuredGrid<dim, dimworld>(ParentType::CellType::Simplex, modelParamGroup);
100 else
101 DUNE_THROW(Dune::IOError, "ALUGrid only supports Dune::cube or Dune::simplex as cell type!");
102
103 27 ParentType::maybeRefineGrid(modelParamGroup);
104 27 ParentType::loadBalance();
105 }
106
107 // Didn't find a way to construct the grid
108 else
109 {
110 const auto prefix = modelParamGroup.empty() ? modelParamGroup : modelParamGroup + ".";
111 DUNE_THROW(ParameterException, "Please supply one of the parameters "
112 << prefix + "Grid.UpperRight"
113 << ", or a grid file in " << prefix + "Grid.File");
114
115 }
116 }
117
118 /*!
119 * \brief Makes a grid from a file. We currently support *.dgf (Dune Grid Format) and *.msh (Gmsh mesh format).
120 */
121 36 void makeGridFromFile(const std::string& fileName,
122 const std::string& modelParamGroup)
123 {
124 // We found a file in the input file...does it have a supported extension?
125
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
72 const std::string extension = ParentType::getFileExtension(fileName);
126
3/4
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
36 if(extension != "dgf" && extension != "msh")
127 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);
128
129 // make the grid
130
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
36 if (extension == "dgf")
131 {
132 24 ParentType::enableDgfGridPointer_ = true;
133
10/20
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 24 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 24 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 24 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 24 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 24 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 22 times.
✓ Branch 23 taken 2 times.
✓ Branch 25 taken 24 times.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
118 ParentType::dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
134
4/8
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 24 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 24 times.
24 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::dgfGridPtr());
135 }
136
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 24 times.
36 if (extension == "msh")
137 {
138 // get some optional parameters
139
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
140
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 const bool boundarySegments = getParamFromGroup<bool>(modelParamGroup, "Grid.BoundarySegments", false);
141
1/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 const bool domainMarkers = getParamFromGroup<bool>(modelParamGroup, "Grid.DomainMarkers", false);
142
143
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (domainMarkers)
144 8 ParentType::enableGmshDomainMarkers_ = true;
145
146 // only fill the factory for rank 0
147
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (domainMarkers)
148 {
149
10/22
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 6 times.
✓ Branch 13 taken 2 times.
✓ Branch 14 taken 6 times.
✓ Branch 15 taken 2 times.
✓ Branch 16 taken 4 times.
✓ Branch 17 taken 4 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
56 std::vector<int> boundaryMarkersInsertionIndex, boundaryMarkers, faceMarkers, elementMarkers;
150
3/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 2 times.
16 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
151
152
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
16 Dune::GmshReader<Grid>::read(*gridFactory, fileName, boundaryMarkersInsertionIndex, elementMarkers, verbose, boundarySegments);
153
7/18
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 8 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 8 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 8 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
16 ParentType::gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
154
155 // reorder boundary markers according to boundarySegmentIndex
156
4/8
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
8 boundaryMarkers.resize(ParentType::gridPtr()->numBoundarySegments(), 0);
157
5/10
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 8 times.
✗ Branch 14 not taken.
8 faceMarkers.resize(ParentType::gridPtr()->leafGridView().size(1), 0);
158
4/8
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
8 const auto& indexSet = ParentType::gridPtr()->leafGridView().indexSet();
159
11/20
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 8 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 8 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 17410 times.
✓ Branch 19 taken 8 times.
✓ Branch 20 taken 17410 times.
✓ Branch 21 taken 8 times.
✓ Branch 23 taken 17410 times.
✗ Branch 24 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
34828 for (const auto& element : elements(ParentType::gridPtr()->leafGridView()))
160 {
161
7/16
✓ Branch 1 taken 17410 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 17410 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 17410 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 17410 times.
✗ Branch 11 not taken.
✓ Branch 14 taken 75614 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 20 taken 58204 times.
✗ Branch 21 not taken.
✓ Branch 26 taken 17410 times.
✗ Branch 27 not taken.
168638 for (const auto& intersection : intersections(ParentType::gridPtr()->leafGridView(), element))
162 {
163
6/8
✓ Branch 1 taken 2692 times.
✓ Branch 2 taken 55512 times.
✓ Branch 4 taken 2692 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2692 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 2612 times.
✓ Branch 10 taken 80 times.
58204 if (intersection.boundary() && gridFactory->wasInserted(intersection))
164 {
165
3/6
✓ Branch 1 taken 2612 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2612 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2612 times.
✗ Branch 8 not taken.
5224 auto marker = boundaryMarkersInsertionIndex[gridFactory->insertionIndex(intersection)];
166
2/4
✓ Branch 1 taken 2612 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2612 times.
✗ Branch 5 not taken.
2612 boundaryMarkers[intersection.boundarySegmentIndex()] = marker;
167
2/4
✓ Branch 1 taken 2612 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2612 times.
✗ Branch 5 not taken.
2612 faceMarkers[indexSet.index(element.template subEntity<1>(intersection.indexInInside()))] = marker;
168 }
169 }
170 }
171
172
4/8
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
8 ParentType::gridData_ = std::make_shared<typename ParentType::GridData>(ParentType::gridPtr(), std::move(gridFactory),
173
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 std::move(elementMarkers), std::move(boundaryMarkers), std::move(faceMarkers));
174 }
175 else
176 {
177
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
178
179
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 if (Dune::MPIHelper::getCommunication().rank() == 0)
180
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
8 Dune::GmshReader<Grid>::read(*gridFactory, fileName, verbose, boundarySegments);
181
182
8/22
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 4 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 4 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 4 times.
✓ Branch 18 taken 4 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
8 ParentType::gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
183 }
184 }
185 36 }
186
187 /*!
188 * \brief Makes a structured cube grid using the structured grid factory
189 */
190 template <int dimension, int dimensionworld, std::enable_if_t<dimension != dimensionworld, int> = 0>
191 void makeStructuredGrid(typename ParentType::CellType cellType,
192 const std::string& modelParamGroup)
193 {
194 DUNE_THROW(Dune::IOError, "ALUGrid currently only supports the creation of structured grids with dimension == dimensionworld. Consider reading in a grid file instead.");
195 }
196
197 /*!
198 * \brief Makes a structured cube grid using the structured grid factory
199 */
200 template <int dimension, int dimensionworld, std::enable_if_t<dimension == dimensionworld, int> = 0>
201 void makeStructuredGrid(typename ParentType::CellType cellType,
202 const std::string& modelParamGroup)
203 {
204 // make a structured grid
205 if (elType == Dune::cube)
206 18 ParentType::template makeStructuredGrid<dimension, dimensionworld>(ParentType::CellType::Cube, modelParamGroup);
207 else if (elType == Dune::simplex)
208 9 ParentType::template makeStructuredGrid<dimension, dimensionworld>(ParentType::CellType::Simplex, modelParamGroup);
209 else
210 DUNE_THROW(Dune::IOError, "ALUGrid only supports Dune::cube or Dune::simplex as cell type!");
211 }
212 };
213
214 /*!
215 * \ingroup InputOutput
216 * \brief Boundary flag
217 */
218 //! alu uses boundary id
219 template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
220 class BoundaryFlag<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
221 {
222 public:
223 18773546 BoundaryFlag() : flag_(-1) {}
224
225 template<class Intersection>
226 24299390 BoundaryFlag(const Intersection& i) : flag_(-1)
227 {
228
2/2
✓ Branch 1 taken 685420 times.
✓ Branch 2 taken 23613970 times.
24299390 if (i.boundary())
229 685420 flag_ = i.impl().boundaryId();
230 24299390 }
231
232 using value_type = int;
233
234 value_type get() const { return flag_; }
235
236 private:
237 int flag_;
238 };
239
240 namespace Grid::Capabilities {
241
242 // To the best of our knowledge ALUGrid is view thread-safe
243 // This specialization can be removed after we depend on Dune release 2.9 in which this is guaranteed by ALUGrid itself
244 template<int dim, int dimworld, Dune::ALUGridElementType elType, Dune::ALUGridRefinementType refinementType>
245 struct MultithreadingSupported<Dune::ALUGrid<dim, dimworld, elType, refinementType>>
246 {
247 template<class GV>
248 static bool eval(const GV&) // default is independent of the grid view
249 { return true; }
250 };
251
252 } // end namespace Grid::Capabilities
253
254 #endif // HAVE_DUNE_ALUGRID
255
256 } // end namespace Dumux
257
258 #endif
259