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 
190  integer const & getNumTimeSteps() const { return m_numTimeSteps; }
191 
192 protected:
193 
196 
199 
202 
205 
208 
211 
214 
217 
220 
223 
226 
229 
232 
233 private:
235  bool m_logOutputRequest = false;
237  bool m_CSVOutputRequest = false;
239  bool m_CSVOutputOpened = false;
241  std::ofstream m_logStream;
243  TableLayout m_iterationCSVLayout;
245  TableData m_iterationData;
247  std::unique_ptr< TableCSVFormatter > m_iterationCSVFormatter;
249  string m_tableIterationName;
251  string m_iterationsFilename;
252 };
253 
258 {
259 public:
260 
265 
270  void setCSVOutputRequest( bool state )
271  { m_CSVOutputRequest = state; }
272 
277  { return m_CSVOutputRequest; }
278 
282  bool getCSVOutputOpened() const
283  { return m_CSVOutputOpened; }
284 
289 
297  void updateSolverStep( real64 const & time_n,
298  real64 const & dt,
299  integer const cycleNumber,
300  integer const newtonIter );
301 
307  void setResidualValue( string const & key, real64 const value )
308  { if( m_CSVOutputRequest ) m_residuals.get_inserted( key ) = value; }
309 
314  void setFilename( string_view filename )
315  { m_convergenceFilename = filename; }
316 
322  { m_tableConvergenceName = name; }
323 
327  string const & getFilename() const
328  { return m_convergenceFilename; }
329 
333  string & getFilename()
334  { return m_convergenceFilename; }
335 
339  void closeFile()
340  { if( m_CSVOutputOpened ) { m_logStream.close(); m_CSVOutputOpened = false; } }
341 
342 private:
344  real64 m_time_n = 0.0;
345 
347  real64 m_dt = 0.0;
348 
350  integer m_cycleNumber = 0;
351 
353  integer m_iteration = 0;
354 
356  stdMap< string, real64 > m_residuals;
357 
359  bool m_CSVOutputRequest = false;
361  bool m_CSVOutputOpened = false;
363  std::ofstream m_logStream;
366  TableLayout m_convergenceLayout;
368  TableData m_convergenceData;
370  std::unique_ptr< TableCSVFormatter > m_convergenceFormatter;
372  string m_tableConvergenceName;
374  string m_convergenceFilename;
375 };
376 
382 {
383 public:
390  SolverStatistics( string const & name,
391  dataRepository::Group * const parent );
392 
397  {
399  static constexpr char const * IterationsStatisticsString() { return "IterationsStatistics"; }
400  };
401 
407  void makeDir( bool writeSolverIteration )
408  { if( writeSolverIteration ) makeDirsForPath( m_outputDir ); }
409 
414  void setOutputFilesName( string_view solverName );
415 
420 
421 protected:
423  string m_outputDir;
424 
425 private:
427  string m_directoryName = "convergence";
428 };
429 
430 } //namespace geos
431 
432 #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.
integer const & getNumTimeSteps() const
Get the number of time steps.
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:34
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()