Skip to content

Commit

Permalink
add format check in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
YilingQiao committed Dec 19, 2024
1 parent da5db77 commit 4a3810f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 64 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install Dependencies
run: |
pip install black
- name: Black Format Check
run: black --check .

- name: Run Tests
run: |
Expand Down
37 changes: 20 additions & 17 deletions examples/smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

import genesis as gs


@ti.data_oriented
class Jet(object):
def __init__(
self,
world_center,
jet_radius,
orbit_radius,
orbit_radius_vel,
orbit_init_degree,
orbit_tau,
sub_orbit_radius,
sub_orbit_tau):
self,
world_center,
jet_radius,
orbit_radius,
orbit_radius_vel,
orbit_init_degree,
orbit_tau,
sub_orbit_radius,
sub_orbit_tau,
):
self.world_center = ti.Vector(world_center)
self.orbit_radius = orbit_radius
self.orbit_radius_vel = orbit_radius_vel
Expand All @@ -35,8 +37,7 @@ def __init__(
@ti.func
def get_pos(self, t: float):
rel_pos = ti.Vector([self.orbit_radius + t * self.orbit_radius_vel, 0.0, 0.0])
rot_mat = ti.math.rot_by_axis(
ti.Vector([0.0, 1.0, 0.0]), self.orbit_init_radian + t * self.orbit_tau)[:3, :3]
rot_mat = ti.math.rot_by_axis(ti.Vector([0.0, 1.0, 0.0]), self.orbit_init_radian + t * self.orbit_tau)[:3, :3]
rel_pos = rot_mat @ rel_pos
return rel_pos

Expand Down Expand Up @@ -73,9 +74,9 @@ def get_tan_dir(self, t: float):
def main():

########################## init ##########################
gs.init(seed=0, precision='32', logging_level='debug')
gs.init(seed=0, precision="32", logging_level="debug")

video_path = Path(__file__).parent / 'video'
video_path = Path(__file__).parent / "video"
video_path.mkdir(exist_ok=True, parents=True)

res = 384
Expand Down Expand Up @@ -109,7 +110,9 @@ def main():
orbit_tau=orbit_tau,
sub_orbit_radius=sub_orbit_radius,
jet_radius=jet_radius,
sub_orbit_tau=sub_orbit_tau) for orbit_init_degree in np.linspace(0, 360, 3, endpoint=False)
sub_orbit_tau=sub_orbit_tau,
)
for orbit_init_degree in np.linspace(0, 360, 3, endpoint=False)
]
scene.sim.solvers[-1].set_jets(jets)

Expand All @@ -122,16 +125,16 @@ def main():

for i in range(num_steps):

scalars = scene.sim.solvers[-1].grid.q.to_numpy().astype(np.float32) # (res, res, res, 3)
scalars = scene.sim.solvers[-1].grid.q.to_numpy().astype(np.float32) # (res, res, res, 3)
scalars[scalars < 1e-4] = 0
layer = scalars[:, res // 2, :]

rgb = (255 * layer).astype(np.uint8)
cv2.imwrite(str(video_path / f'{i:04d}.png'), cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR))
cv2.imwrite(str(video_path / f"{i:04d}.png"), cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR))

for _ in range(substeps):
scene.step()


if __name__ == '__main__':
if __name__ == "__main__":
main()
11 changes: 7 additions & 4 deletions genesis/engine/entities/sf_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@

@ti.data_oriented
class SFParticleEntity(ParticleEntity):
'''
"""
PBD-based entity represented solely by particles.
'''
"""

def __init__(self, scene, solver, material, morph, surface, particle_size, idx, particle_start):
super().__init__(scene, solver, material, morph, surface, particle_size, idx, particle_start, need_skinning=False)
super().__init__(
scene, solver, material, morph, surface, particle_size, idx, particle_start, need_skinning=False
)

def process_input(self, in_backward=False):
# TODO: implement this
Expand All @@ -31,5 +34,5 @@ def sample(self):
pass

def update_particles(self, particles):
self._particles = particles
self._particles = particles
self._n_particles = particles.shape[0]
2 changes: 1 addition & 1 deletion genesis/engine/materials/SF/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .base import Base
from .smoke import Smoke
from .smoke import Smoke
2 changes: 1 addition & 1 deletion genesis/engine/materials/SF/smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def __init__(self):

@property
def sampler(self):
return 'regular'
return "regular"
8 changes: 5 additions & 3 deletions genesis/engine/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,12 @@ def add_entity(

elif isinstance(material, (gs.materials.SF.Smoke)):
if surface.vis_mode is None:
surface.vis_mode = 'particle'
surface.vis_mode = "particle"

if surface.vis_mode not in ['particle']:
gs.raise_exception(f"Unsupported `surface.vis_mode` for material {material}: '{surface.vis_mode}'. Expected one of: ['particle', 'recon'].")
if surface.vis_mode not in ["particle"]:
gs.raise_exception(
f"Unsupported `surface.vis_mode` for material {material}: '{surface.vis_mode}'. Expected one of: ['particle', 'recon']."
)

elif isinstance(material, (gs.materials.PBD.Base, gs.materials.MPM.Base, gs.materials.SPH.Base)):
if surface.vis_mode is None:
Expand Down
51 changes: 25 additions & 26 deletions genesis/engine/solvers/sf_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@

@ti.data_oriented
class SFSolver(Solver):
'''
"""
Stable Fluid solver for eulerian-based gaseous simulation.
'''
"""

def __init__(self, scene, sim, options):
super().__init__(scene, sim, options)

if options is None:
return

self.n_grid = options.res
self.dx = 1 / self.n_grid
self.res = (self.n_grid, self.n_grid, self.n_grid)
self.solver_iters = options.solver_iters
self.decay = options.decay
self.n_grid = options.res
self.dx = 1 / self.n_grid
self.res = (self.n_grid, self.n_grid, self.n_grid)
self.solver_iters = options.solver_iters
self.decay = options.decay

self.t = 0.0
self.inlet_s = options.inlet_s
self.t = 0.0
self.inlet_s = options.inlet_s

self.jets = []

Expand All @@ -48,22 +48,21 @@ def is_active(self):

def setup_fields(self):
cell_state = ti.types.struct(
v = gs.ti_vec3,
v_tmp = gs.ti_vec3,
div = gs.ti_float,
p = gs.ti_float,
q = ti.types.vector(len(self.jets), gs.ti_float),
v=gs.ti_vec3,
v_tmp=gs.ti_vec3,
div=gs.ti_float,
p=gs.ti_float,
q=ti.types.vector(len(self.jets), gs.ti_float),
)

self.grid = cell_state.field(shape=self.res, layout=ti.Layout.SOA)
self.grid = cell_state.field(shape=self.res, layout=ti.Layout.SOA)

# swap area for pressure projection solver
self.p_swap = TexPair(
cur = ti.field(dtype=gs.ti_float, shape=self.res),
nxt = ti.field(dtype=gs.ti_float, shape=self.res),
cur=ti.field(dtype=gs.ti_float, shape=self.res),
nxt=ti.field(dtype=gs.ti_float, shape=self.res),
)


@ti.kernel
def init_fields(self):
for i, j, k in ti.ndrange(*self.res):
Expand Down Expand Up @@ -183,23 +182,22 @@ def compute_location(self, u, v, w, du, dv, dw):
I = max(0, min(self.n_grid - 1, I))
return I


@ti.func
def is_free(self, u, v, w, du, dv, dw):
flag = 1

I = ti.Vector([int(u+du), int(v+dv), int(w+dw)])
I = ti.Vector([int(u + du), int(v + dv), int(w + dw)])
if (I < 0).any() or (I > self.n_grid - 1).any():
flag = 0

return flag

@ti.func
def trilerp_scalar(self, qf, p, qf_idx):
'''
"""
p: position, within (0, 1).
qf: field for interpolation
'''
"""
# convert position to grid index
base_I = ti.floor(p - 0.5, gs.ti_int)
p_I = p - 0.5
Expand All @@ -219,10 +217,10 @@ def trilerp_scalar(self, qf, p, qf_idx):

@ti.func
def trilerp(self, qf, p):
'''
"""
p: position, within (0, 1).
qf: field for interpolation
'''
"""
# convert position to grid index
base_I = ti.floor(p - 0.5, gs.ti_int)
p_I = p - 0.5
Expand All @@ -243,9 +241,9 @@ def trilerp(self, qf, p):
# RK3
@ti.func
def backtrace(self, vf, p, dt):
'''
"""
vf: velocity field
'''
"""
v1 = self.trilerp(vf, p)
p1 = p - 0.5 * dt * v1
v2 = self.trilerp(vf, p1)
Expand All @@ -269,6 +267,7 @@ def set_state(self, f, state):
def reset_grad(self):
return None


class TexPair:
def __init__(self, cur, nxt):
self.cur = cur
Expand Down
25 changes: 13 additions & 12 deletions genesis/options/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,16 @@ class SFOptions(Options):
dt : float, optional
Time duration for each simulation step in seconds. If none, it will inherit from `SimOptions`. Defaults to None.
"""
dt : Optional[float] = None
res : Optional[int] = 128
solver_iters : Optional[int] = 500
decay : Optional[float] = 0.99

T_low : Optional[float] = 1.0
T_high : Optional[float] = 0.0

inlet_pos : Optional[tuple[int, int, int]] = (0.6, 0.0, 0.1)
inlet_vel : Optional[tuple[int, int, int]] = (0, 0, 1)
inlet_quat : Optional[tuple[int, int, int, int]] = (1, 0, 0, 0)
inlet_s : Optional[float] = 400.0

dt: Optional[float] = None
res: Optional[int] = 128
solver_iters: Optional[int] = 500
decay: Optional[float] = 0.99

T_low: Optional[float] = 1.0
T_high: Optional[float] = 0.0

inlet_pos: Optional[tuple[int, int, int]] = (0.6, 0.0, 0.1)
inlet_vel: Optional[tuple[int, int, int]] = (0, 0, 1)
inlet_quat: Optional[tuple[int, int, int, int]] = (1, 0, 0, 0)
inlet_s: Optional[float] = 400.0
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies = [
"pymeshlab",
"coacd",
"OpenEXR",
"black",
]

[tool.setuptools.packages.find]
Expand All @@ -48,6 +49,9 @@ genesis = [
"ext/VolumeSampling",
]

[tool.black]
line-length = 120

[project.scripts]
gs = "genesis._main:main"

Expand Down

0 comments on commit 4a3810f

Please sign in to comment.