Loading [MathJax]/extensions/tex2jax.js
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(
74 "This is OpenGeoSys-6 version {:s}. Log version: {:d}, Log level: "
75 "{:s}.",
77
79
80 {
81 auto const start_time = std::chrono::system_clock::now();
82 auto const time_str = BaseLib::formatDate(start_time);
83 INFO("OGS started on {:s} in serial mode / Python embedded mode.",
84 time_str);
85 }
86
87 std::optional<ApplicationsLib::TestDefinition> test_definition{
88 std::nullopt};
89 auto ogs_status = EXIT_SUCCESS;
90
91 try
92 {
93 simulation = std::make_unique<Simulation>(argc, argv);
94 simulation->initializeDataStructures(
95 std::move(cli_args.project),
96 std::move(cli_args.xml_patch_file_names),
97 cli_args.reference_path_is_set, std::move(cli_args.reference_path),
98 cli_args.nonfatal, std::move(cli_args.outdir),
99 std::move(cli_args.mesh_dir), std::move(cli_args.script_dir),
100 cli_args.write_prj);
101 }
102 catch (std::exception& e)
103 {
104 ERR("{}", e.what());
105 ogs_status = EXIT_FAILURE;
106 }
107
108 INFO("OpenGeoSys is now initialized.");
109
110 return ogs_status;
111}
112
114{
115 BaseLib::RunTime run_time;
116
117 {
118 auto const start_time = std::chrono::system_clock::now();
119 auto const time_str = BaseLib::formatDate(start_time);
120 INFO("OGS started on {:s} in serial mode.", time_str);
121 }
122
123 auto ogs_status = EXIT_SUCCESS;
124
125 try
126 {
127 run_time.start();
128 bool solver_succeeded = simulation->executeSimulation();
129 simulation->outputLastTimeStep();
130 // TODO: test definition ?
131
132 if (solver_succeeded)
133 {
134 INFO("[time] Simulation completed. It took {:g} s.",
135 run_time.elapsed());
136 }
137 else
138 {
139 INFO("[time] Simulation failed. It took {:g} s.",
140 run_time.elapsed());
141 }
142 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
143 }
144 catch (std::exception& e)
145 {
146 ERR("{}", e.what());
147 ogs_status = EXIT_FAILURE;
148 }
149
150 if (ogs_status == EXIT_FAILURE)
151 {
152 auto const end_time = std::chrono::system_clock::now();
153 auto const time_str = BaseLib::formatDate(end_time);
154 ERR("OGS terminated with error on {:s}.", time_str);
155 return EXIT_FAILURE;
156 }
157
158 return ogs_status;
159}
160
162{
163 auto ogs_status = EXIT_SUCCESS;
164 try
165 {
166 bool solver_succeeded = simulation->executeTimeStep();
167 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
168 }
169 catch (std::exception& e)
170 {
171 ERR("{}", e.what());
172 ogs_status = EXIT_FAILURE;
173 }
174 return ogs_status;
175}
176
178{
179 return simulation->currentTime();
180}
181
182double endTime()
183{
184 return simulation->endTime();
185}
186
187OGSMesh getMesh(std::string const& name)
188{
189 return OGSMesh(simulation->getMesh(name));
190}
191
193{
194 simulation.reset(nullptr);
195
196 // TODO don't use global project directory, shared among different OGS
197 // instances.
198 // Unset project dir to make multiple OGS runs in one Python session
199 // possible.
201}
202
209PYBIND11_MODULE(simulator, m)
210{
211 m.attr("__name__") = "ogs.simulator";
212 m.doc() = "pybind11 ogs example plugin";
213 m.def("initialize", &initOGS, "init OGS");
214 m.def("currentTime", &currentTime, "get current OGS time");
215 m.def("endTime", &endTime, "get end OGS time");
216 m.def("executeSimulation", &executeSimulation, "execute OGS simulation");
217 m.def("executeTimeStep", &executeTimeStep, "execute OGS time step");
218 m.def("getMesh", &getMesh, "get unstructured grid from ogs");
219 m.def("finalize", &finalize, "finalize OGS simulation");
220}
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