我有这段代码可以接收我的数组x1,y1,z1,vx1,vy1,vz1并对其进行操作(这是大部分代码),最后我剩下了新的数组x2 ,y2,z2,vx2,vy2,vz2。我想要做的是整个代码,用x2更新x1,用y2更新y1,依此类推。 但是,当我在代码末尾设置x1 = x2 ...时,出现以下错误消息:
Traceback (most recent call last):
File "myfile.py", line 33, in <module>
File "myfile.py", line 30, in do_work
M[xn,step] = ((mass1[xn]*mass1[step+1]*((x1[step+1]**2.+y1[step+1]**2.+z1[step+1]**2.)-(x1[xn]**2.+y1[xn]**2.+z1[xn]**2.)))/ (abs((x1[step+1]**2.+y1[step+1]**2.+z1[step+1]**2.)-(x1[xn]**2.+y1[xn]**2.+z1[xn]**2.))**2.+(.2)**2 )**(3))
IndexError: index 999 is out of bounds for axis 0 with size 999
,我不知道为什么。我不明白为什么我的代码不能用于我的新数组x2,y2,z2,...等(我知道我的函数有点混乱,但我担心问题可能出在那儿,所以这就是我按原样发布的原因)
import matplotlib.pyplot as plt
import numpy as np
import time
import itertools
start_time = time.time()
G=1
dt=.01
n1 = np.loadtxt('homo_sph_N1000_R3_v1.dat',usecols=(0),skiprows=0)
mass1= np.loadtxt('homo_sph_N1000_R3_v1.dat',usecols=(1),skiprows=0)
x1=np.loadtxt('homo_sph_N1000_R3_v1.dat',usecols=(2),skiprows=0)
y1=np.loadtxt('homo_sph_N1000_R3_v1.dat',usecols=(3),skiprows=0)
z1=np.loadtxt('homo_sph_N1000_R3_v1.dat',usecols=(4),skiprows=0)
vx1=np.loadtxt('homo_sph_N1000_R3_v1.dat',usecols=(5),skiprows=0)
vy1=np.loadtxt('homo_sph_N1000_R3_v1.dat',usecols=(6),skiprows=0)
vz1=np.loadtxt('homo_sph_N1000_R3_v1.dat',usecols=(7),skiprows=0)
npoints=len(n1)-1
M = np.zeros((npoints,npoints))
for timestep in xrange(0,2):
def do_work(xn, step):
#This is where I begin operating on intial arrays
M[xn,step] = ((mass1[xn]*mass1[step+1]*((x1[step+1]**2.+y1[step+1]**2.+z1[step+1]**2.)-(x1[xn]**2.+y1[xn]**2.+z1[xn]**2.)))/ (abs((x1[step+1]**2.+y1[step+1]**2.+z1[step+1]**2.)-(x1[xn]**2.+y1[xn]**2.+z1[xn]**2.))**2.+(.2)**2 )**(3))
[do_work(xn, step) for (xn,step) in itertools.product(xrange(0,npoints), xrange(0,npoints))]
a=[np.sum(arr) for arr in M]
a = np.array(a)
vxx = np.array(vx1)
vyy=np.array(vy1)
vzz=np.array(vz1)
vx=vxx[0:npoints]
vy=vyy[0:npoints]
vz=vzz[0:npoints]
vx2 = vx + (a +a)/2 * dt
vy2 = vy + (a +a)/2 * dt
vz2 = vz + (a+a)/2 * dt
xx = np.array(x1)
yy = np.array(y1)
zz = np.array(z1)
x=xx[0:npoints]
y=yy[0:npoints]
z=zz[0:npoints]
#x2,y2,z2.... are new arrays
x2= np.array((x+vx2*dt) + (a*dt**2)/2)
y2= np.array((y+vy2*dt) + (a*dt**2)/2)
z2= np.array((z+vz2*dt) + (a*dt**2)/2)
#I set x1....=x2... so this whole thing will loop using the new array values
x1=x2
y1=y2
z1=z2
答案 0 :(得分:0)
从n点中减去1
[do_work(xn, step) for (xn,step) in itertools.product(xrange(0,npoints-1), xrange(0,npoints-1))]