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
int finalize ()
int status () const
bool initialized () const

Private Attributes

int ogs_status = EXIT_SUCCESS
std::unique_ptr< Simulationsimulation
std::optional< ApplicationsLib::TestDefinitiontest_definition
bool test_definition_pending = false
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#ifdef USE_PETSC
109 int size;
110 MPI_Comm_size(BaseLib::MPI::OGS_COMM_WORLD, &size);
111 int rank;
112 MPI_Comm_rank(BaseLib::MPI::OGS_COMM_WORLD, &rank);
113 INFO(
114 "OGS starts on {:s} in MPI mode [{}] of {} / Python embedded "
115 "mode.",
116 time_str, rank, size);
117#else
118 INFO("OGS starts on {:s} in serial mode / Python embedded mode.",
119 time_str);
120#endif
121 }
122 try
123 {
124 simulation = std::make_unique<Simulation>(argc, argv);
125 simulation->initializeDataStructures(
126 std::move(cli_args.project),
127 std::move(cli_args.xml_patch_file_names),
128 cli_args.reference_path_is_set,
129 std::move(cli_args.reference_path), cli_args.nonfatal,
130 std::move(cli_args.outdir), std::move(cli_args.mesh_dir),
131 std::move(cli_args.script_dir), cli_args.write_prj);
132 }
133 catch (std::exception& e)
134 {
135 ERR("{}", e.what());
136 ogs_status = EXIT_FAILURE;
137 simulation.reset();
138 throw(e);
139 }
140 INFO("OpenGeoSys is now initialized.");
141 }
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
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:104
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
constexpr int size(int const displacement_dim)
Vectorized tensor size for given displacement dimension.
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, BaseLib::MPI::OGS_COMM_WORLD, 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 209 of file ogs_python_module.cpp.

210 {
212 return simulation->currentTime();
213 }
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 215 of file ogs_python_module.cpp.

216 {
218 return simulation->endTime();
219 }

References initialized(), OGS_ALWAYS_ASSERT, and simulation.

Referenced by PYBIND11_MODULE().

◆ executeSimulation()

int OGSSimulation::executeSimulation ( )
inline

Definition at line 143 of file ogs_python_module.cpp.

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

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

Referenced by PYBIND11_MODULE().

◆ executeTimeStep()

int OGSSimulation::executeTimeStep ( )
inline

Definition at line 192 of file ogs_python_module.cpp.

193 {
195
196 try
197 {
198 bool solver_succeeded = simulation->executeTimeStep();
199 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
200 }
201 catch (std::exception& e)
202 {
203 ERR("{}", e.what());
204 ogs_status = EXIT_FAILURE;
205 }
206 return ogs_status;
207 }

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

Referenced by PYBIND11_MODULE().

◆ finalize()

int OGSSimulation::finalize ( )
inline

Definition at line 251 of file ogs_python_module.cpp.

252 {
253 if (simulation)
254 {
255 simulation->outputLastTimeStep();
256 simulation.reset(nullptr);
257 }
258
259 // Check for swallowed ConfigTree errors after Simulation destructor
260 // runs. This catches configuration errors in objects destroyed at end
261 // of scope.
262 try
263 {
265 }
266 catch (std::exception& e)
267 {
268 ERR("{}", e.what());
269 ogs_status = EXIT_FAILURE;
270 }
271
272 if (ogs_status == EXIT_SUCCESS && test_definition_pending)
273 {
275 }
277
278 mpi_setup.reset();
279
280 return ogs_status;
281 }
static void assertNoSwallowedErrors()
Asserts that there have not been any errors reported in the destructor.
static OGS_EXPORT_SYMBOL int runTestDefinitions(std::optional< ApplicationsLib::TestDefinition > &test_definition)

References BaseLib::ConfigTree::assertNoSwallowedErrors(), ERR(), mpi_setup, ogs_status, Simulation::runTestDefinitions(), simulation, test_definition, and test_definition_pending.

Referenced by PYBIND11_MODULE().

◆ getMesh()

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

Definition at line 221 of file ogs_python_module.cpp.

222 {
224
225 auto const mesh_it = mesh_mapping.find(name);
226 if (mesh_it != mesh_mapping.end())
227 {
228 INFO("found OGSMesh '{}' with address: {}", name,
229 fmt::ptr(&(mesh_it->second)));
230 return mesh_it->second;
231 }
232
233 auto const& [it, success] =
234 mesh_mapping.insert({name, OGSMesh(simulation->getMesh(name))});
235 if (!success)
236 {
237 OGS_FATAL("Could not access mesh '{}'.", name);
238 }
239 INFO("insert OGSMesh '{}' with address: {}", name,
240 fmt::ptr(&(it->second)));
241 return it->second;
242 }
#define OGS_FATAL(...)
Definition Error.h:10
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 244 of file ogs_python_module.cpp.

245 {
247
248 return simulation->getMeshNames();
249 }

References initialized(), OGS_ALWAYS_ASSERT, and simulation.

Referenced by PYBIND11_MODULE().

◆ initialized()

bool OGSSimulation::initialized ( ) const
inline

Definition at line 285 of file ogs_python_module.cpp.

285{ 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 283 of file ogs_python_module.cpp.

283{ 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 294 of file ogs_python_module.cpp.

Referenced by getMesh().

◆ mpi_setup

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

Definition at line 295 of file ogs_python_module.cpp.

Referenced by OGSSimulation(), and finalize().

◆ ogs_status

int OGSSimulation::ogs_status = EXIT_SUCCESS
private

◆ simulation

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

◆ test_definition

std::optional<ApplicationsLib::TestDefinition> OGSSimulation::test_definition
private
Initial value:
{
std::nullopt}

Definition at line 291 of file ogs_python_module.cpp.

291 {
292 std::nullopt};

Referenced by executeSimulation(), and finalize().

◆ test_definition_pending

bool OGSSimulation::test_definition_pending = false
private

Definition at line 293 of file ogs_python_module.cpp.

Referenced by executeSimulation(), and finalize().


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