需求
在通过json读取一个由各种不同材料属性的字典组成的list后,想要通过每个粒子的材料id的field获取对应的属性。
在python scope中能够正常运行,但是在taichi scope中会报错。
论坛中已有类似问题,不过这种字典的list似乎不容易转换成taichi field?想请问有没有好的办法解决这个问题。
代码
import taichi as ti
ti.init()
N = 10
a = ti.field(int, shape=N) # index field of particles' material ID
b = [{'matId': 0, 'matType': 1, 'p1': 10}, {'matId': 1, 'matType': 11, 'p2': 1}, {'matId': 2, 'matType': 2, 'p3': 22}]
@ti.kernel
def assign():
for i in range(N):
a[i] = ti.cast(ti.random()*3, int)
# @ti.kernel # will cause error in taichi scope, but works in python scope
def foo():
for i in range(N):
print(a[i], b[a[i]]['matType'])
assign()
foo()
报错为典型的类型错误:TypeError: list indices must be integers or slices, not Expr
.