OGS
Simulation.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "Simulation.h"
5
6#include <spdlog/spdlog.h>
7
13#include "BaseLib/DateTools.h"
14#include "BaseLib/FileTools.h"
16#include "BaseLib/RunTime.h"
17#include "MeshLib/Mesh.h"
19#include "ProcessLib/TimeLoop.h"
20
21Simulation::Simulation(int argc, char* argv[])
22 : linear_solver_library_setup{ApplicationsLib::LinearSolverLibrarySetup::
23 create(argc, argv)},
24#if defined(USE_PETSC)
25 controller(vtkSmartPointer<vtkMPIController>::New()),
26#endif
27 test_definition{std::nullopt}
28{
29#if defined(USE_PETSC)
30 controller->Initialize(&argc, &argv, 1);
31 vtkMPIController::SetGlobalController(controller);
32#endif
33}
34
36 std::string const& project,
37 std::vector<std::string> const& xml_patch_file_names,
38 bool const reference_path_is_set, std::string const& reference_path,
39 bool const nonfatal, std::string const& outdir, std::string const& mesh_dir,
40 std::string const& script_dir, bool const write_prj)
41{
42 INFO("Reading project file {}.",
43 std::filesystem::relative(project).string());
44 DBUG("Project file: {}.", std::filesystem::absolute(project).string());
45
46 std::stringstream prj_stream;
47 BaseLib::prepareProjectFile(prj_stream, project, xml_patch_file_names,
48 write_prj, outdir);
49 auto project_config = BaseLib::makeConfigTree(
50 project, !nonfatal, "OpenGeoSysProject", prj_stream);
51
52 if (!reference_path_is_set)
53 { // Ignore the test_definition section.
54 project_config.ignoreConfigParameter("test_definition");
55 }
56 else
57 {
60 project_config.getConfigSubtree("test_definition"), reference_path,
61 outdir);
62 if (test_definition->numberOfTests() == 0)
63 {
65 "No tests were constructed from the test definitions, "
66 "but reference solutions path was given.");
67 }
68
69 INFO("Cleanup possible output files before running ogs.");
70 BaseLib::removeFiles(test_definition->getOutputFiles());
71 }
72#ifdef OGS_USE_INSITU
74 if (auto t = project_config.getConfigSubtreeOptional("insitu"))
75 {
78 t->getConfigSubtree("scripts"),
79 BaseLib::extractPath(project));
80 isInsituConfigured = true;
81 }
82#else
83 project_config.ignoreConfigParameter("insitu");
84#endif
85
86 project_data = std::make_unique<ProjectData>(project_config, outdir,
87 mesh_dir, script_dir);
88
89 INFO("Initialize processes.");
90 for (auto& p : project_data->getProcesses())
91 {
92 p->initialize(project_data->getMedia());
93 }
94
95 // Check intermediately that config parsing went fine.
96 checkAndInvalidate(project_config);
98
99 auto& time_loop = project_data->getTimeLoop();
100 auto time_value = time_loop.currentTime()();
101 INFO("Time step #0 started. Time: {}. Step size: 0.", time_value);
102
103 BaseLib::RunTime init_timer;
104 init_timer.start();
105 time_loop.initialize();
106 INFO("Time step #0 took {:g} s.", init_timer.elapsed());
107}
108
110{
111 auto const& time_loop = project_data->getTimeLoop();
112 return time_loop.currentTime()();
113}
114
116{
117 auto const& time_loop = project_data->getTimeLoop();
118 return time_loop.endTime()();
119}
120
122{
123 auto& time_loop = project_data->getTimeLoop();
124 if (time_loop.currentTime() < time_loop.endTime())
125 {
126 auto const result = time_loop.executeTimeStep();
127 time_loop.calculateNextTimeStep();
128 return result;
129 }
130 return false;
131}
132
133MeshLib::Mesh& Simulation::getMesh(std::string const& name)
134{
135 return project_data->getMesh(name);
136}
137
138std::vector<std::string> Simulation::getMeshNames() const
139{
140 return project_data->getMeshNames();
141}
142
143static std::atomic<int> gSignalThatStoppedMe{-1};
144
145extern "C" void signalHandler(int signum)
146{
147 gSignalThatStoppedMe.store(signum);
148}
149
151{
152 INFO("Solve processes.");
153 auto& time_loop = project_data->getTimeLoop();
154 while (time_loop.currentTime() < time_loop.endTime())
155 {
156 time_loop.executeTimeStep();
157 if (!time_loop.calculateNextTimeStep())
158 {
159 break;
160 }
161 }
162
163 return time_loop.successful_time_step;
164}
165
167{
168 auto const& time_loop = project_data->getTimeLoop();
169 time_loop.outputLastTimeStep();
170}
171
172std::optional<ApplicationsLib::TestDefinition> Simulation::getTestDefinition()
173 const
174{
175 return test_definition;
176}
177
179{
180#ifdef OGS_USE_INSITU
182 {
184 }
185#endif
186#if defined(USE_PETSC)
187 controller->Finalize(1);
188#endif
189}
190
192 std::optional<ApplicationsLib::TestDefinition>& test_definition)
193{
194 if (!test_definition)
195 {
196 auto const end_time = std::chrono::system_clock::now();
197 auto const time_str = BaseLib::formatDate(end_time);
198 DBUG("No test definition was found. No tests will be executed.");
199 INFO("OGS completed on {:s}.", time_str);
200 return EXIT_SUCCESS;
201 }
202
203 INFO("");
204 INFO("##########################################");
205 INFO("# Running tests #");
206 INFO("##########################################");
207 INFO("");
208 auto status = test_definition->runTests();
209 auto const end_time = std::chrono::system_clock::now();
210 auto const time_str = BaseLib::formatDate(end_time);
211 if (status)
212 {
213 INFO("OGS completed on {:s}.", time_str);
214 }
215 else
216 {
217 ERR("OGS terminated on {:s}. One of the tests failed.", time_str);
218 return EXIT_FAILURE;
219 }
220 return EXIT_SUCCESS;
221}
#define OGS_FATAL(...)
Definition Error.h:19
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
void signalHandler(int signum)
static std::atomic< int > gSignalThatStoppedMe
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
OGS_EXPORT_SYMBOL double currentTime() const
std::shared_ptr< ApplicationsLib::LinearSolverLibrarySetup > linear_solver_library_setup
Definition Simulation.h:50
OGS_EXPORT_SYMBOL bool executeSimulation()
OGS_EXPORT_SYMBOL std::optional< ApplicationsLib::TestDefinition > getTestDefinition() const
std::unique_ptr< ProjectData > project_data
Definition Simulation.h:54
bool isInsituConfigured
Definition Simulation.h:57
static OGS_EXPORT_SYMBOL int runTestDefinitions(std::optional< ApplicationsLib::TestDefinition > &test_definition)
std::optional< ApplicationsLib::TestDefinition > test_definition
Definition Simulation.h:55
OGS_EXPORT_SYMBOL MeshLib::Mesh & getMesh(std::string const &name)
OGS_EXPORT_SYMBOL double endTime() const
OGS_EXPORT_SYMBOL ~Simulation()
OGS_EXPORT_SYMBOL void initializeDataStructures(std::string const &project, std::vector< std::string > const &xml_patch_file_names, bool reference_path_is_set, std::string const &reference_path, bool nonfatal, std::string const &outdir, std::string const &mesh_dir, std::string const &script_dir, bool write_prj)
OGS_EXPORT_SYMBOL bool executeTimeStep()
vtkSmartPointer< vtkMPIController > controller
Definition Simulation.h:52
OGS_EXPORT_SYMBOL void outputLastTimeStep() const
OGS_EXPORT_SYMBOL Simulation(int argc, char *argv[])
OGS_EXPORT_SYMBOL std::vector< std::string > getMeshNames() const
void prepareProjectFile(std::stringstream &prj_stream, const std::string &filepath, const std::vector< std::string > &patch_files, bool write_prj, const std::string &out_directory)
Applies includes and patch files to project file.
std::string extractPath(std::string const &pathname)
ConfigTree makeConfigTree(const std::string &filepath, const bool be_ruthless, const std::string &toplevel_tag, std::stringstream &prj_stream)
std::string formatDate(std::chrono::time_point< std::chrono::system_clock > const &time)
void removeFiles(std::vector< std::string > const &files)
void Initialize(BaseLib::ConfigTree const &scripts_config, std::string const &path)
Definition Adaptor.cpp:23
void Finalize()
Definition Adaptor.cpp:55