Skip to main content

Crate phyz_diff

Crate phyz_diff 

Source
Expand description

Differentiation utilities for phyz: Jacobians of the equations of motion.

Three routines compute the same object — the Jacobians of one semi-implicit Euler step — by different means, trading accuracy against cost:

FunctionMethodExact?
finite_diff_jacobianscentral differences over the whole stepno
semi_implicit_step_jacobianschain rule through the integrator, central differences on ABAno (ABA block is FD)
symbolic::symbolic_step_jacobianssymbolic differentiation of the dynamicsyes

Only the symbolic path is derivative-exact. The other two are finite differences and inherit the usual step-size/round-off tradeoff. For exact parameter and contact-surface gradients over a whole trajectory, see the dual-number adjoint in phyz::diff instead.

§Example

use phyz_diff::semi_implicit_step_jacobians;
use phyz_math::{GRAVITY, Mat3, SpatialInertia, SpatialTransform, Vec3};
use phyz_model::ModelBuilder;

let model = ModelBuilder::new()
    .gravity(Vec3::new(0.0, -GRAVITY, 0.0))
    .dt(0.002)
    .add_revolute_body(
        "pendulum",
        -1,
        SpatialTransform::identity(),
        SpatialInertia::new(
            1.0,
            Vec3::new(0.0, -0.5, 0.0),
            Mat3::from_diagonal(&Vec3::new(0.083, 0.0, 0.083)),
        ),
    )
    .build();

let mut state = model.default_state();
state.q[0] = 0.3;

let jac = semi_implicit_step_jacobians(&model, &state);
assert_eq!(jac.dvnext_dq.nrows(), model.nv);

Re-exports§

pub use rollout::AdjointGradients;
pub use rollout::AdjointRollout;
pub use rollout::CollisionMesh;
pub use rollout::ContactSetup;
pub use rollout::FinalStateObjective;
pub use rollout::GroundContact;
pub use rollout::N_INERTIA_PARAMS;
pub use rollout::adjoint_rollout_gradient;
pub use rollout::inertia_params;
pub use rollout::rollout_objective;
pub use contact_adjoint::ConvexAdjointError;
pub use contact_adjoint::ConvexAdjointGradients;
pub use contact_adjoint::ConvexContactRollout;
pub use contact_adjoint::convex_adjoint_gradient;
pub use contact_adjoint::convex_rollout_objective;

Modules§

contact_adjoint
Trajectory adjoint through the convex contact solve — the same contact model the forward simulator runs.
multidual
Vector-mode forward AD: one pass through the dynamics, N derivatives out.
rollout
Differentiable rollouts: exact parameter and contact-surface adjoints.
symbolic
Symbolic Jacobian computation via tang-expr.

Structs§

StepJacobians
Jacobians of a single simulation step.

Functions§

analytical_step_jacobiansDeprecated
Deprecated alias for semi_implicit_step_jacobians.
finite_diff_jacobians
Compute step Jacobians via finite differences.
semi_implicit_step_jacobians
Compute step Jacobians for semi-implicit Euler by chain rule through the integrator.