I see the Import
function can import .vtk
files. But how about .vtu
files? Is there an existing package for this?
1 Answer
To the best of my knowledge, there is currently (Mathematica v. 14.0.0.0) no package to read .vtu
files. However, you can exploit the structure of the file to write your own reader function after you Import[]
it as a "Table"
.
For instance, let's assume you have a file with the following structure:
<?xml version="1.0"?>
<!-- time 0.0399642-->
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<UnstructuredGrid>
<Piece NumberOfPoints="3220" NumberOfCells="0">
<Points>
<DataArray type="Float32" Name="Position" NumberOfComponents="3" format="ascii">
0.238845 0.025684 0.173811
0.224136 -0.11521 0.135539
0.278716 0.215721 -0.213643
...
</DataArray>
</Points>
<PointData Vectors="vector">
<DataArray type="Float32" Name="Velocity" NumberOfComponents="3" format="ascii">
0 -0.391767 0
0 -0.391767 0
0 -0.391767 0
...
A simple, very low-level code that reads the position and velocity of nP
particles (where the number of particles in each file can differ) is the following:
data = Import[<file_name>, "Table"];
nP = ToExpression[StringSplit[data[[5, 2]], "\""][[2]]];
n0 = 8;
n1 = n0 + nP + 4;
positions = data[[n0 ;; n0 + nP - 1]];
velocities = data[[n1 ;; n1 + nP - 1]];
where n0
and n1
are the lines where the data you need is written. I hope this will help.
-
$\begingroup$ Thanks for the post! This seems to be the only method for now. Unfortunately my data set is quite complicated (from fluid simulations) with many variables and adaptively refined mesh. Writing something from fresh needs quite some work and probably I'll just use other tools. $\endgroup$– H. ZhouCommented May 6 at 5:34
-
$\begingroup$ @H.Zhou Sorry to hear that. I think that if you take the time and invest one day of work, you'll be able to build your own reader in Mathematica. Either way good luck with your work! $\endgroup$ Commented May 7 at 8:17
Import["filename.vtu"]
? $\endgroup$