GEOS
SolverStatistics.hpp
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
16 /*
17  * @file SolverStatistics.hpp
18  */
19 
20 
21 #ifndef GEOS_PHYSICSSOLVERS_SOLVERSTATISTICS_HPP
22 #define GEOS_PHYSICSSOLVERS_SOLVERSTATISTICS_HPP
23 
24 #include "dataRepository/Group.hpp"
28 #include "common/format/table/TableTypes.hpp"
29 
30 namespace geos
31 {
32 
37 {
38 public:
39 
46  IterationsStatistics( string const & name,
47  dataRepository::Group * const parent );
48 
54  {
56  static constexpr char const * numTimeStepsString() { return "numTimeSteps"; }
58  static constexpr char const * numTimeStepCutsString() { return "numTimeStepCuts"; }
59 
61  static constexpr char const * numSuccessfulConfigIterationsString() { return "numSuccessfulConfigIterations"; }
63  static constexpr char const * numSuccessfulNonlinearIterationsString() { return "numSuccessfulNonlinearIterations"; }
65  static constexpr char const * numSuccessfulLinearIterationsString() { return "numSuccessfulLinearIterations"; }
66 
68  static constexpr char const * numDiscardedConfigIterationsString() { return "numDiscardedConfigIterations"; }
70  static constexpr char const * numDiscardedNonlinearIterationsString() { return "numDiscardedNonlinearIterations"; }
72  static constexpr char const * numDiscardedLinearIterationsString() { return "numDiscardedLinearIterations"; }
73  };
74 
79  void setCSVOutputState( bool state )
80  { m_csvOutputEnabled = state; }
81 
86  void setLogOutputState( bool state )
87  { m_logOutput = state; }
88 
89 
94 
99 
105  void updateNonlinearIteration( integer const numLinearIterations );
106 
112  void accumulateSolverLinearTime( real64 setupTime, real64 solveTime );
113 
118 
124 
130 
135 
140  void setFilename( string_view filename )
141  { m_iterationsFilename = filename; }
142 
143 
147  string const & getFilename() const
148  { return m_iterationsFilename; }
149 
153  string & getFilename()
154  { return m_iterationsFilename; }
155 
156 
162  { m_tableIterationName = name; }
163 
168 
172  void closeFile()
173  { if( m_csvOutputOpened ) { m_logStream.close(); m_csvOutputOpened = false; } }
174 
175 protected:
176 
179 
182 
185 
188 
191 
194 
197 
200 
203 
206 
209 
212 
215 
216 private:
218  bool m_logOutput = false;
220  bool m_csvOutputEnabled = false;
222  bool m_csvOutputOpened = false;
224  std::ofstream m_logStream;
226  TableLayout m_iterationCSVLayout;
228  TableData m_iterationData;
230  std::unique_ptr< TableCSVFormatter > m_iterationCSVFormatter;
232  string m_tableIterationName;
234  string m_iterationsFilename;
235 };
236 
241 {
242 public:
243 
248 
250  bool m_csvOutputEnabled = false;
251 
256  void setCSVOutputState( bool state )
257  { m_csvOutputEnabled = state; }
258 
263 
271  void updateSolverStep( real64 const & time_n,
272  real64 const & dt,
273  integer const cycleNumber,
274  integer const newtonIter );
275 
276  void setResidualValue( string const & key, real64 const value )
277  { if( m_csvOutputEnabled ) m_residuals[key] = value; }
278 
283  void setFilename( string_view filename )
284  { m_convergenceFilename = filename; }
285 
291  { m_tableConvergenceName = name; }
292 
296  string const & getFilename() const
297  { return m_convergenceFilename; }
298 
302  string & getFilename()
303  { return m_convergenceFilename; }
304 
308  void closeFile()
309  { if( m_csvOutputOpened ) { m_logStream.close(); m_csvOutputOpened = false; } }
310 
311 private:
313  real64 m_time_n = 0.0;
314 
316  real64 m_dt = 0.0;
317 
319  integer m_cycleNumber = 0;
320 
322  integer m_iteration = 0;
323 
325  std::map< string, real64 > m_residuals;
326 
328  bool m_csvOutputOpened = false;
330  std::ofstream m_logStream;
333  TableLayout m_convergenceLayout;
335  TableData m_convergenceData;
337  std::unique_ptr< TableCSVFormatter > m_convergenceFormatter;
339  string m_tableConvergenceName;
341  string m_convergenceFilename;
342 };
343 
349 {
350 public:
357  SolverStatistics( string const & name,
358  dataRepository::Group * const parent );
359 
364  {
366  static constexpr char const * IterationsStatisticsString() { return "IterationsStatistics"; }
367  };
368 
374  void makeDir( bool writeSolverIteration )
375  { if( writeSolverIteration ) makeDirsForPath( m_outputDir ); }
376 
381  void setOutputFilesName( string_view solverName );
382 
387 
388 protected:
390  string m_outputDir;
391 
392 private:
394  string m_directoryName = "convergence";
395 };
396 
397 } //namespace geos
398 
399 #endif // GEOS_PHYSICSSOLVERS_SOLVERSTATISTICS_HPP
Class containing convergence information given a time-step.
void setFilename(string_view filename)
Set the filename output file.
string & getFilename()
A string reference to a convergence filename.
void writeConvergenceStatsToTable()
Write all the convergence statistics into the ouput stream.
void updateSolverStep(real64 const &time_n, real64 const &dt, integer const cycleNumber, integer const newtonIter)
Update the solver step with the time informations.
bool m_csvOutputEnabled
State of csv output. True when writeSolver is set to 2.
ConvergenceStatistics()
Construct a new Convergence Statistics object.
string const & getFilename() const
void setTableName(string_view name)
Set the filename output file.
void closeFile()
Close the stream output file.
void setCSVOutputState(bool state)
Set the csv state output.
Class containing solver iterations data for a time-step.
void incrementConfigIteration()
Tell the solverStatistics that we have done a configuration iteration.
void setTableName(string_view name)
Set the filename output file.
integer m_numTimeStepCuts
Number of time step cuts.
integer m_currentNumConfigIterations
Number of configuration iterations in the current time step (utility variable constantly overwritten)
void outputStatistics()
Output the statistics to the console in table format.
void resetSolverLinearTime()
Reset the setupTime & solveTime to 0 at the end of each cycle.
void accumulateSolverLinearTime(real64 setupTime, real64 solveTime)
Accumulate the setupTime & solveTime result over each newton iteration.
integer m_numDiscardedConfigIterations
Cumulative number of discarded configuration iterations.
integer m_currentNumLinearIterations
Number of linear iterations in the current time step (utility variable constantly overwritten)
void iterateTimeStepStatistics()
Increment the cumulative number of nonlinear and linear iterations This fonction must be set after a ...
void updateNonlinearIteration(integer const numLinearIterations)
Tell the solverStatistics that we have done a newton iteration.
string const & getFilename() const
void writeIterationStatsToTable()
Write all the iteration statistics into the ouput stream.
integer m_numTimeSteps
Number of time steps.
void setCSVOutputState(bool state)
Set the csv state output.
void setLogOutputState(bool state)
Set the log state output.
integer m_numSuccessfulConfigIterations
Cumulative number of successful configuration iterations.
void closeFile()
Close the stream output file.
integer m_numDiscardedNonlinearIterations
Cumulative number of discarded nonlinear iterations.
integer m_numSuccessfulLinearIterations
Cumulative number of successful linear iterations.
void updateTimeStepCut()
Tell the solverStatistics that we cut the time step and we increment the cumulative counters for disc...
integer m_numSuccessfulNonlinearIterations
Cumulative number of successful nonlinear iterations.
real64 m_setupTime
Linear solver setup.
integer m_numDiscardedLinearIterations
Cumulative number of discarded linear iterations.
IterationsStatistics(string const &name, dataRepository::Group *const parent)
Constructor for SolverStatistics Objects.
void setFilename(string_view filename)
Set the filename output file.
integer m_currentNumNonlinearIterations
Number of nonlinear iterations in the current time step (utility variable constantly overwritten)
string & getFilename()
A string reference to an iteration filename.
void resetCurrentTimeStepStatistics()
Reset the counters used for an individual time step.
real64 m_solveTime
Linear solver solve.
This class records solver statistics for each time step.
void setOutputFilesName(string_view solverName)
Set the Residual Norms filename.
SolverStatistics(string const &name, dataRepository::Group *const parent)
Constructor for SolverStatistics Objects.
void makeDir(bool writeSolverIteration)
Create a convergence directory if we enable the csv for iteration or convergence statistics.
IterationsStatistics m_iterationsStats
Contain iteration data given a time step.
string m_outputDir
Output directory for solver statistics (CSV), passed in the constructor.
ConvergenceStatistics m_convergenceStats
Contain convergence data given a time step.
Class for managing table data.
Definition: TableData.hpp:35
Class for setup the table layout.
Definition: TableLayout.hpp:36
void makeDirsForPath(std::string const &path)
Make directories for path.
double real64
64-bit floating point type.
Definition: DataTypes.hpp:98
int integer
Signed integer type.
Definition: DataTypes.hpp:81
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
Struct to serve as a container for variable strings and keys.
static constexpr char const * numDiscardedLinearIterationsString()
String key for the discarded number of linear iterations.
static constexpr char const * numDiscardedNonlinearIterationsString()
String key for the discarded number of nonlinear iterations.
static constexpr char const * numSuccessfulConfigIterationsString()
String key for the successful number of configuration iterations.
static constexpr char const * numTimeStepCutsString()
String key for the number of time step cuts.
static constexpr char const * numSuccessfulNonlinearIterationsString()
String key for the successful number of nonlinear iterations.
static constexpr char const * numDiscardedConfigIterationsString()
String key for the discarded number of configuration iterations.
static constexpr char const * numSuccessfulLinearIterationsString()
String key for the successful number of linear iterations.
static constexpr char const * numTimeStepsString()
String key for the number of time steps.
Group key associated with IterationsStatistics.
static constexpr char const * IterationsStatisticsString()