在taichi scope中,整数field不能当做给定list的索引

需求

在通过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.

list 和 dict 在 Taichi scope 中都不能动态索引,尽量减少他们在 taichi scope 中的使用。
Taichi 目前对于字符串这种变长类型支持似乎不太好,想用字符串做索引查询 dict 的话可以考虑用定长的字符串作为索引手写建立一个哈希表。