这是从程序中简化的最小复现代码,
报错异常矩阵大小不匹配,为何在element_type_num=1时此时V1 = ti.abs(ti.Matrix.rows([a01,a02,a03]).determinant())/6这一行根本不会执行仍会报错?编译时就已经对向量进行了形状检查吗?我想实现同一个kernel处理不同维度的问题,并通过传入参数element_type_num控制运行分支,该如何实现?传入的pointField参数的维度是一开始不确定(从文件读取)、一旦确定就不会改变的:
File “D:\Data_qiany\态基程序热应变\SBPD\taichideterminant.py”, line 42, in det:
V1 = ti.abs(ti.Matrix.rows([a01,a02,a03]).determinant())/6
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
expected a square matrix, got shape (3, 2)
import numpy as np #the numpy is needed by ti
import taichi as ti
import taichi.math as tim
ti.init(cpu_max_num_threads = 4,arch=ti.cpu,default_ip=ti.i32,device_memory_GB=4, default_fp=ti.f64,debug=True)#debug=True makes error can be reported when array index out of range
nf = ti.field(ti.math.vec2,(8,))
@ti.kernel
def det(element_type_num:ti.i32,nf:ti.template()):
V=0.0
if element_type_num == 1: #"TRI3" or element_type == "TRI"
n1 = nf[0]
n2 = nf[1]
n3 = nf[2]
#pointField.Coor[m] = (n1+n2+n3)/3.0
V = ti.abs((n2-n1).cross(n3-n1)/2.0 )
pass
elif element_type_num == 2: # "QUAD4" or element_type == "QUAD" or element_type == "SHELL" or element_type == "SHELL4":
n1 = nf[0]
n2 = nf[1]
n3 = nf[2]
n4 = nf[3]
V=1
pass
elif element_type_num == 3: #"HEX8" or element_type =="HEX":
print(element_type_num)
n1 = nf[0]
n2 = nf[1]
n3 = nf[2]
n4 = nf[3]
n5 = nf[4]
n6 = nf[5]
n7 = nf[6]
n8 = nf[7]
#Coor_Guass_point:np.ndarray = (n2+n3+n4+n5\
# +n6+n7+n8+n1)/8.0
a01 = n2-n1
a02 = n4-n1
a03 = n5-n1
C1 = (n1+n2+n4+n5)/4.0
V1 = ti.abs(ti.Matrix.rows([a01,a02,a03]).determinant())/6
#print(V1)
a01 = n7-n6
a02 = n2-n6
a03 = n5-n6
C2 = (n2+n5+n6+n7)/4.0
V2 = ti.abs(ti.Matrix.rows([a01,a02,a03]).determinant())/6
a01 = n4-n3
a02 = n2-n3
a03 = n7-n3
C3 = (n3+n2+n4+n7)/4.0
V3 = ti.abs(ti.Matrix.rows([a01,a02,a03]).determinant())/6
a01 = n5-n8
a02 = n7-n8
a03 = n4-n8
C4 = (n4+n5+n7+n8)/4.0
V4 = ti.abs(ti.Matrix.rows([a01,a02,a03]).determinant())/6
a01 = n4-n2
a02 = n5-n2
a03 = n7-n2
C5 = (n2+n4+n5+n7)/4.0
V5 = ti.abs(ti.Matrix.rows([a01,a02,a03]).determinant())/6
V = V1+V2+V3+V4+V5
pass
print(V)
print("det1")
det(1,nf)