djr4cfd |
April 3, 2024 11:10 |
Paraview .vtr files in binary
Dear all,
I am trying to write my output files in vtr format and the following gives me a correct output file in ASCII.
Code:
#include <iostream>
#include <fstream>
#include <cmath>
// Function to calculate phi = sin(x) * cos(y)
double phi(double x, double y) {
return sin(x) * cos(y);
}
int main() {
int n = 10; // Size of the mesh (10x10)
double step = 1.0 / (n - 1); // Step size
std::ofstream outfile("output.vtr");
// Writing the header
outfile << "<?xml version=\"1.0\"?>\n";
outfile << "<VTKFile type=\"RectilinearGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
outfile << "<RectilinearGrid WholeExtent=\"0 " << n-1 << " 0 " << n-1 << " 0 0\">\n";
outfile << "<Piece Extent=\"0 " << n-1 << " 0 " << n-1 << " 0 0\">\n";
// Writing the scalar data (phi)
outfile << "<PointData>\n";
outfile << "<DataArray type=\"Float64\" Name=\"phi\" NumberOfComponents=\"1\" format=\"ascii\">\n";
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
double x = i * step;
double y = j * step;
outfile << phi(x, y) << " ";
}
}
outfile << "\n</DataArray>\n";
outfile << "</PointData>\n";
// Writing the coordinates
outfile << "<Coordinates>\n";
outfile << "<DataArray type=\"Float64\" Name=\"X_COORDINATES\" NumberOfComponents=\"1\" format=\"ascii\">\n";
for (int i = 0; i < n; ++i) {
outfile << i * step << " ";
}
outfile << "\n</DataArray>\n";
outfile << "<DataArray type=\"Float64\" Name=\"Y_COORDINATES\" NumberOfComponents=\"1\" format=\"ascii\">\n";
for (int j = 0; j < n; ++j) {
outfile << j * step << " ";
}
outfile << "\n</DataArray>\n";
outfile << "<DataArray type=\"Float64\" Name=\"Z_COORDINATES\" NumberOfComponents=\"1\" format=\"ascii\">\n";
outfile << "0.0 \n";
outfile << "</DataArray>\n";
outfile << "</Coordinates>\n";
// Closing tags
outfile << "</Piece>\n";
outfile << "</RectilinearGrid>\n";
outfile << "</VTKFile>\n";
outfile.close();
std::cout << "Output file generated: output.vtr\n";
return 0;
}
To get output file in BINARY, I tried the following:
Code:
#include <iostream>
#include <fstream>
#include <cmath>
// Function to calculate phi = sin(x) * cos(y)
double phi(double x, double y) {
return sin(x) * cos(y);
}
int main() {
int n = 10; // Size of the mesh (10x10)
double step = 1.0 / (n - 1); // Step size
std::ofstream outfile("output.vtr");
double A[n][n];
double xx[n];
double yy[n];
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
double x = i * step;
double y = j * step;
//outfile << phi(x, y) << " ";
A[i][j]= phi(x, y);
}
}
for (int i = 0; i < n; ++i) {
xx[i] =i * step;
}
for (int j = 0; j < n; ++j) {
yy[j] =j * step;
}
// Writing the header
outfile << "<?xml version=\"1.0\"?>\n";
outfile << "<VTKFile type=\"RectilinearGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
outfile << "<RectilinearGrid WholeExtent=\"0 " << n-1 << " 0 " << n-1 << " 0 0\">\n";
outfile << "<Piece Extent=\"0 " << n-1 << " 0 " << n-1 << " 0 0\">\n";
// Writing the scalar data (phi)
outfile << "<PointData>\n";
outfile << "<DataArray type=\"Int64\" Name=\"phi\" NumberOfComponents=\"1\" format=\"binary\">\n";
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
outfile.write(reinterpret_cast<const char*>(&A[i][j]), sizeof(double));
}
}
outfile << "\n</DataArray>\n";
outfile << "</PointData>\n";
// Writing the coordinates
outfile << "<Coordinates>\n";
outfile << "<DataArray type=\"Int64\" Name=\"X_COORDINATES\" NumberOfComponents=\"1\" format=\"binary\">\n";
for (int i = 0; i < n; ++i) {
outfile.write(reinterpret_cast<const char*>(&xx[i]), sizeof(double));
}
outfile << "\n</DataArray>\n";
outfile << "<DataArray type=\"Int64\" Name=\"Y_COORDINATES\" NumberOfComponents=\"1\" format=\"binary\">\n";
for (int j = 0; j < n; ++j) {
outfile.write(reinterpret_cast<const char*>(&yy[j]), sizeof(double));
}
outfile << "\n</DataArray>\n";
outfile << "<DataArray type=\"Int64\" Name=\"Z_COORDINATES\" NumberOfComponents=\"1\" format=\"binary\">\n";
double z =0.0;
outfile.write(reinterpret_cast<const char*>(&z), sizeof(double));
//outfile << "0.0 \n";
outfile << "\n</DataArray>\n";
outfile << "</Coordinates>\n";
// Closing tags
outfile << "</Piece>\n";
outfile << "</RectilinearGrid>\n";
outfile << "</VTKFile>\n";
outfile.close();
std::cout << "Output file generated: output.vtr\n";
return 0;
}
The generated output is not correctly read by Paraview. It gives me the following error.
ERROR: In /builds/gitlab-kitware-sciviz-ci/build/superbuild/paraview/src/VTK/IO/XMLParser/vtkXMLParser.cxx, line 379
vtkXMLDataParser (0x1659c570): Error parsing XML in stream at line 7, column 0, byte index 255: not well-formed (invalid token)
ERROR: In /builds/gitlab-kitware-sciviz-ci/build/superbuild/paraview/src/VTK/IO/XML/vtkXMLReader.cxx, line 521
vtkXMLRectilinearGridReader (0x17dda5a0): Error parsing input file. ReadXMLInformation aborting.
I am using a Ubuntu system; byte_order is LittleEndian and using g++ compiler. Paraview version is 5.10.1.
Can anyone help me fix this. A similar example for writing binary vtr files using c++ will also help.
|