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
use core::{marker::PhantomData, num::NonZeroU64};

use necsim_core::{
    cogs::{DispersalSampler, Habitat, MathsCore, RngCore, SeparableDispersalSampler},
    landscape::Location,
};
use necsim_core_bond::ClosedUnitF64;

use crate::cogs::habitat::non_spatial::NonSpatialHabitat;

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M", free = "G"))]
pub struct NonSpatialDispersalSampler<M: MathsCore, G: RngCore<M>> {
    marker: PhantomData<(M, G)>,
}

impl<M: MathsCore, G: RngCore<M>> Default for NonSpatialDispersalSampler<M, G> {
    #[must_use]
    fn default() -> Self {
        Self {
            marker: PhantomData::<(M, G)>,
        }
    }
}

impl<M: MathsCore, G: RngCore<M>> Clone for NonSpatialDispersalSampler<M, G> {
    fn clone(&self) -> Self {
        Self {
            marker: PhantomData::<(M, G)>,
        }
    }
}

#[contract_trait]
impl<M: MathsCore, G: RngCore<M>> DispersalSampler<M, NonSpatialHabitat<M>, G>
    for NonSpatialDispersalSampler<M, G>
{
    #[must_use]
    #[inline]
    fn sample_dispersal_from_location(
        &self,
        _location: &Location,
        habitat: &NonSpatialHabitat<M>,
        rng: &mut G,
    ) -> Location {
        use necsim_core::cogs::RngSampler;

        let habitat_index_max =
            habitat.get_extent().width().get() * habitat.get_extent().height().get();

        // Safety: habitat width and height are both > 0
        let dispersal_target_index =
            rng.sample_index_u64(unsafe { NonZeroU64::new_unchecked(habitat_index_max) });

        #[allow(clippy::cast_possible_truncation)]
        Location::new(
            habitat
                .get_extent()
                .origin()
                .x()
                .wrapping_add((dispersal_target_index % habitat.get_extent().width().get()) as u32),
            habitat
                .get_extent()
                .origin()
                .y()
                .wrapping_add((dispersal_target_index / habitat.get_extent().width().get()) as u32),
        )
    }
}

#[contract_trait]
impl<M: MathsCore, G: RngCore<M>> SeparableDispersalSampler<M, NonSpatialHabitat<M>, G>
    for NonSpatialDispersalSampler<M, G>
{
    #[must_use]
    #[debug_requires((
        u64::from(habitat.get_extent().width()) * u64::from(habitat.get_extent().height())
    ) > 1_u64, "a different, non-self dispersal, target location exists")]
    fn sample_non_self_dispersal_from_location(
        &self,
        location: &Location,
        habitat: &NonSpatialHabitat<M>,
        rng: &mut G,
    ) -> Location {
        use necsim_core::cogs::RngSampler;

        let habitat_index_max =
            habitat.get_extent().width().get() * habitat.get_extent().height().get();
        let current_location_index =
            u64::from(location.y()) * habitat.get_extent().width().get() + u64::from(location.x());

        let dispersal_target_index = {
            // Safety: by PRE, `habitat_index_max` > 1
            let dispersal_target_index =
                rng.sample_index_u64(unsafe { NonZeroU64::new_unchecked(habitat_index_max - 1) });

            if dispersal_target_index >= current_location_index {
                dispersal_target_index + 1
            } else {
                dispersal_target_index
            }
        };

        #[allow(clippy::cast_possible_truncation)]
        Location::new(
            habitat
                .get_extent()
                .origin()
                .x()
                .wrapping_add((dispersal_target_index % habitat.get_extent().width().get()) as u32),
            habitat
                .get_extent()
                .origin()
                .y()
                .wrapping_add((dispersal_target_index / habitat.get_extent().width().get()) as u32),
        )
    }

    #[must_use]
    fn get_self_dispersal_probability_at_location(
        &self,
        _location: &Location,
        habitat: &NonSpatialHabitat<M>,
    ) -> ClosedUnitF64 {
        let self_dispersal = 1.0_f64
            / (f64::from(habitat.get_extent().width()) * f64::from(habitat.get_extent().height()));

        // Safety: Since the method is only called for a valid location,
        //          width >= 1 and height >= 1
        //         => 1.0/(width*height) in [0.0; 1.0]
        unsafe { ClosedUnitF64::new_unchecked(self_dispersal) }
    }
}