Bug with the export to vtk of FLAC3D

Dear all,

There seems to be an issue with the export of results to vtk in FLAC3D version 9.4.33 on Window 11.

I created below a minime viable example:

model new

zone create brick

zone vtk filename “testfile“

This will create a .vtu file, except that if you try to read it in a common vtk file reader such as Paraview it is not detected as a readable .vtu file.

By inspecting the beginning of the file the issue is clear: The file is directly binary instead of having an xml header.

Previous versions suc has 9.1 did produce correctly formatted vtu files as you can see with the example first few characters of a file I created in the past:

<?xml version="1.0" encoding="UTF-8"?>
<!--VTU file generated by flac3d 9.2 Release 20-->
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">

Therefore, I wish to know if there is a workaround that do not involve downgrading the version of FLAC3D.

For example, does someone has experience install packages such as the vtk package using the pip packages with the FLAC3D python? If I have access to the vtk package I can easily write a script to export to vtu using the python API.

I am reluctant to try things on the fly, that could break the flac installation and would be very thankful if someone had a return on experience with this type of endeavors.

Other suggestion would also be warmly welcomed!

Thank you in advance!

Steps to install VTK for FLAC3D’s embedded Python:

  1. Open a terminal (Command Prompt).
    Make sure you have write privileges.

  2. Navigate to the FLAC3D Python path.
    For example:

    cd "C:\Program Files\Itasca\Itasca Software Subscription\exe64\python310"
    
  3. Install VTK using pip.

    python -m pip install vtk
    

    FLAC3D’s embedded Python should now be able to import vtk.

  4. Restart FLAC3D.
    The newly installed package should now be available.

These steps are applicable to installing other Python packages.

I followed the steps, but am I the only one who still can’t get it to work? It won’t save a VTU file in XML format.

Thank you it worked.!

I will post a script to export the results using the vtk library.

A word of warning for other people that comes across this discussion.

Do not install a module using ipython magic within the flac python console like this:

!pip install vtk

This will either crash FLAC or silently fail. install the python modules outside of FLAC.

Here is a script to show how to use vtk to export FLAC3D results. I use the numpy interface both in FLAC and vtk so it should work efficiently even for very large files.

First create a minimal viable mesh:

model new

zone create brick

The python function below will allow you to export gridpoints data to vtk fileformat (indentation is not correct, I do not know how to make it work correctly).

import itasca as it
import numpy as np
import vtk
import vtkmodules

def export_gridpoints_to_vtk(filename):

vtk_points = vtk.vtkPoints()
pts = vtkmodules.util.numpy_support.numpy_to_vtk(it.gridpointarray.pos(), deep=True)
vtk_points.SetData(pts)

pts_cloud = vtk.vtkPolyData()
pts_cloud.SetPoints(vtk_points)

disp = vtkmodules.util.numpy_support.numpy_to_vtk(it.gridpointarray.disp(), deep=True)
disp.SetName(“Displacement”)
pts_cloud.GetPointData().SetScalars(disp)

# writer
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName(filename)
writer.SetInputData(pts_cloud)
#writer.SetDataModeToAscii() uncomment this line to get the file entirely in ascii xml
writer.Write()

print(f" {vtk_points.data.data_size} gripoints saved to file {filename}")

You can then run the script as follow

export_gridpoints_to_vtk(“test1.vtu”)

If you open the file in a viewer such as Paraview you should get the following view:

1 Like

If you also want to export zone data. I advise to use the pyvista library rather than vtk. It offers a much better interoperability with numpy. This way pyvista and the itasca python library can interface using numpy structure, which is is much more performant than using vtk which would force us to use very inefficient python loops.

The following code works for meshes which only contains brick and degenerated bricks. I will come up with a more general solution later.

import numpy as np

import itasca as it

import pyvista as pv

def export_zones_to_vtk(filename):

order = [0, 1, 4, 2, 3, 6, 7, 5]
pos()
celltype = np.full(it.zone.count(), pv.CellType.HEXAHEDRON, dtype = np.uint8)
cells = np.hstack((np.ones((it.zone.count(), 1),dtype=np.int64)*8, it.zonearray.gridpoints()[:, order]))
cellsflat = cells.ravel()
grid = pv.UnstructuredGrid(cellsflat, celltype, points)

grid.point_data[“displacement”] = it.gridpointarray.disp() # gridpoint data
grid.cell_data[“stresses”] = it.zonearray.stress() # zone data

grid.save(filename)

1 Like