AquiferAnalytical.hpp
Go to the documentation of this file.
1/*
2 Copyright 2017 SINTEF Digital, Mathematics and Cybernetics.
3 Copyright 2017 Statoil ASA.
4 Copyright 2017 IRIS
5
6 This file is part of the Open Porous Media project (OPM).
7
8 OPM is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 OPM is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with OPM. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#ifndef OPM_AQUIFERANALYTICAL_HEADER_INCLUDED
23#define OPM_AQUIFERANALYTICAL_HEADER_INCLUDED
24
25#include <opm/common/ErrorMacros.hpp>
26
27#include <opm/input/eclipse/EclipseState/Aquifer/Aquancon.hpp>
28
29#include <opm/material/common/MathToolbox.hpp>
30#include <opm/material/densead/Evaluation.hpp>
31#include <opm/material/densead/Math.hpp>
32#include <opm/material/fluidstates/BlackOilFluidState.hpp>
33
34#include <opm/models/blackoil/blackoilproperties.hh>
35#include <opm/models/utils/basicproperties.hh>
36
37#include <opm/output/data/Aquifer.hpp>
38
41
42#include <algorithm>
43#include <cmath>
44#include <cstddef>
45#include <limits>
46#include <numeric>
47#include <optional>
48#include <vector>
49
50namespace Opm
51{
52template <typename TypeTag>
53class AquiferAnalytical : public AquiferInterface<TypeTag>
54{
55public:
56 using Simulator = GetPropType<TypeTag, Properties::Simulator>;
57 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
58 using ElementContext = GetPropType<TypeTag, Properties::ElementContext>;
59 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
60 using BlackoilIndices = GetPropType<TypeTag, Properties::Indices>;
61 using RateVector = GetPropType<TypeTag, Properties::RateVector>;
62 using IntensiveQuantities = GetPropType<TypeTag, Properties::IntensiveQuantities>;
63 using ElementMapper = GetPropType<TypeTag, Properties::ElementMapper>;
64
65 enum { enableTemperature = getPropValue<TypeTag, Properties::EnableTemperature>() };
66 enum { enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>() };
67 enum { enableBrine = getPropValue<TypeTag, Properties::EnableBrine>() };
68 enum { enableVapwat = getPropValue<TypeTag, Properties::EnableVapwat>() };
69 enum { has_disgas_in_water = getPropValue<TypeTag, Properties::EnableDisgasInWater>() };
70
71 enum { enableSaltPrecipitation = getPropValue<TypeTag, Properties::EnableSaltPrecipitation>() };
72
73 static constexpr int numEq = BlackoilIndices::numEq;
74
75 using Eval = DenseAd::Evaluation<Scalar, /*size=*/numEq>;
76
77 using FluidState = BlackOilFluidState<Eval,
81 BlackoilIndices::gasEnabled,
86 BlackoilIndices::numPhases>;
87
88 // Constructor
89 AquiferAnalytical(const int aqID,
90 const std::vector<Aquancon::AquancCell>& connections,
91 const Simulator& simulator)
92 : AquiferInterface<TypeTag>(aqID, simulator)
93 , connections_(connections)
94 {
96 }
97
98 // Destructor
100 {}
101
102 void computeFaceAreaFraction(const std::vector<Scalar>& total_face_area) override
103 {
104 assert (total_face_area.size() >= static_cast<typename std::vector<Scalar>::size_type>(this->aquiferID()));
105
106 const auto tfa = total_face_area[this->aquiferID() - 1];
107 const auto eps_sqrt = std::sqrt(std::numeric_limits<Scalar>::epsilon());
108
109 if (tfa < eps_sqrt) {
110 this->alphai_.assign(this->size(), Scalar{0});
111 }
112 else {
113 std::transform(this->faceArea_connected_.begin(),
114 this->faceArea_connected_.end(),
115 this->alphai_.begin(),
116 [tfa](const Scalar area)
117 {
118 return area / tfa;
119 });
120 }
121
122 this->area_fraction_ = this->totalFaceArea() / tfa;
123 }
124
125 Scalar totalFaceArea() const override
126 {
127 return this->total_face_area_;
128 }
129
130 void initFromRestart(const data::Aquifers& aquiferSoln) override
131 {
132 auto xaqPos = aquiferSoln.find(this->aquiferID());
133 if (xaqPos == aquiferSoln.end())
134 return;
135
136 this->assignRestartData(xaqPos->second);
137
138 this->W_flux_ = xaqPos->second.volume * this->area_fraction_;
139 this->pa0_ = xaqPos->second.initPressure;
140
141 this->solution_set_from_restart_ = true;
142 }
143
145 {
147 }
148
149 void beginTimeStep() override
150 {
151 ElementContext elemCtx(this->simulator_);
153
154 for (const auto& elem : elements(this->simulator_.gridView())) {
155 elemCtx.updatePrimaryStencil(elem);
156
157 const int cellIdx = elemCtx.globalSpaceIndex(0, 0);
158 const int idx = cellToConnectionIdx_[cellIdx];
159 if (idx < 0)
160 continue;
161
162 elemCtx.updateIntensiveQuantities(0);
163 const auto& iq = elemCtx.intensiveQuantities(0, 0);
164 pressure_previous_[idx] = getValue(iq.fluidState().pressure(this->phaseIdx_()));
165 }
166
167 OPM_END_PARALLEL_TRY_CATCH("AquiferAnalytical::beginTimeStep() failed: ",
168 this->simulator_.vanguard().grid().comm());
169 }
170
172 const unsigned cellIdx,
173 const unsigned timeIdx) override
174 {
175 const auto& model = this->simulator_.model();
176
177 const int idx = this->cellToConnectionIdx_[cellIdx];
178 if (idx < 0)
179 return;
180
181 const auto& intQuants = model.intensiveQuantities(cellIdx, timeIdx);
182
183 // This is the pressure at td + dt
184 this->updateCellPressure(this->pressure_current_, idx, intQuants);
185 this->calculateInflowRate(idx, this->simulator_);
186
187 rates[BlackoilIndices::conti0EqIdx + compIdx_()]
188 += this->Qai_[idx] / model.dofTotalVolume(cellIdx);
189
190 if constexpr (enableEnergy) {
191 auto fs = intQuants.fluidState();
192 if (this->Ta0_.has_value() && this->Qai_[idx] > 0)
193 {
194 fs.setTemperature(this->Ta0_.value());
195 typedef typename std::decay<decltype(fs)>::type::Scalar FsScalar;
196 typename FluidSystem::template ParameterCache<FsScalar> paramCache;
197 const unsigned pvtRegionIdx = intQuants.pvtRegionIndex();
198 paramCache.setRegionIndex(pvtRegionIdx);
199 paramCache.setMaxOilSat(this->simulator_.problem().maxOilSaturation(cellIdx));
200 paramCache.updatePhase(fs, this->phaseIdx_());
201 const auto& h = FluidSystem::enthalpy(fs, paramCache, this->phaseIdx_());
202 fs.setEnthalpy(this->phaseIdx_(), h);
203 }
204 rates[BlackoilIndices::contiEnergyEqIdx]
205 += this->Qai_[idx] *fs.enthalpy(this->phaseIdx_()) * FluidSystem::referenceDensity( this->phaseIdx_(), intQuants.pvtRegionIndex()) / model.dofTotalVolume(cellIdx);
206
207 }
208 }
209
210 std::size_t size() const
211 {
212 return this->connections_.size();
213 }
214
215 template<class Serializer>
216 void serializeOp(Serializer& serializer)
217 {
218 serializer(pressure_previous_);
219 serializer(pressure_current_);
220 serializer(Qai_);
221 serializer(rhow_);
222 serializer(W_flux_);
223 }
224
225 bool operator==(const AquiferAnalytical& rhs) const
226 {
227 return this->pressure_previous_ == rhs.pressure_previous_ &&
229 this->Qai_ == rhs.Qai_ &&
230 this->rhow_ == rhs.rhow_ &&
231 this->W_flux_ == rhs.W_flux_;
232 }
233
234protected:
235 virtual void assignRestartData(const data::AquiferData& xaq) = 0;
236 virtual void calculateInflowRate(int idx, const Simulator& simulator) = 0;
237 virtual void calculateAquiferCondition() = 0;
238 virtual void calculateAquiferConstants() = 0;
239 virtual Scalar aquiferDepth() const = 0;
240
242 {
243 return this->simulator_.problem().gravity()[2];
244 }
245
246 int compIdx_() const
247 {
248 if (this->co2store_or_h2store_())
249 return FluidSystem::oilCompIdx;
250
251 return FluidSystem::waterCompIdx;
252 }
253
255 {
256 // We reset the cumulative flux at the start of any simulation, so, W_flux = 0
257 if (! this->solution_set_from_restart_) {
258 W_flux_ = Scalar{0};
259 }
260
264
265 this->pressure_previous_.resize(this->size(), Scalar{0});
266 this->pressure_current_.resize(this->size(), Scalar{0});
267 this->Qai_.resize(this->size(), Scalar{0});
268 }
269
270 void updateCellPressure(std::vector<Eval>& pressure_water,
271 const int idx,
272 const IntensiveQuantities& intQuants)
273 {
274 const auto& fs = intQuants.fluidState();
275 pressure_water.at(idx) = fs.pressure(this->phaseIdx_());
276 }
277
278 void updateCellPressure(std::vector<Scalar>& pressure_water,
279 const int idx,
280 const IntensiveQuantities& intQuants)
281 {
282 const auto& fs = intQuants.fluidState();
283 pressure_water.at(idx) = fs.pressure(this->phaseIdx_()).value();
284 }
285
287 {
288 this->alphai_.resize(this->size(), 1.0);
289 this->faceArea_connected_.resize(this->size(), Scalar{0});
290
291 // total_face_area_ is the sum of the areas connected to an aquifer
292 this->total_face_area_ = Scalar{0};
293 this->cellToConnectionIdx_.resize(this->simulator_.gridView().size(/*codim=*/0), -1);
294 const auto& gridView = this->simulator_.vanguard().gridView();
295 for (std::size_t idx = 0; idx < this->size(); ++idx) {
296 const auto global_index = this->connections_[idx].global_index;
297 const int cell_index = this->simulator_.vanguard().compressedIndex(global_index);
298 if (cell_index < 0) {
299 continue;
300 }
301
302 auto elemIt = gridView.template begin</*codim=*/ 0>();
303 std::advance(elemIt, cell_index);
304
305 // The global_index is not part of this grid
306 if (elemIt->partitionType() != Dune::InteriorEntity) {
307 continue;
308 }
309
310 this->cellToConnectionIdx_[cell_index] = idx;
311 }
312
313 // Translate the C face tag into the enum used by opm-parser's TransMult class
314 FaceDir::DirEnum faceDirection;
315
316 // Get areas for all connections
317 const auto& elemMapper = this->simulator_.model().dofMapper();
318 for (const auto& elem : elements(gridView)) {
319 const unsigned cell_index = elemMapper.index(elem);
320 const int idx = this->cellToConnectionIdx_[cell_index];
321
322 // Only deal with connections given by the aquifer
323 if (idx < 0) {
324 continue;
325 }
326
327 for (const auto& intersection : intersections(gridView, elem)) {
328 // Only deal with grid boundaries
329 if (! intersection.boundary()) {
330 continue;
331 }
332
333 switch (intersection.indexInInside()) {
334 case 0:
335 faceDirection = FaceDir::XMinus;
336 break;
337 case 1:
338 faceDirection = FaceDir::XPlus;
339 break;
340 case 2:
341 faceDirection = FaceDir::YMinus;
342 break;
343 case 3:
344 faceDirection = FaceDir::YPlus;
345 break;
346 case 4:
347 faceDirection = FaceDir::ZMinus;
348 break;
349 case 5:
350 faceDirection = FaceDir::ZPlus;
351 break;
352 default:
353 OPM_THROW(std::logic_error,
354 "Internal error in initialization of aquifer.");
355 }
356
357 if (faceDirection == this->connections_[idx].face_dir) {
358 this->faceArea_connected_[idx] = this->connections_[idx].influx_coeff;
359 break;
360 }
361 }
362
363 this->total_face_area_ += this->faceArea_connected_.at(idx);
364 }
365 }
366
368 {
369 this->cell_depth_.resize(this->size(), this->aquiferDepth());
370
371 const auto& gridView = this->simulator_.vanguard().gridView();
372 for (std::size_t idx = 0; idx < this->size(); ++idx) {
373 const int cell_index = this->simulator_.vanguard()
374 .compressedIndex(this->connections_[idx].global_index);
375 if (cell_index < 0) {
376 continue;
377 }
378
379 auto elemIt = gridView.template begin</*codim=*/ 0>();
380 std::advance(elemIt, cell_index);
381
382 // The global_index is not part of this grid
383 if (elemIt->partitionType() != Dune::InteriorEntity) {
384 continue;
385 }
386
387 this->cell_depth_.at(idx) = this->simulator_.vanguard().cellCenterDepth(cell_index);
388 }
389 }
390
391 // This function is for calculating the aquifer properties from equilibrium state with the reservoir
393 {
394 // Since the global_indices are the reservoir index, we just need to extract the fluidstate at those indices
395 std::vector<Scalar> pw_aquifer;
396 Scalar water_pressure_reservoir;
397
398 ElementContext elemCtx(this->simulator_);
399 const auto& gridView = this->simulator_.gridView();
400 for (const auto& elem : elements(gridView)) {
401 elemCtx.updatePrimaryStencil(elem);
402
403 const auto cellIdx = elemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
404 const auto idx = this->cellToConnectionIdx_[cellIdx];
405 if (idx < 0)
406 continue;
407
408 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
409 const auto& iq0 = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
410 const auto& fs = iq0.fluidState();
411
412 water_pressure_reservoir = fs.pressure(this->phaseIdx_()).value();
413 const auto water_density = fs.density(this->phaseIdx_());
414
415 const auto gdz =
416 this->gravity_() * (this->cell_depth_[idx] - this->aquiferDepth());
417
418 pw_aquifer.push_back(this->alphai_[idx] *
419 (water_pressure_reservoir - water_density.value()*gdz));
420 }
421
422 // We take the average of the calculated equilibrium pressures.
423 const auto& comm = this->simulator_.vanguard().grid().comm();
424
425 Scalar vals[2];
426 vals[0] = std::accumulate(this->alphai_.begin(), this->alphai_.end(), Scalar{0});
427 vals[1] = std::accumulate(pw_aquifer.begin(), pw_aquifer.end(), Scalar{0});
428
429 comm.sum(vals, 2);
430
431 return vals[1] / vals[0];
432 }
433
434 const std::vector<Aquancon::AquancCell> connections_;
435
436 // Grid variables
437 std::vector<Scalar> faceArea_connected_;
438 std::vector<int> cellToConnectionIdx_;
439
440 // Quantities at each grid id
441 std::vector<Scalar> cell_depth_;
442 std::vector<Scalar> pressure_previous_;
443 std::vector<Eval> pressure_current_;
444 std::vector<Eval> Qai_;
445 std::vector<Scalar> alphai_;
446
447 Scalar Tc_{}; // Time constant
448 Scalar pa0_{}; // initial aquifer pressure
449 std::optional<Scalar> Ta0_{}; // initial aquifer temperature
451
454
456
459};
460
461} // namespace Opm
462
463#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: AquiferAnalytical.hpp:54
void updateCellPressure(std::vector< Scalar > &pressure_water, const int idx, const IntensiveQuantities &intQuants)
Definition: AquiferAnalytical.hpp:278
Scalar total_face_area_
Definition: AquiferAnalytical.hpp:452
Scalar area_fraction_
Definition: AquiferAnalytical.hpp:453
Scalar gravity_() const
Definition: AquiferAnalytical.hpp:241
std::vector< int > cellToConnectionIdx_
Definition: AquiferAnalytical.hpp:438
Scalar Tc_
Definition: AquiferAnalytical.hpp:447
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: AquiferAnalytical.hpp:56
@ enableVapwat
Definition: AquiferAnalytical.hpp:68
void addToSource(RateVector &rates, const unsigned cellIdx, const unsigned timeIdx) override
Definition: AquiferAnalytical.hpp:171
@ enableSaltPrecipitation
Definition: AquiferAnalytical.hpp:71
virtual void calculateAquiferCondition()=0
Scalar totalFaceArea() const override
Definition: AquiferAnalytical.hpp:125
@ enableEnergy
Definition: AquiferAnalytical.hpp:66
GetPropType< TypeTag, Properties::IntensiveQuantities > IntensiveQuantities
Definition: AquiferAnalytical.hpp:62
void computeFaceAreaFraction(const std::vector< Scalar > &total_face_area) override
Definition: AquiferAnalytical.hpp:102
int compIdx_() const
Definition: AquiferAnalytical.hpp:246
bool solution_set_from_restart_
Definition: AquiferAnalytical.hpp:457
void initializeConnectionMappings()
Definition: AquiferAnalytical.hpp:286
virtual Scalar aquiferDepth() const =0
std::size_t size() const
Definition: AquiferAnalytical.hpp:210
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: AquiferAnalytical.hpp:58
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: AquiferAnalytical.hpp:61
void initFromRestart(const data::Aquifers &aquiferSoln) override
Definition: AquiferAnalytical.hpp:130
std::vector< Scalar > faceArea_connected_
Definition: AquiferAnalytical.hpp:437
void initialSolutionApplied() override
Definition: AquiferAnalytical.hpp:144
void beginTimeStep() override
Definition: AquiferAnalytical.hpp:149
std::vector< Scalar > alphai_
Definition: AquiferAnalytical.hpp:445
Scalar pa0_
Definition: AquiferAnalytical.hpp:448
DenseAd::Evaluation< Scalar, numEq > Eval
Definition: AquiferAnalytical.hpp:75
GetPropType< TypeTag, Properties::ElementMapper > ElementMapper
Definition: AquiferAnalytical.hpp:63
virtual void calculateInflowRate(int idx, const Simulator &simulator)=0
std::vector< Scalar > pressure_previous_
Definition: AquiferAnalytical.hpp:442
AquiferAnalytical(const int aqID, const std::vector< Aquancon::AquancCell > &connections, const Simulator &simulator)
Definition: AquiferAnalytical.hpp:89
void initQuantities()
Definition: AquiferAnalytical.hpp:254
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: AquiferAnalytical.hpp:57
Eval W_flux_
Definition: AquiferAnalytical.hpp:455
virtual void calculateAquiferConstants()=0
@ has_disgas_in_water
Definition: AquiferAnalytical.hpp:69
std::vector< Eval > pressure_current_
Definition: AquiferAnalytical.hpp:443
static constexpr int numEq
Definition: AquiferAnalytical.hpp:73
@ enableBrine
Definition: AquiferAnalytical.hpp:67
std::optional< Scalar > Ta0_
Definition: AquiferAnalytical.hpp:449
std::vector< Scalar > cell_depth_
Definition: AquiferAnalytical.hpp:441
@ enableTemperature
Definition: AquiferAnalytical.hpp:65
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: AquiferAnalytical.hpp:59
void serializeOp(Serializer &serializer)
Definition: AquiferAnalytical.hpp:216
bool has_active_connection_on_proc_
Definition: AquiferAnalytical.hpp:458
const std::vector< Aquancon::AquancCell > connections_
Definition: AquiferAnalytical.hpp:434
std::vector< Eval > Qai_
Definition: AquiferAnalytical.hpp:444
Scalar calculateReservoirEquilibrium()
Definition: AquiferAnalytical.hpp:392
virtual ~AquiferAnalytical()
Definition: AquiferAnalytical.hpp:99
virtual void assignRestartData(const data::AquiferData &xaq)=0
void initializeConnectionDepths()
Definition: AquiferAnalytical.hpp:367
BlackOilFluidState< Eval, FluidSystem, enableTemperature, enableEnergy, BlackoilIndices::gasEnabled, enableVapwat, enableBrine, enableSaltPrecipitation, has_disgas_in_water, BlackoilIndices::numPhases > FluidState
Definition: AquiferAnalytical.hpp:86
void updateCellPressure(std::vector< Eval > &pressure_water, const int idx, const IntensiveQuantities &intQuants)
Definition: AquiferAnalytical.hpp:270
GetPropType< TypeTag, Properties::Indices > BlackoilIndices
Definition: AquiferAnalytical.hpp:60
bool operator==(const AquiferAnalytical &rhs) const
Definition: AquiferAnalytical.hpp:225
Scalar rhow_
Definition: AquiferAnalytical.hpp:450
Definition: AquiferInterface.hpp:35
const Simulator & simulator_
Definition: AquiferInterface.hpp:98
int phaseIdx_() const
Definition: AquiferInterface.hpp:88
bool co2store_or_h2store_() const
Definition: AquiferInterface.hpp:82
int aquiferID() const
Definition: AquiferInterface.hpp:79
Definition: BlackoilPhases.hpp:27