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