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