pyrucast 0.3.0

Finite-element library in Rust, inspired by cast3m, exposed to Python
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
//! Coords — node coordinates with garbage collection.
//!
//! A [`Coords`] holds **one or more configurations** (sets of coordinates)
//! for the same set of nodes, in a fixed dimension.
//!
//! # Node identity
//!
//! Every created node receives a **stable** internal identifier
//! ([`NodeId`]), unique for the lifetime of the `Coords`: **no id
//! is ever reused**, even after garbage collection. Other objects (meshes,
//! fields) can therefore reference a node by id without worrying about
//! stability.
//!
//! # Deletion policy: no direct removal
//!
//! There is **no** `remove_node` method. A referenced node is protected.
//! Only the garbage collector [`Coords::gc`] reclaims nodes whose
//! **internal** refcount has reached 0.
//!
//! # Two-level refcount model
//!
//! - The **Coords object** is kept alive by the usual
//!   [`crate::handle::Handle`] refcount.
//! - **Each node** inside the Coords has its own refcount,
//!   manipulated via [`Coords::incref`] / [`Coords::decref`]
//!   (used by [`crate::atoms::Node`] and, later, by meshes and fields).
//!
//! # Identity vs solver ordering
//!
//! An optional permutation (`Vec<u32>`) separates the **solver order**
//! from the **identity**: `permutation[node_id]` is the solver-order index
//! assigned to `node_id`. It is set by the caller today; a bandwidth-reducing
//! renumbering (Cuthill–McKee) will compute it. The identity (`NodeId`) is
//! never modified either way.
//!
//! # Multiple configurations
//!
//! Useful for switching between reference / deformed / predicted
//! configurations. An active configuration is designated by index;
//! [`Coords::position`] reads from the active one.
//!
//! # Coordinate frame
//!
//! A `Coords` also declares **how its coordinates are to be read**: plain
//! Cartesian (the default, [`Coords::new`]) or **axisymmetric**
//! ([`Coords::axisymmetric`]) — a 2-D meridian plane `(r, z)` describing a body
//! of revolution. The frame is a property of the geometry, so every mesh, FE
//! space and integral built on top of it inherits it; see [`CoordinateFrame`].
//!
//! # Example
//!
//! ```
//! use pyrucast::atoms::NodeId;
//! use pyrucast::coords::Coords;
//! use pyrucast::handle::Handle;
//!
//! let h = Handle::new(Coords::new(2).unwrap());
//! let a: NodeId = h.write().add_node(&[0.0, 0.0]).unwrap();
//! // add_node initializes refcount = 1: without decref, the node is protected.
//! assert_eq!(h.write().gc(), 0);
//! // After decref, refcount drops to 0 and gc collects it.
//! let mut c = h.write();
//! c.decref(a).unwrap();
//! assert_eq!(c.gc(), 1);
//! ```

use crate::atoms::NodeId;
use crate::error::{PyrucastError, Result};
use serde::{Deserialize, Serialize};
use std::fmt;

/// How the coordinates of a [`Coords`] are to be read — the geometric
/// hypothesis every integral built on top of it obeys.
///
/// This is the Cast3M `OPTI MODE` axis: it belongs to the **geometry**, not to
/// any one physics, because it changes the integration measure `dΩ` itself —
/// stiffness, mass, distributed flux, volumes and internal forces alike.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum CoordinateFrame {
    /// Plain Cartesian coordinates in `dim` dimensions, `dΩ = |J| dξ`.
    #[default]
    Cartesian,
    /// 2-D meridian plane of a body of revolution: `x = r` (radius, `≥ 0`),
    /// `y = z` (axis of revolution). Integrals run over the **full ring**,
    /// `dΩ = 2πr |J| dξ`, so masses, volumes and nodal resultants are those of
    /// the whole revolved part.
    Axisymmetric,
}

impl CoordinateFrame {
    /// Whether this frame is [`Axisymmetric`](Self::Axisymmetric).
    pub fn is_axisymmetric(self) -> bool {
        self == Self::Axisymmetric
    }
}

impl fmt::Display for CoordinateFrame {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Cartesian => "cartesian",
            Self::Axisymmetric => "axisymmetric",
        })
    }
}

/// Node coordinates with stable identity, multiple configurations,
/// optional solver permutation, and a garbage collector for unreferenced
/// nodes.
#[derive(Serialize, Deserialize)]
pub struct Coords {
    dim: u8,
    /// Geometric hypothesis the coordinates obey. `#[serde(default)]` so a
    /// `Coords` serialised before the frame existed reloads as Cartesian.
    #[serde(default)]
    frame: CoordinateFrame,
    /// `configs[c][id * dim + k]` — each configuration holds `capacity * dim` values.
    configs: Vec<Vec<f64>>,
    config_names: Vec<String>,
    active: usize,
    /// `alive[id] == false` ⇒ collected by the GC. Once `false`, stays so forever.
    alive: Vec<bool>,
    /// Per-node refcount. The GC collects `alive` nodes whose refcount is 0.
    ///
    /// **Never archived.** A file holds some of the objects that reference these
    /// nodes, not all of them, so a saved count would be a count of a world that
    /// no longer exists. It is recounted from zero at reload: `on_load` zeroes
    /// it, then each reloaded `SubMesh` re-increments what it uses.
    #[serde(skip)]
    refcount: Vec<u32>,
    /// Solver permutation (length == capacity) or `None` for identity.
    permutation: Option<Vec<u32>>,
}

impl Coords {
    /// Create an empty **Cartesian** `Coords` in dimension `dim` (≥ 1). A first
    /// configuration named `"default"` is created automatically. For a body of
    /// revolution, see [`Coords::axisymmetric`].
    pub fn new(dim: u8) -> Result<Self> {
        if dim == 0 {
            return Err(PyrucastError::Message("dim must be ≥ 1".into()));
        }
        Ok(Self {
            dim,
            frame: CoordinateFrame::Cartesian,
            configs: vec![Vec::new()],
            config_names: vec!["default".into()],
            active: 0,
            alive: Vec::new(),
            refcount: Vec::new(),
            permutation: None,
        })
    }

    /// Create an empty **axisymmetric** `Coords` — the 2-D meridian plane of a
    /// body of revolution, `x = r` (radius, `≥ 0`) and `y = z` (axis). The
    /// dimension is necessarily 2, so it is not an argument.
    ///
    /// Every FE space built over this geometry integrates over the full ring
    /// (`dΩ = 2πr |J| dξ`); mechanics additionally gains the hoop strain
    /// `ε_θθ = u_r / r` through
    /// [`ElasticityModel::Axisymmetric`](crate::models::elasticity::ElasticityModel::Axisymmetric).
    ///
    /// ```
    /// use pyrucast::coords::Coords;
    ///
    /// let c = Coords::axisymmetric().unwrap();
    /// assert_eq!(c.dim(), 2);
    /// assert!(c.is_axisymmetric());
    /// ```
    pub fn axisymmetric() -> Result<Self> {
        Ok(Self {
            frame: CoordinateFrame::Axisymmetric,
            ..Self::new(2)?
        })
    }

    /// Geometric dimension.
    pub fn dim(&self) -> u8 {
        self.dim
    }

    /// Geometric hypothesis these coordinates obey.
    pub fn frame(&self) -> CoordinateFrame {
        self.frame
    }

    /// Whether these coordinates describe a body of revolution — the shorthand
    /// for `frame().is_axisymmetric()`.
    pub fn is_axisymmetric(&self) -> bool {
        self.frame.is_axisymmetric()
    }

    /// Reject a negative radius in an axisymmetric frame (`x = r ≥ 0`), where
    /// `what` names the calling operation.
    fn check_radius(&self, what: &str, coords: &[f64]) -> Result<()> {
        if self.frame.is_axisymmetric() && coords[0] < 0.0 {
            return Err(PyrucastError::Message(format!(
                "{what}: negative radius x = {} in an axisymmetric Coords \
                 (x = r ≥ 0, y = z along the axis of revolution)",
                coords[0]
            )));
        }
        Ok(())
    }

    /// Number of live (not collected) nodes.
    pub fn node_count(&self) -> usize {
        self.alive.iter().filter(|&&a| a).count()
    }

    /// Capacity (total slots, live + collected). Never decreases.
    pub fn capacity(&self) -> usize {
        self.alive.len()
    }

