Skip to content

Commit 609cb21

Browse files
Update vtk_save.h
1 parent 36a3ab3 commit 609cb21

1 file changed

Lines changed: 29 additions & 20 deletions

File tree

src/io/vtk_save.h

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,63 @@
1313
#include <vector>
1414
#include <string>
1515
#include <fstream>
16-
#include "../struct/particle.h"
16+
#include "struct/particle.h"
1717

18-
inline void SaveVTK(const std::vector<Particle>& p, const std::string& filename)
18+
/**
19+
* @brief Saves the SoA Particle database to a VTK Legacy file for ParaView.
20+
*/
21+
inline void SaveVTK(const Particle& p, const std::string& filename)
1922
{
2023
std::ofstream out(filename);
2124
if (!out) return;
2225

23-
const size_t N = p.size();
26+
const size_t N = p.size(); // p is the SoA database
2427
out << "# vtk DataFile Version 3.0\n";
2528
out << "NEXT snapshot\n";
2629
out << "ASCII\n";
2730
out << "DATASET POLYDATA\n";
2831

32+
// Detect precision based on your build macros
2933
#ifdef NEXT_FP64
3034
constexpr const char* vtkType = "double";
31-
#elif defined(NEXT_FP32)
35+
#else
3236
constexpr const char* vtkType = "float";
3337
#endif
3438

35-
// Points
39+
// --- POINTS (Coordinates) ---
3640
out << "POINTS " << N << " " << vtkType << "\n";
37-
for (const auto& a : p)
38-
out << a.x << " " << a.y << " " << a.z << "\n";
41+
for (size_t i = 0; i < N; i++) {
42+
out << p.x[i] << " " << p.y[i] << " " << p.z[i] << "\n";
43+
}
3944

40-
// Vertices
41-
out << "VERTICES " << N << " " << N*2 << "\n";
42-
for (size_t i = 0; i < N; i++)
45+
// --- VERTICES ---
46+
// VTK needs topology to render points as actual pixels
47+
out << "VERTICES " << N << " " << N * 2 << "\n";
48+
for (size_t i = 0; i < N; i++) {
4349
out << "1 " << i << "\n";
50+
}
4451

4552
out << "POINT_DATA " << N << "\n";
4653

47-
// Type (0 for Star, 1 for DM)
48-
// In ParaView, use the "Threshold" filter on this to hide DM
54+
// --- Type (0 for Star, 1 for DM) ---
4955
out << "SCALARS type int 1\n";
5056
out << "LOOKUP_TABLE default\n";
51-
for (const auto& a : p)
52-
out << a.type << "\n";
57+
for (size_t i = 0; i < N; i++) {
58+
out << p.type[i] << "\n";
59+
}
5360

54-
// Velocity Vectors
61+
// --- Velocity Vectors ---
5562
out << "VECTORS velocity " << vtkType << "\n";
56-
for (const auto& a : p)
57-
out << a.vx << " " << a.vy << " " << a.vz << "\n";
63+
for (size_t i = 0; i < N; i++) {
64+
out << p.vx[i] << " " << p.vy[i] << " " << p.vz[i] << "\n";
65+
}
5866

59-
// Mass
67+
// --- Mass ---
6068
out << "SCALARS mass " << vtkType << " 1\n";
6169
out << "LOOKUP_TABLE default\n";
62-
for (const auto& a : p)
63-
out << a.m << "\n";
70+
for (size_t i = 0; i < N; i++) {
71+
out << p.m[i] << "\n";
72+
}
6473

6574
out.close();
6675
}

0 commit comments

Comments
 (0)