rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//! # Numerical Molecular Dynamics (MD)
//!
//! This module provides numerical methods for Molecular Dynamics (MD) simulations,
//! a computational method for analyzing the physical movements of atoms and molecules.
//!
//! ## Features
//!
//! ### Interatomic Potentials
//! - **Lennard-Jones**: Standard 12-6 potential for noble gases
//! - **Morse potential**: Better for covalent bonds
//! - **Harmonic potential**: Simple spring model
//! - **Coulomb potential**: Electrostatic interactions
//!
//! ### Integration Algorithms
//! - **Velocity Verlet**: Standard symplectic integrator
//! - **Leapfrog**: Alternative formulation
//! - **Euler**: Simple first-order (reference only)
//!
//! ### Thermodynamics
//! - Temperature calculation from kinetic energy
//! - Pressure calculation from virial
//! - Thermostats (velocity rescaling, Berendsen)
//!
//! ### Analysis
//! - Radial distribution function
//! - Mean square displacement
//! - Kinetic and potential energy
//!
//! ## Example
//!
//! ```rust
//! use rssn::numerical::physics_md::*;
//!
//! let p1 = Particle::new(0, 1.0, vec![0.0, 0.0, 0.0], vec![0.0, 0.0, 0.0]);
//!
//! let p2 = Particle::new(1, 1.0, vec![1.5, 0.0, 0.0], vec![0.0, 0.0, 0.0]);
//!
//! let (potential, force) = lennard_jones_interaction(&p1, &p2, 1.0, 1.0).unwrap();
//! ```

use serde::Deserialize;
use serde::Serialize;

use crate::numerical::vector::norm;
use crate::numerical::vector::scalar_mul;
use crate::numerical::vector::vec_add;
use crate::numerical::vector::vec_sub;

// ============================================================================
// Physical Constants (Reduced Units)
// ============================================================================

/// Boltzmann constant in SI units (J/K)
pub const BOLTZMANN_CONSTANT_SI: f64 = 1.380_649e-23;

/// Avogadro's number (1/mol)
pub const AVOGADRO_NUMBER: f64 = 6.022_140_76e23;

/// Reduced unit for temperature (using argon as reference)
/// 1 reduced temperature = `ε/k_B` ≈ 120 K for argon
pub const TEMPERATURE_UNIT_ARGON: f64 = 119.8;

/// Reduced unit for length (using argon as reference)
/// 1 reduced length = σ ≈ 3.4 Å for argon
pub const LENGTH_UNIT_ARGON: f64 = 3.4e-10;

/// Reduced unit for energy (using argon as reference)
/// 1 reduced energy = ε ≈ 1.65e-21 J for argon
pub const ENERGY_UNIT_ARGON: f64 = 1.65e-21;

// ============================================================================
// Particle Definition
// ============================================================================

/// Represents a particle in a molecular dynamics simulation.
/// cbindgen:ignore
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Particle {
    /// Unique identifier
    pub id: usize,
    /// Mass in reduced units
    pub mass: f64,
    /// Position vector
    pub position: Vec<f64>,
    /// Velocity vector
    pub velocity: Vec<f64>,
    /// Force vector (computed during simulation)
    pub force: Vec<f64>,
    /// Charge (for Coulomb interactions)
    pub charge: f64,
    /// Particle type (for multi-species simulations)
    pub particle_type: usize,
}

impl Particle {
    /// Creates a new particle with default charge and type.
    #[must_use]
    pub fn new(
        id: usize,
        mass: f64,
        position: Vec<f64>,
        velocity: Vec<f64>,
    ) -> Self {
        let dim = position.len();

        Self {
            id,
            mass,
            position,
            velocity,
            force: vec![0.0; dim],
            charge: 0.0,
            particle_type: 0,
        }
    }

    /// Creates a new particle with charge.
    #[must_use]
    pub fn with_charge(
        id: usize,
        mass: f64,
        position: Vec<f64>,
        velocity: Vec<f64>,
        charge: f64,
    ) -> Self {
        let dim = position.len();

        Self {
            id,
            mass,
            position,
            velocity,
            force: vec![0.0; dim],
            charge,
            particle_type: 0,
        }
    }

