Skip to content

Commit

Permalink
add async scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
squarefk committed Dec 17, 2018
1 parent 1f9bd83 commit b8690b6
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 0 deletions.
116 changes: 116 additions & 0 deletions scripts/async/combo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import taichi as tc
from math import *
from random import *
from async_mpm import AsyncMPM

r = 300
task_id = 'combo'
Async = True

if __name__ == '__main__':
res = (r, r, r)

if Async:
mpm = AsyncMPM(
res=res,
particle_collision=False,
optimized=True,
verbose_bgeo=True,
gravity=(0, -10, 0),
task_id=task_id,
max_units=256,
strength_dt_mul=0.8,
num_frames=240)
else:
mpm = tc.dynamics.MPM(
res=res,
particle_collision=False,
optimized=True,
verbose_bgeo=True,
gravity=(0, -10, 0),
base_delta_t=1e-6 * 16,
task_id=task_id,
num_frames=240)

levelset = mpm.create_levelset()
levelset.add_plane(tc.Vector(0, 0, 1), -0.41)
levelset.add_plane(tc.Vector(0, 0, -1), 0.59)
levelset.set_friction(-1)
mpm.set_levelset(levelset, False)

tex = tc.Texture('ring', outer=0.025)
tex = tc.Texture(
'bound', tex=tex, axis=2, bounds=(0.4, 0.6), outside_val=(0, 0, 0)) * 8
ys = [0.5, 0.38, 0.26, 0.14]
xs = [6, 7, 6, 7]
for i, y in enumerate(ys):
first = xs[i]
for j in range(first):
x = 0.05 + 0.9 / (first + 1) * (j + 1)
tex_stick = tex.translate((x - 0.5, y - 0.5, 0))
mpm.add_particles(
type='elastic',
E=5e5,
density_tex=tex_stick.id,)

tex_dragon = tc.Texture(
'mesh',
translate=(0.5, 0.5, 0.5),
scale=(0.006, 0.006, 0.006),
adaptive=True,
filename='../../data/cute_dragon.obj') * 8
tex_dragon = tc.Texture(
'rotate', tex=tex_dragon, rotate_axis=1, rotate_times=2)

seed(2)
frame_num = 0

def frame_update(t, frame_dt):
global frame_num
frame_num = frame_num + 1
if frame_num % 48 == 1:
start = 0.2
for i in range(4):
tex = tex_dragon.rotate_angle(45 - random() * 90 * pi / 180).translate(
(start + i * 0.2 - 0.5, 0.7 - 0.5, 0))
kinds = []
if frame_num == 1:
kinds = [0, 2, 1, 4]
if frame_num == 48 + 1:
kinds = [2, 3, 0, 0]
if frame_num == 48 * 2 + 1:
kinds = [2, 0, 1, 3]
if frame_num == 48 * 3 + 1:
kinds = [1, 0, 2, 4]
if frame_num == 48 * 4 + 1:
continue
kind = kinds[i]
if kind == 0:
mpm.add_particles(
type='elastic',
density_tex=tex.id,)
elif kind == 1:
mpm.add_particles(
type='sand',
density_tex=tex.id,
lambda_0=20000,
mu_0=1000,
friction_angle=10)
elif kind == 2:
mpm.add_particles(
type='snow',
density_tex=tex.id,)
elif kind == 3:
mpm.add_particles(
type='water',
density_tex=tex.id,
gamma=1,
k=10000,)
else:
mpm.add_particles(
type='von_mises',
density_tex=tex.id,)

return

mpm.simulate(frame_update=frame_update)
73 changes: 73 additions & 0 deletions scripts/async/sand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import taichi as tc
from math import *
from random import *
from async_mpm import AsyncMPM

r = 400
task_id = 'sand'
Async = True

if __name__ == '__main__':
res = (r, r, r)

if Async:
mpm = AsyncMPM(
res=res,
particle_collision=False,
optimized=True,
verbose_bgeo=True,
gravity=(0, -1, 0),
task_id=task_id,
cfl_dt_mul=0.1,
num_frames=120)
else:
mpm = tc.dynamics.MPM(
res=res,
particle_collision=False,
optimized=True,
verbose_bgeo=True,
gravity=(0, -1, 0),
base_delta_t=1e-6 * 64,
task_id=task_id,
num_frames=120)

levelset = mpm.create_levelset()
levelset.add_cuboid((0, 0.2, 0.05), (0.95, 0.95, 0.95), True)
levelset.set_friction(-3)
mpm.set_levelset(levelset, False)

tex = tc.Texture(
'mesh',
translate=(0.5, 0.5, 0.5),
scale=(0.012, 0.012, 0.012),
adaptive=True,
filename='../../data/cute_dragon.obj') * 8
tex = tc.Texture(
'rotate', tex=tex, rotate_axis=1, rotate_times=3).translate((0.1, -0.125,
0))

mpm.add_particles(
type='sand',
density_tex=tex.id,
lambda_0=20000,
mu_0=1000,
friction_angle=10)

tex_sphere = tc.Texture(
'mesh',
translate=(0, 0, 0),
scale=(0.015, 0.015, 0.015),
adaptive=True,
filename='../../data/sphere_small.obj') * 8
seed(1)
for i in range(6):
x = 0.05 + random() * 0.05
y = 0.25 + random() * 0.2
z = 0.45 + random() * 0.1
if i == 0:
continue
tex = tex_sphere.translate((x, y, z))
mpm.add_particles(
type='elastic', E=2e5, density_tex=tex.id, initial_velocity=(5, 0, 0))

