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

Support Complex Numbers in Stiff Routines #110

Closed
ChrisRackauckas opened this issue Nov 15, 2016 · 7 comments
Closed

Support Complex Numbers in Stiff Routines #110

ChrisRackauckas opened this issue Nov 15, 2016 · 7 comments
Labels

Comments

@ChrisRackauckas
Copy link
Member

ChrisRackauckas commented Nov 15, 2016

After a long journey with @AlexanderKoshkarov, we found that the OrdinaryDiffEq.jl non-stiff (Runge-Kutta) routines are compatible with complex numbers (with adaptivity), but the implicit and Rosenbrock methods are not. This is because the Calculus.jl (now FiniteDiff.jl) and ForwardDiff.jl dependencies cannot take derivatives of complex numbers:

johnmyleswhite/FiniteDiff.jl#14
JuliaDiff/ForwardDiff.jl#157

Also, NLsolve.jl is incompatible with complex numbers. On the other hand, the ODEInterface and Sundials algorithms do not seem to be compatible with complex numbers, and so all of this is a dead-end.

Ways around this are:

  • Implement Jacobian-free nonlinear solvers (Jacobian-Free Newton Krylov)
  • Implement a simple package just for Jacobians and derivatives with complex numbers
  • Get a PR into FiniteDiff / ForwardDiff

I don't think I'll have the time for this right now, but am documenting this in case anyone else has an idea / wants to help.

(@mauro3 do you have routines for Jacobians?)

@mauro3
Copy link

mauro3 commented Nov 15, 2016

Sorry, I'm no help here. What about @pwl's DASSL?

@ChrisRackauckas
Copy link
Member Author

Looks like Calculus.jl might be easy to solve: JuliaMath/Calculus.jl#115

@ChrisRackauckas
Copy link
Member Author

Solved by DiffEqDiffTools.jl replacing Calculus.jl (and gets like a 50x speedup!). ForwardDiff.jl will still fail though, but that should be fixed by it changing to a Cassette.jl backend.

@jebej
Copy link

jebej commented Jun 1, 2020

I am still getting errors with stiff routines, for example with Rosenbrock23:

ERROR: ArgumentError: Cannot create a dual over scalar type Complex{Float64}. If the type behaves as a scalar, define FowardDiff.can_dual.
Stacktrace:
 [1] throw_cannot_dual(::Type) at C:\Users\Jeremy\.julia\packages\ForwardDiff\cXTw0\src\dual.jl:36
 [2] ForwardDiff.Dual{ForwardDiff.Tag{DiffEqBase.TimeGradientWrapper{Liouvillian{SparseMatrixCSC{Complex{Float64},Int64},2,Tuple{typeof(drivem),typeof(drivep)},Schrodinger.var"#46#47"{SparseMatrixCSC{Complex{Float64},Int64},Tuple{SparseMatrixCSC{Complex{Float64},Int64},Array{Complex{Float64},2}},Tuple{typeof(drivem),typeof(drivep)},Tuple{Array{Float64,1},Array{Float64,1}}},2},Array{Complex{Float64},1},DiffEqBase.NullParameters},Complex{Float64}},Complex{Float64},1}(::Complex{Float64}, ::ForwardDiff.Partials{1,Complex{Float64}}) at C:\Users\Jeremy\.julia\packages\ForwardDiff\cXTw0\src\dual.jl:18
 [3] Dual at C:\Users\Jeremy\.julia\packages\ForwardDiff\cXTw0\src\dual.jl:55 [inlined]
 [4] Dual at C:\Users\Jeremy\.julia\packages\ForwardDiff\cXTw0\src\dual.jl:62 [inlined]
 [5] Dual at C:\Users\Jeremy\.julia\packages\ForwardDiff\cXTw0\src\dual.jl:66 [inlined]
 [6] _broadcast_getindex_evalf at .\broadcast.jl:631 [inlined]
 [7] _broadcast_getindex at .\broadcast.jl:604 [inlined]
 [8] getindex at .\broadcast.jl:564 [inlined]
 [9] copy at .\broadcast.jl:854 [inlined]
 [10] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,Type{ForwardDiff.Dual{ForwardDiff.Tag{DiffEqBase.TimeGradientWrapper{Liouvillian{SparseMatrixCSC{Complex{Float64},Int64},2,Tuple{typeof(drivem),typeof(drivep)},Schrodinger.var"#46#47"{SparseMatrixCSC{Complex{Float64},Int64},Tuple{SparseMatrixCSC{Complex{Float64},Int64},Array{Complex{Float64},2}},Tuple{typeof(drivem),typeof(drivep)},Tuple{Array{Float64,1},Array{Float64,1}}},2},Array{Complex{Float64},1},DiffEqBase.NullParameters},Complex{Float64}},V,N} where N where V},Tuple{Array{Complex{Float64},1},Array{Complex{Float64},1}}}) at .\broadcast.jl:820
 [11] build_grad_config(::Rosenbrock23{0,true,DefaultLinSolve,DataType}, ::Function, ::Function, ::Array{Complex{Float64},1}, ::Float64) at C:\Users\Jeremy\.julia\packages\OrdinaryDiffEq\RYCSN\src\derivative_wrappers.jl:175
[...]

@jebej
Copy link

jebej commented Jun 1, 2020

Note that now that I see the stacktrace more clearly this might somehow be due to the custom functor type.

@jebej
Copy link

jebej commented Jun 1, 2020

No, this is indeed due to ForwardDiff not supporting complex numbers. Maybe I am doing something wrong, any advice here?

@ChrisRackauckas
Copy link
Member Author

Use autodiff=false until JuliaDiff/ForwardDiff.jl#455

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants