physics_in_parallel 4.0.0-alpha.2

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
//! Validated undirected power-law interactions for canonical particle state.

use core::fmt;
use std::collections::HashSet;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::engines::soa::interaction::InteractionOrder;
use crate::engines::soa::phys_obj::PhysObj;
use crate::engines::soa::{Interaction, InteractionError};
use crate::models::laws::{PowerLawDecay, PowerLawError};
use crate::models::particles::attrs::{ATTR_A, ATTR_R, ParticleSelection};
use crate::models::particles::state::{
    ParticleStateError, gather_inverse_mass, gather_masks, validate_vector_attr_f64,
};

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum PowerLawNetworkError {
    State(ParticleStateError),
    Law(PowerLawError),
    SelfPair {
        particle: usize,
    },
    ParticleCountOverflow {
        particle: usize,
    },
    DuplicatePair {
        pair: (usize, usize),
    },
    EndpointOutOfBounds {
        particle: usize,
        particle_count: usize,
    },
    InvalidInverseMass {
        particle: usize,
        value: f64,
    },
    InternalInvariant {
        message: String,
    },
}

impl From<ParticleStateError> for PowerLawNetworkError {
    fn from(error: ParticleStateError) -> Self {
        Self::State(error)
    }
}

impl From<PowerLawError> for PowerLawNetworkError {
    fn from(error: PowerLawError) -> Self {
        Self::Law(error)
    }
}

impl fmt::Display for PowerLawNetworkError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::State(error) => write!(formatter, "invalid particle state: {error}"),
            Self::Law(error) => write!(formatter, "invalid power-law: {error}"),
            Self::SelfPair { particle } => write!(
                formatter,
                "power-law pair cannot repeat particle {particle}"
            ),
            Self::ParticleCountOverflow { particle } => write!(
                formatter,
                "power-law endpoint {particle} cannot be represented as a particle-count bound"
            ),
            Self::DuplicatePair { pair } => {
                write!(
                    formatter,
                    "power-law batch contains duplicate pair {pair:?}"
                )
            }
            Self::EndpointOutOfBounds {
                particle,
                particle_count,
            } => write!(
                formatter,
                "power-law endpoint {particle} is outside particle state with {particle_count} particles"
            ),
            Self::InvalidInverseMass { particle, value } => write!(
                formatter,
                "inverse mass at particle {particle} must be finite and non-negative; got {value}"
            ),
            Self::InternalInvariant { message } => {
                write!(formatter, "power-law-network invariant failed: {message}")
            }
        }
    }
}

impl std::error::Error for PowerLawNetworkError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::State(error) => Some(error),
            Self::Law(error) => Some(error),
            _ => None,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct PowerLawRecord {
    pair: (usize, usize),
    law: PowerLawDecay,
}

#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct PowerLawNetworkDocument {
    interactions: Vec<PowerLawRecord>,
}

#[derive(Debug, Clone)]
pub struct PowerLawNetwork {
    interactions: Interaction<PowerLawDecay>,
}

impl Default for PowerLawNetwork {
    fn default() -> Self {
        Self::new()
    }
}

impl Serialize for PowerLawNetwork {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        PowerLawNetworkDocument {
            interactions: self
                .iter()
                .map(|(pair, law)| PowerLawRecord { pair, law: *law })
                .collect(),
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for PowerLawNetwork {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let document = PowerLawNetworkDocument::deserialize(deserializer)?;
        let mut seen = HashSet::with_capacity(document.interactions.len());
        let mut network = Self::with_capacity(document.interactions.len());
        for record in document.interactions {
            let pair = canonical_pair(record.pair);
            if !seen.insert(pair) {
                return Err(serde::de::Error::custom(
                    PowerLawNetworkError::DuplicatePair { pair },
                ));
            }
            network
                .insert(pair, record.law)
                .map_err(serde::de::Error::custom)?;
        }
        Ok(network)
    }
}

impl PowerLawNetwork {
    pub fn new() -> Self {
        Self {
            interactions: Interaction::new(0, InteractionOrder::Unordered),
        }
    }

