color = r * 65536 + g * 256 + b
    triangles = quantized if subframe % 2 == 0 else simulation
    mpm.add_mesh(triangles=triangles,
                 material=MPMSolver.material_elastic,
                 color=color,
                 velocity=(0, -5, 0),
                 translation=((i - 0.5) * 0.4, 0.2, (3 - j) * 0.1 - 0.8))


for frame in range(args.frames):
    print(f'frame {frame}')
    t = time.time()
    frame_split = 1
    if frame < stop_seeding_at:
        for subframe in range(frame * frame_split, (frame + 1) * frame_split):
            if mpm.n_particles[None] < max_num_particles:
                seed_letters(subframe)

        mpm.step(frame_dt / frame_split, print_stat=True)
    else:
        mpm.step(frame_dt, print_stat=True)
    if with_gui:
        particles = mpm.particle_info()
        visualize(particles, frame, output_dir)

    if write_to_disk:
        mpm.write_particles(f'{output_dir}/particles/{frame:05d}.npz')
    print(f'Folder name {output_dir}')
    print(f'Frame total time {time.time() - t:.3f}')
    print(f'Total running time {time.time() - start_t:.3f}')
# Try to run on GPU
ti.init(arch=ti.cuda, device_memory_GB=3.0)

gui = ti.GUI("Taichi Elements", res=512, background_color=0x112F41)

mpm = MPMSolver(res=(64, 64, 64), size=1)

