Skip to main content

rapier2d/counters/
solver_counters.rs

1use crate::counters::Timer;
2use core::fmt::{Display, Formatter, Result};
3
4/// Performance counters related to constraints resolution.
5#[derive(Default, Clone, Copy, Debug)]
6pub struct SolverCounters {
7    /// Number of contact manifolds and impulse joints handed to the constraints solver (summed
8    /// over the CCD substeps). Sleeping bodies' contacts and joints aren't counted.
9    pub nconstraints: usize,
10    /// Number of contact points handed to the constraints solver, soft-body contacts included
11    /// (summed over the CCD substeps).
12    pub ncontacts: usize,
13    /// Time spent for the resolution of the constraints (force computation).
14    pub velocity_resolution_time: Timer,
15    /// Time spent for the assembly of all the velocity constraints.
16    pub velocity_assembly_time: Timer,
17    /// Time spent by the velocity assembly for initializing solver bodies.
18    pub velocity_assembly_time_solver_bodies: Timer,
19    /// Time spent by the velocity assemble for initializing the constraints.
20    pub velocity_assembly_time_constraints_init: Timer,
21    /// Time spent for the update of the velocity of the bodies.
22    pub velocity_update_time: Timer,
23    /// Time spent to write force back to user-accessible data.
24    pub velocity_writeback_time: Timer,
25}
26
27impl SolverCounters {
28    /// Creates a new counter initialized to zero.
29    pub fn new() -> Self {
30        SolverCounters {
31            nconstraints: 0,
32            ncontacts: 0,
33            velocity_assembly_time: Timer::new(),
34            velocity_assembly_time_solver_bodies: Timer::new(),
35            velocity_assembly_time_constraints_init: Timer::new(),
36            velocity_resolution_time: Timer::new(),
37            velocity_update_time: Timer::new(),
38            velocity_writeback_time: Timer::new(),
39        }
40    }
41
42    /// Reset all the counters to zero.
43    pub fn reset(&mut self) {
44        self.nconstraints = 0;
45        self.ncontacts = 0;
46        self.velocity_resolution_time.reset();
47        self.velocity_assembly_time.reset();
48        self.velocity_assembly_time_solver_bodies.reset();
49        self.velocity_assembly_time_constraints_init.reset();
50        self.velocity_update_time.reset();
51        self.velocity_writeback_time.reset();
52    }
53}
54
55impl Display for SolverCounters {
56    fn fmt(&self, f: &mut Formatter) -> Result {
57        writeln!(f, "Number of contacts: {}", self.ncontacts)?;
58        writeln!(f, "Number of constraints: {}", self.nconstraints)?;
59        writeln!(f, "Velocity assembly time: {}", self.velocity_assembly_time)?;
60        writeln!(
61            f,
62            "Velocity resolution time: {}",
63            self.velocity_resolution_time
64        )?;
65        writeln!(f, "Velocity update time: {}", self.velocity_update_time)?;
66        writeln!(
67            f,
68            "Velocity writeback time: {}",
69            self.velocity_writeback_time
70        )
71    }
72}