Skip to content

Commit 6f368c0

Browse files
committed
factored out some code, added some documentation
1 parent 744548e commit 6f368c0

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

schroedinger/plot.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@
99

1010
from schroedinger import Schroedinger1D
1111

12-
import gobject
13-
import matplotlib
14-
matplotlib.use('GTKAgg')
12+
try:
13+
import gobject
14+
import matplotlib
15+
matplotlib.use('GTKAgg')
16+
except:
17+
from sys import exit
18+
print("This app works only with GTK+ installed and matplotlib built with GTK+ support.")
19+
exit(1)
1520

1621
from pylab import plot, show, figure
1722
from numpy import abs, real, imag, vectorize
1823

24+
# initialize the two simulations
1925
s1 = Schroedinger1D(256, L=20)
2026
s2 = Schroedinger1D(256, L=20)
2127

28+
# models the double well potential
2229
def doubleWellPotential(x):
2330
sigma = 0.04
2431
pos = 2.
@@ -27,6 +34,7 @@ def doubleWellPotential(x):
2734
return 0.
2835
return 10.
2936

37+
# use the double well potential in the second plot
3038
s2.setPotential(vectorize(doubleWellPotential))
3139

3240
fig = figure()
@@ -53,8 +61,8 @@ def doubleWellPotential(x):
5361
def update():
5462
global i
5563
i += 1
56-
s1.evolve(0.01)
57-
s2.evolve(0.01)
64+
s1.evolve(0.02)
65+
s2.evolve(0.02)
5866
s1_l_amp.set_ydata(abs(s1.f()))
5967
s1_l_real.set_ydata(real(s1.f()))
6068
s1_l_imag.set_ydata(imag(s1.f()))
@@ -64,7 +72,7 @@ def update():
6472
fig.canvas.draw_idle()
6573
if not runSimulation:
6674
print "stopped simulation after {} iterations".format(i)
67-
return runSimulation
75+
return runSimulation # return False terminates this gobject-callback
6876

6977
def onpress(event):
7078
global runSimulation
@@ -75,7 +83,7 @@ def onpress(event):
7583
else:
7684
print "starting simulation"
7785
runSimulation = True
78-
gobject.idle_add(update)
86+
gobject.idle_add(update) # readding the gobject-callback function
7987

8088
fig.canvas.mpl_connect('key_press_event', onpress)
8189
show()

schroedinger/schroedinger.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
def harmonicPotential(x):
1414
return 0.5*x**2
1515

16+
def wavePacket(x, k_0):
17+
return exp(-0.5*x**2 + 1j*k_0*x)
1618

1719
class Schroedinger1D:
20+
''' This class encapsulates the 1D schroedinger equation solver '''
1821
def __init__(self, N=256, L=10):
1922
self._N = N
2023
self._L = L
@@ -24,12 +27,20 @@ def __init__(self, N=256, L=10):
2427
self._dk = 2.*pi / (N*self._dx)
2528

2629
self._x = (arange(N) - N/2) * self._dx
30+
# this is the grid in momentum space. Instead of realigning self._F after fft and before momentumEvolve, resp again
31+
# after momentumEvolve and before ifft I simply transform the grid itself (see example (6.7) in the lecture notes)
2732
k = (arange(N) - N/2)
2833
self._k = concatenate((k[self._N//2:], k[0:self._N//2])) * self._dk
29-
self._f = exp(-0.5*self._x**2 + 2.*self._x*1j)
34+
35+
# a simple wave-package moving constantly to the right and starting at x=0
36+
self.setInitial(lambda x: wavePacket(x, 2.))
37+
38+
# use the harmonic potential as default potential
3039
self._V = harmonicPotential
3140

3241
def evolve(self, dt = 0.001):
42+
''' one iteration-step '''
43+
3344
def spatialEvolve(f, x):
3445
return f*exp(-0.5 * self._V(x) * dt * 1j)
3546
def momentumEvolve(F, k):
@@ -54,8 +65,13 @@ def V(self):
5465
return self._V(self._x)
5566

5667
def setPotential(self, V):
68+
''' set the potential (as a function of an array of x-values)'''
5769
self._V = V
5870

71+
def setInitial(self, f):
72+
''' set the initial conditions (as a function of x of an array of x-values)'''
73+
self._f = f(self._x)
74+
5975
if __name__ == '__main__':
6076
from matplotlib.pyplot import plot, show
6177

0 commit comments

Comments
 (0)