求解多个变量的方程式以创建网格 python3

Solving an equation for several variables to create grid python3

在创建 4 组均匀间隔的数字(通过 linspace)并加载一组坐标和到达时间(与方程相关,有 20 个不同的坐标组)之后,我创建了四个循环以获得每组变量的均方根值(应该有 24255 个不同的集合),但是到目前为止我得到的只是 20 个均方根值。

我如何在网格中获得所有必要的值,同时显示相应的 x、y、z、t0 值?

这是我使用的代码:

import numpy as np

#opening  files 

s_Coord = np.genfromtxt(fname = 's_Coord_Pset3.txt')

t_ob = np.genfromtxt(fname ='t_ob_Pset3File.txt')

#creating parameters

nt0 = 5
t0_vec = np.linspace(0,4,nt0)

nx = 21
x_vec = np.linspace(-20,20,nx)

ny = 21
y_vec = np.linspace(-20,20,ny)

nz = 11
z_vec = np.linspace(0,20,nz)

v = 6 #km

#calculating ti_p and rms
for x in x_vec:
    for y in y_vec:
        for z in z_vec:
            for t in t0_vec:
                rms_mat = np.zeros((nx,ny,nz,nt0))
                #the rms_mat zeros matrix is possibly for creating the grid to fill?
                g_range = np.arange(0,20,1) #for 20 stations from the file
                x1 = [s_Coord[g:g+1,0] for g in g_range]
                y1 = [s_Coord[g:g+1,1] for g in g_range]
                z1 = [s_Coord[g:g+1,2] for g in g_range]
                t0 = [t_ob[g:g+1] for g in g_range]
                ti_p = t+(((x1-x)**2+(y1-y)**2+(z1-z)**2)**0.5)/v
                rms = ((((t0-ti_p)**2)/(nx*ny*nz*nt0))**0.5)

如有任何帮助,我们将不胜感激

至于 s_Coord 文件:这是样本数据:

   5.5096956e+00  -1.0756354e+01   0.0000000e+00
  -2.4259195e+00   1.4407220e+01   0.0000000e+00
   3.6707655e+00   5.3756022e+00   0.0000000e+00
   4.7193844e+00   9.2582846e+00   0.0000000e+00
  -1.8361331e+00  -1.3745789e+01   0.0000000e+00
     . . ........................................

至于 t_ob 文件:这是样本数据:

   6.0261696e+00
   5.1844492e+00
   4.7735687e+00
   5.1549808e+00
   6.0337739e+00

我尝试以这种方式附加值,但是代码变得太重了 运行 时间太长而没有完成 运行ning 或只是崩溃(我相信如何处理有问题写成

ti_p = []
rms_val = []
grid = []
for x in x_vec:
    for y in y_vec:
        for z in z_vec:
            for t in t0_vec:
                g_range = np.arange(0,20,1) #for 20 stations from the file
                x1 = [s_Coord[g:g+1,0] for g in g_range]
                y1 = [s_Coord[g:g+1,1] for g in g_range]
                z1 = [s_Coord[g:g+1,2] for g in g_range]

                ti_p.append(t+(((x1-x)**2+(y1-y)**2+(z1-z)**2)**0.5)/v)
                rms_val.append(((((t-ti_p)**2)/(nx*ny*nz*nt0))**0.5))
                grid.append((x,y,z,t,rms_val))

最后,在 Andrew H 提出的以下修复之后,这是最后一个修复的块

rms_mat = np.zeros((nx,ny,nz,nt0))
ti_p_mat =  np.zeros(rms_mat.shape , dtype=object) 
for i, x in enumerate(x_vec):
    for j, y in enumerate(y_vec):
        for k, z in enumerate(z_vec):
            for l, t in enumerate(t0_vec):
                g_range = np.arange(0,20,1) #for 20 stations from the file
                x1 = [s_Coord[g:g+1,0] for g in g_range]
                y1 = [s_Coord[g:g+1,1] for g in g_range]
                z1 = [s_Coord[g:g+1,2] for g in g_range]
                t0 = [t_ob[g:g+1] for g in g_range]
                ti_p = t+(((x1-x)**2+(y1-y)**2+(z1-z)**2)**0.5)/v
                rms = (((sum((t0-ti_p)**2))/(len(s_Coord)))**0.5)
                rms_mat[i, j, k, l] = rms
                ti_p_mat[i, j, k, l] = ti_p

您正在覆盖您的 rms 数组。您想要预分配 rms 并将本地计算的数组写入 rms 的一部分,或者您想要追加。

你可能想要这样的东西

rms_mat = np.zeros((nx,ny,nz,nt0))
ti_p_mat =  np.zeros(rms_mat.shape) 
for i, x in enumerate(x_vec):
    for j, y in enumerate(y_vec):
        for k, z in enumerate(z_vec):
            for l, t in enumerate(t0_vec):
                g_range = np.arange(0,20,1) #for 20 stations from the file
                x1 = [s_Coord[g:g+1,0] for g in g_range]
                y1 = [s_Coord[g:g+1,1] for g in g_range]
                z1 = [s_Coord[g:g+1,2] for g in g_range]
                t0 = [t_ob[g:g+1] for g in g_range]
                ti_p = t+(((x1-x)**2+(y1-y)**2+(z1-z)**2)**0.5)/v
                rms = ((((t0-ti_p)**2)/(nx*ny*nz*nt0))**0.5)
                rms_mat[i, j, k, l] = rms
                ti_pi_mat[i, j, k, l] = ti_p