MPS-Basic
Loading...
Searching...
No Matches
Improved Algorithms: Pressure Calculation

Pressure Calculation Methods

How It's Switched

Let's take a look at how the pressure calculation method is being modified.

Input

First, we specify which method to use in settings.yml:

# pressure calculation method
pressureCalculationMethod: Implicit # Implicit or Explicit
# for Implicit
compressibility: 0.45e-09
relaxationCoefficientForPressure: 0.2
# for Explicit
soundSpeed: 17.1

Loading

When executing the program, the location of settings.yml is passed through the --setting argument. Then main() function initiates Simulation class:

int main(int argc, char** argv) {
// -----
// -----
// -----
auto settingPath = fs::path(program.get<std::string>("--setting"));
auto outputDirectory = fs::path(program.get<std::string>("--output"));
Simulation simulation(settingPath, outputDirectory);
simulation.run();
return 0;
}
int main(int argc, char **argv)
entry point of the program
Definition main.cpp:19

Simulation::Simulation() receives settingPath, and make Loader load settings.yml. User-defined pressure calculation method is now set in input.settings.pressureCalculationMethod.

Simulation::Simulation(fs::path& settingPath, fs::path& outputDirectory) {
Input input = loader.load(settingPath, outputDirectory);
// -----
// -----
// -----
}
Input load(const fs::path &settingPath, const fs::path &outputDirectory)
Load the setting file and the particle file.
Definition loader.cpp:16
Loader loader
Simulation(std::filesystem::path &settingPath, std::filesystem::path &outputDirectory)
Represents the input data for MPS simulation.
Definition input.hpp:12

Assigning the Class

Then it moves on to switching the pressure method. This part consists of three steps.

  1. Create PressureCalculator::Interface class variable named pressureCalculator.
  2. User-specified Pressure Calculator class (Implicit, Explicit, ...) will be assigned to the pressureCalculator variable, using if statements.
  3. pressureCalculator will be handed over to MPS::MPS(), the constructor of MPS class. Inside MPS::MPS(), pressureCalculator will be copied to it's own public attributes.
Simulation::Simulation(fs::path& settingPath, fs::path& outputDirectory) {
// -----
// -----
// -----
std::unique_ptr<PressureCalculator::Interface> pressureCalculator;
if (input.settings.pressureCalculationMethod == "Implicit") {
pressureCalculator.reset(
// arguments for constructor
));
} else if (input.settings.pressureCalculationMethod == "Explicit") {
pressureCalculator.reset(
// arguments for constructor
));
}
mps = MPS(input, std::move(pressureCalculator));
// -----
// -----
// -----
}
MPS simulation class.
Definition mps.hpp:25
Class for explicit pressure calculation.
Definition explicit.hpp:14
Class for implicit pressure calculation.
Definition implicit.hpp:21
Settings settings
Settings for the simulation.
Definition input.hpp:13
std::string pressureCalculationMethod
Method for pressure calculation.
Definition settings.hpp:42
Note
Here we use "Interface Class" and "Smart Pointer". See Coding Techniques for more information.

Pressure Calculation

Finally, at each time step, MPS::stepForward() calls pressureCalculator that MPS class maintains, and execute pressure calculation.

// -----
// -----
// -----
auto pressures = pressureCalculator->calc(particles);
for (auto& particle : particles) {
particle.pressure = pressures[particle.id];
}
// -----
// -----
// -----
}
void stepForward()
Definition mps.cpp:32
std::unique_ptr< PressureCalculator::Interface > pressureCalculator
Interface for pressure calculation.
Definition mps.hpp:34
Particles particles
Particles in the simulation.
Definition mps.hpp:31