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