    /// Whether a node is still alive.
    pub fn is_alive(&self, id: NodeId) -> bool {
        self.alive.get(id.0 as usize).copied().unwrap_or(false)
    }

    /// Add a node with these coordinates in **all** configurations. Initializes its
    /// refcount to 1 — the caller is responsible for at least one decrement
    /// (typically through the end-of-life of a [`crate::atoms::Node`]).
    pub fn add_node(&mut self, coords: &[f64]) -> Result<NodeId> {
        if coords.len() != self.dim as usize {
            return Err(PyrucastError::Message(format!(
                "add_node: expected {} coordinates, got {}",
                self.dim,
                coords.len()
            )));
        }
        self.check_radius("add_node", coords)?;
        let id = self.alive.len() as u32;
        for set in &mut self.configs {
            set.extend_from_slice(coords);
        }
        self.alive.push(true);
        self.refcount.push(1);
        if let Some(perm) = &mut self.permutation {
            perm.push(id);
        }
        Ok(NodeId(id))
    }

    /// Increment the refcount of a live node.
    pub fn incref(&mut self, id: NodeId) -> Result<()> {
        self.ensure_alive(id)?;
        let r = &mut self.refcount[id.0 as usize];
        *r = r.saturating_add(1);
        Ok(())
    }

    /// Decrement the refcount of a live node. The node is not immediately
    /// collected even if the refcount reaches 0: call
    /// [`Coords::gc`] for that.
    pub fn decref(&mut self, id: NodeId) -> Result<()> {
        self.ensure_alive(id)?;
        let r = &mut self.refcount[id.0 as usize];
        if *r == 0 {
            return Err(PyrucastError::Message(format!(
                "decref: refcount already zero for node {}",
                id.0
            )));
        }
        *r -= 1;
        Ok(())
    }

    /// Current refcount of a node (0 for a collected or unknown node).
    pub fn refcount(&self, id: NodeId) -> u32 {
        if self.is_alive(id) {
            self.refcount[id.0 as usize]
        } else {
            0
        }
    }

    /// Garbage collector: mark as collected every live node whose refcount
    /// is 0. Returns the number of collected nodes. Ids are never reused.
    pub fn gc(&mut self) -> usize {
        let mut collected = 0;
        for i in 0..self.alive.len() {
            if self.alive[i] && self.refcount[i] == 0 {
                self.alive[i] = false;
                collected += 1;
            }
        }
        collected
    }

    /// Coordinates of a node in the active configuration. Error if the node was
    /// collected or never existed.
    pub fn position(&self, id: NodeId) -> Result<&[f64]> {
        self.ensure_alive(id)?;
        let d = self.dim as usize;
        let s = id.0 as usize * d;
        Ok(&self.configs[self.active][s..s + d])
    }

    /// Set the coordinates of a node in the active configuration.
    pub fn set_position(&mut self, id: NodeId, coords: &[f64]) -> Result<()> {
        self.ensure_alive(id)?;
        if coords.len() != self.dim as usize {
            return Err(PyrucastError::Message(format!(
                "set_position: expected {} coordinates, got {}",
                self.dim,
                coords.len()
            )));
        }
        self.check_radius("set_position", coords)?;
        let d = self.dim as usize;
        let s = id.0 as usize * d;
        self.configs[self.active][s..s + d].copy_from_slice(coords);
        Ok(())
    }

    fn ensure_alive(&self, id: NodeId) -> Result<()> {
        if !self.is_alive(id) {
            return Err(PyrucastError::Message(format!(
                "node {} not found or collected",
                id.0
            )));
        }
        Ok(())
    }

