pyrucast 0.7.1

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
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
//! Sparse direct linear solver: `A · x = b`.
//!
//! This module bridges the abstract [`Matrix`] / [`NodeField`] objects and the
//! sparse linear algebra of [`faer`]. It exposes a single free function —
//! [`solve`] — that:
//!
//! 1. converts the assembled `Matrix` to a CSC (`nalgebra_sparse`), then to a
//!    faer `SparseColMat`;
//! 2. reads a right-hand-side vector out of the `NodeField`, one entry
//!    per **row DOF** of the matrix (zones resolved first-found; missing
//!    entries default to `0.0`);
//! 3. runs a multithreaded **sparse LU** factorization with partial pivoting;
//! 4. wraps the solution back into a fresh single-zone `NodeField`
//!    indexed by the **column DOFs** of the matrix.
//!
//! The factorization is **reusable**: it is cached inside the `Matrix`
//! ([`SolveOptions::cache`]), so a Newton loop or a multi-load-case run pays
//! for it once and only redoes descent / back-substitution afterwards —
//! *factor once, solve many*. [`SolveMethod`] is the seam through which another
//! back-end (iterative, preconditioned) could be selected later without
//! touching the call sites.
//!
//! # Example
//!
//! ```
//! use pyrucast::containers::field::SubField;
//! use pyrucast::aggregate::Aggregate;
//! use pyrucast::coords::Coords;
//! use pyrucast::containers::element_field::{ElementField, SubElementField};
//! use pyrucast::atoms::ElementType;
//! use pyrucast::containers::finite_element_space::FiniteElementSpace;
//! use pyrucast::containers::mesh::{Mesh, SubMesh};
//! use pyrucast::containers::model::{Model, SubModel};
//! use pyrucast::atoms::Node;
//! use pyrucast::containers::node_field::{NodeField, SubNodeField};
//! use pyrucast::ops::matrix;
//! use pyrucast::ops::mesh;
//! use pyrucast::ops::solver::lu::solve;
//! use pyrucast::handle::Handle;
//!
//! // 1-D Poisson on [0, 1] with one SEG2 element, k = 1.
//! let coords = Handle::new(Coords::new(1).unwrap());
//! let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
//! let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
//! let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
//! mesh.add_cell(&[a.id(), b.id()]).unwrap();
//! let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
//! let sub = fes.get(0).unwrap();
//! let mut mat = SubElementField::new(sub.clone(), vec!["k".into()]).unwrap();
//! mat.set_uniform("k", 1.0).unwrap();
//! let mut materials = ElementField::empty();
//! materials.add_sub(Handle::new(mat)).unwrap();
//!
//! let mut model = Model::empty();
//! model
//!     .add_sub(Handle::new(SubModel::heat_conduction(sub).unwrap()))
//!     .unwrap();
//! // Dirichlet at both ends: imposed POI1 meshes + colocated multiplier
//! // supports minted by `barycenter`.
//! let imposed_a = Mesh::from_submesh(SubMesh::poi1_from_nodes(std::slice::from_ref(&a)).unwrap());
//! let imposed_b = Mesh::from_submesh(SubMesh::poi1_from_nodes(std::slice::from_ref(&b)).unwrap());
//! let mult_mesh_a = mesh::barycenter(&imposed_a).unwrap();
//! let mult_mesh_b = mesh::barycenter(&imposed_b).unwrap();
//! let dir_a = SubModel::dirichlet(&model, "T", &imposed_a, &mult_mesh_a, Default::default()).unwrap();
//! let dir_b = SubModel::dirichlet(&model, "T", &imposed_b, &mult_mesh_b, Default::default()).unwrap();
//! let mult_a = dir_a.multiplier_nodes()[0];
//! let mult_b = dir_b.multiplier_nodes()[0];
//! model.add_sub(Handle::new(dir_a)).unwrap();
//! model.add_sub(Handle::new(dir_b)).unwrap();
//!
//! // Load: imposed values T_a = 0, T_b = 1 at the multiplier nodes (slot "imposed_T").
//! let mut load_sm = SubMesh::new(coords.clone(), ElementType::POI1);
//! load_sm.add_cell(&[mult_a]).unwrap();
//! load_sm.add_cell(&[mult_b]).unwrap();
//! let load_sm_h = Handle::new(load_sm);
//! let mut rhs = SubNodeField::from_poi1(&load_sm_h, vec!["imposed_T".into()]).unwrap();
//! rhs.set_value(mult_a, "imposed_T", 0.0).unwrap();
//! rhs.set_value(mult_b, "imposed_T", 1.0).unwrap();
//! let rhs = NodeField::from_sub(rhs);
//!
//! let k = pyrucast::ops::matrix::stiffness(&model, &materials).unwrap();
//! let solution = solve(&k, &rhs).unwrap();
//! // Solution: T(a) = 0, T(b) = 1, λ_a = +1, λ_b = -1 (boundary fluxes).
//! assert!((solution.value(a.id(), "T").unwrap() - 0.0).abs() < 1e-12);
//! assert!((solution.value(b.id(), "T").unwrap() - 1.0).abs() < 1e-12);
//! ```

use crate::containers::matrix::{DofKey, Matrix};
use crate::containers::node_field::NodeField;
use crate::error::{PyrucastError, Result};
use crate::interrupt::{Cancel, NoCancel};
use faer::linalg::solvers::Solve;
use faer::sparse::{SparseColMat, Triplet};
use nalgebra_sparse::CscMatrix;
use std::sync::Arc;

