【发布时间】:2020-02-28 15:06:19
【问题描述】:
我编写了一个函数来创建一个域来收集进入探测器的光强度。当我使用单个值作为起点时很好,但是当我设置一个点数组时(因为我需要描述一个表面并将强度积分到这个表面上)它会引发 ValueError: setting an array element with a sequence .
我试图验证不同数组的形状和它(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
我收到以下错误消息:
运行文件中的文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py”,第 827 行 execfile(文件名,命名空间) 文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/spyder_kernels/customize/spydercustomize.py”,第 102 行,在 execfile builtins.execfile(文件名,*位置) 文件“/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py”,第 1109 行,在 打印(“探针强度=”,probe_intensity(np.pi/20.0,10)) 文件“/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py”,第 878 行,在 probe_intensity ray_solution = ray_trace_solve_ivp(R0, T3, 0.0005) 文件“/home/tont_fe/data/cfd/OH_analysis/OHstar_ray_tracing/oh_ray_trace_candidate_cone_federica.py”,第 690 行,位于 ray_trace_solve_ivp rtol=rtol) 文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/ivp.py”,第456行,在solve_ivp 求解器 = 方法(有趣,t0,y0,tf,矢量化=矢量化,**选项) 文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/rk.py”,第 96 行,在 __init__ support_complex=真) 文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/base.py”,第 120 行,在 __init__ self._fun, self.y = check_arguments(fun, y0, support_complex) 文件“/home/tont_fe/anaconda2/lib/python2.7/site-packages/scipy/integrate/_ivp/base.py”,第 15 行,在 check_arguments y0 = y0.astype(dtype, 复制=假)【问题讨论】:
-
修复缩进。向我们(和您自己)展示错误发生的确切位置。告诉我们当时的变量(类型、形状、dtype 等)。换句话说,调试此类错误所需的所有有用的东西。
-
我认为调用 ray_solve_ivp 函数时会发生错误,在开始时我将两个数组打包在一个数组中,所以当调用 np.r_(R0,T0) 时。 y0 = y0.astype(dtype, 复制=假)。也许可以帮助在 y0 中指定一个 dtype?如果是,是哪一个?
-
错误信息应该准确地显示哪一行有问题!
-
我把错误信息放在帖子里了!
-
您是否检查了您对
ivp函数的输入是否符合记录的要求?类型、形状 dtype 等?
标签: python numpy sequence return-value user-defined-functions