Skip to content

wwinp files: Fix MemoryError in WeightWindowsList.export_to_hdf5 and speed up from_wwinp#3942

Open
yrrepy wants to merge 2 commits into
openmc-dev:developfrom
yrrepy:wwinp_2GB_faster_B
Open

wwinp files: Fix MemoryError in WeightWindowsList.export_to_hdf5 and speed up from_wwinp#3942
yrrepy wants to merge 2 commits into
openmc-dev:developfrom
yrrepy:wwinp_2GB_faster_B

Conversation

@yrrepy
Copy link
Copy Markdown
Contributor

@yrrepy yrrepy commented May 22, 2026

Description

This PR:

  1. Fixes: openmc.WeightWindowsList.from_wwinp('wwinp') fails when processing wwinp files that are larger than ~2GB
  2. Speeds-up WeightWindowsList processing (which gets to be slow with multi-GB wwinp).

  1. WeightWindowsList.export_to_hdf5 raised MemoryError on multi-GB wwinp inputs because the XML path built multi-GB ASCII strings inside lxml. Now writes HDF5 directly via h5py, matching the C++ writer.

  2. WeightWindowsList.from_wwinp was dominated by per-element isinstance checks in check_iterable_type (~90% of total time on multi-million- element bound arrays). Added a fast path for numpy float/complex ndarrays; ~11× speedup on a 172M-element wwinp (397 s → 35 s).

This enables support for many-GB wwinp files and faster processing of them.

An alternative re-factoring style is available here:
https://github.com/yrrepy/openmc/tree/wwinp_2GB_faster

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

Float/complex ndarrays are dtype-validated, so the per-element
isinstance() scan is redundant. Also construct upper_ww_bounds in
WeightWindows.__init__ as an ndarray multiplication (not a list
comprehension) so the upper-bounds setter benefits too. ~11x speedup
on 172M-element wwinp inputs (397 s -> 35 s).
@yrrepy yrrepy requested a review from pshriwise as a code owner May 22, 2026 21:33
The XML serialization raised MemoryError on bound arrays >~200M
elements -- lxml's intermediate ASCII allocation fails before the
text node can be built. Write HDF5 directly via h5py, mirroring
the C++ WeightWindows::to_hdf5 writer.

Critical details for C++ compatibility:
- Bounds are 2D (ne, n_voxels) on disk (4D would segfault the
  C++ tensor::Tensor<double> reader).
- max_lower_bound_ratio is written unconditionally (default 1.0).
- Root attrs filetype and version are required by
  openmc_weight_windows_import.

Mesh writing is handled by a private _write_mesh_group helper in
weight_windows.py that dispatches by mesh type, matching the
reference implementation. UnstructuredMesh raises NotImplementedError
(wwinp cannot produce one).
@yrrepy yrrepy force-pushed the wwinp_2GB_faster_B branch from 09ed5dc to d3124ee Compare May 23, 2026 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant