Load particles from a VTU file.
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
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
33 ifs.clear();
34 ifs.seekg(0, std::ios::beg);
35
36
37 double startTime = NAN;
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
47 while (std::getline(ifs, line)) {
48
49 if (line.find("Time") != std::string::npos) {
50 std::getline(ifs, line);
51 startTime = std::stod(line);
52 }
53
54
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
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
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
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
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
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
125 }
126
127 return {startTime, particles};
128}
Class for particle in MPS method.
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.