OGS
Simulation.cpp
Go to the documentation of this file.
1
13#include "Simulation.h"
14
15#include <spdlog/spdlog.h>
16
22#include "BaseLib/DateTools.h"
23#include "BaseLib/FileTools.h"
25#include "BaseLib/RunTime.h"
26#include "MeshLib/Mesh.h"
28#include "ProcessLib/TimeLoop.h"
29
30Simulation::Simulation(int argc, char* argv[])
31 : linear_solver_library_setup(argc, argv),
32#if defined(USE_PETSC)
33 controller(vtkSmartPointer<vtkMPIController>::New()),
34#endif
35 test_definition{std::nullopt}
36{
37#if defined(USE_PETSC)
38 controller->Initialize(&argc, &argv, 1);
39 vtkMPIController::SetGlobalController(controller);
40#endif
41}
42
44 std::string const& project,
45 std::vector<std::string> const& xml_patch_file_names,
46 bool const reference_path_is_set, std::string const& reference_path,
47 bool const nonfatal, std::string const& outdir, std::string const& mesh_dir,
48 std::string const& script_dir, bool const write_prj)
49{
50 INFO("Reading project file {}.",
51 std::filesystem::relative(project).string());
52 DBUG("Project file: {}.", std::filesystem::absolute(project).string());
53
54 std::stringstream prj_stream;
55 BaseLib::prepareProjectFile(prj_stream, project, xml_patch_file_names,
56 write_prj, outdir);
57 auto project_config = BaseLib::makeConfigTree(
58 project, !nonfatal, "OpenGeoSysProject", prj_stream);
59
60 if (!reference_path_is_set)
61 { // Ignore the test_definition section.
62 project_config.ignoreConfigParameter("test_definition");
63 }
64 else
65 {
68 project_config.getConfigSubtree("test_definition"), reference_path,
69 outdir);
70 if (test_definition->numberOfTests() == 0)
71 {
73 "No tests were constructed from the test definitions, "
74 "but reference solutions path was given.");
75 }
76
77 INFO("Cleanup possible output files before running ogs.");
78 BaseLib::removeFiles(test_definition->getOutputFiles());
79 }
80#ifdef OGS_USE_INSITU
82 if (auto t = project_config.getConfigSubtreeOptional("insitu"))
83 {
86 t->getConfigSubtree("scripts"),
87 BaseLib::extractPath(project));
88 isInsituConfigured = true;
89 }
90#else
91 project_config.ignoreConfigParameter("insitu");
92#endif
93
94 project_data = std::make_unique<ProjectData>(project_config,
96 outdir, mesh_dir, script_dir);
97
98 INFO("Initialize processes.");
99 for (auto& p : project_data->getProcesses())
100 {
101 p->initialize(project_data->getMedia());
102 }
103
104 // Check intermediately that config parsing went fine.
105 checkAndInvalidate(project_config);
107
108 auto& time_loop = project_data->getTimeLoop();
109 auto time_value = time_loop.currentTime()();
110 INFO("Time step #0 started. Time: {}. Step size: 0.", time_value);
111
112 BaseLib::RunTime init_timer;
113 init_timer.start();
114 time_loop.initialize();
115 INFO("Time step #0 took {:g} s.", init_timer.elapsed());
116}
117
119{
120 auto const& time_loop = project_data->getTimeLoop();
121 return time_loop.currentTime()();
122}
123
125{
126 auto const& time_loop = project_data->getTimeLoop();
127 return time_loop.endTime()();
128}
129
131{
132 auto& time_loop = project_data->getTimeLoop();
133 if (time_loop.currentTime() < time_loop.endTime())
134 {
135 auto const result = time_loop.executeTimeStep();
136 if (time_loop.calculateNextTimeStep())
137 {
138 time_loop.outputLastTimeStep();
139 }
140 return result;
141 }
142 return false;
143}
144
145MeshLib::Mesh& Simulation::getMesh(std::string const& name)
146{
147 return project_data->getMesh(name);
148}
149
150static std::atomic<int> gSignalThatStoppedMe{-1};
151
152extern "C" void signalHandler(int signum)
153{
154 gSignalThatStoppedMe.store(signum);
155}
156
158{
159 INFO("Solve processes.");
160 auto& time_loop = project_data->getTimeLoop();
161 while (time_loop.currentTime() < time_loop.endTime())
162 {
163 time_loop.executeTimeStep();
164 if (!time_loop.calculateNextTimeStep())
165 {
166 break;
167 }
168 }
169
170 return time_loop.successful_time_step;
171}
172
174{
175 auto const& time_loop = project_data->getTimeLoop();
176 time_loop.outputLastTimeStep();
177}
178
179std::optional<ApplicationsLib::TestDefinition> Simulation::getTestDefinition()
180 const
181{
182 return test_definition;
183}
184
186{
187#ifdef OGS_USE_INSITU
189 {
191 }
192#endif
193#if defined(USE_PETSC)
194 controller->Finalize(1);
195#endif
196}
197
199 std::optional<ApplicationsLib::TestDefinition>& test_definition)
200{
201 if (!test_definition)
202 {
203 auto const end_time = std::chrono::system_clock::now();
204 auto const time_str = BaseLib::formatDate(end_time);
205 DBUG("No test definition was found. No tests will be executed.");
206 INFO("OGS completed on {:s}.", time_str);
207 return EXIT_SUCCESS;
208 }
209
210 INFO("");
211 INFO("##########################################");
212 INFO("# Running tests #");
213 INFO("##########################################");
214 INFO("");
215 auto status = test_definition->runTests();
216 auto const end_time = std::chrono::system_clock::now();
217 auto const time_str = BaseLib::formatDate(end_time);
218 if (status)
219 {
220 INFO("OGS completed on {:s}.", time_str);
221 }
222 else
223 {
224 ERR("OGS terminated on {:s}. One of the tests failed.", time_str);
225 return EXIT_FAILURE;
226 }
227 return EXIT_SUCCESS;
228}
Definition of date helper functions.
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
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 Mesh class.
Definition of the RunTime class.
void signalHandler(int signum)
static std::atomic< int > gSignalThatStoppedMe
Declaration of class Simulation.
static void assertNoSwallowedErrors()
Asserts that there have not been any errors reported in the destructor.
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
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:61
bool isInsituConfigured
Definition Simulation.h:64
static OGS_EXPORT_SYMBOL int runTestDefinitions(std::optional< ApplicationsLib::TestDefinition > &test_definition)
std::optional< ApplicationsLib::TestDefinition > test_definition
Definition Simulation.h:62
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:59
OGS_EXPORT_SYMBOL void outputLastTimeStep() const
OGS_EXPORT_SYMBOL Simulation(int argc, char *argv[])
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 const & getProjectDirectory()
Returns the directory where the prj file resides.
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:30
void Finalize()
Definition Adaptor.cpp:62