16std::pair<double, Particles>
Vtu::load(
const fs::path& path,
double defaultDensity) {
17 std::ifstream ifs(path);
19 cerr <<
"cannot read vtu file: " << fs::absolute(path) << endl;
25 while (std::getline(ifs, line)) {
26 if (line.find(
"<AppendedData") != std::string::npos) {
27 cerr <<
"Error: Binary VTU files are not supported: " << fs::absolute(path) << endl;
34 ifs.seekg(0, std::ios::beg);
37 double startTime = NAN;
40 std::vector<Eigen::Vector3d> positions;
41 std::vector<Eigen::Vector3d> velocities;
42 std::vector<int> types;
43 std::vector<int> fluidTypes;
44 std::vector<double> densities;
47 while (std::getline(ifs, line)) {
49 if (line.find(
"Time") != std::string::npos) {
50 std::getline(ifs, line);
51 startTime = std::stod(line);
55 if (line.find(
"Position") != std::string::npos) {
56 while (std::getline(ifs, line) && line.find(
"</DataArray>") == std::string::npos) {
57 std::istringstream iss(line);
59 if (!(iss >> x >> y >> z)) {
60 throw std::runtime_error(
"Invalid position data format");
62 positions.emplace_back(x, y, z);
67 if (line.find(
"Velocity") != std::string::npos) {
68 while (std::getline(ifs, line) && line.find(
"</DataArray>") == std::string::npos) {
69 std::istringstream iss(line);
71 if (!(iss >> vx >> vy >> vz)) {
72 throw std::runtime_error(
"Invalid velocity data format");
74 velocities.emplace_back(vx, vy, vz);
79 if (line.find(
"Particle Type") != std::string::npos) {
80 while (std::getline(ifs, line) && line.find(
"</DataArray>") == std::string::npos) {
82 std::istringstream iss(line);
84 throw std::runtime_error(
"Invalid particle type data format");
86 types.push_back(type);
91 if (line.find(
"Fluid Type") != std::string::npos) {
92 while (std::getline(ifs, line) && line.find(
"</DataArray>") == std::string::npos) {
94 std::istringstream iss(line);
95 if (!(iss >> fluidType)) {
96 throw std::runtime_error(
"Invalid fluid type data format");
98 fluidTypes.push_back(fluidType);
103 if (line.find(
"Density") != std::string::npos) {
104 while (std::getline(ifs, line) && line.find(
"</DataArray>") == std::string::npos) {
106 std::istringstream iss(line);
107 if (!(iss >> density)) {
108 throw std::runtime_error(
"Invalid density data format");
110 densities.push_back(density);
116 size_t numParticles = positions.
size();
117 for (
size_t i = 0; i < numParticles; ++i) {
118 Eigen::Vector3d pos = positions[i];
119 Eigen::Vector3d vel = (i < velocities.size()) ? velocities[i] : Eigen::Vector3d(0, 0, 0);
120 int type = (i < types.size()) ? types[i] : 0;
121 int fluidType = (i < fluidTypes.size()) ? fluidTypes[i] : 0;
122 double density = (i < densities.size() && densities[i] >= 0) ? densities[i] : defaultDensity;
127 return {startTime, particles};
Class for particle in MPS method.
Class for loading particles from a VTU file.
std::pair< double, Particles > load(const fs::path &path, double defaultDensity) override
Load particles from a VTU file.
A collection of particles.
int size() const
Get the number of particles.
void add(const Particle &particle)
Add a particle to the collection.
ParticleType
Enum class for particle type.