Skip to content

clamp periodic coordinate to prevent evaluation at rmax.#1101

Closed
Quntized wants to merge 1 commit intoCExA-project:mainfrom
Quntized:speriodic_wrap
Closed

clamp periodic coordinate to prevent evaluation at rmax.#1101
Quntized wants to merge 1 commit intoCExA-project:mainfrom
Quntized:speriodic_wrap

Conversation

@Quntized
Copy link
Copy Markdown
Contributor

The periodic wrapping formula in SplineEvaluator::eval() can produce

a coordinate exactly equal to rmax due to floating-point arithmetic. For periodic splines, rmax is outside the valid evaluation range [rmin, rmax) because rmax and rmin are the same physical point. Example: coord = rmin - 1e-16
floor((-1e-16 - 0.0) / length) = -1
coord -= (-1) * length = length - 1e-16 ≈ rmax

At rmax, eval_basis cannot find a valid cell , leading to incorrect B-spline evaluation.

Fix: after wrapping, if the result >= rmax, replace with rmin. This is mathematically equivalent for periodic domains (same point) but ensures eval_basis finds a valid cell.

…wrapping formula in SplineEvaluator::eval() can produce

a coordinate exactly equal to rmax due to floating-point arithmetic.
For periodic splines, rmax is outside the valid evaluation range
[rmin, rmax) because rmax and rmin are the same physical point.
Example: coord = rmin - 1e-16
  floor((-1e-16 - 0.0) / length) = -1
  coord -= (-1) * length = length - 1e-16 ≈ rmax

At rmax, eval_basis cannot find a valid cell (cells are half-open
intervals [b_i, b_{i+1})), leading to incorrect B-spline evaluation.

Fix: after wrapping, if the result >= rmax, replace with rmin.
This is mathematically equivalent for periodic domains (same point)
but ensures eval_basis finds a valid cell.
@Quntized Quntized requested a review from tpadioleau as a code owner March 28, 2026 18:06
@EmilyBourne
Copy link
Copy Markdown
Collaborator

Isn't t his case already handled by the tolerance in the eval_basis function? Do you have a MRE showing the problem you are fixing?

@tpadioleau
Copy link
Copy Markdown
Member

@Quntized I agree with @EmilyBourne, could you provide a problematic snippet of code that your patch solves ?

@Quntized
Copy link
Copy Markdown
Contributor Author

Thank you both for pointing this out. I checked get_icell_and_offset and find_cell_start — both already handle x >= rmax() by clamping to the last valid cell. I should have checked these functions before opening the PR. Lesson learned — I'll verify the full call chain before proposing fixes in the future. Closing this.

@Quntized Quntized closed this Mar 30, 2026
@Quntized Quntized deleted the speriodic_wrap branch March 30, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants