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 <dune/grid/common/partitionset.hh>
26
27#include <opm/common/ErrorMacros.hpp>
28
29#include <opm/input/eclipse/EclipseState/Aquifer/Aquancon.hpp>
30
31#include <opm/material/common/MathToolbox.hpp>
32#include <opm/material/densead/Evaluation.hpp>
33#include <opm/material/densead/Math.hpp>
34#include <opm/material/fluidstates/BlackOilFluidState.hpp>
35
38
39#include <opm/output/data/Aquifer.hpp>
40
43
44#include <algorithm>
45#include <cmath>
46#include <cstddef>
47#include <limits>
48#include <numeric>
49#include <optional>
50#include <vector>
51
52namespace Opm
53{
54template <typename TypeTag>
55class AquiferAnalytical : public AquiferInterface<TypeTag>
56{
57public:
66
67 static constexpr EnergyModules energyModuleType = getPropValue<TypeTag, Properties::EnergyModuleType>();
68 static constexpr bool enableBrine = getPropValue<TypeTag, Properties::EnableBrine>();
69 enum { enableVapwat = getPropValue<TypeTag, Properties::EnableVapwat>() };
70 enum { has_disgas_in_water = getPropValue<TypeTag, Properties::EnableDisgasInWater>() };
71 static constexpr bool enableSolvent = getPropValue<TypeTag, Properties::EnableSolvent>();
72
73 enum { enableSaltPrecipitation = getPropValue<TypeTag, Properties::EnableSaltPrecipitation>() };
74
76
77 static constexpr int numEq = BlackoilIndices::numEq;
78
79 using FluidState = BlackOilFluidState<Eval,
81 energyModuleType != EnergyModules::NoTemperature,
82 energyModuleType == EnergyModules::FullyImplicitThermal,
83 BlackoilIndices::gasEnabled,
89 BlackoilIndices::numPhases>;
90
91 // Constructor
92 AquiferAnalytical(const int aqID,
93 const std::vector<Aquancon::AquancCell>& connections,
94 const Simulator& simulator)
95 : AquiferInterface<TypeTag>(aqID, simulator)
96 , connections_(connections)
97 {
99 }
100
101 void computeFaceAreaFraction(const std::vector<Scalar>& total_face_area) override
102 {
103 assert (total_face_area.size() >= static_cast<typename std::vector<Scalar>::size_type>(this->aquiferID()));
104
105 const auto tfa = total_face_area[this->aquiferID() - 1];
106 const auto eps_sqrt = std::sqrt(std::numeric_limits<Scalar>::epsilon());
107
108 if (tfa < eps_sqrt) {
109 this->alphai_.assign(this->size(), Scalar{0});
110 }
111 else {
112 std::ranges::transform(this->faceArea_connected_, this->alphai_.begin(),
113 [tfa](const Scalar area)
114 { return area / tfa; });
115 }
116
117 this->area_fraction_ = this->totalFaceArea() / tfa;
118 }
119
120 Scalar totalFaceArea() const override
121 {
122 return this->total_face_area_;
123 }
124
125 void initFromRestart(const data::Aquifers& aquiferSoln) override
126 {
127 auto xaqPos = aquiferSoln.find(this->aquiferID());
128 if (xaqPos == aquiferSoln.end())
129 return;
130
131 this->assignRestartData(xaqPos->second);
132
133 this->W_flux_ = xaqPos->second.volume * this->area_fraction_;
134 this->pa0_ = xaqPos->second.initPressure;
135
136 this->solution_set_from_restart_ = true;
137 }
138
140 {
142 }
143
144 void beginTimeStep() override
145 {
146 ElementContext elemCtx(this->simulator_);
148
149 for (const auto& elem : elements(this->simulator_.gridView())) {
150 elemCtx.updatePrimaryStencil(elem);
151
152 const int cellIdx = elemCtx.globalSpaceIndex(0, 0);
153 const int idx = cellToConnectionIdx_[cellIdx];
154 if (idx < 0)
155 continue;
156
157 elemCtx.updateIntensiveQuantities(0);
158 const auto& iq = elemCtx.intensiveQuantities(0, 0);
159 pressure_previous_[idx] = getValue(iq.fluidState().pressure(this->phaseIdx_()));
160 }
161
162 OPM_END_PARALLEL_TRY_CATCH("AquiferAnalytical::beginTimeStep() failed: ",
163 this->simulator_.vanguard().grid().comm());
164 }
165
166 // Tracer assembly only: scalar readout after the flow step. Never call
167 // calculateInflowRate() here (mutates Carter–Tracy state after endTimeStep).
168 Scalar cachedConnectionInfluxRate(unsigned cellIdx) const override
169 {
170 const int idx = this->cellToConnectionIdx_[cellIdx];
171 if (idx < 0) {
172 return Scalar{0};
173 }
174
175 return getValue(this->Qai_[idx]);
176 }
177
179 const unsigned cellIdx,
180 const unsigned timeIdx) override
181 {
182 const auto& model = this->simulator_.model();
183
184 const int idx = this->cellToConnectionIdx_[cellIdx];
185 if (idx < 0)
186 return;
187
188 const auto& intQuants = model.intensiveQuantities(cellIdx, timeIdx);
189
190 // This is the pressure at td + dt
191 this->updateCellPressure(this->pressure_current_, idx, intQuants);
192 this->calculateInflowRate(idx, this->simulator_);
193
194 // Qai_[idx] is Evaluation: must not pass through getValue() or Newton loses
195 // pressure derivatives and the aquifer source becomes explicit.
196 rates[BlackoilIndices::conti0EqIdx + compIdx_()]
197 += this->Qai_[idx] / model.dofTotalVolume(cellIdx);
198
199 if constexpr (energyModuleType == EnergyModules::FullyImplicitThermal) {
200 auto fs = intQuants.fluidState();
201 if (this->Ta0_.has_value() && this->Qai_[idx] > 0)
202 {
203 fs.setTemperature(this->Ta0_.value());
204 typedef typename std::decay<decltype(fs)>::type::ValueType FsValueType;
205 typename FluidSystem::template ParameterCache<FsValueType> paramCache;
206 const unsigned pvtRegionIdx = intQuants.pvtRegionIndex();
207 paramCache.setRegionIndex(pvtRegionIdx);
208 paramCache.updatePhase(fs, this->phaseIdx_());
209 const auto& h = FluidSystem::enthalpy(fs, paramCache, this->phaseIdx_());
210 fs.setEnthalpy(this->phaseIdx_(), h);
211 }
212 rates[BlackoilIndices::contiEnergyEqIdx]
213 += this->Qai_[idx] * fs.enthalpy(this->phaseIdx_()) * FluidSystem::referenceDensity(this->phaseIdx_(), intQuants.pvtRegionIndex()) / model.dofTotalVolume(cellIdx);
214
215 }
216 }
217
218 std::size_t size() const
219 {
220 return this->connections_.size();
221 }
222
223 template<class Serializer>
224 void serializeOp(Serializer& serializer)
225 {
226 serializer(pressure_previous_);
227 serializer(pressure_current_);
228 serializer(Qai_);
229 serializer(rhow_);
230 serializer(W_flux_);
231 }
232
233 bool operator==(const AquiferAnalytical& rhs) const
234 {
235 return this->pressure_previous_ == rhs.pressure_previous_ &&
237 this->Qai_ == rhs.Qai_ &&
238 this->rhow_ == rhs.rhow_ &&
239 this->W_flux_ == rhs.W_flux_;
240 }
241
242protected:
243 virtual void assignRestartData(const data::AquiferData& xaq) = 0;
244 virtual void calculateInflowRate(int idx, const Simulator& simulator) = 0;
245 virtual void calculateAquiferCondition() = 0;
246 virtual void calculateAquiferConstants() = 0;
247 virtual Scalar aquiferDepth() const = 0;
248
250 {
251 return this->simulator_.problem().gravity()[2];
252 }
253
254 int compIdx_() const
255 {
256 if (this->co2store_or_h2store_())
257 return FluidSystem::oilCompIdx;
258
259 return FluidSystem::waterCompIdx;
260 }
261
263 {
264 // We reset the cumulative flux at the start of any simulation, so, W_flux = 0
265 if (! this->solution_set_from_restart_) {
266 W_flux_ = Scalar{0};
267 }
268
272
273 this->pressure_previous_.resize(this->size(), Scalar{0});
274 this->pressure_current_.resize(this->size(), Scalar{0});
275 this->Qai_.resize(this->size(), Scalar{0});
276 }
277
278 void updateCellPressure(std::vector<Eval>& 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_());
284 }
285
286 void updateCellPressure(std::vector<Scalar>& pressure_water,
287 const int idx,
288 const IntensiveQuantities& intQuants)
289 {
290 const auto& fs = intQuants.fluidState();
291 pressure_water.at(idx) = fs.pressure(this->phaseIdx_()).value();
292 }
293
295 {
296 this->alphai_.resize(this->size(), 1.0);
297 this->faceArea_connected_.resize(this->size(), Scalar{0});
298
299 // total_face_area_ is the sum of the areas connected to an aquifer
300 this->total_face_area_ = Scalar{0};
301 this->cellToConnectionIdx_.resize(this->simulator_.gridView().size(/*codim=*/0), -1);
302 const auto& gridView = this->simulator_.vanguard().gridView();
303 for (std::size_t idx = 0; idx < this->size(); ++idx) {
304 const auto global_index = this->connections_[idx].global_index;
305 const int cell_index = this->simulator_.vanguard().compressedIndex(global_index);
306 if (cell_index < 0) {
307 continue;
308 }
309
310 auto elemIt = gridView.template begin</*codim=*/ 0>();
311 std::advance(elemIt, cell_index);
312
313 // The global_index is not part of this grid
314 if (elemIt->partitionType() != Dune::InteriorEntity) {
315 continue;
316 }
317
318 this->cellToConnectionIdx_[cell_index] = idx;
319 }
320
321 // Translate the C face tag into the enum used by opm-parser's TransMult class
322 FaceDir::DirEnum faceDirection;
323
324 // Get areas for all connections
325 const auto& elemMapper = this->simulator_.model().dofMapper();
326 for (const auto& elem : elements(gridView)) {
327 const unsigned cell_index = elemMapper.index(elem);
328 const int idx = this->cellToConnectionIdx_[cell_index];
329
330 // Only deal with connections given by the aquifer
331 if (idx < 0) {
332 continue;
333 }
334
335 for (const auto& intersection : intersections(gridView, elem)) {
336 // Only deal with grid boundaries
337 if (! intersection.boundary()) {
338 continue;
339 }
340
341 switch (intersection.indexInInside()) {
342 case 0:
343 faceDirection = FaceDir::XMinus;
344 break;
345 case 1:
346 faceDirection = FaceDir::XPlus;
347 break;
348 case 2:
349 faceDirection = FaceDir::YMinus;
350 break;
351 case 3:
352 faceDirection = FaceDir::YPlus;
353 break;
354 case 4:
355 faceDirection = FaceDir::ZMinus;
356 break;
357 case 5:
358 faceDirection = FaceDir::ZPlus;
359 break;
360 default:
361 OPM_THROW(std::logic_error,
362 "Internal error in initialization of aquifer.");
363 }
364
365 if (faceDirection == this->connections_[idx].face_dir) {
366 this->faceArea_connected_[idx] = this->connections_[idx].influx_coeff;
367 break;
368 }
369 }
370
371 this->total_face_area_ += this->faceArea_connected_.at(idx);
372 }
373 }
374
376 {
377 this->cell_depth_.resize(this->size(), this->aquiferDepth());
378
379 const auto& gridView = this->simulator_.vanguard().gridView();
380 for (std::size_t idx = 0; idx < this->size(); ++idx) {
381 const int cell_index = this->simulator_.vanguard()
382 .compressedIndex(this->connections_[idx].global_index);
383 if (cell_index < 0) {
384 continue;
385 }
386
387 auto elemIt = gridView.template begin</*codim=*/ 0>();
388 std::advance(elemIt, cell_index);
389
390 // The global_index is not part of this grid
391 if (elemIt->partitionType() != Dune::InteriorEntity) {
392 continue;
393 }
394
395 this->cell_depth_.at(idx) = this->simulator_.vanguard().cellCenterDepth(cell_index);
396 }
397 }
398
399 // This function is for calculating the aquifer properties from equilibrium state with the reservoir
401 {
402 // Since the global_indices are the reservoir index, we just need to extract the fluidstate at those indices
403 std::vector<Scalar> pw_aquifer;
404 Scalar water_pressure_reservoir;
405
406 ElementContext elemCtx(this->simulator_);
407 const auto& gridView = this->simulator_.gridView();
408 for (const auto& elem : elements(gridView)) {
409 elemCtx.updatePrimaryStencil(elem);
410
411 const auto cellIdx = elemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
412 const auto idx = this->cellToConnectionIdx_[cellIdx];
413 if (idx < 0)
414 continue;
415
416 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
417 const auto& iq0 = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
418 const auto& fs = iq0.fluidState();
419
420 water_pressure_reservoir = fs.pressure(this->phaseIdx_()).value();
421 const auto water_density = fs.density(this->phaseIdx_());
422
423 const auto gdz =
424 this->gravity_() * (this->cell_depth_[idx] - this->aquiferDepth());
425
426 pw_aquifer.push_back(this->alphai_[idx] *
427 (water_pressure_reservoir - water_density.value()*gdz));
428 }
429
430 // We take the average of the calculated equilibrium pressures.
431 const auto& comm = this->simulator_.vanguard().grid().comm();
432
433 Scalar vals[2];
434 vals[0] = std::accumulate(this->alphai_.begin(), this->alphai_.end(), Scalar{0});
435 vals[1] = std::accumulate(pw_aquifer.begin(), pw_aquifer.end(), Scalar{0});
436
437 comm.sum(vals, 2);
438
439 return vals[1] / vals[0];
440 }
441
442 const std::vector<Aquancon::AquancCell> connections_;
443
444 // Grid variables
445 std::vector<Scalar> faceArea_connected_;
446 std::vector<int> cellToConnectionIdx_;
447
448 // Quantities at each grid id
449 std::vector<Scalar> cell_depth_;
450 std::vector<Scalar> pressure_previous_;
451 std::vector<Eval> pressure_current_;
452 std::vector<Eval> Qai_;
453 std::vector<Scalar> alphai_;
454
455 Scalar Tc_{}; // Time constant
456 Scalar pa0_{}; // initial aquifer pressure
457 std::optional<Scalar> Ta0_{}; // initial aquifer temperature
459
462
464
467};
468
469} // namespace Opm
470
471#endif
#define OPM_END_PARALLEL_TRY_CATCH(prefix, comm)
Catch exception and throw in a parallel try-catch clause.
Definition: DeferredLoggingErrorHelpers.hpp:192
#define OPM_BEGIN_PARALLEL_TRY_CATCH()
Macro to setup the try of a parallel try-catch.
Definition: DeferredLoggingErrorHelpers.hpp:158
Defines a type tags and some fundamental properties all models.
Declares the properties required by the black oil model.
Definition: AquiferAnalytical.hpp:56
void updateCellPressure(std::vector< Scalar > &pressure_water, const int idx, const IntensiveQuantities &intQuants)
Definition: AquiferAnalytical.hpp:286
Scalar total_face_area_
Definition: AquiferAnalytical.hpp:460
Scalar area_fraction_
Definition: AquiferAnalytical.hpp:461
Scalar gravity_() const
Definition: AquiferAnalytical.hpp:249
std::vector< int > cellToConnectionIdx_
Definition: AquiferAnalytical.hpp:446
Scalar Tc_
Definition: AquiferAnalytical.hpp:455
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: AquiferAnalytical.hpp:58
void addToSource(RateVector &rates, const unsigned cellIdx, const unsigned timeIdx) override
Definition: AquiferAnalytical.hpp:178
Scalar cachedConnectionInfluxRate(unsigned cellIdx) const override
Definition: AquiferAnalytical.hpp:168
virtual void calculateAquiferCondition()=0
Scalar totalFaceArea() const override
Definition: AquiferAnalytical.hpp:120
GetPropType< TypeTag, Properties::IntensiveQuantities > IntensiveQuantities
Definition: AquiferAnalytical.hpp:64
void computeFaceAreaFraction(const std::vector< Scalar > &total_face_area) override
Definition: AquiferAnalytical.hpp:101
int compIdx_() const
Definition: AquiferAnalytical.hpp:254
bool solution_set_from_restart_
Definition: AquiferAnalytical.hpp:465
void initializeConnectionMappings()
Definition: AquiferAnalytical.hpp:294
virtual Scalar aquiferDepth() const =0
std::size_t size() const
Definition: AquiferAnalytical.hpp:218
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: AquiferAnalytical.hpp:60
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: AquiferAnalytical.hpp:63
void initFromRestart(const data::Aquifers &aquiferSoln) override
Definition: AquiferAnalytical.hpp:125
std::vector< Scalar > faceArea_connected_
Definition: AquiferAnalytical.hpp:445
void initialSolutionApplied() override
Definition: AquiferAnalytical.hpp:139
void beginTimeStep() override
Definition: AquiferAnalytical.hpp:144
std::vector< Scalar > alphai_
Definition: AquiferAnalytical.hpp:453
Scalar pa0_
Definition: AquiferAnalytical.hpp:456
GetPropType< TypeTag, Properties::ElementMapper > ElementMapper
Definition: AquiferAnalytical.hpp:65
virtual void calculateInflowRate(int idx, const Simulator &simulator)=0
static constexpr bool enableBrine
Definition: AquiferAnalytical.hpp:68
std::vector< Scalar > pressure_previous_
Definition: AquiferAnalytical.hpp:450
AquiferAnalytical(const int aqID, const std::vector< Aquancon::AquancCell > &connections, const Simulator &simulator)
Definition: AquiferAnalytical.hpp:92
void initQuantities()
Definition: AquiferAnalytical.hpp:262
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: AquiferAnalytical.hpp:59
Eval W_flux_
Definition: AquiferAnalytical.hpp:463
virtual void calculateAquiferConstants()=0
std::vector< Eval > pressure_current_
Definition: AquiferAnalytical.hpp:451
static constexpr int numEq
Definition: AquiferAnalytical.hpp:77
@ has_disgas_in_water
Definition: AquiferAnalytical.hpp:70
BlackOilFluidState< Eval, FluidSystem, energyModuleType !=EnergyModules::NoTemperature, energyModuleType==EnergyModules::FullyImplicitThermal, BlackoilIndices::gasEnabled, enableVapwat, enableBrine, enableSaltPrecipitation, has_disgas_in_water, enableSolvent, BlackoilIndices::numPhases > FluidState
Definition: AquiferAnalytical.hpp:89
std::optional< Scalar > Ta0_
Definition: AquiferAnalytical.hpp:457
std::vector< Scalar > cell_depth_
Definition: AquiferAnalytical.hpp:449
@ enableSaltPrecipitation
Definition: AquiferAnalytical.hpp:73
static constexpr EnergyModules energyModuleType
Definition: AquiferAnalytical.hpp:67
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: AquiferAnalytical.hpp:61
void serializeOp(Serializer &serializer)
Definition: AquiferAnalytical.hpp:224
bool has_active_connection_on_proc_
Definition: AquiferAnalytical.hpp:466
GetPropType< TypeTag, Properties::Evaluation > Eval
Definition: AquiferAnalytical.hpp:75
const std::vector< Aquancon::AquancCell > connections_
Definition: AquiferAnalytical.hpp:442
std::vector< Eval > Qai_
Definition: AquiferAnalytical.hpp:452
Scalar calculateReservoirEquilibrium()
Definition: AquiferAnalytical.hpp:400
virtual void assignRestartData(const data::AquiferData &xaq)=0
static constexpr bool enableSolvent
Definition: AquiferAnalytical.hpp:71
void initializeConnectionDepths()
Definition: AquiferAnalytical.hpp:375
@ enableVapwat
Definition: AquiferAnalytical.hpp:69
void updateCellPressure(std::vector< Eval > &pressure_water, const int idx, const IntensiveQuantities &intQuants)
Definition: AquiferAnalytical.hpp:278
GetPropType< TypeTag, Properties::Indices > BlackoilIndices
Definition: AquiferAnalytical.hpp:62
bool operator==(const AquiferAnalytical &rhs) const
Definition: AquiferAnalytical.hpp:233
Scalar rhow_
Definition: AquiferAnalytical.hpp:458
Definition: AquiferInterface.hpp:35
const Simulator & simulator_
Definition: AquiferInterface.hpp:105
int phaseIdx_() const
Definition: AquiferInterface.hpp:95
bool co2store_or_h2store_() const
Definition: AquiferInterface.hpp:89
int aquiferID() const
Definition: AquiferInterface.hpp:86
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