    /// Iterate over live NodeIds in ascending id order.
    pub fn iter_live(&self) -> impl Iterator<Item = NodeId> + '_ {
        self.alive
            .iter()
            .enumerate()
            .filter_map(|(i, &a)| a.then_some(NodeId(i as u32)))
    }

    // ─── Configurations ───

    /// Add a new configuration by cloning the active one. Returns its index.
    pub fn add_config(&mut self, name: impl Into<String>) -> usize {
        let copy = self.configs[self.active].clone();
        self.configs.push(copy);
        self.config_names.push(name.into());
        self.configs.len() - 1
    }

    /// Select the active configuration by index.
    pub fn select(&mut self, config: usize) -> Result<()> {
        if config >= self.configs.len() {
            return Err(PyrucastError::Message(format!(
                "select: index {} ≥ configuration count ({})",
                config,
                self.configs.len()
            )));
        }
        self.active = config;
        Ok(())
    }

    /// Index of the active configuration.
    pub fn active(&self) -> usize {
        self.active
    }

    /// Names of the configurations, in order.
    pub fn names(&self) -> &[String] {
        &self.config_names
    }

    // ─── Solver permutation ───

    /// Current permutation (length = capacity), or `None` for identity.
    pub fn permutation(&self) -> Option<&[u32]> {
        self.permutation.as_deref()
    }

    /// Set the solver permutation. Its length must equal `capacity`; each
    /// value must be unique and within `[0, capacity)`.
    pub fn set_permutation(&mut self, perm: Vec<u32>) -> Result<()> {
        let cap = self.capacity();
        if perm.len() != cap {
            return Err(PyrucastError::Message(format!(
                "set_permutation: length {} ≠ capacity {}",
                perm.len(),
                cap
            )));
        }
        let cap_u = cap as u32;
        let mut seen = vec![false; cap];
        for &v in &perm {
            if v >= cap_u {
                return Err(PyrucastError::Message(format!(
                    "set_permutation: value {} ≥ capacity {}",
                    v, cap_u
                )));
            }
            let i = v as usize;
            if seen[i] {
                return Err(PyrucastError::Message(format!(
                    "set_permutation: duplicate value {}",
                    v
                )));
            }
            seen[i] = true;
        }
        self.permutation = Some(perm);
        Ok(())
    }

    /// Clear the permutation (back to identity).
    pub fn clear_permutation(&mut self) {
        self.permutation = None;
    }
}

impl fmt::Debug for Coords {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Coords")
            .field("dim", &self.dim)
            .field("frame", &self.frame)
            .field("configs", &self.config_names)
            .field("active", &self.active)
            .field("node_count", &self.node_count())
            .field("capacity", &self.capacity())
            .field("permutation", &self.permutation.is_some())
            .finish()
    }
}

impl fmt::Display for Coords {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let active_name = &self.config_names[self.active];
        let collected = self.capacity() - self.node_count();
        let perm_label = if self.permutation.is_some() {
            "custom"
        } else {
            "identity"
        };
        // The frame is shown only when it departs from the Cartesian default, so
        // the overwhelmingly common rendering stays unchanged.
        let frame_label = match self.frame {
            CoordinateFrame::Cartesian => String::new(),
            f => format!(" ({f})"),
        };
        write!(
            f,
            "Coords: dim={}{}, configs={} (active=\"{}\"), nodes={} ({} collected), permutation: {}",
            self.dim,
            frame_label,
            self.configs.len(),
            active_name,
            self.node_count(),
            collected,
            perm_label
        )
    }
}

impl crate::dump::Dump for Coords {
    fn render(&self, opts: &crate::dump::DumpOptions) -> String {
        use crate::dump::{fmt_float, table};
        let dim = self.dim as usize;
        const AXES: [&str; 3] = ["x", "y", "z"];
        // In a meridian plane the columns are the radius and the axis, so name
        // them as such rather than x/y.
        const AXES_AXI: [&str; 2] = ["r", "z"];
        let axes: &[&str] = if self.frame.is_axisymmetric() {
            &AXES_AXI
        } else {
            &AXES
        };
        let mut headers = vec!["node".to_string()];
        headers.extend((0..dim).map(|i| axes.get(i).copied().unwrap_or("?").to_string()));
        headers.push("refs".to_string());
        let rows: Vec<Vec<String>> = self
            .iter_live()
            .map(|id| {
                let mut row = vec![id.to_string()];
                match self.position(id) {
                    Ok(c) => row.extend(c.iter().map(|v| fmt_float(*v, opts.precision))),
                    Err(_) => row.extend((0..dim).map(|_| "?".to_string())),
                }
                row.push(self.refcount(id).to_string());
                row
            })
            .collect();
        format!("{self}\n{}", table(&headers, &rows, opts))
    }
}

// ─── Unit tests ─────────────────────────────────────────────────────────────

// ─── Archive ────────────────────────────────────────────────────────────────

