6

I am trying to solve following differential equation using python package PyDDE:

dy[i]/dt = w[i] + K/N * \sum{j=1toN} sin(y[j] -y[i]), where i = 1,2,3,4...N=50

Below is the python code to solve this equation

from numpy import random, sin, arange, pi, array, zeros
import PyDDE.pydde as p

def odegrad(s, c, t):
    global N
    K = c[0]
    theta = s[0]
    w = random.standard_cauchy(N)
    for i in range(N):
        coup_sum = 0.0
        for j in range(N):
            coup_sum += sin(theta[j] - theta[i])
        theta[i] = w[i] + (K*coup_sum)/(float (N))
    return array([theta])

# constant parameters
global N
N = 50
K = 1.0
# initial values for state theta
theta0 = zeros(N, float)
for i in range(N):
    theta0[i] = random.uniform(0, 2*pi)

odecons = array([K])
odeist = array([theta0])
odestsc = array([0.0])

ode_eg = p.dde()
ode_eg.dde(y=odeist, times=arange(0.0, 300.0, 1.0), 
       func=odegrad, parms=odecons, 
       tol=0.000005, dt=1.0, hbsize=0, nlag=0, ssc=odestsc)
ode_eg.solve()
print ode_eg.data

I am getting following error:

DDE Error: Something is wrong: perhaps one of the supplied variables has the wrong type?

DDE Error: Problem initialisation failed!

DDE Error: The DDE has not been properly initialised!

None

matsjoyce
  • 5,744
  • 6
  • 31
  • 38
ADK
  • 259
  • 2
  • 17
  • Your code looks correct, what version of Python are you using? The only thing I can think would be the problem is that either odecons,odeist, or odestsc is not really an array or that you might need to return just theta in the ode grad function. Step through the code and print the types of all the variables. – Jared Reeves Sep 27 '14 at 04:30
  • @JaredReeves I am using Python 2.7.8 and installed PyDDE from https://github.com/hensing/PyDDE as I had installation issue with current version of PyDDE. I have tried every possible thing, however I will do it again as what you have mentioned. – ADK Sep 27 '14 at 06:01
  • I would just use sympy. – Games Brainiac Oct 10 '14 at 01:29

1 Answers1

1

So I have had a look at what was going on internally, and both errors

DDE Error: Something is wrong: perhaps one of the supplied variables has the wrong type?
DDE Error: Problem initialisation failed!

come from the following operation failing: map(float,initstate) (see the source, line 162). This comes from the fact that Y and your other variables are vectors. Mostly this means that you should not use array([theta]) but you should use theta

Full script:

from numpy import random, sin, arange, pi, array, zeros
import PyDDE.pydde as p

def odegrad(s, c, t):
    global N
    K = c[0]
    #Change here
    theta = s
    w = random.standard_cauchy(N)
    for i in range(N):
        coup_sum = 0.0
        for j in range(N):
            coup_sum += sin(theta[j] - theta[i])
        theta[i] = w[i] + (K*coup_sum)/(float (N))
    #Change here
    return theta

# constant parameters
global N
N = 50
K = 1.0
# initial values for state theta
theta0 = zeros(N, float)
for i in range(N):
    theta0[i] = random.uniform(0, 2*pi)

odecons = array([K])
#Change here
odeist = theta0
odestsc = array([0.0])

ode_eg = p.dde()
ode_eg.dde(y=odeist, times=arange(0.0, 300.0, 1.0), 
       func=odegrad, parms=odecons, 
       tol=0.000005, dt=1.0, hbsize=0, nlag=0, ssc=odestsc)

#You should not use this line, as the last step in ode_eg.dde() is solve.
#ode_eg.solve()
print ode_eg.data
Flavian Hautbois
  • 2,940
  • 6
  • 28
  • 45