Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
YoruCathy opened this issue Feb 9, 2024 · 2 comments
Labels
question Question on using Taichi

Comments

@YoruCathy
Copy link

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 YoruCathy added the question Question on using Taichi label Feb 9, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Feb 9, 2024
@BillXu2000
Copy link
Collaborator

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).

@YoruCathy
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question on using Taichi
Projects
Status: Untriaged
Development

No branches or pull requests

2 participants