OGS
ogs_python_module.cpp
Go to the documentation of this file.
1
13#include <pybind11/pybind11.h>
14#include <pybind11/stl.h>
15#include <spdlog/spdlog.h>
16#include <tclap/CmdLine.h>
17
18#include "../ogs.mesh/OGSMesh.h"
21#include "BaseLib/DateTools.h"
22#include "BaseLib/Error.h"
23#include "BaseLib/FileTools.h"
24#include "BaseLib/Logging.h"
25#include "BaseLib/RunTime.h"
27#include "InfoLib/GitInfo.h"
28#include "ogs_embedded_python.h"
29
30std::unique_ptr<Simulation> simulation;
31
32static constexpr int EXIT_ARGPARSE_FAILURE = 3; // "mangled" TCLAP status
33static constexpr int EXIT_ARGPARSE_EXIT_OK = 2; // "mangled" TCLAP status
34static_assert(EXIT_FAILURE == 1);
35static_assert(EXIT_SUCCESS == 0);
36
37int initOGS(std::vector<std::string>& argv_str)
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}
105
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}
142
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}
158
160{
161 return simulation->currentTime();
162}
163
164double endTime()
165{
166 return simulation->endTime();
167}
168
169OGSMesh getMesh(std::string const& name)
170{
171 return OGSMesh(simulation->getMesh(name));
172}
173
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}
184
191PYBIND11_MODULE(simulator, m)
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}
CommandLineArguments parseCommandLineArguments(int argc, char *argv[], bool const exit_on_exception)
Declaration of CommandLineArgumentParser.
Definition of date helper functions.
Filename manipulation routines.
Git information.
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
Definition of the RunTime class.
Declaration of class Simulation.
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
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:54
std::string formatDate(std::chrono::time_point< std::chrono::system_clock > const &time)
void unsetProjectDirectory()
Unsets the project directory.
GITINFOLIB_EXPORT const std::string ogs_version
static constexpr int EXIT_ARGPARSE_EXIT_OK
double currentTime()
int initOGS(std::vector< std::string > &argv_str)
void finalize()
int executeTimeStep()
static constexpr int EXIT_ARGPARSE_FAILURE
OGSMesh getMesh(std::string const &name)
std::unique_ptr< Simulation > simulation
PYBIND11_MODULE(simulator, m)
int executeSimulation()
double endTime()
std::vector< std::string > xml_patch_file_names