5. Fortran code : Hàm sóng bậc n ứng
với trị x bất kỳ
DOUBLE PRECISION FUNCTION ODHON(BETA,X,N)
PARAMETER (NFMAX=100)
COMMON/CONSTS/PIO4,PIO2,PI,SQRT2,SQRTN(NFMAX),SQRTNF(NFMAX)
DOUBLE PRECISION PIO4,PIO2,PI,SQRT2,SQRTN,SQRTNF
DOUBLE PRECISION BETA,X,BETAX
DOUBLE PRECISION PHIN,PHINM1,PHINM2
DOUBLE PRECISION ODHO0,ODHO1
INTEGER I,N
C
IF (N.EQ.0) THEN
ODHON=ODHO0(BETA,X)
ELSEIF (N.EQ.1) THEN
ODHON=ODHO1(BETA,X)
ELSE
BETAX=BETA*X
PHINM2=ODHO0(BETA,X)
PHINM1=ODHO1(BETA,X)
DO I=2,N
PHIN=(PHINM1*BETAX*SQRT2-SQRTN(I-1)*PHINM2)/SQRTN(I)
PHINM2 = PHINM1
PHINM1 = PHIN
ENDDO
ODHON=PHIN
ENDIF
C
END
10. Python : tăng hiệu suất công việc
#!/usr/bin/env python
import sys,os
#using the executable file in "run" directory Ra lệnh cho hệ thống
dir='run';os.chdir(dir) thực hiện chương trình
cmd = './odho < input'
#Data preparation
#default values of input parameters
print_init_values = 0
order_wave_function = 20
beta = 1.0 Chạy chương trình với các
xmin = -10 thông số mặc định
xmax = 10
nsp = 1000
ngh = 20
#Read variables from the command line, one by one:
...
failure = os.system(cmd)
if failure:
print 'running the code failed'; sys.exit(1)
11. Python : Thay đổi tham số trực tiếp
... trên dòng lệnh
#Read variables from the command line, one by one:
while len(sys.argv)>1:
option=sys.argv[1]; del sys.argv[1]
if option == '--print-init-values':
print_init_values=int(sys.argv[1]); del sys.argv[1]
elif option == '--order-wave-function':
order_wave_function =int(sys.argv[1]); del sys.argv[1]
elif option == '--beta':
beta =float(sys.argv[1]); del sys.argv[1]
elif option == '--xmin':
xmin =float(sys.argv[1]); del sys.argv[1]
elif option == '--xmax':
xmax =float(sys.argv[1]); del sys.argv[1]
elif option == '--number-sample-points':
nsp =int(sys.argv[1]); del sys.argv[1]
elif option == '--number-gh-sample-points':
ngh =int(sys.argv[1]); del sys.argv[1]
else:
print sys.argv[0],': invalid option',option
sys.exit(1)
#Make input file to the program
...
13. Python : Tạo dữ liệu đầu vào
...
#Make input file to the program
f = open('input','w')
f.write("""%(print_init_values)g #Print initialized values 0=No 1=Yes
%(order_wave_function)g #Order of the wave-function of Harmonical Oscillator
%(beta)g #beta = sqrt(m.omega/hbar) : harmonical oscillator parameter
%(xmin)g %(xmax)g #the domain of the function
%(nsp)g #number of sample points of the function
%(ngh)g #number of Gauss-Hermite sample points
""" % vars() )
f.close()
...
0 #Print initialized values 0=No 1=Yes
2 #Order of the wave-function of Harmonical Oscillator
0.4 #beta = sqrt(m.omega/hbar) : harmonical oscillator parameter
-10 10 #the domain of the function
1000 #number of sample points of the function
20 #number of Gauss-Hermite sample points
14. Python : Hiển thị bằng hình ảnh
...
kết quả tính toán
# make file with gnuplot commands:
gnuplotfile='phi_%i__beta_%g.gnuplot' % (order_wave_function,beta)
f = open(gnuplotfile,'w')
f.write("""reset Tạo tệp chứa lệnh vẽ cho gnuplot : phương pháp truyền thống
#
set title "Harmonical Oscillator Wave Function of order %(order_wave_function)g, beta=%(beta)g"
set xlabel "x(fm)"
set ylabel "fm^{-1/2}"
#
set xrange [%(xmin)g:%(xmax)g]
# Line width of the axes
set border linewidth 1.5
# Line styles
set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 2
set style line 2 linecolor rgb '#dd181f' linetype 1 linewidth 2
# make a plot in PNG format:
set term png small Tạo hình vẽ với đuôi .png
set output 'phi_%(order_wave_function)i__beta_%(beta)g.png';
plot "data" with lines linestyle 2 notitle
# define the postscript output format: Tích hợp thông tin tham số sử dụng
set term postscript enhanced 'Times-Roman' 20; vào tên tệp
set output 'phi_%(order_wave_function)i__beta_%(beta)g.ps';
set title "Harmonical Oscillator Wave Function of order %(order_wave_function)g, {/Symbol b}=%(beta)g"
plot "data" with lines linestyle 2 notitle
""" % vars() )
f.close()
# make plot
cmd = 'gnuplot -persist '+gnuplotfile
failure = os.system(cmd)
if failure:
print 'running gnuplot failed'; sys.exit(1)
...
16. Cải tiến
●
Chuyển dữ liệu đầu vào và đầu ra của một
tính '../odho vào cùng một thư mục
cmd =
toán < input'
...
caldir='tmp'
...
elif option == '--calcul-directory':
caldir =sys.argv[1]; del sys.argv[1]
...
import shutil
if os.path.isdir(caldir): #does caldir exist
shutil.rmtree(caldir) # yes, remove old directory
os.mkdir(caldir) #make caldir directory
os.chdir(caldir) #move to dir
...
17. Tính toàn bộ các trạng thái kích
thích từ 0 tới 100
#!/usr/bin/env python
import sys,os
for n in range(0,100):
cmd = "./odho_improved.py --beta 1.5 --order-wave-function %g
--number-sample-points 1000 --calcul-directory beta1.5n%g" %(n,n)
failure = os.system(cmd)
if failure:
print 'running gnuplot failed'; sys.exit(1)