/// A faer sparse LU factorization over `usize` indices and `f64` values — the
/// direct back-end shared by [`Factorization`] (full saddle-point system) and
/// [`crate::ops::solver::eliminate`] (reduced condensed system).
pub(crate) type SparseLu = faer::sparse::linalg::solvers::Lu<usize, f64>;

/// Factorize a square CSC matrix with sparse LU (faer). The single place the
/// nalgebra-sparse CSC → faer `SparseColMat` → `sp_lu` conversion lives, so both
/// the Lagrange and the elimination solvers share one implementation.
pub(crate) fn factorize_csc(csc: &CscMatrix<f64>) -> Result<SparseLu> {
    let n = csc.nrows();
    if n == 0 {
        return Err(PyrucastError::Message("solve: matrix is empty".into()));
    }
    if csc.ncols() != n {
        return Err(PyrucastError::Message(format!(
            "solve: matrix must be square; got {}×{}",
            n,
            csc.ncols()
        )));
    }
    // Build a faer sparse matrix from the (duplicate-summed) CSC. Each
    // (row, col) appears once, so the triplet form is exact.
    let col_offsets = csc.col_offsets();
    let row_indices = csc.row_indices();
    let values = csc.values();
    let mut triplets: Vec<Triplet<usize, usize, f64>> = Vec::with_capacity(values.len());
    for col in 0..n {
        for k in col_offsets[col]..col_offsets[col + 1] {
            triplets.push(Triplet::new(row_indices[k], col, values[k]));
        }
    }
    let a = SparseColMat::<usize, f64>::try_new_from_triplets(n, n, &triplets)
        .map_err(|e| PyrucastError::Message(format!("solve: sparse build failed: {e:?}")))?;
    a.sp_lu()
        .map_err(|e| PyrucastError::Message(format!("solve: LU failed (singular?): {e:?}")))
}

/// Solve `A·x = b` for one right-hand side against a computed [`SparseLu`]
/// (descent / back-substitution only). Shared by both direct solvers.
pub(crate) fn lu_solve_vec(lu: &SparseLu, b: &[f64]) -> Vec<f64> {
    let n = b.len();
    let mut x = faer::Mat::<f64>::zeros(n, 1);
    for (i, &v) in b.iter().enumerate() {
        x[(i, 0)] = v;
    }
    lu.solve_in_place(&mut x);
    (0..n).map(|i| x[(i, 0)]).collect()
}

/// Direct solver method. Today only sparse LU (faer); the enum leaves room to
/// force another backend (iterative, …) in the future without changing the
/// `solve` call sites.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::containers::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::RelationSense;
/// # use pyrucast::ops::{element_field, matrix, mesh, solver};
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let n: Vec<Node> = (0..3)
/// #     .map(|i| Node::create_in(coords.clone(), &[i as f64 / 2.0]).unwrap())
/// #     .collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::SEG2);
/// # for i in 0..2 { sm.add_cell(&[n[i].id(), n[i + 1].id()]).unwrap(); }
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # let cible = model::heat_conduction(&fes).unwrap();
/// # let modele = model::heat_conduction(&fes).unwrap()
/// #     .union(&model::dirichlet(&cible, "T", &impose, &mult,
/// #                              RelationSense::Equality).unwrap()).unwrap();
/// # let materiaux = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
/// # let k = matrix::stiffness(&modele, &materiaux).unwrap();
/// # let charge = NodeField::from_submesh(&mult.get(0).unwrap(),
/// #                                      vec!["imposed_T".into()]).unwrap();
/// # charge.get(0).unwrap().write().add_to_component("imposed_T", 100.0).unwrap();
/// # use pyrucast::ops::solver::lu::{SolveMethod, SolveOptions};
/// # use pyrucast::ops::model;
/// // Un seul jeton aujourd'hui — LU creuse — mais l'énumération laisse la
/// // place à un autre moteur sans toucher aux appels.
/// assert_eq!(SolveMethod::default(), SolveMethod::Lu);
/// let o = SolveOptions { method: SolveMethod::Lu, cache: false };
/// assert!(solver::lu::solve_with_options(&k, &charge, &o).is_ok());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SolveMethod {
    /// Sparse LU with partial pivoting (faer, multithreaded).
    #[default]
    Lu,
}

