基于Numpy和SciPy解的一维含时薛定谔方程

基于Numpy和SciPy解的一维含时薛定谔方程,numpy,scipy,numeric,numerical-methods,differential-equations,Numpy,Scipy,Numeric,Numerical Methods,Differential Equations,我试图用有限差分法解一维含时薛定谔方程,下面是方程的外观和离散化过程 假设我有N个空间点(x_I从0到N-1),假设我的时间跨度是K个时间点 我努力得到一个K乘N的矩阵。每一行(j)将是时间t_j的函数 我怀疑我的问题是我用错误的方式定义了耦合方程组 我的边界条件是psi=0,或者盒子两侧的某个常数,所以我使X跨度两侧的ode为零 我的代码: import numpy as np import matplotlib.pyplot as plt from scipy.integrate impo

我试图用有限差分法解一维含时薛定谔方程,下面是方程的外观和离散化过程

假设我有N个空间点(x_I从0到N-1),假设我的时间跨度是K个时间点

我努力得到一个K乘N的矩阵。每一行(j)将是时间t_j的函数

我怀疑我的问题是我用错误的方式定义了耦合方程组

我的边界条件是psi=0,或者盒子两侧的某个常数,所以我使X跨度两侧的ode为零

我的代码:

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)
运行后,我得到一个错误:



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


运行文件('D:/Studies/Project/Simulation Test/Test2.py',wdir='D:/Studies/Project/Simulation Test')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('D:/Studies/Project/Simulation Test/Test2.py',wdir='D:/Studies/Project/Simulation Test')
文件“C:\Users\Pasha\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第704行,在runfile中
execfile(文件名、命名空间)
文件“C:\Users\Pasha\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第108行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“D:/Studies/Project/Simulation Test/Test2.py”,第35行,在
psi=求解ivp(ode_系统(psi,t,delta_x,N),[0,15],psi0,方法='Radau',最大步长=0.1)
文件“C:\Users\Pasha\Anaconda3\lib\site packages\scipy\integrate\\u ivp\ivp.py”,第454行,在solve\u ivp中
解算器=方法(乐趣、t0、y0、tf、矢量化=矢量化,**选项)
文件“C:\Users\Pasha\Anaconda3\lib\site packages\scipy\integrate\\ u ivp\radau.py”,第288行,在\uuu init中__
self.f=self.fun(self.t,self.y)
文件“C:\Users\Pasha\Anaconda3\lib\site packages\scipy\integrate\\u ivp\base.py”,第139行
回归自我。快乐单身(t,y)
文件“C:\Users\Pasha\Anaconda3\lib\site packages\scipy\integrate\\u ivp\base.py”,第21行,用fun\u包装
返回np.asarray(fun(t,y),dtype=dtype)
TypeError:“numpy.ndarray”对象不可调用
在更一般的说明中,如何让python在不手动定义每一个ode的情况下解决N ode

我想要一个叫做xdot的大向量,这个向量中的每一个单元都是X[I]的函数,我似乎没有做到这一点?或者我的方法完全错了


另外,我觉得ivp_solve的“矢量化”参数可能可以连接,但我不理解SciPy文档中的解释。

问题可能是
solve_ivp
需要一个函数作为其第一个参数,而您提供了
ode_系统(psi,t,delta_x,N)
,结果是一个矩阵(因此您会得到
类型错误-ndarray

您需要提供
solve\u ivp
一个函数,该函数接受两个变量,
t
y
(在您的情况下是psi)。可以这样做:

def temp_function(t, psi):
    return ode_system(psi,t,delta_x,N)
然后,你的最后一行应该是:

psi= solve_ivp(temp_function,[0,15],psi0,method='Radau',max_step=0.1)
这个代码为我解决了这个问题

为了简化操作,您也可以使用Lambda内联编写函数:

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

你能详细说明一下我所做的和lambda所做的之间的区别吗?或者你的“更长”解决方案所做的是什么?它们都做相同的事情。lambda允许你内联定义一个新函数,而不是编写“def()blah blah…”并且只使用一次。