1
$\begingroup$

I see the Import function can import .vtk files. But how about .vtu files? Is there an existing package for this?

$\endgroup$
4
  • $\begingroup$ Import["filename.vtk"] $\endgroup$ Commented Jan 13, 2023 at 12:56
  • $\begingroup$ @DanielHuber Thanks for the comment but I want to import .vtu files. $\endgroup$
    – H. Zhou
    Commented Jan 14, 2023 at 5:23
  • $\begingroup$ @H.Zhou what's the issue with Import["filename.vtu"]? $\endgroup$
    – bmf
    Commented Jan 14, 2023 at 5:30
  • $\begingroup$ @bmf The file will be then imported as plain text. $\endgroup$
    – H. Zhou
    Commented Jan 14, 2023 at 7:27

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
2
  • $\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. Zhou
    Commented 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.