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 163 of file ogs_python_module.cpp.

164{
165 return simulation->currentTime();
166}
std::unique_ptr< Simulation > simulation

References simulation.

Referenced by PYBIND11_MODULE().

◆ endTime()

double endTime ( )

Definition at line 168 of file ogs_python_module.cpp.

169{
170 return simulation->endTime();
171}

References simulation.

Referenced by PYBIND11_MODULE().

◆ executeSimulation()

int executeSimulation ( )

Definition at line 110 of file ogs_python_module.cpp.

111{
112 BaseLib::RunTime run_time;
113
114 {
115 auto const start_time = std::chrono::system_clock::now();
116 auto const time_str = BaseLib::formatDate(start_time);
117 INFO("OGS started on {:s}.", time_str);
118 }
119
120 auto ogs_status = EXIT_SUCCESS;
121
122 try
123 {
124 run_time.start();
125 bool solver_succeeded = simulation->executeSimulation();
126 simulation->outputLastTimeStep();
127 // TODO: test definition ?
128
129 INFO("[time] Execution took {:g} s.", run_time.elapsed());
130 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
131 }
132 catch (std::exception& e)
133 {
134 ERR("{}", e.what());
135 ogs_status = EXIT_FAILURE;
136 }
137
138 {
139 auto const end_time = std::chrono::system_clock::now();
140 auto const time_str = BaseLib::formatDate(end_time);
141 INFO("OGS terminated on {:s}.", time_str);
142 }
143
144 return ogs_status;
145}
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 147 of file ogs_python_module.cpp.

148{
149 auto ogs_status = EXIT_SUCCESS;
150 try
151 {
152 bool solver_succeeded = simulation->executeTimeStep();
153 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
154 }
155 catch (std::exception& e)
156 {
157 ERR("{}", e.what());
158 ogs_status = EXIT_FAILURE;
159 }
160 return ogs_status;
161}

References ERR(), and simulation.

Referenced by PYBIND11_MODULE().

◆ finalize()

void finalize ( )

Definition at line 178 of file ogs_python_module.cpp.

179{
180 simulation.reset(nullptr);
181
182 // TODO don't use global project directory, shared among different OGS
183 // instances.
184 // Unset project dir to make multiple OGS runs in one Python session
185 // possible.
187}
void unsetProjectDirectory()
Unsets the project directory.

References simulation, and BaseLib::unsetProjectDirectory().

Referenced by 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("This is OpenGeoSys-6 version {:s}.",
75
77
78 {
79 auto const start_time = std::chrono::system_clock::now();
80 auto const time_str = BaseLib::formatDate(start_time);
81 INFO("OGS started on {:s}.", time_str);
82 }
83
84 std::optional<ApplicationsLib::TestDefinition> test_definition{
85 std::nullopt};
86 auto ogs_status = EXIT_SUCCESS;
87
88 try
89 {
90 simulation = std::make_unique<Simulation>(argc, argv);
91 simulation->initializeDataStructures(
92 std::move(cli_args.project),
93 std::move(cli_args.xml_patch_file_names),
94 cli_args.reference_path_is_set, std::move(cli_args.reference_path),
95 cli_args.nonfatal, std::move(cli_args.outdir),
96 std::move(cli_args.mesh_dir), std::move(cli_args.script_dir),
97 cli_args.write_prj);
98 }
99 catch (std::exception& e)
100 {
101 ERR("{}", e.what());
102 ogs_status = EXIT_FAILURE;
103 }
104
105 INFO("OpenGeoSys is now initialized.");
106
107 return ogs_status;
108}
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 195 of file ogs_python_module.cpp.

196{
197 m.attr("__name__") = "ogs.simulator";
198 m.doc() = "pybind11 ogs example plugin";
199 m.def("initialize", &initOGS, "init OGS");
200 m.def("currentTime", &currentTime, "get current OGS time");
201 m.def("endTime", &endTime, "get end OGS time");
202 m.def("executeSimulation", &executeSimulation, "execute OGS simulation");
203 m.def("executeTimeStep", &executeTimeStep, "execute OGS time step");
204 m.def("getMesh", &getMesh, "get unstructured grid from ogs");
205 m.def("finalize", &finalize, "finalize OGS simulation");
206}
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