    /// Returns the kinetic energy of the particle: KE = 0.5 * m * v²
    #[must_use]
    pub fn kinetic_energy(&self) -> f64 {
        let v2: f64 = self.velocity.iter().map(|v| v * v).sum();

        0.5 * self.mass * v2
    }

    /// Returns the momentum of the particle: p = m * v
    #[must_use]
    pub fn momentum(&self) -> Vec<f64> {
        scalar_mul(&self.velocity, self.mass)
    }

    /// Returns the speed (magnitude of velocity)
    #[must_use]
    pub fn speed(&self) -> f64 {
        norm(&self.velocity)
    }

    /// Distance to another particle
    ///
    /// # Errors
    /// Returns an error if the particles have different dimensions.
    pub fn distance_to(
        &self,
        other: &Self,
    ) -> Result<f64, String> {
        let r_vec = vec_sub(&self.position, &other.position)?;

        Ok(norm(&r_vec))
    }
}

// ============================================================================
// Interatomic Potentials
// ============================================================================

/// Computes the Lennard-Jones potential and force between two particles.
///
/// The Lennard-Jones potential is a simple mathematical model that describes the
/// interaction between a pair of neutral atoms or molecules. It has a repulsive
/// term at short distances and an attractive term at long distances.
///
/// # Arguments
/// * `p1`, `p2` - The two particles.
/// * `epsilon` - The depth of the potential well.
/// * `sigma` - The finite distance at which the inter-particle potential is zero.
///
/// # Returns
/// A tuple `(potential, force_on_p1)` where `potential` is the scalar potential energy
/// and `force_on_p1` is the force vector acting on `p1` due to `p2`.
///
/// # Errors
/// Returns an error if the particles have different dimensions.
pub fn lennard_jones_interaction(
    p1: &Particle,
    p2: &Particle,
    epsilon: f64,
    sigma: f64,
) -> Result<(f64, Vec<f64>), String> {
    let r_vec = vec_sub(&p1.position, &p2.position)?;

    let r = norm(&r_vec);

    if r < 1e-9 {
        return Ok((f64::INFINITY, vec![0.0; r_vec.len()]));
    }

    let sigma_over_r = sigma / r;

    let sigma_over_r6 = sigma_over_r.powi(6);

    let sigma_over_r12 = sigma_over_r6.powi(2);

    let potential = 4.0 * epsilon * (sigma_over_r12 - sigma_over_r6);

    let force_magnitude = 24.0 * epsilon * 2.0f64.mul_add(sigma_over_r12, -sigma_over_r6) / r;

    let force_on_p1 = scalar_mul(&r_vec, force_magnitude / r);

    Ok((potential, force_on_p1))
}

/// Integrates the equations of motion for a system of particles using the Velocity Verlet algorithm.
///
/// The Velocity Verlet algorithm is a popular numerical integration scheme for molecular dynamics
/// simulations. It is time-reversible and preserves phase space volume, making it suitable
/// for long-term simulations.
///
/// # Arguments
/// * `particles` - A mutable vector of `Particle`s.
/// * `dt` - The time step.
/// * `num_steps` - The number of simulation steps.
/// * `force_calculator` - A closure that computes the total force on each particle.
///   It takes `&mut Vec<Particle>` and returns `Result<(), String>`.
///
/// # Returns
/// A `Vec<Vec<Particle>>` representing the trajectory of the particles over time.
///
/// # Errors
/// Returns an error if the force calculator fails or if vector operations fail due to dimension mismatch.
pub fn integrate_velocity_verlet<F>(
    particles: &mut Vec<Particle>,
    dt: f64,
    num_steps: usize,
    mut force_calculator: F,
) -> Result<Vec<Vec<Particle>>, String>
where
    F: FnMut(&mut Vec<Particle>) -> Result<(), String>,
{
    let mut trajectory = Vec::with_capacity(num_steps + 1);

    trajectory.push(particles.clone());

    force_calculator(particles)?;

    for _step in 0..num_steps {
        for p in particles.iter_mut() {
            let acc = scalar_mul(&p.force, 1.0 / p.mass);

            p.velocity = vec_add(&p.velocity, &scalar_mul(&acc, 0.5 * dt))?;

            p.position = vec_add(&p.position, &scalar_mul(&p.velocity, dt))?;
        }

        force_calculator(particles)?;

        for p in particles.iter_mut() {
            let acc = scalar_mul(&p.force, 1.0 / p.mass);

            p.velocity = vec_add(&p.velocity, &scalar_mul(&acc, 0.5 * dt))?;
        }

        trajectory.push(particles.clone());
    }

    Ok(trajectory)
}

