rapier3d/counters/
solver_counters.rs

1use crate::counters::Timer;
2use std::fmt::{Display, Formatter, Result};
3
4/// Performance counters related to constraints resolution.
5#[derive(Default, Clone, Copy)]
6pub struct SolverCounters {
7    /// Number of constraints generated.
8    pub nconstraints: usize,
9    /// Number of contacts found.
10    pub ncontacts: usize,
11    /// Time spent for the resolution of the constraints (force computation).
12    pub velocity_resolution_time: Timer,
13    /// Time spent for the assembly of all the velocity constraints.
14    pub velocity_assembly_time: Timer,
15    /// Time spent for the update of the velocity of the bodies.
16    pub velocity_update_time: Timer,
17    /// Time spent to write force back to user-accessible data.
18    pub velocity_writeback_time: Timer,
19}
20
21impl SolverCounters {
22    /// Creates a new counter initialized to zero.
23    pub fn new() -> Self {
24        SolverCounters {
25            nconstraints: 0,
26            ncontacts: 0,
27            velocity_assembly_time: Timer::new(),
28            velocity_resolution_time: Timer::new(),
29            velocity_update_time: Timer::new(),
30            velocity_writeback_time: Timer::new(),
31        }
32    }
33
34    /// Reset all the counters to zero.
35    pub fn reset(&mut self) {
36        self.nconstraints = 0;
37        self.ncontacts = 0;
38        self.velocity_resolution_time.reset();
39        self.velocity_assembly_time.reset();
40        self.velocity_update_time.reset();
41        self.velocity_writeback_time.reset();
42    }
43}
44
45impl Display for SolverCounters {
46    fn fmt(&self, f: &mut Formatter) -> Result {
47        writeln!(f, "Number of contacts: {}", self.ncontacts)?;
48        writeln!(f, "Number of constraints: {}", self.nconstraints)?;
49        writeln!(f, "Velocity assembly time: {}", self.velocity_assembly_time)?;
50        writeln!(
51            f,
52            "Velocity resolution time: {}",
53            self.velocity_resolution_time
54        )?;
55        writeln!(f, "Velocity update time: {}", self.velocity_update_time)?;
56        writeln!(
57            f,
58            "Velocity writeback time: {}",
59            self.velocity_writeback_time
60        )
61    }
62}