/// Options for [`solve_with_options`]. Defaults: sparse LU with the reusable
/// factorization cache enabled.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::containers::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::RelationSense;
/// # use pyrucast::ops::{element_field, matrix, mesh, solver};
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let n: Vec<Node> = (0..3)
/// #     .map(|i| Node::create_in(coords.clone(), &[i as f64 / 2.0]).unwrap())
/// #     .collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::SEG2);
/// # for i in 0..2 { sm.add_cell(&[n[i].id(), n[i + 1].id()]).unwrap(); }
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # let cible = model::heat_conduction(&fes).unwrap();
/// # let modele = model::heat_conduction(&fes).unwrap()
/// #     .union(&model::dirichlet(&cible, "T", &impose, &mult,
/// #                              RelationSense::Equality).unwrap()).unwrap();
/// # let materiaux = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
/// # let k = matrix::stiffness(&modele, &materiaux).unwrap();
/// # let charge = NodeField::from_submesh(&mult.get(0).unwrap(),
/// #                                      vec!["imposed_T".into()]).unwrap();
/// # charge.get(0).unwrap().write().add_to_component("imposed_T", 100.0).unwrap();
/// # use pyrucast::ops::solver::lu::{SolveMethod, SolveOptions};
/// # use pyrucast::ops::model;
/// // Par défaut, LU creuse avec le cache de factorisation actif : le
/// // premier `solve` factorise, les suivants ne font que les descentes.
/// let d = SolveOptions::default();
/// assert!(d.cache);
/// assert!(k.cached_factorization::<solver::lu::Factorization>().is_none());
/// solver::lu::solve_with_options(&k, &charge, &d)?;
/// assert!(k.cached_factorization::<solver::lu::Factorization>().is_some());
///
/// // `cache: false` factorise à neuf et **ne touche pas** au cache.
/// let sans = SolveOptions { method: SolveMethod::Lu, cache: false };
/// solver::lu::solve_with_options(&k, &charge, &sans)?;
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Copy, Debug)]
pub struct SolveOptions {
    /// Direct method to use.
    pub method: SolveMethod,
    /// Reuse / populate the matrix's cached factorization. When `true` (default)
    /// the first solve factorizes and caches; later solves on the **same**
    /// matrix reuse the factors (descent/back-substitution only). When `false`,
    /// factorize fresh and do not touch the cache.
    pub cache: bool,
}

impl Default for SolveOptions {
    fn default() -> Self {
        Self {
            method: SolveMethod::Lu,
            cache: true,
        }
    }
}

/// A reusable sparse LU factorization of a [`Matrix`], plus the DOF layout
/// needed to map a right-hand side in and a solution out. Cached transparently
/// inside the `Matrix` (see [`SolveOptions::cache`]); derived, non-serialized
/// state — never persisted.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::containers::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::RelationSense;
/// # use pyrucast::ops::{element_field, matrix, mesh, solver};
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let n: Vec<Node> = (0..3)
/// #     .map(|i| Node::create_in(coords.clone(), &[i as f64 / 2.0]).unwrap())
/// #     .collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::SEG2);
/// # for i in 0..2 { sm.add_cell(&[n[i].id(), n[i + 1].id()]).unwrap(); }
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # let cible = model::heat_conduction(&fes).unwrap();
/// # let modele = model::heat_conduction(&fes).unwrap()
/// #     .union(&model::dirichlet(&cible, "T", &impose, &mult,
/// #                              RelationSense::Equality).unwrap()).unwrap();
/// # let materiaux = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
/// # let k = matrix::stiffness(&modele, &materiaux).unwrap();
/// # let charge = NodeField::from_submesh(&mult.get(0).unwrap(),
/// #                                      vec!["imposed_T".into()]).unwrap();
/// # charge.get(0).unwrap().write().add_to_component("imposed_T", 100.0).unwrap();
/// # use pyrucast::ops::solver::lu::Factorization;
/// # use std::sync::Arc;
/// # use pyrucast::ops::model;
/// // Un état **dérivé**, jamais persisté : il porte la LU et la
/// // disposition des DDL qui la rend utilisable. On ne l'appelle pas
/// // directement — `solve` la dépose dans le cache de la matrice et l'y
/// // reprend au solve suivant.
/// k.store_factorization(Arc::new(Factorization::new(&k)?));
/// assert!(k.cached_factorization::<Factorization>().is_some());
/// let u = solver::lu::solve(&k, &charge)?;
/// assert!(u.node_count()? > 0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub struct Factorization {
    lu: SparseLu,
    /// The row numbering the factorization is valid for, in packed form: a
    /// materialised `(NodeId, String)` list would hold one heap allocation per
    /// degree of freedom for the whole life of the cached factorization.
    vars: Arc<Vec<String>>,
    row_keys: Vec<DofKey>,
}

impl Factorization {
    /// Factorize `matrix` (must be square and finalized) with sparse LU.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::field::SubField;
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::containers::model::Model;
    /// # use pyrucast::containers::node_field::NodeField;
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::RelationSense;
    /// # use pyrucast::ops::{element_field, matrix, mesh, solver};
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(1).unwrap());
    /// # let n: Vec<Node> = (0..3)
    /// #     .map(|i| Node::create_in(coords.clone(), &[i as f64 / 2.0]).unwrap())
    /// #     .collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::SEG2);
    /// # for i in 0..2 { sm.add_cell(&[n[i].id(), n[i + 1].id()]).unwrap(); }
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
    /// # let mult = mesh::barycenter(&impose).unwrap();
    /// # let cible = model::heat_conduction(&fes).unwrap();
    /// # let modele = model::heat_conduction(&fes).unwrap()
    /// #     .union(&model::dirichlet(&cible, "T", &impose, &mult,
    /// #                              RelationSense::Equality).unwrap()).unwrap();
    /// # let materiaux = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
    /// # let k = matrix::stiffness(&modele, &materiaux).unwrap();
    /// # let charge = NodeField::from_submesh(&mult.get(0).unwrap(),
    /// #                                      vec!["imposed_T".into()]).unwrap();
    /// # charge.get(0).unwrap().write().add_to_component("imposed_T", 100.0).unwrap();
    /// // La matrice doit être **carrée et finalisée**.
    /// assert!(solver::lu::Factorization::new(&k).is_ok());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn new(matrix: &Matrix) -> Result<Self> {
        let vars = matrix.dof_vars()?;
        let row_keys = matrix.row_dof_keys()?;
        let n_cols = matrix.col_dof_keys()?.len();
        if row_keys.len() != n_cols {
            return Err(PyrucastError::Message(format!(
                "solve: matrix must be square; got {}×{}",
                row_keys.len(),
                n_cols
            )));
        }
        let lu = factorize_csc(&matrix.to_csc()?)?;
        Ok(Self { lu, vars, row_keys })
    }

    /// Solve `A·x = b` for one right-hand side (descent/back-substitution only).
    fn solve_vec(&self, b: &[f64]) -> Vec<f64> {
        lu_solve_vec(&self.lu, b)
    }
}

