OGS
ogs_python_module.cpp File Reference

Detailed Description

Implementation of OpenGeoSys simulation application python module.

Definition in file ogs_python_module.cpp.

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <spdlog/spdlog.h>
#include <tclap/CmdLine.h>
#include <algorithm>
#include "../ogs.mesh/OGSMesh.h"
#include "Applications/ApplicationsLib/Simulation.h"
#include "Applications/ApplicationsLib/TestDefinition.h"
#include "BaseLib/DateTools.h"
#include "BaseLib/Error.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/Logging.h"
#include "BaseLib/RunTime.h"
#include "CommandLineArgumentParser.h"
#include "InfoLib/GitInfo.h"
#include "ogs_embedded_python.h"
Include dependency graph for ogs_python_module.cpp:

Go to the source code of this file.

Functions

int initOGS (std::vector< std::string > &argv_str)
 
int executeSimulation ()
 
int executeTimeStep ()
 
double currentTime ()
 
double endTime ()
 
OGSMesh getMesh (std::string const &name)
 
void finalize ()
 
 PYBIND11_MODULE (simulator, m)
 

Variables

std::unique_ptr< Simulationsimulation
 
static constexpr int EXIT_ARGPARSE_FAILURE = 3
 
static constexpr int EXIT_ARGPARSE_EXIT_OK = 2
 

Function Documentation

◆ currentTime()

double currentTime ( )

Definition at line 177 of file ogs_python_module.cpp.

178{
179 return simulation->currentTime();
180}
std::unique_ptr< Simulation > simulation

References simulation.

Referenced by PYBIND11_MODULE().

◆ endTime()

double endTime ( )

Definition at line 182 of file ogs_python_module.cpp.

183{
184 return simulation->endTime();
185}

References simulation.

Referenced by PYBIND11_MODULE().

◆ executeSimulation()

int executeSimulation ( )

Definition at line 113 of file ogs_python_module.cpp.

114{
115 BaseLib::RunTime run_time;
116
117 {
118 auto const start_time = std::chrono::system_clock::now();
119 auto const time_str = BaseLib::formatDate(start_time);
120 INFO("OGS started on {:s} in serial mode.", time_str);
121 }
122
123 auto ogs_status = EXIT_SUCCESS;
124
125 try
126 {
127 run_time.start();
128 bool solver_succeeded = simulation->executeSimulation();
129 simulation->outputLastTimeStep();
130 // TODO: test definition ?
131
132 if (solver_succeeded)
133 {
134 INFO("[time] Simulation completed. It took {:g} s.",
135 run_time.elapsed());
136 }
137 else
138 {
139 INFO("[time] Simulation failed. It took {:g} s.",
140 run_time.elapsed());
141 }
142 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
143 }
144 catch (std::exception& e)
145 {
146 ERR("{}", e.what());
147 ogs_status = EXIT_FAILURE;
148 }
149
150 if (ogs_status == EXIT_FAILURE)
151 {
152 auto const end_time = std::chrono::system_clock::now();
153 auto const time_str = BaseLib::formatDate(end_time);
154 ERR("OGS terminated with error on {:s}.", time_str);
155 return EXIT_FAILURE;
156 }
157
158 return ogs_status;
159}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Count the running time.
Definition RunTime.h:29
double elapsed() const
Get the elapsed time in seconds.
Definition RunTime.h:42
void start()
Start the timer.
Definition RunTime.h:32
std::string formatDate(std::chrono::time_point< std::chrono::system_clock > const &time)

References BaseLib::RunTime::elapsed(), ERR(), BaseLib::formatDate(), INFO(), simulation, and BaseLib::RunTime::start().

Referenced by PYBIND11_MODULE().

◆ executeTimeStep()

int executeTimeStep ( )

Definition at line 161 of file ogs_python_module.cpp.

162{
163 auto ogs_status = EXIT_SUCCESS;
164 try
165 {
166 bool solver_succeeded = simulation->executeTimeStep();
167 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
168 }
169 catch (std::exception& e)
170 {
171 ERR("{}", e.what());
172 ogs_status = EXIT_FAILURE;
173 }
174 return ogs_status;
175}

References ERR(), and simulation.

Referenced by PYBIND11_MODULE().

◆ finalize()

void finalize ( )

Definition at line 192 of file ogs_python_module.cpp.

193{
194 simulation.reset(nullptr);
195
196 // TODO don't use global project directory, shared among different OGS
197 // instances.
198 // Unset project dir to make multiple OGS runs in one Python session
199 // possible.
201}
void unsetProjectDirectory()
Unsets the project directory.

