Skip to content

Commit

Permalink
examples and frontend update
Browse files Browse the repository at this point in the history
  • Loading branch information
ryichsecondary committed Nov 29, 2024
1 parent 6aef160 commit 7d6221c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
51 changes: 34 additions & 17 deletions examples/frontend/_session_.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,14 @@ def init(self, scene: FixedScene) -> "Session":
self._save_func()
return self

def start(self, param: Param, force: bool = True) -> "Session":
self._check_ready()
def output_path(self) -> str:
return os.path.join(self._path, "output")

def export_shell_command(
self,
param: Param,
) -> str:
param.export(self._path)
if is_running():
if force:
terminate()
else:
display("Solver is already running. Teriminate first.")
display(self._terminate_button("Terminate Now"))
return self
program_path = os.path.join(
self._proj_root, "target", "release", "ppf-contact-solver"
)
Expand All @@ -254,19 +252,40 @@ def start(self, param: Param, force: bool = True) -> "Session":
[
program_path,
f"--path {self._path}",
f"--output {self._path}/output",
f"--output {self.output_path()}",
]
)
with open(os.path.join(self._path, "command.sh"), "w") as f:
path = os.path.join(self._path, "command.sh")
with open(path, "w") as f:
f.write(command)
os.chmod(os.path.join(self._path, "command.sh"), 0o755)
err_path = os.path.join(self._path, "error.log")
log_path = os.path.join(self._path, "stdout.log")
os.chmod(path, 0o755)
return path
else:
raise ValueError("Solver does not exist")

def start(self, param: Param, force: bool = True, blocking=False) -> "Session":
self._check_ready()
if is_running():
if force:
terminate()
else:
display("Solver is already running. Teriminate first.")
display(self._terminate_button("Terminate Now"))
return self
cmd_path = self.export_shell_command(param)
err_path = os.path.join(self._path, "error.log")
log_path = os.path.join(self._path, "stdout.log")
if blocking:
subprocess.run(cmd_path.split(), shell=True)
return self
else:
command = open(cmd_path, "r").read()
process = subprocess.Popen(
command.split(),
stdout=open(log_path, "w"),
stderr=open(err_path, "w"),
start_new_session=True,
cwd=self._proj_root,
)
if process.poll() is not None:
raise ValueError("Solver failed to start")
Expand All @@ -279,8 +298,6 @@ def start(self, param: Param, force: bool = True) -> "Session":
err_content = open(err_path, "r").readlines()
display_log(err_content)
raise ValueError("Solver failed to start")
else:
raise ValueError("Solver executable not found")

def get_number(self, name: str):
path = os.path.join(self._path, "output", "data", f"{name}.out")
Expand Down Expand Up @@ -638,7 +655,7 @@ def get_default_params(path: str) -> dict[str, Any]:
clap_match = re.match(r"clap\((.*?)\)", attr)
if clap_match:
args = clap_match.group(1)
arg_list = re.findall(r'(?:[^,"]|"(?:\\.|[^"])*")+', args)
arg_list = re.findall(r'(?:[^,"]|"(?:\\.|[^"\\])*")+', args)
for arg in arg_list:
arg = arg.strip()
if "=" in arg:
Expand Down
32 changes: 32 additions & 0 deletions examples/headless.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from frontend import App
import os

app = App("headless").clear()

V, F = app.mesh.square(res=64)
app.asset.add.tri("sheet", V, F)

V, F = app.mesh.icosphere(r=0.5, subdiv_count=4)
app.asset.add.tri("sphere", V, F)

scene = app.scene.create("five-curtains")

space = 0.25
for i in range(5):
obj = scene.add("sheet")
obj.at(i * space, 0, 0).rotate(90, "y")
obj.direction([0, 1, 0], [0, 0, 1])
obj.pin(obj.grab([0, 1, 0]))

scene.add("sphere").at(-1, 0, 0).pin().move_by([8, 0, 0], 5)
fixed = scene.build()

param = app.session.param()
param.set("friction", 0.0)
param.set("dt", 0.01)
param.set("min-newton-steps", 8)
param.set("frames", 60)

session = app.session.create("dt-001-newton-8").init(fixed).start(param, blocking=True)
assert os.path.exists(os.path.join(session.output_path(), "vert_60.bin"))

0 comments on commit 7d6221c

Please sign in to comment.