GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: dumux/dumux/io/grid/gridinput_.hh
Date: 2025-05-03 19:19:02
Exec Total Coverage
Lines: 17 25 68.0%
Functions: 8 31 25.8%
Branches: 16 33 48.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-FileCopyrightText: 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 factory adapter for reading grid data
11 */
12 #ifndef DUMUX_IO_GRID_GRID_FACTORY__HH
13 #define DUMUX_IO_GRID_GRID_FACTORY__HH
14
15 #include <memory>
16 #include <array>
17 #include <ranges>
18 #include <algorithm>
19 #include <functional>
20
21 #include <dune/common/exceptions.hh>
22 #include <dune/grid/common/gridfactory.hh>
23 #include <dune/grid/common/capabilities.hh>
24
25 #include <dumux/geometry/intersectingentities.hh>
26
27 namespace Dumux::Detail::GridData {
28
29 template<class Grid>
30 struct GridInput
31 {
32 using Intersection = typename Grid::LeafIntersection;
33 public:
34 73 GridInput(std::shared_ptr<Grid> grid, std::shared_ptr<Dune::GridFactory<Grid>> factory)
35
1/2
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
73 : grid_(std::move(grid))
36 73 , factory_(std::move(factory))
37 {
38 // assume that all entities are on rank 0
39
1/2
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
73 numElements_ = grid_->levelGridView(0).size(0);
40
1/2
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
73 numVertices_ = grid_->levelGridView(0).size(Grid::dimension);
41
42
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 10 times.
73 if (grid_->comm().size() > 1)
43 {
44
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 numElements_ = grid_->comm().sum(numElements_);
45
2/5
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✗ Branch 2 not taken.
12 numVertices_ = grid_->comm().sum(numVertices_);
46 }
47 73 }
48
49 480 bool wasInserted(const Intersection& intersection) const
50
1/2
✓ Branch 1 taken 480 times.
✗ Branch 2 not taken.
480 { return factory_->wasInserted(intersection); }
51
52 template<class Entity>
53 203678 auto insertionIndex(const Entity& e) const
54
5/14
✓ Branch 1 taken 5088 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2650 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 15892 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 30818 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 12 not taken.
✓ Branch 3 taken 491 times.
203678 { return factory_->insertionIndex(e); }
55
56 2 std::size_t numElements() const
57 2 { return numElements_; }
58
59 2 std::size_t numVertices() const
60 2 { return numVertices_; }
61
62 private:
63 std::shared_ptr<Grid> grid_;
64 std::shared_ptr<Dune::GridFactory<Grid>> factory_;
65 std::size_t numElements_, numVertices_;
66 };
67
68 template<class Grid>
69 concept CartesianGrid = Dune::Capabilities::isCartesian<Grid>::v;
70
71 template<CartesianGrid Grid>
72 struct GridInput<Grid>
73 {
74 using Element = typename Grid::template Codim<0>::Entity;
75 using Vertex = typename Grid::template Codim<Grid::dimension>::Entity;
76 using GlobalPosition = typename Element::Geometry::GlobalCoordinate;
77 using Intersection = typename Grid::LeafIntersection;
78 public:
79
80 template<class ImageGrid>
81 GridInput(std::shared_ptr<Grid> grid, std::shared_ptr<ImageGrid> factory)
82 : grid_(std::move(grid))
83 {
84 cells_ = vertices_ = factory->cells();
85 for (std::size_t i = 0; i < Grid::dimension; ++i)
86 vertices_[i] += 1;
87
88 lowerLeft_ = lowerLeftVertices_ = factory->lowerLeft();
89 upperRight_ = upperRightVertices_ = factory->upperRight();
90
91 const auto spacing = factory->spacing();
92 for (std::size_t i = 0; i < Grid::dimension; ++i)
93 {
94 lowerLeftVertices_[i] -= 0.5 * spacing[i];
95 upperRightVertices_[i] += 0.5 * spacing[i];
96 }
97
98 numElements_ = std::accumulate(cells_.begin(), cells_.end(), 1, std::multiplies<int>());
99 numVertices_ = std::accumulate(vertices_.begin(), vertices_.end(), 1, std::multiplies<int>());
100 }
101
102 bool wasInserted(const Intersection& intersection) const
103 { return false; }
104
105 unsigned int insertionIndex(const Element& e) const
106 {
107 const auto c = e.geometry().center();
108 return intersectingEntityCartesianGrid(c, lowerLeft_, upperRight_, cells_);
109 }
110
111 unsigned int insertionIndex(const Vertex& v) const
112 {
113 const auto c = v.geometry().corner(0);
114 return intersectingEntityCartesianGrid(c, lowerLeftVertices_, upperRightVertices_, vertices_);
115 }
116
117 std::size_t numElements() const { return numElements_; }
118 std::size_t numVertices() const { return numVertices_; }
119
120 const GlobalPosition& lowerLeft() const { return lowerLeft_; }
121 const GlobalPosition& upperRight() const { return upperRight_; }
122
123 const GlobalPosition& lowerLeftVertices() const { return lowerLeftVertices_; }
124 const GlobalPosition& upperRightVertices() const { return upperRightVertices_; }
125
126 const std::array<int, Grid::dimension>& cells() const { return cells_; }
127 const std::array<int, Grid::dimension>& vertices() const { return vertices_; }
128
129 private:
130 std::array<int, Grid::dimension> cells_, vertices_;
131 GlobalPosition lowerLeft_, lowerLeftVertices_;
132 GlobalPosition upperRight_, upperRightVertices_;
133
134 std::shared_ptr<Grid> grid_;
135 std::size_t numElements_, numVertices_;
136 };
137
138 } // end namespace Dumux::Detail::GridIO
139
140 #endif
141