impl crate::archive::Archivable for Coords {
    const TAG: &'static str = "Coords";

    /// The per-node refcount is not in the file (see the field's own note).
    /// Size it to the node capacity, at zero: each reloaded `SubMesh` will
    /// re-increment what it references, and the post-order guarantees they come
    /// after this.
    fn on_load(&mut self) {
        self.refcount = vec![0; self.alive.len()];
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn create_and_dim() {
        let c = Coords::new(3).unwrap();
        assert_eq!(c.dim(), 3);
        assert_eq!(c.node_count(), 0);
        assert_eq!(c.capacity(), 0);
    }

    #[test]
    fn cartesian_by_default_axisymmetric_on_demand() {
        assert!(!Coords::new(2).unwrap().is_axisymmetric());
        let c = Coords::axisymmetric().unwrap();
        assert_eq!(c.dim(), 2);
        assert_eq!(c.frame(), CoordinateFrame::Axisymmetric);
        assert!(c.is_axisymmetric());
        // The frame surfaces in Display only when it is not the default.
        assert!(Coords::new(2).unwrap().to_string().contains("dim=2,"));
        assert!(c.to_string().contains("dim=2 (axisymmetric)"));
    }

    /// `x = r` is a radius: negative values are a modelling error, caught at the
    /// door rather than as a negative `|J|` deep in an integral.
    #[test]
    fn axisymmetric_rejects_a_negative_radius() {
        let mut c = Coords::axisymmetric().unwrap();
        let err = c.add_node(&[-1.0, 0.0]).unwrap_err();
        assert!(format!("{err}").contains("negative radius"));
        // On the axis (r = 0) is legitimate.
        let id = c.add_node(&[0.0, 2.0]).unwrap();
        assert!(c.set_position(id, &[-0.5, 2.0]).is_err());
        c.set_position(id, &[0.5, 2.0]).unwrap();
        // A Cartesian Coords keeps accepting negative x.
        assert!(Coords::new(2).unwrap().add_node(&[-1.0, 0.0]).is_ok());
    }

    #[test]
    fn dim_zero_rejected() {
        let err = Coords::new(0).unwrap_err();
        assert!(matches!(err, PyrucastError::Message(_)));
    }

    #[test]
    fn add_node_initializes_refcount_to_one() {
        let mut c = Coords::new(2).unwrap();
        let a = c.add_node(&[0.0, 0.0]).unwrap();
        assert_eq!(c.refcount(a), 1);
        assert!(c.is_alive(a));
    }

    #[test]
    fn add_node_invalid_dim() {
        let mut c = Coords::new(3).unwrap();
        let err = c.add_node(&[1.0, 2.0]).unwrap_err();
        assert!(matches!(err, PyrucastError::Message(_)));
    }

    #[test]
    fn gc_does_not_collect_referenced_nodes() {
        let mut c = Coords::new(2).unwrap();
        let a = c.add_node(&[0.0, 0.0]).unwrap();
        // refcount = 1, gc must not collect anything
        assert_eq!(c.gc(), 0);
        assert!(c.is_alive(a));
        // After decref, refcount drops to 0
        c.decref(a).unwrap();
        assert_eq!(c.refcount(a), 0);
        // but only gc actually removes
        assert_eq!(c.gc(), 1);
        assert!(!c.is_alive(a));
    }

    #[test]
    fn incref_protects_from_gc() {
        let mut c = Coords::new(1).unwrap();
        let a = c.add_node(&[3.0]).unwrap();
        c.incref(a).unwrap(); // refcount = 2
        c.decref(a).unwrap(); // refcount = 1
        assert_eq!(c.gc(), 0);
        c.decref(a).unwrap(); // refcount = 0
        assert_eq!(c.gc(), 1);
    }

    #[test]
    fn id_not_reused_after_gc() {
        let mut c = Coords::new(1).unwrap();
        let a = c.add_node(&[0.0]).unwrap();
        c.decref(a).unwrap();
        c.gc();
        let b = c.add_node(&[1.0]).unwrap();
        assert_ne!(a.0, b.0);
        assert_eq!(b.0, 1);
        assert_eq!(c.capacity(), 2);
    }

    #[test]
    fn coord_after_gc_is_error() {
        let mut c = Coords::new(1).unwrap();
        let a = c.add_node(&[42.0]).unwrap();
        c.decref(a).unwrap();
        c.gc();
        assert!(c.position(a).is_err());
        assert!(c.set_position(a, &[0.0]).is_err());
        assert!(c.incref(a).is_err());
        assert!(c.decref(a).is_err());
    }

    #[test]
    fn decref_at_zero_is_error() {
        let mut c = Coords::new(1).unwrap();
        let a = c.add_node(&[0.0]).unwrap();
        c.decref(a).unwrap();
        let err = c.decref(a).unwrap_err();
        assert!(matches!(err, PyrucastError::Message(_)));
    }

    #[test]
    fn set_position_modifies_active_config() {
        let mut c = Coords::new(2).unwrap();
        let a = c.add_node(&[0.0, 0.0]).unwrap();
        c.set_position(a, &[3.0, 4.0]).unwrap();
        assert_eq!(c.position(a).unwrap(), &[3.0, 4.0]);
    }

    #[test]
    fn multiple_configs_and_select() {
        let mut c = Coords::new(2).unwrap();
        let a = c.add_node(&[0.0, 0.0]).unwrap();
        let s2 = c.add_config("deformed");
        assert_eq!(s2, 1);
        c.select(s2).unwrap();
        c.set_position(a, &[10.0, 20.0]).unwrap();
        c.select(0).unwrap();
        assert_eq!(c.position(a).unwrap(), &[0.0, 0.0]);
        c.select(1).unwrap();
        assert_eq!(c.position(a).unwrap(), &[10.0, 20.0]);
        assert_eq!(c.names(), &["default".to_string(), "deformed".to_string()]);
    }

    #[test]
    fn select_invalid() {
        let mut c = Coords::new(2).unwrap();
        assert!(c.select(5).is_err());
    }

    #[test]
    fn iter_live_skips_collected() {
        let mut c = Coords::new(1).unwrap();
        let a = c.add_node(&[0.0]).unwrap();
        let b = c.add_node(&[1.0]).unwrap();
        let _cc = c.add_node(&[2.0]).unwrap();
        c.decref(b).unwrap();
        c.gc();
        let live: Vec<u32> = c.iter_live().map(|n| n.0).collect();
        assert_eq!(live, vec![a.0, 2]);
    }

    #[test]
    fn permutation_validation_and_invariant() {
        let mut c = Coords::new(1).unwrap();
        for k in 0..4 {
            c.add_node(&[k as f64]).unwrap();
        }
        c.set_permutation(vec![3, 2, 1, 0]).unwrap();
        assert_eq!(c.permutation(), Some(&[3u32, 2, 1, 0][..]));
        assert!(c.set_permutation(vec![0, 0, 1, 2]).is_err());
        assert!(c.set_permutation(vec![0, 1, 2, 99]).is_err());
        assert!(c.set_permutation(vec![0, 1, 2]).is_err());
        c.clear_permutation();
        assert!(c.permutation().is_none());
    }

    #[test]
    fn permutation_extended_by_add_node() {
        let mut c = Coords::new(1).unwrap();
        for k in 0..3 {
            c.add_node(&[k as f64]).unwrap();
        }
        c.set_permutation(vec![2, 1, 0]).unwrap();
        c.add_node(&[42.0]).unwrap();
        let perm = c.permutation().unwrap();
        assert_eq!(perm.len(), 4);
        let mut sorted = perm.to_vec();
        sorted.sort();
        assert_eq!(sorted, vec![0, 1, 2, 3]);
    }

    #[test]
    fn debug_display() {
        let mut c = Coords::new(2).unwrap();
        c.add_node(&[0.0, 0.0]).unwrap();
        c.add_node(&[1.0, 1.0]).unwrap();
        let d = format!("{:?}", c);
        assert!(d.contains("Coords"));
        assert!(d.contains("dim"));
        let s = format!("{}", c);
        assert!(s.contains("dim=2"));
        assert!(s.contains("nodes=2"));
        assert!(s.contains("identity"));
    }

    #[test]
    fn nodeid_display_and_debug() {
        let n = NodeId(7);
        assert_eq!(format!("{}", n), "7");
        assert_eq!(format!("{:?}", n), "NodeId(7)");
    }
}