Skip to content

fix scalar multiply#992

Merged
VincentVanlaer merged 1 commit into
mainfrom
bugfix/accurate_auto_diff_multiply
May 13, 2026
Merged

fix scalar multiply#992
VincentVanlaer merged 1 commit into
mainfrom
bugfix/accurate_auto_diff_multiply

Conversation

@Debraheem
Copy link
Copy Markdown
Member

This PR fixes a typo in the accurate auto-diff multiply code.

When MESA did:

accurate_ad_value * scalar

the code accidentally multiplied by the accurate auto-diff value again, instead
of multiplying by the scalar.

So it was effectively doing:

value * value

when it should have done:

value * scalar

The fix changes the multiply routine, so it uses the scalar. This makes
accurate_ad * scalar behave the same way as scalar * accurate_ad.

This could have affected the hydro routines that use accurate auto-diff sums,
like get1_energy_eqn, get1_momentum_eqn, do1_dudt_eqn, and
do1_turbulent_energy_eqn. But those routines avoided this bug because they
either convert back to normal auto-diff before scaling, or use
the safe multiply direction. That small syntax difference prevented this bug from
breaking anything, because scalar * accurate_ad_value worked, while
accurate_ad_value * scalar was the broken operator overload.

@Debraheem Debraheem added bug Something isn't working numerics issues with math/num/mtx modules labels May 11, 2026
@warrickball
Copy link
Copy Markdown
Contributor

Yikes, good catch! I'm running the test suite on bluebear now.

Do we not have unit tests to catch this? Presumably this error would jump out for appropriate choices of op1 and op2.

I don't think I was aware of this type and am also wondering why it's in num instead of auto_diff.

@VincentVanlaer
Copy link
Copy Markdown
Member

I would say num is a fine location for this, since it is also a specific algorithm.

@Debraheem
Copy link
Copy Markdown
Member Author

I think this would have come out if the operators were switched, but i don't think it affects anything at the moment.

@VincentVanlaer VincentVanlaer merged commit d70d679 into main May 13, 2026
5 checks passed
@VincentVanlaer VincentVanlaer deleted the bugfix/accurate_auto_diff_multiply branch May 13, 2026 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working numerics issues with math/num/mtx modules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants