OGS
ogs.cpp File Reference
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <spdlog/spdlog.h>
#include <tclap/CmdLine.h>
#include <algorithm>
#include <chrono>
#include <csignal>
#include <iostream>
#include <sstream>
#include "Applications/CLI/ogs_embedded_python.h"
#include "CommandLineArgumentParser.h"
#include <cfenv>
#include "Applications/ApplicationsLib/Simulation.h"
#include "Applications/ApplicationsLib/TestDefinition.h"
#include "BaseLib/ConfigTree.h"
#include "BaseLib/DateTools.h"
#include "BaseLib/Error.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/Logging.h"
#include "BaseLib/RunTime.h"
#include "InfoLib/GitInfo.h"
#include <spdlog/sinks/null_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include "BaseLib/MPI.h"
Include dependency graph for ogs.cpp:

Go to the source code of this file.

Functions

void enableFloatingPointExceptions ()
void signalHandler (int signum)
void initializeLogger (bool const all_ranks_log)
int main (int argc, char *argv[])

Function Documentation

◆ enableFloatingPointExceptions()

void enableFloatingPointExceptions ( )

Definition at line 39 of file ogs.cpp.

40{
41#ifdef __APPLE__
42#ifdef __SSE__
43 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID);
44#endif // __SSE__
45#else
46 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
47#endif // __APPLE__
48}

Referenced by main().

◆ initializeLogger()

void initializeLogger ( bool const all_ranks_log)

Definition at line 65 of file ogs.cpp.

66{
67#if defined(USE_PETSC)
68 int mpi_rank;
69 MPI_Comm_rank(BaseLib::MPI::OGS_COMM_WORLD, &mpi_rank);
70 int world_size;
71 MPI_Comm_size(BaseLib::MPI::OGS_COMM_WORLD, &world_size);
72
73 if (all_ranks_log)
74 {
75 if (world_size > 1)
76 {
77 spdlog::set_pattern(fmt::format("[{}] %^%l:%$ %v", mpi_rank));
78 }
79 // else untouched
80 }
81 else // only rank 0 logs
82 {
83 // set_pattern is untouched
84 spdlog::drop_all();
85 if (mpi_rank > 0)
86 {
87 BaseLib::console = spdlog::create<spdlog::sinks::null_sink_st>(
88 "ogs"); // do not log
89 }
90 else // rank 0
91 {
93 spdlog::stdout_color_st("ogs"); // st for performance
94 }
95 }
96
97 {
98 auto const start_time = std::chrono::system_clock::now();
99 auto const time_str = BaseLib::formatDate(start_time);
100 INFO("OGS started on {:s} with MPI. MPI processes: {:d}.", time_str,
101 world_size);
102 }
103
104#else // defined(USE_PETSC)
105
106 {
107 auto const start_time = std::chrono::system_clock::now();
108 auto const time_str = BaseLib::formatDate(start_time);
109 INFO("OGS started on {:s} in serial mode.", time_str);
110 }
111
112#endif
113}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9
std::string formatDate(std::chrono::time_point< std::chrono::system_clock > const &time)
std::shared_ptr< spdlog::logger > console
Definition Logging.cpp:37

References BaseLib::console, BaseLib::formatDate(), INFO(), and BaseLib::MPI::OGS_COMM_WORLD.

Referenced by main().

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 115 of file ogs.cpp.

