AquiferNumerical.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 Equinor ASA
3 Copyright (C) 2020 SINTEF Digital
4
5 This file is part of the Open Porous Media project (OPM).
6
7 OPM is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 OPM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with OPM. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#ifndef OPM_AQUIFERNUMERICAL_HEADER_INCLUDED
22#define OPM_AQUIFERNUMERICAL_HEADER_INCLUDED
23
24#include <dune/grid/common/partitionset.hh>
25
26#include <opm/input/eclipse/EclipseState/Aquifer/NumericalAquifer/SingleNumericalAquifer.hpp>
27
28#include <opm/material/common/MathToolbox.hpp>
29#include <opm/material/densead/Evaluation.hpp>
30
31#include <opm/output/data/Aquifer.hpp>
32
35
36#include <algorithm>
37#include <cassert>
38#include <cstddef>
39#include <vector>
40
41namespace Opm
42{
43template <typename TypeTag>
44class AquiferNumerical : public AquiferInterface<TypeTag>
45{
46public:
56
57 enum { dimWorld = GridView::dimensionworld };
58 enum { numPhases = FluidSystem::numPhases };
59 static constexpr int numEq = BlackoilIndices::numEq;
60
62 using Toolbox = MathToolbox<Eval>;
63
65
66 // Constructor
67 AquiferNumerical(const SingleNumericalAquifer& aquifer,
68 const Simulator& simulator)
69 : AquiferInterface<TypeTag>(aquifer.id(), simulator)
70 , flux_rate_ (0.0)
71 , cumulative_flux_(0.0)
72 , init_pressure_ (aquifer.numCells(), 0.0)
73 {
74 this->cell_to_aquifer_cell_idx_.resize(this->simulator_.gridView().size(/*codim=*/0), -1);
75
76 for (std::size_t idx = 0; idx < aquifer.numCells(); ++idx) {
77 const auto* cell = aquifer.getCellPrt(idx);
78
79 // Include overlap cells so that internal aquifer faces remain identifiable
80 // when their cells are owned by different processes.
81 const int compressed_idx = simulator.vanguard().compressedIndex(cell->global_index);
82 if (compressed_idx >= 0) {
83 this->cell_to_aquifer_cell_idx_[compressed_idx] = idx;
84 }
85
86 if (idx == 0) {
87 this->connects_to_reservoir_
88 = simulator.vanguard().compressedIndexForInterior(cell->global_index) >= 0;
89 }
90 }
91 }
92
94 {
95 AquiferNumerical result({}, simulator);
96 result.flux_rate_ = 1.0;
97 result.cumulative_flux_ = 2.0;
98 result.init_pressure_ = {3.0, 4.0};
99 result.pressure_ = 5.0;
100
101 return result;
102 }
103
104 void initFromRestart(const data::Aquifers& aquiferSoln) override
105 {
106 auto xaqPos = aquiferSoln.find(this->aquiferID());
107 if (xaqPos == aquiferSoln.end())
108 return;
109
110 if (this->connects_to_reservoir_) {
111 this->cumulative_flux_ = xaqPos->second.volume;
112 }
113
114 if (const auto* aqData = xaqPos->second.typeData.template get<data::AquiferType::Numerical>();
115 aqData != nullptr)
116 {
117 this->init_pressure_.resize(aqData->initPressure.size());
118 std::ranges::copy(aqData->initPressure, this->init_pressure_.begin());
119 }
120
121 this->solution_set_from_restart_ = true;
122 }
123
124 void beginTimeStep() override {}
125 void addToSource(RateVector&, const unsigned, const unsigned) override {}
126
127 void endTimeStep() override
128 {
129 this->pressure_ = this->calculateAquiferPressure();
130 this->flux_rate_ = this->calculateAquiferFluxRate();
131 this->cumulative_flux_ += this->flux_rate_ * this->simulator_.timeStepSize();
132 }
133
134 data::AquiferData aquiferData() const override
135 {
136 data::AquiferData data;
137 data.aquiferID = this->aquiferID();
138 data.pressure = this->pressure_;
139 data.fluxRate = this->flux_rate_;
140 data.volume = this->cumulative_flux_;
141
142 auto* aquNum = data.typeData.template create<data::AquiferType::Numerical>();
143 aquNum->initPressure.resize(this->init_pressure_.size());
144 std::ranges::copy(this->init_pressure_, aquNum->initPressure.begin());
145
146 return data;
147 }
148
150 {
151 if (this->solution_set_from_restart_) {
152 return;
153 }
154
155 this->pressure_ = this->calculateAquiferPressure(this->init_pressure_);
156 this->flux_rate_ = 0.;
157 this->cumulative_flux_ = 0.;
158 }
159
160 void computeFaceAreaFraction(const std::vector<Scalar>& /*total_face_area*/) override
161 {}
162
163 Scalar totalFaceArea() const override
164 {
165 return 1.0;
166 }
167
168 template<class Serializer>
169 void serializeOp(Serializer& serializer)
170 {
171 serializer(flux_rate_);
172 serializer(cumulative_flux_);
173 serializer(init_pressure_);
174 serializer(pressure_);
175 }
176
177 bool operator==(const AquiferNumerical& rhs) const
178 {
179 return this->flux_rate_ == rhs.flux_rate_ &&
180 this->cumulative_flux_ == rhs.cumulative_flux_ &&
181 this->init_pressure_ == rhs.init_pressure_ &&
182 this->pressure_ == rhs.pressure_;
183 }
184
186 {
187 return this->cumulative_flux_;
188 }
189
190private:
191 Scalar calculateAquiferPressure() const
192 {
193 auto capture = std::vector<Scalar>(this->init_pressure_.size(), 0.0);
194 return this->calculateAquiferPressure(capture);
195 }
196
197 Scalar calculateAquiferPressure(std::vector<Scalar>& cell_pressure) const
198 {
199 Scalar sum_pressure_watervolume = 0.;
200 Scalar sum_watervolume = 0.;
201
202 ElementContext elem_ctx(this->simulator_);
203 const auto& gridView = this->simulator_.gridView();
205
206 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
207 elem_ctx.updatePrimaryStencil(elem);
208
209 const std::size_t cell_index = elem_ctx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
210 const int idx = this->cell_to_aquifer_cell_idx_[cell_index];
211 if (idx < 0) {
212 continue;
213 }
214
215 elem_ctx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
216 const auto& iq0 = elem_ctx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
217 const auto& fs = iq0.fluidState();
218
219 // TODO: the porosity of the cells are still wrong for numerical aquifer cells
220 // Because the dofVolume still based on the grid information.
221 // The pore volume is correct. Extra efforts will be done to get sensible porosity value here later.
222 const Scalar water_saturation = fs.saturation(this->phaseIdx_()).value();
223 const Scalar porosity = iq0.porosity().value();
224 const Scalar volume = elem_ctx.dofTotalVolume(0, 0);
225 // TODO: not sure we should use water pressure here
226 const Scalar water_pressure_reservoir = fs.pressure(this->phaseIdx_()).value();
227 const Scalar water_volume = volume * porosity * water_saturation;
228 sum_pressure_watervolume += water_volume * water_pressure_reservoir;
229 sum_watervolume += water_volume;
230
231 cell_pressure[idx] = water_pressure_reservoir;
232 }
233 OPM_END_PARALLEL_TRY_CATCH("AquiferNumerical::calculateAquiferPressure() failed: ",
234 this->simulator_.vanguard().grid().comm());
235 const auto& comm = this->simulator_.vanguard().grid().comm();
236 comm.sum(&sum_pressure_watervolume, 1);
237 comm.sum(&sum_watervolume, 1);
238
239 // Ensure all processes have same notion of the aquifer cells' pressure values.
240 comm.sum(cell_pressure.data(), cell_pressure.size());
241
242 return sum_pressure_watervolume / sum_watervolume;
243 }
244
245 template <class ElemCtx>
246 Scalar getWaterFlux(const ElemCtx& elem_ctx, unsigned face_idx) const
247 {
248 const auto& exQuants = elem_ctx.extensiveQuantities(face_idx, /*timeIdx*/ 0);
249 const Scalar water_flux = Toolbox::value(exQuants.volumeFlux(this->phaseIdx_()));
250 return water_flux;
251 }
252
253 Scalar calculateAquiferFluxRate() const
254 {
255 Scalar aquifer_flux = 0.0;
256
257 if (! this->connects_to_reservoir_) {
258 return aquifer_flux;
259 }
260
261 ElementContext elem_ctx(this->simulator_);
262 const auto& gridView = this->simulator_.gridView();
263 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
264 elem_ctx.updatePrimaryStencil(elem);
265 const std::size_t cell_index = elem_ctx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
266 const int idx = this->cell_to_aquifer_cell_idx_[cell_index];
267 // we only need the first aquifer cell
268 if (idx != 0) {
269 continue;
270 }
271
272 elem_ctx.updateStencil(elem);
273 const std::size_t num_interior_faces = elem_ctx.numInteriorFaces(/*timeIdx*/ 0);
274 const auto& stencil = elem_ctx.stencil(0);
275 elem_ctx.updateAllIntensiveQuantities();
276 elem_ctx.updateAllExtensiveQuantities();
277
278 for (std::size_t face_idx = 0; face_idx < num_interior_faces; ++face_idx) {
279 const auto& face = stencil.interiorFace(face_idx);
280 // dof index
281 const std::size_t i = face.interiorIndex();
282 const std::size_t j = face.exteriorIndex();
283 // compressed index
284 // const std::size_t I = stencil.globalSpaceIndex(i);
285 const std::size_t J = stencil.globalSpaceIndex(j);
286
287 assert(stencil.globalSpaceIndex(i) == cell_index);
288
289 // we do not consider the flux within aquifer cells
290 // we only need the flux to the connections
291 if (this->cell_to_aquifer_cell_idx_[J] > 0) {
292 continue;
293 }
294
295 const Scalar water_flux = getWaterFlux(elem_ctx,face_idx);
296 const std::size_t up_id = water_flux >= 0.0 ? i : j;
297 const auto& intQuantsIn = elem_ctx.intensiveQuantities(up_id, 0);
298 const Scalar invB = Toolbox::value(intQuantsIn.fluidState().invB(this->phaseIdx_()));
299 const Scalar face_area = face.area();
300 aquifer_flux += water_flux * invB * face_area;
301 }
302
303 // we only need to handle the first aquifer cell, we can exit loop here
304 break;
305 }
306
307 return aquifer_flux;
308 }
309
310 Scalar flux_rate_; // aquifer influx rate
311 Scalar cumulative_flux_; // cumulative aquifer influx
312 std::vector<Scalar> init_pressure_{};
313 Scalar pressure_; // aquifer pressure
314 bool solution_set_from_restart_ {false};
315 bool connects_to_reservoir_ {false};
316
317 // TODO: maybe unordered_map can also do the work to save memory?
318 std::vector<int> cell_to_aquifer_cell_idx_;
319};
320
321} // namespace Opm
322
323#endif
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:197
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:160
Definition: AquiferInterface.hpp:41
const Simulator & simulator_
Definition: AquiferInterface.hpp:119
int phaseIdx_() const
Definition: AquiferInterface.hpp:109
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: AquiferInterface.hpp:45
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: AquiferInterface.hpp:43
int aquiferID() const
Definition: AquiferInterface.hpp:93
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: AquiferInterface.hpp:44
Definition: AquiferNumerical.hpp:45
void computeFaceAreaFraction(const std::vector< Scalar > &) override
Definition: AquiferNumerical.hpp:160
GetPropType< TypeTag, Properties::Indices > BlackoilIndices
Definition: AquiferNumerical.hpp:47
@ dimWorld
Definition: AquiferNumerical.hpp:57
MathToolbox< Eval > Toolbox
Definition: AquiferNumerical.hpp:62
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: AquiferNumerical.hpp:48
void serializeOp(Serializer &serializer)
Definition: AquiferNumerical.hpp:169
void initialSolutionApplied() override
Definition: AquiferNumerical.hpp:149
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: AquiferNumerical.hpp:55
GetPropType< TypeTag, Properties::ExtensiveQuantities > ExtensiveQuantities
Definition: AquiferNumerical.hpp:49
GetPropType< TypeTag, Properties::MaterialLaw > MaterialLaw
Definition: AquiferNumerical.hpp:53
AquiferNumerical(const SingleNumericalAquifer &aquifer, const Simulator &simulator)
Definition: AquiferNumerical.hpp:67
GetPropType< TypeTag, Properties::Evaluation > Eval
Definition: AquiferNumerical.hpp:61
Scalar cumulativeFlux() const
Definition: AquiferNumerical.hpp:185
Scalar totalFaceArea() const override
Definition: AquiferNumerical.hpp:163
GetPropType< TypeTag, Properties::IntensiveQuantities > IntensiveQuantities
Definition: AquiferNumerical.hpp:52
@ numPhases
Definition: AquiferNumerical.hpp:58
void endTimeStep() override
Definition: AquiferNumerical.hpp:127
void addToSource(RateVector &, const unsigned, const unsigned) override
Definition: AquiferNumerical.hpp:125
data::AquiferData aquiferData() const override
Definition: AquiferNumerical.hpp:134
bool operator==(const AquiferNumerical &rhs) const
Definition: AquiferNumerical.hpp:177
void initFromRestart(const data::Aquifers &aquiferSoln) override
Definition: AquiferNumerical.hpp:104
static constexpr int numEq
Definition: AquiferNumerical.hpp:59
GetPropType< TypeTag, Properties::GridView > GridView
Definition: AquiferNumerical.hpp:51
void beginTimeStep() override
Definition: AquiferNumerical.hpp:124
static AquiferNumerical serializationTestObject(const Simulator &simulator)
Definition: AquiferNumerical.hpp:93
Definition: blackoilbioeffectsmodules.hh:45
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:233