opm-simulators
NewtonIterationContext.hpp
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 
25 namespace Opm {
26 
44 
45  NewtonIterationContext() = default;
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 
137 private:
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 
151 template<class Problem>
153 public:
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 
166  LocalContextGuard(const LocalContextGuard&) = delete;
167  LocalContextGuard& operator=(const LocalContextGuard&) = delete;
169  LocalContextGuard& operator=(LocalContextGuard&&) = delete;
170 
171  NewtonIterationContext& context() { return problem_.mutableIterationContext(); }
172  const NewtonIterationContext& context() const { return problem_.iterationContext(); }
173 
174 private:
175  Problem& problem_;
176  NewtonIterationContext saved_;
177 };
178 
179 } // namespace Opm
180 
181 #endif // OPM_NEWTON_ITERATION_CONTEXT_HPP
Context for iteration-dependent decisions in the Newton solver.
Definition: NewtonIterationContext.hpp:43
bool shouldRunInnerWellIterations(int maxIter) const
Whether inner well iterations should run.
Definition: NewtonIterationContext.hpp:95
NewtonIterationContext forLocalSolve() const
Create a context for a domain-local solve.
Definition: NewtonIterationContext.hpp:132
bool timestepInitialized() const
Whether timestep initialization has been performed.
Definition: NewtonIterationContext.hpp:61
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
RAII guard for NLDD domain-local iteration context.
Definition: NewtonIterationContext.hpp:152
void resetForNewTimestep()
Reset all state for a new timestep.
Definition: NewtonIterationContext.hpp:122
bool needsTimestepInit() const
Semantic Queries.
Definition: NewtonIterationContext.hpp:67
bool isFirstGlobalIteration() const
Is this the first iteration of the global solve (not a local solve)? Use for one-time-per-timestep lo...
Definition: NewtonIterationContext.hpp:74
bool withinNupcol(int nupcol) const
Are we within the NUPCOL iteration window? Always uses global iteration regardless of local solve sta...
Definition: NewtonIterationContext.hpp:81
int iteration() const
Getters.
Definition: NewtonIterationContext.hpp:52
void advanceIteration()
Advance the current iteration counter.
Definition: NewtonIterationContext.hpp:112
void markTimestepInitialized()
State Mutations.
Definition: NewtonIterationContext.hpp:104
bool shouldRelax(int strictIterations) const
Should tolerances be relaxed based on iteration count? Uses local iteration for local solves...
Definition: NewtonIterationContext.hpp:88
bool inLocalSolve() const
Whether we are inside a domain-local solve (NLDD).
Definition: NewtonIterationContext.hpp:58