/// Solve `matrix · x = rhs` using the default options (sparse LU, cached).
///
/// `matrix` must be square (`n_rows == n_cols ≥ 1`). The `rhs` `NodeField` is
/// read at every row DOF of the matrix, through the aggregate (first zone
/// defining the pair wins); missing entries (no zone defines that
/// `(node, component)`) default to `0.0`.
///
/// The returned `NodeField` has one zone per distinct **column support** of the
/// matrix's blocks, each zone sharing that block's own POI1 support handle and
/// carrying its primal variables (see [`Matrix::field_from_col_values`]) — no
/// support submesh is rebuilt, and the output aligns by
/// [`same_support`](crate::containers::field::SubField::same_support) with
/// any other field on those supports. On a Lagrange-constrained model this
/// includes a zone for the multipliers (the reactions).
///
/// Uninterruptible convenience form; see [`solve_cancellable`].
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::containers::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::RelationSense;
/// # use pyrucast::ops::{element_field, matrix, mesh, solver};
/// # use pyrucast::ops::model;
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let n: Vec<Node> = (0..3)
/// #     .map(|i| Node::create_in(coords.clone(), &[i as f64 / 2.0]).unwrap())
/// #     .collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::SEG2);
/// # for i in 0..2 { sm.add_cell(&[n[i].id(), n[i + 1].id()]).unwrap(); }
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # let cible = model::heat_conduction(&fes).unwrap();
/// # let modele = model::heat_conduction(&fes).unwrap()
/// #     .union(&model::dirichlet(&cible, "T", &impose, &mult,
/// #                              RelationSense::Equality).unwrap()).unwrap();
/// # let materiaux = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
/// # let k = matrix::stiffness(&modele, &materiaux).unwrap();
/// # let charge = NodeField::from_submesh(&mult.get(0).unwrap(),
/// #                                      vec!["imposed_T".into()]).unwrap();
/// # charge.get(0).unwrap().write().add_to_component("imposed_T", 100.0).unwrap();
/// // Une barre encastrée à gauche à 100 °C, sans autre charge : toute la
/// // barre est à 100 °C. La solution porte aussi la **réaction** au nœud
/// // multiplicateur — d'où deux zones.
/// let u = solver::lu::solve(&k, &charge)?;
/// assert!((u.get(0)?.read().value(n[2].id(), "T")? - 100.0).abs() < 1e-9);
/// // Une zone par support du modèle : la barre, les nœuds imposés, et le
/// // multiplicateur qui porte la **réaction**.
/// assert_eq!(u.len(), 3);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn solve(matrix: &Matrix, rhs: &NodeField) -> Result<NodeField> {
    solve_inner(matrix, rhs, &SolveOptions::default(), &NoCancel)
}

/// Like [`solve`] but with explicit [`SolveOptions`] (method / factorization
/// cache).
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::containers::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::RelationSense;
/// # use pyrucast::ops::{element_field, matrix, mesh, solver};
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let n: Vec<Node> = (0..3)
/// #     .map(|i| Node::create_in(coords.clone(), &[i as f64 / 2.0]).unwrap())
/// #     .collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::SEG2);
/// # for i in 0..2 { sm.add_cell(&[n[i].id(), n[i + 1].id()]).unwrap(); }
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # let cible = model::heat_conduction(&fes).unwrap();
/// # let modele = model::heat_conduction(&fes).unwrap()
/// #     .union(&model::dirichlet(&cible, "T", &impose, &mult,
/// #                              RelationSense::Equality).unwrap()).unwrap();
/// # let materiaux = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
/// # let k = matrix::stiffness(&modele, &materiaux).unwrap();
/// # let charge = NodeField::from_submesh(&mult.get(0).unwrap(),
/// #                                      vec!["imposed_T".into()]).unwrap();
/// # charge.get(0).unwrap().write().add_to_component("imposed_T", 100.0).unwrap();
/// # use pyrucast::ops::solver::lu::{SolveMethod, SolveOptions};
/// # use pyrucast::ops::model;
/// // La forme explicite : méthode et cache de factorisation.
/// let o = SolveOptions { method: SolveMethod::Lu, cache: true };
/// let u = solver::lu::solve_with_options(&k, &charge, &o)?;
/// assert!((u.get(0)?.read().value(n[0].id(), "T")? - 100.0).abs() < 1e-9);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn solve_with_options(
    matrix: &Matrix,
    rhs: &NodeField,
    options: &SolveOptions,
) -> Result<NodeField> {
    solve_inner(matrix, rhs, options, &NoCancel)
}