mpm.simulate()
88 changes: 88 additions & 0 deletions scripts/async/slope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import taichi as tc
import os
import math
from async_mpm import AsyncMPM

r = 400
task_id = 'slope'
Async = True

thickness = 0.012

if __name__ == '__main__':
res = (r, r, r)

if Async:
mpm = AsyncMPM(
res=res,
particle_collision=False,
optimized=True,
max_units=512,
gravity=(0, -10, 0),
task_id=task_id,
num_frames=120)
else:
mpm = tc.dynamics.MPM(
res=res,
particle_collision=False,
optimized=True,
gravity=(0, -10, 0),
base_delta_t=1e-6 * 8,
task_id=task_id,
num_frames=120)

levelset = mpm.create_levelset()
levelset.add_slope(tc.Vector(0.7, 0.5, 0.5), 0.4, 30.0 / 180 * math.pi)
levelset.set_friction(1)
mpm.set_levelset(levelset, False)

# Snow Ball
tex_ball = tc.Texture(
'sphere', center=(0.2, 0.38 + thickness, 0.5), radius=0.04) * 8
mpm.add_particles(
type='snow',
density_tex=tex_ball.id,
initial_velocity=(0, 0, 0),
density=400,)

# Snow Man
men_h = [0.0475, 0.095 + 0.0325, 0.095 + 0.065 + 0.02]
men_r = [0.0475, 0.0325, 0.02]
for i in range(3):
tex = tc.Texture(
'sphere', center=(0.75, 0.1 + men_h[i], 0.5), radius=men_r[i])
tex = tc.Texture(
'bound', tex=tex, axis=1, bounds=(0.1, 0.9), outside_val=(0, 0, 0)) * 8
mpm.add_particles(
type='snow',
density_tex=tex.id,
initial_velocity=(0, 0, 0),
youngs_modulus=6e5,
density=400,)

# Ground
levelset = mpm.create_levelset()
levelset.add_slope(tc.Vector(0.7, 0.5, 0.5), 0.4, 30.0 / 180 * math.pi)
tex = tc.Texture(
'levelset3d',
levelset=levelset,
bounds=(0, thickness / levelset.get_delta_x()))
tex = tc.Texture(
'bound', tex=tex, axis=2, bounds=(0.25, 0.75), outside_val=(0, 0, 0))
tex = tc.Texture(
'bound', tex=tex, axis=0, bounds=(0.1, 0.9), outside_val=(0, 0, 0))
tex = tc.Texture(
'bound', tex=tex, axis=1, bounds=(0.1, 0.9), outside_val=(0, 0, 0))
tex2 = tc.Texture(
'sphere', center=(0.75, 0.1 + men_h[0], 0.5), radius=men_r[0])
tex = (tex - tex2) * 8
mpm.add_particles(
type='snow',
density_tex=tex.id,
initial_velocity=(0, 0, 0),
density=100,
youngs_modulus=2000,
# Jp=1.5,
)

mpm.simulate()
71 changes: 71 additions & 0 deletions scripts/async/water.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import taichi as tc
from math import *
from random import *
from async_mpm import AsyncMPM

r = 400
task_id = 'water'
Async = True

if __name__ == '__main__':
res = (r, r, r)

if Async:
mpm = AsyncMPM(
res=res,
particle_collision=False,
optimized=True,
verbose_bgeo=True,
gravity=(0, -1, 0),
task_id=task_id,
cfl_dt_mul=0.1,
num_frames=240)
else:
mpm = tc.dynamics.MPM(
res=res,
particle_collision=False,
optimized=True,
verbose_bgeo=True,
gravity=(0, -1, 0),
base_delta_t=1e-6 * 16,
task_id=task_id,
num_frames=240)

levelset = mpm.create_levelset()
levelset.add_cuboid((0, 0.2, 0.05), (0.95, 0.95, 0.95), True)
levelset.set_friction(-2.5)
mpm.set_levelset(levelset, False)

tex = tc.Texture(
'mesh',
translate=(0.5, 0.5, 0.5),
scale=(0.012, 0.012, 0.012),
adaptive=True,
filename='../../data/cute_dragon.obj') * 8
tex = tc.Texture(
'rotate', tex=tex, rotate_axis=1, rotate_times=3).translate((0.1, -0.125,
0))

mpm.add_particles(
type='elastic',
density_tex=tex.id,)

tex = tc.Texture('ring', outer=0.03) * 8
tex = tc.Texture(
'bound', tex=tex, axis=2, bounds=(0.45, 0.55), outside_val=(0, 0, 0))
tex = tc.Texture(
'rotate', tex=tex, rotate_axis=1, rotate_times=1).translate((-0.4, -0.1,
0))

def frame_update(t, frame_dt):
if t < 0.3:
mpm.add_particles(
type='water',
gamma=1,
k=10000,
pd_source=True,
delta_t=frame_dt,
density_tex=tex.id,
initial_velocity=(2, 0, 0),)

mpm.simulate(frame_update=frame_update)

0 comments on commit b8690b6

Please sign in to comment.