用Python实现三维阵列中速度分量的转换

2024-09-27 00:18:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在有一个n体系统中10000个时间点的速度列表。它是一个三维阵列,涉及三维空间中时间点的n粒子。例如,如果两个时间点上有三个粒子,则将其设置为

[[[vx1][vy1][vz1]
  [vx2][vy2][vz2]
  [vx3][vy3][vz3]]
 [[vx1][vy1][vz1]
  [vx2][vy2][vz2]
  [vx3][vy3][vz3]]]

我的目标是这样的:

^{pr2}$

但是我不能求出速度分量的平方相加,而粒子的数量是自由变化的。我可以用这样的两个粒子来做:

# Takes all velocities and converts them to speeds
all_speeds=[]
for i in range(len(all_velocities)):
    all_speeds.append([math.sqrt(all_velocities[i][0][0]**2\
        +all_velocities[i][0][1]**2+all_velocities[i][0][2]**2)],\
            [math.sqrt(all_velocities[i][1][0]**2+all_velocities[i][1][1]**2\
                +all_velocities[i][1][2]**2)])

但我不确定如何将其扩展到n粒子。我的最终目标是把速度数组平方,然后乘以质量数组,我要计算系统的动能,但是我不能把它推广到任何输入下。谢谢。在


Tags: 系统时间粒子all速度speedsvelocitiesvx1
3条回答

你只需要在主循环中的粒子数上增加一个循环。在

为了便于阅读,我还在下面的代码片段中创建了一个函数来进行速度计算:

import math

all_velocities = [[[1,2,3], [2,3,4]], [[3,4,5], [4,5,6], [7,8,9]]]
all_speeds = []

def calculate_speed(v):
    return math.sqrt(v[0]**2 + v[1]**2 + v[2]**2)

for i in range(len(all_velocities)):
    all_speeds.append([])
    for j in range(len(all_velocities[i])):
        velocity = all_velocities[i][j]
        all_speeds[i].append(calculate_speed(velocity))

print all_speeds

再添加一个循环

# Takes all velocities and converts them to speeds
all_speeds=[]
# Looping over time
for i in range(len(all_velocities)):
    #adding and empty list to collect all the speeds
    all_speeds.append([])
    #looping over number of particles
    for j in range(len(all_velocities[i])):
        all_speeds[-1].append([math.sqrt(all_velocities[i][j][0]**2\
                                         +all_velocities[i][j][1]**2\
                                         +all_velocities[i][j][2]**2)])

希望这有帮助。在

import numpy as np
v1 = [[[1,1,1],
       [2,2,2],
       [3,3,3],
       [4,4,4]],
      [[1,1,1],
       [2,2,2],
       [3,3,3],
       [4,4,4]]]

a = np.array(v1)

a.shape是(t,n,d)。你需要三维速度平方和的平方根:

^{pr2}$

b.shape是(t,n)。在我的数据集中似乎没有粒子在加速。在

或者简单地说:

>>> np.linalg.norm(a, axis = 2)
array([[ 1.73205081,  3.46410162,  5.19615242,  6.92820323],
       [ 1.73205081,  3.46410162,  5.19615242,  6.92820323]])
>>> 

相关问题 更多 >

    热门问题