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 enum { enableEnergy = getPropValue<TypeTag, Properties::EnergyModuleType>() == EnergyModules::FullyImplicitThermal };
69 enum { enableTemperature = getPropValue<TypeTag, Properties::EnergyModuleType>() == EnergyModules::ConstantTemperature };
70
71 enum { enableBrine = getPropValue<TypeTag, Properties::EnableBrine>() };
72 enum { enableVapwat = getPropValue<TypeTag, Properties::EnableVapwat>() };
73 enum { has_disgas_in_water = getPropValue<TypeTag, Properties::EnableDisgasInWater>() };
74
75 enum { enableSaltPrecipitation = getPropValue<TypeTag, Properties::EnableSaltPrecipitation>() };
76
77 static constexpr int numEq = BlackoilIndices::numEq;
78
79 using Eval = DenseAd::Evaluation<Scalar, /*size=*/numEq>;
80
81 using FluidState = BlackOilFluidState<Eval,
85 BlackoilIndices::gasEnabled,
90 BlackoilIndices::numPhases>;
91
92 // Constructor
93 AquiferAnalytical(const int aqID,
94 const std::vector<Aquancon::AquancCell>& connections,
95 const Simulator& simulator)
96 : AquiferInterface<TypeTag>(aqID, simulator)
97 , connections_(connections)
98 {
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.updatePhase(fs, this->phaseIdx_());
200 const auto& h = FluidSystem::enthalpy(fs, paramCache, this->phaseIdx_());
201 fs.setEnthalpy(this->phaseIdx_(), h);
202 }
203 rates[BlackoilIndices::contiEnergyEqIdx]
204 += this->Qai_[idx] *fs.enthalpy(this->phaseIdx_()) * FluidSystem::referenceDensity( this->phaseIdx_(), intQuants.pvtRegionIndex()) / model.dofTotalVolume(cellIdx);
205
206 }
207 }
208
209 std::size_t size() const
210 {
211 return this->connections_.size();
212 }
213
214 template<class Serializer>
215 void serializeOp(Serializer& serializer)
216 {
217 serializer(pressure_previous_);
218 serializer(pressure_current_);
219 serializer(Qai_);
220 serializer(rhow_);
221 serializer(W_flux_);
222 }
223
224 bool operator==(const AquiferAnalytical& rhs) const
225 {
226 return this->pressure_previous_ == rhs.pressure_previous_ &&
228 this->Qai_ == rhs.Qai_ &&
229 this->rhow_ == rhs.rhow_ &&
230 this->W_flux_ == rhs.W_flux_;
231 }
232
233protected:
234 virtual void assignRestartData(const data::AquiferData& xaq) = 0;
235 virtual void calculateInflowRate(int idx, const Simulator& simulator) = 0;
236 virtual void calculateAquiferCondition() = 0;
237 virtual void calculateAquiferConstants() = 0;
238 virtual Scalar aquiferDepth() const = 0;
239
241 {
242 return this->simulator_.problem().gravity()[2];
243 }
244
245 int compIdx_() const
246 {
247 if (this->co2store_or_h2store_())
248 return FluidSystem::oilCompIdx;
249
250 return FluidSystem::waterCompIdx;
251 }
252
254 {
255 // We reset the cumulative flux at the start of any simulation, so, W_flux = 0
256 if (! this->solution_set_from_restart_) {
257 W_flux_ = Scalar{0};
258 }
259
263
264 this->pressure_previous_.resize(this->size(), Scalar{0});
265 this->pressure_current_.resize(this->size(), Scalar{0});
266 this->Qai_.resize(this->size(), Scalar{0});
267 }
268
269 void updateCellPressure(std::vector<Eval>& pressure_water,
270 const int idx,
271 const IntensiveQuantities& intQuants)
272 {
273 const auto& fs = intQuants.fluidState();
274 pressure_water.at(idx) = fs.pressure(this->phaseIdx_());
275 }
276
277 void updateCellPressure(std::vector<Scalar>& pressure_water,
278 const int idx,
279 const IntensiveQuantities& intQuants)
280 {
281 const auto& fs = intQuants.fluidState();
282 pressure_water.at(idx) = fs.pressure(this->phaseIdx_()).value();
283 }
284
286 {
287 this->alphai_.resize(this->size(), 1.0);
288 this->faceArea_connected_.resize(this->size(), Scalar{0});
289
290 // total_face_area_ is the sum of the areas connected to an aquifer
291 this->total_face_area_ = Scalar{0};
292 this->cellToConnectionIdx_.resize(this->simulator_.gridView().size(/*codim=*/0), -1);
293 const auto& gridView = this->simulator_.vanguard().gridView();
294 for (std::size_t idx = 0; idx < this->size(); ++idx) {
295 const auto global_index = this->connections_[idx].global_index;
296 const int cell_index = this->simulator_.vanguard().compressedIndex(global_index);
297 if (cell_index < 0) {
298 continue;
299 }
300
301 auto elemIt = gridView.template begin</*codim=*/ 0>();
302 std::advance(elemIt, cell_index);
303
304 // The global_index is not part of this grid
305 if (elemIt->partitionType() != Dune::InteriorEntity) {
306 continue;
307 }
308
309 this->cellToConnectionIdx_[cell_index] = idx;
310 }
311
312 // Translate the C face tag into the enum used by opm-parser's TransMult class
313 FaceDir::DirEnum faceDirection;
314
315 // Get areas for all connections
316 const auto& elemMapper = this->simulator_.model().dofMapper();
317 for (const auto& elem : elements(gridView)) {
318 const unsigned cell_index = elemMapper.index(elem);
319 const int idx = this->cellToConnectionIdx_[cell_index];
320
321 // Only deal with connections given by the aquifer
322 if (idx < 0) {
323 continue;
324 }
325
326 for (const auto& intersection : intersections(gridView, elem)) {
327 // Only deal with grid boundaries
328 if (! intersection.boundary()) {
329 continue;
330 }
331
332 switch (intersection.indexInInside()) {
333 case 0:
334 faceDirection = FaceDir::XMinus;
335 break;
336 case 1:
337 faceDirection = FaceDir::XPlus;
338 break;
339 case 2:
340 faceDirection = FaceDir::YMinus;
341 break;
342 case 3:
343 faceDirection = FaceDir::YPlus;
344 break;
345 case 4:
346 faceDirection = FaceDir::ZMinus;
347 break;
348 case 5:
349 faceDirection = FaceDir::ZPlus;
350 break;
351 default:
352 OPM_THROW(std::logic_error,
353 "Internal error in initialization of aquifer.");
354 }
355
356 if (faceDirection == this->connections_[idx].face_dir) {
357 this->faceArea_connected_[idx] = this->connections_[idx].influx_coeff;
358 break;
359 }
360 }
361
362 this->total_face_area_ += this->faceArea_connected_.at(idx);
363 }
364 }
365
367 {
368 this->cell_depth_.resize(this->size(), this->aquiferDepth());
369
370 const auto& gridView = this->simulator_.vanguard().gridView();
371 for (std::size_t idx = 0; idx < this->size(); ++idx) {
372 const int cell_index = this->simulator_.vanguard()
373 .compressedIndex(this->connections_[idx].global_index);
374 if (cell_index < 0) {
375 continue;
376 }
377
378 auto elemIt = gridView.template begin</*codim=*/ 0>();
379 std::advance(elemIt, cell_index);
380
381 // The global_index is not part of this grid
382 if (elemIt->partitionType() != Dune::InteriorEntity) {
383 continue;
384 }
385
386 this->cell_depth_.at(idx) = this->simulator_.vanguard().cellCenterDepth(cell_index);
387 }
388 }
389
390 // This function is for calculating the aquifer properties from equilibrium state with the reservoir
392 {
393 // Since the global_indices are the reservoir index, we just need to extract the fluidstate at those indices
394 std::vector<Scalar> pw_aquifer;
395 Scalar water_pressure_reservoir;
396
397 ElementContext elemCtx(this->simulator_);
398 const auto& gridView = this->simulator_.gridView();
399 for (const auto& elem : elements(gridView)) {
400 elemCtx.updatePrimaryStencil(elem);
401
402 const auto cellIdx = elemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
403 const auto idx = this->cellToConnectionIdx_[cellIdx];
404 if (idx < 0)
405 continue;
406
407 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
408 const auto& iq0 = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
409 const auto& fs = iq0.fluidState();
410
411 water_pressure_reservoir = fs.pressure(this->phaseIdx_()).value();
412 const auto water_density = fs.density(this->phaseIdx_());
413
414 const auto gdz =
415 this->gravity_() * (this->cell_depth_[idx] - this->aquiferDepth());
416
417 pw_aquifer.push_back(this->alphai_[idx] *
418 (water_pressure_reservoir - water_density.value()*gdz));
419 }
420
421 // We take the average of the calculated equilibrium pressures.
422 const auto& comm = this->simulator_.vanguard().grid().comm();
423
424 Scalar vals[2];
425 vals[0] = std::accumulate(this->alphai_.begin(), this->alphai_.end(), Scalar{0});
426 vals[1] = std::accumulate(pw_aquifer.begin(), pw_aquifer.end(), Scalar{0});
427
428 comm.sum(vals, 2);
429
430 return vals[1] / vals[0];
431 }
432
433 const std::vector<Aquancon::AquancCell> connections_;
434
435 // Grid variables
436 std::vector<Scalar> faceArea_connected_;
437 std::vector<int> cellToConnectionIdx_;
438
439 // Quantities at each grid id
440 std::vector<Scalar> cell_depth_;
441 std::vector<Scalar> pressure_previous_;
442 std::vector<Eval> pressure_current_;
443 std::vector<Eval> Qai_;
444 std::vector<Scalar> alphai_;
445
446 Scalar Tc_{}; // Time constant
447 Scalar pa0_{}; // initial aquifer pressure
448 std::optional<Scalar> Ta0_{}; // initial aquifer temperature
450
453
455
458};
459
460} // namespace Opm
461
462#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:277
Scalar total_face_area_
Definition: AquiferAnalytical.hpp:451
Scalar area_fraction_
Definition: AquiferAnalytical.hpp:452
Scalar gravity_() const
Definition: AquiferAnalytical.hpp:240
std::vector< int > cellToConnectionIdx_
Definition: AquiferAnalytical.hpp:437
Scalar Tc_
Definition: AquiferAnalytical.hpp:446
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: AquiferAnalytical.hpp:59
void addToSource(RateVector &rates, const unsigned cellIdx, const unsigned timeIdx) override
Definition: AquiferAnalytical.hpp:171
@ enableBrine
Definition: AquiferAnalytical.hpp:71
@ enableVapwat
Definition: AquiferAnalytical.hpp:72
virtual void calculateAquiferCondition()=0
Scalar totalFaceArea() const override
Definition: AquiferAnalytical.hpp:125
@ has_disgas_in_water
Definition: AquiferAnalytical.hpp:73
GetPropType< TypeTag, Properties::IntensiveQuantities > IntensiveQuantities
Definition: AquiferAnalytical.hpp:65
void computeFaceAreaFraction(const std::vector< Scalar > &total_face_area) override
Definition: AquiferAnalytical.hpp:102
int compIdx_() const
Definition: AquiferAnalytical.hpp:245
bool solution_set_from_restart_
Definition: AquiferAnalytical.hpp:456
void initializeConnectionMappings()
Definition: AquiferAnalytical.hpp:285
virtual Scalar aquiferDepth() const =0
std::size_t size() const
Definition: AquiferAnalytical.hpp:209
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:130
std::vector< Scalar > faceArea_connected_
Definition: AquiferAnalytical.hpp:436
void initialSolutionApplied() override
Definition: AquiferAnalytical.hpp:144
void beginTimeStep() override
Definition: AquiferAnalytical.hpp:149
std::vector< Scalar > alphai_
Definition: AquiferAnalytical.hpp:444
Scalar pa0_
Definition: AquiferAnalytical.hpp:447
DenseAd::Evaluation< Scalar, numEq > Eval
Definition: AquiferAnalytical.hpp:79
GetPropType< TypeTag, Properties::ElementMapper > ElementMapper
Definition: AquiferAnalytical.hpp:66
virtual void calculateInflowRate(int idx, const Simulator &simulator)=0
@ enableEnergy
Definition: AquiferAnalytical.hpp:68
std::vector< Scalar > pressure_previous_
Definition: AquiferAnalytical.hpp:441
AquiferAnalytical(const int aqID, const std::vector< Aquancon::AquancCell > &connections, const Simulator &simulator)
Definition: AquiferAnalytical.hpp:93
void initQuantities()
Definition: AquiferAnalytical.hpp:253
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: AquiferAnalytical.hpp:60
Eval W_flux_
Definition: AquiferAnalytical.hpp:454
virtual void calculateAquiferConstants()=0
std::vector< Eval > pressure_current_
Definition: AquiferAnalytical.hpp:442
static constexpr int numEq
Definition: AquiferAnalytical.hpp:77
std::optional< Scalar > Ta0_
Definition: AquiferAnalytical.hpp:448
std::vector< Scalar > cell_depth_
Definition: AquiferAnalytical.hpp:440
@ enableTemperature
Definition: AquiferAnalytical.hpp:69
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: AquiferAnalytical.hpp:62
void serializeOp(Serializer &serializer)
Definition: AquiferAnalytical.hpp:215
bool has_active_connection_on_proc_
Definition: AquiferAnalytical.hpp:457
@ enableSaltPrecipitation
Definition: AquiferAnalytical.hpp:75
const std::vector< Aquancon::AquancCell > connections_
Definition: AquiferAnalytical.hpp:433
std::vector< Eval > Qai_
Definition: AquiferAnalytical.hpp:443
Scalar calculateReservoirEquilibrium()
Definition: AquiferAnalytical.hpp:391
virtual void assignRestartData(const data::AquiferData &xaq)=0
void initializeConnectionDepths()
Definition: AquiferAnalytical.hpp:366
BlackOilFluidState< Eval, FluidSystem, enableTemperature, enableEnergy, BlackoilIndices::gasEnabled, enableVapwat, enableBrine, enableSaltPrecipitation, has_disgas_in_water, BlackoilIndices::numPhases > FluidState
Definition: AquiferAnalytical.hpp:90
void updateCellPressure(std::vector< Eval > &pressure_water, const int idx, const IntensiveQuantities &intQuants)
Definition: AquiferAnalytical.hpp:269
GetPropType< TypeTag, Properties::Indices > BlackoilIndices
Definition: AquiferAnalytical.hpp:63
bool operator==(const AquiferAnalytical &rhs) const
Definition: AquiferAnalytical.hpp:224
Scalar rhow_
Definition: AquiferAnalytical.hpp:449
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:43
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