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
use crate::{
FloatExt, SimulationError, XResult,
simulation::prelude::{FirstPassageTime, Moment, OccupationTime, TAMSD},
};
/// Continuous process trait
pub trait ContinuousProcess<T: FloatExt = f64>: Send + Sync {
/// Simulate the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `time_step` - The time step of the simulation.
fn simulate(&self, duration: T, time_step: T) -> XResult<(Vec<T>, Vec<T>)>;
/// Get the displacement of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `time_step` - The time step of the simulation.
fn displacement(&self, duration: T, time_step: T) -> XResult<T> {
let (_, x) = self.simulate(duration, time_step)?;
match (x.first(), x.last()) {
(Some(first), Some(last)) => Ok(*last - *first),
_ => Err(SimulationError::Unknown.into()),
}
}
/// Get the starting position
fn start(&self) -> T;
/// Get the ending position
fn end(&self, duration: T, time_step: T) -> XResult<T> {
let delta_x = self.displacement(duration, time_step)?;
Ok(self.start() + delta_x)
}
/// Get the mean of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `particles` - The number of particles.
/// * `time_step` - The time step of the simulation.
fn mean(&self, duration: T, particles: usize, time_step: T) -> XResult<T>
where
Self: ContinuousTrajectoryTrait<T>,
{
let traj = self.duration(duration)?;
traj.mean(particles, time_step)
}
/// Get the mean square displacement of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `particles` - The number of particles.
/// * `time_step` - The time step of the simulation.
fn msd(&self, duration: T, particles: usize, time_step: T) -> XResult<T>
where
Self: ContinuousTrajectoryTrait<T>,
{
let traj = self.duration(duration)?;
traj.msd(particles, time_step)
}
/// Get the raw moment of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the continuous process.
/// * `order` - The order of the moment.
/// * `particles` - The number of particles.
/// * `time_step` - The time step of the continuous process.
fn raw_moment(&self, duration: T, order: i32, particles: usize, time_step: T) -> XResult<T>
where
Self: ContinuousTrajectoryTrait<T>,
{
let traj = self.duration(duration)?;
traj.raw_moment(order, particles, time_step)
}
/// Get the central moment of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the continuous process.
/// * `order` - The order of the moment.
/// * `particles` - The number of particles.
/// * `time_step` - The time step of the continuous process.
fn central_moment(&self, duration: T, order: i32, particles: usize, time_step: T) -> XResult<T>
where
Self: ContinuousTrajectoryTrait<T>,
{
let traj = self.duration(duration)?;
traj.central_moment(order, particles, time_step)
}
/// Get the fractional raw moment of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the continuous process.
/// * `order` - The order of the moment.
/// * `particles` - The number of particles.
/// * `time_step` - The time step of the continuous process.
fn frac_raw_moment(&self, duration: T, order: T, particles: usize, time_step: T) -> XResult<T>
where
Self: ContinuousTrajectoryTrait<T>,
{
let traj = self.duration(duration)?;
traj.frac_raw_moment(order, particles, time_step)
}
/// Get the fractional central moment of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the continuous process.
/// * `order` - The order of the moment.
/// * `particles` - The number of particles.
/// * `time_step` - The time step of the continuous process.
fn frac_central_moment(
&self,
duration: T,
order: T,
particles: usize,
time_step: T,
) -> XResult<T>
where
Self: ContinuousTrajectoryTrait<T>,
{
let traj = self.duration(duration)?;
traj.frac_central_moment(order, particles, time_step)
}
/// Get the first passage time of the continuous process
///
/// # Arguments
///
/// * `domain` - The domain which the first passage time is interested in.
/// * `max_duration` - The maximum duration of the continuous process. If the process does not exit the domain before the maximum duration, the function returns None.
/// * `time_step` - The time step of the simulation.
fn fpt(&self, domain: (T, T), max_duration: T, time_step: T) -> XResult<Option<T>>
where
Self: Sized,
{
let fpt = FirstPassageTime::new(self, domain)?;
fpt.simulate(max_duration, time_step)
}
/// Get the occupation time of the continuous process
///
/// # Arguments
///
/// * `domain` - The domain which the occupation time is interested in.
/// * `duration` - The duration of the continuous process.
/// * `time_step` - The time step of the simulation.
fn occupation_time(&self, domain: (T, T), duration: T, time_step: T) -> XResult<T>
where
Self: Sized,
{
let ot = OccupationTime::new(self, domain, duration)?;
ot.simulate(time_step)
}
/// Get the time-averaged mean square displacement of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `delta` - The slag length.
/// * `time_step` - The time step of the simulation.
/// * `quad_order` - The order of the Gauss-Legendre quadrature.
fn tamsd(&self, duration: T, delta: T, time_step: T, quad_order: usize) -> XResult<T>
where
Self: Sized,
{
let tamsd = TAMSD::new(self, duration, delta)?;
tamsd.simulate(time_step, quad_order)
}
/// Get the ensemble average of the time-averaged mean square displacement of the continuous process
///
/// # Arguments
///
/// * `duration` - The duration of the simulation.
/// * `delta` - The slag length.
/// * `particles` - The number of particles.
/// * `time_step` - The time step of the simulation.
/// * `quad_order` - The order of the Gauss-Legendre quadrature.
fn eatamsd(
&self,
duration: T,
delta: T,
particles: usize,
time_step: T,
quad_order: usize,
) -> XResult<T>
where
Self: Sized,
{
let tamsd = TAMSD::new(self, duration, delta)?;
tamsd.mean(particles, time_step, quad_order)
}
}
/// Continuous trajectory
#[derive(Debug, Clone)]
pub struct ContinuousTrajectory<SP, T: FloatExt = f64>
where
SP: ContinuousProcess<T> + Clone,
{
/// The continuous process
pub(crate) sp: SP,
/// The duration of the simulation
pub(crate) duration: T,
}
pub trait ContinuousTrajectoryTrait<T: FloatExt>: ContinuousProcess<T> + Clone {
fn duration(&self, duration_arg: T) -> XResult<ContinuousTrajectory<Self, T>> {
let traj = ContinuousTrajectory::new(self.clone(), duration_arg)?;
Ok(traj)
}
}
impl<T: FloatExt, SP: ContinuousProcess<T> + Clone> ContinuousTrajectoryTrait<T> for SP {}
impl<T: FloatExt, SP: ContinuousProcess<T> + Clone> ContinuousTrajectory<SP, T> {
/// Create a new `ContinuousTrajectory` with given `ContinuousProcess` and duration.
///
/// # Arguments
///
/// * `sp` - The continuous process.
/// * `duration` - The duration of the simulation.
pub fn new(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, duration })
}
/// Get the continuous process
pub fn get_process(&self) -> &SP {
&self.sp
}
/// Get the duration of the trajectory
pub fn get_duration(&self) -> T {
self.duration
}
/// Simulate method
///
/// # Arguments
///
/// * `time_step` - The time step of the simulation.
pub fn simulate(&self, time_step: T) -> XResult<(Vec<T>, Vec<T>)> {
self.sp.simulate(self.duration, time_step)
}
}