/// Like [`solve`], but polls `cancel` at each phase boundary so the call can be
/// stopped early (returning [`PyrucastError::Interrupted`]).
///
/// **Granularity.** The sparse factorization (faer's `sp_lu` / `solve_in_place`)
/// is a single library call with no cooperative checkpoint, so it is **not**
/// interrupted mid-way: `cancel` is polled *around* the heavy steps (vector
/// assembly, before factorization, result write-back). A `Ctrl+C` therefore
/// lands at the next phase boundary, not inside the factorization itself. When
/// the factorization is already cached, only the (cheap) substitution runs.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::containers::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::RelationSense;
/// # use pyrucast::ops::{element_field, matrix, mesh, solver};
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let n: Vec<Node> = (0..3)
/// #     .map(|i| Node::create_in(coords.clone(), &[i as f64 / 2.0]).unwrap())
/// #     .collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::SEG2);
/// # for i in 0..2 { sm.add_cell(&[n[i].id(), n[i + 1].id()]).unwrap(); }
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # let cible = model::heat_conduction(&fes).unwrap();
/// # let modele = model::heat_conduction(&fes).unwrap()
/// #     .union(&model::dirichlet(&cible, "T", &impose, &mult,
/// #                              RelationSense::Equality).unwrap()).unwrap();
/// # let materiaux = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
/// # let k = matrix::stiffness(&modele, &materiaux).unwrap();
/// # let charge = NodeField::from_submesh(&mult.get(0).unwrap(),
/// #                                      vec!["imposed_T".into()]).unwrap();
/// # charge.get(0).unwrap().write().add_to_component("imposed_T", 100.0).unwrap();
/// # use std::sync::atomic::{AtomicBool, Ordering};
/// # use pyrucast::ops::model;
/// // Le jeton est sondé **autour** des étapes lourdes : la factorisation
/// // creuse est un appel de bibliothèque sans point d'arrêt coopératif.
/// let stop = AtomicBool::new(false);
/// assert!(solver::lu::solve_cancellable(&k, &charge, &stop).is_ok());
/// // Jeton armé d'avance : l'arrêt tombe à la première frontière de phase.
/// stop.store(true, Ordering::Relaxed);
/// assert!(solver::lu::solve_cancellable(&k, &charge, &stop).is_err());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn solve_cancellable(
    matrix: &Matrix,
    rhs: &NodeField,
    cancel: &dyn Cancel,
) -> Result<NodeField> {
    solve_inner(matrix, rhs, &SolveOptions::default(), cancel)
}

/// [`solve_cancellable`] with explicit [`SolveOptions`] — the full form the
/// Python binding routes to.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::containers::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::RelationSense;
/// # use pyrucast::ops::{element_field, matrix, mesh, solver};
/// # let coords = Handle::new(Coords::new(1).unwrap());
/// # let n: Vec<Node> = (0..3)
/// #     .map(|i| Node::create_in(coords.clone(), &[i as f64 / 2.0]).unwrap())
/// #     .collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::SEG2);
/// # for i in 0..2 { sm.add_cell(&[n[i].id(), n[i + 1].id()]).unwrap(); }
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # let cible = model::heat_conduction(&fes).unwrap();
/// # let modele = model::heat_conduction(&fes).unwrap()
/// #     .union(&model::dirichlet(&cible, "T", &impose, &mult,
/// #                              RelationSense::Equality).unwrap()).unwrap();
/// # let materiaux = element_field::material_field(&modele, &[("k", 1.0)]).unwrap();
/// # let k = matrix::stiffness(&modele, &materiaux).unwrap();
/// # let charge = NodeField::from_submesh(&mult.get(0).unwrap(),
/// #                                      vec!["imposed_T".into()]).unwrap();
/// # charge.get(0).unwrap().write().add_to_component("imposed_T", 100.0).unwrap();
/// # use pyrucast::ops::solver::lu::SolveOptions;
/// # use std::sync::atomic::AtomicBool;
/// # use pyrucast::ops::model;
/// // La forme complète, celle vers laquelle route la liaison Python.
/// let stop = AtomicBool::new(false);
/// let u = solver::lu::solve_cancellable_with_options(
///     &k, &charge, &SolveOptions::default(), &stop)?;
/// assert!(u.node_count()? > 0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn solve_cancellable_with_options(
    matrix: &Matrix,
    rhs: &NodeField,
    options: &SolveOptions,
    cancel: &dyn Cancel,
) -> Result<NodeField> {
    solve_inner(matrix, rhs, options, cancel)
}