// ============================================================================
// Additional Potentials
// ============================================================================

/// Morse potential for diatomic molecules.
///
/// V(r) = De * (1 - exp(-a(r - re)))²
///
/// # Arguments
/// * `p1`, `p2` - The two particles
/// * `de` - Dissociation energy
/// * `a` - Controls the width of the potential well
/// * `re` - Equilibrium bond distance
///
/// # Errors
/// Returns an error if the particles have different dimensions.
pub fn morse_interaction(
    p1: &Particle,
    p2: &Particle,
    de: f64,
    a: f64,
    re: f64,
) -> Result<(f64, Vec<f64>), String> {
    let r_vec = vec_sub(&p1.position, &p2.position)?;

    let r = norm(&r_vec);

    if r < 1e-9 {
        return Ok((f64::INFINITY, vec![0.0; r_vec.len()]));
    }

    let exp_term = (-a * (r - re)).exp();

    let one_minus_exp = 1.0 - exp_term;

    let potential = de * one_minus_exp * one_minus_exp;

    // Force = -dV/dr = 2 * De * a * (1 - exp) * exp
    let force_magnitude = 2.0 * de * a * one_minus_exp * exp_term;

    let force_on_p1 = scalar_mul(&r_vec, force_magnitude / r);

    Ok((potential, force_on_p1))
}

/// Harmonic (spring) potential.
///
/// V(r) = 0.5 * k * (r - r0)²
///
/// # Arguments
/// * `p1`, `p2` - The two particles
/// * `k` - Spring constant
/// * `r0` - Equilibrium distance
///
/// # Errors
/// Returns an error if the particles have different dimensions.
pub fn harmonic_interaction(
    p1: &Particle,
    p2: &Particle,
    k: f64,
    r0: f64,
) -> Result<(f64, Vec<f64>), String> {
    let r_vec = vec_sub(&p1.position, &p2.position)?;

    let r = norm(&r_vec);

    if r < 1e-9 {
        return Ok((0.0, vec![0.0; r_vec.len()]));
    }

    let dr = r - r0;

    let potential = 0.5 * k * dr * dr;

    let force_magnitude = -k * dr;

    let force_on_p1 = scalar_mul(&r_vec, force_magnitude / r);

    Ok((potential, force_on_p1))
}

/// Coulomb (electrostatic) potential.
///
/// V(r) = `k_e` * q1 * q2 / r
///
/// # Arguments
/// * `p1`, `p2` - The two particles (with charge fields)
/// * `k_coulomb` - Coulomb constant (in appropriate units)
///
/// # Errors
/// Returns an error if the particles have different dimensions.
pub fn coulomb_interaction(
    p1: &Particle,
    p2: &Particle,
    k_coulomb: f64,
) -> Result<(f64, Vec<f64>), String> {
    let r_vec = vec_sub(&p1.position, &p2.position)?;

    let r = norm(&r_vec);

    if r < 1e-9 {
        return Ok((f64::INFINITY, vec![0.0; r_vec.len()]));
    }

    let potential = k_coulomb * p1.charge * p2.charge / r;

    // Force = -dV/dr * r_hat = k * q1 * q2 / r² * r_hat
    let force_magnitude = k_coulomb * p1.charge * p2.charge / (r * r);

    let force_on_p1 = scalar_mul(&r_vec, force_magnitude / r);

    Ok((potential, force_on_p1))
}