    pub fn with_capacity(edge_capacity: usize) -> Self {
        let mut network = Self::new();
        network.interactions.reserve(edge_capacity);
        network
    }

    pub fn len(&self) -> usize {
        self.interactions.len()
    }

    pub fn is_empty(&self) -> bool {
        self.interactions.is_empty()
    }

    pub fn minimum_particle_count(&self) -> usize {
        self.iter()
            .map(|((i, j), _)| i.max(j))
            .max()
            .map_or(0, |particle| particle + 1)
    }

    pub fn insert(
        &mut self,
        pair: (usize, usize),
        law: PowerLawDecay,
    ) -> Result<Option<PowerLawDecay>, PowerLawNetworkError> {
        law.validate()?;
        let pair = validate_pair(pair)?;
        let needed = pair
            .1
            .checked_add(1)
            .ok_or(PowerLawNetworkError::ParticleCountOverflow { particle: pair.1 })?;
        let previous = self.get(pair).copied();
        self.ensure_particle_bound(needed)?;
        self.interactions
            .set_pair(pair.0, pair.1, law)
            .map_err(invariant)?;
        Ok(previous)
    }

    pub fn insert_many(
        &mut self,
        entries: impl IntoIterator<Item = ((usize, usize), PowerLawDecay)>,
    ) -> Result<(), PowerLawNetworkError> {
        let mut checked = Vec::new();
        let mut seen = HashSet::new();
        let mut needed = self.interactions.n_objects();
        for (pair, law) in entries {
            law.validate()?;
            let pair = validate_pair(pair)?;
            if !seen.insert(pair) {
                return Err(PowerLawNetworkError::DuplicatePair { pair });
            }
            needed = needed.max(
                pair.1
                    .checked_add(1)
                    .ok_or(PowerLawNetworkError::ParticleCountOverflow { particle: pair.1 })?,
            );
            checked.push((pair, law));
        }

        let mut replacement = self.clone();
        replacement.ensure_particle_bound(needed)?;
        replacement.interactions.reserve(checked.len());
        for (pair, law) in checked {
            replacement
                .interactions
                .set_pair(pair.0, pair.1, law)
                .map_err(invariant)?;
        }
        *self = replacement;
        Ok(())
    }

    /// Inserts one law on every unordered pair in `0..particle_count`.
    pub fn insert_all_to_all(
        &mut self,
        particle_count: usize,
        law: PowerLawDecay,
    ) -> Result<(), PowerLawNetworkError> {
        law.validate()?;
        let pair_count = particle_count
            .checked_mul(particle_count.saturating_sub(1))
            .and_then(|count| count.checked_div(2))
            .ok_or(PowerLawNetworkError::ParticleCountOverflow {
                particle: particle_count,
            })?;
        let entries =
            (0..particle_count).flat_map(|i| ((i + 1)..particle_count).map(move |j| ((i, j), law)));
        let mut replacement = self.clone();
        replacement.interactions.reserve(pair_count);
        replacement.insert_many(entries)?;
        *self = replacement;
        Ok(())
    }

    pub fn get(&self, pair: (usize, usize)) -> Option<&PowerLawDecay> {
        let pair = canonical_pair(pair);
        if pair.1 >= self.interactions.n_objects() || pair.0 == pair.1 {
            return None;
        }
        self.interactions.get_pair(pair.0, pair.1).ok().flatten()
    }

    pub fn remove(
        &mut self,
        pair: (usize, usize),
    ) -> Result<Option<PowerLawDecay>, PowerLawNetworkError> {
        let pair = canonical_pair(pair);
        if pair.1 >= self.interactions.n_objects() || pair.0 == pair.1 {
            return Ok(None);
        }
        Ok(self
            .interactions
            .remove_pair(pair.0, pair.1)
            .map_err(invariant)?
            .map(|(_, law)| law))
    }

