Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert particle output into binary VTK XML #4386

Draft
wants to merge 2 commits into
base: development
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions Tools/Py_util/amrex_particles_to_vtp/write_pview_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import numpy as np
import struct
import os

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'os' is not used.

def write_paraview_file_structmesh(fname,xp,yp,ccdata,ncdata):

Expand Down Expand Up @@ -184,7 +186,7 @@
Npts=pts.shape[0]

outfile.write("<?xml version=\"1.0\"?>\n")
outfile.write("<VTKFile type=\"PolyData\" version=\"0.1\" byte_order=\"LittleEndian\">\n")
outfile.write("<VTKFile type=\"PolyData\" version=\"0.1\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n")
outfile.write("<PolyData>\n")
outfile.write("<Piece NumberOfPoints=\"%d\" NumberOfVerts=\"0\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n"%(Npts))

Expand All @@ -199,14 +201,25 @@
outfile.write("</PointData>\n")

outfile.write("<Points>\n")
outfile.write("<DataArray type=\"Float32\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">\n")
for i in range(Npts):
outfile.write("%e\t%e\t%e\t"%(pts[i][0],pts[i][1],pts[i][2]))
outfile.write("\n</DataArray>\n")
outfile.write("</Points>\n")

outfile.write("<DataArray type=\"Float32\" Name=\"Points\" NumberOfComponents=\"3\" format=\"appended\" offset=\"0\">\n")
outfile.write("</DataArray>\n")
outfile.write("</Points>\n")
outfile.write("</Piece>\n")
outfile.write("</PolyData>\n")
outfile.write("</VTKFile>\n")

# write AppendedData in special format
# https://vtk.org/Wiki/VTK_XML_Formats#Uncompressed_Data
outfile.write("<AppendedData encoding=\"raw\">\n")
outfile.write("\t_")
outfile.close()
with open(fname, 'ab') as binfile:
arrays_to_write = [pts]
for arr in arrays_to_write:
float_arr = arr.astype(np.float32)
binfile.write(struct.pack('<L', arr.nbytes))
binfile.write(float_arr.tobytes('C'))

outfile = open(fname, 'a')

Check warning

Code scanning / CodeQL

File is not always closed Warning

File may not be closed if an exception is raised.
outfile.write("\n</AppendedData>")
outfile.write("</VTKFile>\n")
outfile.close()
Loading