/// Soft-sphere potential for avoiding particle overlap.
///
/// V(r) = ε * (σ/r)^n for r < σ, 0 otherwise
///
/// # Errors
/// Returns an error if the particles have different dimensions.
pub fn soft_sphere_interaction(
    p1: &Particle,
    p2: &Particle,
    epsilon: f64,
    sigma: f64,
    n: i32,
) -> Result<(f64, Vec<f64>), String> {
    let r_vec = vec_sub(&p1.position, &p2.position)?;

    let r = norm(&r_vec);

    if r >= sigma {
        return Ok((0.0, vec![0.0; r_vec.len()]));
    }

    if r < 1e-9 {
        return Ok((f64::INFINITY, vec![0.0; r_vec.len()]));
    }

    let sigma_over_r = sigma / r;

    let potential = epsilon * sigma_over_r.powi(n);

    let force_magnitude = f64::from(n) * epsilon * sigma_over_r.powi(n) / r;

    let force_on_p1 = scalar_mul(&r_vec, force_magnitude / r);

    Ok((potential, force_on_p1))
}

// ============================================================================
// System Properties
// ============================================================================

/// Calculates the total kinetic energy of the system.
#[must_use]
pub fn total_kinetic_energy(particles: &[Particle]) -> f64 {
    particles.iter().map(Particle::kinetic_energy).sum()
}

/// Calculates the total momentum of the system.
///
/// # Errors
/// Returns an error if the particle list is empty.
pub fn total_momentum(particles: &[Particle]) -> Result<Vec<f64>, String> {
    if particles.is_empty() {
        return Err("Empty particle \
                    list"
            .to_string());
    }

    let dim = particles[0].position.len();

    let mut total = vec![0.0; dim];

    for p in particles {
        let mom = p.momentum();

        for (i, m) in mom.iter().enumerate() {
            total[i] += m;
        }
    }

    Ok(total)
}

/// Calculates the center of mass of the system.
///
/// # Errors
/// Returns an error if the particle list is empty.
pub fn center_of_mass(particles: &[Particle]) -> Result<Vec<f64>, String> {
    if particles.is_empty() {
        return Err("Empty particle \
                    list"
            .to_string());
    }

    let dim = particles[0].position.len();

    let mut com = vec![0.0; dim];

    let mut total_mass = 0.0;

    for p in particles {
        total_mass += p.mass;

        for (i, pos) in p.position.iter().enumerate() {
            com[i] += p.mass * pos;
        }
    }

    for c in &mut com {
        *c /= total_mass;
    }

    Ok(com)
}

/// Calculates the temperature from kinetic energy.
///
/// T = 2 * KE / (dim * N * `k_B`)
/// In reduced units with `k_B` = 1: T = 2 * KE / (dim * N)
#[must_use]
pub fn temperature(particles: &[Particle]) -> f64 {
    if particles.is_empty() {
        return 0.0;
    }

    let dim = particles[0].position.len();

    let ke = total_kinetic_energy(particles);

    let n = particles.len();

    // In reduced units (k_B = 1)
    2.0 * ke / (dim * n) as f64
}

/// Calculates instantaneous pressure using the virial theorem.
///
/// P = (N * `k_B` * T + virial) / V
#[must_use]
pub fn pressure(
    particles: &[Particle],
    volume: f64,
    virial: f64,
) -> f64 {
    if particles.is_empty() || volume <= 0.0 {
        return 0.0;
    }

    let n = particles.len() as f64;

    let t = temperature(particles);

    // In reduced units (k_B = 1)
    n.mul_add(t, virial) / volume
}

/// Removes center of mass velocity from the system.
///
/// # Errors
/// Returns an error if the particle list is empty.
pub fn remove_com_velocity(particles: &mut [Particle]) -> Result<(), String> {
    if particles.is_empty() {
        return Ok(());
    }

    let dim = particles[0].position.len();

    let mut total_momentum = vec![0.0; dim];

    let mut total_mass = 0.0;

    for p in particles.iter() {
        total_mass += p.mass;

        for (i, v) in p.velocity.iter().enumerate() {
            total_momentum[i] += p.mass * v;
        }
    }

    let com_velocity: Vec<f64> = total_momentum.iter().map(|m| m / total_mass).collect();

    for p in particles.iter_mut() {
        for (i, v) in p.velocity.iter_mut().enumerate() {
            *v -= com_velocity[i];
        }
    }

    Ok(())
}

