28#ifndef EWOMS_NCP_NEWTON_METHOD_HH
29#define EWOMS_NCP_NEWTON_METHOD_HH
33#include <opm/common/Exceptions.hpp>
41template <
class TypeTag,
class MyTypeTag>
42struct DiscNewtonMethod;
53template <
class TypeTag>
66 enum { numEq = getPropValue<TypeTag, Properties::NumEq>() };
67 enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
68 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
69 enum { fugacity0Idx = Indices::fugacity0Idx };
70 enum { saturation0Idx = Indices::saturation0Idx };
71 enum { pressure0Idx = Indices::pressure0Idx };
72 enum { ncp0EqIdx = Indices::ncp0EqIdx };
86 const GlobalEqVector& currentResidual)
88 const auto& constraintsMap = this->model().linearizer().constraintsMap();
89 this->lastError_ = this->error_;
94 for (
unsigned dofIdx = 0; dofIdx < currentResidual.size(); ++dofIdx) {
96 if (dofIdx >= this->model().numGridDof() || this->model().dofTotalVolume(dofIdx) <= 0.0)
100 if (this->enableConstraints_()) {
101 if (constraintsMap.count(dofIdx) > 0)
105 const auto& r = currentResidual[dofIdx];
106 for (
unsigned eqIdx = 0; eqIdx < r.size(); ++eqIdx) {
107 if (ncp0EqIdx <= eqIdx && eqIdx < Indices::ncp0EqIdx + numPhases)
110 std::max(std::abs(r[eqIdx]*this->model().eqWeight(dofIdx, eqIdx)),
116 this->error_ = this->comm_.max(this->error_);
121 throw Opm::NumericalProblem(
"Newton: Error "+std::to_string(
double(this->error_))+
122 +
" is larger than maximum allowed error of "
130 PrimaryVariables& nextValue,
131 const PrimaryVariables& currentValue,
132 const EqVector& update,
136 nextValue = currentValue;
144 Scalar sumSatDelta = 0.0;
145 Scalar maxSatDelta = 0.0;
146 for (
unsigned phaseIdx = 0; phaseIdx < numPhases - 1; ++phaseIdx) {
147 maxSatDelta = std::max(std::abs(update[saturation0Idx + phaseIdx]),
149 sumSatDelta += update[saturation0Idx + phaseIdx];
151 maxSatDelta = std::max(std::abs(- sumSatDelta), maxSatDelta);
153 if (maxSatDelta > 0.2) {
154 Scalar alpha = 0.2/maxSatDelta;
155 for (
unsigned phaseIdx = 0; phaseIdx < numPhases - 1; ++phaseIdx) {
156 nextValue[saturation0Idx + phaseIdx] =
157 currentValue[saturation0Idx + phaseIdx]
158 - alpha*update[saturation0Idx + phaseIdx];
163 clampValue_(nextValue[pressure0Idx],
164 currentValue[pressure0Idx]*0.8,
165 currentValue[pressure0Idx]*1.2);
168 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
169 Scalar& val = nextValue[fugacity0Idx + compIdx];
170 Scalar oldVal = currentValue[fugacity0Idx + compIdx];
174 Scalar minPhi = this->problem().model().minActivityCoeff(globalDofIdx, compIdx);
176 minPhi = std::max(0.001*currentValue[pressure0Idx], minPhi);
180 Scalar maxDelta = 0.7 * minPhi;
181 clampValue_(val, oldVal - maxDelta, oldVal + maxDelta);
184 val = std::max(val, 0.0);
189 if (this->numIterations_ < 3) {
191 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
192 Scalar& val = nextValue[fugacity0Idx + compIdx];
193 Scalar oldVal = currentValue[fugacity0Idx + compIdx];
194 Scalar minPhi = this->problem().model().minActivityCoeff(globalDofIdx, compIdx);
195 if (oldVal < 1.0*minPhi && val > 1.0*minPhi)
197 else if (oldVal > 0.0 && val < 0.0)
202 for (
unsigned phaseIdx = 0; phaseIdx < numPhases - 1; ++phaseIdx) {
203 Scalar& val = nextValue[saturation0Idx + phaseIdx];
204 Scalar oldVal = currentValue[saturation0Idx + phaseIdx];
205 if (oldVal < 1.0 && val > 1.0)
207 else if (oldVal > 0.0 && val < 0.0)
214 void clampValue_(Scalar& val, Scalar minVal, Scalar maxVal)
const
215 { val = std::max(minVal, std::min(val, maxVal)); }
A Newton solver specific to the NCP model.
Definition: ncpnewtonmethod.hh:55
friend ParentType
Definition: ncpnewtonmethod.hh:82
NcpNewtonMethod(Simulator &simulator)
Definition: ncpnewtonmethod.hh:78
void updatePrimaryVariables_(unsigned globalDofIdx, PrimaryVariables &nextValue, const PrimaryVariables ¤tValue, const EqVector &update, const EqVector &)
Update a single primary variables object.
Definition: ncpnewtonmethod.hh:129
void preSolve_(const SolutionVector &, const GlobalEqVector ¤tResidual)
Definition: ncpnewtonmethod.hh:85
The multi-dimensional Newton method.
Definition: newtonmethod.hh:92
auto Get(bool errorIfNotRegistered=true)
Retrieve a runtime parameter.
Definition: parametersystem.hh:840
Definition: blackoilmodel.hh:72
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
Declares the properties required for the NCP compositional multi-phase model.
Definition: newtonmethodparameters.hh:31