Skip to content

analysis: raise instead of NaN for empty particle types#5372

Merged
jngrad merged 3 commits into
espressomd:pythonfrom
RudolfWeeber:fix/bug-47-statistics-empty-type-div0
Jun 30, 2026
Merged

analysis: raise instead of NaN for empty particle types#5372
jngrad merged 3 commits into
espressomd:pythonfrom
RudolfWeeber:fix/bug-47-statistics-empty-type-div0

Conversation

@RudolfWeeber

Copy link
Copy Markdown
Contributor

check_particle_type only verifies 0 <= p_type <= max_seen_particle_type,
and max_seen is never decremented when particles are removed. A
seen-then-deleted type (or any populated-then-emptied type) therefore
passes validation while contributing an empty buffer / zero total mass.

On such a type the analysis routines divided by zero:

  • center_of_mass returned com/mass = 0/0 = NaN (the double mass = 1.
    placeholder was a red herring: boost::mpi::reduce overwrites the root's
    output with the reduced sum, so it never prevented the division).
  • gyration_tensor divided by buf_pos.size() == 0; its non-empty
    precondition was only a release-stripped assert(), so it produced NaN
    in Release and aborted in RelWithAssert.
    moment_of_inertia_matrix inherits center_of_mass, so it threw too.

Fix (PREVENT, matching the subsystem convention where bad analysis
preconditions throw):

  • center_of_mass: all_reduce the total mass to every rank and throw a
    std::runtime_error when it is zero. The throw is collective so the
    script interface does not deadlock on the following MPI collective.
  • gyration_tensor: replace the assert with a real throw on rank 0.
  • Analysis.cpp: move the core calls and their MPI collectives inside
    context()->parallel_try_catch (mirroring calc_rg) so the new throws
    are marshalled correctly across MPI.

Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com

🤖 Generated with Claude Code

RudolfWeeber and others added 3 commits June 15, 2026 12:02
check_particle_type only verifies 0 <= p_type <= max_seen_particle_type,
and max_seen is never decremented when particles are removed. A
seen-then-deleted type (or any populated-then-emptied type) therefore
passes validation while contributing an empty buffer / zero total mass.

On such a type the analysis routines divided by zero:
- center_of_mass returned com/mass = 0/0 = NaN (the `double mass = 1.`
  placeholder was a red herring: boost::mpi::reduce overwrites the root's
  output with the reduced sum, so it never prevented the division).
- gyration_tensor divided by buf_pos.size() == 0; its non-empty
  precondition was only a release-stripped assert(), so it produced NaN
  in Release and aborted in RelWithAssert.
moment_of_inertia_matrix inherits center_of_mass, so it threw too.

Fix (PREVENT, matching the subsystem convention where bad analysis
preconditions throw):
- center_of_mass: all_reduce the total mass to every rank and throw a
  std::runtime_error when it is zero. The throw is collective so the
  script interface does not deadlock on the following MPI collective.
- gyration_tensor: replace the assert with a real throw on rank 0.
- Analysis.cpp: move the core calls and their MPI collectives inside
  context()->parallel_try_catch (mirroring calc_rg) so the new throws
  are marshalled correctly across MPI.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@jngrad
jngrad marked this pull request as ready for review June 30, 2026 15:41
@jngrad jngrad changed the title analysis: raise instead of NaN for empty particle types (bug-sweep #47) analysis: raise instead of NaN for empty particle types Jun 30, 2026
@jngrad
jngrad merged commit 84cc1d9 into espressomd:python Jun 30, 2026
10 checks passed
@jngrad jngrad added this to the ESPResSo 5.0.2 milestone Jul 1, 2026
juliopas pushed a commit to juliopas/espresso that referenced this pull request Jul 16, 2026
)

Mass-related analysis functions now throw an exception when no particle is found.

Co-authored-by: Jean-Noël Grad <jgrad@icp.uni-stuttgart.de>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants