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 <algorithm>
19
20#include "../ogs.mesh/OGSMesh.h"
23#include "BaseLib/DateTools.h"
24#include "BaseLib/Error.h"
25#include "BaseLib/FileTools.h"
26#include "BaseLib/Logging.h"
27#include "BaseLib/RunTime.h"
29#include "InfoLib/GitInfo.h"
30#include "ogs_embedded_python.h"
31
32std::unique_ptr<Simulation> simulation;
33
34static constexpr int EXIT_ARGPARSE_FAILURE = 3; // "mangled" TCLAP status
35static constexpr int EXIT_ARGPARSE_EXIT_OK = 2; // "mangled" TCLAP status
36static_assert(EXIT_FAILURE == 1);
37static_assert(EXIT_SUCCESS == 0);
38
39int initOGS(std::vector<std::string>& argv_str)
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}
109
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}
146
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}
162
164{
165 return simulation->currentTime();
166}
167
168double endTime()
169{
170 return simulation->endTime();
171}
172
173OGSMesh getMesh(std::string const& name)
174{
175 return OGSMesh(simulation->getMesh(name));
176}
177
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}
188
195PYBIND11_MODULE(simulator, m)
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}
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.
bool createOutputDirectory(std::string const &dir)
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