opm-simulators
NonlinearSystem.hpp
1 #ifndef OPM_FLOW_NONLINEAR_SYSTEM_HEADER_INCLUDED
2 #define OPM_FLOW_NONLINEAR_SYSTEM_HEADER_INCLUDED
3 /*
4  Copyright 2026, SINTEF Digital
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <cstddef>
22 
23 #include <opm/input/eclipse/Schedule/Tuning.hpp>
24 
26 
27 #include <opm/simulators/flow/BlackoilModelParameters.hpp>
30 #include <opm/simulators/timestepping/ConvergenceReport.hpp>
31 #include <opm/simulators/timestepping/SimulatorReport.hpp>
32 #include <opm/simulators/timestepping/SimulatorTimerInterface.hpp>
33 #include <opm/simulators/utils/ComponentName.hpp>
34 #include <opm/simulators/utils/ParallelCommunication.hpp>
35 #include <span>
36 #include <string_view>
37 #include <tuple>
38 #include <vector>
39 
40 namespace Opm {
41 
42 template <class TypeTag>
44 {
45 public:
46  // --------- Types ---------
56 
57  // --------- Public methods ---------
58  virtual ~NonlinearSystem() = default;
59 
60  bool isParallel() const
61  { return grid_.comm().size() > 1; }
62 
63  const Simulator& simulator() const
64  { return simulator_; }
65 
66  Simulator& simulator()
67  { return simulator_; }
68 
69  bool terminalOutputEnabled() const
70  { return terminal_output_; }
71 
72  int numPhases() const
73  { return Indices::numPhases; }
74 
75  const SimulatorReportSingle& failureReport() const
76  { return failureReport_; }
77 
78  const std::vector<StepReport>& stepReports() const
79  { return convergence_reports_; }
80 
81  const ComponentName& compNames() const
82  { return compNames_; }
83 
84  const ModelParameters& param() const
85  { return param_; }
86 
87  WellModel& wellModel()
88  { return well_model_; }
89 
90  const WellModel& wellModel() const
91  { return well_model_; }
92 
93  void beginReportStep()
94  { simulator_.problem().beginEpisode(); }
95 
96  void endReportStep()
97  { simulator_.problem().endEpisode(); }
98 
99  SimulatorReportSingle assembleReservoir(const SimulatorTimerInterface& timer);
100 
101  void updateTUNING(const Tuning& tuning);
102 
103  void updateTUNINGDP(const TuningDp& tuning_dp);
104 
105  void updateSolution(const GlobalEqVector& dx);
106 
107  template <class LogFailure>
108  void addReservoirConvergenceMetrics(ConvergenceReport& report,
109  const int componentIdx,
110  const std::string_view componentName,
111  const std::span<const Scalar> residuals,
112  const std::span<const ConvergenceReport::ReservoirFailure::Type> types,
113  const std::span<const Scalar> tolerances,
114  const Scalar maxResidualAllowed,
115  LogFailure&& logFailure) const;
116 
117 protected:
118  explicit NonlinearSystem(Simulator& simulator,
119  const ModelParameters& param,
120  WellModel& wellModel,
121  const bool terminal_output);
122 
123  virtual void initialLinearization(SimulatorReportSingle& report,
124  int minIter,
125  int maxIter,
126  const SimulatorTimerInterface& timer);
127 
128  virtual bool shouldStoreSolutionUpdate() const
129  { return false; }
130 
131  virtual void prepareSolutionUpdate()
132  {}
133 
134  virtual void storeSolutionUpdate(const GlobalEqVector&)
135  {}
136 
137  SimulatorReportSingle prepareStep(const SimulatorTimerInterface& timer);
138 
139  template <class WellModelType>
140  SimulatorReportSingle assembleReservoir(WellModelType& wellModel);
141 
142  template <class ModelParametersType>
143  void applyTUNING(ModelParametersType& param,
144  const Tuning& tuning);
145 
146  template <class ModelParametersType>
147  void applyTUNINGDP(ModelParametersType& param,
148  const TuningDp& tuning_dp);
149 
150  template <class ValueType>
151  std::tuple<ValueType, ValueType>
152  convergenceReduction(Parallel::Communication comm,
153  const ValueType primaryVolumeLocal,
154  const ValueType secondaryVolumeLocal,
155  std::vector<ValueType>& sumValues,
156  std::vector<ValueType>& maxValues,
157  std::vector<ValueType>& averagedValues);
158 
159  void popLastStepReport()
160  { convergence_reports_.back().report.pop_back(); }
161 
162  // --------- Data members ---------
163  Simulator& simulator_;
164  const Grid& grid_;
165  bool terminal_output_;
166  ModelParameters param_;
167  WellModel& well_model_;
168  SimulatorReportSingle failureReport_;
169  std::vector<StepReport> convergence_reports_;
170  ComponentName compNames_{};
171  std::vector<std::vector<Scalar>> residual_norms_history_;
172  Scalar current_relaxation_;
173  GlobalEqVector dx_old_;
174 };
175 
176 } // namespace Opm
177 
178 #include <opm/simulators/flow/NonlinearSystem_impl.hpp>
179 
180 #endif // OPM_FLOW_NONLINEAR_SYSTEM_HEADER_INCLUDED
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
Structs needed for tpfalinearizer and its gpuparams struct extracted to be defined in one place that ...
Definition: blackoilbioeffectsmodules.hh:45
Solver parameters for the NonlinearSystemBlackOilReservoir.
Definition: BlackoilModelParameters.hpp:200
A struct for returning timing data from a simulator to its caller.
Definition: SimulatorReport.hpp:33
Interface class for SimulatorTimer objects, to be improved.
Definition: SimulatorTimerInterface.hpp:33
The Opm property system, traits with inheritance.
Represents the convergence status of the whole simulator, to make it possible to query and store the ...
Definition: ConvergenceReport.hpp:37
Definition: NonlinearSystem.hpp:43
Declares the properties required by the black oil model.
Definition: ComponentName.hpp:33