AMReX-Astro / AMReX-Astro/Castro

`define_new_center` can divide by zero when parabola fit curvature is zero

Open
#3,219 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

ai-code-audit gravity
Dominant language
C++
Stars
340
Forks
105
Avg merge
3d 8h
Merged PRs (30d)
8

Description

Summary

Castro::define_new_center computes subcell center shift via parabola fit and uses x = -b / (2a) (and similarly y, z) without guarding a == 0 (or near zero). Flat/near-flat local maxima can produce NaN/Inf center updates.

Location

  • Source/driver/Castro.cpp:4366
  • Source/driver/Castro.cpp:4372
  • Source/driver/Castro.cpp:4379

Problem Details

For each direction:

  • a = 0.5 * (f(+1) + f(-1))
  • b = 0.5 * (f(+1) - f(-1))
  • shift = -b / (2a)

If a is zero or tiny, this division is unstable and can corrupt problem::center.

Impact

  • NaN/Inf moving center values in symmetric or plateau-like density peaks.
  • Follow-on failures in any center-relative diagnostics/source terms.

Suggested Patch

diff --git a/Source/driver/Castro.cpp b/Source/driver/Castro.cpp
--- a/Source/driver/Castro.cpp
+++ b/Source/driver/Castro.cpp
@@
         Real a = 0.5_rt * (data(mi[0]+1, mi[1], mi[2]) + data(mi[0]-1, mi[1], mi[2]));
         Real b = 0.5_rt * (data(mi[0]+1, mi[1], mi[2]) - data(mi[0]-1, mi[1], mi[2]));
-        Real x = -b / (2.0_rt * a);
+        Real x = 0.0_rt;
+        if (std::abs(a) > std::numeric_limits<Real>::epsilon()) {
+            x = -b / (2.0_rt * a);
+        }
         problem::center[0] = new_center[0] + x * dx[0];
@@
-        Real y = -b / (2.0_rt * a);
+        Real y = 0.0_rt;
+        if (std::abs(a) > std::numeric_limits<Real>::epsilon()) {
+            y = -b / (2.0_rt * a);
+        }
         problem::center[1] = new_center[1] + y * dx[1];
@@
-        Real z = -b / (2.0_rt * a);
+        Real z = 0.0_rt;
+        if (std::abs(a) > std::numeric_limits<Real>::epsilon()) {
+            z = -b / (2.0_rt * a);
+        }
         problem::center[2] = new_center[2] + z * dx[2];

Notes

Choosing zero shift when the local quadratic is singular is a safe fallback that preserves the cell-center location.

Prepared by Codex

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Source/driver/Castro.cpp at the define_new_center locations around lines 4366, 4372, and 4379, and review how the parabola-fit coefficients determine each center shift. Verify the zero-curvature fallback for x, y, and z prevents NaN or Inf center values while preserving the cell-center location.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
hpc
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.