os.makedirs(f'{output_dir}/particles') os.makedirs(f'{output_dir}/previews') print("Writing 2D vis and binary particle data to folder", output_dir) tee = Tee(fn=f'{output_dir}/log.txt', mode='w') print(args) else: output_dir = None # Use 512 for final simulation/render R = args.res thickness = 2 mpm = MPMSolver(res=(R, R, R), size=1, unbounded=True, dt_scale=1, quant=True, use_g2p2g=False, support_plasticity=True, water_density=1.5) mpm.add_surface_collider(point=(0, 0, 0), normal=(0, 1, 0), surface=mpm.surface_slip, friction=0.5) mpm.add_surface_collider(point=(0, 1.9, 0), normal=(0, -1, 0), surface=mpm.surface_slip, friction=0.5) bound = 1.9
import taichi as ti import numpy as np import utils from engine.mpm_solver import MPMSolver write_to_disk = False # 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])
import taichi as ti import numpy as np import utils from engine.mpm_solver import MPMSolver write_to_disk = False # Try to run on GPU 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
for i, face in enumerate(elements['vertex_indices']): assert len(face) == 3 for d in range(3): triangles[i, d * 3 + 0] = x[face[d]] * scale + offset[0] triangles[i, d * 3 + 1] = y[face[d]] * scale + offset[1] triangles[i, d * 3 + 2] = z[face[d]] * scale + offset[2] print('loaded') return triangles # Use 512 for final simulation/render R = 256 mpm = MPMSolver(res=(R, R, R), size=1, unbounded=True, dt_scale=0.5, E_scale=8) mpm.add_surface_collider(point=(0, 0, 0), normal=(0, 1, 0), surface=mpm.surface_slip, friction=0.5) triangles = load_mesh('taichi.ply', scale=0.04, offset=(0.5, 0.5, 0.5)) triangles_small = load_mesh('taichi.ply', scale=0.0133, offset=(0.5, 0.5, 0.5)) mpm.set_gravity((0, -25, 0)) def visualize(particles): np_x = particles['position'] / 1.0
import taichi as ti import numpy as np import utils from engine.mpm_solver import MPMSolver write_to_disk = False ti.init(arch=ti.cuda) # Try to run on GPU gui = ti.GUI("Taichi MLS-MPM-99", res=512, background_color=0x112F41) mpm = MPMSolver(res=(256, 256)) mpm.set_gravity([0, 0]) mpm.add_ellipsoid(center=[0.25, 0.45], radius=0.07, velocity=[1, 0], color=0xAAAAFF, material=MPMSolver.material_snow) mpm.add_ellipsoid(center=[0.75, 0.52], radius=0.07, velocity=[-1, 0], color=0xFFAAAA, material=MPMSolver.material_snow) for frame in range(500): mpm.step(8e-3) particles = mpm.particle_info() gui.circles(particles['position'], radius=1.5, color=particles['color']) gui.show(f'{frame:06d}.png' if write_to_disk else None)
print(args) return args args = parse_args() write_to_disk = args.out_dir is not None if write_to_disk: os.mkdir(f'{args.out_dir}') ti.init(arch=ti.cuda) # Try to run on GPU n = 256 gui = ti.GUI("Taichi Elements", res=n, background_color=0x112F41) activate_vis = ti.Vector.field(3, dtype=ti.f32, shape=[n, n]) mpm = MPMSolver(res=(n, n)) for i in range(3): mpm.add_cube(lower_corner=[0.2 + i * 0.1, 0.3 + i * 0.1], cube_size=[0.1, 0.1], 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:
import taichi as ti import numpy as np import utils import math from engine.mpm_solver import MPMSolver write_to_disk = False ti.init(arch=ti.cuda) # Try to run on GPU gui = ti.GUI("Taichi MLS-MPM-99", res=512, background_color=0x112F41) mpm = MPMSolver(res=(128, 128), unbounded=True) mpm.add_surface_collider(point=(0, 0), normal=(0.3, 1), surface=mpm.surface_slip) for i in range(3): mpm.add_cube(lower_corner=[0.2 + i * 0.1, 0.3 + i * 0.1], cube_size=[0.1, 0.1], material=MPMSolver.material_elastic) for frame in range(500): mpm.step(8e-3) if frame < 100: mpm.add_cube(lower_corner=[0.1, 0.4], cube_size=[0.01, 0.05], velocity=[1, 0], material=MPMSolver.material_sand) if 10 < frame < 100: mpm.add_cube(lower_corner=[0.3, 0.7],
import taichi as ti import numpy as np import utils import math from engine.mpm_solver import MPMSolver write_to_disk = False ti.init(arch=ti.cuda) # Try to run on GPU gui = ti.GUI("Taichi MLS-MPM-99", res=512, background_color=0x112F41) mpm = MPMSolver(res=(128, 128)) for i in range(3): mpm.add_cube(lower_corner=[0.2 + i * 0.1, 0.3 + i * 0.1], cube_size=[0.1, 0.1], material=MPMSolver.material_elastic) for frame in range(500): mpm.step(8e-3) if frame < 100: mpm.add_cube(lower_corner=[0.1, 0.4], cube_size=[0.01, 0.05], velocity=[1, 0], material=MPMSolver.material_sand) if 10 < frame < 100: mpm.add_cube(lower_corner=[0.3, 0.7], cube_size=[0.2, 0.01], material=MPMSolver.material_water, velocity=[math.sin(frame * 0.1), 0])
def main(): parser = parse_mpm() args = parser.parse_args() simulation_trajectories = [] if args.material == 'water': material = MPMSolver.material_water elif args.material == 'elastic': material = MPMSolver.material_elastic elif args.material == 'snow': material = MPMSolver.material_snow elif args.material == 'sand': material = MPMSolver.material_sand for sim_i in range(args.num_sims): ti.init(arch=ti.cuda) # Try to run on GPU gui = ti.GUI("Taichi MLS-MPM-99", res=512, background_color=0x112F41) mpm = MPMSolver(res=(128, 128)) mpm.E = args.young # init condition n_samples = args.ncubes * 10 lower_corner_candidates = np.random.uniform(0.2, 0.8, size=(n_samples, 2)) cube_size_candidates = np.random.uniform(0.06, 0.15, size=(n_samples, 2)) lower_corners = [] cube_sizes = [] num_cubes = args.ncubes for s in range(1, n_samples): if not cube_overlap( lower_corner_candidates[:s], cube_size_candidates[:s], lower_corner_candidates[s], cube_size_candidates[s]): lower_corners.append(lower_corner_candidates[s]) cube_sizes.append(cube_size_candidates[s]) if len(lower_corners) == num_cubes: break for i in range(len(lower_corners)): mpm.add_cube(lower_corner=lower_corners[i], cube_size=cube_sizes[i], material=material) simulation_trajectories.append(simulate(mpm, gui, args)) #rollout(simulation_trajectories[0], gui, args.gui_particle_radius) #cluster_scene(simulation_trajectories[-1][0]) if (sim_i + 1) % 1 == 0: print('Simulated {} trajectories.'.format(sim_i + 1)) data_path = os.path.join('../data/', args.dataset_name) if not os.path.exists(data_path): os.makedirs(data_path) if (sim_i + 1) % 100 == 0 or sim_i + 1 == args.num_sims: print(len(simulation_trajectories)) with open( '../data/{}/{}_{}.pkl'.format(args.dataset_name, args.material, sim_i // 100), 'wb') as f: pkl.dump(simulation_trajectories, f) simulation_trajectories = []
triangles[i, d * 3 + 1] = y[face[d]] * scale[1] + offset[1] triangles[i, d * 3 + 2] = z[face[d]] * scale[2] + offset[2] print('loaded') return triangles # Use 512 for final simulation/render R = 256 mpm = MPMSolver( res=(R, R, R), size=1, unbounded=True, dt_scale=1, quant=True, use_g2p2g=True, support_plasticity=False, # adaptive_dt=True, v_clamp_g2p2g=True) mpm.add_surface_collider(point=(0, 0, 0), normal=(0, 1, 0), surface=mpm.surface_slip, friction=0.5) mpm.add_surface_collider(point=(0, 1.9, 0), normal=(0, -1, 0), surface=mpm.surface_slip, friction=0.5)
import taichi as ti import numpy as np from engine.mpm_solver import MPMSolver write_to_disk = False # Try to run on GPU ti.init(arch=ti.gpu, device_memory_GB=4.0) mpm = MPMSolver(res=(64, 64, 64), size=10, max_num_particles=2**15, use_ggui=True) mpm.add_ellipsoid(center=[2, 4, 3], radius=1, material=MPMSolver.material_snow, velocity=[0, -10, 0]) mpm.add_cube(lower_corner=[2, 6, 3], cube_size=[1, 1, 3], material=MPMSolver.material_elastic) mpm.add_cube(lower_corner=[2, 8, 3], cube_size=[1, 1, 3], material=MPMSolver.material_sand) mpm.set_gravity((0, -50, 0)) @ti.kernel def set_color(ti_color: ti.template(), material_color: ti.ext_arr(), ti_material: ti.template()):
import numpy as np import os import time import utils from engine.mpm_solver import MPMSolver write_to_disk = True if write_to_disk: output_dir = "/home/blatny/repos/taichi_elements/demo/output/sim_1/" os.makedirs(os.path.dirname(output_dir), exist_ok=True) ti.init(arch=ti.cpu) gui = ti.GUI("Taichi Elements", res=512, background_color=0x112F41) mpm = MPMSolver(res=(32, 32, 32), size=10) mpm.add_ellipsoid(center=[2, 4, 3], radius=1, material=MPMSolver.material_snow, velocity=[0, -10, 0]) mpm.set_gravity((0, -50, 0)) start_t = time.time() for frame in range(3): print(f'frame: {frame}') t = time.time() mpm.step(4e-3) # colors = np.array([0x068587, 0xED553B, 0xEEEEF0, 0xFFFF00], dtype=np.uint32)
np_x = particles['position'] / 1.0 # simple camera transform screen_x = np_x[:, 0] screen_y = np_x[:, 1] screen_pos = np.stack([screen_x, screen_y], axis=-1) gui.circles(screen_pos, radius=0.8, color=particles['color']) gui.show() # Use 512 for final simulation/render R = 64 mpm = MPMSolver(res=(R, R, R), size=1, unbounded=True, dt_scale=0.5, E_scale=8, max_num_particles=max_num_particles) mpm.add_surface_collider(point=(0, 0, 0), normal=(0, 1, 0), surface=mpm.surface_slip, friction=3.5) mpm.set_gravity((0, -25, 0)) counter = 0 start_t = time.time() addParticlesCount = 2000 for frame in range(15000): print(f'frame {frame}')