You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: utf-8 -*-
"""Запись сетки и полей в формате VTK legacy (binary) — для визуализации (fea/render.py)."""
import numpy as np
def write_vtk(path, X, T, point_data=None, cell_type=24, title="valve body"):
"""X (N,3), T (ne,10) в порядке ccx (совпадает с VTK_QUADRATIC_TETRA = 24)."""
point_data = point_data or {}
N, ne = len(X), len(T)
npn = T.shape[1]
with open(path, "wb") as f:
f.write(b"# vtk DataFile Version 3.0\n")
f.write((title[:200] + "\n").encode("ascii", "replace"))
f.write(b"BINARY\nDATASET UNSTRUCTURED_GRID\n")
f.write(("POINTS %d double\n" % N).encode())
f.write(np.asarray(X, ">f8").tobytes())
f.write(b"\n")
cells = np.column_stack([np.full(ne, npn), T]).astype(">i4")
f.write(("CELLS %d %d\n" % (ne, ne * (npn + 1))).encode())
f.write(cells.tobytes())
f.write(b"\n")
f.write(("CELL_TYPES %d\n" % ne).encode())
f.write(np.full(ne, cell_type, ">i4").tobytes())
f.write(b"\n")
if point_data:
f.write(("POINT_DATA %d\n" % N).encode())
for name, arr in point_data.items():
arr = np.asarray(arr, float)
if arr.ndim == 1:
f.write(("SCALARS %s double 1\nLOOKUP_TABLE default\n" % name).encode())
f.write(arr.astype(">f8").tobytes())
else:
f.write(("VECTORS %s double\n" % name).encode())
f.write(arr[:, :3].astype(">f8").tobytes())
f.write(b"\n")

Powered by TurnKey Linux.