1
+ import os
2
+ import im2gif
3
+ import cmath as cm
4
+ import numpy as np
5
+ from io import BytesIO
6
+ from PIL import Image as im
7
+ from matplotlib import pyplot as plt
8
+
9
+ def font (family = 'serif' , color = 'black' , weight = 'normal' , size = '16' ):
10
+ return {'family' : family , 'color' : color , 'weight' : weight , 'size' : size }
11
+
12
+ # relationship with ratio
13
+ # ratio = np.linspace(0,5,1e4)
14
+ # ratio[ratio==1]=1+1e-10
15
+ # k0 = 1e-10
16
+ # k1 = np.array([k0*cm.sqrt(r) for r in ratio])
17
+ # k2 = np.array([k0*cm.sqrt(r-1) for r in ratio])
18
+ # D = 4*np.square(k1)*np.square(k2)/(np.square(np.square(k1)-np.square(k2))*np.square(np.sin(k2))+4*np.square(k1)*np.square(k2))
19
+ # R = 1-D
20
+ # flg = plt.figure(figsize=(10,45/8))
21
+ # plt.xlabel('$E/U_0$', fontdict=font())
22
+ # plt.ylabel('$Intensity$', fontdict=font())
23
+ # plt.title('Relationship With $E/U_0$')
24
+ # plt.hold(True)
25
+ # hd, = plt.plot(ratio, np.real(D), color='blue', label='transmission coefficient')
26
+ # hr, = plt.plot(ratio, np.real(R), color='red', label='reflection coefficient')
27
+ # plt.legend(fontsize=14, frameon=True, fancybox=True, framealpha=0.3)
28
+ # plt.hold(False)
29
+ # ht= plt.text(2.1,.5,'$k_0=%.2f$' % k0, fontdict=font(size=20))
30
+ # # plt.show()
31
+ # gif_root = './gif'
32
+ # # check gif root dir
33
+ # if not os.path.isdir(gif_root):
34
+ # os.makedirs(gif_root)
35
+ # FN = 50
36
+ # images = []
37
+ # for k0 in np.linspace(0,50,FN):
38
+ # k1 = np.array([k0*cm.sqrt(r) for r in ratio])
39
+ # k2 = np.array([k0*cm.sqrt(r-1) for r in ratio])
40
+
41
+ # D = 4*np.square(k1)*np.square(k2)/(np.square(np.square(k1)-np.square(k2))*np.square(np.sin(k2))+4*np.square(k1)*np.square(k2))
42
+ # R = 1-D
43
+
44
+ # hd.set_ydata(np.real(D))
45
+ # hr.set_ydata(np.real(R))
46
+ # ht.set_text('$k_0=%.2f$' % k0)
47
+ # buf = BytesIO()
48
+ # plt.savefig(buf, format='png')
49
+ # images.append(im.open(buf))
50
+ # gif_path = os.path.join(gif_root, 'analyse_r2e.gif' % ratio)
51
+ # im2gif.writeGif(filename=gif_path, images=images, duration=2 / FN)
52
+ # print('gif file created at path: %s.' % os.path.abspath(gif_path))
53
+
54
+ # relationship with k0
55
+ ratio = 0.01
56
+ ratio = 0.01 if ratio < 0.01 else ratio if ratio != 1 else ratio + 1e-10
57
+ k0 = np .linspace (0 ,20 ,1e3 )
58
+
59
+ k1 = k0 * cm .sqrt (ratio )
60
+ k2 = k0 * cm .sqrt (ratio - 1 )
61
+
62
+ D = 4 * np .square (k1 )* np .square (k2 )/ (np .square (np .square (k1 )- np .square (k2 ))* np .square (np .sin (k2 ))+ 4 * np .square (k1 )* np .square (k2 ))
63
+ R = 1 - D
64
+
65
+ flg = plt .figure (figsize = (10 ,45 / 8 ))
66
+ plt .xlabel ('$k_0$' , fontdict = font ())
67
+ plt .ylabel ('$Intensity$' , fontdict = font ())
68
+ plt .title ('Relationship With $K0$' )
69
+ plt .hold (True )
70
+ hd , = plt .plot (k0 , np .real (D ), color = 'blue' , label = 'transmission coefficient' )
71
+ hr , = plt .plot (k0 , np .real (R ), color = 'red' , label = 'reflection coefficient' )
72
+ plt .legend (fontsize = 14 , frameon = True , fancybox = True , framealpha = 0.3 )
73
+ plt .hold (False )
74
+ ht = plt .text (8 ,.5 ,'$E/U_0=%.2f$' % ratio , fontdict = font (size = 20 ))
75
+ # plt.show()
76
+ gif_root = './gif'
77
+ # check gif root dir
78
+ if not os .path .isdir (gif_root ):
79
+ os .makedirs (gif_root )
80
+ FN = 40
81
+ images = []
82
+ for ratio in np .linspace (0 ,5 ,FN ):
83
+ k1 = k0 * cm .sqrt (ratio )
84
+ k2 = k0 * cm .sqrt (ratio - 1 )
85
+
86
+ D = 4 * np .square (k1 )* np .square (k2 )/ (np .square (np .square (k1 )- np .square (k2 ))* np .square (np .sin (k2 ))+ 4 * np .square (k1 )* np .square (k2 ))
87
+ R = 1 - D
88
+
89
+ hd .set_ydata (np .real (D ))
90
+ hr .set_ydata (np .real (R ))
91
+ ht .set_text ('$E/U_0=%.2f$' % ratio )
92
+ buf = BytesIO ()
93
+ plt .savefig (buf , format = 'png' )
94
+ images .append (im .open (buf ))
95
+ gif_path = os .path .join (gif_root , 'analyse_e2r.gif' % ratio )
96
+ im2gif .writeGif (filename = gif_path , images = images , duration = 2 / FN )
97
+ print ('gif file created at path: %s.' % os .path .abspath (gif_path ))
0 commit comments