问题描述
我正在尝试使用跳蛙方案编写 n 体模拟来模拟球状星团,但是我遇到了粒子从(我认为)接近其他粒子而被抛出系统的问题这导致势能的大幅增加。我曾尝试编写一些代码来解释碰撞,但如果我也使用软化因子,我不确定要使用什么碰撞半径。
初始位置在球体内随机生成,初始速度均为 0。
如果软化因子设置为 0.1 * 生成球体半径,碰撞半径设置为太阳半径(导致没有碰撞)与 N 个太阳质量的物体,我得到以下结果:
从能量图中,我们可以看到当物体最初彼此靠近时,势能会出现巨大的峰值。随着时间的推移,我的能量逐渐减少到 0,它认为只是由于数值计算。
有没有办法阻止这些星星被扔出去。我正在尝试调查初始集群需要多长时间才能形成平衡状态。
球体生成:
sun_mass = 1.989e30
N = 100
mass = sun_mass
M = np.full([N],mass)
R = 1e13
epsilon = 0.1 * R
collision_radius = 7e8
V = np.zeros([N,3])
M = np.full([N],mass)
P = np.zeros([N,3])
t = 50 * 365 * 24 * 60 * 60
dt = 30 * 24 * 60 * 60
print(t/dt)
np.random.seed(54321)
for i in range(N):
phi = np.random.uniform(0,2*np.pi)
costheta = np.random.uniform(-1,1)
u = np.random.uniform(0,1)
theta = np.arccos( costheta )
r = R * (u) **(1/3)
x = r * np.sin( theta) * np.cos( phi )
y = r * np.sin( theta) * np.sin( phi )
z = r * np.cos( theta )
P[i] = (x,y,z)
程序:
def programe(position,mass,velocity,softening,time,dt,R,collision_radius):
no_of_time_steps = (round(time/dt))
all_positions = []
all_velocities = []
#print(all_positions)
#print(len(all_positions[0]))
kinetic_energy = []
potential_energy = []
total_energy = []
for i in range(no_of_time_steps):
position,velocity = detect_collisions(position,collision_radius)
all_positions.append(position)
all_velocities.append(velocity)
'graph'
plots = np.round(np.linspace(0,no_of_time_steps,num=500))
for k in range(len(plots)):
if i == plots[k]:
print("test")
#print(i)
graph(position,k)
'energies'
kinetic_energy.append(calc_kinetic_energy(velocity,mass))
potential_energy.append(calc_potential_energy(position,mass))
total_energy.append(calc_total_energy(position,mass))
'leap frog'
velocity = calc_next_v_half(position,dt)
position = calc_next_position(position,dt)
velocity = calc_next_v_half(position,dt)
all_positions = np.array(all_positions)
graphing(all_positions,kinetic_energy,potential_energy,total_energy,R)
#print(len(mass))
return(all_positions,all_velocities,total_energy)
碰撞检测:
def indexOf(Array,item):
for i in range(len(Array)):
if (Array[i] == item).all():
return i
def detect_collisions(position,collision_radius):
i = 0
newP = position
newM = mass
newV = velocity
#print(len(position),len(newM))
while i < len(newM):
if newM[i] == 0:
i += 1
continue
j = i + 1
while j < len(newP):
#print(j)
if newM[j] == 0:
j += 1
continue
p1 = position[i]
p2 = position[j]
if calc_seperation(p1,p2) < collision_radius:
index1 = indexOf(position,p1)
index2 = indexOf(position,p2)
print('collision',index1,index2)
newM,newV,newP = handle_collision(newM,newP,[index1,index2])
j += 1
i += 1
return(newP,newM,newV)
def handle_collision(M,V,P,indexes):
if M[indexes[0]] > M[indexes[1]]:
primary = indexes[0]
secondary = indexes[1]
else:
primary = indexes[1]
secondary = indexes[0]
primaryMomentum = M[primary] * V[primary]
secondaryMomentum = M[secondary] * V[secondary]
newMomentum = primaryMomentum + secondaryMomentum
newMass = M[primary] + M[secondary]
newVelocity = newMomentum / newMass
M[primary] = newMass
V[primary] = newVelocity
M[secondary] = 0
V[secondary] = 0
P[secondary] = 0
newM = np.delete(M,secondary,axis=0)
newV = np.delete(V,axis=0)
newP = np.delete(P,axis=0)
return (newM,newP)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)