rusty_kernel_tools/
particle_container.rs

1//! This module defines a simple container structure to store source particles and target particles
2//! in a convenient way.
3
4use crate::RealType;
5use ndarray::{Array2, ArrayView2};
6
7// This traits describes any type that provides an array of sources and
8// an array of targets.
9pub trait ParticleContainerAccessor {
10    type FloatingPointType: RealType;
11
12    /// Return a non-owning representation of the sources.
13    fn sources(&self) -> ArrayView2<Self::FloatingPointType>;
14    /// Return a non-owning representation of the targets.
15    fn targets(&self) -> ArrayView2<Self::FloatingPointType>;
16}
17
18/// The basic data structure for sources and targets that are owned.
19pub struct ParticleContainer<T: RealType> {
20    sources: Array2<T>,
21    targets: Array2<T>,
22}
23
24/// Create a new particle container and transfer ownership to it.
25pub fn make_particle_container_owned<T: RealType>(
26    sources: Array2<T>,
27    targets: Array2<T>,
28) -> ParticleContainer<T> {
29    ParticleContainer { sources, targets }
30}
31
32/// Create a new particle container that does not take over ownership.
33pub fn make_particle_container<'a, T: RealType>(
34    sources: ArrayView2<'a, T>,
35    targets: ArrayView2<'a, T>,
36) -> ParticleContainerView<'a, T> {
37    ParticleContainerView { sources, targets }
38}
39
40// The basic data structure of for sources and targets that are not owned.
41pub struct ParticleContainerView<'a, T: RealType> {
42    sources: ArrayView2<'a, T>,
43    targets: ArrayView2<'a, T>,
44}
45
46impl<T: RealType> ParticleContainerAccessor for ParticleContainer<T> {
47    type FloatingPointType = T;
48
49    /// Return a non-owning representation of the sources.
50    fn sources(&self) -> ArrayView2<Self::FloatingPointType> {
51        self.sources.view()
52    }
53
54    /// Return a non-owning representation of the targets.
55    fn targets(&self) -> ArrayView2<Self::FloatingPointType> {
56        self.targets.view()
57    }
58}
59
60impl<'a, T: RealType> ParticleContainerAccessor for ParticleContainerView<'a, T> {
61    type FloatingPointType = T;
62
63    /// Return a non-owning representation of the sources.
64    fn sources(&self) -> ArrayView2<Self::FloatingPointType> {
65        self.sources.view()
66    }
67
68    /// Return a non-owning representation of the targets.
69    fn targets(&self) -> ArrayView2<Self::FloatingPointType> {
70        self.targets.view()
71    }
72}