28#ifndef OPM_BLACK_OIL_NEWTON_METHOD_HPP
29#define OPM_BLACK_OIL_NEWTON_METHOD_HPP
31#include <opm/common/Exceptions.hpp>
41#include <opm/material/common/Valgrind.hpp>
50template <
class TypeTag,
class MyTypeTag>
51struct DiscNewtonMethod;
62template <
class TypeTag>
75 static constexpr bool enableBioeffects = getPropValue<TypeTag, Properties::EnableBioeffects>();
76 using BioeffectsModule = BlackOilBioeffectsModule<TypeTag, enableBioeffects>;
78 static const unsigned numEq = getPropValue<TypeTag, Properties::NumEq>();
79 static constexpr bool enableSaltPrecipitation = getPropValue<TypeTag, Properties::EnableSaltPrecipitation>();
92 ParentType::finishInit();
94 wasSwitched_.resize(this->model().numTotalDof(),
false);
100 wasSwitchedAtStepStart_ = wasSwitched_;
105 wasSwitched_ = wasSwitchedAtStepStart_;
113 ParentType::registerParameters();
122 {
return numPriVarsSwitched_; }
133 numPriVarsSwitched_ = 0;
134 ParentType::beginIteration_();
144 const SolutionVector& uLastIter)
149 const int localSwitched = numPriVarsSwitched_;
150 MPI_Allreduce(&localSwitched,
151 &numPriVarsSwitched_,
158 this->simulator_.model().newtonMethod().endIterMsg()
159 <<
", num switched=" << numPriVarsSwitched_;
161 ParentType::endIteration_(uCurrentIter, uLastIter);
168 {
return wasSwitched_[globalDofIdx]; }
171 { wasSwitched_[globalDofIdx] = switched; }
174 const SolutionVector& currentSolution,
175 const GlobalEqVector& solutionUpdate,
176 const GlobalEqVector& currentResidual)
178 const auto& comm = this->simulator_.gridView().comm();
182 ParentType::update_(nextSolution,
191 succeeded = comm.min(succeeded);
194 throw NumericalProblem(
"A process did not succeed in adapting the primary variables");
197 numPriVarsSwitched_ = comm.sum(numPriVarsSwitched_);
200 template <
class DofIndices>
202 const SolutionVector& currentSolution,
203 const GlobalEqVector& solutionUpdate,
204 const GlobalEqVector& currentResidual,
205 const DofIndices& dofIndices)
207 const auto zero = 0.0 * solutionUpdate[0];
208 for (
auto dofIdx : dofIndices) {
209 if (solutionUpdate[dofIdx] == zero) {
213 nextSolution[dofIdx],
214 currentSolution[dofIdx],
215 solutionUpdate[dofIdx],
216 currentResidual[dofIdx]);
225 PrimaryVariables& nextValue,
226 const PrimaryVariables& currentValue,
227 const EqVector& update,
228 const EqVector& currentResidual)
230 static constexpr bool enableSolvent =
231 Indices::solventSaturationIdx != std::numeric_limits<unsigned>::max();
232 static constexpr bool enableExtbo =
233 Indices::zFractionIdx != std::numeric_limits<unsigned>::max();
234 static constexpr bool enablePolymer =
235 Indices::polymerConcentrationIdx != std::numeric_limits<unsigned>::max();
236 static constexpr bool enablePolymerWeight =
237 Indices::polymerMoleWeightIdx != std::numeric_limits<unsigned>::max();
238 static constexpr bool enableFullyImplicitThermal =
239 Indices::temperatureIdx != std::numeric_limits<unsigned>::max();
240 static constexpr bool enableFoam =
241 Indices::foamConcentrationIdx != std::numeric_limits<unsigned>::max();
242 static constexpr bool enableBrine =
243 Indices::saltConcentrationIdx != std::numeric_limits<unsigned>::max();
244 static constexpr bool enableMICP = Indices::enableMICP;
246 currentValue.checkDefined();
247 Valgrind::CheckDefined(update);
248 Valgrind::CheckDefined(currentResidual);
251 Scalar deltaSw = 0.0;
252 Scalar deltaSo = 0.0;
253 Scalar deltaSg = 0.0;
254 Scalar deltaSs = 0.0;
256 if (currentValue.primaryVarsMeaningWater() == PrimaryVariables::WaterMeaning::Sw)
258 if constexpr (Indices::waterSwitchIdx != std::numeric_limits<unsigned>::max()) {
259 deltaSw = update[Indices::waterSwitchIdx];
263 if (currentValue.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Sg)
265 if constexpr (Indices::compositionSwitchIdx != std::numeric_limits<unsigned>::max()) {
266 deltaSg = update[Indices::compositionSwitchIdx];
270 if (currentValue.primaryVarsMeaningSolvent() == PrimaryVariables::SolventMeaning::Ss) {
271 if constexpr (Indices::solventSaturationIdx != std::numeric_limits<unsigned>::max()) {
272 deltaSs = update[Indices::solventSaturationIdx];
278 Scalar maxSatDelta = std::max(std::abs(deltaSg), std::abs(deltaSo));
279 maxSatDelta = std::max(maxSatDelta, std::abs(deltaSw));
280 maxSatDelta = std::max(maxSatDelta, std::abs(deltaSs));
284 Scalar satAlpha = 1.0;
285 if (maxSatDelta > bparams_.dsMax_) {
286 satAlpha = bparams_.dsMax_ / maxSatDelta;
289 for (
unsigned pvIdx = 0; pvIdx < numEq; ++pvIdx) {
296 Scalar delta = update[pvIdx];
299 if (pvIdx == Indices::pressureSwitchIdx) {
300 if (std::abs(delta) > bparams_.dpMaxRel_ * currentValue[pvIdx]) {
301 delta =
signum(delta) * bparams_.dpMaxRel_ * currentValue[pvIdx];
305 else if (pvIdx == Indices::waterSwitchIdx)
306 if (currentValue.primaryVarsMeaningWater() == PrimaryVariables::WaterMeaning::Sw) {
311 if (delta > currentValue[ Indices::waterSwitchIdx]) {
312 delta = currentValue[ Indices::waterSwitchIdx];
315 else if (pvIdx == Indices::compositionSwitchIdx) {
321 if (currentValue.primaryVarsMeaningGas() == PrimaryVariables::GasMeaning::Sg) {
326 if (delta > currentValue[Indices::compositionSwitchIdx]) {
327 delta = currentValue[Indices::compositionSwitchIdx];
331 else if (enableSolvent && pvIdx == Indices::solventSaturationIdx) {
333 if (currentValue.primaryVarsMeaningSolvent() == PrimaryVariables::SolventMeaning::Ss) {
338 if (delta > currentValue[Indices::solventSaturationIdx]) {
339 delta = currentValue[Indices::solventSaturationIdx];
343 else if (enableExtbo && pvIdx == Indices::zFractionIdx) {
345 const auto& curr = currentValue[Indices::zFractionIdx];
346 delta = std::clamp(delta, curr - Scalar{1.0}, curr);
348 else if (enablePolymerWeight && pvIdx == Indices::polymerMoleWeightIdx) {
349 const double sign = delta >= 0. ? 1. : -1.;
352 const Scalar maxMolarWeightChange = 100.0;
353 delta = sign * std::min(std::abs(delta), maxMolarWeightChange);
356 else if (enableFullyImplicitThermal && pvIdx == Indices::temperatureIdx) {
357 const double sign = delta >= 0. ? 1. : -1.;
358 delta = sign * std::min(std::abs(delta), bparams_.maxTempChange_);
360 else if (enableBrine && pvIdx == Indices::saltConcentrationIdx &&
361 enableSaltPrecipitation &&
362 currentValue.primaryVarsMeaningBrine() == PrimaryVariables::BrineMeaning::Sp)
364 const Scalar maxSaltSaturationChange = 0.1;
365 const Scalar sign = delta >= 0. ? 1. : -1.;
366 delta = sign * std::min(std::abs(delta), maxSaltSaturationChange);
370 nextValue[pvIdx] = currentValue[pvIdx] - delta;
373 if (enableSolvent && pvIdx == Indices::solventSaturationIdx) {
374 if (currentValue.primaryVarsMeaningSolvent() == PrimaryVariables::SolventMeaning::Ss) {
375 nextValue[pvIdx] = std::min(std::max(nextValue[pvIdx], Scalar{0.0}), Scalar{1.0});
380 if (enableExtbo && pvIdx == Indices::zFractionIdx) {
381 nextValue[pvIdx] = std::min(std::max(nextValue[pvIdx], Scalar{0.0}), Scalar{1.0});
385 if (enablePolymer && pvIdx == Indices::polymerConcentrationIdx) {
386 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
389 if (enablePolymerWeight && pvIdx == Indices::polymerMoleWeightIdx) {
390 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
391 const double polymerConcentration = nextValue[Indices::polymerConcentrationIdx];
392 if (polymerConcentration < 1.e-10) {
393 nextValue[pvIdx] = 0.0;
398 if (enableFoam && pvIdx == Indices::foamConcentrationIdx) {
399 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
402 if (enableBrine && pvIdx == Indices::saltConcentrationIdx) {
404 if (!enableSaltPrecipitation ||
405 currentValue.primaryVarsMeaningBrine() == PrimaryVariables::BrineMeaning::Cs)
407 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
410 if (enableSaltPrecipitation &&
411 currentValue.primaryVarsMeaningBrine() == PrimaryVariables::BrineMeaning::Sp)
413 nextValue[pvIdx] = std::min(nextValue[pvIdx], Scalar{1.0-1.e-8});
418 if (enableFullyImplicitThermal && pvIdx == Indices::temperatureIdx) {
419 nextValue[pvIdx] = std::clamp(nextValue[pvIdx], bparams_.tempMin_, bparams_.tempMax_);
422 if (pvIdx == Indices::pressureSwitchIdx) {
423 nextValue[pvIdx] = std::clamp(nextValue[pvIdx], bparams_.pressMin_, bparams_.pressMax_);
431 if constexpr (enableBioeffects) {
432 if (pvIdx == Indices::microbialConcentrationIdx) {
433 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
435 if (pvIdx == Indices::biofilmVolumeFractionIdx) {
436 nextValue[pvIdx] = std::clamp(nextValue[pvIdx],
438 this->problem().referencePorosity(globalDofIdx, 0) - 1e-8);
440 if constexpr (enableMICP) {
441 if (pvIdx == Indices::oxygenConcentrationIdx) {
442 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
444 if (pvIdx == Indices::ureaConcentrationIdx) {
445 nextValue[pvIdx] = std::max(nextValue[pvIdx], Scalar{0.0});
447 if (pvIdx == Indices::calciteVolumeFractionIdx) {
448 nextValue[pvIdx] = std::clamp(nextValue[pvIdx], Scalar{0.0},
449 this->problem().referencePorosity(globalDofIdx, 0) - 1e-8);
458 if (wasSwitched_[globalDofIdx]) {
459 wasSwitched_[globalDofIdx] = nextValue.adaptPrimaryVariables(this->problem(),
461 bparams_.waterSaturationMax_,
462 bparams_.waterOnlyThreshold_,
463 bparams_.priVarOscilationThreshold_);
466 wasSwitched_[globalDofIdx] = nextValue.adaptPrimaryVariables(this->problem(),
468 bparams_.waterSaturationMax_,
469 bparams_.waterOnlyThreshold_);
472 if (wasSwitched_[globalDofIdx]) {
473 ++numPriVarsSwitched_;
475 if (bparams_.projectSaturations_) {
476 nextValue.chopAndNormalizeSaturations();
479 nextValue.checkDefined();
483 int numPriVarsSwitched_{};
485 BlackoilNewtonParams<Scalar> bparams_{};
489 std::vector<bool> wasSwitched_{};
490 std::vector<bool> wasSwitchedAtStepStart_{};
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:131
bool wasSwitched(const unsigned globalDofIdx) const
Definition: blackoilnewtonmethod.hpp:167
static void registerParameters()
Register all run-time parameters for the blackoil newton method.
Definition: blackoilnewtonmethod.hpp:111
void update_(SolutionVector &nextSolution, const SolutionVector ¤tSolution, const GlobalEqVector &solutionUpdate, const GlobalEqVector ¤tResidual)
Definition: blackoilnewtonmethod.hpp:173
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:143
unsigned numPriVarsSwitched() const
Returns the number of degrees of freedom for which the interpretation has changed for the most recent...
Definition: blackoilnewtonmethod.hpp:121
BlackOilNewtonMethod(Simulator &simulator)
Definition: blackoilnewtonmethod.hpp:82
void updatePrimaryVariables_(unsigned globalDofIdx, PrimaryVariables &nextValue, const PrimaryVariables ¤tValue, const EqVector &update, const EqVector ¤tResidual)
Update a single primary variables object.
Definition: blackoilnewtonmethod.hpp:224
friend ParentType
Definition: blackoilnewtonmethod.hpp:126
void setWasSwitched(const unsigned globalDofIdx, const bool switched)
Definition: blackoilnewtonmethod.hpp:170
void restorePrimaryVariableSwitches()
Definition: blackoilnewtonmethod.hpp:103
void capturePrimaryVariableSwitches()
Definition: blackoilnewtonmethod.hpp:97
void update_(SolutionVector &nextSolution, const SolutionVector ¤tSolution, const GlobalEqVector &solutionUpdate, const GlobalEqVector ¤tResidual, const DofIndices &dofIndices)
Definition: blackoilnewtonmethod.hpp:201
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.