neurophysik / jitcdde

Just-in-time compilation for delay differential equations
Other
56 stars 14 forks source link

UnsuccessfulIntegration Error #18

Closed elliot-brown closed 5 years ago

elliot-brown commented 5 years ago

First of all, I have to admit that I don't have much experience with DDEs. That being said I get the concept behind them, and JiTCDDE seems like the best Python package out there for solving them. (I am using version 1.3.3.) I think my code is almost there, but I'm getting an error that I believe is related to step size. Here is a working (or not-quite-working) example. (My actual code uses 6 different equations, but this example with 4 is sufficient to raise the error.)


import numpy as np
from jitcdde import jitcdde, y, t

# Params
a1 = 40.0
a2 = 50.0
u  = 0.69
f1 = 22.0
f2 = 8.4
d  = 3.0
e1 = 3.5
e2 = 5.0
K  = 22.0
τ  = 0.093

func = {
        y(1),
        -a1 * (y(0)**2 - u) * y(1) - f1 * y(0) * (y(0) + d) * (y(0) + e1), 
        y(3), 
        -a2 * (y(2)**2 - u) * y(3) - f2 * y(2) * (y(2) + d) * (y(2) + e2) + K * (y(1, t-τ) - y(3)),
       }

y0 = [-0.1, 0.025, -0.1, 0.025]

I = jitcdde(func)
I.set_integration_parameters()
I.constant_past(y0, time=0.0)
I.step_on_discontinuities()

data = []
for time in np.linspace(0, 2):
    data.append( I.integrate(time).to_list() )

It seemingly works for early iterations. (In this example, the first 35 or so steps take less than 3 seconds collectively.) But then the speed of each iteration rapidly slow down so that a few iterations later, it takes 40 seconds for a single iteration. The iteration after that takes a very long time (a couple minutes) before raising the following error:

UnsuccessfulIntegration Traceback (most recent call last)

in () 8 data = [] 9 for time in np.linspace(0, 2)): ---> 10 data.append( I.integrate(time).to_list() ) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\jitcdde\_jitcdde.py in integrate(self, target_time) 795 continue 796 --> 797 if self._adjust_step_size(): 798 self.DDE.accept_step() 799 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\jitcdde\_jitcdde.py in _adjust_step_size(self) 728 if p > self.decrease_threshold: 729 self.dt *= max(self.safety_factor*p**(-1/self.q), self.min_factor) --> 730 self._control_for_min_step() 731 return False 732 else: ~\AppData\Local\Continuum\anaconda3\lib\site-packages\jitcdde\_jitcdde.py in _control_for_min_step(self) 714 if self.atol==0: 715 message += "\n• You did not allow for an absolute error tolerance (atol) though your DDE calls for it. Even a very small absolute tolerance (1e-16) may sometimes help." --> 716 raise UnsuccessfulIntegration(message) 717 718 def _increase_chance(self, new_dt): UnsuccessfulIntegration: Could not integrate with the given tolerance parameters: atol: 1.000000e-10 rtol: 1.000000e-05 min_step: 1.000000e-10 The most likely reasons for this are: • You did not sufficiently address initial discontinuities. • The DDE is ill-posed or stiff.

I've tried playing the a_tol, r_tol, and min_step, but that doesn't seem to help. I'm not sure how else to address the initial discontinuities besides I.step_on_discontinuities(). Finally, I don't believe there is a problem with the DDE itself since it has been successfully solved with MatLab's dde23.

Though I realize this is almost certainly a user issue rather than a package issue, any help and understanding would be greatly appreciated!

Wrzlprmft commented 5 years ago

Your problem are the curly braces in the definition of func. This makes it a set instead of a list, tuple, or other iterable which preserves order. Due to the different order of equations (elements of func), you have been integrating a completely different differential equation, which probably was escalating or otherwise problematic.

A few asides on your code:

elliot-brown commented 5 years ago

That makes sense. Some examples had curly braces while others didn't, and I didn't realize there was a significant difference. That fixed everything perfectly. The other oddities were left from my previous experiments. Thanks again for you prompt response!

Wrzlprmft commented 5 years ago

Some examples had curly braces while others didn't, and I didn't realize there was a significant difference.

Depending on what you put inside, curly braces can define a dictionary or set. Dictionaries are fine; sets aren’t.

Wrzlprmft commented 5 years ago

Also, in the future, users will get an explicit error when using a set here.