68 std::ofstream ofs(path);
70 cerr <<
"cannot write " << path << endl;
75 ofs <<
"<?xml version='1.0' encoding='UTF-8'?>" << endl;
76 ofs <<
"<VTKFile type=\"UnstructuredGrid\" version=\"1.0\" header_type=\"UInt64\" byte_order=\"";
80 ofs <<
"LittleEndian";
83 ofs <<
"<UnstructuredGrid>" << endl
89 ofs <<
"<Points>" << endl;
92 ofs << p.position.x() <<
" " << p.position.y() <<
" " << p.position.z() << endl;
95 ofs <<
"</Points>" << endl;
100 ofs <<
"<PointData>" << endl;
102 ofs <<
dataArrayBegin(
"Int32",
"Particle Type", 1,
"ascii") << endl;
104 ofs << static_cast<int32_t>(p.type) << endl;
110 ofs << p.velocity.x() <<
" " << p.velocity.y() <<
" " << p.velocity.z() << endl;
116 ofs << p.pressure << endl;
120 ofs <<
dataArrayBegin(
"Float64",
"Number Density", 1,
"ascii") << endl;
122 ofs << p.numberDensity << endl;
126 ofs <<
dataArrayBegin(
"Float64",
"Number Density Ratio", 1,
"ascii") << endl;
128 ofs << p.numberDensity / n0ForNumberDensity << endl;
132 ofs <<
dataArrayBegin(
"Int32",
"Boundary Condition", 1,
"ascii") << endl;
134 ofs << static_cast<int32_t>(p.boundaryCondition) << endl;
140 ofs << p.fluidType << endl;
143 ofs <<
"</PointData>" << endl;
148 ofs <<
"<Cells>" << endl;
150 ofs <<
dataArrayBegin(
"Int64",
"connectivity", 1,
"ascii") << endl;
158 ofs << i + 1 << endl;
167 ofs <<
"</Cells>" << endl;
168 ofs <<
"</Piece>" << endl;
172 ofs <<
"<FieldData>" << endl;
173 ofs <<
"<DataArray type=\"Float64\" Name=\"Time\" NumberOfTuples=\"1\" format=\"ascii\">" << endl;
175 ofs <<
"</DataArray>" << endl;
176 ofs <<
"</FieldData>" << endl;
177 ofs <<
"</UnstructuredGrid>" << endl;
178 ofs <<
"</VTKFile>" << endl;
182 std::ofstream ofs(path);
184 cerr <<
"cannot write " << path << endl;
188 std::stringstream binaryData;
191 ofs <<
"<?xml version='1.0' encoding='UTF-8'?>" << endl;
192 ofs <<
"<VTKFile type=\"UnstructuredGrid\" version=\"1.0\" header_type=\"UInt64\" byte_order=\"";
196 ofs <<
"LittleEndian";
198 ofs <<
"\">" << endl;
199 ofs <<
"<UnstructuredGrid>" << endl
204 ofs <<
"<Points>" << endl;
205 ofs <<
dataArrayBegin(
"Float64",
"Position", 3,
"appended", binaryData.tellp()) << endl;
207 uint64_t length_points =
particles.
size() * 3 *
sizeof(double);
208 binaryData.write(
reinterpret_cast<char*
>(&length_points),
sizeof(uint64_t));
210 double x = p.position.x();
211 double y = p.position.y();
212 double z = p.position.z();
213 binaryData.write(
reinterpret_cast<char*
>(&x),
sizeof(
double));
214 binaryData.write(
reinterpret_cast<char*
>(&y),
sizeof(
double));
215 binaryData.write(
reinterpret_cast<char*
>(&z),
sizeof(
double));
217 ofs <<
"</Points>" << endl;
221 ofs <<
"<PointData>" << endl;
223 ofs <<
dataArrayBegin(
"Int32",
"Particle Type", 1,
"appended", binaryData.tellp()) << endl;
225 uint64_t length_particle_type =
particles.
size() *
sizeof(int32_t);
226 binaryData.write(
reinterpret_cast<char*
>(&length_particle_type),
sizeof(uint64_t));
228 int32_t type =
static_cast<int32_t
>(p.type);
229 binaryData.write(
reinterpret_cast<char*
>(&type),
sizeof(int32_t));
232 ofs <<
dataArrayBegin(
"Float64",
"Velocity", 3,
"appended", binaryData.tellp()) << endl;
234 uint64_t length_velocity =
particles.
size() * 3 *
sizeof(double);
235 binaryData.write(
reinterpret_cast<char*
>(&length_velocity),
sizeof(uint64_t));
237 double vx = p.velocity.x();
238 double vy = p.velocity.y();
239 double vz = p.velocity.z();
240 binaryData.write(
reinterpret_cast<char*
>(&vx),
sizeof(
double));
241 binaryData.write(
reinterpret_cast<char*
>(&vy),
sizeof(
double));
242 binaryData.write(
reinterpret_cast<char*
>(&vz),
sizeof(
double));
245 ofs <<
dataArrayBegin(
"Float64",
"Pressure", 1,
"appended", binaryData.tellp()) << endl;
247 uint64_t length_pressure =
particles.
size() *
sizeof(double);
248 binaryData.write(
reinterpret_cast<char*
>(&length_pressure),
sizeof(uint64_t));
250 double pressure = p.pressure;
251 binaryData.write(
reinterpret_cast<char*
>(&pressure),
sizeof(
double));
254 ofs <<
dataArrayBegin(
"Float64",
"Number Density", 1,
"appended", binaryData.tellp()) << endl;
256 uint64_t length_number_density =
particles.
size() *
sizeof(double);
257 binaryData.write(
reinterpret_cast<char*
>(&length_number_density),
sizeof(uint64_t));
259 double number_density = p.numberDensity;
260 binaryData.write(
reinterpret_cast<char*
>(&number_density),
sizeof(
double));
263 ofs <<
dataArrayBegin(
"Float64",
"Number Density Ratio", 1,
"appended", binaryData.tellp()) << endl;
265 uint64_t length_number_density_ratio =
particles.
size() *
sizeof(double);
266 binaryData.write(
reinterpret_cast<char*
>(&length_number_density_ratio),
sizeof(uint64_t));
268 double number_density_ratio = p.numberDensity / n0ForNumberDensity;
269 binaryData.write(
reinterpret_cast<char*
>(&number_density_ratio),
sizeof(
double));
272 ofs <<
dataArrayBegin(
"Int32",
"Boundary Condition", 1,
"appended", binaryData.tellp()) << endl;
274 uint64_t length_boundary_condition =
particles.
size() *
sizeof(int32_t);
275 binaryData.write(
reinterpret_cast<char*
>(&length_boundary_condition),
sizeof(uint64_t));
277 int32_t boundary_condition =
static_cast<int32_t
>(p.boundaryCondition);
278 binaryData.write(
reinterpret_cast<char*
>(&boundary_condition),
sizeof(int32_t));
281 ofs <<
dataArrayBegin(
"Int32",
"Fluid Type", 1,
"appended", binaryData.tellp()) << endl;
283 uint64_t length_fluid_type =
particles.
size() *
sizeof(int32_t);
284 binaryData.write(
reinterpret_cast<char*
>(&length_fluid_type),
sizeof(uint64_t));
286 int32_t fluid_type = p.fluidType;
287 binaryData.write(
reinterpret_cast<char*
>(&fluid_type),
sizeof(int32_t));
289 ofs <<
"</PointData>" << endl;
293 ofs <<
"<Cells>" << endl;
295 ofs <<
dataArrayBegin(
"Int64",
"connectivity", 1,
"appended", binaryData.tellp()) << endl;
297 uint64_t length_connectivity =
particles.
size() *
sizeof(int64_t);
298 binaryData.write(
reinterpret_cast<char*
>(&length_connectivity),
sizeof(uint64_t));
300 int64_t connectivity = i;
301 binaryData.write(
reinterpret_cast<char*
>(&connectivity),
sizeof(int64_t));
304 ofs <<
dataArrayBegin(
"Int64",
"offsets", 1,
"appended", binaryData.tellp()) << endl;
307 binaryData.write(
reinterpret_cast<char*
>(&length_offset),
sizeof(uint64_t));
309 int64_t offset = i + 1;
310 binaryData.write(
reinterpret_cast<char*
>(&offset),
sizeof(int64_t));
313 ofs <<
dataArrayBegin(
"UInt8",
"types", 1,
"appended", binaryData.tellp()) << endl;
316 binaryData.write(
reinterpret_cast<char*
>(&length_types),
sizeof(uint64_t));
319 binaryData.write(
reinterpret_cast<char*
>(&type),
sizeof(uint8_t));
321 ofs <<
"</Cells>" << endl;
322 ofs <<
"</Piece>" << endl;
326 ofs <<
"<FieldData>" << endl;
327 ofs <<
"<DataArray type=\"Float64\" Name=\"Time\" NumberOfTuples=\"1\" format=\"appended\" offset=\""
328 << binaryData.tellp() <<
"\"/>" << endl;
330 uint64_t length_time =
sizeof(double);
331 binaryData.write(
reinterpret_cast<char*
>(&length_time),
sizeof(uint64_t));
332 double time_copied = time;
333 binaryData.write(
reinterpret_cast<char*
>(&time_copied),
sizeof(
double));
334 ofs <<
"</FieldData>" << endl;
335 ofs <<
"</UnstructuredGrid>" << endl;
339 ofs <<
"<AppendedData encoding=\"raw\">" << endl;
340 ofs <<
"_" << binaryData.str() << endl;
341 ofs <<
"</AppendedData>" << endl;
342 ofs <<
"</VTKFile>" << endl;
void toVtuAscii(const std::filesystem::path &path, const double &time, const double &n0ForNumberDensity=1.0)
Export the particles to a file in the VTK zlib format.
void setParticles(const Particles &particles)
Set the particles to export to a file. This method is required before exporting.
void toVtu(const std::filesystem::path &path, const double &time, const double &n0ForNumberDensity=1.0, const bool binary=false)
Export the particles to a file in the VTK zlib format.
void toVtuBinary(const std::filesystem::path &path, const double &time, const double &n0ForNumberDensity=1.0)
Export the particles to a file in the VTK zlib format.
void toProf(const std::filesystem::path &path, const double &time)
Export the particles to a file in the Prof format.
std::string dataArrayBegin(const std::string &type, const std::string &name, int numberOfComponents, const std::string &format, size_t offset=SIZE_MAX) const
write the header of DataArray for the VTK format
std::string dataArrayEnd() const
write the end of DataArray for the VTK format
bool isBigEndian() const
detect the endian of the system
void toCsv(const std::filesystem::path &path, const double &time)
Export the particles to a file in the CSV format.
A collection of particles.
int size() const
Get the number of particles.