Skip to content

First Simulation

Simulate a falling particle with ground contact on CUDA. This program needs no window, external models, or textures, making it a useful first check of installation and authentication.

1. Check the Environment

Complete Installation and Authentication. Confirm that _core points to the current build and warp.is_cuda_available() is True. Run the commands below from the repository root.

2. Run the Complete Program

The repository includes first_simulation.py:

uv run python docs/samples/first_simulation.py

Without credential variables, it first asks for your account and password. It prints a position every 30 frames and finishes with:

Simulation completed: 120 frames

Warp may compile kernels on the first run and reuse its cache afterwards. Exact positions depend on configuration and version; decimal values are not a cross-version acceptance criterion.

3. Complete Code

"""Run a small headless Newton scene through Style3D Sim."""

import numpy as np
import newton
import warp as wp

import style3d_sim as s3d


def main() -> None:
    wp.init()
    if not wp.is_cuda_available():
        raise RuntimeError("This sample requires a CUDA-capable device")

    builder = s3d.ModelBuilder(up_axis=newton.Axis.Z)
    builder.add_ground_plane()
    builder.add_particle(pos=(0.0, 0.0, 1.0), vel=(0.0, 0.0, 0.0), mass=1.0, radius=0.05)
    model = builder.finalize(device="cuda:0")
    solver = s3d.NewtonSolver(model)
    solver.config.nonlinear_solver = s3d.NonlinearSolver.NEWTON
    solver.config.nonlinear_iterations = 4
    solver.config.linear_iterations = 50

    # Allocate states after the solver has finalized any affine particle proxies.
    state, following = model.state(), model.state()
    control = model.control()
    pipeline = newton.CollisionPipeline(model)
    contacts = pipeline.contacts()
    frame_dt = 1.0 / 60.0
    substeps = 4
    dt = frame_dt / substeps

    for frame in range(120):
        for _ in range(substeps):
            state.clear_forces()
            pipeline.collide(state, contacts)
            solver.step(state, following, control, contacts, dt)
            state, following = following, state
        if frame % 30 == 0:
            position = state.particle_q.numpy()[0]
            print(f"t={(frame + 1) * frame_dt:.3f}s, particle position={position}")

    wp.synchronize()
    assert np.isfinite(state.particle_q.numpy()).all(), "Non-finite particle position"
    print("Simulation completed: 120 frames")


if __name__ == "__main__":
    main()

4. What Each Step Does

Step Purpose
wp.init() Initialize Warp and detect devices
s3d.ModelBuilder() Prepare scene objects and contact materials
add_particle() Set position, velocity, mass, and contact radius
finalize(device="cuda:0") Allocate the model on the GPU
s3d.NewtonSolver(model) Authenticate and initialize the adapter
Two calls to model.state() Create input and output buffers
CollisionPipeline Generate contacts between particles and shapes
step() Advance one substep using those contacts
Swap the states Use the latest output as the next input

The frame rate is 60 Hz with four substeps per frame, so step() receives dt=1/240 seconds. Collision detection runs on every substep to generate ground contacts from the current state.

5. Open a Viewer

Try the existing rigid body scene:

uv run python examples/example_basic_shapes.py --solver synreal --device cuda:0

These examples need the examples extra and a graphical environment; some assets download on first use. Without a display, continue with this page's script or use --viewer null where supported.

6. Next Steps

Change the initial height, radius, and substep count to observe the landing behavior. Then read Models, States, and Simulation Loop to add a viewer, apply forces, and reset the scene.