taichi icon indicating copy to clipboard operation
taichi copied to clipboard

AttributeError: module 'taichi.lang.matrix_ops' has no attribute 'to_numpy'

Open YoruCathy opened this issue 1 year ago • 2 comments

Hi, I was trying to convert a Taichi Vector to be saved. However, this gives an error saying `AttributeError: module 'taichi.lang.matrix_ops' has no attribute 'to_numpy' My code is as the following

new_v = ti.Vector.zero(DTYPE_TI, self.dim)
new_v.to_numpy()

I also tried to add to_numpy in other places, but none of them worked

@ti.kernel
    def g2p(self, f: ti.i32):
        for p in range(self.n_particles):
            if self.particles_ng[f, p].used:
                base = (self.particles[f, p].x * self.inv_dx - 0.5).cast(int)
                fx = self.particles[f, p].x * self.inv_dx - base.cast(DTYPE_TI)
                w = [0.5 * (1.5 - fx) ** 2, 0.75 - (fx - 1.0) ** 2, 0.5 * (fx - 0.5) ** 2]
                new_v = ti.Vector.zero(DTYPE_TI, self.dim)
                new_v.to_numpy()
                new_C = ti.Matrix.zero(DTYPE_TI, self.dim, self.dim)
                for offset in ti.static(ti.grouped(self.stencil_range())):
                    dpos = offset.cast(DTYPE_TI) - fx
                    g_v = self.grid[f, base + offset].v_out
                    weight = ti.cast(1.0, DTYPE_TI)
                    for d in ti.static(range(self.dim)):
                        weight *= w[offset[d]][d]
                    new_v += weight * g_v
                    new_C += 4 * self.inv_dx * weight * g_v.outer_product(dpos)

                # collide with agent
                if ti.static(self.agent is not None):
                    if ti.static(self.agent.collide_type in ['particle', 'both']):
                        new_v.to_numpy()
                        old_v = new_v
                        new_x_tmp = self.particles[f, p].x + self.dt * new_v
                        new_v = self.agent.collide(f, new_x_tmp, new_v, self.dt)

                        delta_v = new_v - old_v
                        delta_v = delta_v.to_numpy()
                        self.delta_v_list[self.current_step, :] = delta_v.to_numpy()
                        
                        if self.current_step >= 800:

                            np.savetxt('delta_v_list.txt', self.delta_v_list)
                        
                        self.current_step += 1

The taichi version I'm using is 1.5.0. Any help is appreciated.

YoruCathy avatar Feb 09 '24 00:02 YoruCathy

to_numpy can only be used in fields (ti.Vector.field) or ndarray, while ti.Vector.zero is a temporary variable and can hardly be converted into numpy arrays. Maybe you can try to save results into a field and then use to_numpy in python scope (i.e. out of ti.kernel).

BillXu2000 avatar Feb 09 '24 05:02 BillXu2000

Thank you for your help! Using it for fields worked. The documentation for this is a little bit confusing since this looks like it works for Vector/Matrix. Would suggest adding something saying it only works for fields or ndarray. Screenshot 2024-02-09 at 00 11 21

YoruCathy avatar Feb 09 '24 05:02 YoruCathy