我正在尝试使用有限差分方法求解一维时间相关的Schroedinger方程,这是方程的外观以及如何离散化
假设我有N个空间点(x_i从0到N-1),并且假设我的时间跨度是K个时间点。
我努力获得K by 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
在更一般的注释中,如何在不手动定义每个节点的情况下使python解决N ode?
我想要一个大的向量xdot,该向量中的每个单元格都将是某些X [i]的函数,而我似乎未能做到这一点?也许我的方法是完全错误的?
我也觉得ivp_solve的“向量化”参数可以连接,但我不理解SciPy文档中的解释。
答案 0 :(得分:1)
问题可能是solve_ivp
期望其第一个参数有一个函数,而您提供了ode_system(psi,t,delta_x,N)
却生成了矩阵(因此得到了type error - ndarray
)。
您需要提供solve_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)