OGS
ogs_python_module.cpp
Go to the documentation of this file.
1
13#include <algorithm>
14#include <pybind11/pybind11.h>
15#include <pybind11/stl.h>
16#include <spdlog/spdlog.h>
17#include <tclap/CmdLine.h>
18
19#include "../ogs.mesh/OGSMesh.h"
22#include "BaseLib/DateTools.h"
23#include "BaseLib/Error.h"
24#include "BaseLib/FileTools.h"
25#include "BaseLib/Logging.h"
26#include "BaseLib/RunTime.h"
28#include "InfoLib/GitInfo.h"
29#include "ogs_embedded_python.h"
30
31std::unique_ptr<Simulation> simulation;
32
33static constexpr int EXIT_ARGPARSE_FAILURE = 3; // "mangled" TCLAP status
34static constexpr int EXIT_ARGPARSE_EXIT_OK = 2; // "mangled" TCLAP status
35static_assert(EXIT_FAILURE == 1);
36static_assert(EXIT_SUCCESS == 0);
37
38int initOGS(std::vector<std::string>& argv_str)
39{
40 int argc = argv_str.size();
41 char** argv = new char*[argc];
42 for (int i = 0; i < argc; ++i)
43 {
44 argv[i] = argv_str[i].data();
45 }
46
47 CommandLineArguments cli_args;
48 try
49 {
50 cli_args = parseCommandLineArguments(argc, argv, false);
51 }
52 catch (TCLAP::ArgException const& e)
53 {
54 ERR("Parsing the OGS commandline failed: {}", e.what());
55
56 // "mangle" TCLAP's status
58 }
59 catch (TCLAP::ExitException const& e)
60 {
61 if (e.getExitStatus() == 0)
62 {
64 }
65
66 // "mangle" TCLAP's status
68 }
69
71
72 INFO("This is OpenGeoSys-6 version {:s}.",
74
75 {
76 auto const start_time = std::chrono::system_clock::now();
77 auto const time_str = BaseLib::formatDate(start_time);
78 INFO("OGS started on {:s}.", time_str);
79 }
80
81 std::optional<ApplicationsLib::TestDefinition> test_definition{
82 std::nullopt};
83 auto ogs_status = EXIT_SUCCESS;
84
85 try
86 {
87 simulation = std::make_unique<Simulation>(argc, argv);
88 simulation->initializeDataStructures(
89 std::move(cli_args.project),
90 std::move(cli_args.xml_patch_file_names),
91 cli_args.reference_path_is_set, std::move(cli_args.reference_path),
92 cli_args.nonfatal, std::move(cli_args.outdir),
93 std::move(cli_args.mesh_dir), std::move(cli_args.script_dir),
94 cli_args.write_prj);
95 }
96 catch (std::exception& e)
97 {
98 ERR("{}", e.what());
99 ogs_status = EXIT_FAILURE;
100 }
101
102 INFO("OpenGeoSys is now initialized.");
103
104 return ogs_status;
105}
106
108{
109 BaseLib::RunTime run_time;
110
111 {
112 auto const start_time = std::chrono::system_clock::now();
113 auto const time_str = BaseLib::formatDate(start_time);
114 INFO("OGS started on {:s}.", time_str);
115 }
116
117 auto ogs_status = EXIT_SUCCESS;
118
119 try
120 {
121 run_time.start();
122 bool solver_succeeded = simulation->executeSimulation();
123 simulation->outputLastTimeStep();
124 // TODO: test definition ?
125
126 INFO("[time] Execution took {:g} s.", run_time.elapsed());
127 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
128 }
129 catch (std::exception& e)
130 {
131 ERR("{}", e.what());
132 ogs_status = EXIT_FAILURE;
133 }
134
135 {
136 auto const end_time = std::chrono::system_clock::now();
137 auto const time_str = BaseLib::formatDate(end_time);
138 INFO("OGS terminated on {:s}.", time_str);
139 }
140
141 return ogs_status;
142}
143
145{
146 auto ogs_status = EXIT_SUCCESS;
147 try
148 {
149 bool solver_succeeded = simulation->executeTimeStep();
150 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
151 }
152 catch (std::exception& e)
153 {
154 ERR("{}", e.what());
155 ogs_status = EXIT_FAILURE;
156 }
157 return ogs_status;
158}
159
161{
162 return simulation->currentTime();
163}
164
165double endTime()
166{
167 return simulation->endTime();
168}
169
170OGSMesh getMesh(std::string const& name)
171{
172 return OGSMesh(simulation->getMesh(name));
173}
174
176{
177 simulation.reset(nullptr);
178
179 // TODO don't use global project directory, shared among different OGS
180 // instances.
181 // Unset project dir to make multiple OGS runs in one Python session
182 // possible.
184}
185
192PYBIND11_MODULE(simulator, m)
193{
194 m.attr("__name__") = "ogs.simulator";
195 m.doc() = "pybind11 ogs example plugin";
196 m.def("initialize", &initOGS, "init OGS");
197 m.def("currentTime", &currentTime, "get current OGS time");
198 m.def("endTime", &endTime, "get end OGS time");
199 m.def("executeSimulation", &executeSimulation, "execute OGS simulation");
200 m.def("executeTimeStep", &executeTimeStep, "execute OGS time step");
201 m.def("getMesh", &getMesh, "get unstructured grid from ogs");
202 m.def("finalize", &finalize, "finalize OGS simulation");
203}
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