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 "MeshLib/Mesh.h"
26#include "ProcessLib/TimeLoop.h"
27
28Simulation::Simulation(int argc, char* argv[])
29 : linear_solver_library_setup(argc, argv),
30#if defined(USE_PETSC)
31 controller(vtkSmartPointer<vtkMPIController>::New()),
32#endif
33 test_definition{std::nullopt}
34{
35#if defined(USE_PETSC)
36 controller->Initialize(&argc, &argv, 1);
37 vtkMPIController::SetGlobalController(controller);
38
39 { // Can be called only after MPI_INIT.
40 int mpi_rank;
41 MPI_Comm_rank(PETSC_COMM_WORLD, &mpi_rank);
42 spdlog::set_pattern(fmt::format("[{}] %^%l:%$ %v", mpi_rank));
43 }
44#endif
45}
46
48 std::string const& project,
49 std::vector<std::string> const& xml_patch_file_names,
50 bool const reference_path_is_set, std::string const& reference_path,
51 bool const nonfatal, std::string const& outdir, std::string const& mesh_dir,
52 std::string const& script_dir, bool const write_prj)
53{
54 INFO("Reading project file {}.",
55 std::filesystem::absolute(project).string());
56
57 std::stringstream prj_stream;
58 BaseLib::prepareProjectFile(prj_stream, project, xml_patch_file_names,
59 write_prj, outdir);
60 auto project_config = BaseLib::makeConfigTree(
61 project, !nonfatal, "OpenGeoSysProject", prj_stream);
62
63 if (!reference_path_is_set)
64 { // Ignore the test_definition section.
65 project_config.ignoreConfigParameter("test_definition");
66 }
67 else
68 {
71 project_config.getConfigSubtree("test_definition"), reference_path,
72 outdir);
73 if (test_definition->numberOfTests() == 0)
74 {
76 "No tests were constructed from the test definitions, "
77 "but reference solutions path was given.");
78 }
79
80 INFO("Cleanup possible output files before running ogs.");
81 BaseLib::removeFiles(test_definition->getOutputFiles());
82 }
83#ifdef OGS_USE_INSITU
85 if (auto t = project_config.getConfigSubtreeOptional("insitu"))
86 {
89 t->getConfigSubtree("scripts"),
90 BaseLib::extractPath(project));
91 isInsituConfigured = true;
92 }
93#else
94 project_config.ignoreConfigParameter("insitu");
95#endif
96
97 project_data = std::make_unique<ProjectData>(project_config,
99 outdir, mesh_dir, script_dir);
100
101 INFO("Initialize processes.");
102 for (auto& p : project_data->getProcesses())
103 {
104 p->initialize(project_data->getMedia());
105 }
106
107 // Check intermediately that config parsing went fine.
108 checkAndInvalidate(project_config);
110
111 auto& time_loop = project_data->getTimeLoop();
112 time_loop.initialize();
113}
114
116{
117 auto const& time_loop = project_data->getTimeLoop();
118 return time_loop.currentTime();
119}
120
122{
123 auto const& time_loop = project_data->getTimeLoop();
124 return time_loop.endTime();
125}
126
128{
129 auto& time_loop = project_data->getTimeLoop();
130 if (time_loop.currentTime() < time_loop.endTime())
131 {
132 auto const result = time_loop.executeTimeStep();
133 if (time_loop.calculateNextTimeStep())
134 {
135 time_loop.outputLastTimeStep();
136 }
137 return result;
138 }
139 return false;
140}
141
142MeshLib::Mesh& Simulation::getMesh(std::string const& name)
143{
144 return project_data->getMesh(name);
145}
146
148{
149 INFO("Solve processes.");
150 auto& time_loop = project_data->getTimeLoop();
151 while (time_loop.currentTime() < time_loop.endTime())
152 {
153 time_loop.executeTimeStep();
154 if (!time_loop.calculateNextTimeStep())
155 {
156 break;
157 }
158 }
159
160 return time_loop.successful_time_step;
161}
162
164{
165 auto const& time_loop = project_data->getTimeLoop();
166 time_loop.outputLastTimeStep();
167}
168
169std::optional<ApplicationsLib::TestDefinition> Simulation::getTestDefinition()
170 const
171{
172 return test_definition;
173}
174
176{
177#ifdef OGS_USE_INSITU
179 {
181 }
182#endif
183#if defined(USE_PETSC)
184 controller->Finalize(1);
185#endif
186}
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
Definition of the Mesh class.
Declaration of class Simulation.
static void assertNoSwallowedErrors()
Asserts that there have not been any errors reported in the destructor.
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)
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