OGS
ogs.cpp
Go to the documentation of this file.
1
14#include <pybind11/pybind11.h>
15#include <spdlog/spdlog.h>
16#include <tclap/CmdLine.h>
17
18#include <algorithm>
19#include <chrono>
20#include <csignal>
21#include <iostream>
22#include <sstream>
23
25
26#ifndef _WIN32
27#ifdef __APPLE__
28#ifdef __SSE__
29#include <xmmintrin.h>
30#endif // __SSE__
31#else
32#include <cfenv>
33#endif // __APPLE__
34#endif // _WIN32
35
38#include "BaseLib/DateTools.h"
39#include "BaseLib/Error.h"
40#include "BaseLib/FileTools.h"
41#include "BaseLib/Logging.h"
42#include "BaseLib/RunTime.h"
43#include "InfoLib/GitInfo.h"
44
45#ifndef _WIN32 // On windows this command line option is not present.
47{
48#ifdef __APPLE__
49#ifdef __SSE__
50 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID);
51#endif // __SSE__
52#else
53 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
54#endif // __APPLE__
55}
56#endif // _WIN32
57
58#include <spdlog/sinks/null_sink.h>
59#include <spdlog/sinks/stdout_color_sinks.h>
60
61#include "BaseLib/MPI.h"
62
63void signalHandler(int signum)
64{
65 auto const end_time = std::chrono::system_clock::now();
66 auto const time_str = BaseLib::formatDate(end_time);
67
68 ERR("Simulation aborted on {:s}. Received signal: {:d}.", time_str, signum);
69 exit(signum);
70}
71
72void initializeLogger([[maybe_unused]] bool const all_ranks_log)
73{
74#if defined(USE_PETSC)
75 int mpi_rank;
76 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
77 int world_size;
78 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
79
80 if (all_ranks_log)
81 {
82 if (world_size > 1)
83 {
84 spdlog::set_pattern(fmt::format("[{}] %^%l:%$ %v", mpi_rank));
85 }
86 // else untouched
87 }
88 else // only rank 0 logs
89 {
90 // set_pattern is untouched
91 spdlog::drop_all();
92 if (mpi_rank > 0)
93 {
94 BaseLib::console = spdlog::create<spdlog::sinks::null_sink_st>(
95 "ogs"); // do not log
96 }
97 else // rank 0
98 {
99 auto console = BaseLib::console =
100 spdlog::stdout_color_st("ogs"); // st for performance
101 }
102 }
103
104 {
105 auto const start_time = std::chrono::system_clock::now();
106 auto const time_str = BaseLib::formatDate(start_time);
107 INFO("OGS started on {:s} with MPI. MPI processes: {:d}.", time_str,
108 world_size);
109 }
110
111#else // defined(USE_PETSC)
112
113 {
114 auto const start_time = std::chrono::system_clock::now();
115 auto const time_str = BaseLib::formatDate(start_time);
116 INFO("OGS started on {:s} in serial mode.", time_str);
117 }
118
119#endif
120}
121
122int main(int argc, char* argv[])
123{
125
126 // Initialize MPI
127 // also in python hook
128 // check tools
129 BaseLib::MPI::Setup mpi_setup(argc, argv);
132
133 signal(SIGINT, signalHandler); // CTRL+C
134 signal(SIGTERM, signalHandler); // pkill -SIGTERM <process_id> , It is NOT
135 // possible to catch SIGKILL
136
137#ifndef _WIN32 // TODO: On windows floating point exceptions are not handled
138 if (cli_arg.enable_fpe_is_set)
139 {
141 }
142#endif // _WIN32
143
144 INFO(
145 "This is OpenGeoSys-6 version {:s}. Log version: {:d}, Log level: "
146 "{:s}.",
149
150 std::optional<ApplicationsLib::TestDefinition> test_definition{
151 std::nullopt};
152 auto ogs_status = EXIT_SUCCESS;
153
154 try
155 {
156 Simulation simulation(argc, argv);
157
158 BaseLib::RunTime run_time;
159 run_time.start();
160
161 bool solver_succeeded = false;
162 try
163 {
164 simulation.initializeDataStructures(
165 std::move(cli_arg.project),
166 std::move(cli_arg.xml_patch_file_names),
167 cli_arg.reference_path_is_set,
168 std::move(cli_arg.reference_path), cli_arg.nonfatal,
169 std::move(cli_arg.outdir), std::move(cli_arg.mesh_dir),
170 std::move(cli_arg.script_dir), cli_arg.write_prj);
171 solver_succeeded = simulation.executeSimulation();
172 simulation.outputLastTimeStep();
173 test_definition = simulation.getTestDefinition();
174 }
175 catch (pybind11::error_already_set const& e)
176 {
177 OGS_FATAL("Python exception thrown: {}", e.what());
178 }
179 if (solver_succeeded)
180 {
181 INFO("[time] Simulation completed. It took {:g} s.",
182 run_time.elapsed());
183 }
184 else
185 {
186 INFO("[time] Simulation failed. It took {:g} s.",
187 run_time.elapsed());
188 }
189
190 ogs_status = solver_succeeded ? EXIT_SUCCESS : EXIT_FAILURE;
191 }
192 catch (std::exception& e)
193 {
194 ERR("{}", e.what());
195 ogs_status = EXIT_FAILURE;
196 }
197
198 if (ogs_status == EXIT_FAILURE)
199 {
200 auto const end_time = std::chrono::system_clock::now();
201 auto const time_str = BaseLib::formatDate(end_time);
202 ERR("OGS terminated with error on {:s}.", time_str);
203 return EXIT_FAILURE;
204 }
205
206 if (!test_definition)
207 {
208 auto const end_time = std::chrono::system_clock::now();
209 auto const time_str = BaseLib::formatDate(end_time);
210 DBUG("No test definition was found. No tests will be executed.");
211 INFO("OGS completed on {:s}.", time_str);
212 return ogs_status;
213 }
214
215 INFO("");
216 INFO("##########################################");
217 INFO("# Running tests #");
218 INFO("##########################################");
219 INFO("");
220 auto status = test_definition->runTests();
221 auto const end_time = std::chrono::system_clock::now();
222 auto const time_str = BaseLib::formatDate(end_time);
223 if (status)
224 {
225 INFO("OGS completed on {:s}.", time_str);
226 }
227 else
228 {
229 ERR("OGS terminated on {:s}. One of the tests failed.", time_str);
230 return EXIT_FAILURE;
231 }
232
233 return EXIT_SUCCESS;
234}
CommandLineArguments parseCommandLineArguments(int argc, char *argv[], bool const exit_on_exception)
Declaration of CommandLineArgumentParser.
Definition of date helper functions.
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
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)
std::shared_ptr< spdlog::logger > console
Definition Logging.cpp:32
bool createOutputDirectory(std::string const &dir)
GITINFOLIB_EXPORT const std::string ogs_version
int main(int argc, char *argv[])
Definition ogs.cpp:122
void signalHandler(int signum)
Definition ogs.cpp:63
void enableFloatingPointExceptions()
Definition ogs.cpp:46
void initializeLogger(bool const all_ranks_log)
Definition ogs.cpp:72
std::unique_ptr< Simulation > simulation
std::vector< std::string > xml_patch_file_names