    pub fn iter(&self) -> impl Iterator<Item = ((usize, usize), &PowerLawDecay)> + '_ {
        self.interactions
            .iter()
            .filter(|(_, nodes, _)| nodes.nodes.len() == 2)
            .map(|(_, nodes, law)| ((nodes.nodes[0], nodes.nodes[1]), law))
    }

    pub fn clear(&mut self) {
        self.interactions.clear();
    }

    pub fn apply(
        &self,
        objects: &mut PhysObj,
        selection: ParticleSelection,
    ) -> Result<(), PowerLawNetworkError> {
        let (dim, particle_count, positions) = {
            let positions = objects
                .core
                .get::<f64>(ATTR_R)
                .map_err(ParticleStateError::from)?;
            (
                positions.dim(),
                positions.num_vectors(),
                positions.logical_values(),
            )
        };
        validate_vector_attr_f64(objects, ATTR_A, dim, particle_count)?;
        let inverse_mass = gather_inverse_mass(objects, particle_count)?;
        let masks = gather_masks(objects, particle_count, selection)?;
        for (particle, &value) in inverse_mass.iter().enumerate() {
            if !value.is_finite() || value < 0.0 {
                return Err(PowerLawNetworkError::InvalidInverseMass { particle, value });
            }
        }

        let mut acceleration = vec![0.0; particle_count * dim];
        for ((i, j), law) in self.iter() {
            for endpoint in [i, j] {
                if endpoint >= particle_count {
                    return Err(PowerLawNetworkError::EndpointOutOfBounds {
                        particle: endpoint,
                        particle_count,
                    });
                }
            }
            if !masks.is_included(selection, i) || !masks.is_included(selection, j) {
                continue;
            }
            let i_base = i * dim;
            let j_base = j * dim;
            let norm_squared = (0..dim)
                .map(|component| {
                    let delta = positions[i_base + component] - positions[j_base + component];
                    delta * delta
                })
                .sum::<f64>();
            if !norm_squared.is_finite() || norm_squared <= f64::EPSILON {
                continue;
            }
            let norm = norm_squared.sqrt();
            if law
                .range()
                .is_some_and(|(minimum, maximum)| norm < minimum || norm > maximum)
            {
                continue;
            }
            let scale = law.strength() * norm.powf(law.exponent() - 1.0);
            let i_rigid = masks.rigid.as_ref().is_some_and(|flags| flags[i]);
            let j_rigid = masks.rigid.as_ref().is_some_and(|flags| flags[j]);
            for component in 0..dim {
                let force = (positions[i_base + component] - positions[j_base + component]) * scale;
                if !i_rigid {
                    acceleration[i_base + component] += force * inverse_mass[i];
                }
                if !j_rigid {
                    acceleration[j_base + component] -= force * inverse_mass[j];
                }
            }
        }

        let output = objects
            .core
            .get_mut::<f64>(ATTR_A)
            .map_err(ParticleStateError::from)?;
        output.edit_values(|values| {
            for (destination, contribution) in values.iter_mut().zip(acceleration) {
                *destination += contribution;
            }
        });
        Ok(())
    }

    fn ensure_particle_bound(&mut self, needed: usize) -> Result<(), PowerLawNetworkError> {
        if needed > self.interactions.n_objects() {
            self.interactions.set_n_objects(needed).map_err(invariant)?;
        }
        Ok(())
    }
}

fn canonical_pair((i, j): (usize, usize)) -> (usize, usize) {
    if i <= j { (i, j) } else { (j, i) }
}

fn validate_pair(pair: (usize, usize)) -> Result<(usize, usize), PowerLawNetworkError> {
    let pair = canonical_pair(pair);
    if pair.0 == pair.1 {
        return Err(PowerLawNetworkError::SelfPair { particle: pair.0 });
    }
    Ok(pair)
}

fn invariant(error: InteractionError) -> PowerLawNetworkError {
    PowerLawNetworkError::InternalInvariant {
        message: error.to_string(),
    }
}