References simulation, and BaseLib::unsetProjectDirectory().

Referenced by NumLib::detail::calculateFluxCorrectedTransportPETSc(), and PYBIND11_MODULE().

◆ getMesh()

◆ initOGS()

int initOGS ( std::vector< std::string > & argv_str)

Definition at line 39 of file ogs_python_module.cpp.

40{
41 int argc = argv_str.size();
42 char** argv = new char*[argc];
43 for (int i = 0; i < argc; ++i)
44 {
45 argv[i] = argv_str[i].data();
46 }
47
48 CommandLineArguments cli_args;
49 try
50 {
51 cli_args = parseCommandLineArguments(argc, argv, false);
52 }
53 catch (TCLAP::ArgException const& e)
54 {
55 ERR("Parsing the OGS commandline failed: {}", e.what());
56
57 // "mangle" TCLAP's status
59 }
60 catch (TCLAP::ExitException const& e)
61 {
62 if (e.getExitStatus() == 0)
63 {
65 }
66
67 // "mangle" TCLAP's status
69 }
70
72
73 INFO(
74 "This is OpenGeoSys-6 version {:s}. Log version: {:d}, Log level: "
75 "{:s}.",
77
79
80 {
81 auto const start_time = std::chrono::system_clock::now();
82 auto const time_str = BaseLib::formatDate(start_time);
83 INFO("OGS started on {:s} in serial mode / Python embedded mode.",
84 time_str);
85 }
86
87 std::optional<ApplicationsLib::TestDefinition> test_definition{
88 std::nullopt};
89 auto ogs_status = EXIT_SUCCESS;
90
91 try
92 {
93 simulation = std::make_unique<Simulation>(argc, argv);
94 simulation->initializeDataStructures(
95 std::move(cli_args.project),
96 std::move(cli_args.xml_patch_file_names),
97 cli_args.reference_path_is_set, std::move(cli_args.reference_path),
98 cli_args.nonfatal, std::move(cli_args.outdir),
99 std::move(cli_args.mesh_dir), std::move(cli_args.script_dir),
100 cli_args.write_prj);
101 }
102 catch (std::exception& e)
103 {
104 ERR("{}", e.what());
105 ogs_status = EXIT_FAILURE;
106 }
107
108 INFO("OpenGeoSys is now initialized.");
109
110 return ogs_status;
111}
CommandLineArguments parseCommandLineArguments(int argc, char *argv[], bool const exit_on_exception)
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:54
bool createOutputDirectory(std::string const &dir)
GITINFOLIB_EXPORT const std::string ogs_version
static constexpr int EXIT_ARGPARSE_EXIT_OK
static constexpr int EXIT_ARGPARSE_FAILURE
std::vector< std::string > xml_patch_file_names

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

Referenced by PYBIND11_MODULE().

◆ PYBIND11_MODULE()

PYBIND11_MODULE ( simulator ,
m  )

To use this module import dependencies first: import ogs.mesh as mesh import ogs.simulator as sim

See also https://github.com/pybind/pybind11/issues/1391#issuecomment-912642979

Definition at line 209 of file ogs_python_module.cpp.

210{
211 m.attr("__name__") = "ogs.simulator";
212 m.doc() = "pybind11 ogs example plugin";
213 m.def("initialize", &initOGS, "init OGS");
214 m.def("currentTime", &currentTime, "get current OGS time");
215 m.def("endTime", &endTime, "get end OGS time");
216 m.def("executeSimulation", &executeSimulation, "execute OGS simulation");
217 m.def("executeTimeStep", &executeTimeStep, "execute OGS time step");
218 m.def("getMesh", &getMesh, "get unstructured grid from ogs");
219 m.def("finalize", &finalize, "finalize OGS simulation");
220}
double currentTime()
int initOGS(std::vector< std::string > &argv_str)
void finalize()
int executeTimeStep()
OGSMesh getMesh(std::string const &name)
int executeSimulation()
double endTime()

References currentTime(), endTime(), executeSimulation(), executeTimeStep(), finalize(), getMesh(), and initOGS().

Variable Documentation

◆ EXIT_ARGPARSE_EXIT_OK

int EXIT_ARGPARSE_EXIT_OK = 2
staticconstexpr

Definition at line 35 of file ogs_python_module.cpp.

Referenced by initOGS().

◆ EXIT_ARGPARSE_FAILURE

int EXIT_ARGPARSE_FAILURE = 3
staticconstexpr

Definition at line 34 of file ogs_python_module.cpp.

Referenced by initOGS().

◆ simulation

std::unique_ptr<Simulation> simulation