My Project
Loading...
Searching...
No Matches
PropsDataHandle.hpp
1/*
2 Copyright 2020, 2023 Equinor AS.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM 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 OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
26#ifndef PROPS_DATAHANDLE_HPP
27#define PROPS_DATAHANDLE_HPP
28
29#if HAVE_MPI
30
31#include <opm/input/eclipse/EclipseState/Grid/FieldData.hpp>
32
33#include <opm/simulators/utils/ParallelEclipseState.hpp>
34#include <opm/simulators/utils/ParallelRestart.hpp>
35#include <dune/grid/common/datahandleif.hh>
36#include <dune/grid/common/mcmgmapper.hh>
37#include <dune/grid/common/partitionset.hh>
38#include <dune/common/parallel/mpihelper.hh>
39#include <unordered_map>
40#include <iostream>
41
42namespace Opm
43{
44
50template<class Grid>
51class PropsDataHandle
52 : public Dune::CommDataHandleIF< PropsDataHandle<Grid>, double>
53{
54public:
56 using DataType = std::pair<double, unsigned char>;
57
62 PropsDataHandle(const Grid& grid, ParallelEclipseState& eclState)
63 : m_grid(grid),
64 m_distributed_fieldProps(eclState.m_fieldProps)
65 {
66 // Scatter the keys
67 const Parallel::Communication comm = m_grid.comm();
68 if (comm.rank() == 0)
69 {
70 const FieldPropsManager& globalProps = eclState.globalFieldProps();
71 m_intKeys = globalProps.keys<int>();
72 m_doubleKeys = globalProps.keys<double>();
73 m_distributed_fieldProps.copyTran(globalProps);
74 }
75
76 EclMpiSerializer ser(comm);
77 ser.broadcast(*this);
78
79 m_no_data = m_intKeys.size() + m_doubleKeys.size();
80
81 if (comm.rank() == 0) {
82 const FieldPropsManager& globalProps = eclState.globalFieldProps();
83 const auto& idSet = m_grid.localIdSet();
84 const auto& gridView = m_grid.levelGridView(0);
85 using ElementMapper =
86 Dune::MultipleCodimMultipleGeomTypeMapper<typename Grid::LevelGridView>;
87 ElementMapper elemMapper(gridView, Dune::mcmgElementLayout());
88
89 for (const auto &element : elements(gridView, Dune::Partitions::interiorBorder))
90 {
91 const auto& id = idSet.id(element);
92 auto index = elemMapper.index(element);
93 auto& data = elementData_[id];
94 data.reserve(m_no_data);
95
96 for (const auto& intKey : m_intKeys)
97 {
98 const auto& fieldData = globalProps.get_int_field_data(intKey);
99 data.emplace_back(fieldData.data[index],
100 static_cast<unsigned char>(fieldData.value_status[index]));
101 }
102
103 for (const auto& doubleKey : m_doubleKeys)
104 {
105 // We need to allow unsupported keywords to get the data
106 // for TranCalculator, too.
107 const auto& fieldData = globalProps.get_double_field_data(doubleKey,
108 /* allow_unsupported = */ true);
109 data.emplace_back(fieldData.data[index],
110 static_cast<unsigned char>(fieldData.value_status[index]));
111 }
112 }
113 }
114 }
115
116 ~PropsDataHandle()
117 {
118 // distributed grid is now correctly set up.
119 for (const auto& intKey : m_intKeys)
120 {
121 m_distributed_fieldProps.m_intProps[intKey].data.resize(m_grid.size(0));
122 m_distributed_fieldProps.m_intProps[intKey].value_status.resize(m_grid.size(0));
123 }
124
125 for (const auto& doubleKey : m_doubleKeys)
126 {
127 m_distributed_fieldProps.m_doubleProps[doubleKey].data.resize(m_grid.size(0));
128 m_distributed_fieldProps.m_doubleProps[doubleKey].value_status.resize(m_grid.size(0));
129 }
130
131 // copy data for the persistent mao to the field properties
132 const auto& idSet = m_grid.localIdSet();
133 const auto& gridView = m_grid.levelGridView(0);
134 using ElementMapper =
135 Dune::MultipleCodimMultipleGeomTypeMapper<typename Grid::LevelGridView>;
136 ElementMapper elemMapper(gridView, Dune::mcmgElementLayout());
137
138 for (const auto &element : elements( gridView, Dune::Partitions::all))
139 {
140 std::size_t counter{};
141 const auto& id = idSet.id(element);
142 auto index = elemMapper.index(element);
143 auto data = elementData_.find(id);
144 assert(data != elementData_.end());
145
146 for (const auto& intKey : m_intKeys)
147 {
148 const auto& pair = data->second[counter++];
149 m_distributed_fieldProps.m_intProps[intKey].data[index] = static_cast<int>(pair.first);
150 m_distributed_fieldProps.m_intProps[intKey].value_status[index] = static_cast<value::status>(pair.second);
151 }
152
153 for (const auto& doubleKey : m_doubleKeys)
154 {
155 const auto& pair = data->second[counter++];
156 m_distributed_fieldProps.m_doubleProps[doubleKey].data[index] = pair.first;
157 m_distributed_fieldProps.m_doubleProps[doubleKey].value_status[index] = static_cast<value::status>(pair.second);
158 }
159 }
160 }
161
162 bool contains(int /* dim */, int codim)
163 {
164 return codim == 0;
165 }
166
167 bool fixedsize(int /* dim */, int /* codim */)
168 {
169 return true;
170 }
171 bool fixedSize(int /* dim */, int /* codim */)
172 {
173 return true;
174 }
175
176 template<class EntityType>
177 std::size_t size(const EntityType /* entity */)
178 {
179 return m_no_data;
180 }
181
182 template<class BufferType, class EntityType>
183 void gather(BufferType& buffer, const EntityType& e) const
184 {
185 auto iter = elementData_.find(m_grid.localIdSet().id(e));
186 assert(iter != elementData_.end());
187 for (const auto& data : iter->second)
188 {
189 buffer.write(data);
190 }
191 }
192
193 template<class BufferType, class EntityType>
194 void scatter(BufferType& buffer, const EntityType& e, std::size_t n)
195 {
196 assert(n == m_no_data);
197 auto& array = elementData_[m_grid.localIdSet().id(e)];
198 array.resize(n);
199 for (auto& data : array)
200 {
201 buffer.read(data);
202 }
203 }
204
205 template<class Serializer>
206 void serializeOp(Serializer& serializer)
207 {
208 serializer(m_intKeys);
209 serializer(m_doubleKeys);
210 m_distributed_fieldProps.serializeOp(serializer);
211 }
212
213private:
214 using LocalIdSet = typename Grid::LocalIdSet;
215 const Grid& m_grid;
217 ParallelFieldPropsManager& m_distributed_fieldProps;
219 std::vector<std::string> m_intKeys;
221 std::vector<std::string> m_doubleKeys;
225 std::unordered_map<typename LocalIdSet::IdType, std::vector<std::pair<double,unsigned char> > > elementData_;
227 std::size_t m_no_data;
228};
229
230} // end namespace Opm
231#endif // HAVE_MPI
232#endif // PROPS_DATAHANDLE_HPP
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition BlackoilPhases.hpp:27