MPS-Basic
Loading...
Searching...
No Matches
Simulation Class Reference

#include <simulation.hpp>

Collaboration diagram for Simulation:

Public Member Functions

 Simulation (std::filesystem::path &settingPath, std::filesystem::path &outputDirectory)
 
void run ()
 

Public Attributes

MPS mps
 
Loader loader
 
Saver saver
 
std::chrono::system_clock::time_point realStartTime
 
std::chrono::system_clock::time_point realEndTime
 
double startTime
 
double time
 
double endTime
 
double dt
 
double outputPeriod
 
int timeStep = 0
 

Private Member Functions

void startSimulation ()
 
template<typename Rep , typename Period >
std::string calHourMinuteSecond (std::chrono::duration< Rep, Period > d)
 
void endSimulation ()
 
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.
 
bool saveCondition ()
 
std::string getCurrentTimeString ()
 

Detailed Description

Simulation class

This class is responsible for the simulation process. It loads the input, saves the results, and manages the time. It also contains the main loop of the simulation.

Definition at line 22 of file simulation.hpp.

Constructor & Destructor Documentation

◆ Simulation()

Simulation::Simulation ( std::filesystem::path & settingPath,
std::filesystem::path & outputDirectory )

Definition at line 17 of file simulation.cpp.

17 {
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}
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)
Loader loader
double outputPeriod
double time
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
Here is the call graph for this function:

Member Function Documentation

◆ run()

void Simulation::run ( )

Definition at line 29 of file simulation.cpp.

29 {
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}
void stepForward()
Definition mps.cpp:32
void save(const MPS &mps, const double time)
Definition saver.cpp:13
void startSimulation()
void endSimulation()
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.
bool saveCondition()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ startSimulation()

void Simulation::startSimulation ( )
private

Definition at line 50 of file simulation.cpp.

50 {
51 cout << endl;
52 cout << "*** START SIMULATION ***" << endl;
53 realStartTime = chrono::system_clock::now();
54}
std::chrono::system_clock::time_point realStartTime
Here is the caller graph for this function:

◆ calHourMinuteSecond()

template<typename Rep , typename Period >
std::string Simulation::calHourMinuteSecond ( std::chrono::duration< Rep, Period > d)
inlineprivate

Definition at line 42 of file simulation.hpp.

42 {
43 auto h = std::chrono::duration_cast<std::chrono::hours>(d);
44 d -= h;
45 auto m = std::chrono::duration_cast<std::chrono::minutes>(d);
46 d -= m;
47 auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
48
49 std::stringstream out;
50 out << h.count() << "h ";
51 out << std::setfill('0') << std::setw(2) << m.count() << "m " << s.count() << "s";
52 return out.str();
53 }
Here is the caller graph for this function:

◆ endSimulation()

void Simulation::endSimulation ( )
private

Definition at line 56 of file simulation.cpp.

56 {
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}
std::chrono::system_clock::time_point realEndTime
std::string calHourMinuteSecond(std::chrono::duration< Rep, Period > d)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ timeStepReport()

void Simulation::timeStepReport ( const std::chrono::system_clock::time_point & timeStepStartTime,
const std::chrono::system_clock::time_point & timeStepEndTime )
private

Report time step information to the console.

Definition at line 65 of file simulation.cpp.

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}
double courant
Maximum courant number among all particles.
Definition mps.hpp:36
int getFileNumber() const
Definition saver.cpp:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ saveCondition()

bool Simulation::saveCondition ( )
private

Definition at line 105 of file simulation.cpp.

105 {
106 // NOTE: Is fileNumber really necessary?
107 return time - startTime >= outputPeriod * double(saver.getFileNumber());
108}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getCurrentTimeString()

std::string Simulation::getCurrentTimeString ( )
private

Definition at line 111 of file simulation.cpp.

111 {
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}

Member Data Documentation

◆ mps

MPS Simulation::mps

Definition at line 26 of file simulation.hpp.

◆ loader

Loader Simulation::loader

Definition at line 27 of file simulation.hpp.

◆ saver

Saver Simulation::saver

Definition at line 28 of file simulation.hpp.

◆ realStartTime

std::chrono::system_clock::time_point Simulation::realStartTime

Definition at line 30 of file simulation.hpp.

◆ realEndTime

std::chrono::system_clock::time_point Simulation::realEndTime

Definition at line 30 of file simulation.hpp.

◆ startTime

double Simulation::startTime

Definition at line 31 of file simulation.hpp.

◆ time

double Simulation::time

Definition at line 31 of file simulation.hpp.

◆ endTime

double Simulation::endTime

Definition at line 31 of file simulation.hpp.

◆ dt

double Simulation::dt

Definition at line 31 of file simulation.hpp.

◆ outputPeriod

double Simulation::outputPeriod

Definition at line 32 of file simulation.hpp.

◆ timeStep

int Simulation::timeStep = 0

Definition at line 33 of file simulation.hpp.


The documentation for this class was generated from the following files: