OGS
ogs.cpp File Reference

Detailed Description

Implementation of OpenGeoSys simulation application.

Date
2014-08-04

Definition in file ogs.cpp.

#include <pybind11/pybind11.h>
#include <spdlog/spdlog.h>
#include <tclap/CmdLine.h>
#include <algorithm>
#include <chrono>
#include <csignal>
#include <iostream>
#include <sstream>
#include "CommandLineArgumentParser.h"
#include <cfenv>
#include "Applications/ApplicationsLib/Simulation.h"
#include "Applications/ApplicationsLib/TestDefinition.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 46 of file ogs.cpp.

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}

Referenced by main().

◆ initializeLogger()

void initializeLogger ( bool const all_ranks_log)

Definition at line 72 of file ogs.cpp.

73{
74#if defined(USE_PETSC)
75 int mpi_rank;
76 MPI_Comm_rank(BaseLib::MPI::OGS_COMM_WORLD, &mpi_rank);
77 int world_size;
78 MPI_Comm_size(BaseLib::MPI::OGS_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 {
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}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:15
std::string formatDate(std::chrono::time_point< std::chrono::system_clock > const &time)
std::shared_ptr< spdlog::logger > console
Definition Logging.cpp:32

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 122 of file ogs.cpp.

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)
#define OGS_FATAL(...)
Definition Error.h:26
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
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
bool createOutputDirectory(std::string const &dir)
GITINFOLIB_EXPORT const std::string ogs_version
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

References BaseLib::createOutputDirectory(), DBUG(), BaseLib::RunTime::elapsed(), CommandLineArguments::enable_fpe_is_set, enableFloatingPointExceptions(), ERR(), BaseLib::formatDate(), INFO(), initializeLogger(), BaseLib::initOGSLogger(), CommandLineArguments::log_level, CommandLineArguments::log_parallel, CommandLineArguments::mesh_dir, CommandLineArguments::nonfatal, OGS_FATAL, GitInfoLib::GitInfo::ogs_version, CommandLineArguments::outdir, parseCommandLineArguments(), CommandLineArguments::project, CommandLineArguments::reference_path, CommandLineArguments::reference_path_is_set, CommandLineArguments::script_dir, signalHandler(), simulation, BaseLib::RunTime::start(), CommandLineArguments::write_prj, and CommandLineArguments::xml_patch_file_names.

◆ signalHandler()

void signalHandler ( int signum)

Definition at line 63 of file ogs.cpp.

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}

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

Referenced by main().