Sfd2012Hanoi - Hà Thụy Long - Odho: Mã nguồn mở và các bài toán vật lý

19
[email protected] Minh h a vi c k t h p các ế công c mã ngu n m tìm ở để hi u m t bài toán v t lý Hà N i, 15/09/2012

Transcript of Sfd2012Hanoi - Hà Thụy Long - Odho: Mã nguồn mở và các bài toán vật lý

[email protected]

Minh h a vi c k t h p các ọ ệ ế ợcông c mã ngu n m tìm ụ ồ ở để

hi u m t bài toán v t lýể ộ ậ

Hà N i, 15/09/2012ộ

Dao Động Tử Điều Hòa Một Chiều

… và trong Cơ học lượng tử

ω=√ km H=P2

2m+

12mω

2 X 2

Trạng thái kích thích tổng quát

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,NC 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 ENDIFC END

Tạo tệp thực thi với Makefile

1

Makefileobjects = objs/initialization.o objs/debug.o objs/quadrature.o objs/odho.o objs/main.o forcomp = gfortran #forcomp = fort77

run/odho: $(objects)time $(forcomp) -o run/odho $(objects)

objs/initialization.o: initialization.ftime $(forcomp) -c -O4 -o objs/initialization.o initialization.f

objs/debug.o: debug.ftime $(forcomp) -c -O4 -o objs/debug.o debug.f

objs/quadrature.o: quadrature.ftime $(forcomp) -c -O4 -o objs/quadrature.o quadrature.f

objs/odho.o: odho.ftime $(forcomp) -c -O4 -o objs/odho.o odho.f

objs/main.o: main.ftime $(forcomp) -c -O4 -o objs/main.o main.f

.PHONY: clean runclean:

rm -rf run/odho $(objects) *~

run:cd run;./odho < input

Chương trình chính, dữ liệu đầu vào và kết quả

C ... OPEN(UNIT=20,FILE='data')C READ *,BETA READ *,XMIN,XMAX READ *,NSP READ *,NGH WRITE(21,100) BETA,NGH CALL ROOTHE(HERRTS,NGH) WRITE(21,105) (I,HERRTS(I),I=1,NGH) DX=(XMAX-XMIN)/DBLE(NSP) X=XMIN DO I=0,NSP WRITE (20,*) X,ODHON(BETA,X,N) X=X+DX ENDDOC ...

0 #Print initialized values 0=No 1=Yes20 #Order of the wave-function of Harmonical Oscillator1 #beta = sqrt(m.omega/hbar) : harmonical oscillator parameter-10 10 #the domain of the function1000 #number of sample points of the function20 #number of Gauss-Hermite sample points

-10.000000000000000 3.31402378637182642E-009 -9.9800000000000004 3.86975188178886400E-009 -9.9600000000000009 4.51634903048100019E-009 -9.9400000000000013 5.26827539296001357E-009 -9.9200000000000017 6.14222537578560566E-009 -9.9000000000000021 7.15746188556575456E-009 -9.8800000000000026 8.33619883446549584E-009 -9.8600000000000030 9.70403858885294332E-009 -9.8400000000000034 1.12904719391459490E-008 -9.8200000000000038 1.31294491666361272E-008 -9.8000000000000043 1.52600319051601025E-008 -9.7800000000000047 1.77271367564591956E-008 -9.7600000000000051 2.05823830339850479E-008 -9.7400000000000055 2.38850585985262592E-008 -9.7200000000000060 2.77032195299829601E-008 -9.7000000000000064 3.21149413745403316E-008 -9.6800000000000068 3.72097419392771055E-008 -9.6600000000000072 4.30901981031658081E-008 ...

Gnuplot để hiển thị kết quả

Python : tăng hiệu suất công việc#!/usr/bin/env pythonimport sys,os#using the executable file in "run" directorydir='run';os.chdir(dir)cmd = './odho < input'#Data preparation#default values of input parametersprint_init_values = 0order_wave_function = 20beta = 1.0xmin = -10xmax = 10nsp = 1000ngh = 20#Read variables from the command line, one by one:...failure = os.system(cmd)if failure: print 'running the code failed'; sys.exit(1)

Ra lệnh cho hệ thống thực hiện chương trình

Chạy chương trình với cácthông số mặc định

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...

Vai trò của tham số beta

Python : Tạo dữ liệu đầu vào ...#Make input file to the programf = 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=Yes2 #Order of the wave-function of Harmonical Oscillator0.4 #beta = sqrt(m.omega/hbar) : harmonical oscillator parameter-10 10 #the domain of the function1000 #number of sample points of the function20 #number of Gauss-Hermite sample points

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#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 axesset border linewidth 1.5# Line stylesset style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 2set style line 2 linecolor rgb '#dd181f' linetype 1 linewidth 2# make a plot in PNG format:set term png smallset output 'phi_%(order_wave_function)i__beta_%(beta)g.png';plot "data" with lines linestyle 2 notitle# define the postscript output format:set term postscript enhanced 'Times-Roman' 20;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 plotcmd = 'gnuplot -persist '+gnuplotfilefailure = os.system(cmd)if failure: print 'running gnuplot failed'; sys.exit(1)...

Tạo tệp chứa lệnh vẽ cho gnuplot : phương pháp truyền thống

Tạo hình vẽ với đuôi .png

Tích hợp thông tin tham số sử dụng vào tên tệp

Trạng thái kích thích bậc 100

Cải tiến

● Chuyển dữ liệu đầu vào và đầu ra của một tính toán vào cùng một thư mụccmd = '../odho < input'...caldir='tmp'... elif option == '--calcul-directory': caldir =sys.argv[1]; del sys.argv[1]...import shutilif os.path.isdir(caldir): #does caldir exist shutil.rmtree(caldir) # yes, remove old directory os.mkdir(caldir) #make caldir directoryos.chdir(caldir) #move to dir...

Tính toàn bộ các trạng thái kích thích từ 0 tới 100

#!/usr/bin/env pythonimport 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)

Còn gì nữa

Thank you for your attention!!!