MPS-Basic
Loading...
Searching...
No Matches
simulation.cpp
Go to the documentation of this file.
1#include "simulation.hpp"
2
3#include "input.hpp"
4#include "mps_factory.hpp"
6
7#include <cstdio>
8#include <iostream>
9#include <memory>
10
11using std::cerr;
12using std::cout;
13using std::endl;
14namespace fs = std::filesystem;
15namespace chrono = std::chrono;
16
17Simulation::Simulation(fs::path& settingPath, fs::path& outputDirectory) {
18 Input input = loader.load(settingPath, outputDirectory);
19 saver = Saver(outputDirectory, input.settings.outputVtkInBinary);
20
21 mps = MPSFactory::create(input);
22 startTime = input.startTime;
24 endTime = input.settings.endTime;
25 dt = input.settings.dt;
27}
28
32
33 while (time < endTime) {
34 auto timeStepStartTime = chrono::system_clock::now();
35
37 timeStep++;
38 time += dt;
39
40 auto timeStepEndTime = chrono::system_clock::now();
41
42 timeStepReport(timeStepStartTime, timeStepEndTime);
43 if (saveCondition()) {
45 }
46 }
48}
49
51 cout << endl;
52 cout << "*** START SIMULATION ***" << endl;
53 realStartTime = chrono::system_clock::now();
54}
55
57 realEndTime = chrono::system_clock::now();
58 cout << endl;
59 cout << "Total Simulation time = " << calHourMinuteSecond(realEndTime - realStartTime) << endl;
60
61 cout << endl;
62 cout << "*** END SIMULATION ***" << endl;
63}
64
66 const chrono::system_clock::time_point& timeStepStartTime, const chrono::system_clock::time_point& timeStepEndTime
67) {
68 auto elapsedTime = timeStepEndTime - realStartTime;
69 auto elapsed = "elapsed=" + calHourMinuteSecond(elapsedTime);
70
71 double ave = 0.0;
72 if (timeStep != 0) {
73 ave = (double) (chrono::duration_cast<chrono::nanoseconds>(elapsedTime).count()) / (timeStep * 1e9);
74 }
75
76 std::string remain = "remain=";
77 if (timeStep == 0) {
78 remain += "-h --m --s";
79 } else {
80 auto totalTime = chrono::nanoseconds((int64_t) (ave * (endTime - startTime) / dt * 1e9));
81 remain += calHourMinuteSecond(totalTime - elapsedTime);
82 }
83 double last = chrono::duration_cast<chrono::nanoseconds>(timeStepEndTime - timeStepStartTime).count() * 1e-9;
84
85 // terminal output
86 printf(
87 "%d: dt=%.gs t=%.3lfs fin=%.1lfs %s %s ave=%.3lfs/step "
88 "last=%.3lfs/step out=%dfiles Courant=%.2lf\n",
90 dt,
91 time,
92 endTime,
93 elapsed.c_str(),
94 remain.c_str(),
95 ave,
96 last,
99 );
100
101 // error output
102 fprintf(stderr, "%4d: t=%.3lfs\n", timeStep, time);
103}
104
106 // NOTE: Is fileNumber really necessary?
107 return time - startTime >= outputPeriod * double(saver.getFileNumber());
108}
109
110// NOTE: If this function is also needed in other classes, it should be moved to a separate file.
112 auto currentTime = chrono::system_clock::to_time_t(chrono::system_clock::now());
113 std::tm* timeInfo = std::localtime(&currentTime);
114 std::stringstream formattedTime;
115 formattedTime << std::put_time(timeInfo, "%Y-%m-%d_%H-%M-%S");
116 std::string formattedTimeString = formattedTime.str();
117
118 return formattedTimeString;
119}
Input load(const fs::path &settingPath, const fs::path &outputDirectory)
Load the setting file and the particle file.
Definition loader.cpp:16
static MPS create(const Input &input)
void stepForward()
Definition mps.cpp:32
double courant
Maximum courant number among all particles.
Definition mps.hpp:36
int getFileNumber() const
Definition saver.cpp:34
void save(const MPS &mps, const double time)
Definition saver.cpp:13
void startSimulation()
void endSimulation()
Loader loader
std::chrono::system_clock::time_point realEndTime
std::string getCurrentTimeString()
std::string calHourMinuteSecond(std::chrono::duration< Rep, Period > d)
double outputPeriod
double time
std::chrono::system_clock::time_point realStartTime
void timeStepReport(const std::chrono::system_clock::time_point &timeStepStartTime, const std::chrono::system_clock::time_point &timeStepEndTime)
Report time step information to the console.
Simulation(std::filesystem::path &settingPath, std::filesystem::path &outputDirectory)
bool saveCondition()
double startTime
double endTime
Represents the input data for MPS simulation.
Definition input.hpp:12
Settings settings
Settings for the simulation.
Definition input.hpp:13
double startTime
Start time of the simulation.
Definition input.hpp:15
double outputPeriod
Output period of the simulation.
Definition settings.hpp:21
bool outputVtkInBinary
Flag for saving VTK file in binary format.
Definition settings.hpp:61
double endTime
End time of the simulation.
Definition settings.hpp:20
double dt
Time step.
Definition settings.hpp:19