opm-simulators
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 
35 
36 #include <algorithm>
37 #include <cmath>
38 
39 namespace Opm::Properties {
40 
41 template <class TypeTag, class MyTypeTag>
42 struct DiscNewtonMethod;
43 
44 } // namespace Opm::Properties
45 
46 namespace Opm {
47 
53 template <class TypeTag>
54 class FlashNewtonMethod : public GetPropType<TypeTag, Properties::DiscNewtonMethod>
55 {
57 
63 
64  enum { pressure0Idx = Indices::pressure0Idx };
65  enum { z0Idx = Indices::z0Idx };
66  enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
67 
68  static constexpr bool waterEnabled = Indices::waterEnabled;
69 
70 public:
74  explicit FlashNewtonMethod(Simulator& simulator) : ParentType(simulator)
75  {}
76 
77 protected:
78  friend ParentType;
79  friend NewtonMethod<TypeTag>;
80 
84  void updatePrimaryVariables_(unsigned /* globalDofIdx */,
85  PrimaryVariables& nextValue,
86  const PrimaryVariables& currentValue,
87  const EqVector& update,
88  const EqVector& /* currentResidual */)
89  {
90  // normal Newton-Raphson update
91  nextValue = currentValue;
92  nextValue -= update;
93 
95  // Pressure updates
97  // limit pressure reference change relative to the total value per iteration
98  constexpr Scalar max_percent_change = 0.2;
99  constexpr Scalar upper_bound = 1. + max_percent_change;
100  constexpr Scalar lower_bound = 1. - max_percent_change;
101  nextValue[pressure0Idx] = std::clamp(nextValue[pressure0Idx],
102  currentValue[pressure0Idx] * lower_bound,
103  currentValue[pressure0Idx] * upper_bound);
104 
106  // z updates
108  // restrict update
109  Scalar maxDeltaZ = 0.0; // in update vector
110  Scalar sumDeltaZ = 0.0; // changes in last component (not in update vector)
111  for (unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
112  maxDeltaZ = std::max(std::abs(update[z0Idx + compIdx]), maxDeltaZ);
113  sumDeltaZ += update[z0Idx + compIdx];
114  }
115  maxDeltaZ = std::max(std::abs(sumDeltaZ), maxDeltaZ);
116 
117  // if max. update is above limit, restrict that one to limit and adjust the rest
118  // accordingly (s.t. last comp. update is sum of the changes in update vector)
119  // \Note: original code uses 0.1, while 0.1 looks like having problem make it converged.
120  // So there is some more to investigate here
121  constexpr Scalar deltaz_limit = 0.2;
122  if (maxDeltaZ > deltaz_limit) {
123  const Scalar alpha = deltaz_limit / maxDeltaZ;
124  for (unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
125  nextValue[z0Idx + compIdx] = currentValue[z0Idx + compIdx] - alpha * update[z0Idx + compIdx];
126  }
127  }
128 
129  // ensure that z-values are less than tol or more than 1-tol
130  constexpr Scalar tol = 1e-8;
131  for (unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
132  nextValue[z0Idx + compIdx] = std::clamp(nextValue[z0Idx + compIdx], tol, 1-tol);
133  }
134 
135  if constexpr (waterEnabled) {
136  // limit change in water saturation
137  constexpr Scalar dSwMax = 0.2;
138  if (update[Indices::water0Idx] > dSwMax) {
139  nextValue[Indices::water0Idx] = currentValue[Indices::water0Idx] - dSwMax;
140  }
141  }
142  }
143 }; // class FlashNewtonMethod
144 
145 } // namespace Opm
146 
147 #endif
The multi-dimensional Newton method.
The multi-dimensional Newton method.
Definition: newtonmethod.hh:64
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
Defines the common properties required by the porous medium multi-phase models.
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
FlashNewtonMethod(Simulator &simulator)
Definition: flashnewtonmethod.hh:74
void updatePrimaryVariables_(unsigned, PrimaryVariables &nextValue, const PrimaryVariables &currentValue, const EqVector &update, const EqVector &)
Update a single primary variables object.
Definition: flashnewtonmethod.hh:84
Definition: blackoilmodel.hh:80
A Newton solver specific to the PTFlash model.
Definition: flashnewtonmethod.hh:54