# Trying to CVODE\_BDF with complex-valued ODE (?)

**URL:** https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710
**Category:** General Usage
**Tags:** question
**Created:** [May 24, 2021, 8:20am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710 "2021-05-24T08:20:33Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Caique\_Rodrigues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caique_rodrigues/32/25812_2.png) [@Caique\_Rodrigues](https://discourse.julialang.org/u/Caique_Rodrigues)
#### Post date: [May 24, 2021, 8:20am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/1 "2021-05-24T08:20:33Z")

</div>

Most of the systems that I solve have complex-valued functions and, until now, I always separate the real and imaginary part of each equation to solve them in the real domain, i.e., going from a single complex equation to two-coupled real ones. But then I tried to implement the complex problem directly into the system now that I am using Julia (I am new at Julia, but im enjoying it).

In this topic [DifferentialEquations.jl error when solving equations with complex variables - Usage - JuliaLang](https://discourse.julialang.org/t/differentialequations-jl-error-when-solving-equations-with-complex-variables/7939) I saw how to implement complex-valued ODE, but what if the system was stiff? Most of time I use solvers like CVODE\_BDF, but for complex-valued ODE this warning happens: “MethodError: Cannot `convert` an object of type Vector{ComplexF64} to an object of type Ptr{Sundials.\_generic\_N\_Vector}”

I don’t know if I can do something in my problem description to avoid this error or the solver simple can’t handle this kind of problem. I will try to encompass the idea of my code in the example below (the following system isn’t necessary stiff, its just an example):

function example!(du,u,p,t)  
x,y = u  
du[1] = dx = y  
du[2] = dy = im\*x  
end

Δt = (0, 10)  
x₀ = 1 + 0_im  
y₀ = 1 + 0_im  
u₀ = [x₀,y₀]

prob = ODEProblem(example!, u₀, Δt)  
@time sol = solve(prob, CVODE\_BDF())

It is the solver that can’t handle this kind of problems or is something that I am doing wrong? How can I solve these kind of problem without always separating in real/imaginary parts ?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 24, 2021, 10:07am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/2 "2021-05-24T10:07:59Z")

</div>

Using OrdinaryDiffEq methods is a good alternative.

```julia
using DifferentialEquations
function example!(du,u,p,t)
    x,y = u
    du[1] = dx = y
    du[2] = dy = im*x
end

Δt = (0, 10.0)
x₀ = 1.0 + 0im
y₀ = 1.0 + 0im
u₀ = [x₀,y₀]

prob = ODEProblem(example!, u₀, Δt)
@time sol = solve(prob, QNDF(autodiff=false))
@time sol = solve(prob, TRBDF2(autodiff=false))
@time sol = solve(prob, KenCarp47(autodiff=false))

```

With `QNDF` now outperforming Sundials CVODE it makes a lot of sense to just use that, since it’s completely compatible with complex numbers (as long as you avoid ForwardDiff.jl).

---

<div class="post-metadata">

### Author: ![Caique\_Rodrigues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caique_rodrigues/32/25812_2.png) [@Caique\_Rodrigues](https://discourse.julialang.org/u/Caique_Rodrigues)
#### Post date: [May 24, 2021, 10:34am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/3 "2021-05-24T10:34:31Z")

</div>

Oh sure, I use OrdinaryDiffEq methods. I didn’t know this QNDF, I will definitely take a look.

So the problem with what I was doing is the autodiff=true ? We cannot autodiff at CVODE while using complex numbers ?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 24, 2021, 10:36am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/4 "2021-05-24T10:36:21Z")

</div>

> [@Caique\_Rodrigues](#):
>
> So the problem with what I was doing is the autodiff=true ? We cannot autodiff at CVODE while using complex numbers ?

CVODE doesn’t autodiff. CVODE is a C++ code which only works on Float64 because it’s ahead of time compiled. 🤷‍♂️ it’s probably not fixable. (Well, we could split to real and imaginary parts for you, but that would be a mess and I don’t really see the point with OrdinaryDiffEq.jl around. If someone else wants to put the work into it, more power to them.)

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [May 25, 2021, 6:15am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/5 "2021-05-25T06:15:56Z")

</div>

> [@ChrisRackauckas](#):
>
> With `QNDF` now outperforming Sundials CVODE it makes a lot of sense to just use that,

Do you a place where you document this? This is not what I see in a particular application, perhaps I use wrong options

---

<div class="post-metadata">

### Author: ![Caique\_Rodrigues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caique_rodrigues/32/25812_2.png) [@Caique\_Rodrigues](https://discourse.julialang.org/u/Caique_Rodrigues)
#### Post date: [May 25, 2021, 6:57am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/6 "2021-05-25T06:57:50Z")

</div>

I was trying several methods and, so far, CVODE\_BDF was doing better indeed.

At least for my system, QNDF was not able to solve it.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 25, 2021, 9:29am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/7 "2021-05-25T09:29:43Z")

</div>

You’re using yesterday’s release?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 25, 2021, 11:38am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/8 "2021-05-25T11:38:24Z")

</div>

> [@rveltz](#):
>
> Do you a place where you document this? This is not what I see in a particular application, perhaps I use wrong options

Using the OrdinaryDiffEq v5.56 release of last night, the SciMLBenchmarks just finished updating. So see things like:

[https://benchmarks.sciml.ai/html/StiffODE/Pollution.html](https://benchmarks.sciml.ai/html/StiffODE/Pollution.html)

QNDF either trades blows with CVODE\_BDF or, at lower tolerances, it is a half magnitude below. You see this across all of the benchmarks. Note that BCR is re-running right now as well.

> [@Caique\_Rodrigues](#):
>
> At least for my system, QNDF was not able to solve it.

Make sure you’re using last night’s release. It’s a result from the last day.

> **[SciML: Open Source Software for Scientific Machine Learning](https://sciml.ai/news/2021/05/24/QNDF/)**
>
> Open Source Software for Scientific Machine Learning

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [May 25, 2021, 11:54am UTC](https://discourse.julialang.org/t/trying-to-cvode-bdf-with-complex-valued-ode/61710/9 "2021-05-25T11:54:47Z")

</div>

> [@ChrisRackauckas](#):
>
> Using the OrdinaryDiffEq v5.56 release of last night, the SciMLBenchmarks just finished updating. So see things like:

BEAUTIFUL!
