forked from feipai11/computationalphysics_N2013301020066
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolveNS.py
executable file
·93 lines (71 loc) · 2.52 KB
/
SolveNS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
##!/public/usrs/liuhj6/anaconda/bin/python
import numpy as np
import time
class SolveNS:
def Init(self, nnodes, x_min, x_max, nlevel):
self.nnodes = nnodes
self.x_min = x_min
self.x_max = x_max
self.step = float(x_max - x_min) / nnodes
#Calculate all the nodes
self.x = np.zeros(nnodes)
self.x[0] = x_min
for ix in range(1,self.nnodes):
self.x[ix] = self.x[ix-1] + self.step
#Initialize the Hamiltonian
self.Ham = np.zeros([nnodes, nnodes])
#Set the first n level which needs to be calculated
self.nlevel = nlevel
self.CalHam()
self.SolveEnAndWave()
def Poten(self,x):
##Calculate the Potential
#############################################
# one dimentional infinite potential
#if( x==self.x_min or x==self.x_max ):
# return 10000
#else:
# return 0.0
#############################################
#############################################
# one dimentional harmonic potential
return 0.5*x**2
#############################################
def CalHam(self):
#Calculate the Hamiltonian matrix
self.Ham[0,0] = 1.0/self.step**2 + self.Poten(self.x[0])
self.Ham[0,1] = -0.5*self.step**2
self.Ham[self.nnodes-1,self.nnodes-2] = -0.5*self.step**2
self.Ham[self.nnodes-1,self.nnodes-1] = 1.0/self.step**2 + self.Poten(self.x[-1])
for inodes in range(1,self.nnodes-2):
for jnodes in range(3):
self.Ham[inodes, inodes] = 1.0/self.step**2 + self.Poten(self.x[inodes])
self.Ham[inodes, inodes-1] = -0.5 / self.step**2
self.Ham[inodes, inodes+1] = -0.5 / self.step**2
def SolveEnAndWave(self):
# Solve the eigenvalues and wavefunctions
self.eig, self.wave = np.linalg.eig(self.Ham)
# Store the wavefunction to output
wave = np.zeros([self.nnodes,self.nlevel+1])
# save the original eigvalues: keep the original order to find the correct wavefunction
eig = self.eig
# Convert to list to use the function eig.index(eig) to locate the index
eig = eig.tolist()
# Sort the eigenvalues
self.eig.sort()
print " The first %d eigenvalues :" % self.nlevel
nlevel = min(self.nlevel, self.nnodes)
for ilevel in range(nlevel):
print " %8.4f " % self.eig[ilevel] ,
print
# build the eig list
wave[:,0] = self.x
for ilevel in range(1,nlevel+1):
index = eig.index(self.eig[ilevel])
wave[:,ilevel:ilevel+1]=self.wave[:,index:index+1]
np.savetxt('wavefun.dat',wave,fmt='%12.6f',delimiter='')
start = time.clock()
mySolve = SolveNS()
mySolve.Init(2000, -10, 10, 10)
finish = time.clock()
print " Elapsed time %12.6f s" % (finish - start)