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 <opm/input/eclipse/EclipseState/Aquifer/NumericalAquifer/SingleNumericalAquifer.hpp>
25
26#include <opm/material/common/MathToolbox.hpp>
27#include <opm/material/densead/Evaluation.hpp>
28
29#include <opm/output/data/Aquifer.hpp>
30
33
34#include <algorithm>
35#include <cassert>
36#include <cstddef>
37#include <vector>
38
39namespace Opm
40{
41template <typename TypeTag>
42class AquiferNumerical : public AquiferInterface<TypeTag>
43{
44public:
45 using BlackoilIndices = GetPropType<TypeTag, Properties::Indices>;
46 using ElementContext = GetPropType<TypeTag, Properties::ElementContext>;
47 using ExtensiveQuantities = GetPropType<TypeTag, Properties::ExtensiveQuantities>;
48 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
49 using GridView = GetPropType<TypeTag, Properties::GridView>;
50 using IntensiveQuantities = GetPropType<TypeTag, Properties::IntensiveQuantities>;
51 using MaterialLaw = GetPropType<TypeTag, Properties::MaterialLaw>;
52 using Simulator = GetPropType<TypeTag, Properties::Simulator>;
53 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
54
55 enum { dimWorld = GridView::dimensionworld };
56 enum { numPhases = FluidSystem::numPhases };
57 static constexpr int numEq = BlackoilIndices::numEq;
58
59 using Eval = DenseAd::Evaluation<Scalar, numEq>;
60 using Toolbox = MathToolbox<Eval>;
61
63
64 // Constructor
65 AquiferNumerical(const SingleNumericalAquifer& aquifer,
66 const Simulator& simulator)
67 : AquiferInterface<TypeTag>(aquifer.id(), simulator)
68 , flux_rate_ (0.0)
69 , cumulative_flux_(0.0)
70 , init_pressure_ (aquifer.numCells(), 0.0)
71 {
72 this->cell_to_aquifer_cell_idx_.resize(this->simulator_.gridView().size(/*codim=*/0), -1);
73
74 auto aquifer_on_process = false;
75 for (std::size_t idx = 0; idx < aquifer.numCells(); ++idx) {
76 const auto* cell = aquifer.getCellPrt(idx);
77
78 // Due to parallelisation, the cell might not exist in the current process
79 const int compressed_idx = simulator.vanguard().compressedIndexForInterior(cell->global_index);
80 if (compressed_idx >= 0) {
81 this->cell_to_aquifer_cell_idx_[compressed_idx] = idx;
82 aquifer_on_process = true;
83 }
84 }
85
86 if (aquifer_on_process) {
87 this->checkConnectsToReservoir();
88 }
89 }
90
92 {
93 AquiferNumerical result({}, simulator);
94 result.flux_rate_ = 1.0;
95 result.cumulative_flux_ = 2.0;
96 result.init_pressure_ = {3.0, 4.0};
97 result.pressure_ = 5.0;
98
99 return result;
100 }
101
102 void initFromRestart(const data::Aquifers& aquiferSoln) override
103 {
104 auto xaqPos = aquiferSoln.find(this->aquiferID());
105 if (xaqPos == aquiferSoln.end())
106 return;
107
108 if (this->connects_to_reservoir_) {
109 this->cumulative_flux_ = xaqPos->second.volume;
110 }
111
112 if (const auto* aqData = xaqPos->second.typeData.template get<data::AquiferType::Numerical>();
113 aqData != nullptr)
114 {
115 this->init_pressure_.resize(aqData->initPressure.size());
116 std::copy(aqData->initPressure.begin(),
117 aqData->initPressure.end(),
118 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::copy(this->init_pressure_.begin(),
145 this->init_pressure_.end(),
146 aquNum->initPressure.begin());
147
148 return data;
149 }
150
152 {
153 if (this->solution_set_from_restart_) {
154 return;
155 }
156
157 this->pressure_ = this->calculateAquiferPressure(this->init_pressure_);
158 this->flux_rate_ = 0.;
159 this->cumulative_flux_ = 0.;
160 }
161
162 void computeFaceAreaFraction(const std::vector<Scalar>& /*total_face_area*/) override
163 {}
164
165 Scalar totalFaceArea() const override
166 {
167 return 1.0;
168 }
169
170 template<class Serializer>
171 void serializeOp(Serializer& serializer)
172 {
173 serializer(flux_rate_);
174 serializer(cumulative_flux_);
175 serializer(init_pressure_);
176 serializer(pressure_);
177 }
178
179 bool operator==(const AquiferNumerical& rhs) const
180 {
181 return this->flux_rate_ == rhs.flux_rate_ &&
182 this->cumulative_flux_ == rhs.cumulative_flux_ &&
183 this->init_pressure_ == rhs.init_pressure_ &&
184 this->pressure_ == rhs.pressure_;
185 }
186
188 {
189 return this->cumulative_flux_;
190 }
191
192private:
193 void checkConnectsToReservoir()
194 {
195 ElementContext elem_ctx(this->simulator_);
196 auto elemIt = std::find_if(this->simulator_.gridView().template begin</*codim=*/0>(),
197 this->simulator_.gridView().template end</*codim=*/0>(),
198 [&elem_ctx, this](const auto& elem) -> bool
199 {
200 elem_ctx.updateStencil(elem);
201
202 const auto cell_index = elem_ctx
203 .globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
204
205 return this->cell_to_aquifer_cell_idx_[cell_index] == 0;
206 });
207
208 assert ((elemIt != this->simulator_.gridView().template end</*codim=*/0>())
209 && "Internal error locating numerical aquifer's connecting cell");
210
211 this->connects_to_reservoir_ =
212 elemIt->partitionType() == Dune::InteriorEntity;
213 }
214
215 Scalar calculateAquiferPressure() const
216 {
217 auto capture = std::vector<Scalar>(this->init_pressure_.size(), 0.0);
218 return this->calculateAquiferPressure(capture);
219 }
220
221 Scalar calculateAquiferPressure(std::vector<Scalar>& cell_pressure) const
222 {
223 Scalar sum_pressure_watervolume = 0.;
224 Scalar sum_watervolume = 0.;
225
226 ElementContext elem_ctx(this->simulator_);
227 const auto& gridView = this->simulator_.gridView();
229
230 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
231 elem_ctx.updatePrimaryStencil(elem);
232
233 const std::size_t cell_index = elem_ctx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
234 const int idx = this->cell_to_aquifer_cell_idx_[cell_index];
235 if (idx < 0) {
236 continue;
237 }
238
239 elem_ctx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
240 const auto& iq0 = elem_ctx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
241 const auto& fs = iq0.fluidState();
242
243 // TODO: the porosity of the cells are still wrong for numerical aquifer cells
244 // Because the dofVolume still based on the grid information.
245 // The pore volume is correct. Extra efforts will be done to get sensible porosity value here later.
246 const Scalar water_saturation = fs.saturation(this->phaseIdx_()).value();
247 const Scalar porosity = iq0.porosity().value();
248 const Scalar volume = elem_ctx.dofTotalVolume(0, 0);
249 // TODO: not sure we should use water pressure here
250 const Scalar water_pressure_reservoir = fs.pressure(this->phaseIdx_()).value();
251 const Scalar water_volume = volume * porosity * water_saturation;
252 sum_pressure_watervolume += water_volume * water_pressure_reservoir;
253 sum_watervolume += water_volume;
254
255 cell_pressure[idx] = water_pressure_reservoir;
256 }
257 OPM_END_PARALLEL_TRY_CATCH("AquiferNumerical::calculateAquiferPressure() failed: ",
258 this->simulator_.vanguard().grid().comm());
259 const auto& comm = this->simulator_.vanguard().grid().comm();
260 comm.sum(&sum_pressure_watervolume, 1);
261 comm.sum(&sum_watervolume, 1);
262
263 // Ensure all processes have same notion of the aquifer cells' pressure values.
264 comm.sum(cell_pressure.data(), cell_pressure.size());
265
266 return sum_pressure_watervolume / sum_watervolume;
267 }
268
269 template <class ElemCtx>
270 Scalar getWaterFlux(const ElemCtx& elem_ctx, unsigned face_idx) const
271 {
272 const auto& exQuants = elem_ctx.extensiveQuantities(face_idx, /*timeIdx*/ 0);
273 const Scalar water_flux = Toolbox::value(exQuants.volumeFlux(this->phaseIdx_()));
274 return water_flux;
275 }
276
277 Scalar calculateAquiferFluxRate() const
278 {
279 Scalar aquifer_flux = 0.0;
280
281 if (! this->connects_to_reservoir_) {
282 return aquifer_flux;
283 }
284
285 ElementContext elem_ctx(this->simulator_);
286 const auto& gridView = this->simulator_.gridView();
287 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
288 // elem_ctx.updatePrimaryStencil(elem);
289 elem_ctx.updateStencil(elem);
290
291 const std::size_t cell_index = elem_ctx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
292 const int idx = this->cell_to_aquifer_cell_idx_[cell_index];
293 // we only need the first aquifer cell
294 if (idx != 0) {
295 continue;
296 }
297
298 const std::size_t num_interior_faces = elem_ctx.numInteriorFaces(/*timeIdx*/ 0);
299 // const auto &problem = elem_ctx.problem();
300 const auto& stencil = elem_ctx.stencil(0);
301 // const auto& inQuants = elem_ctx.intensiveQuantities(0, /*timeIdx*/ 0);
302
303 for (std::size_t face_idx = 0; face_idx < num_interior_faces; ++face_idx) {
304 const auto& face = stencil.interiorFace(face_idx);
305 // dof index
306 const std::size_t i = face.interiorIndex();
307 const std::size_t j = face.exteriorIndex();
308 // compressed index
309 // const std::size_t I = stencil.globalSpaceIndex(i);
310 const std::size_t J = stencil.globalSpaceIndex(j);
311
312 assert(stencil.globalSpaceIndex(i) == cell_index);
313
314 // we do not consider the flux within aquifer cells
315 // we only need the flux to the connections
316 if (this->cell_to_aquifer_cell_idx_[J] > 0) {
317 continue;
318 }
319 elem_ctx.updateAllIntensiveQuantities();
320 elem_ctx.updateAllExtensiveQuantities();
321
322 const Scalar water_flux = getWaterFlux(elem_ctx,face_idx);
323 const std::size_t up_id = water_flux >= 0.0 ? i : j;
324 const auto& intQuantsIn = elem_ctx.intensiveQuantities(up_id, 0);
325 const Scalar invB = Toolbox::value(intQuantsIn.fluidState().invB(this->phaseIdx_()));
326 const Scalar face_area = face.area();
327 aquifer_flux += water_flux * invB * face_area;
328 }
329
330 // we only need to handle the first aquifer cell, we can exit loop here
331 break;
332 }
333
334 return aquifer_flux;
335 }
336
337 Scalar flux_rate_; // aquifer influx rate
338 Scalar cumulative_flux_; // cumulative aquifer influx
339 std::vector<Scalar> init_pressure_{};
340 Scalar pressure_; // aquifer pressure
341 bool solution_set_from_restart_ {false};
342 bool connects_to_reservoir_ {false};
343
344 // TODO: maybe unordered_map can also do the work to save memory?
345 std::vector<int> cell_to_aquifer_cell_idx_;
346};
347
348} // namespace Opm
349
350#endif
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:172
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:138
Definition: AquiferInterface.hpp:35
const Simulator & simulator_
Definition: AquiferInterface.hpp:98
int phaseIdx_() const
Definition: AquiferInterface.hpp:88
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: AquiferInterface.hpp:39
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: AquiferInterface.hpp:37
int aquiferID() const
Definition: AquiferInterface.hpp:79
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: AquiferInterface.hpp:38
Definition: AquiferNumerical.hpp:43
void computeFaceAreaFraction(const std::vector< Scalar > &) override
Definition: AquiferNumerical.hpp:162
GetPropType< TypeTag, Properties::Indices > BlackoilIndices
Definition: AquiferNumerical.hpp:45
@ dimWorld
Definition: AquiferNumerical.hpp:55
MathToolbox< Eval > Toolbox
Definition: AquiferNumerical.hpp:60
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: AquiferNumerical.hpp:46
void serializeOp(Serializer &serializer)
Definition: AquiferNumerical.hpp:171
void initialSolutionApplied() override
Definition: AquiferNumerical.hpp:151
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: AquiferNumerical.hpp:53
GetPropType< TypeTag, Properties::ExtensiveQuantities > ExtensiveQuantities
Definition: AquiferNumerical.hpp:47
GetPropType< TypeTag, Properties::MaterialLaw > MaterialLaw
Definition: AquiferNumerical.hpp:51
AquiferNumerical(const SingleNumericalAquifer &aquifer, const Simulator &simulator)
Definition: AquiferNumerical.hpp:65
Scalar cumulativeFlux() const
Definition: AquiferNumerical.hpp:187
Scalar totalFaceArea() const override
Definition: AquiferNumerical.hpp:165
GetPropType< TypeTag, Properties::IntensiveQuantities > IntensiveQuantities
Definition: AquiferNumerical.hpp:50
@ numPhases
Definition: AquiferNumerical.hpp:56
void endTimeStep() override
Definition: AquiferNumerical.hpp:127
DenseAd::Evaluation< Scalar, numEq > Eval
Definition: AquiferNumerical.hpp:59
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:179
void initFromRestart(const data::Aquifers &aquiferSoln) override
Definition: AquiferNumerical.hpp:102
static constexpr int numEq
Definition: AquiferNumerical.hpp:57
GetPropType< TypeTag, Properties::GridView > GridView
Definition: AquiferNumerical.hpp:49
void beginTimeStep() override
Definition: AquiferNumerical.hpp:124
static AquiferNumerical serializationTestObject(const Simulator &simulator)
Definition: AquiferNumerical.hpp:91
Definition: BlackoilPhases.hpp:27