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 setLogOutputRequest( bool state )
80  { m_logOutputRequest = state; }
81 
82 
87  void setCSVOutputRequest( bool state )
88  { m_CSVOutputRequest = state; }
89 
94  { return m_CSVOutputRequest; }
95 
99  bool getCSVOutputOpened() const
100  { return m_CSVOutputOpened; }
101 
106 
111 
117  void updateNonlinearIteration( integer const numLinearIterations );
118 
124  void accumulateSolverLinearTime( real64 setupTime, real64 solveTime );
125 
130 
136 
142 
147 
152  void setFilename( string_view filename )
153  { m_iterationsFilename = filename; }
154 
155 
159  string const & getFilename() const
160  { return m_iterationsFilename; }
161 
165  string & getFilename()
166  { return m_iterationsFilename; }
167 
168 
174  { m_tableIterationName = name; }
175 
179  void outputStatistics() const;
180 
184  void closeFile()
185  { if( m_CSVOutputOpened ) { m_logStream.close(); m_CSVOutputOpened = false; } }
186 
187 protected:
188 
191 
194 
197 
200 
203 
206 
209 
212 
215 
218 
221 
224 
227 
228 private:
230  bool m_logOutputRequest = false;
232  bool m_CSVOutputRequest = false;
234  bool m_CSVOutputOpened = false;
236  std::ofstream m_logStream;
238  TableLayout m_iterationCSVLayout;
240  TableData m_iterationData;
242  std::unique_ptr< TableCSVFormatter > m_iterationCSVFormatter;
244  string m_tableIterationName;
246  string m_iterationsFilename;
247 };
248 
253 {
254 public:
255 
260 
265  void setCSVOutputRequest( bool state )
266  { m_CSVOutputRequest = state; }
267 
272  { return m_CSVOutputRequest; }
273 
277  bool getCSVOutputOpened() const
278  { return m_CSVOutputOpened; }
279 
284 
292  void updateSolverStep( real64 const & time_n,
293  real64 const & dt,
294  integer const cycleNumber,
295  integer const newtonIter );
296 
302  void setResidualValue( string const & key, real64 const value )
303  { if( m_CSVOutputRequest ) m_residuals.insert( {key, value} ); }
304 
309  void setFilename( string_view filename )
310  { m_convergenceFilename = filename; }
311 
317  { m_tableConvergenceName = name; }
318 
322  string const & getFilename() const
323  { return m_convergenceFilename; }
324 
328  string & getFilename()
329  { return m_convergenceFilename; }
330 
334  void closeFile()
335  { if( m_CSVOutputOpened ) { m_logStream.close(); m_CSVOutputOpened = false; } }
336 
337 private:
339  real64 m_time_n = 0.0;
340 
342  real64 m_dt = 0.0;
343 
345  integer m_cycleNumber = 0;
346 
348  integer m_iteration = 0;
349 
351  stdMap< string, real64 > m_residuals;
352 
354  bool m_CSVOutputRequest = false;
356  bool m_CSVOutputOpened = false;
358  std::ofstream m_logStream;
361  TableLayout m_convergenceLayout;
363  TableData m_convergenceData;
365  std::unique_ptr< TableCSVFormatter > m_convergenceFormatter;
367  string m_tableConvergenceName;
369  string m_convergenceFilename;
370 };
371 
377 {
378 public:
385  SolverStatistics( string const & name,
386  dataRepository::Group * const parent );
387 
392  {
394  static constexpr char const * IterationsStatisticsString() { return "IterationsStatistics"; }
395  };
396 
402  void makeDir( bool writeSolverIteration )
403  { if( writeSolverIteration ) makeDirsForPath( m_outputDir ); }
404 
409  void setOutputFilesName( string_view solverName );
410 
415 
416 protected:
418  string m_outputDir;
419 
420 private:
422  string m_directoryName = "convergence";
423 };
424 
425 } //namespace geos
426 
427 #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 setResidualValue(string const &key, real64 const value)
Set a residual value given a key ( column in the CSV )
void updateSolverStep(real64 const &time_n, real64 const &dt, integer const cycleNumber, integer const newtonIter)
Update the solver step with the time informations.
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 if a file was created.
void setCSVOutputRequest(bool state)
Set the csv output state. Set this to true when a CSV output is requested.
Class containing solver iterations data for a time-step.
void incrementConfigIteration()
Tell the solverStatistics that we have done a configuration iteration.
void outputStatistics() const
Output the statistics to the console in table format.
void setTableName(string_view name)
Set the filename output file.
void setLogOutputRequest(bool state)
Set the log state state request.
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 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.
integer m_numSuccessfulConfigIterations
Cumulative number of successful configuration iterations.
void closeFile()
Close the stream output file if a file was created.
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.
void setCSVOutputRequest(bool state)
Set the csv output state. Set this to true when a CSV output is requested.
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
internal::StdMapWrapper< std::map< Key, T, Compare, Allocator >, USE_STD_CONTAINER_BOUNDS_CHECKING > stdMap
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()