physics_in_parallel 3.0.0

High-performance infrastructure for numerical simulations in physics
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
/*!
Square-lattice representation for discrete spaces.

Purpose:
    This module defines a square, cubic, or hypercubic lattice topology over a
    tensor-style shape such as `[128]`, `[64, 64]`, or `[32, 64, 16]`. The shape
    lists the number of lattice sites on each axis, exactly like a tensor shape.
    Future discrete-space families with different neighbor geometry should live
    in sibling modules with their own names and invariants.

Storage model:
    - SquareLattice sites are stored in a dense rank-N tensor with the requested
      shape.
    - SquareLattice code handles spatial semantics: boundary normalization, vacancy
      sentinels, shape validation, initialization, and point downsampling.
    - Tensor code handles dense storage, row-major indexing, parallel fill, and
      scalar type constraints.

Boundary conditions:
    - `Periodic`: coordinates wrap around the lattice, so `-1` selects the last
      site on that axis.
    - `Reflective`: coordinates reflect at the lattice walls, modelling a
      hard-wall mirror boundary for index lookup.
*/

use std::path::PathBuf;

use rand::random_range;
use rayon::prelude::*;

use crate::math::prelude::{DenseBackend, Scalar, ScalarSerde, Tensor};
use crate::space::space_trait::Space;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BoundaryCondition {
    Periodic,
    Reflective,
}

