标签:
源文件
1 #!/usr/bin/env python 2 3 from os import popen 4 5 class gnuplot_leon: 6 # Author : Leon Email: yangli0534@gmail.com 7 # a gnuplot api of python 8 9 def __init__(self): 10 self.gnuplot = popen(‘gnuplot‘,‘w‘) 11 self.write = self.gnuplot.write 12 self.flush = self.gnuplot.flush 13 self.close = self.gnuplot.close 14 #return gp 15 16 def set_plot_size(self,x=0.85,y=0.85): 17 self.write(‘‘.join([‘set size ‘,str(x),‘ ,‘,str(y),‘\n‘])) 18 #self.write(‘‘.join([‘set term png size ‘,str(x),‘ ‘,str(y),‘\n‘])) 19 #self.flush() 20 21 def set_canvas_size(self,x=600,y=400): 22 #self.write(‘set size 0.85, 0.85\n‘) 23 self.write(‘‘.join([‘set term png size ‘,str(x),‘ ‘,str(y),‘\n‘])) 24 25 26 def set_title(self,title=‘gnuplot‘): 27 self.write(‘‘.join([‘set title "{/Times:Italic ‘,str(title), ‘}"\n‘])) 28 self.write(‘set title font ",10" norotate tc rgb "white"\n‘) 29 30 def set_gif(self): 31 self.write(‘set terminal gif animate\n‘) 32 33 def set_png(self): 34 self.write(‘set terminal png\n‘) 35 36 def set_file_name(self,filename=‘gnuplot.gif‘): 37 self.write(‘‘.join([‘set output ‘, ‘"‘,str(filename) ,‘"‘,‘\n‘])) 38 39 def set_tics_color(self,color=‘orange‘): 40 self.write(‘‘.join([‘set tics textcolor rgb ‘,‘"‘,str(color),‘"‘,‘\n‘])) 41 42 def set_border_color(self,color=‘orange‘): 43 self.write(‘‘.join([‘set border lc rgb ‘,‘"‘,str(color),‘"‘,‘\n‘])) 44 45 def set_grid_color(self,color=‘orange‘): 46 self.write(‘‘.join([‘set grid lc rgb ‘,‘"‘,str(color),‘"‘,‘\n‘])) 47 48 def set_bkgr_color(self,color=‘orange‘): 49 self.write(‘‘.join([‘set object 1 rectangle from screen 0,0 to screen 1,1 fc rgb ‘,‘"‘,str(color),‘"‘,‘ behind\n‘])) 50 51 def set_xlabel(self,text=‘x‘,color=‘white‘): 52 self.write(‘‘.join([‘set xlabel " {/Times:Italic distance: ‘, str(text) ,‘ } " tc rgb ‘,‘"‘,str(color),‘"‘,‘ \n‘])) 53 54 def set_ylabel(self,text=‘x‘,color=‘white‘): 55 self.write(‘‘.join([‘set ylabel " {/Times:Italic distance: ‘, str(text) ,‘ } " tc rgb ‘,‘"‘,str(color),‘"‘,‘ \n‘])) 56 57 def auto_scale_enable(self): 58 self.write(‘set autoscale\n‘) 59 60 def set_key(self,onoff=‘off ‘,text=‘gnuplot‘,color=‘white‘): 61 self.write(‘unset key\n‘) 62 self.write(‘‘.join([‘set key ‘,str(onoff),‘ title "‘,str(text),‘" textcolor rgbcolor "‘,str(color),‘"\n‘])) 63 #self.write(‘show key\n‘) 64 65 def set_x_range(self,start,end): 66 self.write(‘‘.join([‘set xrange [ ‘,str(start),‘:‘,str(end),‘]\n‘])) 67 68 def set_y_range(self,start,end): 69 self.write(‘‘.join([‘set yrange [ ‘,str(start),‘:‘,str(end),‘]\n‘])) 70 71 def set_frame_start(self,linestype = ‘l‘,linewidth=3,l_color=‘green,‘): 72 #self.write(‘plot "-" w l lw 1.5 lc rgb "green"\n‘) 73 self.write(‘‘.join([‘plot "-" notitle w ‘,str(linestype),‘ lw ‘, str(linewidth), ‘ lc rgb ‘, ‘"‘, str(l_color),‘" \n‘])) 74 75 def update_point(self,x,y): 76 self.write(‘‘.join([str(x),‘ ‘,str(y),‘\n‘])) 77 78 def set_frame_end(self): 79 self.write(‘e\n‘) 80 81 def set_output_valid(self): 82 self.write(‘set output\n‘) 83 84 def close(self): 85 self.close()
例‘1
1 #!/usr/bin/env python 2 3 import sys 4 import math 5 import os 6 from gnuplot_leon import * 7 8 # Author : Leon Email: yangli0534@gmail.com 9 # fdtd simulation , plotting with gnuplot, writting in python 10 # perl and gnuplot software packages should be installed before running this program 11 # 1d fdtd with absorbing boundary and TFSF boundary between [49] and [50] 12 # lossy dielectric material localted at > ez[150] 13 14 gp = gnuplot_leon() 15 16 gp.set_plot_size(1,1) 17 gp.set_canvas_size(600,800) 18 gp.set_title(‘fdtd simulation by leon : gnuplot class test‘) 19 title = ‘fdtd simulation by leon,yangli0534\\\\@gmail.com‘ 20 #gp.write(‘set terminal gif animate\n‘) 21 gp.set_title(title) 22 #gp.set_gif() 23 gp.set_png() 24 gp.set_file_name(‘demo1.png‘) 25 gp.set_tics_color(‘white‘) 26 gp.set_border_color(‘orange‘) 27 gp.set_grid_color(‘orange‘) 28 gp.set_bkgr_color(‘gray10‘) 29 gp.set_xlabel(‘length‘,‘white‘) 30 gp.set_ylabel(‘amplitude‘,‘white‘) 31 gp.auto_scale_enable() 32 gp.set_key(‘off‘,‘sin(x)‘,‘white‘) 33 34 size = 400#physical distance 35 36 sinwave=size * [0.00]# 37 38 cnt = 0 39 elem = 0.00000 40 pi = 3.14159265358979323846 41 #gp.write(‘‘.join([‘set xrange [0:‘,str(size),‘-1]\n‘])); 42 gp.set_x_range(0,size-1) 43 #for i in range(0,size): 44 # sinwave[i] = 0.0 45 46 47 48 for mm in range(0, size-1): 49 sinwave[mm] = math.sin(2*pi*mm/size) 50 51 gp.set_frame_start(‘l‘, 3, ‘green‘) 52 cnt = 0 53 for elem in sinwave: 54 gp.update_point(cnt,elem) 55 #print ‘‘.join([str(cnt),‘:‘,str(elem),‘\n‘]) 56 cnt += 1 57 gp.set_frame_end() 58 gp.set_key(‘off‘,‘sin(x)‘,‘white‘) 59 gp.set_output_valid() 60 gp.close()
例2
1 #!/usr/bin/env python 2 3 import sys 4 import math 5 import os 6 from gnuplot_leon import * 7 8 # Author : Leon Email: yangli0534@gmail.com 9 # fdtd simulation , plotting with gnuplot, writting in python 10 # perl and gnuplot software packages should be installed before running this program 11 # 1d fdtd with absorbing boundary and TFSF boundary between [49] and [50] 12 # lossy dielectric material localted at > ez[150] 13 14 gp = gnuplot_leon() 15 16 gp.set_plot_size(0.85,0.85) 17 gp.set_canvas_size(600,400) 18 #gp.set_title(‘fdtd simulation by leon : gnuplot class test‘) 19 title = ‘fdtd simulation by leon,yangli0534\\\\@gmail.com‘ 20 21 gp.set_title(title) 22 gp.set_gif() 23 #gp.set_png() 24 gp.set_file_name(‘demo2.gif‘) 25 gp.set_tics_color(‘white‘) 26 gp.set_border_color(‘orange‘) 27 gp.set_grid_color(‘orange‘) 28 gp.set_bkgr_color(‘gray10‘) 29 gp.set_xlabel(‘length‘,‘white‘) 30 gp.set_ylabel(‘amplitude‘,‘white‘) 31 gp.auto_scale_enable() 32 gp.set_key(‘off‘,‘sin(x)‘,‘white‘) 33 34 size = 400#physical distance 35 ez=size * [0.00]#electric field 36 hy=size * [0.00]#magnetic field 37 ceze=size * [0.00]# 38 cezh=size * [0.00]# 39 chye=size * [0.00]# 40 chyh=size * [0.00]# 41 #sinwave=size * [0.00]# 42 imp0 = 377.00 43 LOSS = 0.01 44 LOSS_LAYER = 250 45 MaxTime = 18000 46 cnt = 0 47 elem = 0.00000 48 49 gp.set_x_range(0,size-1) 50 for i in range(0,size): 51 ez[i] = 0.0 52 hy[i] = 0.0 53 #sinwave[i] = 0.0 54 if (i < 100): 55 #$epsR[$i] = 1.0; 56 ceze[i] = 1.0 57 cezh[i] = imp0 58 elif(i < LOSS_LAYER): 59 #$epsR[$i] = 1.0; 60 ceze[i] = 1.0 61 cezh[i] = imp0/9.0 62 else : 63 #$epsR[$i] = 9.0; 64 ceze[i] = (1.0-LOSS)/(1.0+LOSS) 65 cezh[i] = imp0 / 9 /(1.0+LOSS) 66 if( i < LOSS_LAYER): 67 chye[i] = 1.0/imp0 68 chyh[i] = 1.0 69 else: 70 chye[i] = 1.0/imp0/(1.0+LOSS) 71 chyh[i] = (1.0-LOSS)/(1.0+LOSS) 72 for qTime in range(0, MaxTime): 73 # update magnetic field 74 for mm in range(0, size-1): 75 hy[mm] = hy[mm]*chyh[mm] + (ez[mm+1]-ez[mm])*chye[mm] 76 #sinwave[mm] = math.sin(mm/size*2*pi) 77 hy[49] = hy[49]-math.exp(-(qTime - 30.0)*(qTime - 30.0)/100.0)/imp0 78 # update electric field 79 ez[0] = ez[1]#abc 80 #$ez[$size-1] = $ez[$size-2]; 81 for mm in range(1, size-1): 82 ez[mm] = ez[mm]*ceze[mm] + (hy[mm] - hy[mm-1])*cezh[mm] 83 if(qTime % 30 == 0): 84 gp.set_frame_start(‘l‘, 3, ‘green‘) 85 cnt = 0 86 for elem in ez: 87 gp.update_point(cnt,elem) 88 cnt += 1 89 gp.set_frame_end() 90 ez[50] = ez[50]+math.exp(-(qTime +0.5-(-0.5)- 30.0)*(qTime +0.5-(-0.5)- 30.0)/100.0); 91 #gp.write(‘set output\n‘) 92 #gp.close() 93 gp.set_output_valid() 94 gp.close()
标签:
原文地址:http://www.cnblogs.com/hiramlee0534/p/5873958.html