116{
118
119 // Initialize MPI
120 // also in python hook
121 // check tools
122 BaseLib::MPI::Setup mpi_setup(argc, argv);
125
126 signal(SIGINT, signalHandler); // CTRL+C
127 signal(SIGTERM, signalHandler); // pkill -SIGTERM <process_id> , It is NOT
128 // possible to catch SIGKILL
129
130#ifndef _WIN32 // TODO: On windows floating point exceptions are not handled
131 if (cli_arg.enable_fpe_is_set)
132 {
134 }
135#endif // _WIN32
136
137 INFO(
138 "This is OpenGeoSys-6 version {:s}. Log version: {:d}, Log level: "
139 "{:s}.",
142
143 std::optional<ApplicationsLib::TestDefinition> test_definition{
144 std::nullopt};
145 auto ogs_status = EXIT_SUCCESS;
146
147 try
148 {
149 pybind11::scoped_interpreter py_interpreter{
152
153 Simulation simulation(argc, argv);
154
155 BaseLib::RunTime run_time;
156 run_time.start();
157
158 bool solver_succeeded = false;
159 try
160 {
161 simulation.initializeDataStructures(
162 std::move(cli_arg.project),
163 std::move(cli_arg.xml_patch_file_names),
164 cli_arg.reference_path_is_set,
165 std::move(cli_arg.reference_path), cli_arg.nonfatal,
166 std::move(cli_arg.outdir), std::move(cli_arg.mesh_dir),
167 std::move(cli_arg.script_dir), cli_arg.write_prj);
168 solver_succeeded = simulation.executeSimulation();
169 simulation.outputLastTimeStep();
170 test_definition = simulation.getTestDefinition();
171 }
172 catch (pybind11::error_already_set const& e)
173 {
174 OGS_FATAL("Python exception thrown: {}", e.what());
175 }
176 if (solver_succeeded)
177 {
178 INFO("[time] Simulation completed. It took {:g} s.",
179 run_time.elapsed());
180 }
181 else
182 {
183 INFO("[time] Simulation failed. It took {:g} s.",
184 run_time.elapsed());
185 }
186
187 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
188 }
189 catch (std::exception& e)
190 {
191 ERR("{}", e.what());
192 ogs_status = EXIT_FAILURE;
193 }
194
195 // Check for swallowed ConfigTree errors after Simulation destructor runs.
196 // This catches configuration errors in objects destroyed at end of scope.
197 try
198 {
200 }
201 catch (std::exception& e)
202 {
203 ERR("{}", e.what());
204 ogs_status = EXIT_FAILURE;
205 }
206
207 if (ogs_status == EXIT_FAILURE)
208 {
209 auto const end_time = std::chrono::system_clock::now();
210 auto const time_str = BaseLib::formatDate(end_time);
211 ERR("OGS terminated with error on {:s}.", time_str);
212 return EXIT_FAILURE;
213 }
214
215 return Simulation::runTestDefinitions(test_definition);
216}
CommandLineArguments parseCommandLineArguments(int argc, char *argv[], bool const exit_on_exception)
#define OGS_FATAL(...)
Definition Error.h:19
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
void signalHandler(int signum)
static void assertNoSwallowedErrors()
Asserts that there have not been any errors reported in the destructor.
Count the running time.
Definition RunTime.h:18
double elapsed() const
Get the elapsed time in seconds.
Definition RunTime.h:31
void start()
Start the timer.
Definition RunTime.h:21
static OGS_EXPORT_SYMBOL int runTestDefinitions(std::optional< ApplicationsLib::TestDefinition > &test_definition)
pybind11::scoped_interpreter setupEmbeddedPython()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
bool createOutputDirectory(std::string const &dir)
GITINFOLIB_EXPORT const std::string ogs_version
void enableFloatingPointExceptions()
Definition ogs.cpp:39
void initializeLogger(bool const all_ranks_log)
Definition ogs.cpp:65
std::vector< std::string > xml_patch_file_names

References BaseLib::ConfigTree::assertNoSwallowedErrors(), BaseLib::createOutputDirectory(), BaseLib::RunTime::elapsed(), CommandLineArguments::enable_fpe_is_set, enableFloatingPointExceptions(), ERR(), Simulation::executeSimulation(), BaseLib::formatDate(), Simulation::getTestDefinition(), INFO(), Simulation::initializeDataStructures(), initializeLogger(), BaseLib::initOGSLogger(), CommandLineArguments::log_level, CommandLineArguments::log_parallel, CommandLineArguments::mesh_dir, CommandLineArguments::nonfatal, OGS_FATAL, GitInfoLib::GitInfo::ogs_version, CommandLineArguments::outdir, Simulation::outputLastTimeStep(), parseCommandLineArguments(), CommandLineArguments::project, CommandLineArguments::reference_path, CommandLineArguments::reference_path_is_set, Simulation::runTestDefinitions(), CommandLineArguments::script_dir, ApplicationsLib::setupEmbeddedPython(), ApplicationsLib::setupEmbeddedPythonVenvPaths(), signalHandler(), BaseLib::RunTime::start(), CommandLineArguments::write_prj, and CommandLineArguments::xml_patch_file_names.

◆ signalHandler()

void signalHandler ( int signum)

Definition at line 56 of file ogs.cpp.

57{
58 auto const end_time = std::chrono::system_clock::now();
59 auto const time_str = BaseLib::formatDate(end_time);
60
61 ERR("Simulation aborted on {:s}. Received signal: {:d}.", time_str, signum);
62 exit(signum);
63}

References ERR(), and BaseLib::formatDate().