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
use crate::{
FloatExt, RealExt, SimulationError, XResult,
simulation::prelude::{FirstPassageTime, Moment, OccupationTime},
};
/// Point process trait
///
/// `T`: The type of the position (default: f64)
/// `V`: The type of the time (default: f64)
pub trait PointProcess<T: FloatExt = f64, X: RealExt = T>: Send + Sync {
/// Get the starting position
fn start(&self) -> X;
/// Get the ending position
fn end(&self, duration: T) -> XResult<X> {
Ok(self.start() + self.displacement(duration)?)
}
/// Get the displacement of the point process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
fn displacement(&self, duration: T) -> XResult<X> {
let mut num_step = duration.ceil().to_usize().unwrap();
let (t, x) = loop {
let (t, x) = self.simulate_with_step(num_step)?;
if t.last().is_none() {
return Err(SimulationError::Unknown.into());
}
let end_time = *t.last().unwrap();
if T::from(end_time).unwrap() >= duration {
break (t, x);
}
num_step *= 2;
};
let index = t.iter().position(|&time| time >= duration).unwrap();
let delta_x = if t[index] > duration {
x[index - 1] - x[0]
} else {
x[index] - x[0]
};
Ok(delta_x)
}
/// Simulate the point process with given duration
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
fn simulate_with_duration(&self, duration: T) -> XResult<(Vec<T>, Vec<X>)>
where
Self: Sized,
{
let mut num_step = duration.ceil().to_usize().unwrap();
let (t, x) = loop {
let (t, x) = self.simulate_with_step(num_step)?;
if t.last().is_none() {
return Err(SimulationError::Unknown.into());
}
let end_time = *t.last().unwrap();
if end_time >= duration {
break (t, x);
}
num_step *= 2;
};
let index = t.iter().position(|&time| time >= duration).unwrap();
let mut t_ = vec![T::zero(); index + 1];
let mut x_ = vec![X::zero(); index + 1];
t_[..index].copy_from_slice(&t[..index]);
x_[..index].copy_from_slice(&x[..index]);
if t[index] > duration {
t_[index] = duration;
x_[index] = x_[index - 1];
} else {
t_[index] = t[index];
x_[index] = x[index];
}
Ok((t_, x_))
}
/// Get the mean of the point process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `particles` - The number of particles.
fn mean(&self, duration: T, particles: usize) -> XResult<f64>
where
Self: Sized + Clone + PointTrajectoryTrait<T, X>,
{
let traj = self.duration(duration)?;
traj.raw_moment(1, particles, 0.1)
}
/// Get the mean square displacement of the point process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `particles` - The number of particles.
fn msd(&self, duration: T, particles: usize) -> XResult<f64>
where
Self: Sized + Clone + PointTrajectoryTrait<T, X>,
{
let traj = self.duration(duration)?;
traj.msd(particles, 0.1)
}
/// Get the raw moment of the point process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `order` - The order of the moment.
/// * `particles` - The number of particles.
fn raw_moment(&self, duration: T, order: i32, particles: usize) -> XResult<f64>
where
Self: Sized + Clone + PointTrajectoryTrait<T, X>,
{
let traj = self.duration(duration)?;
traj.raw_moment(order, particles, 0.1)
}
/// Get the central moment of the point process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `order` - The order of the moment.
/// * `particles` - The number of particles.
fn central_moment(&self, duration: T, order: i32, particles: usize) -> XResult<f64>
where
Self: Sized + Clone + PointTrajectoryTrait<T, X>,
{
let traj = self.duration(duration)?;
traj.central_moment(order, particles, 0.1)
}
/// Get the fractional raw moment of the point process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `order` - The order of the moment.
/// * `particles` - The number of particles.
fn frac_raw_moment(&self, duration: T, order: f64, particles: usize) -> XResult<f64>
where
Self: Sized + Clone + PointTrajectoryTrait<T, X>,
{
let traj = self.duration(duration)?;
traj.frac_raw_moment(order, particles, 0.1)
}
/// Get the fractional central moment of the point process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `order` - The order of the moment.
/// * `particles` - The number of particles.
fn frac_central_moment(&self, duration: T, order: f64, particles: usize) -> XResult<f64>
where
Self: Sized + Clone + PointTrajectoryTrait<T, X>,
{
let traj = self.duration(duration)?;
traj.frac_central_moment(order, particles, 0.1)
}
/// Get the first passage time of the point process
///
/// # Arguments
///
/// * `domain` - The domain which the first passage time is interested in.
/// * `max_duration` - The maximum duration of the simulation. If the process does not exit the domain before the maximum duration, the function returns None.
fn fpt(&self, domain: (X, X), max_duration: T) -> XResult<Option<T>>
where
Self: Sized + Clone,
{
let fpt = FirstPassageTime::new(self, domain)?;
fpt.simulate_p(max_duration)
}
/// Get the occupation time of the point process
///
/// # Arguments
///
/// * `domain` - The domain which the occupation time is interested in.
/// * `duration` - The duration of the simulation.
fn occupation_time(&self, domain: (X, X), duration: T) -> XResult<T>
where
Self: Sized + Clone,
{
let ot = OccupationTime::new(self, domain, duration)?;
ot.simulate_p()
}
/// Simulate the point process with a given number of steps
///
/// # Arguments
///
/// * `num_step` - The number of steps of the simulation.
fn simulate_with_step(&self, num_step: usize) -> XResult<(Vec<T>, Vec<X>)>;
}
/// Point process trajectory
#[derive(Debug, Clone)]
pub struct PointTrajectory<SP: PointProcess<T, X> + Clone, T: FloatExt = f64, X: RealExt = T> {
/// The point process
pub(crate) sp: SP,
/// The duration of the trajectory
pub(crate) duration: Option<T>,
/// The number of steps of the trajectory
pub(crate) num_step: Option<usize>,
_marker: std::marker::PhantomData<X>,
}
pub trait PointTrajectoryTrait<T: FloatExt = f64, X: RealExt = T>:
PointProcess<T, X> + Clone
{
/// Create a `PointTrajectory` with given duration
///
/// # Arguments
///
/// * `duration` - The duration of the trajectory
fn duration(&self, duration: T) -> XResult<PointTrajectory<Self, T, X>> {
let traj = PointTrajectory::with_duration(self.clone(), duration)?;
Ok(traj)
}
/// Create a `PointTrajectory` with given number of steps
///
/// # Arguments
///
/// * `num_step` - The number of steps of the trajectory
fn step(&self, num_step: usize) -> XResult<PointTrajectory<Self, T, X>> {
let traj = PointTrajectory::with_step(self.clone(), num_step)?;
Ok(traj)
}
}
impl<SP: PointProcess<T, X> + Sized + Clone, T: FloatExt, X: RealExt> PointTrajectoryTrait<T, X>
for SP
{
}
impl<SP: PointProcess<T, X> + Clone, T: FloatExt, X: RealExt> PointTrajectory<SP, T, X> {
/// Get the point process
pub fn get_process(&self) -> &SP {
&self.sp
}
/// Get the duration of the trajectory
pub fn get_duration(&self) -> Option<T> {
self.duration
}
/// Get the number of steps of the trajectory
pub fn get_num_step(&self) -> Option<usize> {
self.num_step
}
/// Create a `PointTrajectory` with duration.
///
/// # Arguments
///
/// * `duration` - The duration of the trajectory
pub fn with_duration(sp: SP, duration: T) -> XResult<Self> {
if duration <= T::zero() {
return Err(SimulationError::InvalidParameters(format!(
"The `duration` must be positive, got {duration:?}"
))
.into());
}
Ok(Self {
sp: sp.clone(),
duration: Some(duration),
num_step: None,
_marker: std::marker::PhantomData,
})
}
/// Create a `PointTrajectory` with num of steps.
///
/// # Arguments
///
/// * `num_step` - The number of steps of the trajectory
pub fn with_step(sp: SP, num_step: usize) -> XResult<Self> {
if num_step == 0 {
return Err(SimulationError::InvalidParameters(format!(
"The `num_step` must be positive, got {num_step}"
))
.into());
}
Ok(Self {
sp: sp.clone(),
duration: None,
num_step: Some(num_step),
_marker: std::marker::PhantomData,
})
}
/// Simulate the trajectory with duration
pub fn simulate_with_duration(&self) -> XResult<(Vec<T>, Vec<X>)> {
if self.duration.is_none() {
return Err(SimulationError::InvalidParameters(
"The `duration` must be provided".to_string(),
)
.into());
}
let duration = self.duration.unwrap();
if duration <= T::zero() {
return Err(SimulationError::InvalidParameters(format!(
"The `duration` must be positive, got {duration:?}"
))
.into());
}
self.sp.simulate_with_duration(duration)
}
/// Simulate with number of steps
pub fn simulate_with_step(&self) -> XResult<(Vec<T>, Vec<X>)> {
if self.num_step.is_none() {
return Err(SimulationError::InvalidParameters(
"num_step must be provided".to_string(),
)
.into());
}
let num_step = self.num_step.unwrap();
if num_step == 0 {
return Err(SimulationError::InvalidParameters(format!(
"The `num_step` must be positive, got {num_step}"
))
.into());
}
self.sp.simulate_with_step(num_step)
}
}