Skip to main content

rapier2d/dynamics/soft_body/
soft_body_settings.rs

1//! Simulation settings shared by every soft body of a world, grouped on
2//! [`IntegrationParameters::soft_bodies`](crate::dynamics::IntegrationParameters::soft_bodies).
3
4use super::SoftRecoverySettings;
5use crate::math::Real;
6
7/// Simulation settings shared by every soft body of a world.
8///
9/// Lives on [`IntegrationParameters::soft_bodies`], so it can be changed between steps.
10///
11/// [`IntegrationParameters::soft_bodies`]: crate::dynamics::IntegrationParameters::soft_bodies
12#[derive(Copy, Clone, Debug, PartialEq)]
13#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
14#[cfg_attr(feature = "serde-serialize", serde(default))]
15pub struct SoftBodiesSettings {
16    /// Runtime toggles and tuning for the soft-body tangle detection and recovery stack
17    /// (see [`SoftRecoverySettings`]). Every mechanism can be switched off individually.
18    pub recovery: SoftRecoverySettings,
19    /// Strain beyond which a soft-body constraint (an elastic cell's strain component, a distance
20    /// constraint's relative error) is re-solved after the contacts inside every substep, so a
21    /// light body buried under heavier ones is not torn (default: `0.75`; `Real::MAX` to disable).
22    pub resweep_strain: Real,
23    /// Maximum number of extra substeps a soft body requests for its island component while it
24    /// is hit fast (default: `4`; `0` disables the impact-adaptive substeps): enough substeps to
25    /// bound the per-substep travel of its particles and approaching rigid bodies by one radius.
26    pub max_extra_substeps: usize,
27    /// Factor applied to the [`IntegrationParameters`](crate::dynamics::IntegrationParameters)
28    /// contact softness natural frequencies for the soft-body contacts (default: `4.0`); they run
29    /// stiffer because a contact's effective mass is a few particles' worth, not the whole body's.
30    pub contact_stiffening: Real,
31    /// Tuning of the FEM soft-body solver (see
32    /// [`SoftBodySolver::Fem`](crate::dynamics::SoftBodySolver::Fem)); only read by the soft
33    /// bodies that select it.
34    #[cfg(feature = "fem")]
35    pub fem: SoftFemParameters,
36}
37
38impl Default for SoftBodiesSettings {
39    fn default() -> Self {
40        Self {
41            recovery: SoftRecoverySettings::default(),
42            resweep_strain: 0.75,
43            max_extra_substeps: 4,
44            contact_stiffening: 4.0,
45            #[cfg(feature = "fem")]
46            fem: SoftFemParameters::default(),
47        }
48    }
49}
50
51/// Tuning of the FEM soft-body solver (see
52/// [`SoftBodySolver::Fem`](crate::dynamics::SoftBodySolver::Fem)), which solves a linear system
53/// per substep by conjugate gradient and factorizes it once per step (see `max_dense_dofs`).
54#[cfg(feature = "fem")]
55#[derive(Copy, Clone, Debug, PartialEq)]
56#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
57pub struct SoftFemParameters {
58    /// Relative residual at which the conjugate gradient stops (default: `1.0e-5`).
59    pub linear_tolerance: Real,
60    /// Hard cap on the conjugate-gradient iterations, whatever the residual (default: `20`).
61    ///
62    /// A very stiff, finely meshed body can need several hundred iterations to converge; the
63    /// truncated step it gets instead is under-relaxed (safe), only slower to settle.
64    pub max_linear_iterations: usize,
65    /// Largest number of degrees of freedom (`DIM` per particle) for which the step-start
66    /// matrix `A` of a body is factorized directly (default: `600`). Each constraint on a FEM body
67    /// needs one solve against `A` per step; larger bodies fall back to a conjugate gradient.
68    pub max_dense_dofs: usize,
69}
70
71#[cfg(feature = "fem")]
72impl Default for SoftFemParameters {
73    fn default() -> Self {
74        Self {
75            linear_tolerance: 1.0e-5,
76            max_linear_iterations: 20,
77            max_dense_dofs: 600,
78        }
79    }
80}