Skip to content

Commit

Permalink
Merge pull request #14 from GazzolaLab/example-euler
Browse files Browse the repository at this point in the history
Example Case: Rendering spring action for rigid rod
  • Loading branch information
skim0119 authored Jun 3, 2024
2 parents db8921e + 1437d22 commit 165522a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 151 deletions.
151 changes: 0 additions & 151 deletions Blender/rod_sim.py

This file was deleted.

55 changes: 55 additions & 0 deletions examples/single_rigid_rod_spring_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import numpy as np

import bsr

bsr.clear_mesh_objects()

# Environment configuration
k = 5 # spring constant


def f(x):
y, v = x[0], x[1]
return np.array([v, (-1) * (k) * (y)])


def euler_forward_step(x, f, dt):
return x + f(x) * dt


# Rod configuration
N = 5
velocity = np.arange(20, 45, N) # initial z-velocities
position = np.stack(
[
np.zeros(N), # x
np.zeros(N), # y
np.linspace(0, 10, N), # z
],
axis=0,
) # (3, 5)
radius = 0.2 * np.ones(N) # radius of the rod

rod = bsr.rod()
rod.update(keyframe=0, positions=position, radius=radius)


####### SIMULATION ########
# Simulation parameters

dt = 10 ** (-3)
framerate = 25
simulation_ratio = int(1 / framerate / dt)
time = np.arange(0, 10, dt)

# Euler-Forward time stepper
for time_index, t in enumerate(time[:-1]):
state = np.stack([position[2], velocity], axis=0)
position[2] = euler_forward_step(state, f, dt)

if time_index % simulation_ratio == 0:
# update the rod
keyframe = int(time_index / simulation_ratio) + 1
rod.update(keyframe=keyframe, positions=position, radius=radius)

bsr.save("single_rigid_rod_spring_action.blend")
2 changes: 2 additions & 0 deletions src/bsr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
from importlib import metadata as importlib_metadata

from .macros import *


def get_version() -> str:
try:
Expand Down
10 changes: 10 additions & 0 deletions src/bsr/macros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
__all__ = ["clear_mesh_objects"]

import bpy


def clear_mesh_objects() -> None:
# Clear existing mesh objects in the scene
bpy.ops.object.select_all(action="DESELECT")
bpy.ops.object.select_by_type(type="MESH")
bpy.ops.object.delete()

0 comments on commit 165522a

Please sign in to comment.