import matplotlib.pyplot as plt
import numpy as np
class billiard_circle():
def __init__(self,x_0,y_0,vx_0,vy_0,N,dt):
self.x_0 = x_0
self.y_0 = y_0
self.vx_0 = vx_0
self.vy_0 = vy_0
self.N = N
self.dt = dt
def motion_calculate(self):
self.x = []
self.y = []
self.vx = []
self.vy = []
self.t = [0]
self.x.append(self.x_0)
self.y.append(self.y_0)
self.vx.append(self.vx_0)
self.vy.append(self.vy_0)
for i in range(1,self.N):
self.x.append(self.x[i - 1] + self.vx[i - 1]*self.dt)
self.y.append(self.y[i - 1] + self.vy[i - 1]*self.dt)
self.vx.append(self.vx[i - 1])
self.vy.append(self.vy[i - 1])
if (np.sqrt( self.x[i]**2+self.y[i]**2 ) > 1.0):
self.x[i],self.y[i] = self.correct('np.sqrt(x**2+(y)**2) < 1.0',self.x[i - 1], self.y[i - 1], self.vx[i - 1], self.vy[i - 1])
self.vx[i],self.vy[i] = self.reflect(self.x[i],self.y[i],self.vx[i - 1], self.vy[i - 1])
self.t.append(self.t[i - 1] + self.dt)
return self.x, self.y
def correct(self,condition,x,y,vx,vy):
vx_c = vx/100.0
vy_c = vy/100.0
while eval(condition):
x = x + vx_c*self.dt
y = y + vy_c*self.dt
return x-vx_c*self.dt,y-vy_c*self.dt
def reflect(self,x,y,vx,vy):
module = np.sqrt(x**2+y**2) ### normalization
x = x/module
y = y/module
v = np.sqrt(vx**2+vy**2)
cos1 = (vx*x+vy*y)/v
cos2 = (vx*y-vy*x)/v
vt = -v*cos1
vc = v*cos2
vx_n = vt*x+vc*y
vy_n = vt*y-vc*x
return vx_n,vy_n
def phase_plot(self):
plt.figure(figsize = (8,8))
record_x = []
record_vx = []
for i in range(len(self.x)):
if (abs(self.y[i] - 0)<0.001):
record_vx.append(self.vx[i])
record_x.append(self.x[i])
plt.xlabel('x')
plt.ylabel(r'$v_x$')
plt.plot(record_x,record_vx,'r.')
#plt.savefig('chapter3_3.31_phasey=0.png', dpi= 144)
plt.show()
A=billiard_circle(0.2,0,1,0.6,150000,0.01)
A.motion_calculate()
A.phase_plot()
无标题文章
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...