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
use core::marker::PhantomData;

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

pub mod uniform;

#[allow(clippy::inline_always, clippy::inline_fn_without_body)]
#[allow(clippy::no_effect_underscore_binding)]
#[allow(clippy::module_name_repetitions)]
#[contract_trait]
pub trait AntiTrespassingDispersalSampler<M: MathsCore, H: Habitat<M>, G: RngCore<M>>:
    Backup + core::fmt::Debug
{
    #[must_use]
    #[debug_requires(!habitat.is_location_habitable(location), "location is inhabitable")]
    #[debug_ensures(old(habitat).is_location_habitable(&ret), "target is habitable")]
    fn sample_anti_trespassing_dispersal_from_location(
        &self,
        location: &Location,
        habitat: &H,
        rng: &mut G,
    ) -> Location;
}

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct TrespassingDispersalSampler<
    M: MathsCore,
    H: Habitat<M>,
    G: RngCore<M>,
    D: DispersalSampler<M, H, G>,
    T: AntiTrespassingDispersalSampler<M, H, G>,
> {
    #[cfg_attr(feature = "cuda", cuda(embed))]
    legal_dispersal_sampler: D,
    #[cfg_attr(feature = "cuda", cuda(embed))]
    trespassing_dispersal_sampler: T,
    marker: PhantomData<(M, H, G)>,
}

impl<
        M: MathsCore,
        H: Habitat<M>,
        G: RngCore<M>,
        D: DispersalSampler<M, H, G>,
        T: AntiTrespassingDispersalSampler<M, H, G>,
    > TrespassingDispersalSampler<M, H, G, D, T>
{
    #[must_use]
    pub fn new(dispersal_sampler: D, anti_trespassing: T) -> Self {
        Self {
            legal_dispersal_sampler: dispersal_sampler,
            trespassing_dispersal_sampler: anti_trespassing,
            marker: PhantomData::<(M, H, G)>,
        }
    }
}

#[contract_trait]
impl<
        M: MathsCore,
        H: Habitat<M>,
        G: RngCore<M>,
        D: DispersalSampler<M, H, G>,
        T: AntiTrespassingDispersalSampler<M, H, G>,
    > Backup for TrespassingDispersalSampler<M, H, G, D, T>
{
    unsafe fn backup_unchecked(&self) -> Self {
        Self {
            legal_dispersal_sampler: self.legal_dispersal_sampler.backup_unchecked(),
            trespassing_dispersal_sampler: self.trespassing_dispersal_sampler.backup_unchecked(),
            marker: PhantomData::<(M, H, G)>,
        }
    }
}

impl<
        M: MathsCore,
        H: Habitat<M>,
        G: RngCore<M>,
        D: DispersalSampler<M, H, G>,
        T: AntiTrespassingDispersalSampler<M, H, G>,
    > DispersalSampler<M, H, G> for TrespassingDispersalSampler<M, H, G, D, T>
{
    #[must_use]
    #[inline]
    #[allow(clippy::no_effect_underscore_binding)]
    #[debug_ensures(old(habitat).is_location_habitable(&ret), "target is habitable")]
    fn sample_dispersal_from_location(
        &self,
        location: &Location,
        habitat: &H,
        rng: &mut G,
    ) -> Location {
        // Explicitly circumvent the precondition that
        //  `habitat.is_location_habitable(location)`
        self.__contracts_impl_sample_dispersal_from_location(location, habitat, rng)
    }

    #[must_use]
    #[inline]
    fn __contracts_impl_sample_dispersal_from_location(
        &self,
        location: &Location,
        habitat: &H,
        rng: &mut G,
    ) -> Location {
        if habitat.is_location_habitable(location) {
            // Re-establish the precondition that `habitat.is_location_habitable(location)`
            self.legal_dispersal_sampler
                .sample_dispersal_from_location(location, habitat, rng)
        } else {
            // Establish the precondition that `!habitat.is_location_habitable(location)`
            self.trespassing_dispersal_sampler
                .sample_anti_trespassing_dispersal_from_location(location, habitat, rng)
        }
    }
}

impl<
        M: MathsCore,
        H: Habitat<M>,
        G: RngCore<M>,
        D: SeparableDispersalSampler<M, H, G>,
        T: AntiTrespassingDispersalSampler<M, H, G>,
    > SeparableDispersalSampler<M, H, G> for TrespassingDispersalSampler<M, H, G, D, T>
{
    #[must_use]
    #[inline]
    #[allow(clippy::no_effect_underscore_binding)]
    #[debug_ensures(old(habitat).is_location_habitable(&ret), "target is habitable")]
    #[debug_ensures(&ret != location, "disperses to a different location")]
    fn sample_non_self_dispersal_from_location(
        &self,
        location: &Location,
        habitat: &H,
        rng: &mut G,
    ) -> Location {
        // Explicitly circumvent the precondition that
        //  `habitat.is_location_habitable(location)`
        self.__contracts_impl_sample_non_self_dispersal_from_location(location, habitat, rng)
    }

    #[must_use]
    #[inline]
    fn __contracts_impl_sample_non_self_dispersal_from_location(
        &self,
        location: &Location,
        habitat: &H,
        rng: &mut G,
    ) -> Location {
        if habitat.is_location_habitable(location) {
            // Re-establish the precondition that `habitat.is_location_habitable(location)`
            self.legal_dispersal_sampler
                .sample_non_self_dispersal_from_location(location, habitat, rng)
        } else {
            // Establish the precondition that `!habitat.is_location_habitable(location)`
            // Since the origin location is inhabitable but the target location is
            //  habitable, the target location must be different from the origin
            //  location
            self.trespassing_dispersal_sampler
                .sample_anti_trespassing_dispersal_from_location(location, habitat, rng)
        }
    }

    #[must_use]
    #[inline]
    fn get_self_dispersal_probability_at_location(
        &self,
        location: &Location,
        habitat: &H,
    ) -> ClosedUnitF64 {
        // Explicitly circumvent the precondition that
        //  `habitat.is_location_habitable(location)`
        self.__contracts_impl_get_self_dispersal_probability_at_location(location, habitat)
    }

    #[must_use]
    #[inline]
    fn __contracts_impl_get_self_dispersal_probability_at_location(
        &self,
        location: &Location,
        habitat: &H,
    ) -> ClosedUnitF64 {
        if habitat.is_location_habitable(location) {
            // Re-establish the precondition that `habitat.is_location_habitable(location)`
            self.legal_dispersal_sampler
                .get_self_dispersal_probability_at_location(location, habitat)
        } else {
            // The `AntiTrespassingDispersalSampler` always jumps from an inhabitable
            //  location to a habitable location, i.e. there is never any self-dispersal
            //  when starting at an inhabitable location
            ClosedUnitF64::zero()
        }
    }
}