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

Class for loading setting file and particle file. More...

#include <loader.hpp>

Public Member Functions

 Loader ()=default
 
Input load (const fs::path &settingPath, const fs::path &outputDirectory)
 Load the setting file and the particle file.
 

Private Member Functions

std::unique_ptr< ParticlesLoader::Interface > getParticlesLoader (const fs::path &particlesPath)
 Get the Particles Loader object according to the input file extension.
 
void copyInputFileToOutputDirectory (const fs::path &inputFilePath, const fs::path &outputDirectory)
 Copy the input file to the output directory.
 
Settings loadSettingYaml (const fs::path &settingPath)
 

Private Attributes

std::unique_ptr< ParticlesLoader::Interface > particlesLoader
 

Detailed Description

Class for loading setting file and particle file.

This class is responsible for loading the input. It loads the settings and the particles from the file system. It uses the YAML library to load the settings from a YAML file. It also uses the std::filesystem library to load the particles from a file.

Definition at line 22 of file loader.hpp.

Constructor & Destructor Documentation

◆ Loader()

Loader::Loader ( )
default

Member Function Documentation

◆ load()

Input Loader::load ( const fs::path & settingPath,
const fs::path & outputDirectory )

Load the setting file and the particle file.

Note
This function copy the setting file and the particle file to the output directory.
Parameters
settingPathPath to the setting file
outputDirectoryPath to the output directory
Returns
Input object

Definition at line 17 of file loader.cpp.

17 {
18 Input input;
19
20 input.settings = loadSettingYaml(settingPath);
21
22 auto particlesPath = input.settings.particlesPath;
23 this->particlesLoader = getParticlesLoader(particlesPath);
24 auto [startTime, particles] = this->particlesLoader->load(particlesPath, input.settings.defaultDensity);
25 input.startTime = startTime;
26 input.particles = particles;
27
28 copyInputFileToOutputDirectory(settingPath, outputDirectory);
29 copyInputFileToOutputDirectory(particlesPath, outputDirectory);
30
31 return input;
32}
std::unique_ptr< ParticlesLoader::Interface > particlesLoader
Definition loader.hpp:37
std::unique_ptr< ParticlesLoader::Interface > getParticlesLoader(const fs::path &particlesPath)
Get the Particles Loader object according to the input file extension.
Definition loader.cpp:40
void copyInputFileToOutputDirectory(const fs::path &inputFilePath, const fs::path &outputDirectory)
Copy the input file to the output directory.
Definition loader.cpp:60
Settings loadSettingYaml(const fs::path &settingPath)
Definition loader.cpp:70
Represents the input data for MPS simulation.
Definition input.hpp:12
Particles particles
Initial particles arrangement in the simulation.
Definition input.hpp:14
Settings settings
Settings for the simulation.
Definition input.hpp:13
double startTime
Start time of the simulation.
Definition input.hpp:15
std::filesystem::path particlesPath
Path for input particle file.
Definition settings.hpp:62
double defaultDensity
default density for fluid and wall particles.
Definition settings.hpp:28
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getParticlesLoader()

std::unique_ptr< ParticlesLoader::Interface > Loader::getParticlesLoader ( const fs::path & particlesPath)
private

Get the Particles Loader object according to the input file extension.

Parameters
particlesPathPath to the particles file
Returns
std::unique_ptr<ParticlesLoader::Interface> Particles Loader object

Supported file formats: prof, csv

Definition at line 40 of file loader.cpp.

40 {
41 auto extension = particlesPath.extension();
42 if (extension == ".csv") {
43 return std::make_unique<ParticlesLoader::Csv>();
44 } else if (extension == ".prof") {
45 return std::make_unique<ParticlesLoader::Prof>();
46 } else if (extension == ".vtu") {
47 return std::make_unique<ParticlesLoader::Vtu>();
48 } else {
49 cerr << "unsupported file format: " << particlesPath.extension() << endl;
50 std::exit(-1);
51 }
52}
Here is the caller graph for this function:

◆ copyInputFileToOutputDirectory()

void Loader::copyInputFileToOutputDirectory ( const fs::path & inputFilePath,
const fs::path & outputDirectory )
private

Copy the input file to the output directory.

Parameters
inputFilePathPath to the input file
outputDirectoryPath to the output directory
Warning
If the file already exists in the output directory, the program will exit.

Definition at line 60 of file loader.cpp.

60 {
61 auto outputFilePath = outputDirectory / inputFilePath.filename();
62 if (fs::exists(outputFilePath)) {
63 cerr << "file " << outputFilePath << " already exists in the output directory" << endl;
64 std::exit(-1);
65 } else {
66 fs::copy_file(inputFilePath, outputFilePath);
67 }
68}
Here is the caller graph for this function:

◆ loadSettingYaml()

Settings Loader::loadSettingYaml ( const fs::path & settingPath)
private

Definition at line 70 of file loader.cpp.

70 {
71 YAML::Node yaml = YAML::LoadFile(settingPath.string());
72
73 Settings s;
74
75 // computational conditions
76 YAML::Node comp = yaml["computationalConditions"];
77 s.dim = comp["dim"].as<int>();
78 s.particleDistance = comp["particleDistance"].as<double>();
79 s.dt = comp["dt"].as<double>();
80 s.endTime = comp["endTime"].as<double>();
81 s.outputPeriod = comp["outputPeriod"].as<double>();
82 s.cflCondition = comp["cflCondition"].as<double>();
83
84 // physical properties
85 YAML::Node phys = yaml["physicalProperties"];
86 s.defaultDensity = phys["defaultDensity"].as<double>();
87 s.kinematicViscosity = phys["kinematicViscosity"].as<double>();
88
89 // gravity
90 YAML::Node grav = yaml["gravity"];
91 s.gravityMethod = grav["method"] ? grav["method"].as<std::string>() : "xyz";
92 if (s.gravityMethod == "xyz") {
93 s.gravity[0] = grav["xyz"][0].as<double>();
94 s.gravity[1] = grav["xyz"][1].as<double>();
95 s.gravity[2] = grav["xyz"][2].as<double>();
96 } else if (s.gravityMethod == "normAndAngle") {
97 s.gNorm = grav["normAndAngle"]["norm"] ? grav["normAndAngle"]["norm"].as<double>() : 0.0;
98 s.gAngle = grav["normAndAngle"]["angle"] ? grav["normAndAngle"]["angle"].as<double>() : 0.0;
99 } else {
100 cerr << "unsupported gravity method: " << s.gravityMethod << endl;
101 std::exit(-1);
102 }
103
104 // free surface detection
105 YAML::Node surf = yaml["surfaceDetection"];
106 s.surfaceDetection_numberDensity_threshold = surf["numberDensity"]["threshold"].as<double>();
107 s.surfaceDetection_particleDistribution = surf["particleDistribution"]["enabled"].as<bool>();
108 s.surfaceDetection_particleDistribution_threshold = surf["particleDistribution"]["threshold"].as<double>();
109
110 // pressure calculation method
111 YAML::Node press = yaml["pressureCalculation"];
112 s.pressureCalculationMethod = press["method"].as<std::string>();
113 if (press["method"].as<std::string>() == "Implicit") {
114 // for Implicit
115 s.compressibility = press["implicit"]["compressibility"].as<double>();
116 s.relaxationCoefficientForPressure = press["implicit"]["relaxationCoefficient"].as<double>();
117 } else if (press["method"].as<std::string>() == "Explicit") {
118 // for Explicit
119 s.soundSpeed = press["explicit"]["soundSpeed"].as<double>();
120 } else {
121 cerr << "unsupported pressure calculation method: " << press["method"].as<std::string>() << endl;
122 std::exit(-1);
123 }
124
125 // collision
126 YAML::Node coll = yaml["collision"];
127 s.collisionDistance = coll["distanceRatio"].as<double>() * s.particleDistance;
128 s.coefficientOfRestitution = coll["coefficientOfRestitution"].as<double>();
129
130 // effective radius
131 YAML::Node re = yaml["effectiveRadius"];
132 s.re_forNumberDensity = re["ratioForNumberDensity"].as<double>() * s.particleDistance;
133 s.re_forGradient = re["ratioForGradient"].as<double>() * s.particleDistance;
134 s.re_forLaplacian = re["ratioForLaplacian"].as<double>() * s.particleDistance;
136
137 // domain
138 YAML::Node dom = yaml["domain"];
139 s.domain.xMin = dom["min"][0].as<double>();
140 s.domain.xMax = dom["max"][0].as<double>();
141 s.domain.yMin = dom["min"][1].as<double>();
142 s.domain.yMax = dom["max"][1].as<double>();
143 s.domain.zMin = dom["min"][2].as<double>();
144 s.domain.zMax = dom["max"][2].as<double>();
148
149 // i/o
150 YAML::Node io = yaml["io"];
151 auto yamlDir = settingPath.parent_path();
152 auto relativeProfPath = io["particlesPath"].as<std::string>();
153 s.particlesPath = fs::weakly_canonical(yamlDir / relativeProfPath);
154
155 // outputVtkFormatInBinary
156 s.outputVtkInBinary = io["outputVtkInBinary"] ? io["outputVtkInBinary"].as<bool>() : false;
157 return s;
158}
double xMax
maximum x coordinate of the domain
Definition domain.hpp:12
double zMin
minimum z coordinate of the domain
Definition domain.hpp:15
double zMax
maximum z coordinate of the domain
Definition domain.hpp:16
double xMin
minimum x coordinate of the domain
Definition domain.hpp:11
double yMin
minimum y coordinate of the domain
Definition domain.hpp:13
double zLength
Definition domain.hpp:17
double yLength
Definition domain.hpp:17
double yMax
maximum y coordinate of the domain
Definition domain.hpp:14
double xLength
Definition domain.hpp:17
Struct for settings of calculation.
Definition settings.hpp:15
Eigen::Vector3d gravity
Gravity vector when using xyz input.
Definition settings.hpp:32
std::string gravityMethod
Method for gravity input.
Definition settings.hpp:31
double reMax
Maximum of effective radius.
Definition settings.hpp:59
double outputPeriod
Output period of the simulation.
Definition settings.hpp:21
double soundSpeed
Speed of sound for Explicit method.
Definition settings.hpp:49
double compressibility
Compressibility of the fluid for Implicit method.
Definition settings.hpp:46
double collisionDistance
Distance for collision detection.
Definition settings.hpp:52
double particleDistance
Initial distance between particles.
Definition settings.hpp:18
bool outputVtkInBinary
Flag for saving VTK file in binary format.
Definition settings.hpp:63
double endTime
End time of the simulation.
Definition settings.hpp:20
int dim
Dimension of the simulation.
Definition settings.hpp:17
double relaxationCoefficientForPressure
Relaxation coefficient for pressure for Implicit method.
Definition settings.hpp:47
double re_forNumberDensity
Effective radius for number density.
Definition settings.hpp:56
double gNorm
Norm of gravity when using norm and angle.
Definition settings.hpp:33
bool surfaceDetection_particleDistribution
flag for free surface detection based on particle distribution
Definition settings.hpp:40
double gAngle
Angle of gravity when using norm and angle.
Definition settings.hpp:34
std::string pressureCalculationMethod
Method for pressure calculation.
Definition settings.hpp:44
double surfaceDetection_particleDistribution_threshold
Definition settings.hpp:41
double kinematicViscosity
Kinematic viscosity.
Definition settings.hpp:27
double cflCondition
CFL condition.
Definition settings.hpp:22
double coefficientOfRestitution
Coefficient of restitution.
Definition settings.hpp:53
double surfaceDetection_numberDensity_threshold
threshold ratio of number density for free surface detection
Definition settings.hpp:38
double re_forGradient
Effective radius for gradient.
Definition settings.hpp:57
double dt
Time step.
Definition settings.hpp:19
Domain domain
domain of the simulation
Definition settings.hpp:24
double re_forLaplacian
Effective radius for Laplacian.
Definition settings.hpp:58
Here is the caller graph for this function:

Member Data Documentation

◆ particlesLoader

std::unique_ptr<ParticlesLoader::Interface> Loader::particlesLoader
private

Definition at line 37 of file loader.hpp.


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