@@ -693,7 +693,7 @@ def num_mesh_cells(self):
693693 def write_data_to_vtk (self ,
694694 filename : PathLike ,
695695 datasets : dict | None = None ,
696- volume_normalization : bool = False ,
696+ volume_normalization : bool | None = None ,
697697 curvilinear : bool = False ):
698698 """Creates a VTK object of the mesh
699699
@@ -709,9 +709,10 @@ def write_data_to_vtk(self,
709709 with structured indexing in "C" ordering. See the "expand_dims" flag
710710 of :meth:`~openmc.Tally.get_reshaped_data` on reshaping tally data when using
711711 :class:`~openmc.MeshFilter`'s.
712- volume_normalization : bool, optional
712+ volume_normalization : bool or None , optional
713713 Whether or not to normalize the data by the volume of the mesh
714- elements.
714+ elements. When None (default), the format-appropriate default is
715+ used: True for legacy ASCII .vtk files, False for .vtkhdf files.
715716 curvilinear : bool
716717 Whether or not to write curvilinear elements. Only applies to
717718 ``SphericalMesh`` and ``CylindricalMesh``.
@@ -748,10 +749,14 @@ def write_data_to_vtk(self,
748749 if write_impl is None :
749750 raise NotImplementedError (
750751 f"VTKHDF output not implemented for { type (self ).__name__ } " )
751- # write_impl is a bound method – do NOT pass self again
752- write_impl (filename , datasets , volume_normalization )
752+ # vtkhdf default is False; explicit value is respected
753+ norm = volume_normalization if volume_normalization is not None else False
754+ write_impl (filename , datasets , norm )
753755 return None
754756
757+ # legacy ASCII .vtk default is True
758+ norm = volume_normalization if volume_normalization is not None else True
759+
755760 # vtk is an optional dependency only needed for the legacy ASCII path
756761 import vtk
757762 from vtk .util import numpy_support as nps
@@ -769,20 +774,26 @@ def write_data_to_vtk(self,
769774 # maintain a list of the datasets as added to the VTK arrays to
770775 # ensure they persist in memory until the file is written
771776 datasets_out = []
777+ # Regular/Rectilinear meshes store data in C (ijk) order which
778+ # matches VTK's expected ordering directly — no transpose needed.
779+ # Curvilinear meshes (Cylindrical, Spherical) require a transpose
780+ # to convert from C ordering to the Fortran (kji) ordering that VTK
781+ # expects.
782+ # TODO: update to "C" ordering throughout
783+ needs_transpose = not isinstance (
784+ self , (RegularMesh , RectilinearMesh ))
772785 for label , dataset in datasets .items ():
773786 dataset = self ._reshape_vtk_dataset (dataset )
774787 self ._check_vtk_dataset (label , dataset )
775- # If the array data is 3D, assume is in C ordering and transpose
776- # before flattening to match the ordering expected by the VTK
777- # array based on the way mesh indices are ordered in the Python
778- # API
779- # TODO: update to "C" ordering throughout
780788 if dataset .ndim == 3 :
781- dataset = dataset .ravel ()
789+ dataset = dataset .T . ravel () if needs_transpose else dataset . ravel ()
782790 datasets_out .append (dataset )
783791
784- if volume_normalization :
785- dataset /= self .volumes .ravel ()
792+ if norm :
793+ if needs_transpose :
794+ dataset /= self .volumes .T .ravel ()
795+ else :
796+ dataset /= self .volumes .ravel ()
786797
787798 dataset_array = vtk .vtkDoubleArray ()
788799 dataset_array .SetName (label )
0 commit comments