I am writing a program in Python to solve the Schrödinger equation using the Free ICI Method (well, SICI method right now... but Free ICI is what it will turn into). If this does not sound familiar, that is because there is very little information out there on the subject, and absolutely no sample code to work from.
This process involves iteratively arriving at a solution to the partial differential equation. In doing this, there are a lot of symbolic derivatives that need to be performed. The problem is, as the program runs, the functions that need to be differentiated continue to get larger and larger so that by the fifth iteration it takes a very large amount of time to compute the symbolic derivatives.
I need to speed this up because I'd like to be able to achieve at least 30 iterations, and I'd like to have it do that before I retire.
I've gone through and removed unnecessary repeats of calculations (or at least the ones I know of), which has helped quite a bit. Beyond this, I have absolutely no clue how to speed things up.
Here is the code where containing the function that is computing the derivatives (the inf_integrate
function is just the composite Simpson’s method, as it is way faster than using SymPy’s integrate
, and doesn’t raise errors due to oscillatory functions):
from sympy import *
def inf_integrate(fun, n, a, b):
f = lambdify(r, fun)
h = (b-a)/n
XI0 = f(a) + f(b)
XI1 = 0
XI2 = 0
for i in range(1, n):
X = a + i*h
if i % 2 == 0:
XI2 = XI2 + f(X)
else:
XI1 = XI1 + f(X)
XI = h*(XI0 + 2*XI2 + 4*XI1)/3
return XI
r = symbols('r')
def H(fun):
return (-1/2)*diff(fun, r, 2) - (1/r)*diff(fun, r) - (1/r)*fun
E1 = symbols('E1')
low = 10**(-5)
high = 40
n = 5000
g = Lambda(r, r)
psi0 = Lambda(r, exp(-1.5*r))
I1 = inf_integrate(4*pi*(r**2)*psi0(r)*H(psi0(r)), n, low, high)
I2 = inf_integrate(4*pi*(r**2)*psi0(r)*psi0(r), n, low, high)
E0 = I1/I2
print(E0)
for x in range(10):
f1 = Lambda(r, psi0(r))
f2 = Lambda(r, g(r)*(H(psi0(r)) - E0*psi0(r)))
Hf1 = Lambda(r, H(f1(r)))
Hf2 = Lambda(r, H(f2(r)))
H11 = inf_integrate(4*pi*(r**2)*f1(r)*Hf1(r), n, low, high)
H12 = inf_integrate(4*pi*(r**2)*f1(r)*Hf2(r), n, low, high)
H21 = inf_integrate(4*pi*(r**2)*f2(r)*Hf1(r), n, low, high)
H22 = inf_integrate(4*pi*(r**2)*f2(r)*Hf2(r), n, low, high)
S11 = inf_integrate(4*pi*(r**2)*f1(r)*f1(r), n, low, high)
S12 = inf_integrate(4*pi*(r**2)*f1(r)*f2(r), n, low, high)
S21 = S12
S22 = inf_integrate(4*pi*(r**2)*f2(r)*f2(r), n, low, high)
eqn = Lambda(E1, (H11 - E1*S11)*(H22 - E1*S22) - (H12 - E1*S12)*(H21 - E1*S21))
roots = solve(eqn(E1), E1)
E0 = roots[0]
C = -(H11 - E0*S11)/(H12 - E0*S12)
psi0 = Lambda(r, f1(r) + C*f2(r))
print(E0)
The program is working and converges to exactly what the expected result is, but it is way too slow. Any help on speeding this up is very much appreciated.