NewtonIterationContext.hpp
Go to the documentation of this file.
1/*
2 Copyright 2026 SINTEF AS.
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 3 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
20#ifndef OPM_NEWTON_ITERATION_CONTEXT_HPP
21#define OPM_NEWTON_ITERATION_CONTEXT_HPP
22
23#include <cassert>
24
25namespace Opm {
26
44
46
48
52 int iteration() const
53 {
54 return inLocalSolve_ ? localIteration_ : globalIteration_;
55 }
56
58 bool inLocalSolve() const { return inLocalSolve_; }
59
61 bool timestepInitialized() const { return timestepInitialized_; }
62
64
67 bool needsTimestepInit() const
68 {
69 return !timestepInitialized_ && !inLocalSolve_;
70 }
71
75 {
76 return globalIteration_ == 0 && !inLocalSolve_;
77 }
78
81 bool withinNupcol(int nupcol) const
82 {
83 return globalIteration_ < nupcol;
84 }
85
88 bool shouldRelax(int strictIterations) const
89 {
90 return iteration() >= strictIterations;
91 }
92
95 bool shouldRunInnerWellIterations(int maxIter) const
96 {
97 if (inLocalSolve_) return false;
98 return globalIteration_ < maxIter;
99 }
100
102
105 {
106 timestepInitialized_ = true;
107 }
108
113 {
114 if (inLocalSolve_) {
115 localIteration_++;
116 } else {
117 globalIteration_++;
118 }
119 }
120
123 {
124 globalIteration_ = 0;
125 localIteration_ = 0;
126 timestepInitialized_ = false;
127 inLocalSolve_ = false;
128 }
129
133 {
134 return {globalIteration_, 0, /*inLocalSolve=*/true, /*timestepInitialized=*/true};
135 }
136
137private:
138 NewtonIterationContext(int globalIter, int localIter, bool localSolve, bool tsInit)
139 : globalIteration_(globalIter), localIteration_(localIter)
140 , inLocalSolve_(localSolve), timestepInitialized_(tsInit)
141 {}
142 int globalIteration_ = 0;
143 int localIteration_ = 0;
144 bool inLocalSolve_ = false;
145 bool timestepInitialized_ = false;
146};
147
151template<class Problem>
153public:
154 LocalContextGuard(Problem& problem)
155 : problem_(problem)
156 , saved_(problem.iterationContext())
157 {
158 problem_.mutableIterationContext() = saved_.forLocalSolve();
159 }
160
162 {
163 problem_.mutableIterationContext() = saved_;
164 }
165
170
171 NewtonIterationContext& context() { return problem_.mutableIterationContext(); }
172 const NewtonIterationContext& context() const { return problem_.iterationContext(); }
173
174private:
175 Problem& problem_;
177};
178
179} // namespace Opm
180
181#endif // OPM_NEWTON_ITERATION_CONTEXT_HPP
Definition: NewtonIterationContext.hpp:152
NewtonIterationContext & context()
Definition: NewtonIterationContext.hpp:171
~LocalContextGuard()
Definition: NewtonIterationContext.hpp:161
LocalContextGuard & operator=(LocalContextGuard &&)=delete
const NewtonIterationContext & context() const
Definition: NewtonIterationContext.hpp:172
LocalContextGuard(const LocalContextGuard &)=delete
LocalContextGuard(LocalContextGuard &&)=delete
LocalContextGuard(Problem &problem)
Definition: NewtonIterationContext.hpp:154
LocalContextGuard & operator=(const LocalContextGuard &)=delete
Definition: blackoilbioeffectsmodules.hh:45
Context for iteration-dependent decisions in the Newton solver.
Definition: NewtonIterationContext.hpp:43
bool isFirstGlobalIteration() const
Definition: NewtonIterationContext.hpp:74
void markTimestepInitialized()
State Mutations.
Definition: NewtonIterationContext.hpp:104
bool inLocalSolve() const
Whether we are inside a domain-local solve (NLDD).
Definition: NewtonIterationContext.hpp:58
void advanceIteration()
Definition: NewtonIterationContext.hpp:112
NewtonIterationContext forLocalSolve() const
Definition: NewtonIterationContext.hpp:132
bool withinNupcol(int nupcol) const
Definition: NewtonIterationContext.hpp:81
bool needsTimestepInit() const
Semantic Queries.
Definition: NewtonIterationContext.hpp:67
bool timestepInitialized() const
Whether timestep initialization has been performed.
Definition: NewtonIterationContext.hpp:61
int iteration() const
Getters.
Definition: NewtonIterationContext.hpp:52
void resetForNewTimestep()
Reset all state for a new timestep.
Definition: NewtonIterationContext.hpp:122
bool shouldRunInnerWellIterations(int maxIter) const
Definition: NewtonIterationContext.hpp:95
bool shouldRelax(int strictIterations) const
Definition: NewtonIterationContext.hpp:88