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
37
39
41
42#include <opm/material/common/Valgrind.hpp>
43
44#include <algorithm>
45#include <cmath>
46#include <limits>
47#include <vector>
48
49namespace Opm::Properties {
50
51template <class TypeTag, class MyTypeTag>
52struct DiscNewtonMethod;
53
54} // namespace Opm::Properties
55
56namespace Opm {
57
63template <class TypeTag>
64class BlackOilNewtonMethod : public GetPropType<TypeTag, Properties::DiscNewtonMethod>
65{
76 static constexpr bool enableBioeffects = getPropValue<TypeTag, Properties::EnableBioeffects>();
77 using BioeffectsModule = BlackOilBioeffectsModule<TypeTag, enableBioeffects>;
78
79 static const unsigned numEq = getPropValue<TypeTag, Properties::NumEq>();
80 static constexpr bool enableSaltPrecipitation = getPropValue<TypeTag, Properties::EnableSaltPrecipitation>();
81
82public:
83 explicit BlackOilNewtonMethod(Simulator& simulator) : ParentType(simulator)
84 {
85 bparams_.read();
86 }
87
92 {
93 ParentType::finishInit();
94
95 wasSwitched_.resize(this->model().numTotalDof(), false);
96 }
97
99 {
100 // Preserve the switching history across retries of this timestep.
101 wasSwitchedAtStepStart_ = wasSwitched_;
102 }
103
105 {
106 wasSwitched_ = wasSwitchedAtStepStart_;
107 }
108
112 static void registerParameters()
113 {
114 ParentType::registerParameters();
116 }
117
122 unsigned numPriVarsSwitched() const
123 { return numPriVarsSwitched_; }
124
125protected:
128
133 {
134 numPriVarsSwitched_ = 0;
135 ParentType::beginIteration_();
136 }
137
144 void endIteration_(SolutionVector& uCurrentIter,
145 const SolutionVector& uLastIter)
146 {
147#if HAVE_MPI
148 // in the MPI enabled case we need to add up the number of DOF
149 // for which the interpretation changed over all processes.
150 const int localSwitched = numPriVarsSwitched_;
151 MPI_Allreduce(&localSwitched,
152 &numPriVarsSwitched_,
153 /*num=*/1,
154 MPI_INT,
155 MPI_SUM,
156 MPI_COMM_WORLD);
157#endif // HAVE_MPI
158
159 this->simulator_.model().newtonMethod().endIterMsg()
160 << ", num switched=" << numPriVarsSwitched_;
161
162 ParentType::endIteration_(uCurrentIter, uLastIter);
163 }
164
165public:
168 bool wasSwitched(const unsigned globalDofIdx) const
169 { return wasSwitched_[globalDofIdx]; }
170
171 void setWasSwitched(const unsigned globalDofIdx, const bool switched)
172 { wasSwitched_[globalDofIdx] = switched; }
173
174 void update_(SolutionVector& nextSolution,
175 const SolutionVector& currentSolution,
176 const GlobalEqVector& solutionUpdate,
177 const GlobalEqVector& currentResidual)
178 {
179 const auto& comm = this->simulator_.gridView().comm();
180
181 int succeeded;
182 try {
183 ParentType::update_(nextSolution,
184 currentSolution,
185 solutionUpdate,
186 currentResidual);
187 succeeded = 1;
188 }
189 catch (...) {
190 succeeded = 0;
191 }
192 succeeded = comm.min(succeeded);
193
194 if (!succeeded) {
195 throw NumericalProblem("A process did not succeed in adapting the primary variables");
196 }
197
198 numPriVarsSwitched_ = comm.sum(numPriVarsSwitched_);
199 }
200
201 template <class DofIndices>
202 void update_(SolutionVector& nextSolution,
203 const SolutionVector& currentSolution,
204 const GlobalEqVector& solutionUpdate,
205 const GlobalEqVector& currentResidual,
206 const DofIndices& dofIndices)
207 {
208 const auto zero = 0.0 * solutionUpdate[0];
209 for (auto dofIdx : dofIndices) {
210 if (solutionUpdate[dofIdx] == zero) {
211 continue;
212 }
214 nextSolution[dofIdx],
215 currentSolution[dofIdx],
216 solutionUpdate[dofIdx],
217 currentResidual[dofIdx]);
218 }
219 }
220
221protected:
225 void updatePrimaryVariables_(unsigned globalDofIdx,
226 PrimaryVariables& nextValue,
227 const PrimaryVariables& currentValue,
228 const EqVector& update,
229 const EqVector& currentResidual)
230 {
231 Valgrind::CheckDefined(currentResidual);
232 wasSwitched_[globalDofIdx] = BlackOilNewtonUpdate<TypeTag>::update(
233 this->problem(), FluidSystem{}, globalDofIdx, nextValue, currentValue,
234 update, bparams_, wasSwitched_[globalDofIdx]);
235 if (wasSwitched_[globalDofIdx]) {
236 ++numPriVarsSwitched_;
237 }
238 }
239
240private:
241 int numPriVarsSwitched_{};
242
243 BlackoilNewtonParams<Scalar> bparams_{};
244
245 // keep track of cells where the primary variable meaning has changed
246 // to detect and hinder oscillations
247 std::vector<bool> wasSwitched_{};
248 std::vector<bool> wasSwitchedAtStepStart_{};
249};
250
251} // namespace Opm
252
253#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:65
void beginIteration_()
Indicates the beginning of a Newton iteration.
Definition: blackoilnewtonmethod.hpp:132
bool wasSwitched(const unsigned globalDofIdx) const
Definition: blackoilnewtonmethod.hpp:168
static void registerParameters()
Register all run-time parameters for the blackoil newton method.
Definition: blackoilnewtonmethod.hpp:112
void update_(SolutionVector &nextSolution, const SolutionVector &currentSolution, const GlobalEqVector &solutionUpdate, const GlobalEqVector &currentResidual)
Definition: blackoilnewtonmethod.hpp:174
void finishInit()
Finialize the construction of the object.
Definition: blackoilnewtonmethod.hpp:91
void endIteration_(SolutionVector &uCurrentIter, const SolutionVector &uLastIter)
Indicates that one Newton iteration was finished.
Definition: blackoilnewtonmethod.hpp:144
unsigned numPriVarsSwitched() const
Returns the number of degrees of freedom for which the interpretation has changed for the most recent...
Definition: blackoilnewtonmethod.hpp:122
BlackOilNewtonMethod(Simulator &simulator)
Definition: blackoilnewtonmethod.hpp:83
void updatePrimaryVariables_(unsigned globalDofIdx, PrimaryVariables &nextValue, const PrimaryVariables &currentValue, const EqVector &update, const EqVector &currentResidual)
Update a single primary variables object.
Definition: blackoilnewtonmethod.hpp:225
friend ParentType
Definition: blackoilnewtonmethod.hpp:127
void setWasSwitched(const unsigned globalDofIdx, const bool switched)
Definition: blackoilnewtonmethod.hpp:171
void restorePrimaryVariableSwitches()
Definition: blackoilnewtonmethod.hpp:104
void capturePrimaryVariableSwitches()
Definition: blackoilnewtonmethod.hpp:98
void update_(SolutionVector &nextSolution, const SolutionVector &currentSolution, const GlobalEqVector &solutionUpdate, const GlobalEqVector &currentResidual, const DofIndices &dofIndices)
Definition: blackoilnewtonmethod.hpp:202
The multi-dimensional Newton method.
Definition: newtonmethod.hh:100
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
static OPM_HOST_DEVICE bool update(const Problem &problem, const FluidSystem &fluidSystem, unsigned globalDofIdx, PrimaryVariables &nextValue, const PrimaryVariables &currentValue, const Update &update, const BlackoilNewtonParams< Scalar > &params, bool wasSwitched)
Definition: blackoilnewtonupdate.hpp:55
static void registerParameters()
Registers the parameters in parameter system.