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

Passthrough AD in ode with complex numbers #860

Merged
merged 7 commits into from
Jan 1, 2023

Conversation

AmitRotem
Copy link
Contributor

see issue #858

@AmitRotem
Copy link
Contributor Author

Seems that real(eltype(u0)) used for the complex case fails on other types, like unitful quantity.
Specialized promote_u0 for u0::AbstractArray{Complex}

@AmitRotem
Copy link
Contributor Author

This should solve the complex ad test.
Not sure what is the problem with the Sundails.jl test.
Nor the format test.

@ChrisRackauckas
Copy link
Member

The Sundials thing is unrelated. The formatting failure is because it's not following the standard formatting rules https://docs.sciml.ai/SciMLStyle/stable/, but I'll just run the formatter after merging. In the future, remember to do:

using JuliaFormatter, SomePackage
format(joinpath(dirname(pathof(SomePackage)), ".."))

@AmitRotem
Copy link
Contributor Author

AmitRotem commented Jan 4, 2023

Found a small issue with complex numbers. The Dual of time in the ode function has NaN values. Meaning, in the ode function (u,p,t) -> ... t is Dual with derivative NaN
Maybe a problem with the promotion of tspan ? affecting the initial dt value ?

Example;

using LinearAlgebra, DifferentialEquations
import ForwardDiff as FD

H0 = randn(2,2)
u0 = [1.0, 0im]
Ht(u,p,t) = (H0*u)*cos(p*t)
prob0 = ODEProblem(Ht, u0, (0.0,1.0))

function loss(p)
    prob = remake(prob0; p)
    sol = solve(prob)
    lo = abs2(tr(first(sol.u)'last(sol.u)))
    lo
end

loss(rand())
FD.derivative(loss, rand()) # NaN's

This does not show up in other cases, such as;

  • if the initial difference is small; e.g., Ht(u,p,t) = (H0*u)*sin(p*t)
  • other random initial state; u0 = randn(ComplexF64,2)
  • real initial state u0 = [1.0, 0.0]
  • use some finite dt in ODEProblem(...; dt=1)

Also nan safe mode for ForwardDiff solve this.

@ChrisRackauckas
Copy link
Member

Open a new issue for this.

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.

2 participants