Exporting results to VTK / ParaView#

Once you’ve got a ModalResult / StaticResult, sending it downstream to ParaView / VisIt / any VTK-aware tool is a one-liner. femorph_solver.io wraps the pyvista writers so you don’t have to hand-roll the point-data scatter.

from __future__ import annotations

import tempfile
from pathlib import Path

import numpy as np
import pyvista as pv

import femorph_solver

Build + solve a tiny plate#

grid = pv.StructuredGrid(
    *np.meshgrid(
        np.linspace(0.0, 1.0, 11),
        np.linspace(0.0, 0.3, 5),
        np.linspace(0.0, 0.01, 3),
        indexing="ij",
    )
).cast_to_unstructured_grid()

m = femorph_solver.Model.from_grid(grid)
m.et(1, "SOLID185")
m.mp("EX", 1, 2.0e11)
m.mp("PRXY", 1, 0.30)
m.mp("DENS", 1, 7850.0)

pts = np.asarray(grid.points)
node_nums = np.asarray(grid.point_data["ansys_node_num"])
for nn in node_nums[pts[:, 0] < 1e-9]:
    m.d(int(nn), "ALL")

res = m.modal_solve(n_modes=4)

Write every mode as a .vtu snapshot#

out_dir = Path(tempfile.mkdtemp(prefix="femorph_solver_docs_"))
vtu_path = out_dir / "plate_modes.vtu"
femorph_solver.io.write_modal_vtk(vtu_path, m, res)
print(f"wrote modal snapshot → {vtu_path}  ({vtu_path.stat().st_size:,} bytes)")
wrote modal snapshot → /tmp/femorph_solver_docs_48_t5e1w/plate_modes.vtu  (28,693 bytes)

Inspect the file#

back = pv.read(str(vtu_path))
vector_arrays = [
    name for name in back.point_data if name.startswith("mode_") and name.endswith("_disp")
]
print(f"file carries {len(vector_arrays)} mode-shape vector fields:")
for name in vector_arrays:
    print(f"  {name}: shape = {back.point_data[name].shape}")
file carries 4 mode-shape vector fields:
  mode_1_disp: shape = (165, 3)
  mode_2_disp: shape = (165, 3)
  mode_3_disp: shape = (165, 3)
  mode_4_disp: shape = (165, 3)

For a single static field, the canonical pattern is the same#

femorph_solver.io.static_result_to_grid() is the static analogue of modal_result_to_grid(). Example:

grid_static = femorph_solver.io.static_result_to_grid(m, static_res)
grid_static.save("static.vtu")

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

Gallery generated by Sphinx-Gallery