From 7c13f83a802b576821a664a72847ab95abeb5a16 Mon Sep 17 00:00:00 2001 From: lmoresi Date: Sat, 6 Jun 2026 09:48:56 +0100 Subject: [PATCH] SwarmVariable._pack_array_to_data_format: guard empty partitions A rank that owns no local particles has an N=0 array; numpy then cannot infer the -1 component dimension in array_data.reshape(shape[0], -1) (raises "cannot reshape array of size 0 into shape (0,newaxis)"). This crashed parallel read_timestep of a vector field (e.g. velocity) at np>=2 when a rank had an empty partition, killing the run at checkpoint load. Compute the component count from the trailing dims explicitly when the array is empty. Underworld development team with AI support from Claude Code --- src/underworld3/swarm.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/underworld3/swarm.py b/src/underworld3/swarm.py index c69d1142f..f05410fa4 100644 --- a/src/underworld3/swarm.py +++ b/src/underworld3/swarm.py @@ -928,6 +928,14 @@ def _pack_array_to_data_format(self, array_data): """Convert array format (N,a,b) back to canonical data format (N,components)""" # Use existing pack logic but return numpy array instead of writing to PETSc # This is a pure conversion method - no PETSc access + # Empty-partition guard: an N=0 array has total size 0, so numpy cannot + # infer the -1 component dimension ("cannot reshape array of size 0 into + # shape (0,newaxis)"). This bites a rank that owns no local particles + # during a parallel read_timestep. Compute the component count from the + # trailing dims explicitly. + if array_data.size == 0: + ncomp = int(np.prod(array_data.shape[1:])) if array_data.ndim > 1 else 1 + return array_data.reshape(array_data.shape[0], ncomp) return array_data.reshape(array_data.shape[0], -1) # Legacy methods preserved for backward compatibility (now do nothing)