impl BoundaryCondition {
    #[inline]
    pub fn normalize(self, coord: isize, side_len: usize) -> usize {
        debug_assert!(side_len > 0);
        match self {
            Self::Periodic => wrap_periodic(coord, side_len),
            Self::Reflective => reflect_coordinate(coord, side_len),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SquareLatticeConfig {
    pub shape: Vec<usize>,
    pub boundary: BoundaryCondition,
}

impl SquareLatticeConfig {
    #[inline]
    pub fn new(shape: &[usize], boundary: BoundaryCondition) -> Self {
        assert!(
            !shape.is_empty(),
            "SquareLatticeConfig requires at least one axis"
        );
        assert!(
            shape.iter().all(|&n| n > 0),
            "SquareLatticeConfig requires every axis length to be nonzero; got {shape:?}"
        );
        Self {
            shape: shape.to_vec(),
            boundary,
        }
    }

    #[inline]
    pub fn periodic(shape: &[usize]) -> Self {
        Self::new(shape, BoundaryCondition::Periodic)
    }

    #[inline]
    pub fn reflective(shape: &[usize]) -> Self {
        Self::new(shape, BoundaryCondition::Reflective)
    }

    #[inline]
    pub fn rank(&self) -> usize {
        self.shape.len()
    }

    #[inline]
    pub fn num_sites(&self) -> usize {
        self.shape
            .iter()
            .copied()
            .try_fold(1usize, |acc, dim| {
                acc.checked_mul(dim)
                    .ok_or("square lattice site count overflow")
            })
            .expect("square lattice site count overflow")
    }

    #[inline]
    pub fn tensor_shape(&self) -> Vec<usize> {
        self.shape.clone()
    }

    #[inline]
    pub fn shape(&self) -> &[usize] {
        &self.shape
    }
}

#[derive(Debug, Clone)]
pub enum SquareLatticeInitMethod<T: Scalar> {
    Empty,
    Uniform { val: T },
    RandomUniformChoices { choices: Vec<T> },
    SeededCenter { val: T },
}

#[derive(Debug, Clone)]
pub struct SquareLattice<T: Scalar> {
    pub cfg: SquareLatticeConfig,
    cells: Tensor<T, DenseBackend>,
}

pub trait VacancyValue: Sized + Clone {
    const VACANCY: Self;

    #[inline]
    fn vacancy() -> Self {
        Self::VACANCY
    }
}

impl VacancyValue for usize {
    const VACANCY: usize = 0;
}
impl VacancyValue for u64 {
    const VACANCY: u64 = 0;
}
impl VacancyValue for u32 {
    const VACANCY: u32 = 0;
}
impl VacancyValue for u16 {
    const VACANCY: u16 = 0;
}
impl VacancyValue for u8 {
    const VACANCY: u8 = 0;
}
impl VacancyValue for isize {
    const VACANCY: isize = 0;
}
impl VacancyValue for i64 {
    const VACANCY: i64 = 0;
}
impl VacancyValue for i32 {
    const VACANCY: i32 = 0;
}
impl VacancyValue for i16 {
    const VACANCY: i16 = 0;
}
impl VacancyValue for i8 {
    const VACANCY: i8 = 0;
}
impl VacancyValue for f64 {
    const VACANCY: f64 = 0.0;
}
impl VacancyValue for f32 {
    const VACANCY: f32 = 0.0;
}

impl<T: Scalar + VacancyValue> SquareLattice<T> {
    pub fn new(cfg: SquareLatticeConfig, init_method: SquareLatticeInitMethod<T>) -> Self {
        let mut lattice = Self {
            cells: Tensor::<T, DenseBackend>::empty(&cfg.tensor_shape()),
            cfg,
        };

        match init_method {
            SquareLatticeInitMethod::Empty => {}
            SquareLatticeInitMethod::Uniform { val } => lattice.cells.fill(val),
            SquareLatticeInitMethod::RandomUniformChoices { choices } => {
                assert!(
                    !choices.is_empty(),
                    "RandomUniformChoices requires at least one choice"
                );
                lattice
                    .cells_mut()
                    .par_iter_mut()
                    .for_each(|slot| *slot = choices[random_range(0..choices.len())]);
            }
            SquareLatticeInitMethod::SeededCenter { val } => {
                let center: Vec<isize> = lattice
                    .cfg
                    .shape
                    .iter()
                    .map(|&axis_len| (axis_len / 2) as isize)
                    .collect();
                lattice.cells.set(&center, val);
            }
        }

        lattice
    }

    #[inline]
    pub fn vacancy() -> T {
        T::vacancy()
    }

    #[inline]
    pub fn set_vacant(&mut self, coord: &[isize]) {
        let coord = self.boundary_index(coord);
        self.cells.set(&coord, Self::vacancy());
    }

    #[inline]
    pub fn is_vacant(&self, coord: &[isize]) -> bool {
        let coord = self.boundary_index(coord);
        self.cells.get(&coord) == Self::vacancy()
    }

    #[inline]
    pub fn fill_vacancy(&mut self) {
        self.cells.fill(Self::vacancy());
    }

    pub fn downsample(&self, target_shape: &[usize]) -> Self {
        assert_eq!(
            target_shape.len(),
            self.cfg.rank(),
            "downsample rank mismatch: expected {}, got {}",
            self.cfg.rank(),
            target_shape.len()
        );
        assert!(
            target_shape.iter().all(|&n| n > 0),
            "downsample target shape must have only nonzero axis lengths; got {target_shape:?}"
        );
        assert!(
            target_shape
                .iter()
                .zip(self.cfg.shape.iter())
                .all(|(&new_dim, &old_dim)| new_dim <= old_dim),
            "downsample target shape must not exceed source shape: source {:?}, target {target_shape:?}",
            self.cfg.shape
        );
        if target_shape == self.cfg.shape.as_slice() {
            return self.clone();
        }

        let d = self.cfg.rank();
        let scale: Vec<f64> = self
            .cfg
            .shape
            .iter()
            .zip(target_shape.iter())
            .map(|(&old_dim, &new_dim)| old_dim as f64 / new_dim as f64)
            .collect();
        let new_cfg = SquareLatticeConfig::new(target_shape, self.cfg.boundary);
        let mut new = Self {
            cells: Tensor::<T, DenseBackend>::empty(&new_cfg.tensor_shape()),
            cfg: new_cfg,
        };

        new.cells_mut()
            .par_iter_mut()
            .enumerate()
            .for_each(|(flat, slot)| {
                let mut rem = flat;
                let mut coord_new = vec![0usize; d];
                for axis in (0..d).rev() {
                    coord_new[axis] = rem % target_shape[axis];
                    rem /= target_shape[axis];
                }

                let coord_old: Vec<isize> = coord_new
                    .iter()
                    .enumerate()
                    .map(|(axis, &x)| (x as f64 * scale[axis]).floor() as isize)
                    .collect();
                let coord_old = self.boundary_index(&coord_old);
                *slot = self.cells.get(&coord_old);
            });

        new
    }

    #[inline]
    pub fn rescale(&self, target_shape: &[usize]) -> Self {
        self.downsample(target_shape)
    }
}

impl<T: Scalar> SquareLattice<T> {
    #[inline]
    pub fn data(&self) -> &[T] {
        self.cells.storage().data()
    }

    #[inline]
    pub(crate) fn cells_mut(&mut self) -> &mut [T] {
        self.cells.storage_mut().data_mut()
    }

    #[inline]
    pub(crate) fn tensor_shape(&self) -> Vec<usize> {
        self.cfg.tensor_shape()
    }

    #[inline]
    pub(crate) fn from_parts(cfg: SquareLatticeConfig, data: Vec<T>) -> Self {
        let expected = cfg.num_sites();
        assert_eq!(
            data.len(),
            expected,
            "lattice data length mismatch: expected {expected}, got {}",
            data.len()
        );
        Self {
            cells: Tensor::<T, DenseBackend>::from_vec(&cfg.tensor_shape(), data),
            cfg,
        }
    }

    #[inline]
    pub(crate) fn boundary_index(&self, coord: &[isize]) -> Vec<isize> {
        assert_eq!(
            coord.len(),
            self.cfg.rank(),
            "lattice coordinate rank mismatch: expected {}, got {}",
            self.cfg.rank(),
            coord.len()
        );
        coord
            .iter()
            .zip(self.cfg.shape.iter())
            .map(|(&c, &axis_len)| self.cfg.boundary.normalize(c, axis_len) as isize)
            .collect()
    }
}

impl<T: ScalarSerde + VacancyValue> Space<T> for SquareLattice<T> {
    #[inline]
    fn data(&self) -> &[T] {
        self.data()
    }

    #[inline]
    fn dims(&self) -> Vec<usize> {
        self.cfg.tensor_shape()
    }

    #[inline]
    fn linear_size(&self) -> usize {
        self.cfg.num_sites()
    }

    #[inline]
    fn get(&self, coord: &[isize]) -> &T {
        let coord = self.boundary_index(coord);
        self.cells.get_mut_for_ref(&coord)
    }

    #[inline]
    fn get_mut(&mut self, coord: &[isize]) -> &mut T {
        let coord = self.boundary_index(coord);
        self.cells.get_mut(&coord)
    }

    #[inline]
    fn set(&mut self, coord: &[isize], val: T) {
        let coord = self.boundary_index(coord);
        self.cells.set(&coord, val);
    }

    #[inline]
    fn save(&self, output_file: &PathBuf, l_target: usize) -> std::io::Result<()> {
        let target_shape = vec![l_target; self.cfg.rank()];
        crate::space::io::square_lattice::save_square_lattice(self, &target_shape, output_file)
    }

    #[inline]
    fn set_all(&mut self, val: T) {
        self.cells.fill(val);
    }
}

#[inline]
fn wrap_periodic(coord: isize, side_len: usize) -> usize {
    let side_len = side_len as isize;
    let mut wrapped = coord % side_len;
    if wrapped < 0 {
        wrapped += side_len;
    }
    wrapped as usize
}

#[inline]
fn reflect_coordinate(coord: isize, side_len: usize) -> usize {
    if side_len == 1 {
        return 0;
    }
    let period = 2 * (side_len as isize - 1);
    let mut reflected = coord % period;
    if reflected < 0 {
        reflected += period;
    }
    if reflected >= side_len as isize {
        (period - reflected) as usize
    } else {
        reflected as usize
    }
}

trait TensorRefGet<T: Scalar> {
    fn get_mut_for_ref(&self, coord: &[isize]) -> &T;
}

impl<T: Scalar> TensorRefGet<T> for Tensor<T, DenseBackend> {
    #[inline]
    fn get_mut_for_ref(&self, coord: &[isize]) -> &T {
        let data = self.storage().data();
        let shape = self.shape();
        let mut flat = 0usize;
        for (&c, &dim) in coord.iter().zip(shape.iter()) {
            flat = flat * dim + c as usize;
        }
        &data[flat]
    }
}