MPS-Basic
Loading...
Searching...
No Matches
ParticlesLoader::Vtu Class Reference

Class for loading particles from a VTU file. More...

#include <vtu.hpp>

Inheritance diagram for ParticlesLoader::Vtu:
Collaboration diagram for ParticlesLoader::Vtu:

Public Member Functions

std::pair< double, Particlesload (const fs::path &path, double defaultDensity) override
 Load particles from a VTU file.
 
 ~Vtu () override
 
 Vtu ()
 
- Public Member Functions inherited from ParticlesLoader::Interface
virtual ~Interface ()=default
 

Detailed Description

Class for loading particles from a VTU file.

This class handles the VTU file format for particle data.

Definition at line 11 of file vtu.hpp.

Constructor & Destructor Documentation

◆ ~Vtu()

Vtu::~Vtu ( )
override

Definition at line 130 of file vtu.cpp.

130 {
131}

◆ Vtu()

Vtu::Vtu ( )

Definition at line 13 of file vtu.cpp.

13 {
14}

Member Function Documentation

◆ load()

std::pair< double, Particles > Vtu::load ( const fs::path & path,
double defaultDensity )
overridevirtual

Load particles from a VTU file.

Parameters
pathPath to the VTU file
defaultDensityDefault density to use if not specified in the file
Returns
Pair of simulation start time and particles

Implements ParticlesLoader::Interface.

Definition at line 16 of file vtu.cpp.

16 {
17 std::ifstream ifs(path);
18 if (ifs.fail()) {
19 cerr << "cannot read vtu file: " << fs::absolute(path) << endl;
20 std::exit(-1);
21 }
22
23 // Check if the file contains "<AppendedData>" tag
24 std::string line;
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;
28 std::exit(-1);
29 }
30 }
31
32 // Reset the file stream to the beginning for further processing
33 ifs.clear();
34 ifs.seekg(0, std::ios::beg);
35
36 // Load start
37 double startTime = NAN;
38 Particles particles;
39
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;
45
46 // Parse VTU file
47 while (std::getline(ifs, line)) {
48 // Extract start time
49 if (line.find("Time") != std::string::npos) {
50 std::getline(ifs, line);
51 startTime = std::stod(line);
52 }
53
54 // Extract particle positions
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);
58 double x, y, z;
59 if (!(iss >> x >> y >> z)) {
60 throw std::runtime_error("Invalid position data format");
61 }
62 positions.emplace_back(x, y, z);
63 }
64 }
65
66 // Extract velocities
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);
70 double vx, vy, vz;
71 if (!(iss >> vx >> vy >> vz)) {
72 throw std::runtime_error("Invalid velocity data format");
73 }
74 velocities.emplace_back(vx, vy, vz);
75 }
76 }
77
78 // Extract particle types
79 if (line.find("Particle Type") != std::string::npos) {
80 while (std::getline(ifs, line) && line.find("</DataArray>") == std::string::npos) {
81 int type;
82 std::istringstream iss(line);
83 if (!(iss >> type)) {
84 throw std::runtime_error("Invalid particle type data format");
85 }
86 types.push_back(type);
87 }
88 }
89
90 // Extract fluid types
91 if (line.find("Fluid Type") != std::string::npos) {
92 while (std::getline(ifs, line) && line.find("</DataArray>") == std::string::npos) {
93 int fluidType;
94 std::istringstream iss(line);
95 if (!(iss >> fluidType)) {
96 throw std::runtime_error("Invalid fluid type data format");
97 }
98 fluidTypes.push_back(fluidType);
99 }
100 }
101
102 // Extract densities
103 if (line.find("Density") != std::string::npos) {
104 while (std::getline(ifs, line) && line.find("</DataArray>") == std::string::npos) {
105 double density;
106 std::istringstream iss(line);
107 if (!(iss >> density)) {
108 throw std::runtime_error("Invalid density data format");
109 }
110 densities.push_back(density);
111 }
112 }
113 }
114
115 // Construct particles
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;
123
124 particles.add(Particle(particles.size(), static_cast<ParticleType>(type), pos, vel, density, fluidType));
125 }
126
127 return {startTime, particles};
128}
Class for particle in MPS method.
Definition particle.hpp:47
A collection of particles.
Definition particles.hpp:10
int size() const
Get the number of particles.
Definition particles.cpp:22
void add(const Particle &particle)
Add a particle to the collection.
Definition particles.cpp:26
ParticleType
Enum class for particle type.
Definition particle.hpp:11
Here is the call graph for this function:

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