blackoilnewtonmethod.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
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 2 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 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
28#ifndef OPM_BLACK_OIL_NEWTON_METHOD_HPP
29#define OPM_BLACK_OIL_NEWTON_METHOD_HPP
30
31#include <opm/common/Exceptions.hpp>
32
36
38
40
41#include <opm/material/common/Valgrind.hpp>
42
43#include <algorithm>
44#include <cmath>
45#include <limits>
46#include <vector>
47
48namespace Opm::Properties {
49
50template <class TypeTag, class MyTypeTag>
51struct DiscNewtonMethod;
52
53} // namespace Opm::Properties
54
55namespace Opm {
56
62template <class TypeTag>
63class BlackOilNewtonMethod : public GetPropType<TypeTag, Properties::DiscNewtonMethod>
64{
75 static constexpr bool enableBioeffects = getPropValue<TypeTag, Properties::EnableBioeffects>();
76 using BioeffectsModule = BlackOilBioeffectsModule<TypeTag, enableBioeffects>;
77
78 static const unsigned numEq = getPropValue<TypeTag, Properties::NumEq>();
79 static constexpr bool enableSaltPrecipitation = getPropValue<TypeTag, Properties::EnableSaltPrecipitation>();
80
81public:
82 explicit BlackOilNewtonMethod(Simulator& simulator) : ParentType(simulator)
83 {
84 bparams_.read();
85 }
86
91 {
92 ParentType::finishInit();
93
94 wasSwitched_.resize(this->model().numTotalDof(), false);
95 }
96
98 {
99 numPriVarsSwitched_ = 0;
100 std::fill(wasSwitched_.begin(), wasSwitched_.end(), false);
101 }
102
106 static void registerParameters()
107 {
108 ParentType::registerParameters();
110 }
111
116 unsigned numPriVarsSwitched() const
117 { return numPriVarsSwitched_; }
118
119protected:
122
127 {
128 numPriVarsSwitched_ = 0;
129 ParentType::beginIteration_();
130 }
131
138 void endIteration_(SolutionVector& uCurrentIter,
139 const SolutionVector& uLastIter)
140 {
141#if HAVE_MPI
142 // in the MPI enabled case we need to add up the number of DOF
143 // for which the interpretation changed over all processes.
144 const int localSwitched = numPriVarsSwitched_;
145 MPI_Allreduce(&localSwitched,
146 &numPriVarsSwitched_,
147 /*num=*/1,
148 MPI_INT,
149 MPI_SUM,
150 MPI_COMM_WORLD);
151#endif // HAVE_MPI
152
153 this->simulator_.model().newtonMethod().endIterMsg()
154 << ", num switched=" << numPriVarsSwitched_;
155
156 ParentType::endIteration_(uCurrentIter, uLastIter);
157 }
158
159public:
160 void update_(SolutionVector& nextSolution,
161 const SolutionVector& currentSolution,
162 const GlobalEqVector& solutionUpdate,
163 const GlobalEqVector& currentResidual)
164 {
165 const auto& comm = this->simulator_.gridView().comm();
166
167 int succeeded;
168 try {
169 ParentType::update_(nextSolution,
170 currentSolution,
171 solutionUpdate,
172 currentResidual);
173 succeeded = 1;
174 }
175 catch (...) {
176 succeeded = 0;
177 }
178 succeeded = comm.min(succeeded);
179
180 if (!succeeded) {
181 throw NumericalProblem("A process did not succeed in adapting the primary variables");
182 }
183
184 numPriVarsSwitched_ = comm.sum(numPriVarsSwitched_);
185 }
186
187 template <class DofIndices>
188 void update_(SolutionVector& nextSolution,
189 const SolutionVector& currentSolution,
190 const GlobalEqVector& solutionUpdate,
191 const GlobalEqVector& currentResidual,
192 const DofIndices& dofIndices)
193 {
194 const auto zero = 0.0 * solutionUpdate[0];
195 for (auto dofIdx : dofIndices) {
196 if (solutionUpdate[dofIdx] == zero) {
197 continue;
198 }
200 nextSolution[dofIdx],
201 currentSolution[dofIdx],
202 solutionUpdate[dofIdx],
203 currentResidual[dofIdx]);
204 }
205 }
206
207protected:
211 void updatePrimaryVariables_(unsigned globalDofIdx,
212 PrimaryVariables& nextValue,
213 const PrimaryVariables& currentValue,
214 const EqVector& update,
215 const EqVector& currentResidual)
216 {
217 static constexpr bool enableSolvent =
218 Indices::solventSaturationIdx != std::numeric_limits<unsigned>::max();
219 static constexpr bool enableExtbo =
220 Indices::zFractionIdx != std::numeric_limits<unsigned>::max();
221 static constexpr bool enablePolymer =
222 Indices::polymerConcentrationIdx != std::numeric_limits<unsigned>::max();
223 static constexpr bool enablePolymerWeight =
224 Indices::polymerMoleWeightIdx != std::numeric_limits<unsigned>::max();
225 static constexpr bool enableFullyImplicitThermal =
226 Indices::temperatureIdx != std::numeric_limits<unsigned>::max();
227 static constexpr bool enableFoam =
228 Indices::foamConcentrationIdx != std::numeric_limits<unsigned>::max();
229 static constexpr bool enableBrine =
230 Indices::saltConcentrationIdx != std::numeric_limits<unsigned>::max();
231 static constexpr bool enableMICP = Indices::enableMICP;
232
233 currentValue.checkDefined();
234 Valgrind::CheckDefined(update);
235 Valgrind::CheckDefined(currentResidual);
236
237 // saturation delta for each phase
238 Scalar deltaSw = 0.0;
239 Scalar deltaSo = 0.0;
240 Scalar deltaSg = 0.0;
241 Scalar deltaSs = 0.0;
242
243 if (currentValue.primaryVarsMeaningWater() == PrimaryVariables::WaterMeaning::Sw)
244 {
245 if constexpr (Indices::waterSwitchIdx != std::numeric_limits<unsigned>::max()) {
246 deltaSw = update[Indices::waterSwitchIdx];
247 deltaSo -= deltaSw;
248 }
249 }
250 if (currentValue.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Sg)
251 {
252 if constexpr (Indices::compositionSwitchIdx != std::numeric_limits<unsigned>::max()) {
253 deltaSg = update[Indices::compositionSwitchIdx];
254 deltaSo -= deltaSg;
255 }
256 }
257 if (currentValue.primaryVarsMeaningSolvent() == PrimaryVariables::SolventMeaning::Ss) {
258 if constexpr (Indices::solventSaturationIdx != std::numeric_limits<unsigned>::max()) {
259 deltaSs = update[Indices::solventSaturationIdx];
260 deltaSo -= deltaSs;
261 }
262 }
263
264 // maximum saturation delta
265 Scalar maxSatDelta = std::max(std::abs(deltaSg), std::abs(deltaSo));
266 maxSatDelta = std::max(maxSatDelta, std::abs(deltaSw));
267 maxSatDelta = std::max(maxSatDelta, std::abs(deltaSs));
268
269 // scaling factor for saturation deltas to make sure that none of them exceeds
270 // the specified threshold value.
271 Scalar satAlpha = 1.0;
272 if (maxSatDelta > bparams_.dsMax_) {
273 satAlpha = bparams_.dsMax_ / maxSatDelta;
274 }
275
276 for (unsigned pvIdx = 0; pvIdx < numEq; ++pvIdx) {
277 // calculate the update of the current primary variable. For the black-oil
278 // model we limit the pressure delta relative to the pressure's current
279 // absolute value (Default: 30%) and saturation deltas to an absolute change
280 // (Default: 20%). Further, we ensure that the R factors, solvent
281 // "saturation" and polymer concentration do not become negative after the
282 // update.
283 Scalar delta = update[pvIdx];
284
285 // limit pressure delta
286 if (pvIdx == Indices::pressureSwitchIdx) {
287 if (std::abs(delta) > bparams_.dpMaxRel_ * currentValue[pvIdx]) {
288 delta = signum(delta) * bparams_.dpMaxRel_ * currentValue[pvIdx];
289 }
290 }
291 // water saturation delta
292 else if (pvIdx == Indices::waterSwitchIdx)
293 if (currentValue.primaryVarsMeaningWater() == PrimaryVariables::WaterMeaning::Sw) {
294 delta *= satAlpha;
295 }
296 else {
297 //Ensure Rvw and Rsw factor does not become negative
298 if (delta > currentValue[ Indices::waterSwitchIdx]) {
299 delta = currentValue[ Indices::waterSwitchIdx];
300 }
301 }
302 else if (pvIdx == Indices::compositionSwitchIdx) {
303 // the switching primary variable for composition is tricky because the
304 // "reasonable" value ranges it exhibits vary widely depending on its
305 // interpretation since it can represent Sg, Rs or Rv. For now, we only
306 // limit saturation deltas and ensure that the R factors do not become
307 // negative.
308 if (currentValue.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Sg) {
309 delta *= satAlpha;
310 }
311 else {
312 // Ensure Rv and Rs factor does not become negative
313 if (delta > currentValue[Indices::compositionSwitchIdx]) {
314 delta = currentValue[Indices::compositionSwitchIdx];
315 }
316 }
317 }
318 else if (enableSolvent && pvIdx == Indices::solventSaturationIdx) {
319 // solvent saturation updates are also subject to the Appleyard chop
320 if (currentValue.primaryVarsMeaningSolvent() == PrimaryVariables::SolventMeaning::Ss) {
321 delta *= satAlpha;
322 }
323 else {
324 // Ensure Rssolw factor does not become negative
325 if (delta > currentValue[Indices::solventSaturationIdx]) {
326 delta = currentValue[Indices::solventSaturationIdx];
327 }
328 }
329 }
330 else if (enableExtbo && pvIdx == Indices::zFractionIdx) {
331 // z fraction updates are also subject to the Appleyard chop
332 const auto& curr = currentValue[Indices::zFractionIdx]; // or currentValue[pvIdx] given the block condition
333 delta = std::clamp(delta, curr - Scalar{1.0}, curr);
334 }
335 else if (enablePolymerWeight && pvIdx == Indices::polymerMoleWeightIdx) {
336 const double sign = delta >= 0. ? 1. : -1.;
337 // maximum change of polymer molecular weight, the unit is MDa.
338 // applying this limit to stabilize the simulation. The value itself is still experimental.
339 const Scalar maxMolarWeightChange = 100.0;
340 delta = sign * std::min(std::abs(delta), maxMolarWeightChange);
341 delta *= satAlpha;
342 }
343 else if (enableFullyImplicitThermal && pvIdx == Indices::temperatureIdx) {
344 const double sign = delta >= 0. ? 1. : -1.;
345 delta = sign * std::min(std::abs(delta), bparams_.maxTempChange_);
346 }
347 else if (enableBrine && pvIdx == Indices::saltConcentrationIdx &&
348 enableSaltPrecipitation &&
349 currentValue.primaryVarsMeaningBrine() == PrimaryVariables::BrineMeaning::Sp)
350 {
351 const Scalar maxSaltSaturationChange = 0.1;
352 const Scalar sign = delta >= 0. ? 1. : -1.;
353 delta = sign * std::min(std::abs(delta), maxSaltSaturationChange);
354 }
355
356 // do the actual update
357 nextValue[pvIdx] = currentValue[pvIdx] - delta;
358
359 // keep the solvent saturation between 0 and 1
360 if (enableSolvent && pvIdx == Indices::solventSaturationIdx) {
361 if (currentValue.primaryVarsMeaningSolvent() == PrimaryVariables::SolventMeaning::Ss) {
362 nextValue[pvIdx] = std::min(std::max(nextValue[pvIdx], Scalar{0.0}), Scalar{1.0});
363 }
364 }
365
366 // keep the z fraction between 0 and 1
367 if (enableExtbo && pvIdx == Indices::zFractionIdx) {
368 nextValue[pvIdx] = std::min(std::max(nextValue[pvIdx], Scalar{0.0}), Scalar{1.0});
369 }
370
371 // keep the polymer concentration above 0
372 if (enablePolymer && pvIdx == Indices::polymerConcentrationIdx) {
373 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
374 }
375
376 if (enablePolymerWeight && pvIdx == Indices::polymerMoleWeightIdx) {
377 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
378 const double polymerConcentration = nextValue[Indices::polymerConcentrationIdx];
379 if (polymerConcentration < 1.e-10) {
380 nextValue[pvIdx] = 0.0;
381 }
382 }
383
384 // keep the foam concentration above 0
385 if (enableFoam && pvIdx == Indices::foamConcentrationIdx) {
386 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
387 }
388
389 if (enableBrine && pvIdx == Indices::saltConcentrationIdx) {
390 // keep the salt concentration above 0
391 if (!enableSaltPrecipitation ||
392 currentValue.primaryVarsMeaningBrine() == PrimaryVariables::BrineMeaning::Cs)
393 {
394 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
395 }
396 // keep the salt saturation below upperlimit
397 if (enableSaltPrecipitation &&
398 currentValue.primaryVarsMeaningBrine() == PrimaryVariables::BrineMeaning::Sp)
399 {
400 nextValue[pvIdx] = std::min(nextValue[pvIdx], Scalar{1.0-1.e-8});
401 }
402 }
403
404 // keep the temperature within given values
405 if (enableFullyImplicitThermal && pvIdx == Indices::temperatureIdx) {
406 nextValue[pvIdx] = std::clamp(nextValue[pvIdx], bparams_.tempMin_, bparams_.tempMax_);
407 }
408
409 if (pvIdx == Indices::pressureSwitchIdx) {
410 nextValue[pvIdx] = std::clamp(nextValue[pvIdx], bparams_.pressMin_, bparams_.pressMax_);
411 }
412
413 // keep the values above 0
414 // for the biofilm and calcite, we set an upper limit equal to the initial porosity
415 // minus 1e-8. This prevents singularities (e.g., one of the calcite source term is
416 // evaluated at 1/(iniPoro - calcite)). The value 1e-8 is taken from the salt precipitation
417 // clapping above.
418 if constexpr (enableBioeffects) {
419 if (pvIdx == Indices::microbialConcentrationIdx) {
420 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
421 }
422 if (pvIdx == Indices::biofilmVolumeFractionIdx) {
423 nextValue[pvIdx] = std::clamp(nextValue[pvIdx],
424 Scalar{0.0},
425 this->problem().referencePorosity(globalDofIdx, 0) - 1e-8);
426 }
427 if constexpr (enableMICP) {
428 if (pvIdx == Indices::oxygenConcentrationIdx) {
429 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
430 }
431 if (pvIdx == Indices::ureaConcentrationIdx) {
432 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
433 }
434 if (pvIdx == Indices::calciteVolumeFractionIdx) {
435 nextValue[pvIdx] = std::clamp(nextValue[pvIdx], Scalar{0.0},
436 this->problem().referencePorosity(globalDofIdx, 0) - 1e-8);
437 }
438 }
439 }
440 }
441
442 // switch the new primary variables to something which is physically meaningful.
443 // use a threshold value after a switch to make it harder to switch back
444 // immediately.
445 if (wasSwitched_[globalDofIdx]) {
446 wasSwitched_[globalDofIdx] = nextValue.adaptPrimaryVariables(this->problem(),
447 globalDofIdx,
448 bparams_.waterSaturationMax_,
449 bparams_.waterOnlyThreshold_,
450 bparams_.priVarOscilationThreshold_);
451 }
452 else {
453 wasSwitched_[globalDofIdx] = nextValue.adaptPrimaryVariables(this->problem(),
454 globalDofIdx,
455 bparams_.waterSaturationMax_,
456 bparams_.waterOnlyThreshold_);
457 }
458
459 if (wasSwitched_[globalDofIdx]) {
460 ++numPriVarsSwitched_;
461 }
462 if (bparams_.projectSaturations_) {
463 nextValue.chopAndNormalizeSaturations();
464 }
465
466 nextValue.checkDefined();
467 }
468
469private:
470 int numPriVarsSwitched_{};
471
472 BlackoilNewtonParams<Scalar> bparams_{};
473
474 // keep track of cells where the primary variable meaning has changed
475 // to detect and hinder oscillations
476 std::vector<bool> wasSwitched_{};
477};
478
479} // namespace Opm
480
481#endif // OPM_BLACK_OIL_NEWTHON_METHOD_HPP
Contains classes extending the black-oil model. \detail This file holds dummy definitions,...
Declares the properties required by the black oil model.
A newton solver which is specific to the black oil model.
Definition: blackoilnewtonmethod.hpp:64
void beginIteration_()
Indicates the beginning of a Newton iteration.
Definition: blackoilnewtonmethod.hpp:126
void resetPrimaryVariableSwitches()
Definition: blackoilnewtonmethod.hpp:97
static void registerParameters()
Register all run-time parameters for the blackoil newton method.
Definition: blackoilnewtonmethod.hpp:106
void update_(SolutionVector &nextSolution, const SolutionVector &currentSolution, const GlobalEqVector &solutionUpdate, const GlobalEqVector &currentResidual)
Definition: blackoilnewtonmethod.hpp:160
void finishInit()
Finialize the construction of the object.
Definition: blackoilnewtonmethod.hpp:90
void endIteration_(SolutionVector &uCurrentIter, const SolutionVector &uLastIter)
Indicates that one Newton iteration was finished.
Definition: blackoilnewtonmethod.hpp:138
unsigned numPriVarsSwitched() const
Returns the number of degrees of freedom for which the interpretation has changed for the most recent...
Definition: blackoilnewtonmethod.hpp:116
BlackOilNewtonMethod(Simulator &simulator)
Definition: blackoilnewtonmethod.hpp:82
void updatePrimaryVariables_(unsigned globalDofIdx, PrimaryVariables &nextValue, const PrimaryVariables &currentValue, const EqVector &update, const EqVector &currentResidual)
Update a single primary variables object.
Definition: blackoilnewtonmethod.hpp:211
friend ParentType
Definition: blackoilnewtonmethod.hpp:121
void update_(SolutionVector &nextSolution, const SolutionVector &currentSolution, const GlobalEqVector &solutionUpdate, const GlobalEqVector &currentResidual, const DofIndices &dofIndices)
Definition: blackoilnewtonmethod.hpp:188
The multi-dimensional Newton method.
Definition: newtonmethod.hh:100
Definition: blackoilmodel.hh:74
Definition: blackoilbioeffectsmodules.hh:45
constexpr int signum(Scalar val)
Template function which returns the sign of a floating point value.
Definition: signum.hh:41
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
static void registerParameters()
Registers the parameters in parameter system.