Write a mesh as FRD#

pyvista_frd.write() writes a pyvista.UnstructuredGrid as long ASCII, binary float32, or binary float64 FRD. This example builds a mesh, writes ASCII FRD, and reads the result.

from pathlib import Path
import tempfile

import numpy as np
import pyvista as pv

import pyvista_frd

Build an unstructured grid with vector and scalar point data.

mesh = pv.ParametricTorus(ringradius=6, crosssectionradius=2)
mesh = mesh.cast_to_unstructured_grid().triangulate()

xyz = mesh.points
mesh.point_data['DISP'] = np.column_stack(
    [np.sin(xyz[:, 0] / 3), np.cos(xyz[:, 1] / 3), xyz[:, 2] / 4]
)
mesh.point_data['TEMP'] = np.linalg.norm(xyz, axis=1)

out = Path(tempfile.mkdtemp()) / 'torus.frd'
pyvista_frd.write(out, mesh)
print(f'{out.name}: {out.stat().st_size / 1024:.0f} KB')
/home/runner/work/pyvista-frd-reader/pyvista-frd-reader/doc/examples/plot_d_writing.py:32: PyVistaDeprecationWarning: `UnstructuredGrid.offset` is deprecated. Use `UnstructuredGrid.cell_offsets` instead.
  pyvista_frd.write(out, mesh)
torus.frd: 2891 KB

Read the file and check the point coordinates.

again = pyvista_frd.read(out)
print(again)
print('arrays:', list(again.point_data))

np.testing.assert_allclose(again.points, mesh.points, rtol=1e-5)
print('points survive the round trip')
UnstructuredGrid (0x7f9965d492a0)
  N Cells:    19602
  N Points:   9801
  X Bounds:   -7.999e+00, 7.999e+00
  Y Bounds:   -7.996e+00, 8.000e+00
  Z Bounds:   -2.000e+00, 2.000e+00
  N Arrays:   4
arrays: ['original_node_ids', 'Normals', 'DISP', 'TEMP']
points survive the round trip

Compare the original and loaded arrays.

pl = pv.Plotter(shape=(1, 2), window_size=(1000, 460))
pl.subplot(0, 0)
pl.add_text('written', font_size=10)
pl.add_mesh(mesh, scalars='TEMP', cmap='magma')
pl.subplot(0, 1)
pl.add_text('read back', font_size=10)
pl.add_mesh(again, scalars='TEMP', cmap='magma')
pl.link_views()
pl.view_isometric()
pl.camera.zoom(1.35)
pl.show()
plot d writing

A grid loaded by pyvista_frd.read() includes original_node_ids. Writing that grid preserves its node numbering.

# Sphinx-Gallery runs this file from ``doc/examples``.
DATA = Path('../_data').resolve()
loaded = pyvista_frd.read(DATA / 'cantilever.frd')
print('carries original ids:', 'original_node_ids' in loaded.point_data)
print('first five:', loaded.point_data['original_node_ids'][:5])
carries original ids: True
first five: [1 2 3 4 5]

Unsupported VTK cell types raise an error. The writer does not drop them.

bad = pv.PolyData(np.array([[0.0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1]])).delaunay_3d()
bad = bad.cast_to_unstructured_grid()
try:
    pyvista_frd.write(Path(tempfile.mkdtemp()) / 'x.frd', pv.Sphere().cast_to_unstructured_grid())
    print('sphere written')
except Exception as err:  # noqa: BLE001
    print(f'{type(err).__name__}: {err}')
/home/runner/work/pyvista-frd-reader/pyvista-frd-reader/doc/examples/plot_d_writing.py:76: PyVistaDeprecationWarning: `UnstructuredGrid.offset` is deprecated. Use `UnstructuredGrid.cell_offsets` instead.
  pyvista_frd.write(Path(tempfile.mkdtemp()) / 'x.frd', pv.Sphere().cast_to_unstructured_grid())
sphere written

Total running time of the script: (0 minutes 0.323 seconds)

Gallery generated by Sphinx-Gallery