函数错误。ValueError:使用序列设置数组元素

2024-09-30 05:18:14 发布

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

我写了一个函数来创建一个域来收集进入探测器的光的强度。当我使用一个单一的值作为起始点是可以的,但是当我设置一个poitns数组时(因为我需要描述一个曲面并将强度积分到这个曲面上),它会引发ValueError:用序列设置一个数组元素。你知道吗

我试着验证不同数组的形状和它(3,)和(3,),所以在理论上没有不一致。。。这里是函数

def probe_intensity(cone_FOV, n_circles):
    probeI = 0
    # probe location information
    #probe_angle = 5.0/4.0*np.pi
    #tvar=np.linspace(4.09,4.186,30)
    ray_cone_angle = cone_FOV/(2.0*n_circles)
   # T0 = np.array([0, 0, 1])
    r = 6.2
    x = 5.5
    probe_angle = np.linspace(1.2334*np.pi-1,1.2334*np.pi+1,30)
    probe_direction_angle = probe_angle - np.pi
    #R1=np.ones(len(probe_angle))
    # probe direction
    for k in probe_angle:

# getting the estimated cone for a single ray
#ray_cone_angle = cone_FOV/(2.0*n_circles)

# probe position information
#r = 6.2
#x = 5.5

        R0 = np.array([5.5,
               r*np.sin(probe_angle)+0.0001,
               r*np.cos(probe_angle)+0.0001])*0.001

        gamma = probe_direction_angle

        ROT1 = np.matrix([[1, 0, 0],
                  [0, np.cos(gamma), np.sin(gamma)],
                  [0, -np.sin(gamma), np.cos(gamma)]])


# vector T0 before any rotation
        T0 = np.array([0, 0, 1])

# vector T1 - probe axis vector
        T1 = np.array(ROT1*np.matrix(T0).T).flatten()

    # rotation for the cone_half_angle
        for ray_carrier_angle in [(i + 0.5)*ray_cone_angle for i in range(n_circles)]:

            alpha = ray_carrier_angle

            ROT2 = np.matrix([[np.cos(alpha), 0, np.sin(alpha)],
                          [0, 1, 0],
                          [-np.sin(alpha), 0, np.cos(alpha)]])

        # rotating to make a full circle of the cone
            for beta in np.linspace(0, 2*np.pi, int(np.round(n_circles*np.pi))):
                ROT3 = np.matrix([[np.cos(beta), np.sin(beta), 0],
                              [-np.sin(beta), np.cos(beta), 0],
                              [0, 0, 1]])

            # second rotaiton for the cone after the probe direction rotation
                T3 = np.array(ROT1*ROT3*ROT2*np.matrix(T0).T).flatten()
                print R0
                print R0.shape
                print T3
                print T3.shape
                T3=list(T3)
                R0=list(R0)


                ray_solution = ray_trace_solve_ivp(R0, T3, 0.0005)
                t, I = field.integrate_trace(ray_solution, 0.0005)
            #s,e,alpha,I = field.getspectra

        # Add the intensity of the ray to the intensity gathered at the probe. Dot product takes care of the projection
            probeI += I[-1]*np.dot(T0, T3)
            print probeI

return probeI

另一个被调用的函数是

def ray_trace_solve_ivp(R0, T0, optical=False, dt=np.inf, atol=1e-6, rtol=1e-2):
    y0 = np.r_[R0, T0]

    if optical:
        differential_equation = eikonalODE1system_optical
    else:
        differential_equation = eikonalODE1system_physical

    sol = solve_ivp(differential_equation, [0, 0.5], y0,
                    events=limit_functions,
                    dense_output=True,
                    max_step=dt,
                    atol=atol,
                    rtol=rtol)
    return sol

我收到以下错误消息:

  File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 102, in execfile
    builtins.execfile(filename, *where)

  File "/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py", line 1109, in 
    print("probe intensity=", probe_intensity(np.pi/20.0, 10))

  File "/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py", line 878, in probe_intensity
    ray_solution = ray_trace_solve_ivp(R0, T3, 0.0005)

  File "/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py", line 690, in ray_trace_solve_ivp
    rtol=rtol)

  File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/ivp.py", line 456, in solve_ivp
    solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)

  File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/rk.py", line 96, in __init__
    support_complex=True)

  File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/base.py", line 120, in __init__
    self._fun, self.y = check_arguments(fun, y0, support_complex)

  File "/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/base.py", line 15, in check_arguments
    y0 = y0.astype(dtype, copy=False)

Tags: theinpyhomenplinefileprobe
1条回答
网友
1楼 · 发布于 2024-09-30 05:18:14

因此,代码的相关部分是:

probe_angle = np.linspace(1.2334*np.pi-1,1.2334*np.pi+1,30)
R0 = np.array([5.5,
               r*np.sin(probe_angle)+0.0001,
               r*np.cos(probe_angle)+0.0001])*0.001
T0 = np.array([0, 0, 1])
y0 = np.r_[R0, T0]

或者简化一点:

In [96]: R0 = np.array([1, np.linspace(0,1,3), np.linspace(0,3,3)])             
In [97]: R0                                                                     
Out[97]: array([1, array([0. , 0.5, 1. ]), array([0. , 1.5, 3. ])], dtype=object)
In [98]: T0 = np.array([0,0,1])                                                 
In [99]: np.r_[R0,T0]                                                           
Out[99]: 
array([1, array([0. , 0.5, 1. ]), array([0. , 1.5, 3. ]), 0, 0, 1],
      dtype=object)
In [100]: _.astype(float)                                                       
                                     -
ValueError                                Traceback (most recent call last)
<ipython-input-100-b12cc607d111> in <module>
  > 1 _.astype(float)

ValueError: setting an array element with a sequence.

您的R0,然后是y0构造函数,生成一个对象数据类型数组—一个包含数组的数组。你知道吗

但是solve_ivp需要一个(n,)浮点数组,其中ndifferential_equation产生的值的数目。你知道吗

直接的问题是从y0创建一个数字数组。既然我不知道你需要或想要什么,我就不打算提出解决办法。你知道吗

而且我对differential_equation一无所知。我不知道它能返回多少值。就一个,三个?或者它是否随y0输入的大小而变化?你知道吗

退一步,做一个更简单的问题。确保您了解solve_ivp的需求,包括函数和y0。你知道吗

当你带着新问题回来的时候,请让它更容易帮助你!你知道吗

相关问题 更多 >

    热门问题