From 235a8d0c09a76f8ee4bc7c79c04839a93b2ed673 Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Wed, 11 Mar 2026 15:49:33 -0300 Subject: [PATCH] change the abort check and add a tiny tolerance in floating-point deviations in get_rx and get_rz funcitons --- src/DMSwarm_move.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/DMSwarm_move.cpp b/src/DMSwarm_move.cpp index 678017f..120f448 100644 --- a/src/DMSwarm_move.cpp +++ b/src/DMSwarm_move.cpp @@ -123,17 +123,33 @@ PetscInt get_k(PetscReal cz){ } PetscReal get_rx(PetscReal cx, PetscInt i){ - PetscReal rx = (cx-i*dx_const)/dx_const; - if (rx<0 || rx>1) {printf("weird rx=%f\n",rx); exit(1);} + PetscReal rx = (cx - i*dx_const) / dx_const; - return rx; + // tolerate tiny floating-point deviations around the boundaries. + if (rx < -1e-12 || rx > 1.0 + 1e-12) { + printf("weird rx=%.16e cx=%.16e i=%d\n", rx, cx, i); + exit(1); + } + + if (rx < 0.0) rx = 0.0; + if (rx > 1.0) rx = 1.0; + + return rx; } PetscReal get_rz(PetscReal cz, PetscInt k){ - PetscReal rz = (cz-(-depth+k*dz_const))/dz_const; - if (rz<0 || rz>1) {printf("weird rz=%f\n",rz); exit(1);} + PetscReal rz = (cz - (-depth + k*dz_const)) / dz_const; + + // tolerate tiny floating-point deviations around the boundaries. + if (rz < -1e-12 || rz > 1.0 + 1e-12) { + printf("weird rz=%.16e cz=%.16e k=%d\n", rz, cz, k); + exit(1); + } + + if (rz < 0.0) rz = 0.0; + if (rz > 1.0) rz = 1.0; - return rz; + return rz; } PetscErrorCode moveSwarm(int dimensions, PetscReal dt)