1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use crate::counters::Counters;
use crate::geometry::HGrid;
use crate::math::{Point, Real, Vector};
use crate::object::Boundary;
use crate::object::Fluid;

use std::sync::RwLock;

#[cfg(feature = "parallel")]
use rayon::prelude::*;

#[derive(Copy, Clone, Debug)]
/// A particle inserted on a spacial grid.
pub enum HGridEntry {
    /// A fluid particle with its fluid ID and particle ID.
    FluidParticle(usize, usize),
    /// A fluid particle with its boundary ID and particle ID.
    BoundaryParticle(usize, usize),
}

impl HGridEntry {
    /// Returns (object ID, particle ID, is_boundary).
    ///
    /// The last tuple entry is `true` if this is a fluid particle, or `false` if it is a boundary particle.
    pub fn into_tuple(self) -> (usize, usize, bool) {
        match self {
            HGridEntry::FluidParticle(a, b) => (a, b, false),
            HGridEntry::BoundaryParticle(a, b) => (a, b, true),
        }
    }
}

#[derive(Copy, Clone, Debug)]
/// A contact between two particles.
///
/// If the contact is between two fluid particles, it is assumed "one-way", i.e., this contact can
/// only result in a force applied by the particle `j` to the particle `i`. The force applied by
/// `i` on `j` will result from another contacts.
/// In other words, for each par of distinct fluid particles, there will be be two symmetric contacts.
pub struct Contact {
    /// The index of the first particle involved in this contact.
    pub i: usize,
    /// The index of the first fluid involved in this contact.
    pub i_model: usize,
    /// The index of the second particle involved in this contact.
    pub j: usize,
    /// The index of the second fluid boundary involved in this contact.
    pub j_model: usize,
    /// The kernel evaluated at `xi - xj` where `xi` is the position of the
    /// particle `i`, and `xj` is the position of the particle `j`.
    pub weight: Real,
    /// The kernel gradient evaluated at `xi - xj` where `xi` is the position of the
    /// particle `i`, and `xj` is the position of the particle `j`.
    pub gradient: Vector<Real>,
}

impl Contact {
    /// Flips this contact by swapping `i` with `j`, `i_model` with `j_model`, and by negating the gradient.
    pub fn flip(&self) -> Self {
        Self {
            i: self.j,
            i_model: self.j_model,
            j: self.i,
            j_model: self.i_model,
            weight: self.weight,
            gradient: -self.gradient,
        }
    }

    /// Returns `true` if this contact involves a single particle with itself.
    pub fn is_same_particle_contact(&self) -> bool {
        self.i_model == self.j_model && self.i == self.j
    }

    /// Returns `true` if this contact involves two particles from the same fluid.
    pub fn is_same_model_contact(&self) -> bool {
        self.i_model == self.j_model
    }
}

#[derive(Debug)]
/// The set of contacts affecting the particles of a single fluid.
pub struct ParticlesContacts {
    // All the particle contact for one model.
    // `self.contacts[i]` contains all the contacts involving the particle `i`.
    contacts: Vec<RwLock<Vec<Contact>>>,
}

impl ParticlesContacts {
    /// Creates an empty set of contacts.
    pub fn new() -> Self {
        Self {
            contacts: Vec::new(),
        }
    }

    /// The set of contacts affecting the particle `i`.
    pub fn particle_contacts(&self, i: usize) -> &RwLock<Vec<Contact>> {
        &self.contacts[i]
    }

    /// The set of mutable contacts affecting the particle `i`.
    pub fn particle_contacts_mut(&mut self, i: usize) -> &mut RwLock<Vec<Contact>> {
        &mut self.contacts[i]
    }

    /// All the contacts in this set.
    ///
    /// The `self.contacts()[i]` contains all the contact affecting the particle `i`.
    pub fn contacts(&self) -> &[RwLock<Vec<Contact>>] {
        &self.contacts[..]
    }

    /// All the mutable contacts in this set.
    ///
    /// The `self.contacts()[i]` contains all the contact affecting the particle `i`.
    pub fn contacts_mut(&mut self) -> &mut [RwLock<Vec<Contact>>] {
        &mut self.contacts[..]
    }

    /// The total number of contacts in this set.
    pub fn len(&self) -> usize {
        self.contacts.iter().map(|c| c.read().unwrap().len()).sum()
    }

    /// Apply a permutation to this set of contacts.
    pub fn apply_permutation(&mut self, _permutation: &[usize]) {
        unimplemented!()
    }
}

/// Insert all the particles from the given fluids into the `grid`.
pub fn insert_fluids_to_grid(fluids: &[Fluid], grid: &mut HGrid<HGridEntry>) {
    for (fluid_id, fluid) in fluids.iter().enumerate() {
        for (particle_id, point) in fluid.positions.iter().enumerate() {
            grid.insert(&point, HGridEntry::FluidParticle(fluid_id, particle_id));
        }
    }
}

