OGS
OGSSimulation Class Reference

Detailed Description

Definition at line 51 of file ogs_python_module.cpp.

Public Member Functions

 OGSSimulation (std::vector< std::string > &argv_str)
int executeSimulation ()
int executeTimeStep ()
double currentTime () const
double endTime () const
OGSMeshgetMesh (std::string const &name)
std::vector< std::string > getMeshNames () const
void finalize ()
int status () const
bool initialized () const

Private Attributes

int ogs_status = EXIT_SUCCESS
std::unique_ptr< Simulationsimulation
std::map< std::string, OGSMeshmesh_mapping
std::optional< BaseLib::MPI::Setupmpi_setup

Constructor & Destructor Documentation

◆ OGSSimulation()

OGSSimulation::OGSSimulation ( std::vector< std::string > & argv_str)
inlineexplicit

Definition at line 54 of file ogs_python_module.cpp.

55 {
56 auto [argc, argv_vec] = toArgcArgv(argv_str);
57 char** argv = argv_vec.data();
58
59 mpi_setup.emplace(argc, argv);
60
61 CommandLineArguments cli_args;
62 try
63 {
64 cli_args = parseCommandLineArguments(argc, argv, false);
65 }
66 catch (TCLAP::ArgException const& e)
67 {
68 // TODO fragile interplay between (incomplete) simulation
69 // initialization and OGS logger initialization
71
72 std::cerr << "Parsing the OGS commandline failed: " << e.what()
73 << '\n';
74
75 // "mangle" TCLAP's status
76 throw(e);
77 }
78 catch (TCLAP::ExitException const& e)
79 {
80 // TODO fragile interplay between (incomplete) simulation
81 // initialization and OGS logger initialization
83
84 if (e.getExitStatus() == 0)
85 {
86 // --version/--help
87 return;
88 }
89
90 throw(e);
91 }
92
94
95 DBUG("OGSSimulation::OGSSimulation(std::vector<std::string>&)");
96
97 INFO(
98 "This is OpenGeoSys-6 version {:s}. Log version: {:d}, Log level: "
99 "{:s}.",
101
103
104 {
105 auto const start_time = std::chrono::system_clock::now();
106 auto const time_str = BaseLib::formatDate(start_time);
107 // todo ask Tobias: started vs starts
108 INFO("OGS starts on {:s} in serial mode / Python embedded mode.",
109 time_str);
110 }
111 try
112 {
113 simulation = std::make_unique<Simulation>(argc, argv);
114 simulation->initializeDataStructures(
115 std::move(cli_args.project),
116 std::move(cli_args.xml_patch_file_names),
117 cli_args.reference_path_is_set,
118 std::move(cli_args.reference_path), cli_args.nonfatal,
119 std::move(cli_args.outdir), std::move(cli_args.mesh_dir),
120 std::move(cli_args.script_dir), cli_args.write_prj);
121 }
122 catch (std::exception& e)
123 {
124 ERR("{}", e.what());
125 ogs_status = EXIT_FAILURE;
126 simulation.reset();
127 throw(e);
128 }
129 INFO("OpenGeoSys is now initialized.");
130 }
CommandLineArguments parseCommandLineArguments(int argc, char *argv[], bool const exit_on_exception)
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::optional< BaseLib::MPI::Setup > mpi_setup
std::unique_ptr< Simulation > simulation
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
std::string formatDate(std::chrono::time_point< std::chrono::system_clock > const &time)
std::string defaultLogLevel()
bool createOutputDirectory(std::string const &dir)
GITINFOLIB_EXPORT const std::string ogs_version
std::pair< int, std::vector< char * > > toArgcArgv(std::vector< std::string > &argv_str)
std::vector< std::string > xml_patch_file_names

References BaseLib::createOutputDirectory(), DBUG(), BaseLib::defaultLogLevel(), ERR(), BaseLib::formatDate(), INFO(), BaseLib::initOGSLogger(), CommandLineArguments::log_level, CommandLineArguments::mesh_dir, mpi_setup, CommandLineArguments::nonfatal, ogs_status, GitInfoLib::GitInfo::ogs_version, CommandLineArguments::outdir, parseCommandLineArguments(), CommandLineArguments::project, CommandLineArguments::reference_path, CommandLineArguments::reference_path_is_set, CommandLineArguments::script_dir, simulation, toArgcArgv(), CommandLineArguments::write_prj, and CommandLineArguments::xml_patch_file_names.

Member Function Documentation

◆ currentTime()

double OGSSimulation::currentTime ( ) const
inline

Definition at line 200 of file ogs_python_module.cpp.

201 {
203 return simulation->currentTime();
204 }
bool initialized() const
#define OGS_ALWAYS_ASSERT(cond)

References initialized(), OGS_ALWAYS_ASSERT, and simulation.

Referenced by PYBIND11_MODULE().

◆ endTime()

double OGSSimulation::endTime ( ) const
inline

Definition at line 206 of file ogs_python_module.cpp.

207 {
209 return simulation->endTime();
210 }

References initialized(), OGS_ALWAYS_ASSERT, and simulation.

Referenced by PYBIND11_MODULE().

◆ executeSimulation()

int OGSSimulation::executeSimulation ( )
inline

Definition at line 132 of file ogs_python_module.cpp.

133 {
135
136 BaseLib::RunTime run_time;
137
138 {
139 auto const start_time = std::chrono::system_clock::now();
140 auto const time_str = BaseLib::formatDate(start_time);
141 INFO("OGS started on {:s} in serial mode.", time_str);
142 }
143
144 std::optional<ApplicationsLib::TestDefinition> test_definition{
145 std::nullopt};
146
147 try
148 {
149 run_time.start();
150 bool solver_succeeded = simulation->executeSimulation();
151 simulation->outputLastTimeStep();
152 test_definition = simulation->getTestDefinition();
153
154 if (solver_succeeded)
155 {
156 INFO("[time] Simulation completed. It took {:g} s.",
157 run_time.elapsed());
158 }
159 else
160 {
161 INFO("[time] Simulation failed. It took {:g} s.",
162 run_time.elapsed());
163 }
164 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
165 }
166 catch (std::exception& e)
167 {
168 ERR("{}", e.what());
169 ogs_status = EXIT_FAILURE;
170 }
171
172 if (ogs_status == EXIT_FAILURE)
173 {
174 auto const end_time = std::chrono::system_clock::now();
175 auto const time_str = BaseLib::formatDate(end_time);
176 ERR("OGS terminated with error on {:s}.", time_str);
177 return EXIT_FAILURE;
178 }
179
180 return Simulation::runTestDefinitions(test_definition);
181 }
double elapsed() const
Get the elapsed time in seconds.
Definition RunTime.h:31
void start()
Start the timer.
Definition RunTime.h:21
static OGS_EXPORT_SYMBOL int runTestDefinitions(std::optional< ApplicationsLib::TestDefinition > &test_definition)

References BaseLib::RunTime::elapsed(), ERR(), BaseLib::formatDate(), INFO(), initialized(), OGS_ALWAYS_ASSERT, ogs_status, Simulation::runTestDefinitions(), simulation, and BaseLib::RunTime::start().

Referenced by PYBIND11_MODULE().

◆ executeTimeStep()

int OGSSimulation::executeTimeStep ( )
inline

Definition at line 183 of file ogs_python_module.cpp.

184 {
186
187 try
188 {
189 bool solver_succeeded = simulation->executeTimeStep();
190 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
191 }
192 catch (std::exception& e)
193 {
194 ERR("{}", e.what());
195 ogs_status = EXIT_FAILURE;
196 }
197 return ogs_status;
198 }

References ERR(), initialized(), OGS_ALWAYS_ASSERT, ogs_status, and simulation.

Referenced by PYBIND11_MODULE().

◆ finalize()

void OGSSimulation::finalize ( )
inline

Definition at line 242 of file ogs_python_module.cpp.

243 {
244 if (simulation)
245 {
246 simulation->outputLastTimeStep();
247 simulation.reset(nullptr);
248 }
249 mpi_setup.reset();
250
251 // Check for swallowed ConfigTree errors after Simulation destructor
252 // runs. This catches configuration errors in objects destroyed at end
253 // of scope.
254 try
255 {
257 }
258 catch (std::exception& e)
259 {
260 ERR("{}", e.what());
261 throw;
262 }
263 }
static void assertNoSwallowedErrors()
Asserts that there have not been any errors reported in the destructor.

References BaseLib::ConfigTree::assertNoSwallowedErrors(), ERR(), mpi_setup, and simulation.

Referenced by PYBIND11_MODULE().

◆ getMesh()

OGSMesh & OGSSimulation::getMesh ( std::string const & name)
inline

Definition at line 212 of file ogs_python_module.cpp.

213 {
215
216 auto const mesh_it = mesh_mapping.find(name);
217 if (mesh_it != mesh_mapping.end())
218 {
219 INFO("found OGSMesh '{}' with address: {}", name,
220 fmt::ptr(&(mesh_it->second)));
221 return mesh_it->second;
222 }
223
224 auto const& [it, success] =
225 mesh_mapping.insert({name, OGSMesh(simulation->getMesh(name))});
226 if (!success)
227 {
228 OGS_FATAL("Could not access mesh '{}'.", name);
229 }
230 INFO("insert OGSMesh '{}' with address: {}", name,
231 fmt::ptr(&(it->second)));
232 return it->second;
233 }
#define OGS_FATAL(...)
Definition Error.h:19
std::map< std::string, OGSMesh > mesh_mapping

References INFO(), initialized(), mesh_mapping, OGS_ALWAYS_ASSERT, OGS_FATAL, and simulation.

Referenced by PYBIND11_MODULE().

◆ getMeshNames()

std::vector< std::string > OGSSimulation::getMeshNames ( ) const
inline

Definition at line 235 of file ogs_python_module.cpp.

236 {
238
239 return simulation->getMeshNames();
240 }

References initialized(), OGS_ALWAYS_ASSERT, and simulation.

Referenced by PYBIND11_MODULE().

◆ initialized()

bool OGSSimulation::initialized ( ) const
inline

Definition at line 267 of file ogs_python_module.cpp.

267{ return simulation != nullptr; }

References simulation.

Referenced by currentTime(), endTime(), executeSimulation(), executeTimeStep(), getMesh(), getMeshNames(), and PYBIND11_MODULE().

◆ status()

int OGSSimulation::status ( ) const
inline

Definition at line 265 of file ogs_python_module.cpp.

265{ return ogs_status; }

References ogs_status.

Referenced by PYBIND11_MODULE().

Member Data Documentation

◆ mesh_mapping

std::map<std::string, OGSMesh> OGSSimulation::mesh_mapping
private

Definition at line 273 of file ogs_python_module.cpp.

Referenced by getMesh().

◆ mpi_setup

std::optional<BaseLib::MPI::Setup> OGSSimulation::mpi_setup
private

Definition at line 274 of file ogs_python_module.cpp.

Referenced by OGSSimulation(), and finalize().

◆ ogs_status

int OGSSimulation::ogs_status = EXIT_SUCCESS
private

Definition at line 270 of file ogs_python_module.cpp.

Referenced by OGSSimulation(), executeSimulation(), executeTimeStep(), and status().

◆ simulation

std::unique_ptr<Simulation> OGSSimulation::simulation
private

The documentation for this class was generated from the following file: