salva2d/geometry/
contact_manager.rs

1use crate::counters::Counters;
2use crate::geometry::{self, HGrid, HGridEntry, ParticlesContacts};
3use crate::math::Real;
4use crate::object::Boundary;
5use crate::object::Fluid;
6
7/// Structure responsible for computing and grouping all the contact between fluid and boundary particles.
8pub struct ContactManager {
9    /// All contacts detected between pairs of fluid partices.
10    pub fluid_fluid_contacts: Vec<ParticlesContacts>,
11    /// All contacts detected between a fluid particle and a boundary particle.
12    pub fluid_boundary_contacts: Vec<ParticlesContacts>,
13    /// All contacts detected between two boundary particles.
14    pub boundary_boundary_contacts: Vec<ParticlesContacts>,
15}
16
17impl ContactManager {
18    /// Create a new contact manager.
19    pub fn new() -> Self {
20        Self {
21            fluid_fluid_contacts: Vec::new(),
22            fluid_boundary_contacts: Vec::new(),
23            boundary_boundary_contacts: Vec::new(),
24        }
25    }
26
27    /// The total number of contacts detected by this manager.
28    ///
29    /// Note that there will be two contact for each pair of distinct particles.
30    pub fn ncontacts(&self) -> usize {
31        self.fluid_fluid_contacts
32            .iter()
33            .map(|c| c.len())
34            .sum::<usize>()
35            + self
36                .fluid_boundary_contacts
37                .iter()
38                .map(|c| c.len())
39                .sum::<usize>()
40            + self
41                .boundary_boundary_contacts
42                .iter()
43                .map(|c| c.len())
44                .sum::<usize>()
45    }
46
47    /// Computes all the contacts between the particles inserted on the provided spacial grid.
48    pub fn update_contacts(
49        &mut self,
50        counters: &mut Counters,
51        h: Real,
52        fluids: &[Fluid],
53        boundaries: &[Boundary],
54        hgrid: &HGrid<HGridEntry>,
55    ) {
56        geometry::compute_contacts(
57            counters,
58            h,
59            &fluids,
60            &boundaries,
61            &mut self.fluid_fluid_contacts,
62            &mut self.fluid_boundary_contacts,
63            &mut self.boundary_boundary_contacts,
64            hgrid,
65        );
66    }
67}