fn solve_inner(
    matrix: &Matrix,
    rhs: &NodeField,
    options: &SolveOptions,
    cancel: &dyn Cancel,
) -> Result<NodeField> {
    let SolveMethod::Lu = options.method;
    cancel.check()?;

    // ── Step 1 — obtain the factorization (cached or fresh) ────────────
    let fact: Arc<Factorization> = if options.cache {
        match matrix.cached_factorization::<Factorization>() {
            Some(f) => f,
            None => {
                let f = Arc::new(Factorization::new(matrix)?);
                matrix.store_factorization(f.clone());
                f
            }
        }
    } else {
        Arc::new(Factorization::new(matrix)?)
    };
    cancel.check()?;

    // ── Step 2 — build the b vector at the row DOFs ────────────────────
    // "No zone defines this DOF" means "no imposed value here" — zero.
    let b = rhs.gather_keys(&fact.row_keys, &fact.vars)?;

    // ── Step 3 — substitution ──────────────────────────────────────────
    let x = fact.solve_vec(&b);
    // A singular matrix factorizes with a zero pivot; the back-substitution
    // then divides by it, yielding non-finite entries. Flag it like the old
    // dense solver did, instead of returning a garbage field.
    if x.iter().any(|v| !v.is_finite()) {
        return Err(PyrucastError::Message(
            "solve: LU failed (matrix is singular)".into(),
        ));
    }
    cancel.check()?;

    // ── Step 4 — wrap the solution into a NodeField on the blocks' supports ──
    // One zone per distinct column support, sharing the block's own POI1 handle
    // (no submesh rebuilt); `x` is in `fact.col_dofs` order, which is the
    // assembled column order (`Factorization::new` reads `matrix.col_dofs()`).
    matrix.field_from_col_values(&x)
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::aggregate::Aggregate;
    use crate::atoms::ElementType;
    use crate::atoms::Node;
    use crate::containers::element_field::SubElementField;
    use crate::containers::field::SubField;
    use crate::containers::finite_element_space::FiniteElementSpace;
    use crate::containers::mesh::Mesh;
    use crate::containers::mesh::SubMesh;
    use crate::containers::model::{Model, SubModel};
    use crate::containers::node_field::SubNodeField;
    use crate::coords::Coords;
    use crate::handle::Handle;

    /// 1-D Poisson `-u'' = 0` on `[0, 1]` with `u(0) = 0` and `u(1) = 1`,
    /// discretized with `n` SEG2 elements. The analytical solution is
    /// `u(x) = x`. Lagrange multipliers at the boundary represent the
    /// boundary heat flux: `+1` at x=0, `-1` at x=1 (outward normal
    /// conventions).
    #[test]
    fn poisson_1d_dirichlet_at_both_ends_recovers_linear_solution() {
        let n_elems = 4;
        let h = 1.0 / n_elems as f64;
        let coords = Handle::new(Coords::new(1).unwrap());
        let nodes: Vec<Node> = (0..=n_elems)
            .map(|i| Node::create_in(coords.clone(), &[i as f64 * h]).unwrap())
            .collect();

        // Mesh.
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
        for i in 0..n_elems {
            mesh.add_cell(&[nodes[i].id(), nodes[i + 1].id()]).unwrap();
        }
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let sub = fes.get(0).unwrap();

        // k = 1 uniform.
        let mut mat = SubElementField::new(sub.clone(), vec!["k".into()]).unwrap();
        mat.set_uniform("k", 1.0).unwrap();
        let mut materials = crate::containers::element_field::ElementField::empty();
        materials.add_sub(Handle::new(mat)).unwrap();

        // Model.
        let mut model = Model::empty();
        model
            .add_sub(Handle::new(SubModel::heat_conduction(sub).unwrap()))
            .unwrap();
        let imposed_left =
            Mesh::from_submesh(SubMesh::poi1_from_nodes(std::slice::from_ref(&nodes[0])).unwrap());
        let imposed_right = Mesh::from_submesh(
            SubMesh::poi1_from_nodes(std::slice::from_ref(&nodes[n_elems])).unwrap(),
        );
        let mult_mesh_left = crate::ops::mesh::barycenter(&imposed_left).unwrap();
        let mult_mesh_right = crate::ops::mesh::barycenter(&imposed_right).unwrap();
        let left_dir = SubModel::dirichlet(
            &model,
            "T",
            &imposed_left,
            &mult_mesh_left,
            Default::default(),
        )
        .unwrap();
        let right_dir = SubModel::dirichlet(
            &model,
            "T",
            &imposed_right,
            &mult_mesh_right,
            Default::default(),
        )
        .unwrap();
        let mult_left = left_dir.multiplier_nodes()[0];
        let mult_right = right_dir.multiplier_nodes()[0];
        model.add_sub(Handle::new(left_dir)).unwrap();
        model.add_sub(Handle::new(right_dir)).unwrap();

        // Build rhs: T_left = 0 at mult_left, T_right = 1 at mult_right
        // (imposed value goes to the "imposed_T" slot).
        let mut rhs_sm = SubMesh::new(coords.clone(), ElementType::POI1);
        rhs_sm.add_cell(&[mult_left]).unwrap();
        rhs_sm.add_cell(&[mult_right]).unwrap();
        let rhs_sm_h = Handle::new(rhs_sm);
        let mut rhs = SubNodeField::from_poi1(&rhs_sm_h, vec!["imposed_T".into()]).unwrap();
        rhs.set_value(mult_left, "imposed_T", 0.0).unwrap();
        rhs.set_value(mult_right, "imposed_T", 1.0).unwrap();
        let rhs = NodeField::from_sub(rhs);

        // Assemble + solve.
        let k = crate::ops::matrix::stiffness(&model, &materials).unwrap();
        let solution = solve(&k, &rhs).unwrap();

        // Verify T at every node equals its physical coordinate.
        let tol = 1e-10;
        for (i, node) in nodes.iter().enumerate() {
            let expected = i as f64 * h;
            let got = solution.value(node.id(), "T").unwrap();
            assert!(
                (got - expected).abs() < tol,
                "T at node {i}: got {got}, expected {expected}"
            );
        }
        // Verify the boundary fluxes (Lagrange multipliers).
        let lambda_left = solution.value(mult_left, "lambda_T").unwrap();
        let lambda_right = solution.value(mult_right, "lambda_T").unwrap();
        assert!(
            (lambda_left - 1.0).abs() < tol,
            "lambda at left: got {lambda_left}, expected 1.0"
        );
        assert!(
            (lambda_right + 1.0).abs() < tol,
            "lambda at right: got {lambda_right}, expected -1.0"
        );
    }

    /// Singular matrix (Neumann everywhere) should produce a clean error.
    #[test]
    fn singular_matrix_yields_error() {
        // 2-node SEG2 with no Dirichlet → K is the discrete Laplacian
        // [[1, -1], [-1, 1]], singular (kernel = constants).
        let coords = Handle::new(Coords::new(1).unwrap());
        let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
        let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
        mesh.add_cell(&[a.id(), b.id()]).unwrap();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let sub = fes.get(0).unwrap();
        let mut mat = SubElementField::new(sub.clone(), vec!["k".into()]).unwrap();
        mat.set_uniform("k", 1.0).unwrap();
        let mut materials = crate::containers::element_field::ElementField::empty();
        materials.add_sub(Handle::new(mat)).unwrap();
        let mut model = Model::empty();
        model
            .add_sub(Handle::new(SubModel::heat_conduction(sub).unwrap()))
            .unwrap();
        let k = crate::ops::matrix::stiffness(&model, &materials).unwrap();

        // Build a tiny non-empty rhs on a real node so we can find the
        // Coords of the result SubNodeField.
        let mut rhs_sm = SubMesh::new(coords.clone(), ElementType::POI1);
        rhs_sm.add_cell(&[a.id()]).unwrap();
        let rhs_sm_h = Handle::new(rhs_sm);
        let rhs =
            NodeField::from_sub(SubNodeField::from_poi1(&rhs_sm_h, vec!["q".into()]).unwrap());
        // K is singular ⇒ solve must err.
        assert!(solve(&k, &rhs).is_err());
    }

    /// The solution's zones live on the matrix blocks' **own** column supports
    /// (`same_object`), so consecutive solves — and any block-shaped field —
    /// align by support instead of falling into merge passthrough. Two solves
    /// on the same matrix share the very same support handles.
    #[test]
    fn solution_zones_share_the_blocks_column_supports() {
        // 1-D Poisson with one Dirichlet end (multi-block: K + C/Cᵀ).
        let coords = Handle::new(Coords::new(1).unwrap());
        let nodes: Vec<Node> = (0..3)
            .map(|i| Node::create_in(coords.clone(), &[i as f64]).unwrap())
            .collect();
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
        for i in 0..2 {
            mesh.add_cell(&[nodes[i].id(), nodes[i + 1].id()]).unwrap();
        }
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
        let sub = fes.get(0).unwrap();
        let mut mat = SubElementField::new(sub.clone(), vec!["k".into()]).unwrap();
        mat.set_uniform("k", 1.0).unwrap();
        let mut materials = crate::containers::element_field::ElementField::empty();
        materials.add_sub(Handle::new(mat)).unwrap();
        let mut model = Model::empty();
        model
            .add_sub(Handle::new(SubModel::heat_conduction(sub).unwrap()))
            .unwrap();
        // Ground both ends so K-with-constraints is nonsingular.
        for end in [0usize, 2] {
            let imposed = Mesh::from_submesh(
                SubMesh::poi1_from_nodes(std::slice::from_ref(&nodes[end])).unwrap(),
            );
            let mult = crate::ops::mesh::barycenter(&imposed).unwrap();
            model
                .add_sub(Handle::new(
                    SubModel::dirichlet(&model, "T", &imposed, &mult, Default::default()).unwrap(),
                ))
                .unwrap();
        }
        let k = crate::ops::matrix::stiffness(&model, &materials).unwrap();
        let rhs_sm = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[nodes[0].id()]).unwrap();
            Handle::new(sm)
        };
        let rhs = NodeField::from_sub(SubNodeField::from_poi1(&rhs_sm, vec!["q".into()]).unwrap());

        let sol_a = solve(&k, &rhs).unwrap();
        let sol_b = solve(&k, &rhs).unwrap();
        sol_a.check().unwrap();

        // Each zone's support is one of the blocks' column supports.
        let block_col_supports: Vec<_> = k.iter().map(|h| h.read().col_support().clone()).collect();
        assert!(sol_a.len() > 1, "multi-block model ⇒ multi-zone solution");
        for zh in &sol_a {
            let zone_support = zh.read().support();
            assert!(
                block_col_supports
                    .iter()
                    .any(|bs| bs.same_object(&zone_support)),
                "zone support must be one of the blocks' col supports"
            );
        }
        // Consecutive solves share the same support handles (same_object),
        // so their arithmetic aligns by support.
        for (za, zb) in sol_a.iter().zip(sol_b.iter()) {
            let sa = za.read().support();
            let sb = zb.read().support();
            assert!(sa.same_object(&sb));
        }
    }

    #[test]
    fn rectangular_matrix_yields_error() {
        use crate::containers::matrix::{DofOrdering, SubMatrix};
        // 2-row support, 1-col support → 2×1 rectangular block.
        let coords = Handle::new(Coords::new(1).unwrap());
        let r0 = Node::create_in(coords.clone(), &[0.0]).unwrap();
        let r1 = Node::create_in(coords.clone(), &[1.0]).unwrap();
        let c0 = Node::create_in(coords.clone(), &[2.0]).unwrap();
        let mut row_sm = SubMesh::new(coords.clone(), ElementType::POI1);
        row_sm.add_cell(&[r0.id()]).unwrap();
        row_sm.add_cell(&[r1.id()]).unwrap();
        let mut col_sm = SubMesh::new(coords.clone(), ElementType::POI1);
        col_sm.add_cell(&[c0.id()]).unwrap();
        let mut block = SubMatrix::new(
            Handle::new(row_sm),
            Handle::new(col_sm),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            false,
        )
        .unwrap();
        block.add_entry(r0.id(), "q", c0.id(), "T", 1.0).unwrap();
        block.add_entry(r1.id(), "q", c0.id(), "T", 1.0).unwrap();
        // 2 rows × 1 col — rectangular.
        let mut m = crate::containers::matrix::Matrix::empty();
        m.add_sub(Handle::new(block)).unwrap();
        m.finalize().unwrap();

        // Build a minimal rhs on the same coords.
        let mut rhs_sm = SubMesh::new(coords.clone(), ElementType::POI1);
        rhs_sm.add_cell(&[r0.id()]).unwrap();
        let rhs = NodeField::from_sub(
            SubNodeField::from_poi1(&Handle::new(rhs_sm), vec!["q".into()]).unwrap(),
        );
        assert!(solve(&m, &rhs).is_err());
    }

    /// A solvable 1×1 system `2·T = b` and a rhs carrying `b` at the row DOF.
    fn tiny_system() -> (
        crate::containers::matrix::Matrix,
        NodeField,
        crate::atoms::NodeId,
    ) {
        use crate::containers::matrix::{DofOrdering, SubMatrix};
        let coords = Handle::new(Coords::new(1).unwrap());
        let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
        let sm = {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[a.id()]).unwrap();
            Handle::new(sm)
        };
        let mut block = SubMatrix::new(
            sm.clone(),
            sm.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            false,
        )
        .unwrap();
        block.add_entry(a.id(), "q", a.id(), "T", 2.0).unwrap();
        let mut m = crate::containers::matrix::Matrix::empty();
        m.add_sub(Handle::new(block)).unwrap();
        m.finalize().unwrap();

        let mut rhs = SubNodeField::from_poi1(&sm, vec!["q".into()]).unwrap();
        rhs.set_value(a.id(), "q", 6.0).unwrap();
        (m, NodeField::from_sub(rhs), a.id())
    }

    #[test]
    fn solve_cancellable_stops_on_preset_flag() {
        use std::sync::atomic::AtomicBool;
        let (m, rhs, _a) = tiny_system();
        let flag = AtomicBool::new(true);
        let err = solve_cancellable(&m, &rhs, &flag).unwrap_err();
        assert!(matches!(err, PyrucastError::Interrupted));
    }

    #[test]
    fn solve_cancellable_completes_when_not_cancelled() {
        use std::sync::atomic::AtomicBool;
        let (m, rhs, a) = tiny_system();
        let flag = AtomicBool::new(false);
        let sol = solve_cancellable(&m, &rhs, &flag).unwrap();
        // 2·T = 6 ⇒ T = 3.
        assert!((sol.value(a, "T").unwrap() - 3.0).abs() < 1e-12);
    }

    #[test]
    fn empty_matrix_yields_error() {
        let m = crate::containers::matrix::Matrix::empty();
        let coords = Handle::new(Coords::new(1).unwrap());
        let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
        let mut sm = SubMesh::new(coords, ElementType::POI1);
        sm.add_cell(&[a.id()]).unwrap();
        let rhs = NodeField::from_sub(
            SubNodeField::from_poi1(&Handle::new(sm), vec!["q".into()]).unwrap(),
        );
        assert!(solve(&m, &rhs).is_err());
    }

    #[test]
    fn factorization_cache_reused_then_invalidated_on_change() {
        use crate::containers::matrix::{DofOrdering, SubMatrix};
        let (mut m, rhs, a) = tiny_system();

        // Nothing cached before the first solve.
        assert!(m.cached_factorization::<Factorization>().is_none());

        // First solve factorizes + caches; 2·T = 6 ⇒ T = 3.
        let s1 = solve(&m, &rhs).unwrap();
        assert!((s1.value(a, "T").unwrap() - 3.0).abs() < 1e-12);
        assert!(m.cached_factorization::<Factorization>().is_some());

        // Second solve reuses the cached factorization: identical result.
        let s2 = solve(&m, &rhs).unwrap();
        assert_eq!(s1.value(a, "T").unwrap(), s2.value(a, "T").unwrap());

        // Mutating the matrix invalidates the cache.
        let coords = Handle::new(Coords::new(1).unwrap());
        let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
        let sm = {
            let mut sm = SubMesh::new(coords, ElementType::POI1);
            sm.add_cell(&[b.id()]).unwrap();
            Handle::new(sm)
        };
        let mut block = SubMatrix::new(
            sm.clone(),
            sm.clone(),
            vec!["q".into()],
            vec!["T".into()],
            DofOrdering::NodesThenVars,
            false,
        )
        .unwrap();
        block.add_entry(b.id(), "q", b.id(), "T", 4.0).unwrap();
        m.add_sub(Handle::new(block)).unwrap();
        assert!(m.cached_factorization::<Factorization>().is_none());
    }

    #[test]
    fn solve_with_cache_disabled_does_not_populate() {
        let (m, rhs, a) = tiny_system();
        let opts = SolveOptions {
            method: SolveMethod::Lu,
            cache: false,
        };
        let s = solve_with_options(&m, &rhs, &opts).unwrap();
        assert!((s.value(a, "T").unwrap() - 3.0).abs() < 1e-12);
        assert!(m.cached_factorization::<Factorization>().is_none());
    }
}