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();
108 INFO(
"OGS starts on {:s} in serial mode / Python embedded mode.",
113 simulation = std::make_unique<Simulation>(argc, argv);
122 catch (std::exception& e)
129 INFO(
"OpenGeoSys is now initialized.");
139 auto const start_time = std::chrono::system_clock::now();
141 INFO(
"OGS started on {:s} in serial mode.", time_str);
144 std::optional<ApplicationsLib::TestDefinition> test_definition{
150 bool solver_succeeded =
simulation->executeSimulation();
152 test_definition =
simulation->getTestDefinition();
154 if (solver_succeeded)
156 INFO(
"[time] Simulation completed. It took {:g} s.",
161 INFO(
"[time] Simulation failed. It took {:g} s.",
164 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
166 catch (std::exception& e)
174 auto const end_time = std::chrono::system_clock::now();
176 ERR(
"OGS terminated with error on {:s}.", time_str);
189 bool solver_succeeded =
simulation->executeTimeStep();
190 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
192 catch (std::exception& e)
219 INFO(
"found OGSMesh '{}' with address: {}", name,
220 fmt::ptr(&(mesh_it->second)));
221 return mesh_it->second;
224 auto const& [it, success] =
228 OGS_FATAL(
"Could not access mesh '{}'.", name);
230 INFO(
"insert OGSMesh '{}' with address: {}", name,
231 fmt::ptr(&(it->second)));
258 catch (std::exception& e)
285 m.attr(
"__name__") =
"ogs.OGSSimulator";
286 m.doc() =
"pybind11 ogs plugin";
288 pybind11::class_<OGSSimulation>(m,
"OGSSimulation")
289 .def(pybind11::init<std::vector<std::string>&>())
291 "get current OGS time")
294 "execute OGS simulation")
296 "execute OGS time step")
298 pybind11::return_value_policy::automatic_reference,
299 pybind11::arg(
"name"),
"get unstructured grid from ogs")
301 "get names of all meshes from ogs")
304 .def_property_readonly(
306 "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< 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