user3780330
user3780330

Reputation: 71

Solver breaks down with variables

I have been struggling with a this problem for the past couple of weeks. I am working on solving a series of ODEs in Python for a research project I am on. (The python code has to imitate MATLAB code that came before it.)

The relevant segment is here:

def dy (t,y,params):

    dy = np.zeros(3)

    wL = params[0]
    T = params[1]
    CH4 = params[2]
    k = params[3]
    ka = params[4]
    H = params[5]
    kmt = params[6]
    E = params[7]
    d = params[8]
    y0 = params[9]

#    dy[1] = 1500000 - 0 - 9.987338917338950e-11*y[1]*1000000
#    - 1.437829040671314e-17*y[1]*9.84e11

     dy[1] = E[1]-y[1]*d[1]
     - k[1]*y[1]*y0[14]
     - k[55]*y[1]*y0[37]

     dy[2] = 0 - 0 + 9.987338917338950e-11*y[1]*1000000 
     - 7.742358922306635e-12*y[2]*0 + 0.7*7.432069505555159e-12*0*1000000
     -3.5e-15*(0 + 0)*y[2] - 2* 7e-16*y[2]**2 


     return dy

Which works with:

y = np.zeros(3)
ode15s = ode(dy)

y[1] = 2.46e10
ode15s.set_initial_value(y)
ode15s.set_f_params(params)
ode15s.set_integrator('vode', method = 'bdf')

The commented out lines work correctly, but the uncommented equivalent lines with variables instead of variables do not work correctly. Any suggestions?

Upvotes: 1

Views: 71

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114811

If your source code contains code exactly like this:

     dy[1] = E[1]-y[1]*d[1]
     - k[1]*y[1]*y0[14]
     - k[55]*y[1]*y0[37]

then the problem is that dy[1] is being computed as dy[1] = E[1] - y[1]*d[1], and the next two lines are (effectively) ignored. They are valid python expressions, but they are not part of the previous line, and they are not assigned to anything, so the values are ignored.

Any of the following will fix that problem:

  • Make it one line:

     dy[1] = E[1]-y[1]*d[1] - k[1]*y[1]*y0[14] - k[55]*y[1]*y0[37]
    
  • Enclose the entire expression in parentheses:

     dy[1] = (E[1]-y[1]*d[1]
              - k[1]*y[1]*y0[14]
              - k[55]*y[1]*y0[37])
    
  • Use a line continuation character (\) at the end of the lines to be continued:

     dy[1] = E[1]-y[1]*d[1] \
     - k[1]*y[1]*y0[14]     \
     - k[55]*y[1]*y0[37]
    

Upvotes: 1

Related Questions