假设有一个已知中心(Xo,Yo,Zo)和半径Ro的大球体。它包含数以百万计的粒子,每个粒子具有已知的质量和相对于同一参照系的三维位置。在这个大球体中,有十几个较小的假想球体随机分布在3d空间中,但其他已知的位置。我想计算每个较小的球体内的粒子数目,因此,通过计算每个小球体(容器)内的粒子,计算出每个球(容器)的质量。
这是我的MWE片段,我在单个粒子上循环,看看它们是否在每个较小的球体中,然后对每个球体分别进行计数,从而得出每个较小球体的总质量:
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个小球体),他们的身份证,首先是他们的总质量。但是,这是我的输出:
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个粒子。(提示:所有粒子都有质量。)
发布于 2017-04-23 22:04:29
我刚刚(通过我的一个朋友)了解到,我把python和C++之间的语法混合在一起,因为与C++不同的是,在C++中,变量的赋值不能从内循环取到外部循环,python实际上能够通过循环之间的缩进来记住指定的值。在python中(与C++不同),一旦执行了一个循环并到达了它的末尾,然后通过一个缩进,python仍然可以回忆它正在计算的内容,因此可以访问该循环的最终产品。因此,我应该使用while循环,而不是使用if循环,以确保所有的粒子都已经根据条件进行了检查。另外,变量small_sphere_mass和small_sphere_mass_array的初始化应该在内部循环之外进行,这样我就不会强迫循环始终保持第一个零值。
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))https://stackoverflow.com/questions/42463767
复制相似问题