/// Insert all the particles from the given boundaries into the `grid`.
pub fn insert_boundaries_to_grid(boundaries: &[Boundary], grid: &mut HGrid<HGridEntry>) {
    for (boundary_id, boundary) in boundaries.iter().enumerate() {
        for (particle_id, point) in boundary.positions.iter().enumerate() {
            grid.insert(
                &point,
                HGridEntry::BoundaryParticle(boundary_id, particle_id),
            );
        }
    }
}

/// Compute all the contacts between the particles inserted in `grid`.
pub fn compute_contacts(
    counters: &mut Counters,
    h: Real,
    fluids: &[Fluid],
    boundaries: &[Boundary],
    fluid_fluid_contacts: &mut Vec<ParticlesContacts>,
    fluid_boundary_contacts: &mut Vec<ParticlesContacts>,
    boundary_boundary_contacts: &mut Vec<ParticlesContacts>,
    grid: &HGrid<HGridEntry>,
) {
    // Needed so the loop in -1..=1 bellow works.
    assert_eq!(h, grid.cell_width());
    counters.cd.neighborhood_search_time.resume();

    fluid_fluid_contacts.resize_with(fluids.len(), || ParticlesContacts::new());
    fluid_boundary_contacts.resize_with(fluids.len(), || ParticlesContacts::new());
    boundary_boundary_contacts.resize_with(boundaries.len(), || ParticlesContacts::new());

    for (fluid, contacts) in fluids.iter().zip(fluid_fluid_contacts.iter_mut()) {
        contacts
            .contacts
            .iter_mut()
            .for_each(|c| c.write().unwrap().clear());
        contacts
            .contacts
            .resize_with(fluid.num_particles(), || RwLock::new(Vec::new()))
    }

    for (fluid, contacts) in fluids.iter().zip(fluid_boundary_contacts.iter_mut()) {
        contacts
            .contacts
            .iter_mut()
            .for_each(|c| c.write().unwrap().clear());
        contacts
            .contacts
            .resize_with(fluid.num_particles(), || RwLock::new(Vec::new()))
    }

    for (boundary, contacts) in boundaries.iter().zip(boundary_boundary_contacts.iter_mut()) {
        contacts
            .contacts
            .iter_mut()
            .for_each(|c| c.write().unwrap().clear());
        contacts
            .contacts
            .resize_with(boundary.num_particles(), || RwLock::new(Vec::new()))
    }

    #[cfg(feature = "dim2")]
    let neighbours: [(i64, i64); 5] = [(0, 0), (0, 1), (1, -1), (1, 0), (1, 1)];
    #[cfg(feature = "dim3")]
    let neighbours: [(i64, i64, i64); 14] = [
        (0, 0, 0),
        (0, 0, 1),
        (0, 1, -1),
        (0, 1, 0),
        (0, 1, 1),
        (1, -1, -1),
        (1, -1, 0),
        (1, -1, 1),
        (1, 0, -1),
        (1, 0, 0),
        (1, 0, 1),
        (1, 1, -1),
        (1, 1, 0),
        (1, 1, 1),
    ];

    par_iter!(grid.inner_table()).for_each(|(curr_cell, curr_particles)| {
        for &val in neighbours.iter() {
            #[cfg(feature = "dim2")]
            let neighbor_cell = {
                let (i, j) = val;
                curr_cell + Vector::new(i, j)
            };
            #[cfg(feature = "dim3")]
            let neighbor_cell = {
                let (i, j, k) = val;
                curr_cell + Vector::new(i, j, k)
            };
            if let Some(neighbor_particles) = grid.cell(&neighbor_cell) {
                compute_contacts_for_pair_of_cells(
                    h,
                    fluids,
                    boundaries,
                    fluid_fluid_contacts,
                    fluid_boundary_contacts,
                    boundary_boundary_contacts,
                    curr_cell,
                    curr_particles,
                    &neighbor_cell,
                    neighbor_particles,
                );
            }
        }
    });

    counters.cd.neighborhood_search_time.pause();
}

