بنقرة واحدة
simulation-loop
Run the time-stepping loop, collect data, and post-process results with matplotlib or CSV.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Run the time-stepping loop, collect data, and post-process results with matplotlib or CSV.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Entry point for FSI-coupled hybrid plans (plan_type=fsi_in_scene) — any scene combining SPH fluid (water tanks, dam-break, wave channels) with multi-body dynamics, optionally with a wheeled vehicle. Pick this over mbs_in_scene whenever the plan involves a fluid domain (scene_objects with domain_type starting "sph_") or an FSI body registration (scene_objects with fsi_registration set). Routes to fsi/sph (always), veh/wheeled_vehicle (when vehicle present), and enforces FSI-specific invariants distinct from generic mbs_in_scene rigid scenes.
Entry point for rigid-body hybrid plans (plan_type=mbs_in_scene) combining a robot or vehicle with scene assets — NO fluid coupling. Routes to the correct domain skills and defines only high-level invariants. Use core/fsi_in_scene instead when the plan involves SPH fluid or FSI body registration.
Entry point for pure multi-body simulation plans (plan_type=mbs). Routes the agent to the correct mechanics, system, and camera skills and defines only high-level invariants.
Entry point for static scene plans (plan_type=scene). Routes the agent to the correct scene, system, and camera skills and defines only high-level invariants.
Set up SPH-based Fluid-Structure Interaction (FSI): create ChFsiFluidSystemSPH and ChFsiSystemSPH, configure fluid and SPH parameters, seed fluid particles with hydrostatic initialization, add container BCE boundary markers, register floating rigid bodies, optionally couple with a wheeled vehicle, and advance with sysFSI.DoStepDynamics(dT).
Translate a `geometry_relations` entry from the plan into correct PyChrono coordinate code. Read this whenever the plan declares a relation_name you have not previously encoded, and especially BEFORE writing SetPos / camera placement for any body that participates in a multi-body geometric constraint. Each subsection below is one canonical pattern named exactly as it appears in `plan.geometry_relations[i].relation_name`.
| name | simulation_loop |
| description | Run the time-stepping loop, collect data, and post-process results with matplotlib or CSV. |
| compatibility | pychrono >= 8.0 |
| metadata | {"domain":"mbs"} |
allowed_methods:
canonical_examples:
Run the time-stepping loop, collect data, and post-process results with matplotlib or CSV output.
After setting up the system, bodies, joints, and visualization — to advance the simulation in time and optionally record outputs.
dt = float # time step [s]
while vis.Run():
vis.BeginScene()
vis.Render()
vis.EndScene()
sys.DoStepDynamics(dt)
Timestep is set per-call, NOT on the system object:
dt = 0.001 # time step [s]
while vis.Run():
sys.DoStepDynamics(dt) # dt passed as argument to the stepping call
Common solver/timestepper settings (if needed):
sys.SetTimestepperType(chrono.ChTimestepper.Type_EULER) # default
# Other types: Type_BDF, Type_RK45, etc.
| Scenario | Typical dt |
|---|---|
| High-precision mechanisms | 1e-3 (1 ms) |
| General MBS | 5e-3 (5 ms) |
| Collision-heavy scenes | 0.02 (20 ms) |
| SMC soft contacts | 1e-4 (0.1 ms) |
duration = float # simulation duration [s]
if sys.GetChTime() > duration:
vis.GetDevice().closeDevice()
body.SetPos(chrono.ChVector3d(x, y, z)) # position
body.SetRot(chrono.QuatFromAngleZ(angle)) # orientation
body.SetPosDt(chrono.ChVector3d(vx, vy, vz)) # linear velocity
body.SetLinVel(chrono.ChVector3d(vx, vy, vz)) # linear velocity (alias for SetPosDt)
body.SetAngVelParent(chrono.ChVector3d(wx, wy, wz)) # angular velocity in world frame
body.GetPos() # ChVector3d position
body.GetPosDt() # ChVector3d velocity
body.GetLinVel() # ChVector3d velocity (alias for GetPosDt)
body.GetRot() # ChQuaterniond orientation
body.GetAngVelParent() # ChVector3d angular velocity in world frame
# Rotational motors (ChLinkMotorRotation*)
motor.GetMotorAngle() # integrated angle [rad]
motor.GetMotorAngleDt() # angular speed [rad/s]
motor.GetMotorAngleDt2() # angular acceleration [rad/s²]
# Linear motors (ChLinkMotorLinear*)
motor.GetMotorPos() # linear position [m]
motor.GetMotorPosDt() # linear speed [m/s]
motor.GetMotorPosDt2() # linear acceleration [m/s²]
spring.GetLength() # current length [m]
spring.GetVelocity() # extension rate [m/s]
spring.GetForce() # current force [N]
Initialize lists before the loop, append inside:
dt = float # time step [s]
duration = float # simulation duration [s]
array_time = []
array_angle = []
array_pos = []
array_speed = []
while vis.Run():
array_time.append(sys.GetChTime())
array_angle.append(motor.GetMotorAngle())
array_pos.append(piston.GetPos().x)
array_speed.append(piston.GetPosDt().x)
vis.BeginScene()
vis.Render()
vis.EndScene()
sys.DoStepDynamics(dt)
if sys.GetChTime() > duration:
vis.GetDevice().closeDevice()
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(2, sharex=True)
ax1.plot(array_angle, array_pos)
ax1.set(ylabel='position [m]')
ax1.grid()
ax2.plot(array_angle, array_speed, 'r--')
ax2.set(ylabel='speed [m/s]', xlabel='angle [rad]')
ax2.grid()
# Format x-axis in multiples of π
plt.xticks(np.linspace(0, 2 * np.pi, 5),
['0', r'$\pi/2$', r'$\pi$', r'$3\pi/2$', r'$2\pi$'])
plt.show()
import csv
dt = float # time step [s]
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['time', 'pos_x', 'vel_x']) # header
while vis.Run():
t = sys.GetChTime()
writer.writerow([t, body.GetPos().x, body.GetPosDt().x])
vis.BeginScene()
vis.Render()
vis.EndScene()
sys.DoStepDynamics(dt)
dt = float # time step [s]
frame = 0
while vis.Run():
vis.BeginScene()
vis.Render()
vis.EndScene()
sys.DoStepDynamics(dt)
if frame % 50 == 0:
print(f"t={sys.GetChTime():.4f} L={spring.GetLength():.4f} F={spring.GetForce():.4f}")
frame += 1
dt = float # physics time step [s]
fps = int # render frames per second
out_step = 1.0 / fps
out_time = 0.0
while vis.Run():
sys.DoStepDynamics(dt)
if sys.GetChTime() >= out_time:
vis.BeginScene()
vis.Render()
vis.EndScene()
out_time += out_step