让我们假设有一个有着已知中心(Xo,Yo,Zo)和半径Ro的大球体。它包含数百万个粒子,每个粒子具有相同参考帧的已知质量和3d位置。在这个大球体内部,有十几个较小的假想球体随机分布在3d空间中,但其他已知的位置。我想计算每个较小球体内的粒子数量,从而计算出每个较小球体(容器)的最终质量,方法是计算每个球体内部的粒子。
这是我的MWE片段,其中我循环遍历各个粒子,看它们是否在每个较小的球体内,然后为每个球体单独计算它们,以便为每个较小的球体提供总质量: / p>
import numpy as np
from scipy.spatial.distance import euclidean
### there is 10 small spheres inside Big Sphere each containing different number of particles and hence different masses
### small_sphere_id_array is the array of the IDs of the small spheres
### small_sphere_position_array is the array of the position of the center of small spheres
### small_sphere_radius_array is the array of the radii of small spheres
### small_sphere_mass_array is the array of the masses of small spheres
### particle_position_array is the array of the positions of particles inside the Big Sphere
### particle_mass_array is the array of the masses of particles inside the Big Sphere
for small_sphere_index in np.arange(0, 10)):
for particle_index in np.arange(0, 6000000)):
small_sphere_mass_array = []
small_sphere_mass = 0
distances = euclidean(particle_position_array[particle_index], small_sphere_position_array[small_sphere_index])
success_condition = (distances <= small_shpere_radius_array[small_sphere_index])
while success_condition:
small_sphere_mass += particle_mass_array[particle_index]
small_sphere_mass_array.append(small_sphere_mass)
small_sphere_mass = np.sum(small_sphere_mass_array)
else:
break
print('{}, {}'.format(small_sphere_id_array[small_sphere_index], small_sphere_mass))
我希望打印出10行(对应10个小球),首先是ID,然后是总质量。但是,这是我的输出:
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
some number, 0
特别是我无法获取while
循环的结果并将其移动到外部for
循环,因此循环遍历所有粒子,为每个小球体提供单个非零总质量。我似乎正确地循环了10个小球体但问题是质量最终为零,这意味着中间for
环不能解释所有6000000个粒子。 (提示:所有粒子做都有质量。)
答案 0 :(得分:0)
我刚刚(通过我的一个朋友)了解到我在混合python和C ++之间的语法,这与C ++不同,C ++中变量的赋值不能从内循环到外循环,python是实际上只能通过循环之间的缩进来记住赋值。在python中(与C ++不同)一旦执行循环并且它的结束到达,那么通过一次缩进,python仍然可以回忆它正在计算的内容,因此可以访问循环的最终产品。因此,我应该使用while
循环来确保已根据条件检查了所有粒子,而不是使用if
循环。此外,变量 small_sphere_mass 和 small_sphere_mass_array 的初始化应该在内部循环之外进行,这样我就不会强制循环始终坚持第一个零值。 / p>
import numpy as np
from scipy.spatial.distance import euclidean
### there is 10 small spheres inside Big Sphere each containing different number of particles and hence different masses
### small_sphere_id_array is the array of the IDs of the small spheres
### small_sphere_position_array is the array of the position of the center of small spheres
### small_sphere_radius_array is the array of the radii of small spheres
### small_sphere_mass_array is the array of the masses of small spheres
### particle_position_array is the array of the positions of particles inside the Big Sphere
### particle_mass_array is the array of the masses of particles inside the Big Sphere
for small_sphere_index in np.arange(0, 10)):
small_sphere_mass_array = []
small_sphere_mass = 0
for particle_index in np.arange(0, 6000000)):
distances = euclidean(particle_position_array[particle_index], small_sphere_position_array[small_sphere_index])
if (distances <= small_shpere_radius_array[small_sphere_index]):
part_particle=particle_mass_array[particle_index]
small_sphere_mass += part_particle
small_sphere_mass_array.append(part_particle)
print('{}, {}'.format(small_sphere_id_array[small_sphere_index], small_sphere_mass))