想知道为什么加法会失败?ptr是什么属性?

class adjustnbody:
    def __init__(self,N:ti.f32,mass:ti.f32,dimension:ti.f32):
        self.PI = ti.atan2(1,1)*4
        self.G = ti.field(ti.f32,shape=())
        
        self.dim = dimension
        self.n = N
        self.mass = mass
        self.pos = ti.Vector.field(self.dim,ti.f32,self.n)
        self.vel = ti.Vector.field(self.dim,ti.f32,self.n)
        self.force = ti.Vector.field(self.dim,ti.f32,self.n)
        self.color = ti.Vector.field(3,ti.f32,self.n)
        if self.dim == 2:
            self.offset = ti.Vector([np.random.uniform(),np.random.uniform()])
            self.expand = ti.Vector([1,1])
            self.originvel = ti.Vector([0,0])
        elif self.dim == 3:
            self.offset = ti.Vector([np.random.uniform(),np.random.uniform(),np.random.uniform()])
            self.expand = ti.Vector([1,1,1])
            self.originvel = ti.Vector([0,0,0])
    @ti.kernel
    def initial(self,center:ti.template(),expand_size:ti.f32,init_vel:ti.f32):
        
        for i in range(self.n):
            if self.n == 1:
                self.pos[i] = center
                for j in ti.static(range(self.dim)):
                    self.vel[i][j] = 0
            else:
                #off, ovel = self.xvcalcu(self.dim,init_vel,expand_size)
                self.offset += -self.expand*expand_size*0.5
                self.originvel[0] = -self.offset[1]
                self.originvel[1] = self.offset[0]
                self.originvel *= init_vel
                self.pos[i] = center+self.offset
                self.vel[i] = self.originvel

其中让offset去加后面的expand会报错

这个ptr又是什么属性,在python scope试过这两个向量可以加的,为什么里面不能加

两个向量都是ti.Vector(),但是在taichi scope里面不能做加法

定义改成taichi field然后再从field中取出来加又可以了。。。,这个真奇怪

这里虽然self.offset对于initial这个kernel来说是全局的,但是Taichi中目前只允许field是全局变量,其他都只能作为常数来用,可以读,但是不可以写

1 个赞

理解了,谢谢

1 个赞

如果想要写的话,可以offset可以声明成 :
self.offset = ti.Vector.field(2, ti.f32, ())

在kerne两种写操作就变成:
self.offset[None] += -self.expand*expand_size*0.5