flashnewtonmethod.hh
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_FLASH_NEWTON_METHOD_HH
29#define OPM_FLASH_NEWTON_METHOD_HH
30
31#include <opm/common/Exceptions.hpp>
32
36
37#include <algorithm>
38#include <array>
39
40namespace Opm::Properties {
41
42template <class TypeTag, class MyTypeTag>
43struct DiscNewtonMethod;
44
45} // namespace Opm::Properties
46
47namespace Opm {
48
54template <class TypeTag>
55class FlashNewtonMethod : public GetPropType<TypeTag, Properties::DiscNewtonMethod>
56{
58
65
66 enum { pressure0Idx = Indices::pressure0Idx };
67 enum { z0Idx = Indices::z0Idx };
68 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
69
70 static constexpr bool waterEnabled = Indices::waterEnabled;
71
72public:
76 explicit FlashNewtonMethod(Simulator& simulator) : ParentType(simulator)
77 {}
78
79protected:
80 friend ParentType;
82
86 void updatePrimaryVariables_(unsigned /* globalDofIdx */,
87 PrimaryVariables& nextValue,
88 const PrimaryVariables& currentValue,
89 const EqVector& update,
90 const EqVector& /* currentResidual */)
91 {
92 // nextValue may alias currentValue. Preserve the pre-update value because the
93 // limiters below must chop relative to it after nextValue has been updated.
94 const PrimaryVariables priVarsOld = currentValue;
95
96 // normal Newton-Raphson update
97 nextValue = priVarsOld;
98 nextValue -= update;
99
101 // Pressure updates
103 // limit pressure reference change relative to the total value per iteration
104 constexpr Scalar max_percent_change = 0.2;
105 constexpr Scalar upper_bound = 1. + max_percent_change;
106 constexpr Scalar lower_bound = 1. - max_percent_change;
107 nextValue[pressure0Idx] = std::clamp(nextValue[pressure0Idx],
108 priVarsOld[pressure0Idx] * lower_bound,
109 priVarsOld[pressure0Idx] * upper_bound);
110
112 // Composition and water saturation updates
114 // Steps are new minus old values. The last fraction is dependent: it and its step
115 // complete the sums of z and dz to one and zero.
116 std::array<Scalar, numComponents> z{};
117 std::array<Scalar, numComponents> dz{};
118 z.back() = 1.0;
119 for (unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
120 z[compIdx] = priVarsOld[z0Idx + compIdx];
121 dz[compIdx] = -update[z0Idx + compIdx];
122 z.back() -= z[compIdx];
123 dz.back() -= dz[compIdx];
124 }
125
126 Scalar sw = 0.0;
127 Scalar dSw = 0.0;
128 if constexpr (waterEnabled) {
129 sw = priVarsOld[Indices::water0Idx];
130 dSw = -update[Indices::water0Idx];
131 }
132
133 constexpr Scalar maxAmountChange = 0.2;
134 applyFlashCompositionStep(z, sw, dz, dSw,
135 IntensiveQuantities::compositionFloor,
136 IntensiveQuantities::hydrocarbonFloor,
137 maxAmountChange);
138
139 for (unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
140 nextValue[z0Idx + compIdx] = z[compIdx];
141 }
142 if constexpr (waterEnabled) {
143 nextValue[Indices::water0Idx] = sw;
144 }
145 }
146}; // class FlashNewtonMethod
147
148} // namespace Opm
149
150#endif
A Newton solver specific to the PTFlash model.
Definition: flashnewtonmethod.hh:56
FlashNewtonMethod(Simulator &simulator)
Definition: flashnewtonmethod.hh:76
void updatePrimaryVariables_(unsigned, PrimaryVariables &nextValue, const PrimaryVariables &currentValue, const EqVector &update, const EqVector &)
Update a single primary variables object.
Definition: flashnewtonmethod.hh:86
friend ParentType
Definition: flashnewtonmethod.hh:80
The multi-dimensional Newton method.
Definition: newtonmethod.hh:100
Defines the common properties required by the porous medium multi-phase models.
Definition: blackoilmodel.hh:74
Definition: blackoilbioeffectsmodules.hh:45
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
void applyFlashCompositionStep(std::array< Scalar, numComponents > &z, Scalar &sw, const std::array< Scalar, numComponents > &dz, const Scalar dSw, const Scalar compositionFloor, const Scalar hydrocarbonFloor, const Scalar maxAmountChange)
Applies a Newton step to the overall composition and the water saturation.
Definition: flashcompositionstep.hh:139