Loading [MathJax]/extensions/tex2jax.js
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/FileTools.h"
24#include "BaseLib/RunTime.h"
25#include "MeshLib/Mesh.h"
27#include "ProcessLib/TimeLoop.h"
28
29Simulation::Simulation(int argc, char* argv[])
30 : linear_solver_library_setup(argc, argv),
31#if defined(USE_PETSC)
32 controller(vtkSmartPointer<vtkMPIController>::New()),
33#endif
34 test_definition{std::nullopt}
35{
36#if defined(USE_PETSC)
37 controller->Initialize(&argc, &argv, 1);
38 vtkMPIController::SetGlobalController(controller);
39#endif
40}
41
43 std::string const& project,
44 std::vector<std::string> const& xml_patch_file_names,
45 bool const reference_path_is_set, std::string const& reference_path,
46 bool const nonfatal, std::string const& outdir, std::string const& mesh_dir,
47 std::string const& script_dir, bool const write_prj)
48{
49 INFO("Reading project file {}.",
50 std::filesystem::relative(project).string());
51 DBUG("Project file: {}.", std::filesystem::absolute(project).string());
52
53 std::stringstream prj_stream;
54 BaseLib::prepareProjectFile(prj_stream, project, xml_patch_file_names,
55 write_prj, outdir);
56 auto project_config = BaseLib::makeConfigTree(
57 project, !nonfatal, "OpenGeoSysProject", prj_stream);
58
59 if (!reference_path_is_set)
60 { // Ignore the test_definition section.
61 project_config.ignoreConfigParameter("test_definition");
62 }
63 else
64 {
67 project_config.getConfigSubtree("test_definition"), reference_path,
68 outdir);
69 if (test_definition->numberOfTests() == 0)
70 {
72 "No tests were constructed from the test definitions, "
73 "but reference solutions path was given.");
74 }
75
76 INFO("Cleanup possible output files before running ogs.");
77 BaseLib::removeFiles(test_definition->getOutputFiles());
78 }
79#ifdef OGS_USE_INSITU
81 if (auto t = project_config.getConfigSubtreeOptional("insitu"))
82 {
85 t->getConfigSubtree("scripts"),
86 BaseLib::extractPath(project));
87 isInsituConfigured = true;
88 }
89#else
90 project_config.ignoreConfigParameter("insitu");
91#endif
92
93 project_data = std::make_unique<ProjectData>(project_config,
95 outdir, mesh_dir, script_dir);
96
97 INFO("Initialize processes.");
98 for (auto& p : project_data->getProcesses())
99 {
100 p->initialize(project_data->getMedia());
101 }
102
103 // Check intermediately that config parsing went fine.
104 checkAndInvalidate(project_config);
106
107 auto& time_loop = project_data->getTimeLoop();
108 auto time_value = time_loop.currentTime()();
109 INFO("Time step #0 started. Time: {}. Step size: 0.", time_value);
110
111 BaseLib::RunTime init_timer;
112 init_timer.start();
113 time_loop.initialize();
114 INFO("Time step #0 took {:g} s.", init_timer.elapsed());
115}
116
118{
119 auto const& time_loop = project_data->getTimeLoop();
120 return time_loop.currentTime()();
121}
122
124{
125 auto const& time_loop = project_data->getTimeLoop();
126 return time_loop.endTime()();
127}
128
130{
131 auto& time_loop = project_data->getTimeLoop();
132 if (time_loop.currentTime() < time_loop.endTime())
133 {
134 auto const result = time_loop.executeTimeStep();
135 if (time_loop.calculateNextTimeStep())
136 {
137 time_loop.outputLastTimeStep();
138 }
139 return result;
140 }
141 return false;
142}
143
144MeshLib::Mesh& Simulation::getMesh(std::string const& name)
145{
146 return project_data->getMesh(name);
147}
148
149static std::atomic<int> gSignalThatStoppedMe{-1};
150
151extern "C" void signalHandler(int signum)
152{
153 gSignalThatStoppedMe.store(signum);
154}
155
157{
158 INFO("Solve processes.");
159 auto& time_loop = project_data->getTimeLoop();
160 while (time_loop.currentTime() < time_loop.endTime())
161 {
162 time_loop.executeTimeStep();
163 if (!time_loop.calculateNextTimeStep())
164 {
165 break;
166 }
167 }
168
169 return time_loop.successful_time_step;
170}
171
173{
174 auto const& time_loop = project_data->getTimeLoop();
175 time_loop.outputLastTimeStep();
176}
177
178std::optional<ApplicationsLib::TestDefinition> Simulation::getTestDefinition()
179 const
180{
181 return test_definition;
182}
183
185{
186#ifdef OGS_USE_INSITU
188 {
190 }
191#endif
192#if defined(USE_PETSC)
193 controller->Finalize(1);
194#endif
195}
#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
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:58
bool isInsituConfigured
Definition Simulation.h:61
std::optional< ApplicationsLib::TestDefinition > test_definition
Definition Simulation.h:59
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:56
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)
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