Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider type of t0 in promote_u0 #1039

Merged
merged 3 commits into from
Jun 6, 2024

Conversation

AayushSabharwal
Copy link
Member

@AayushSabharwal AayushSabharwal commented May 22, 2024

Close SciML/ModelingToolkit.jl#2721

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

Comment on lines 268 to 275
eltype(u0) <: ForwardDiff.Dual && return u0
T = anyeltypedual(p)
if T <: ForwardDiff.Dual
return T.(u0)
end
T = anyeltypedual(t0)
if T <: ForwardDiff.Dual
return T.(u0)
Copy link
Member

Choose a reason for hiding this comment

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

This isn't necessarily correct. If these things are 3 different duals we should error. It needs to try to promote to the common dual type or error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Member Author

Choose a reason for hiding this comment

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

This apparently breaks tests, since in some cases it tests with two different duals. Previously, if u0 was a dual it would return u0 without checking if p is a dual or trying to promote. I added the same thing here to make tests pass. Should it be reverted, and the tests updated accordingly? And if so, wouldn't that be breaking?

@AayushSabharwal AayushSabharwal force-pushed the as/promote-u0-tspan branch 3 times, most recently from 75466ab to 9c375c7 Compare May 27, 2024 06:40
@ChrisRackauckas
Copy link
Member

What's the state here?

@AayushSabharwal
Copy link
Member Author

promote_type(::Type{Measurement}, ::Type{ForwardDiff.Dual}) is ambiguous. I'm fairly certain Dual{Measurement} is what this should be, but wanted confirmation. I'll PR to the appropriate repository with an extension and then hopefully tests will pass here.

@ChrisRackauckas
Copy link
Member

We just need to handle Measurement differently. Does this actually show up in the tests?

@AayushSabharwal
Copy link
Member Author

@ChrisRackauckas
Copy link
Member

. I'm fairly certain Dual{Measurement} is what this should be

I think the other way around, Measurement with dual numbers in it. Make a PR to Measurements.jl

@AayushSabharwal
Copy link
Member Author

Turns out that's not possible because Measurements requires that the wrapped type be <:AbstractFloat which Dual isn't

@ChrisRackauckas
Copy link
Member

Ask Mose if that can be relaxed.

@AayushSabharwal
Copy link
Member Author

It seems unlikely given JuliaPhysics/Measurements.jl#100 (comment). Also relaxing that would require breaking Measurements

@ChrisRackauckas
Copy link
Member

Okay then we should just change the test if that's fundamental

@AayushSabharwal
Copy link
Member Author

AayushSabharwal commented Jun 6, 2024

I'm not entirely sure why that test has duals in tspan. It looks like it's meant to test that the rhs function is unwrapped when the unknown is a measurement, which is already tested. If it's meant to test that this also works when t isa Dual then the test simply won't run, so it might as well be removed.

@ChrisRackauckas
Copy link
Member

Yeah seems fundamental. I think it can be removed as it was only working because the expected conversion, i.e. differentiation of the solve, didn't actually take place.

@ChrisRackauckas ChrisRackauckas merged commit bc50a2b into SciML:master Jun 6, 2024
21 of 40 checks passed
@AayushSabharwal AayushSabharwal deleted the as/promote-u0-tspan branch June 6, 2024 17:48
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.

ForwardDiff fails to differentiate ODE, due to type error between tspan and u0
2 participants