MPS-Basic
Loading...
Searching...
No Matches
loader.cpp
Go to the documentation of this file.
1#include "loader.hpp"
2
6
7#include <cmath>
8#include <iostream>
9#include <yaml-cpp/yaml.h>
10
11using std::cerr;
12using std::cout;
13using std::endl;
14
15namespace fs = std::filesystem;
16
17Input Loader::load(const fs::path& settingPath, const fs::path& outputDirectory) {
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}
33
40std::unique_ptr<ParticlesLoader::Interface> Loader::getParticlesLoader(const fs::path& particlesPath) {
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}
53
60void Loader::copyInputFileToOutputDirectory(const fs::path& inputFilePath, const fs::path& outputDirectory) {
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}
69
70Settings Loader::loadSettingYaml(const fs::path& settingPath) {
71 YAML::Node yaml = YAML::LoadFile(settingPath.string());
72
73 Settings s;
74
75 // computational conditions
76 s.dim = yaml["dim"].as<int>();
77 s.particleDistance = yaml["particleDistance"].as<double>();
78 s.dt = yaml["dt"].as<double>();
79 s.endTime = yaml["endTime"].as<double>();
80 s.outputPeriod = yaml["outputPeriod"].as<double>();
81 s.cflCondition = yaml["cflCondition"].as<double>();
82
83 // physical properties
84 s.defaultDensity = yaml["defaultDensity"].as<double>();
85 s.kinematicViscosity = yaml["kinematicViscosity"].as<double>();
86
87 // gravity
88 s.xyzInput = yaml["xyz-input"] ? yaml["xyz-input"].as<bool>() : true;
89 s.gravity[0] = yaml["gravity"][0].as<double>();
90 s.gravity[1] = yaml["gravity"][1].as<double>();
91 s.gravity[2] = yaml["gravity"][2].as<double>();
92 s.gNorm = yaml["gravity-norm"] ? yaml["gravity-norm"].as<double>() : 0.0;
93 s.gAngle = yaml["gravity-angle"] ? yaml["gravity-angle"].as<double>() : 0.0;
94
95 // free surface detection
96 s.surfaceDetection_numberDensity_threshold = yaml["surfaceDetection-numberDensity-threshold"].as<double>();
97 s.surfaceDetection_particleDistribution = yaml["surfaceDetection-particleDistribution"].as<bool>();
99 yaml["surfaceDetection-particleDistribution-threshold"].as<double>();
100
101 // pressure calculation method
102 s.pressureCalculationMethod = yaml["pressureCalculationMethod"].as<std::string>();
103 // for Implicit
104 s.compressibility = yaml["compressibility"].as<double>();
105 s.relaxationCoefficientForPressure = yaml["relaxationCoefficientForPressure"].as<double>();
106 // for Explicit
107 s.soundSpeed = yaml["soundSpeed"].as<double>();
108
109 // collision
110 s.collisionDistance = yaml["collisionDistanceRatio"].as<double>() * s.particleDistance;
111 s.coefficientOfRestitution = yaml["coefficientOfRestitution"].as<double>();
112
113 // effective radius
114 s.re_forNumberDensity = yaml["radiusRatioForNumberDensity"].as<double>() * s.particleDistance;
115 s.re_forGradient = yaml["radiusRatioForGradient"].as<double>() * s.particleDistance;
116 s.re_forLaplacian = yaml["radiusRatioForLaplacian"].as<double>() * s.particleDistance;
118
119 // domain
120 s.domain.xMin = yaml["domainMin"][0].as<double>();
121 s.domain.xMax = yaml["domainMax"][0].as<double>();
122 s.domain.yMin = yaml["domainMin"][1].as<double>();
123 s.domain.yMax = yaml["domainMax"][1].as<double>();
124 s.domain.zMin = yaml["domainMin"][2].as<double>();
125 s.domain.zMax = yaml["domainMax"][2].as<double>();
129
130 // particlesPath
131 auto yamlDir = settingPath.parent_path();
132 auto relativeProfPath = yaml["particlesPath"].as<std::string>();
133 s.particlesPath = fs::weakly_canonical(yamlDir / relativeProfPath);
134
135 // outputVtkFormatInBinary
136 // check if outputVtkFormat is defined in the yaml file since it is optional
137 if (yaml["outputVtkInBinary"]) {
138 s.outputVtkInBinary = yaml["outputVtkInBinary"].as<bool>();
139 } else {
140 s.outputVtkInBinary = false;
141 }
142 return s;
143}
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
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
Input load(const fs::path &settingPath, const fs::path &outputDirectory)
Load the setting file and the particle file.
Definition loader.cpp:17
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
Struct for settings of calculation.
Definition settings.hpp:15
Eigen::Vector3d gravity
Gravity vector when using xyz input.
Definition settings.hpp:32
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
std::filesystem::path particlesPath
Path for input particle file.
Definition settings.hpp:62
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
double defaultDensity
default density for fluid and wall particles.
Definition settings.hpp:28
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
bool xyzInput
Definition settings.hpp:31
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