triangles = np.fromfile('suzanne.npy', dtype=np.float32)
triangles = np.reshape(triangles, (len(triangles) // 9, 9)) * 0.306 + 0.501

mpm.add_mesh(triangles=triangles,
             material=MPMSolver.material_elastic,
             color=0xFFFF00)

mpm.set_gravity((0, -20, 0))

for frame in range(1500):
    mpm.step(4e-3)
    particles = mpm.particle_info()
    np_x = particles['position'] / 1.0

    # simple camera transform
    screen_x = ((np_x[:, 0] + np_x[:, 2]) / 2**0.5) - 0.2
    screen_y = (np_x[:, 1])

    screen_pos = np.stack([screen_x, screen_y], axis=-1)

    gui.circles(screen_pos, radius=1.1, color=particles['color'])
    gui.show(f'{frame:06d}.png' if write_to_disk else None)
                     velocity=(0, -6, 0),
                     translation=(0.0, 0.16, (F % 2) * 0.4))

    if frame > 60 and mpm.n_particles[None] < max_num_particles:
        i = frame % 3 - 1.5
        j = 0  # frame / 4 % 4 - 1
        colors = [0xFF8888, 0xEEEEFF, 0xFFFF55]
        materials = [
            MPMSolver.material_elastic, MPMSolver.material_elastic,
            MPMSolver.material_elastic
        ]
        mpm.add_mesh(triangles=triangles_small,
                     material=materials[frame % 3],
                     color=colors[frame % 3],
                     velocity=(0, -6, 0),
                     translation=((i + 0.5) * 0.33, 0.13, 0.2))

    mpm.step(4e-3, print_stat=True)
    if with_gui and frame % 3 == 0:
        particles = mpm.particle_info()
        visualize(particles)

    if write_to_disk:
        mpm.write_particles(f'{output_dir}/{frame:05d}.npz')

    if w2ply:
        mpm.write_ply(output_dir,frame + 1)

    print(f'Frame total time {time.time() - t:.3f}')
    print(f'Total running time {time.time() - start_t:.3f}')
Exemple #4
0
ti.init(arch=ti.cuda, kernel_profiler=True)

gui = ti.GUI("MPM Benchmark", res=256, background_color=0x112F41)

mpm = MPMSolver(res=(256, 256, 256), size=1, unbounded=False)

particles = np.fromfile('benchmark_particles.bin', dtype=np.float32)
particles = particles.reshape(len(particles) // 3, 3)
print(len(particles))

mpm.add_particles(particles=particles,
                  material=MPMSolver.material_elastic,
                  color=0xFFFF00)

mpm.set_gravity((0, -20, 0))

for frame in range(1500):
    mpm.step(3e-3)
    particles = mpm.particle_info()
    np_x = particles['position'] / 1.0

    # simple camera transform
    screen_x = ((np_x[:, 0] + np_x[:, 2]) / 2**0.5) - 0.2
    screen_y = (np_x[:, 1])

    screen_pos = np.stack([screen_x, screen_y], axis=-1)

    gui.circles(screen_pos, radius=1.1, color=particles['color'])
    gui.show(f'{frame:06d}.png' if write_to_disk else None)
    ti.kernel_profiler_print()
                 material=MPMSolver.material_elastic)


@ti.kernel
def block_active(vs_field: ti.template(), solver: ti.template()):
    for I in ti.grouped(vs_field):
        blk_I = I // solver.leaf_block_size - solver.block_offset
        if ti.is_active(solver.block,
                        blk_I) and (I % solver.leaf_block_size).min() != 0:
            vs_field[I] = [0.11, 0.22, 0.25]
        else:
            vs_field[I] = [0.07, 0.125, 0.23]


for frame in range(500):
    mpm.step(8e-3)
    if frame < 500:
        mpm.add_cube(lower_corner=[0.1, 0.8],
                     cube_size=[0.01, 0.05],
                     velocity=[1, 0],
                     material=MPMSolver.material_sand)
    if 10 < frame < 100:
        mpm.add_cube(lower_corner=[0.6, 0.7],
                     cube_size=[0.2, 0.01],
                     material=MPMSolver.material_water,
                     velocity=[math.sin(frame * 0.1), 0])
    if 120 < frame < 200 and frame % 10 == 0:
        mpm.add_cube(
            lower_corner=[0.4 + frame * 0.001, 0.6 + frame // 40 * 0.02],
            cube_size=[0.2, 0.1],
            velocity=[-3, -1],
Exemple #6
0
    start_frame = load_mpm_state(mpm, args.state_dir) + 1

for frame in range(start_frame, args.frames):
    print(f'frame {frame}')
    t = time.time()
    if args.thin:
        frame_split = 5
    else:
        frame_split = 1

    for subframe in range(frame * frame_split, (frame + 1) * frame_split):
        if mpm.n_particles[None] < max_num_particles:
            if args.thin:
                seed_letters(subframe)
            else:
                seed_bars(subframe)

        mpm.step(1e-2 / frame_split, print_stat=True)

    if frame % args.state_fre == 0:
        save_mpm_state(mpm, frame, f'{output_dir}/states/{frame:05d}.npz')

    if with_gui and frame % 4 == 0:
        particles = mpm.particle_info()
        visualize(particles, frame, output_dir)

    if write_to_disk and frame % 4 == 0:
        mpm.write_particles(f'{output_dir}/particles/{frame:05d}.npz')
    print(f'Frame total time {time.time() - t:.3f}')
    print(f'Total running time {time.time() - start_t:.3f}')
    screen_x = np_x[:, 0] * 0.5 + 0.5
    screen_y = np_x[:, 1] * 0.5

    screen_pos = np.stack([screen_x, screen_y], axis=-1)

    gui.circles(screen_pos, radius=1.0, color=particles['color'])
    if output_dir is None:
        gui.show()
    else:
        gui.show(f'{output_dir}/previews/{frame:05d}.png')


counter = 0

start_t = time.time()

for frame in range(args.frames):
    print(f'frame {frame}')
    t = time.time()
    mpm.step(1e-2, print_stat=True)
    if with_gui:
        particles = mpm.particle_info()
        visualize(particles, frame, output_dir)

    if write_to_disk:
        mpm.write_particles(f'{output_dir}/particles/{frame:05d}.npz')
        if args.output_ply:
            mpm.write_particles_ply(f'{output_dir}/particles/{frame:05d}.ply')
    print(f'Frame total time {time.time() - t:.3f}')
    print(f'Total running time {time.time() - start_t:.3f}')