// ============================================================================
// Thermostats
// ============================================================================

/// Velocity rescaling thermostat.
///
/// Rescales velocities to achieve target temperature.
pub fn velocity_rescale(
    particles: &mut [Particle],
    target_temp: f64,
) {
    let current_temp = temperature(particles);

    if current_temp <= 0.0 {
        return;
    }

    let scale = (target_temp / current_temp).sqrt();

    for p in particles.iter_mut() {
        for v in &mut p.velocity {
            *v *= scale;
        }
    }
}

/// Berendsen thermostat.
///
/// Gently couples the system to a heat bath.
///
/// # Arguments
/// * `particles` - System particles
/// * `target_temp` - Target temperature
/// * `tau` - Coupling time constant
/// * `dt` - Time step
pub fn berendsen_thermostat(
    particles: &mut [Particle],
    target_temp: f64,
    tau: f64,
    dt: f64,
) {
    let current_temp = temperature(particles);

    if current_temp <= 0.0 {
        return;
    }

    let scale = (dt / tau)
        .mul_add(target_temp / current_temp - 1.0, 1.0)
        .sqrt();

    for p in particles.iter_mut() {
        for v in &mut p.velocity {
            *v *= scale;
        }
    }
}

// ============================================================================
// Periodic Boundary Conditions
// ============================================================================

/// Applies periodic boundary conditions to a position.
#[must_use]
pub fn apply_pbc(
    position: &[f64],
    box_size: &[f64],
) -> Vec<f64> {
    position
        .iter()
        .zip(box_size.iter())
        .map(|(&x, &l)| {
            let mut wrapped = x % l;

            if wrapped < 0.0 {
                wrapped += l;
            }

            wrapped
        })
        .collect()
}

/// Applies minimum image convention for distance calculation.
#[must_use]
pub fn minimum_image_distance(
    r: &[f64],
    box_size: &[f64],
) -> Vec<f64> {
    r.iter()
        .zip(box_size.iter())
        .map(|(&dx, &l)| {
            let mut d = dx % l;

            if d > l / 2.0 {
                d -= l;
            } else if d < -l / 2.0 {
                d += l;
            }

            d
        })
        .collect()
}

// ============================================================================
// Analysis Functions
// ============================================================================

/// Calculates the radial distribution function g(r).
///
/// # Arguments
/// * `particles` - System particles
/// * `box_size` - Box dimensions
/// * `num_bins` - Number of histogram bins
/// * `r_max` - Maximum distance to consider
///
/// # Returns
/// (`r_values`, `g_r`) where `r_values` are bin centers and `g_r` are g(r) values
#[must_use]
pub fn radial_distribution_function(
    particles: &[Particle],
    box_size: &[f64],
    num_bins: usize,
    r_max: f64,
) -> (Vec<f64>, Vec<f64>) {
    let n = particles.len();

    if n < 2 {
        return (vec![], vec![]);
    }

    let dr = r_max / num_bins as f64;

    let mut histogram = vec![0usize; num_bins];

    // Count pairs in each bin
    for i in 0..n {
        for j in (i + 1)..n {
            if let Ok(r_vec) = vec_sub(&particles[i].position, &particles[j].position) {
                let r_mic = minimum_image_distance(&r_vec, box_size);

                let r = norm(&r_mic);

                if r < r_max {
                    // Safe to cast as r and dr are positive
                    let bin: usize = ((r / dr) as i64).try_into().unwrap_or(0);

                    if bin < num_bins {
                        histogram[bin] += 2; // Count both i-j and j-i
                    }
                }
            }
        }
    }

    // Normalize by ideal gas distribution
    let volume: f64 = box_size.iter().product();

    let rho = n as f64 / volume;

    let pi = std::f64::consts::PI;

    let r_values: Vec<f64> = (0..num_bins).map(|i| (i as f64 + 0.5) * dr).collect();

    let g_r: Vec<f64> = histogram
        .iter()
        .enumerate()
        .map(|(i, &count)| {
            let _r = (i as f64 + 0.5) * dr;

            let shell_volume =
                (4.0 / 3.0) * pi * (((i + 1) as f64 * dr).powi(3) - (i as f64 * dr).powi(3));

            let ideal_count = rho * shell_volume * n as f64;

            if ideal_count > 0.0 {
                count as f64 / ideal_count
            } else {
                0.0
            }
        })
        .collect();

    (r_values, g_r)
}

