AquiferNumerical.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 Equinor ASA
3 Copyright (C) 2020 SINTEF Digital
4
5 This file is part of the Open Porous Media project (OPM).
6
7 OPM is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 OPM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with OPM. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#ifndef OPM_AQUIFERNUMERICAL_HEADER_INCLUDED
22#define OPM_AQUIFERNUMERICAL_HEADER_INCLUDED
23
24#include <dune/grid/common/partitionset.hh>
25
26#include <opm/input/eclipse/EclipseState/Aquifer/NumericalAquifer/SingleNumericalAquifer.hpp>
27
28#include <opm/material/common/MathToolbox.hpp>
29#include <opm/material/densead/Evaluation.hpp>
30
31#include <opm/output/data/Aquifer.hpp>
32
35
36#include <algorithm>
37#include <cassert>
38#include <cstddef>
39#include <vector>
40
41namespace Opm
42{
43template <typename TypeTag>
44class AquiferNumerical : public AquiferInterface<TypeTag>
45{
46public:
56
57 enum { dimWorld = GridView::dimensionworld };
58 enum { numPhases = FluidSystem::numPhases };
59 static constexpr int numEq = BlackoilIndices::numEq;
60
61 using Eval = DenseAd::Evaluation<Scalar, numEq>;
62 using Toolbox = MathToolbox<Eval>;
63
65
66 // Constructor
67 AquiferNumerical(const SingleNumericalAquifer& aquifer,
68 const Simulator& simulator)
69 : AquiferInterface<TypeTag>(aquifer.id(), simulator)
70 , flux_rate_ (0.0)
71 , cumulative_flux_(0.0)
72 , init_pressure_ (aquifer.numCells(), 0.0)
73 {
74 this->cell_to_aquifer_cell_idx_.resize(this->simulator_.gridView().size(/*codim=*/0), -1);
75
76 auto aquifer_on_process = false;
77 for (std::size_t idx = 0; idx < aquifer.numCells(); ++idx) {
78 const auto* cell = aquifer.getCellPrt(idx);
79
80 // Due to parallelisation, the cell might not exist in the current process
81 const int compressed_idx = simulator.vanguard().compressedIndexForInterior(cell->global_index);
82 if (compressed_idx >= 0) {
83 this->cell_to_aquifer_cell_idx_[compressed_idx] = idx;
84 aquifer_on_process = true;
85 }
86 }
87
88 if (aquifer_on_process) {
89 this->checkConnectsToReservoir();
90 }
91 }
92
94 {
95 AquiferNumerical result({}, simulator);
96 result.flux_rate_ = 1.0;
97 result.cumulative_flux_ = 2.0;
98 result.init_pressure_ = {3.0, 4.0};
99 result.pressure_ = 5.0;
100
101 return result;
102 }
103
104 void initFromRestart(const data::Aquifers& aquiferSoln) override
105 {
106 auto xaqPos = aquiferSoln.find(this->aquiferID());
107 if (xaqPos == aquiferSoln.end())
108 return;
109
110 if (this->connects_to_reservoir_) {
111 this->cumulative_flux_ = xaqPos->second.volume;
112 }
113
114 if (const auto* aqData = xaqPos->second.typeData.template get<data::AquiferType::Numerical>();
115 aqData != nullptr)
116 {
117 this->init_pressure_.resize(aqData->initPressure.size());
118 std::copy(aqData->initPressure.begin(),
119 aqData->initPressure.end(),
120 this->init_pressure_.begin());
121 }
122
123 this->solution_set_from_restart_ = true;
124 }
125
126 void beginTimeStep() override {}
127 void addToSource(RateVector&, const unsigned, const unsigned) override {}
128
129 void endTimeStep() override
130 {
131 this->pressure_ = this->calculateAquiferPressure();
132 this->flux_rate_ = this->calculateAquiferFluxRate();
133 this->cumulative_flux_ += this->flux_rate_ * this->simulator_.timeStepSize();
134 }
135
136 data::AquiferData aquiferData() const override
137 {
138 data::AquiferData data;
139 data.aquiferID = this->aquiferID();
140 data.pressure = this->pressure_;
141 data.fluxRate = this->flux_rate_;
142 data.volume = this->cumulative_flux_;
143
144 auto* aquNum = data.typeData.template create<data::AquiferType::Numerical>();
145 aquNum->initPressure.resize(this->init_pressure_.size());
146 std::copy(this->init_pressure_.begin(),
147 this->init_pressure_.end(),
148 aquNum->initPressure.begin());
149
150 return data;
151 }
152
154 {
155 if (this->solution_set_from_restart_) {
156 return;
157 }
158
159 this->pressure_ = this->calculateAquiferPressure(this->init_pressure_);
160 this->flux_rate_ = 0.;
161 this->cumulative_flux_ = 0.;
162 }
163
164 void computeFaceAreaFraction(const std::vector<Scalar>& /*total_face_area*/) override
165 {}
166
167 Scalar totalFaceArea() const override
168 {
169 return 1.0;
170 }
171
172 template<class Serializer>
173 void serializeOp(Serializer& serializer)
174 {
175 serializer(flux_rate_);
176 serializer(cumulative_flux_);
177 serializer(init_pressure_);
178 serializer(pressure_);
179 }
180
181 bool operator==(const AquiferNumerical& rhs) const
182 {
183 return this->flux_rate_ == rhs.flux_rate_ &&
184 this->cumulative_flux_ == rhs.cumulative_flux_ &&
185 this->init_pressure_ == rhs.init_pressure_ &&
186 this->pressure_ == rhs.pressure_;
187 }
188
190 {
191 return this->cumulative_flux_;
192 }
193
194private:
195 void checkConnectsToReservoir()
196 {
197 ElementContext elem_ctx(this->simulator_);
198 auto elemIt = std::find_if(this->simulator_.gridView().template begin</*codim=*/0>(),
199 this->simulator_.gridView().template end</*codim=*/0>(),
200 [&elem_ctx, this](const auto& elem) -> bool
201 {
202 elem_ctx.updateStencil(elem);
203
204 const auto cell_index = elem_ctx
205 .globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
206
207 return this->cell_to_aquifer_cell_idx_[cell_index] == 0;
208 });
209
210 assert ((elemIt != this->simulator_.gridView().template end</*codim=*/0>())
211 && "Internal error locating numerical aquifer's connecting cell");
212
213 this->connects_to_reservoir_ =
214 elemIt->partitionType() == Dune::InteriorEntity;
215 }
216
217 Scalar calculateAquiferPressure() const
218 {
219 auto capture = std::vector<Scalar>(this->init_pressure_.size(), 0.0);
220 return this->calculateAquiferPressure(capture);
221 }
222
223 Scalar calculateAquiferPressure(std::vector<Scalar>& cell_pressure) const
224 {
225 Scalar sum_pressure_watervolume = 0.;
226 Scalar sum_watervolume = 0.;
227
228 ElementContext elem_ctx(this->simulator_);
229 const auto& gridView = this->simulator_.gridView();
231
232 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
233 elem_ctx.updatePrimaryStencil(elem);
234
235 const std::size_t cell_index = elem_ctx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
236 const int idx = this->cell_to_aquifer_cell_idx_[cell_index];
237 if (idx < 0) {
238 continue;
239 }
240
241 elem_ctx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
242 const auto& iq0 = elem_ctx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
243 const auto& fs = iq0.fluidState();
244
245 // TODO: the porosity of the cells are still wrong for numerical aquifer cells
246 // Because the dofVolume still based on the grid information.
247 // The pore volume is correct. Extra efforts will be done to get sensible porosity value here later.
248 const Scalar water_saturation = fs.saturation(this->phaseIdx_()).value();
249 const Scalar porosity = iq0.porosity().value();
250 const Scalar volume = elem_ctx.dofTotalVolume(0, 0);
251 // TODO: not sure we should use water pressure here
252 const Scalar water_pressure_reservoir = fs.pressure(this->phaseIdx_()).value();
253 const Scalar water_volume = volume * porosity * water_saturation;
254 sum_pressure_watervolume += water_volume * water_pressure_reservoir;
255 sum_watervolume += water_volume;
256
257 cell_pressure[idx] = water_pressure_reservoir;
258 }
259 OPM_END_PARALLEL_TRY_CATCH("AquiferNumerical::calculateAquiferPressure() failed: ",
260 this->simulator_.vanguard().grid().comm());
261 const auto& comm = this->simulator_.vanguard().grid().comm();
262 comm.sum(&sum_pressure_watervolume, 1);
263 comm.sum(&sum_watervolume, 1);
264
265 // Ensure all processes have same notion of the aquifer cells' pressure values.
266 comm.sum(cell_pressure.data(), cell_pressure.size());
267
268 return sum_pressure_watervolume / sum_watervolume;
269 }
270
271 template <class ElemCtx>
272 Scalar getWaterFlux(const ElemCtx& elem_ctx, unsigned face_idx) const
273 {
274 const auto& exQuants = elem_ctx.extensiveQuantities(face_idx, /*timeIdx*/ 0);
275 const Scalar water_flux = Toolbox::value(exQuants.volumeFlux(this->phaseIdx_()));
276 return water_flux;
277 }
278
279 Scalar calculateAquiferFluxRate() const
280 {
281 Scalar aquifer_flux = 0.0;
282
283 if (! this->connects_to_reservoir_) {
284 return aquifer_flux;
285 }
286
287 ElementContext elem_ctx(this->simulator_);
288 const auto& gridView = this->simulator_.gridView();
289 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
290 elem_ctx.updatePrimaryStencil(elem);
291 const std::size_t cell_index = elem_ctx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
292 const int idx = this->cell_to_aquifer_cell_idx_[cell_index];
293 // we only need the first aquifer cell
294 if (idx != 0) {
295 continue;
296 }
297
298 elem_ctx.updateStencil(elem);
299 const std::size_t num_interior_faces = elem_ctx.numInteriorFaces(/*timeIdx*/ 0);
300 const auto& stencil = elem_ctx.stencil(0);
301 elem_ctx.updateAllIntensiveQuantities();
302 elem_ctx.updateAllExtensiveQuantities();
303
304 for (std::size_t face_idx = 0; face_idx < num_interior_faces; ++face_idx) {
305 const auto& face = stencil.interiorFace(face_idx);
306 // dof index
307 const std::size_t i = face.interiorIndex();
308 const std::size_t j = face.exteriorIndex();
309 // compressed index
310 // const std::size_t I = stencil.globalSpaceIndex(i);
311 const std::size_t J = stencil.globalSpaceIndex(j);
312
313 assert(stencil.globalSpaceIndex(i) == cell_index);
314
315 // we do not consider the flux within aquifer cells
316 // we only need the flux to the connections
317 if (this->cell_to_aquifer_cell_idx_[J] > 0) {
318 continue;
319 }
320
321 const Scalar water_flux = getWaterFlux(elem_ctx,face_idx);
322 const std::size_t up_id = water_flux >= 0.0 ? i : j;
323 const auto& intQuantsIn = elem_ctx.intensiveQuantities(up_id, 0);
324 const Scalar invB = Toolbox::value(intQuantsIn.fluidState().invB(this->phaseIdx_()));
325 const Scalar face_area = face.area();
326 aquifer_flux += water_flux * invB * face_area;
327 }
328
329 // we only need to handle the first aquifer cell, we can exit loop here
330 break;
331 }
332
333 return aquifer_flux;
334 }
335
336 Scalar flux_rate_; // aquifer influx rate
337 Scalar cumulative_flux_; // cumulative aquifer influx
338 std::vector<Scalar> init_pressure_{};
339 Scalar pressure_; // aquifer pressure
340 bool solution_set_from_restart_ {false};
341 bool connects_to_reservoir_ {false};
342
343 // TODO: maybe unordered_map can also do the work to save memory?
344 std::vector<int> cell_to_aquifer_cell_idx_;
345};
346
347} // namespace Opm
348
349#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
Definition: AquiferInterface.hpp:35
const Simulator & simulator_
Definition: AquiferInterface.hpp:98
int phaseIdx_() const
Definition: AquiferInterface.hpp:88
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: AquiferInterface.hpp:39
GetPropType< TypeTag, Properties::FluidSystem > FluidSystem
Definition: AquiferInterface.hpp:37
int aquiferID() const
Definition: AquiferInterface.hpp:79
GetPropType< TypeTag, Properties::RateVector > RateVector
Definition: AquiferInterface.hpp:38
Definition: AquiferNumerical.hpp:45
void computeFaceAreaFraction(const std::vector< Scalar > &) override
Definition: AquiferNumerical.hpp:164
GetPropType< TypeTag, Properties::Indices > BlackoilIndices
Definition: AquiferNumerical.hpp:47
MathToolbox< Eval > Toolbox
Definition: AquiferNumerical.hpp:62
GetPropType< TypeTag, Properties::ElementContext > ElementContext
Definition: AquiferNumerical.hpp:48
@ dimWorld
Definition: AquiferNumerical.hpp:57
void serializeOp(Serializer &serializer)
Definition: AquiferNumerical.hpp:173
void initialSolutionApplied() override
Definition: AquiferNumerical.hpp:153
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: AquiferNumerical.hpp:55
GetPropType< TypeTag, Properties::ExtensiveQuantities > ExtensiveQuantities
Definition: AquiferNumerical.hpp:49
GetPropType< TypeTag, Properties::MaterialLaw > MaterialLaw
Definition: AquiferNumerical.hpp:53
AquiferNumerical(const SingleNumericalAquifer &aquifer, const Simulator &simulator)
Definition: AquiferNumerical.hpp:67
Scalar cumulativeFlux() const
Definition: AquiferNumerical.hpp:189
Scalar totalFaceArea() const override
Definition: AquiferNumerical.hpp:167
GetPropType< TypeTag, Properties::IntensiveQuantities > IntensiveQuantities
Definition: AquiferNumerical.hpp:52
void endTimeStep() override
Definition: AquiferNumerical.hpp:129
DenseAd::Evaluation< Scalar, numEq > Eval
Definition: AquiferNumerical.hpp:61
void addToSource(RateVector &, const unsigned, const unsigned) override
Definition: AquiferNumerical.hpp:127
data::AquiferData aquiferData() const override
Definition: AquiferNumerical.hpp:136
bool operator==(const AquiferNumerical &rhs) const
Definition: AquiferNumerical.hpp:181
@ numPhases
Definition: AquiferNumerical.hpp:58
void initFromRestart(const data::Aquifers &aquiferSoln) override
Definition: AquiferNumerical.hpp:104
static constexpr int numEq
Definition: AquiferNumerical.hpp:59
GetPropType< TypeTag, Properties::GridView > GridView
Definition: AquiferNumerical.hpp:51
void beginTimeStep() override
Definition: AquiferNumerical.hpp:126
static AquiferNumerical serializationTestObject(const Simulator &simulator)
Definition: AquiferNumerical.hpp:93
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