My Project
RegionAverageCalculator.hpp
Go to the documentation of this file.
1/*
2 Copyright 2021, Equinor
3
4 This file is part of the Open Porous Media Project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_REGIONAVERAGECALCULATOR_HPP_HEADER_INCLUDED
21#define OPM_REGIONAVERAGECALCULATOR_HPP_HEADER_INCLUDED
22
23#include <opm/core/props/BlackoilPhases.hpp>
24#include <opm/simulators/wells/RegionAttributeHelpers.hpp>
25#include <opm/simulators/utils/DeferredLoggingErrorHelpers.hpp>
26
27#include <dune/grid/common/gridenums.hh>
28#include <dune/grid/common/rangegenerators.hh>
29#include <algorithm>
30#include <cmath>
31#include <memory>
32#include <stdexcept>
33#include <type_traits>
34#include <unordered_map>
35#include <utility>
36#include <vector>
47namespace Opm {
48 namespace RegionAverageCalculator {
49
61 template <class FluidSystem, class Region>
63 public:
72 const Region& region)
73 : phaseUsage_(phaseUsage)
74 , rmap_ (region)
75 , attr_ (rmap_, Attributes())
76 {
77 }
78
79
83 template <typename ElementContext, class EbosSimulator>
84 void defineState(const EbosSimulator& simulator)
85 {
86
87
88 int numRegions = 0;
89 const auto& gridView = simulator.gridView();
90 const auto& comm = gridView.comm();
91 for (const auto& reg : rmap_.activeRegions()) {
92 numRegions = std::max(numRegions, reg);
93 }
94 numRegions = comm.max(numRegions);
95 for (int reg = 1; reg <= numRegions ; ++ reg) {
96 if(!attr_.has(reg))
97 attr_.insert(reg, Attributes());
98 }
99 // create map from cell to region
100 // and set all attributes to zero
101 for (int reg = 1; reg <= numRegions ; ++ reg) {
102 auto& ra = attr_.attributes(reg);
103 ra.pressure = 0.0;
104 ra.pv = 0.0;
105
106 }
107
108 // quantities for pore volume average
109 std::unordered_map<RegionId, Attributes> attributes_pv;
110
111 // quantities for hydrocarbon volume average
112 std::unordered_map<RegionId, Attributes> attributes_hpv;
113
114 for (int reg = 1; reg <= numRegions ; ++ reg) {
115 attributes_pv.insert({reg, Attributes()});
116 attributes_hpv.insert({reg, Attributes()});
117 }
118
119 ElementContext elemCtx( simulator );
120
121 OPM_BEGIN_PARALLEL_TRY_CATCH();
122 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
123 elemCtx.updatePrimaryStencil(elem);
124 elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
125 const unsigned cellIdx = elemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
126 const auto& intQuants = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
127 const auto& fs = intQuants.fluidState();
128 // use pore volume weighted averages.
129 const double pv_cell =
130 simulator.model().dofTotalVolume(cellIdx)
131 * intQuants.porosity().value();
132
133 // only count oil and gas filled parts of the domain
134 double hydrocarbon = 1.0;
135 const auto& pu = phaseUsage_;
137 hydrocarbon -= fs.saturation(FluidSystem::waterPhaseIdx).value();
138 }
139
140 const int reg = rmap_.region(cellIdx);
141 assert(reg >= 0);
142
143 // sum p, rs, rv, and T.
144 const double hydrocarbonPV = pv_cell*hydrocarbon;
145 if (hydrocarbonPV > 0.) {
146 auto& attr = attributes_hpv[reg];
147 attr.pv += hydrocarbonPV;
149 attr.pressure += fs.pressure(FluidSystem::oilPhaseIdx).value() * hydrocarbonPV;
150 } else {
152 attr.pressure += fs.pressure(FluidSystem::gasPhaseIdx).value() * hydrocarbonPV;
153 }
154 }
155
156 if (pv_cell > 0.) {
157 auto& attr = attributes_pv[reg];
158 attr.pv += pv_cell;
160 attr.pressure += fs.pressure(FluidSystem::oilPhaseIdx).value() * pv_cell;
162 attr.pressure += fs.pressure(FluidSystem::gasPhaseIdx).value() * pv_cell;
163 } else {
165 attr.pressure += fs.pressure(FluidSystem::waterPhaseIdx).value() * pv_cell;
166 }
167 }
168 }
169 OPM_END_PARALLEL_TRY_CATCH("AverageRegionalPressure::defineState(): ", simulator.vanguard().grid().comm());
170
171 for (int reg = 1; reg <= numRegions ; ++ reg) {
172 auto& ra = attr_.attributes(reg);
173 const double hpv_sum = comm.sum(attributes_hpv[reg].pv);
174 // TODO: should we have some epsilon here instead of zero?
175 if (hpv_sum > 0.) {
176 const auto& attri_hpv = attributes_hpv[reg];
177 const double p_hpv_sum = comm.sum(attri_hpv.pressure);
178 ra.pressure = p_hpv_sum / hpv_sum;
179 } else {
180 // using the pore volume to do the averaging
181 const auto& attri_pv = attributes_pv[reg];
182 const double pv_sum = comm.sum(attri_pv.pv);
183 // pore volums can be zero if a fipnum region is empty
184 if (pv_sum > 0) {
185 const double p_pv_sum = comm.sum(attri_pv.pressure);
186 ra.pressure = p_pv_sum / pv_sum;
187 }
188 }
189 }
190 }
191
197 typedef typename RegionMapping<Region>::RegionId RegionId;
198
203 double
204 pressure(const RegionId r) const
205 {
206 if (r == 0 ) // region 0 is the whole field
207 {
208 double pressure = 0.0;
209 int num_active_regions = 0;
210 for (const auto& attr : attr_.attributes()) {
211 const auto& value = *attr.second;
212 const auto& ra = value.attr_;
213 pressure += ra.pressure;
214 num_active_regions ++;
215 }
216 return pressure / num_active_regions;
217 }
218
219 const auto& ra = attr_.attributes(r);
220 return ra.pressure;
221 }
222
223
224 private:
228 const PhaseUsage phaseUsage_;
229
233 const RegionMapping<Region> rmap_;
234
238 struct Attributes {
239 Attributes()
240 : pressure(0.0)
241 , pv(0.0)
242
243 {}
244
245 double pressure;
246 double pv;
247
248 };
249
250 RegionAttributeHelpers::RegionAttributes<RegionId, Attributes> attr_;
251
252 };
253 } // namespace RegionAverageCalculator
254} // namespace Opm
255
256#endif /* OPM_REGIONAVERAGECALCULATOR_HPP_HEADER_INCLUDED */
Computes hydrocarbon weighed average pressures over regions.
Definition: RegionAverageCalculator.hpp:62
void defineState(const EbosSimulator &simulator)
Compute pore volume averaged hydrocarbon state pressure, *.
Definition: RegionAverageCalculator.hpp:84
AverageRegionalPressure(const PhaseUsage &phaseUsage, const Region &region)
Constructor.
Definition: RegionAverageCalculator.hpp:71
double pressure(const RegionId r) const
Average pressure.
Definition: RegionAverageCalculator.hpp:204
RegionMapping< Region >::RegionId RegionId
Region identifier.
Definition: RegionAverageCalculator.hpp:197
bool water(const PhaseUsage &pu)
Active water predicate.
Definition: RegionAttributeHelpers.hpp:308
bool oil(const PhaseUsage &pu)
Active oil predicate.
Definition: RegionAttributeHelpers.hpp:321
bool gas(const PhaseUsage &pu)
Active gas predicate.
Definition: RegionAttributeHelpers.hpp:334
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27
PhaseUsage phaseUsage(const Phases &phases)
Determine the active phases.
Definition: phaseUsageFromDeck.cpp:37
Definition: BlackoilPhases.hpp:46