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 "../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 159 of file ogs_python_module.cpp.

160{
161 return simulation->currentTime();
162}
std::unique_ptr< Simulation > simulation

References simulation.

Referenced by PYBIND11_MODULE().

◆ endTime()

double endTime ( )

Definition at line 164 of file ogs_python_module.cpp.

165{
166 return simulation->endTime();
167}

References simulation.

Referenced by PYBIND11_MODULE().

◆ executeSimulation()

int executeSimulation ( )

Definition at line 106 of file ogs_python_module.cpp.

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

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

References ERR(), and simulation.

Referenced by PYBIND11_MODULE().

◆ finalize()

void finalize ( )

Definition at line 174 of file ogs_python_module.cpp.

175{
176 simulation.reset(nullptr);
177
178 // TODO don't use global project directory, shared among different OGS
179 // instances.
180 // Unset project dir to make multiple OGS runs in one Python session
181 // possible.
183}
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 37 of file ogs_python_module.cpp.

38{
39 int argc = argv_str.size();
40 char** argv = new char*[argc];
41 for (int i = 0; i < argc; ++i)
42 {
43 argv[i] = argv_str[i].data();
44 }
45
46 CommandLineArguments cli_args;
47 try
48 {
49 cli_args = parseCommandLineArguments(argc, argv, false);
50 }
51 catch (TCLAP::ArgException const& e)
52 {
53 ERR("Parsing the OGS commandline failed: {}", e.what());
54
55 // "mangle" TCLAP's status
57 }
58 catch (TCLAP::ExitException const& e)
59 {
60 if (e.getExitStatus() == 0)
61 {
63 }
64
65 // "mangle" TCLAP's status
67 }
68
70
71 INFO("This is OpenGeoSys-6 version {:s}.",
73
74 {
75 auto const start_time = std::chrono::system_clock::now();
76 auto const time_str = BaseLib::formatDate(start_time);
77 INFO("OGS started on {:s}.", time_str);
78 }
79
80 std::optional<ApplicationsLib::TestDefinition> test_definition{
81 std::nullopt};
82 auto ogs_status = EXIT_SUCCESS;
83
84 try
85 {
86 simulation = std::make_unique<Simulation>(argc, argv);
87 simulation->initializeDataStructures(
88 std::move(cli_args.project),
89 std::move(cli_args.xml_patch_file_names),
90 cli_args.reference_path_is_set, std::move(cli_args.reference_path),
91 cli_args.nonfatal, std::move(cli_args.outdir),
92 std::move(cli_args.mesh_dir), std::move(cli_args.script_dir),
93 cli_args.write_prj);
94 }
95 catch (std::exception& e)
96 {
97 ERR("{}", e.what());
98 ogs_status = EXIT_FAILURE;
99 }
100
101 INFO("OpenGeoSys is now initialized.");
102
103 return ogs_status;
104}
CommandLineArguments parseCommandLineArguments(int argc, char *argv[], bool const exit_on_exception)
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:54
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 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 191 of file ogs_python_module.cpp.

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

constexpr int EXIT_ARGPARSE_EXIT_OK = 2
staticconstexpr

Definition at line 33 of file ogs_python_module.cpp.

Referenced by initOGS().

◆ EXIT_ARGPARSE_FAILURE

constexpr int EXIT_ARGPARSE_FAILURE = 3
staticconstexpr

Definition at line 32 of file ogs_python_module.cpp.

Referenced by initOGS().

◆ simulation

std::unique_ptr<Simulation> simulation