4#include <pybind11/pybind11.h>
5#include <pybind11/stl.h>
6#include <spdlog/spdlog.h>
7#include <tclap/CmdLine.h>
9#include <range/v3/range/conversion.hpp>
10#include <range/v3/view/transform.hpp>
28static_assert(EXIT_FAILURE == 1);
29static_assert(EXIT_SUCCESS == 0);
31#define OGS_ALWAYS_ASSERT(cond) \
34 OGS_FATAL("OGS assertion failed {}", #cond); \
38 std::vector<std::string>& argv_str)
40 int argc = argv_str.size();
41 auto argv_vec = argv_str |
42 ranges::views::transform([](
auto& s) {
return s.data(); }) |
43 ranges::to<std::vector<char*>>();
44 argv_vec.push_back(
nullptr);
46 return {argc, std::move(argv_vec)};
57 char** argv = argv_vec.data();
66 catch (TCLAP::ArgException
const& e)
72 std::cerr <<
"Parsing the OGS commandline failed: " << e.what()
78 catch (TCLAP::ExitException
const& e)
84 if (e.getExitStatus() == 0)
95 DBUG(
"OGSSimulation::OGSSimulation(std::vector<std::string>&)");
98 "This is OpenGeoSys-6 version {:s}. Log version: {:d}, Log level: "
105 auto const start_time = std::chrono::system_clock::now();
114 "OGS starts on {:s} in MPI mode [{}] of {} / Python embedded "
116 time_str, rank, size);
118 INFO(
"OGS starts on {:s} in serial mode / Python embedded mode.",
124 simulation = std::make_unique<Simulation>(argc, argv);
133 catch (std::exception& e)
140 INFO(
"OpenGeoSys is now initialized.");
150 auto const start_time = std::chrono::system_clock::now();
152 INFO(
"OGS started on {:s} in serial mode.", time_str);
158 bool solver_succeeded =
simulation->executeSimulation();
163 if (solver_succeeded)
165 INFO(
"[time] Simulation completed. It took {:g} s.",
170 INFO(
"[time] Simulation failed. It took {:g} s.",
173 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
175 catch (std::exception& e)
183 auto const end_time = std::chrono::system_clock::now();
185 ERR(
"OGS terminated with error on {:s}.", time_str);
198 bool solver_succeeded =
simulation->executeTimeStep();
199 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
201 catch (std::exception& e)
228 INFO(
"found OGSMesh '{}' with address: {}", name,
229 fmt::ptr(&(mesh_it->second)));
230 return mesh_it->second;
233 auto const& [it, success] =
237 OGS_FATAL(
"Could not access mesh '{}'.", name);
239 INFO(
"insert OGSMesh '{}' with address: {}", name,
240 fmt::ptr(&(it->second)));
266 catch (std::exception& e)
306 m.attr(
"__name__") =
"ogs.OGSSimulator";
307 m.doc() =
"pybind11 ogs plugin";
309 pybind11::class_<OGSSimulation>(m,
"OGSSimulation")
310 .def(pybind11::init<std::vector<std::string>&>())
312 "get current OGS time")
315 "execute OGS simulation")
317 "execute OGS time step")
319 pybind11::return_value_policy::automatic_reference,
320 pybind11::arg(
"name"),
"get unstructured grid from ogs")
322 "get names of all meshes from ogs")
325 .def_property_readonly(
327 "Tells if the simulation object has been completely initialized.");
CommandLineArguments parseCommandLineArguments(int argc, char *argv[], bool const exit_on_exception)
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
static void assertNoSwallowedErrors()
Asserts that there have not been any errors reported in the destructor.
double elapsed() const
Get the elapsed time in seconds.
void start()
Start the timer.
std::map< std::string, OGSMesh > mesh_mapping
std::optional< ApplicationsLib::TestDefinition > test_definition
bool test_definition_pending
std::optional< BaseLib::MPI::Setup > mpi_setup
OGSMesh & getMesh(std::string const &name)
std::unique_ptr< Simulation > simulation
OGSSimulation(std::vector< std::string > &argv_str)
double currentTime() const
std::vector< std::string > getMeshNames() const
static OGS_EXPORT_SYMBOL int runTestDefinitions(std::optional< ApplicationsLib::TestDefinition > &test_definition)
void initOGSLogger(std::string const &log_level)
std::string formatDate(std::chrono::time_point< std::chrono::system_clock > const &time)
std::string defaultLogLevel()
bool createOutputDirectory(std::string const &dir)
GITINFOLIB_EXPORT const std::string ogs_version
static constexpr int EXIT_ARGPARSE_EXIT_OK
PYBIND11_MODULE(OGSSimulator, m)
static constexpr int EXIT_ARGPARSE_FAILURE
#define OGS_ALWAYS_ASSERT(cond)
std::pair< int, std::vector< char * > > toArgcArgv(std::vector< std::string > &argv_str)
bool reference_path_is_set
std::string reference_path
std::vector< std::string > xml_patch_file_names