Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Input / Output

Figures use PyVista; verbose plotting boilerplate is omitted.

import numpy as np

import mefikit as mf
volumes = mf.build_cmesh(
    range(2), np.linspace(0.0, 1.0, 5), np.logspace(0.0, 1.0, 5) / 10.0
)

Memory exports

A UMesh can be handed to other libraries without ever touching the disk:

print(volumes.to_mc())
Unstructured mesh with name : "mf_UMesh"
Description of mesh : ""
Time attached to the mesh [unit] : 0 []
Iteration : -1 Order : -1
Mesh dimension : 3
Space dimension : 3
Info attached on space dimension : "" "" ""
Number of nodes : 50
Number of cells : 16
Cell types present : NORM_HEXA8
print(volumes.to_pyvista())
UnstructuredGrid (0x78585195b280)
  N Cells:    16
  N Points:   50
  X Bounds:   0.000e+00, 1.000e+00
  Y Bounds:   0.000e+00, 1.000e+00
  Z Bounds:   1.000e-01, 1.000e+00
  N Arrays:   0
volumes.to_pyvista().plot(show_edges=True)

print(volumes.to_meshio())
<meshio mesh object>
  Number of points: 50
  Number of cells:
    hexahedron: 16
print(volumes.to_json())
{"coords":{"v":1,"dim":[50,3],"data":[0.0,0.0,0.1,1.0,0.0,0.1,0.0,0.25,0.1,1.0,0.25,0.1,0.0,0.5,0.1,1.0,0.5,0.1,0.0,0.75,0.1,1.0,0.75,0.1,0.0,1.0,0.1,1.0,1.0,0.1,0.0,0.0,0.17782794100389226,1.0,0.0,0.17782794100389226,0.0,0.25,0.17782794100389226,1.0,0.25,0.17782794100389226,0.0,0.5,0.17782794100389226,1.0,0.5,0.17782794100389226,0.0,0.75,0.17782794100389226,1.0,0.75,0.17782794100389226,0.0,1.0,0.17782794100389226,1.0,1.0,0.17782794100389226,0.0,0.0,0.31622776601683794,1.0,0.0,0.31622776601683794,0.0,0.25,0.31622776601683794,1.0,0.25,0.31622776601683794,0.0,0.5,0.31622776601683794,1.0,0.5,0.31622776601683794,0.0,0.75,0.31622776601683794,1.0,0.75,0.31622776601683794,0.0,1.0,0.31622776601683794,1.0,1.0,0.31622776601683794,0.0,0.0,0.5623413251903491,1.0,0.0,0.5623413251903491,0.0,0.25,0.5623413251903491,1.0,0.25,0.5623413251903491,0.0,0.5,0.5623413251903491,1.0,0.5,0.5623413251903491,0.0,0.75,0.5623413251903491,1.0,0.75,0.5623413251903491,0.0,1.0,0.5623413251903491,1.0,1.0,0.5623413251903491,0.0,0.0,1.0,1.0,0.0,1.0,0.0,0.25,1.0,1.0,0.25,1.0,0.0,0.5,1.0,1.0,0.5,1.0,0.0,0.75,1.0,1.0,0.75,1.0,0.0,1.0,1.0,1.0,1.0,1.0]},"element_blocks":{"HEX8":{"cell_type":"HEX8","connectivity":{"Regular":{"v":1,"dim":[16,8],"data":[0,1,3,2,10,11,13,12,2,3,5,4,12,13,15,14,4,5,7,6,14,15,17,16,6,7,9,8,16,17,19,18,10,11,13,12,20,21,23,22,12,13,15,14,22,23,25,24,14,15,17,16,24,25,27,26,16,17,19,18,26,27,29,28,20,21,23,22,30,31,33,32,22,23,25,24,32,33,35,34,24,25,27,26,34,35,37,36,26,27,29,28,36,37,39,38,30,31,33,32,40,41,43,42,32,33,35,34,42,43,45,44,34,35,37,36,44,45,47,46,36,37,39,38,46,47,49,48]}},"fields":{},"families":{"v":1,"dim":[16],"data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},"groups":{}}}}

File read/write

read/write select the format from the file extension:

  • json
  • yaml / yml
  • vtk / vtu (legacy binary VTK 2.0)
  • vtkhdf (HDF5-based VTK)
  • cgns (HDF5-based CGNS)
  • med (medfile)

The legacy vtk reader/writer only handles the old binary VTK 2.0 format; the HDF5-based .vtkhdf and .cgns formats are the recommended options for modern, HPC-friendly storage.

Fields and groups

Cell-centered fields and element groups are stored alongside the geometry in every format. Coordinates are written once, and each field/group refers to cells through the same global cell ordering, so nothing is duplicated per element block.

FormatFieldsGroups
jsonall (any number of components)all
yaml/ymlall (any number of components)all
medall (any number of components)all
vtkhdfscalar/vector, single topological dimall (GroupData, 0-based)
cgnsscalar only, one solution per dimall (ZoneSubRegion_t, 1-based)
vtk/vtunone (legacy reader/writer, geometry only)none

Some restrictions apply to the HDF5 formats:

  • VTKHDF fields are only exported when the mesh has a single topological dimension; on mixed-dimension meshes they are skipped.
  • CGNS cell-centered solutions must be scalar, so multi-component fields are skipped. Each topological dimension gets its own FlowSolution node with a GridLocation matching that dimension (CellCenter, FaceCenter or EdgeCenter) and, when needed, a PointList. Groups are written as ZoneSubRegion_t nodes; a group spanning several dimensions is split into one node per dimension so each node has a single valid GridLocation. Vertex fields/groups cannot be represented and are skipped.
import pathlib

pathlib.Path("data").mkdir(exist_ok=True)
for ext in ("vtk", "yaml", "json", "vtkhdf", "cgns", "med"):
    volumes.write(f"data/volumes.{ext}")
    volumes_from_disk = mf.UMesh.read(f"data/volumes.{ext}")
    assert volumes_from_disk
    assert (
        volumes != volumes_from_disk
    )  # this is a new instance, with a different memory adress