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(argc, argv),
23#if defined(USE_PETSC)
24 controller(vtkSmartPointer<vtkMPIController>::New()),
25#endif
26 test_definition{std::nullopt}
27{
28#if defined(USE_PETSC)
29 controller->Initialize(&argc, &argv, 1);
30 vtkMPIController::SetGlobalController(controller);
31#endif
32}
33
35 std::string const& project,
36 std::vector<std::string> const& xml_patch_file_names,
37 bool const reference_path_is_set, std::string const& reference_path,
38 bool const nonfatal, std::string const& outdir, std::string const& mesh_dir,
39 std::string const& script_dir, bool const write_prj)
40{
41 INFO("Reading project file {}.",
42 std::filesystem::relative(project).string());
43 DBUG("Project file: {}.", std::filesystem::absolute(project).string());
44
45 std::stringstream prj_stream;
46 BaseLib::prepareProjectFile(prj_stream, project, xml_patch_file_names,
47 write_prj, outdir);
48 auto project_config = BaseLib::makeConfigTree(
49 project, !nonfatal, "OpenGeoSysProject", prj_stream);
50
51 if (!reference_path_is_set)
52 { // Ignore the test_definition section.
53 project_config.ignoreConfigParameter("test_definition");
54 }
55 else
56 {
59 project_config.getConfigSubtree("test_definition"), reference_path,
60 outdir);
61 if (test_definition->numberOfTests() == 0)
62 {
64 "No tests were constructed from the test definitions, "
65 "but reference solutions path was given.");
66 }
67
68 INFO("Cleanup possible output files before running ogs.");
69 BaseLib::removeFiles(test_definition->getOutputFiles());
70 }
71#ifdef OGS_USE_INSITU
73 if (auto t = project_config.getConfigSubtreeOptional("insitu"))
74 {
77 t->getConfigSubtree("scripts"),
78 BaseLib::extractPath(project));
79 isInsituConfigured = true;
80 }
81#else
82 project_config.ignoreConfigParameter("insitu");
83#endif
84
85 project_data = std::make_unique<ProjectData>(project_config, outdir,
86 mesh_dir, script_dir);
87
88 INFO("Initialize processes.");
89 for (auto& p : project_data->getProcesses())
90 {
91 p->initialize(project_data->getMedia());
92 }
93
94 // Check intermediately that config parsing went fine.
95 checkAndInvalidate(project_config);
97
98 auto& time_loop = project_data->getTimeLoop();
99 auto time_value = time_loop.currentTime()();
100 INFO("Time step #0 started. Time: {}. Step size: 0.", time_value);
101
102 BaseLib::RunTime init_timer;
103 init_timer.start();
104 time_loop.initialize();
105 INFO("Time step #0 took {:g} s.", init_timer.elapsed());
106}
107
109{
110 auto const& time_loop = project_data->getTimeLoop();
111 return time_loop.currentTime()();
112}
113
115{
116 auto const& time_loop = project_data->getTimeLoop();
117 return time_loop.endTime()();
118}
119
121{
122 auto& time_loop = project_data->getTimeLoop();
123 if (time_loop.currentTime() < time_loop.endTime())
124 {
125 auto const result = time_loop.executeTimeStep();
126 time_loop.calculateNextTimeStep();
127 return result;
128 }
129 return false;
130}
131
132MeshLib::Mesh& Simulation::getMesh(std::string const& name)
133{
134 return project_data->getMesh(name);
135}
136
137std::vector<std::string> Simulation::getMeshNames() const
138{
139 return project_data->getMeshNames();
140}
141
142static std::atomic<int> gSignalThatStoppedMe{-1};
143
144extern "C" void signalHandler(int signum)
145{
146 gSignalThatStoppedMe.store(signum);
147}
148
150{
151 INFO("Solve processes.");
152 auto& time_loop = project_data->getTimeLoop();
153 while (time_loop.currentTime() < time_loop.endTime())
154 {
155 time_loop.executeTimeStep();
156 if (!time_loop.calculateNextTimeStep())
157 {
158 break;
159 }
160 }
161
162 return time_loop.successful_time_step;
163}
164
166{
167 auto const& time_loop = project_data->getTimeLoop();
168 time_loop.outputLastTimeStep();
169}
170
171std::optional<ApplicationsLib::TestDefinition> Simulation::getTestDefinition()
172 const
173{
174 return test_definition;
175}
176
178{
179#ifdef OGS_USE_INSITU
181 {
183 }
184#endif
185#if defined(USE_PETSC)
186 controller->Finalize(1);
187#endif
188}
189
191 std::optional<ApplicationsLib::TestDefinition>& test_definition)
192{
193 if (!test_definition)
194 {
195 auto const end_time = std::chrono::system_clock::now();
196 auto const time_str = BaseLib::formatDate(end_time);
197 DBUG("No test definition was found. No tests will be executed.");
198 INFO("OGS completed on {:s}.", time_str);
199 return EXIT_SUCCESS;
200 }
201
202 INFO("");
203 INFO("##########################################");
204 INFO("# Running tests #");
205 INFO("##########################################");
206 INFO("");
207 auto status = test_definition->runTests();
208 auto const end_time = std::chrono::system_clock::now();
209 auto const time_str = BaseLib::formatDate(end_time);
210 if (status)
211 {
212 INFO("OGS completed on {:s}.", time_str);
213 }
214 else
215 {
216 ERR("OGS terminated on {:s}. One of the tests failed.", time_str);
217 return EXIT_FAILURE;
218 }
219 return EXIT_SUCCESS;
220}
#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
OGS_EXPORT_SYMBOL bool executeSimulation()
OGS_EXPORT_SYMBOL std::optional< ApplicationsLib::TestDefinition > getTestDefinition() const
std::unique_ptr< ProjectData > project_data
Definition Simulation.h:53
bool isInsituConfigured
Definition Simulation.h:56
static OGS_EXPORT_SYMBOL int runTestDefinitions(std::optional< ApplicationsLib::TestDefinition > &test_definition)
std::optional< ApplicationsLib::TestDefinition > test_definition
Definition Simulation.h:54
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)
ApplicationsLib::LinearSolverLibrarySetup linear_solver_library_setup
Definition Simulation.h:49
OGS_EXPORT_SYMBOL bool executeTimeStep()
vtkSmartPointer< vtkMPIController > controller
Definition Simulation.h:51
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