spectral_galerkin_heat.io_utils.xdmf_io

XDMF file loading and writing utilities.

Provides functions to read simulation results from XDMF/HDF5 format and write error fields for visualization in ParaView.

Uses xml.etree.ElementTree for proper XML generation instead of hardcoded string templates.

Classes

StructuredField

Temperature on a rectilinear (3DRectMesh / VXVYVZ) grid.

UnstructuredField

Temperature at scattered FE nodes (Tetrahedron / XYZ).

XdmfBuilder

Build XDMF XML documents for structured (3DRectMesh) and

Functions

load_xdmf(xdmf_path[, attr_name])

Load the last time step from an XDMF file.

write_structured_fields(output_base, fields, x, y, z)

Write multiple structured fields to HDF5 + XDMF.

write_unstructured_fields(output_base, fields, xyz, ...)

Write multiple fields on an unstructured (tetrahedral) mesh to HDF5 + XDMF.

Module Contents

class spectral_galerkin_heat.io_utils.xdmf_io.StructuredField

Temperature on a rectilinear (3DRectMesh / VXVYVZ) grid.

class spectral_galerkin_heat.io_utils.xdmf_io.UnstructuredField

Temperature at scattered FE nodes (Tetrahedron / XYZ).

class spectral_galerkin_heat.io_utils.xdmf_io.XdmfBuilder(version='2.0')

Build XDMF XML documents for structured (3DRectMesh) and unstructured (Tetrahedron) grids, via xml.etree.ElementTree.

Parameters:

version (str)

add_structured_grid(name, dims, h5_ref, coord_datasets=('X', 'Y', 'Z'), attributes=None, time=None, step=None, precision=4, parent=None)

Add a structured 3DRectMesh grid to the XDMF document.

Parameters:
  • name (str) – Name of the grid (e.g., “Error”, “Mesh”)

  • dims (tuple of int) – Grid dimensions in (nz, ny, nx) order

  • h5_ref (str) – HDF5 filename reference (basename only)

  • coord_datasets (tuple of str) – Dataset names for X, Y, Z coordinates in HDF5

  • attributes (dict, optional) – Mapping of attribute names to HDF5 dataset names

  • time (float, optional) – Time value for this grid

  • step (int, optional) – Step number for this grid

  • precision (int) – Float precision (4 for float32, 8 for float64)

  • parent (xml.etree.ElementTree.Element | None)

Returns:

The created Grid element

Return type:

ET.Element

add_unstructured_grid(name, n_vertices, n_elements, h5_ref, topology_dataset='topology', geometry_dataset='geometry', topology_type='Tetrahedron', nodes_per_element=4, attributes=None, time=None, geometry_precision=8, attribute_precision=4)

Add an unstructured grid to the XDMF document.

Parameters:
  • name (str) – Name of the grid

  • n_vertices (int) – Number of vertices in the mesh

  • n_elements (int) – Number of elements (e.g., tetrahedra)

  • h5_ref (str) – HDF5 filename reference (basename only)

  • topology_dataset (str) – HDF5 dataset name for connectivity

  • geometry_dataset (str) – HDF5 dataset name for vertex coordinates

  • topology_type (str) – Element type (e.g., “Tetrahedron”, “Triangle”)

  • nodes_per_element (int) – Nodes per element (e.g., 4 for tetrahedra)

  • attributes (dict, optional) – Mapping of attribute names to HDF5 dataset names

  • time (float, optional) – Time value for this grid

  • geometry_precision (int) – Float precision for geometry (default 8 = float64)

  • attribute_precision (int) – Float precision for attributes (default 4 = float32)

Returns:

The created Grid element

Return type:

ET.Element

to_string(indent=True)

Convert the XDMF document to a string.

Parameters:

indent (bool) – Whether to pretty-print with indentation

Returns:

The XDMF XML document as a string

Return type:

str

write(filepath)

Write the XDMF document to a file.

Parameters:

filepath (Path) – Path to the output file

Return type:

None

spectral_galerkin_heat.io_utils.xdmf_io.load_xdmf(xdmf_path, attr_name=None)

Load the last time step from an XDMF file.

Parameters:
  • xdmf_path (Path) – Path to the .xdmf or .xmf file.

  • attr_name (str or None) – Name of the Attribute to load (e.g. "temperature"). If None, the first <Attribute> element found is used. Also used to select among multiple temporal collections.

Return type:

FieldData

spectral_galerkin_heat.io_utils.xdmf_io.write_structured_fields(output_base, fields, x, y, z, time=None, step=None)

Write multiple structured fields to HDF5 + XDMF.

Parameters:
  • output_base (Path) – Base path for output files (without extension)

  • fields (Dict[str, np.ndarray]) – Mapping of field names to 3D arrays of shape (nz, ny, nx)

  • x (np.ndarray) – 1D coordinate arrays

  • y (np.ndarray) – 1D coordinate arrays

  • z (np.ndarray) – 1D coordinate arrays

  • time (float, optional) – Time value

  • step (int, optional) – Step number

Return type:

None

spectral_galerkin_heat.io_utils.xdmf_io.write_unstructured_fields(output_base, fields, xyz, connectivity, time=None)

Write multiple fields on an unstructured (tetrahedral) mesh to HDF5 + XDMF.

Parameters:
  • output_base (Path) – Base path for output files (without extension)

  • fields (Dict[str, np.ndarray]) – Mapping of field names to 1D arrays of shape (n_vertices,)

  • xyz (np.ndarray) – Vertex coordinates of shape (n_vertices, 3)

  • connectivity (np.ndarray) – Element connectivity of shape (n_elements, 4) for tetrahedra

  • time (float, optional) – Time value

Return type:

None