Skip to main content

rapier2d/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 by the velocity assembly for initializing solver bodies.
16    pub velocity_assembly_time_solver_bodies: Timer,
17    /// Time spent by the velocity assemble for initializing the constraints.
18    pub velocity_assembly_time_constraints_init: Timer,
19    /// Time spent for the update of the velocity of the bodies.
20    pub velocity_update_time: Timer,
21    /// Time spent to write force back to user-accessible data.
22    pub velocity_writeback_time: Timer,
23}
24
25impl SolverCounters {
26    /// Creates a new counter initialized to zero.
27    pub fn new() -> Self {
28        SolverCounters {
29            nconstraints: 0,
30            ncontacts: 0,
31            velocity_assembly_time: Timer::new(),
32            velocity_assembly_time_solver_bodies: Timer::new(),
33            velocity_assembly_time_constraints_init: Timer::new(),
34            velocity_resolution_time: Timer::new(),
35            velocity_update_time: Timer::new(),
36            velocity_writeback_time: Timer::new(),
37        }
38    }
39
40    /// Reset all the counters to zero.
41    pub fn reset(&mut self) {
42        self.nconstraints = 0;
43        self.ncontacts = 0;
44        self.velocity_resolution_time.reset();
45        self.velocity_assembly_time.reset();
46        self.velocity_assembly_time_solver_bodies.reset();
47        self.velocity_assembly_time_constraints_init.reset();
48        self.velocity_update_time.reset();
49        self.velocity_writeback_time.reset();
50    }
51}
52
53impl Display for SolverCounters {
54    fn fmt(&self, f: &mut Formatter) -> Result {
55        writeln!(f, "Number of contacts: {}", self.ncontacts)?;
56        writeln!(f, "Number of constraints: {}", self.nconstraints)?;
57        writeln!(f, "Velocity assembly time: {}", self.velocity_assembly_time)?;
58        writeln!(
59            f,
60            "Velocity resolution time: {}",
61            self.velocity_resolution_time
62        )?;
63        writeln!(f, "Velocity update time: {}", self.velocity_update_time)?;
64        writeln!(
65            f,
66            "Velocity writeback time: {}",
67            self.velocity_writeback_time
68        )
69    }
70}