fn compute_contacts_for_pair_of_cells(
    h: Real,
    fluids: &[Fluid],
    boundaries: &[Boundary],
    fluid_fluid_contacts: &[ParticlesContacts],
    fluid_boundary_contacts: &[ParticlesContacts],
    boundary_boundary_contacts: &[ParticlesContacts],
    curr_cell: &Point<i64>,
    curr_particles: &[HGridEntry],
    neighbor_cell: &Point<i64>,
    neighbor_particles: &[HGridEntry],
) {
    for entry in curr_particles {
        match entry {
            HGridEntry::BoundaryParticle(boundary_i, particle_i) => {
                for entry in neighbor_particles {
                    // NOTE: we are not interested by boundary-fluid contacts.
                    // Those will already be detected as fluid-boundary contacts instead.
                    match entry {
                        HGridEntry::BoundaryParticle(boundary_j, particle_j) => {
                            let pi = &boundaries[*boundary_i].positions[*particle_i];
                            let pj = &boundaries[*boundary_j].positions[*particle_j];

                            if na::distance_squared(pi, pj) <= h * h {
                                let contact = Contact {
                                    i_model: *boundary_i,
                                    j_model: *boundary_j,
                                    i: *particle_i,
                                    j: *particle_j,
                                    weight: na::zero::<Real>(),
                                    gradient: Vector::zeros(),
                                };

                                boundary_boundary_contacts[*boundary_i].contacts[*particle_i]
                                    .write()
                                    .unwrap()
                                    .push(contact);

                                if *curr_cell != *neighbor_cell {
                                    boundary_boundary_contacts[*boundary_j].contacts[*particle_j]
                                        .write()
                                        .unwrap()
                                        .push(contact.flip());
                                }
                            }
                        }
                        HGridEntry::FluidParticle(fluid_j, particle_j) => {
                            if *curr_cell == *neighbor_cell {
                                // This pair will already be handled by the case where particle_i is a
                                // fluid particle.
                                continue;
                            }

                            let pi = &boundaries[*boundary_i].positions[*particle_i];
                            let pj = &fluids[*fluid_j].positions[*particle_j];

                            if na::distance_squared(pi, pj) <= h * h {
                                let contact = Contact {
                                    i_model: *fluid_j,
                                    j_model: *boundary_i,
                                    i: *particle_j,
                                    j: *particle_i,
                                    weight: na::zero::<Real>(),
                                    gradient: Vector::zeros(),
                                };

                                fluid_boundary_contacts[*fluid_j].contacts[*particle_j]
                                    .write()
                                    .unwrap()
                                    .push(contact);
                            }
                        }
                    }
                }
            }
            HGridEntry::FluidParticle(fluid_i, particle_i) => {
                for entry in neighbor_particles {
                    let (fluid_j, particle_j, is_boundary_j) = entry.into_tuple();
                    let pi = fluids[*fluid_i].positions[*particle_i];
                    let pj = if is_boundary_j {
                        boundaries[fluid_j].positions[particle_j]
                    } else {
                        fluids[fluid_j].positions[particle_j]
                    };

                    if na::distance_squared(&pi, &pj) <= h * h {
                        assert!(na::distance_squared(&pj, &pi) <= h * h);
                        let contact = Contact {
                            i_model: *fluid_i,
                            j_model: fluid_j,
                            i: *particle_i,
                            j: particle_j,
                            weight: na::zero::<Real>(),
                            gradient: Vector::zeros(),
                        };

                        if is_boundary_j {
                            fluid_boundary_contacts[*fluid_i].contacts[*particle_i]
                                .write()
                                .unwrap()
                                .push(contact);
                        } else {
                            fluid_fluid_contacts[*fluid_i].contacts[*particle_i]
                                .write()
                                .unwrap()
                                .push(contact);

                            if *curr_cell != *neighbor_cell {
                                fluid_fluid_contacts[fluid_j].contacts[particle_j]
                                    .write()
                                    .unwrap()
                                    .push(contact.flip());
                            }
                        }
                    }
                }
            }
        }
    }
}

/// Compute all the contacts between the particles of a single fluid object.
pub fn compute_self_contacts(h: Real, fluid: &Fluid, contacts: &mut ParticlesContacts) {
    contacts
        .contacts
        .iter_mut()
        .for_each(|c| c.write().unwrap().clear());

    contacts
        .contacts
        .resize_with(fluid.num_particles(), || RwLock::new(Vec::new()));

    let mut grid = HGrid::new(h);
    for (i, particle) in fluid.positions.iter().enumerate() {
        grid.insert(particle, i);
    }

    for (cell, curr_particles) in grid.cells() {
        let neighbors: Vec<_> = grid.neighbor_cells(cell, h).collect();

        for particle_i in curr_particles {
            for (_, nbh_particles) in &neighbors {
                for particle_j in *nbh_particles {
                    let pi = fluid.positions[*particle_i];
                    let pj = fluid.positions[*particle_j];

                    if na::distance_squared(&pi, &pj) <= h * h {
                        let contact = Contact {
                            i_model: 0,
                            j_model: 0,
                            i: *particle_i,
                            j: *particle_j,
                            weight: na::zero::<Real>(),
                            gradient: Vector::zeros(),
                        };

                        contacts.contacts[*particle_i]
                            .write()
                            .unwrap()
                            .push(contact);
                    }
                }
            }
        }
    }
}