Pavel Penshin
Pavel Penshin

Reputation: 123

Time Dependant 1D Schroedinger Equation using Numpy and SciPy solve_ivp

I am trying to solve the 1D time dependent Schroedinger equation using finite difference methods, here is how the equation looks and how it undergoes discretization

enter image description here

Say I have N spatial points (the x_i goes from 0 to N-1), and suppose my time span is K time points.

I strive to get a K by N matrix. each row (j) will be the function at time t_j

I suspect that my issue is that I am defining the system of the coupled equations in a wrong way.

My boundary conditions are psi=0, or some constant at the sides of the box so I make the ode's in the sides of my X span to be zero

My Code:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

#Defining the length and the resolution of our x vector
length = 2*np.pi
delta_x = .01

# create a vector of X values, and the number of X values
def create_x_vector(length, delta_x):
    x = np.arange(-length, length, delta_x)
    N = len(x)
    return x, N

# create initial condition vector
def create_initial_cond(x,x0,Gausswidth):
    psi0 = np.exp((-(x-x0)**2)/Gausswidth)
    return psi0

#create the system of ODEs
def ode_system(psi,t,delta_x,N):
    psi_t = np.zeros(N)
    psi_t[0]=0
    psi_t[N-1]=0
    for i in range(1,N-1):
        psi_t[i] = (psi[i+1]-2*psi[i]+psi[i-1])/(delta_x)**2
    return psi_t

#Create the actual time, x and initial condition vectors using the functions
t = np.linspace(0,15,5000)
x,N= create_x_vector(length,delta_x)
psi0 = create_initial_cond(x,0,1)

psi = np.zeros(N)
psi= solve_ivp(ode_system(psi,t,delta_x,N),[0,15],psi0,method='Radau',max_step=0.1)

After running I get an error:



runfile('D:/Studies/Project/Simulation Test/Test2.py', wdir='D:/Studies/Project/Simulation Test')
Traceback (most recent call last):

  File "<ipython-input-16-bff0a1fd9937>", line 1, in <module>
    runfile('D:/Studies/Project/Simulation Test/Test2.py', wdir='D:/Studies/Project/Simulation Test')

  File "C:\Users\Pasha\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "C:\Users\Pasha\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Studies/Project/Simulation Test/Test2.py", line 35, in <module>
    psi= solve_ivp(ode_system(psi,t,delta_x,N),[0,15],psi0,method='Radau',max_step=0.1)

  File "C:\Users\Pasha\Anaconda3\lib\site-packages\scipy\integrate\_ivp\ivp.py", line 454, in solve_ivp
    solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)

  File "C:\Users\Pasha\Anaconda3\lib\site-packages\scipy\integrate\_ivp\radau.py", line 288, in __init__
    self.f = self.fun(self.t, self.y)

  File "C:\Users\Pasha\Anaconda3\lib\site-packages\scipy\integrate\_ivp\base.py", line 139, in fun
    return self.fun_single(t, y)

  File "C:\Users\Pasha\Anaconda3\lib\site-packages\scipy\integrate\_ivp\base.py", line 21, in fun_wrapped
    return np.asarray(fun(t, y), dtype=dtype)

TypeError: 'numpy.ndarray' object is not callable

In a more general note, how can I make python solve N ode's without manually defining each and one of them?

I want to have a big vector called xdot where each cell in this vector will be a function of some X[i]'s and I seem to fail to do that? or maybe my approach is completely wrong?

Also I feel maybe that the "Vectorized" argument of ivp_solve can be connected, but I do not understand the explanation in the SciPy documentation.

Upvotes: 1

Views: 1134

Answers (1)

Yotam Vaknin
Yotam Vaknin

Reputation: 676

The problem is probably that solve_ivp expects a function for its first parameter, and you provided ode_system(psi,t,delta_x,N) which results in a matrix instead (therefore you get type error - ndarray).

You need to provide solve_ivp a function that accepts two variables, t and y (which in your case is psi). It can be done like this:

def temp_function(t, psi):
    return ode_system(psi,t,delta_x,N)

and then, your last line should be:

psi= solve_ivp(temp_function,[0,15],psi0,method='Radau',max_step=0.1)

This code solved the problem for me.

For a shorthand way of doing this, you can also just write the function inline using Lambda :

psi= solve_ivp(lambda t,psi : ode_system(psi,t,delta_x,N),[0,15],psi0,method='Radau',max_step=0.1)

Upvotes: 1

Related Questions