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