/// Calculates mean square displacement.
///
/// MSD(t) = <|r(t) - r(0)|²>
#[must_use]
pub fn mean_square_displacement(
    initial: &[Particle],
    current: &[Particle],
) -> f64 {
    if initial.len() != current.len() || initial.is_empty() {
        return 0.0;
    }

    let mut msd = 0.0;

    for (p0, p) in initial.iter().zip(current.iter()) {
        if let Ok(dr) = vec_sub(&p.position, &p0.position) {
            let dr2: f64 = dr.iter().map(|x| x * x).sum();

            msd += dr2;
        }
    }

    msd / initial.len() as f64
}

/// Initializes particle velocities from Maxwell-Boltzmann distribution.
pub fn initialize_velocities_maxwell_boltzmann(
    particles: &mut [Particle],
    target_temp: f64,
    rng_seed: u64,
) {
    // Simple pseudo-random generator (LCG)
    let mut rng_state = rng_seed;

    let _next_random = || {
        rng_state = rng_state
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1_442_695_040_888_963_407);

        (rng_state >> 33) as f64 / (1u64 << 31) as f64
    };

    // Box-Muller transform for Gaussian random numbers
    let gaussian = |rng: &mut dyn FnMut() -> f64| -> f64 {
        let u1 = rng();

        let u2 = rng();

        (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos()
    };

    for p in particles.iter_mut() {
        let sigma = (target_temp / p.mass).sqrt();

        for v in &mut p.velocity {
            // Create a mutable closure to use with gaussian
            let mut rng_fn = || {
                rng_state = rng_state
                    .wrapping_mul(6_364_136_223_846_793_005)
                    .wrapping_add(1_442_695_040_888_963_407);

                (rng_state >> 33) as f64 / (1u64 << 31) as f64
            };

            *v = sigma * gaussian(&mut rng_fn);
        }
    }

    // Remove center of mass velocity
    let _ = remove_com_velocity(particles);

    // Rescale to exact target temperature
    velocity_rescale(particles, target_temp);
}

/// Creates a simple cubic lattice of particles.
#[must_use]
pub fn create_cubic_lattice(
    n_per_side: usize,
    lattice_constant: f64,
    mass: f64,
) -> Vec<Particle> {
    let mut particles = Vec::with_capacity(n_per_side * n_per_side * n_per_side);

    let mut id = 0;

    for i in 0..n_per_side {
        for j in 0..n_per_side {
            for k in 0..n_per_side {
                let position = vec![
                    i as f64 * lattice_constant,
                    j as f64 * lattice_constant,
                    k as f64 * lattice_constant,
                ];

                let velocity = vec![0.0, 0.0, 0.0];

                particles.push(Particle::new(id, mass, position, velocity));

                id += 1;
            }
        }
    }

    particles
}

/// Creates an FCC (face-centered cubic) lattice of particles.
#[must_use]
pub fn create_fcc_lattice(
    n_cells: usize,
    lattice_constant: f64,
    mass: f64,
) -> Vec<Particle> {
    let mut particles = Vec::with_capacity(4 * n_cells * n_cells * n_cells);

    let mut id = 0;

    // FCC basis positions (in units of lattice constant)
    let basis = [
        [0.0, 0.0, 0.0],
        [0.5, 0.5, 0.0],
        [0.5, 0.0, 0.5],
        [0.0, 0.5, 0.5],
    ];

    for i in 0..n_cells {
        for j in 0..n_cells {
            for k in 0..n_cells {
                for b in &basis {
                    let position = vec![
                        (i as f64 + b[0]) * lattice_constant,
                        (j as f64 + b[1]) * lattice_constant,
                        (k as f64 + b[2]) * lattice_constant,
                    ];

                    let velocity = vec![0.0, 0.0, 0.0];

                    particles.push(Particle::new(id, mass, position, velocity));

                    id += 1;
                }
            }
        }
    }

    particles
}