@ti.kernel
def g2p(f: ti.i32):
    for p in range(0, n_particles):
        base = ti.cast(((x[(f, p)] * inv_dx) - 0.5), ti.i32)
        fx = ((x[(f, p)] * inv_dx) - ti.cast(base, real))
        w = [(0.5 * ti.sqr((1.5 - fx))), (0.75 - ti.sqr((fx - 1.0))), (0.5 * ti.sqr((fx - 0.5)))]
        new_v = ti.Vector(zero_vec())
        new_C = ti.Matrix(zero_matrix())
        for i in ti.static(range(3)):
            for j in ti.static(range(3)):
                for k in ti.static(range(3)):
                    dpos = (ti.cast(ti.Vector([i, j, k]), real) - fx)
                    g_v = grid_v_out[((base(0) + i), (base(1) + j), (base(2) + k))]
                    weight = ((w[i](0) * w[j](1)) * w[k](2))
                    new_v += (weight * g_v)
                    new_C += (((4 * weight) * ti.outer_product(g_v, dpos)) * inv_dx)
        v[((f + 1), p)] = new_v
        x[((f + 1), p)] = (x[(f, p)] + (dt * v[((f + 1), p)]))
        C[((f + 1), p)] = new_C
@ti.kernel
def g2p(f: ti.i32):
    for p in range(0, n_particles):
        base = ti.cast(((x[(f, p)] * inv_dx) - 0.5), ti.i32)
        fx = ((x[(f, p)] * inv_dx) - ti.cast(base, real))
        w = [(0.5 * ti.sqr((1.5 - fx))), (0.75 - ti.sqr((fx - 1.0))), (0.5 * ti.sqr((fx - 0.5)))]
        new_v = ti.Vector(zero_vec())
        new_C = ti.Matrix(zero_matrix())
        for i in ti.static(range(3)):
            for j in ti.static(range(3)):
                for k in ti.static(range(3)):
                    dpos = (ti.cast(ti.Vector([i, j, k]), real) - fx)
                    g_v = grid_v_out[((base(0) + i), (base(1) + j), (base(2) + k))]
                    weight = ((w[i](0) * w[j](1)) * w[k](2))
                    new_v += (weight * g_v)
                    new_C += (((4 * weight) * ti.outer_product(g_v, dpos)) * inv_dx)
        v[((f + 1), p)] = new_v
        x[((f + 1), p)] = (x[(f, p)] + (dt * v[((f + 1), p)]))
        C[((f + 1), p)] = new_C