# Problem with using ODE solvers with autodifferentiation

**URL:** <https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808>\
**Category:** New to Julia\
**Created:** [October 12, 2019, 5:18am UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808 "2019-10-12T05:18:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nirvik](https://avatars.discourse-cdn.com/v4/letter/n/8491ac/32.png) [@Nirvik](https://discourse.julialang.org/u/Nirvik)\
**Post date:** [October 12, 2019, 5:18am UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/1 "2019-10-12T05:18:49Z")

</div>

I am using the DifferentialEquations package to simulate Hodgkin Huxley neurons. This involves evaluating certain parameters inside the ODE function using dependent variables for which I need to make function calls from within the ODE function, e.g.  
V = u[1];  
[param1, param2] = my\_function(V);  
du[1] = (V - param1) / param2;

To speed up the code I have made my\_function accept only Float64 by type assertion:

function my\_function(V::Float64)  
#operations  
end

The problem is while using certain solvers that use autodiff (e.g. QNDF() or Rodas4()) I get an error when instead of passing a Float64 a Dual type number is passed. The solution [here](http://docs.juliadiffeq.org/latest/basics/faq.html) describes two workarounds: 1st is obvious (turn off autodiff). However i don’t understand the second. Could anyone please explain it to me? If I just turn off autodiff the accuracy of the solution will take a hit right?  
Thanks

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 12, 2019, 5:46am UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/2 "2019-10-12T05:46:15Z")

</div>

Restricting the input types has no effect on performance, just remove the restriction and you’ll be fine 😀

[https://docs.julialang.org/en/v1/manual/performance-tips/index.html](https://docs.julialang.org/en/v1/manual/performance-tips/index.html)

See “type declaratios”

---

<div class="post-metadata">

**Author:** ![Nirvik](https://avatars.discourse-cdn.com/v4/letter/n/8491ac/32.png) [@Nirvik](https://discourse.julialang.org/u/Nirvik)\
**Post date:** [October 12, 2019, 4:18pm UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/3 "2019-10-12T16:18:04Z")

</div>

Actually it does seem to make a difference. Please see attached screenshot:  
I used QNDF() from the DifferentialEquations package: without any type assertions on the input to the functions computing the parameters (hence with autodiff=true) vs. with Float64 type assertions (hence autodiff=false). However at the expense of increased time, the solution (neuron membrane voltage plot in the plot window) did not drift off with autodiff turned on (blue=autodiff, orange=no autodiff). So assuming autodiff gives the better solution, i guess I have to compromise on the time and memory effeciency.  
Are there better ways to approach this? Please advise.  
Thanks

 ![Screenshot%20from%202019-10-12%2011-04-27](https://global.discourse-cdn.com/julialang/original/3X/5/5/5570f545eaf53a829a42f7d8cbe2bc8239c32d59.png)

---

<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:** [October 12, 2019, 5:30pm UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/4 "2019-10-12T17:30:03Z")

</div>

> [@Nirvik](#):
>
> However at the expense of increased time

Type assertions in function signatures don’t do anything for performance. Functions autospecialize on types. That’s the very essence of why Julia works.

---

<div class="post-metadata">

**Author:** ![Nirvik](https://avatars.discourse-cdn.com/v4/letter/n/8491ac/32.png) [@Nirvik](https://discourse.julialang.org/u/Nirvik)\
**Post date:** [October 12, 2019, 6:00pm UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/5 "2019-10-12T18:00:44Z")

</div>

Thanks for clarifying. So is it using/not using autodiff that makes the difference here?

---

<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:** [October 12, 2019, 7:18pm UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/6 "2019-10-12T19:18:29Z")

</div>

Yes. It has to build double caches since it needs to use `(m+1)*n` sized caches for `m` partials

---

<div class="post-metadata">

**Author:** ![Nirvik](https://avatars.discourse-cdn.com/v4/letter/n/8491ac/32.png) [@Nirvik](https://discourse.julialang.org/u/Nirvik)\
**Post date:** [October 14, 2019, 1:22am UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/7 "2019-10-14T01:22:54Z")

</div>

Thanks a lot for the help!

---

<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:** [October 14, 2019, 6:12am UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/8 "2019-10-14T06:12:26Z")

</div>

You can probably use `StaticArrays.jl` to reduce the difference in timing. Indeed, you seem to have a low dimensional ODE.

---

<div class="post-metadata">

**Author:** ![Nirvik](https://avatars.discourse-cdn.com/v4/letter/n/8491ac/32.png) [@Nirvik](https://discourse.julialang.org/u/Nirvik)\
**Post date:** [October 15, 2019, 11:36pm UTC](https://discourse.julialang.org/t/problem-with-using-ode-solvers-with-autodifferentiation/29808/9 "2019-10-15T23:36:25Z")

</div>

Thanks for the advice, I am new to Julia and am learning Static Array implementation…hoping for the best.
