Skip to content

BUG: accept roundoff-sized overshoot in Function.compose - #1222

Open
Gui-FernandesBR wants to merge 5 commits into
developfrom
bug/compose-domain-tolerance
Open

Gui-FernandesBR wants to merge 5 commits into
developfrom
bug/compose-domain-tolerance

Conversation

@Gui-FernandesBR

Copy link
Copy Markdown
Member

Related to #1209
Related to #1166

Covers the #1166 part of the Motors item of the v1.14.0 checklist, as decided in #1194. Function.compose now accepts inner values outside the domain by at most 1e-9 of the domain size and clips them to it; larger overshoots still raise the same ValueError, and extrapolate=True is unchanged. With that, the tanks use plain compose and _compose_clipped is removed; the exact-depletion tank test still reproduces the original failure without the fix.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XDW575d9C9zpS7Z1v2KXVq

Gui-FernandesBR and others added 2 commits November 17, 2026 17:48
Inner values that differ from the outer domain by floating-point noise
(e.g. -1e-17 instead of 0) made compose raise a spurious ValueError. Values
outside the domain by at most 1e-9 of the domain size are now clipped to
it; larger overshoots still raise as before. Fixing it in compose covers
every caller, not only the tank code.

Related to #1209.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDW575d9C9zpS7Z1v2KXVq
Function.compose now absorbs roundoff-sized overshoot itself, so the tank
helper that clipped any amount at six call sites is redundant. The helper
tests moved to the Function tests; the exact-depletion tank test stays and
fails if compose reverts to the strict check.

Related to #1209.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDW575d9C9zpS7Z1v2KXVq
@Gui-FernandesBR
Gui-FernandesBR requested a review from a team as a code owner November 17, 2026 20:51
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.58%. Comparing base (96bfbb3) to head (74fc106).
⚠️ Report is 6 commits behind head on develop.

❗ There is a different number of reports uploaded between BASE (96bfbb3) and HEAD (74fc106). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (96bfbb3) HEAD (74fc106)
2 1
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1222      +/-   ##
===========================================
- Coverage    91.71%   86.58%   -5.14%     
===========================================
  Files          131      131              
  Lines        18248    18252       +4     
===========================================
- Hits         16737    15803     -934     
- Misses        1511     2449     +938     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@CharlesXmf CharlesXmf left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this against Function.compose and the old _compose_clipped helper.

Moving the roundoff clip into the array–array path of compose is the right place for it, and the new tests in tests/unit/mathutils/test_function.py cover the important cases: both sides of the domain, a relative tolerance that depends on domain size, overshoots that should still raise, extrapolate=True leaving values unclipped, and callable sources still deferring to the existing no-bounds-check path. Keeping test_mass_flow_rate_tank_exact_depletion is also the right regression check for #1166.

Two notes:

  1. The old _compose_clipped helper clipped any overshoot to the domain and then composed, so a physically large over/underfill would still produce a height at the tank edge and rely on the Tank volume/height checks for a specific error. After this change, an overshoot larger than 1e-9 * (x_final - x_initial) raises ValueError inside compose (“must be within the domain”) before those Tank checks run. For the exact-depletion / roundoff case this is what you want; for a true overfill the failure mode is stricter and the message is less Tank-specific. That looks like an improvement to me, but it is worth being explicit about if callers were depending on the old silent clip.

  2. tolerance = 1e-9 * (self.x_final - self.x_initial) is zero if the domain is a single point, and is negative if x_final < x_initial. Array-sourced Functions are normally sorted, so this may not come up, but a zero-width domain would still reject a -1e-17 overshoot. Using abs(self.x_final - self.x_initial) (or a small absolute floor) would make that case safer.

I did not run the tests here.

@CharlesXmf CharlesXmf left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the tank-only clip helper into Function.compose is the right place for this, and the tests cover the important edges: both sides of the domain, a tolerance that scales with domain size, a hard reject beyond the 1e-9 relative window, and extrapolate=True still not clipping. Leaving the exact-depletion tank test as a regression for the original failure is good.

One assumption this inherits from the existing bounds check: x_initial / x_final are x_array[0] / x_array[-1], and they are treated as a low-to-high interval. tolerance = 1e-9 * (self.x_final - self.x_initial) and np.clip(..., self.x_initial, self.x_final) would go the wrong way on a decreasing array-valued domain. If compose is only used with increasing domains (as _from_sorted_arrays suggests), this is fine; otherwise abs(...) and min/max would be safer.

Comment thread rocketpy/mathutils/function.py Resolved
The 1e-9 relative overshoot is now the default of a tol parameter,
as suggested in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDW575eTpLt8NEpXTiDNqw
@Gui-FernandesBR

Copy link
Copy Markdown
Member Author

@CharlesXmf on the overfill note: _check_volume_bounds runs before the heights, so a liquid or gas volume above the tank by more than 1e-6 m³ still gets the Tank message. What changes is gas + liquid summing above the total volume: _compose_clipped (only on develop since #1166, never released) silently clipped the gas height to the top, and now it raises compose's ValueError, as in v1.13.

On the domain direction: array sources are sorted ascending on construction and _from_sorted_arrays requires sorted input, so the width is never negative. A single-point Function can't be evaluated at all, so composing into one fails before the tolerance matters. Leaving it as is.

@Gui-FernandesBR Gui-FernandesBR mentioned this pull request Nov 20, 2026
4 of 11 tasks
The tank checks accept an error of up to 1e-6 m³ in the volumes and 1e-4 m
in the heights, but Function.compose only accepted 1e-9 of the domain. An
input between the two passed the tank checks and then stopped at the domain
error of compose, in all four tank types. The six compose calls now pass
the tolerance of the tank checks, so a larger error is still reported by
the tank as underfilled or overfilled.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@MateusStano

MateusStano commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

I pushed a small commit here (74fc106)

The tank checks accept up to 1e-6 m³ of error in the volumes and 1e-4 m in the heights, but compose only accepts 1e-9 of the domain. So an input in between passed the tank checks and then stopped in compose with the "must be within the domain" message. Example: a MassFlowRateBasedTank that drains 5e-7 kg more than it has. Same thing in the other three tank types.

Now the six compose calls in tank.py pass the tank's own tolerance as tol. Bigger errors still get the "underfilled" / "overfilled" message, because every tank checks the value before composing it.

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.

4 participants