oxiphysics-fem 0.1.2

Finite element method for the OxiPhysics engine
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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use std::collections::HashMap;

/// Incomplete Cholesky (ICC) preconditioner for symmetric positive definite
/// sparse matrices.
///
/// Computes a lower-triangular factor `L` such that `L L^T ≈ A`, maintaining
/// the sparsity pattern of the lower triangle of `A`.  This is the sparse
/// counterpart of the dense incomplete Cholesky; suitable for use as a
/// preconditioner in PCG.
#[derive(Debug, Clone)]
pub struct IccPreconditioner {
    /// Lower-triangular Cholesky factor stored in CSR format.
    pub(super) l_values: Vec<f64>,
    pub(super) row_ptr: Vec<usize>,
    pub(super) col_indices: Vec<usize>,
    pub(super) n: usize,
}
impl IccPreconditioner {
    /// Compute the ICC factorization of a symmetric CSR matrix `a`.
    ///
    /// Only the lower-triangular pattern (including diagonal) of `a` is used.
    /// Fill-in is dropped (ICC(0) strategy).
    pub fn new(a: &CsrMatrix) -> Self {
        assert_eq!(a.nrows, a.ncols, "ICC requires a square matrix");
        let n = a.nrows;
        let mut triplets: Vec<(usize, usize, f64)> = Vec::new();
        for row in 0..n {
            let start = a.row_ptr[row];
            let end = a.row_ptr[row + 1];
            for idx in start..end {
                let col = a.col_indices[idx];
                if col <= row {
                    triplets.push((row, col, a.values[idx]));
                }
            }
        }
        let lt = CsrMatrix::from_triplets(n, n, &triplets);
        let mut l_values = lt.values.clone();
        let row_ptr = lt.row_ptr.clone();
        let col_indices = lt.col_indices.clone();
        for j in 0..n {
            let j_start = row_ptr[j];
            let j_end = row_ptr[j + 1];
            let diag_pos = col_indices[j_start..j_end]
                .iter()
                .position(|&c| c == j)
                .map(|off| j_start + off);
            if let Some(dp) = diag_pos {
                let sum_sq: f64 = l_values[j_start..dp].iter().map(|&v| v * v).sum();
                let diag_val = l_values[dp] - sum_sq;
                if diag_val <= 0.0 {
                    l_values[dp] = 1e-30_f64.sqrt();
                } else {
                    l_values[dp] = diag_val.sqrt();
                }
                let l_jj = l_values[dp];
                for row_i in (j + 1)..n {
                    let i_start = row_ptr[row_i];
                    let i_end = row_ptr[row_i + 1];
                    let pos_ij = col_indices[i_start..i_end]
                        .iter()
                        .position(|&c| c == j)
                        .map(|off| i_start + off);
                    if let Some(pij) = pos_ij {
                        let mut dot = 0.0f64;
                        let mut pi = i_start;
                        let mut pj = j_start;
                        while pi < pij && pj < dp {
                            let ci = col_indices[pi];
                            let cj = col_indices[pj];
                            if ci == cj {
                                dot += l_values[pi] * l_values[pj];
                                pi += 1;
                                pj += 1;
                            } else if ci < cj {
                                pi += 1;
                            } else {
                                pj += 1;
                            }
                        }
                        if l_jj.abs() > 1e-60 {
                            l_values[pij] = (l_values[pij] - dot) / l_jj;
                        }
                    }
                }
            }
        }
        IccPreconditioner {
            l_values,
            row_ptr,
            col_indices,
            n,
        }
    }
    /// Apply the ICC preconditioner: solve `(L L^T) z = r`.
    ///
    /// Performs two triangular solves: `L y = r` (forward), then `L^T z = y`
    /// (backward).
    pub fn solve(&self, rhs: &[f64]) -> Vec<f64> {
        let n = self.n;
        assert_eq!(rhs.len(), n);
        let mut y = rhs.to_vec();
        for i in 0..n {
            let start = self.row_ptr[i];
            let end = self.row_ptr[i + 1];
            let mut diag = 1.0;
            for p in start..end {
                let j = self.col_indices[p];
                if j == i {
                    diag = self.l_values[p];
                } else if j < i {
                    y[i] -= self.l_values[p] * y[j];
                }
            }
            if diag.abs() > 1e-60 {
                y[i] /= diag;
            }
        }
        let mut z = y.clone();
        for i in (0..n).rev() {
            let start = self.row_ptr[i];
            let end = self.row_ptr[i + 1];
            let mut diag = 1.0;
            for p in start..end {
                let j = self.col_indices[p];
                if j == i {
                    diag = self.l_values[p];
                }
            }
            if diag.abs() > 1e-60 {
                z[i] /= diag;
            }
            for p in start..end {
                let j = self.col_indices[p];
                if j < i {
                    z[j] -= self.l_values[p] * z[i];
                }
            }
        }
        z
    }
    /// Return the number of non-zero entries in the factor L.
    pub fn nnz(&self) -> usize {
        self.l_values.len()
    }
}
/// A node in a 2-D quadtree used for adaptive mesh refinement.
///
/// Each node represents a rectangular cell in the mesh.  Leaf nodes
/// correspond to actual mesh cells; internal nodes have been refined.
#[derive(Debug, Clone)]
pub struct QuadTreeNode {
    /// Lower-left x coordinate.
    pub x0: f64,
    /// Lower-left y coordinate.
    pub y0: f64,
    /// Cell width.
    pub width: f64,
    /// Cell height.
    pub height: f64,
    /// Refinement level (0 = root).
    pub level: u32,
    /// Per-element error indicator (set by the caller before refinement).
    pub error_indicator: f64,
    /// Children (SW, SE, NW, NE) if this node has been refined.
    pub children: Option<Box<[QuadTreeNode; 4]>>,
    /// Unique cell index (assigned during leaf enumeration).
    pub cell_id: usize,
}
impl QuadTreeNode {
    /// Create a root node covering `[x0, x0+width] × [y0, y0+height]`.
    pub fn new_root(x0: f64, y0: f64, width: f64, height: f64) -> Self {
        Self {
            x0,
            y0,
            width,
            height,
            level: 0,
            error_indicator: 0.0,
            children: None,
            cell_id: 0,
        }
    }
    /// Create a child node.
    fn new_child(x0: f64, y0: f64, width: f64, height: f64, level: u32) -> Self {
        Self {
            x0,
            y0,
            width,
            height,
            level,
            error_indicator: 0.0,
            children: None,
            cell_id: 0,
        }
    }
    /// Return the cell width (same as `self.width`).
    pub fn cell_width(&self) -> f64 {
        self.width
    }
    /// Return the cell height (same as `self.height`).
    pub fn cell_height(&self) -> f64 {
        self.height
    }
    /// Return `true` if this node has no children (is a leaf).
    pub fn is_leaf(&self) -> bool {
        self.children.is_none()
    }
    /// Refine this node into four child cells (SW, SE, NW, NE).
    ///
    /// Does nothing if the node is already refined.
    pub fn refine(&mut self) {
        if self.children.is_some() {
            return;
        }
        let hw = self.width * 0.5;
        let hh = self.height * 0.5;
        let lev = self.level + 1;
        let sw = Self::new_child(self.x0, self.y0, hw, hh, lev);
        let se = Self::new_child(self.x0 + hw, self.y0, hw, hh, lev);
        let nw = Self::new_child(self.x0, self.y0 + hh, hw, hh, lev);
        let ne = Self::new_child(self.x0 + hw, self.y0 + hh, hw, hh, lev);
        self.children = Some(Box::new([sw, se, nw, ne]));
    }
    /// Count the total number of leaf nodes in this subtree.
    pub fn leaf_count(&self) -> usize {
        match &self.children {
            None => 1,
            Some(ch) => ch.iter().map(|c| c.leaf_count()).sum(),
        }
    }
    /// Enumerate leaf nodes and assign sequential cell IDs.
    ///
    /// Returns the number of leaves assigned.
    pub fn enumerate_leaves(&mut self, start_id: usize) -> usize {
        match self.children.as_mut() {
            None => {
                self.cell_id = start_id;
                start_id + 1
            }
            Some(ch) => {
                let mut id = start_id;
                for child in ch.iter_mut() {
                    id = child.enumerate_leaves(id);
                }
                id
            }
        }
    }
    /// Collect all leaf nodes as immutable references.
    pub fn collect_leaves<'a>(&'a self, leaves: &mut Vec<&'a QuadTreeNode>) {
        match &self.children {
            None => leaves.push(self),
            Some(ch) => {
                for child in ch.iter() {
                    child.collect_leaves(leaves);
                }
            }
        }
    }
    /// Centroid x-coordinate.
    pub fn cx(&self) -> f64 {
        self.x0 + 0.5 * self.width
    }
    /// Centroid y-coordinate.
    pub fn cy(&self) -> f64 {
        self.y0 + 0.5 * self.height
    }
}
/// Incomplete LU factorization with level-of-fill `k` (ILU(k)).
///
/// Level k = 0 reproduces ILU(0) (no fill).  Level k = 1 allows one level
/// of fill beyond the original pattern, and so on.  Higher levels produce
/// better preconditioners at the cost of more memory.
///
/// The factorization is stored in CSR format with an extended sparsity pattern
/// computed via symbolic analysis.
#[derive(Debug, Clone)]
pub struct IlukPreconditioner {
    /// L and U factors in combined CSR (L strictly lower, U includes diagonal).
    pub(super) lu_values: Vec<f64>,
    pub(super) row_ptr: Vec<usize>,
    pub(super) col_indices: Vec<usize>,
    /// Level-of-fill for each non-zero.
    pub(super) fill_levels: Vec<u32>,
    pub(super) n: usize,
    /// Fill level parameter k.
    pub(super) k: u32,
}
impl IlukPreconditioner {
    /// Compute the ILU(k) factorization of matrix `a` with fill level `k`.
    ///
    /// For k=0 this matches ILU(0).  For k≥1 additional fill-in entries
    /// are allowed in the sparsity pattern.
    pub fn new(a: &CsrMatrix, k: u32) -> Self {
        assert_eq!(a.nrows, a.ncols, "matrix must be square");
        let n = a.nrows;
        let mut pattern: Vec<Vec<(usize, u32)>> = (0..n)
            .map(|row| {
                let start = a.row_ptr[row];
                let end = a.row_ptr[row + 1];
                let mut row_pat: Vec<(usize, u32)> =
                    (start..end).map(|idx| (a.col_indices[idx], 0)).collect();
                row_pat.sort_by_key(|&(c, _)| c);
                row_pat
            })
            .collect();
        for i in 1..n {
            let mut j = 0;
            while j < pattern[i].len() {
                let (col, lev_ij) = pattern[i][j];
                if col >= i {
                    break;
                }
                let row_p_copy: Vec<(usize, u32)> = pattern[col].clone();
                for &(q, lev_pq) in &row_p_copy {
                    if q <= col {
                        continue;
                    }
                    let new_lev = lev_ij + lev_pq + 1;
                    if new_lev <= k {
                        match pattern[i].binary_search_by_key(&q, |&(c, _)| c) {
                            Ok(pos) => {
                                if pattern[i][pos].1 > new_lev {
                                    pattern[i][pos].1 = new_lev;
                                }
                            }
                            Err(pos) => {
                                pattern[i].insert(pos, (q, new_lev));
                            }
                        }
                    }
                }
                j += 1;
            }
        }
        let mut row_ptr = vec![0usize; n + 1];
        for i in 0..n {
            row_ptr[i + 1] = row_ptr[i] + pattern[i].len();
        }
        let nnz = row_ptr[n];
        let mut col_indices = vec![0usize; nnz];
        let mut fill_levels = vec![0u32; nnz];
        let mut lu_values = vec![0.0f64; nnz];
        for i in 0..n {
            let start = row_ptr[i];
            for (k_idx, &(col, lev)) in pattern[i].iter().enumerate() {
                col_indices[start + k_idx] = col;
                fill_levels[start + k_idx] = lev;
            }
        }
        for row in 0..n {
            let a_start = a.row_ptr[row];
            let a_end = a.row_ptr[row + 1];
            for a_idx in a_start..a_end {
                let col = a.col_indices[a_idx];
                let lu_start = row_ptr[row];
                let lu_end = row_ptr[row + 1];
                if let Ok(offset) = col_indices[lu_start..lu_end].binary_search(&col) {
                    lu_values[lu_start + offset] = a.values[a_idx];
                }
            }
        }
        for i in 1..n {
            let row_start = row_ptr[i];
            let row_end = row_ptr[i + 1];
            let lower_cols: Vec<usize> = (row_start..row_end)
                .map(|p| col_indices[p])
                .take_while(|&c| c < i)
                .collect();
            for &kk in &lower_cols {
                let k_start = row_ptr[kk];
                let k_end = row_ptr[kk + 1];
                let diag_k = match col_indices[k_start..k_end].binary_search(&kk) {
                    Ok(off) => lu_values[k_start + off],
                    Err(_) => 0.0,
                };
                if diag_k.abs() < 1e-60 {
                    continue;
                }
                let p_ik = col_indices[row_start..row_end]
                    .binary_search(&kk)
                    .map(|off| row_start + off)
                    .unwrap_or(usize::MAX);
                if p_ik == usize::MAX {
                    continue;
                }
                lu_values[p_ik] /= diag_k;
                let factor = lu_values[p_ik];
                for k_idx in k_start..k_end {
                    let j = col_indices[k_idx];
                    if j <= kk {
                        continue;
                    }
                    if let Ok(off) = col_indices[row_start..row_end].binary_search(&j) {
                        lu_values[row_start + off] -= factor * lu_values[k_idx];
                    }
                }
            }
        }
        IlukPreconditioner {
            lu_values,
            row_ptr,
            col_indices,
            fill_levels,
            n,
            k,
        }
    }
    /// Apply the ILU(k) preconditioner: solve (LU) z = r.
    pub fn solve(&self, rhs: &[f64]) -> Vec<f64> {
        assert_eq!(rhs.len(), self.n);
        let n = self.n;
        let mut y = rhs.to_vec();
        for i in 0..n {
            let start = self.row_ptr[i];
            let end = self.row_ptr[i + 1];
            for p in start..end {
                let j = self.col_indices[p];
                if j >= i {
                    break;
                }
                y[i] -= self.lu_values[p] * y[j];
            }
        }
        for i in (0..n).rev() {
            let start = self.row_ptr[i];
            let end = self.row_ptr[i + 1];
            let mut diag = 1.0;
            for p in start..end {
                let j = self.col_indices[p];
                if j == i {
                    diag = self.lu_values[p];
                } else if j > i {
                    y[i] -= self.lu_values[p] * y[j];
                }
            }
            if diag.abs() > 1e-60 {
                y[i] /= diag;
            }
        }
        y
    }
    /// Return the fill level parameter used for this factorization.
    pub fn fill_level(&self) -> u32 {
        self.k
    }
    /// Return the number of non-zeros in the extended sparsity pattern.
    pub fn nnz(&self) -> usize {
        self.lu_values.len()
    }
    /// Return the fill-in count: entries added beyond the original pattern.
    pub fn fill_in_count(&self) -> usize {
        self.fill_levels.iter().filter(|&&lev| lev > 0).count()
    }
}
/// Block sparse matrix where each scalar entry is replaced by a 3×3 dense
/// block.  Suitable for 3-D elasticity problems where DOFs come in groups of
/// three per node.
///
/// The block structure is stored in CSR format on the block level.  Each
/// block `(i, j)` holds a flat 9-element row-major 3×3 sub-matrix.
#[derive(Debug, Clone)]
pub struct BlockCsrMatrix3 {
    /// Row pointer array (block-level, length = n_block_rows + 1).
    pub row_ptr: Vec<usize>,
    /// Block column indices (block-level).
    pub col_indices: Vec<usize>,
    /// Block values: each entry is a 9-element \[f64; 9\] (row-major 3×3 block).
    pub blocks: Vec<[f64; 9]>,
    /// Number of block rows.
    pub n_block_rows: usize,
    /// Number of block columns.
    pub n_block_cols: usize,
}
impl BlockCsrMatrix3 {
    /// Create an empty block CSR matrix.
    pub fn new(n_block_rows: usize, n_block_cols: usize) -> Self {
        Self {
            row_ptr: vec![0; n_block_rows + 1],
            col_indices: Vec::new(),
            blocks: Vec::new(),
            n_block_rows,
            n_block_cols,
        }
    }
    /// Build from a list of block-level triplets `(block_row, block_col, block_3x3)`.
    ///
    /// Duplicate block entries at the same `(block_row, block_col)` are summed
    /// element-wise.
    pub fn from_block_triplets(
        n_block_rows: usize,
        n_block_cols: usize,
        triplets: &[(usize, usize, [f64; 9])],
    ) -> Self {
        let mut map: std::collections::HashMap<(usize, usize), [f64; 9]> =
            std::collections::HashMap::new();
        for &(r, c, ref blk) in triplets {
            let entry = map.entry((r, c)).or_insert([0.0; 9]);
            for k in 0..9 {
                entry[k] += blk[k];
            }
        }
        let mut entries: Vec<((usize, usize), [f64; 9])> = map.into_iter().collect();
        entries.sort_by_key(|&((r, c), _)| (r, c));
        let mut row_ptr = vec![0usize; n_block_rows + 1];
        let mut col_indices = Vec::new();
        let mut blocks = Vec::new();
        for &((r, c), ref blk) in &entries {
            row_ptr[r + 1] += 1;
            col_indices.push(c);
            blocks.push(*blk);
        }
        for i in 1..=n_block_rows {
            row_ptr[i] += row_ptr[i - 1];
        }
        Self {
            row_ptr,
            col_indices,
            blocks,
            n_block_rows,
            n_block_cols,
        }
    }
    /// Multiply by a dense vector of length `3 * n_block_cols`.
    ///
    /// Returns a dense vector of length `3 * n_block_rows`.
    pub fn mul_vec(&self, x: &[f64]) -> Vec<f64> {
        assert_eq!(x.len(), self.n_block_cols * 3);
        let mut y = vec![0.0f64; self.n_block_rows * 3];
        for br in 0..self.n_block_rows {
            let start = self.row_ptr[br];
            let end = self.row_ptr[br + 1];
            for bidx in start..end {
                let bc = self.col_indices[bidx];
                let blk = &self.blocks[bidx];
                for i in 0..3 {
                    for j in 0..3 {
                        y[br * 3 + i] += blk[i * 3 + j] * x[bc * 3 + j];
                    }
                }
            }
        }
        y
    }
    /// Return the number of stored blocks.
    pub fn n_blocks(&self) -> usize {
        self.blocks.len()
    }
    /// Get a block at `(block_row, block_col)`.  Returns a zero block if not
    /// stored.
    pub fn get_block(&self, block_row: usize, block_col: usize) -> [f64; 9] {
        let start = self.row_ptr[block_row];
        let end = self.row_ptr[block_row + 1];
        for idx in start..end {
            if self.col_indices[idx] == block_col {
                return self.blocks[idx];
            }
        }
        [0.0; 9]
    }
    /// Frobenius norm of the entire block matrix.
    pub fn frobenius_norm(&self) -> f64 {
        self.blocks
            .iter()
            .flat_map(|b| b.iter())
            .map(|v| v * v)
            .sum::<f64>()
            .sqrt()
    }
    /// Convert to a scalar CSR matrix (expand each 3×3 block into scalar entries).
    pub fn to_scalar_csr(&self) -> CsrMatrix {
        let n_scalar_rows = self.n_block_rows * 3;
        let n_scalar_cols = self.n_block_cols * 3;
        let mut triplets = Vec::with_capacity(self.blocks.len() * 9);
        for br in 0..self.n_block_rows {
            let start = self.row_ptr[br];
            let end = self.row_ptr[br + 1];
            for bidx in start..end {
                let bc = self.col_indices[bidx];
                let blk = &self.blocks[bidx];
                for i in 0..3 {
                    for j in 0..3 {
                        let v = blk[i * 3 + j];
                        if v.abs() > 1e-30 {
                            triplets.push((br * 3 + i, bc * 3 + j, v));
                        }
                    }
                }
            }
        }
        CsrMatrix::from_triplets(n_scalar_rows, n_scalar_cols, &triplets)
    }
}
/// Compressed Sparse Row (CSR) matrix.
///
/// Stores a sparse matrix in CSR format with row pointers, column indices,
/// and values arrays. Efficient for matrix-vector multiplication and row access.
#[derive(Debug, Clone)]
pub struct CsrMatrix {
    /// Row pointer array (length = nrows + 1).
    pub row_ptr: Vec<usize>,
    /// Column indices for non-zero entries.
    pub col_indices: Vec<usize>,
    /// Non-zero values.
    pub values: Vec<f64>,
    /// Number of rows.
    pub nrows: usize,
    /// Number of columns.
    pub ncols: usize,
}
impl CsrMatrix {
    /// Create a new empty CSR matrix with the given dimensions.
    pub fn new(nrows: usize, ncols: usize) -> Self {
        Self {
            row_ptr: vec![0; nrows + 1],
            col_indices: Vec::new(),
            values: Vec::new(),
            nrows,
            ncols,
        }
    }
    /// Build a CSR matrix from coordinate (triplet) format.
    ///
    /// Duplicate entries at the same `(row, col)` are summed together.
    ///
    /// # Panics
    ///
    /// Panics if any index is out of bounds.
    pub fn from_triplets(nrows: usize, ncols: usize, triplets: &[(usize, usize, f64)]) -> Self {
        let mut map: HashMap<(usize, usize), f64> = HashMap::new();
        for &(r, c, v) in triplets {
            assert!(r < nrows, "row index {r} out of bounds for {nrows} rows");
            assert!(c < ncols, "col index {c} out of bounds for {ncols} cols");
            *map.entry((r, c)).or_insert(0.0) += v;
        }
        let mut entries: Vec<((usize, usize), f64)> = map.into_iter().collect();
        entries.sort_by_key(|&((r, c), _)| (r, c));
        let mut row_ptr = vec![0usize; nrows + 1];
        let mut col_indices = Vec::with_capacity(entries.len());
        let mut values = Vec::with_capacity(entries.len());
        for &((r, c), v) in &entries {
            row_ptr[r + 1] += 1;
            col_indices.push(c);
            values.push(v);
        }
        for i in 1..=nrows {
            row_ptr[i] += row_ptr[i - 1];
        }
        Self {
            row_ptr,
            col_indices,
            values,
            nrows,
            ncols,
        }
    }
    /// Get the value at `(row, col)`. Returns 0.0 if the entry is not stored.
    ///
    /// # Panics
    ///
    /// Panics if indices are out of bounds.
    pub fn get(&self, row: usize, col: usize) -> f64 {
        assert!(row < self.nrows);
        assert!(col < self.ncols);
        let start = self.row_ptr[row];
        let end = self.row_ptr[row + 1];
        for idx in start..end {
            if self.col_indices[idx] == col {
                return self.values[idx];
            }
        }
        0.0
    }
    /// Set the value at `(row, col)`. If the entry exists, update it;
    /// otherwise insert a new entry.
    ///
    /// **Note:** Inserting new entries is O(nnz) in the worst case because the
    /// arrays must be shifted. Prefer [`from_triplets`](Self::from_triplets) for
    /// bulk construction.
    ///
    /// # Panics
    ///
    /// Panics if indices are out of bounds.
    pub fn set(&mut self, row: usize, col: usize, value: f64) {
        assert!(row < self.nrows);
        assert!(col < self.ncols);
        let start = self.row_ptr[row];
        let end = self.row_ptr[row + 1];
        for idx in start..end {
            if self.col_indices[idx] == col {
                self.values[idx] = value;
                return;
            }
        }
        let mut insert_pos = start;
        while insert_pos < end && self.col_indices[insert_pos] < col {
            insert_pos += 1;
        }
        self.col_indices.insert(insert_pos, col);
        self.values.insert(insert_pos, value);
        for r in (row + 1)..=self.nrows {
            self.row_ptr[r] += 1;
        }
    }
    /// Add `value` to the entry at `(row, col)`. If the entry does not exist,
    /// it is created with the given value. This is the primary method used
    /// during stiffness matrix assembly.
    ///
    /// # Panics
    ///
    /// Panics if indices are out of bounds.
    pub fn add_to(&mut self, row: usize, col: usize, value: f64) {
        assert!(row < self.nrows);
        assert!(col < self.ncols);
        let start = self.row_ptr[row];
        let end = self.row_ptr[row + 1];
        for idx in start..end {
            if self.col_indices[idx] == col {
                self.values[idx] += value;
                return;
            }
        }
        let mut insert_pos = start;
        while insert_pos < end && self.col_indices[insert_pos] < col {
            insert_pos += 1;
        }
        self.col_indices.insert(insert_pos, col);
        self.values.insert(insert_pos, value);
        for r in (row + 1)..=self.nrows {
            self.row_ptr[r] += 1;
        }
    }
    /// Multiply this matrix by a dense vector: `y = A * x`.
    ///
    /// # Panics
    ///
    /// Panics if `x.len() != self.ncols`.
    pub fn mul_vec(&self, x: &[f64]) -> Vec<f64> {
        assert_eq!(x.len(), self.ncols, "vector length must equal ncols");
        let mut y = vec![0.0; self.nrows];
        for (row, y_row) in y.iter_mut().enumerate().take(self.nrows) {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];
            let mut sum = 0.0;
            for idx in start..end {
                sum += self.values[idx] * x[self.col_indices[idx]];
            }
            *y_row = sum;
        }
        y
    }
    /// Return the number of stored non-zero entries.
    pub fn nnz(&self) -> usize {
        self.values.len()
    }
    /// Get the diagonal element at `(i, i)`.
    pub fn diagonal(&self, i: usize) -> f64 {
        self.get(i, i)
    }
    /// Transpose this CSR matrix, returning a new CSR matrix.
    pub fn transpose(&self) -> CsrMatrix {
        let mut triplets = Vec::with_capacity(self.nnz());
        for row in 0..self.nrows {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];
            for idx in start..end {
                triplets.push((self.col_indices[idx], row, self.values[idx]));
            }
        }
        CsrMatrix::from_triplets(self.ncols, self.nrows, &triplets)
    }
    /// Add two CSR matrices: C = A + B.
    ///
    /// Both matrices must have the same dimensions.
    pub fn add(&self, other: &CsrMatrix) -> CsrMatrix {
        assert_eq!(self.nrows, other.nrows, "row dimensions must match");
        assert_eq!(self.ncols, other.ncols, "col dimensions must match");
        let mut triplets = Vec::with_capacity(self.nnz() + other.nnz());
        for row in 0..self.nrows {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];
            for idx in start..end {
                triplets.push((row, self.col_indices[idx], self.values[idx]));
            }
        }
        for row in 0..other.nrows {
            let start = other.row_ptr[row];
            let end = other.row_ptr[row + 1];
            for idx in start..end {
                triplets.push((row, other.col_indices[idx], other.values[idx]));
            }
        }
        CsrMatrix::from_triplets(self.nrows, self.ncols, &triplets)
    }
    /// Scale all values by a scalar: A *= alpha.
    pub fn scale(&mut self, alpha: f64) {
        for v in self.values.iter_mut() {
            *v *= alpha;
        }
    }
    /// Return a scaled copy: B = alpha * A.
    pub fn scaled(&self, alpha: f64) -> CsrMatrix {
        let mut result = self.clone();
        result.scale(alpha);
        result
    }
    /// Optimized sparse matrix-vector multiply using row-based access.
    ///
    /// Same as `mul_vec` but with explicit prefetch-friendly ordering.
    /// y = alpha * A * x + beta * y
    pub fn mul_vec_axpby(&self, x: &[f64], y: &mut [f64], alpha: f64, beta: f64) {
        assert_eq!(x.len(), self.ncols);
        assert_eq!(y.len(), self.nrows);
        for (row, y_row) in y.iter_mut().enumerate() {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];
            let mut sum = 0.0;
            for idx in start..end {
                sum += self.values[idx] * x[self.col_indices[idx]];
            }
            *y_row = alpha * sum + beta * *y_row;
        }
    }
    /// Symmetric sparse matrix-vector multiply (upper triangle only).
    ///
    /// Assumes only the upper triangle is stored. Computes y = A * x
    /// using symmetry: a_ij contributes to both y\[i\] and y\[j\].
    pub fn symmetric_mul_vec(&self, x: &[f64]) -> Vec<f64> {
        assert_eq!(x.len(), self.ncols);
        assert_eq!(
            self.nrows, self.ncols,
            "matrix must be square for symmetric multiply"
        );
        let n = self.nrows;
        let mut y = vec![0.0; n];
        for row in 0..n {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];
            for idx in start..end {
                let col = self.col_indices[idx];
                let val = self.values[idx];
                y[row] += val * x[col];
                if col != row {
                    y[col] += val * x[row];
                }
            }
        }
        y
    }
    /// Extract diagonal as a vector.
    pub fn diagonal_vec(&self) -> Vec<f64> {
        let n = self.nrows.min(self.ncols);
        (0..n).map(|i| self.get(i, i)).collect()
    }
    /// Convert this CSR matrix to CSC format.
    pub fn to_csc(&self) -> CscMatrix {
        let mut triplets = Vec::with_capacity(self.nnz());
        for row in 0..self.nrows {
            let start = self.row_ptr[row];
            let end = self.row_ptr[row + 1];
            for idx in start..end {
                triplets.push((row, self.col_indices[idx], self.values[idx]));
            }
        }
        CscMatrix::from_triplets(self.nrows, self.ncols, &triplets)
    }
    /// Frobenius norm: ||A||_F = sqrt(sum of a_ij^2).
    pub fn frobenius_norm(&self) -> f64 {
        self.values.iter().map(|v| v * v).sum::<f64>().sqrt()
    }
}
/// Thin wrapper around `Vec`f64` for sparse/dense vector operations.
#[derive(Debug, Clone)]
pub struct SparseVector {
    /// The underlying dense data.
    pub data: Vec<f64>,
}
impl SparseVector {
    /// Create a new zero vector of the given length.
    pub fn new(len: usize) -> Self {
        Self {
            data: vec![0.0; len],
        }
    }
    /// Create a sparse vector from existing data.
    pub fn from_vec(data: Vec<f64>) -> Self {
        Self { data }
    }
    /// Return the length of the vector.
    pub fn len(&self) -> usize {
        self.data.len()
    }
    /// Check if the vector is empty.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
    /// Compute the dot product with another vector.
    pub fn dot(&self, other: &Self) -> f64 {
        self.data
            .iter()
            .zip(other.data.iter())
            .map(|(a, b)| a * b)
            .sum()
    }
    /// Compute the Euclidean norm.
    pub fn norm(&self) -> f64 {
        self.data.iter().map(|x| x * x).sum::<f64>().sqrt()
    }
    /// AXPY: self = alpha * self + beta * other
    pub fn axpby(&mut self, alpha: f64, other: &SparseVector, beta: f64) {
        assert_eq!(self.data.len(), other.data.len());
        for (a, b) in self.data.iter_mut().zip(other.data.iter()) {
            *a = alpha * *a + beta * *b;
        }
    }
    /// Scale all entries by a scalar.
    pub fn scale(&mut self, alpha: f64) {
        for v in self.data.iter_mut() {
            *v *= alpha;
        }
    }
}
/// Incomplete LU factorization with zero fill-in (ILU(0)).
///
/// The factorization maintains the same sparsity pattern as the original matrix.
/// Used as a preconditioner for iterative solvers like CG or GMRES.
#[derive(Debug, Clone)]
pub struct Ilu0Preconditioner {
    /// Combined L and U factors stored in CSR format.
    /// L has unit diagonal (not stored), U diagonal is stored.
    /// The storage reuses the sparsity pattern of A.
    pub(super) lu_values: Vec<f64>,
    pub(super) row_ptr: Vec<usize>,
    pub(super) col_indices: Vec<usize>,
    pub(super) n: usize,
}
impl Ilu0Preconditioner {
    /// Compute the ILU(0) factorization of matrix A.
    ///
    /// The factorization uses the same sparsity pattern as A. Elements that
    /// would be fill-in are dropped.
    pub fn new(a: &CsrMatrix) -> Self {
        assert_eq!(a.nrows, a.ncols, "matrix must be square");
        let n = a.nrows;
        let mut lu_values = a.values.clone();
        let row_ptr = a.row_ptr.clone();
        let col_indices = a.col_indices.clone();
        for i in 1..n {
            let row_start = row_ptr[i];
            let row_end = row_ptr[i + 1];
            for p in row_start..row_end {
                let k = col_indices[p];
                if k >= i {
                    break;
                }
                let k_start = row_ptr[k];
                let k_end = row_ptr[k + 1];
                let mut diag_k = 0.0;
                for q in k_start..k_end {
                    if col_indices[q] == k {
                        diag_k = lu_values[q];
                        break;
                    }
                }
                if diag_k.abs() < 1e-60 {
                    continue;
                }
                lu_values[p] /= diag_k;
                let factor = lu_values[p];
                for q in k_start..k_end {
                    let j = col_indices[q];
                    if j <= k {
                        continue;
                    }
                    for s in row_start..row_end {
                        if col_indices[s] == j {
                            lu_values[s] -= factor * lu_values[q];
                            break;
                        }
                    }
                }
            }
        }
        Ilu0Preconditioner {
            lu_values,
            row_ptr,
            col_indices,
            n,
        }
    }
    /// Apply the ILU(0) preconditioner: solve (L U) z = r.
    ///
    /// First forward-substitutes L y = r (L has unit diagonal),
    /// then back-substitutes U z = y.
    pub fn solve(&self, rhs: &[f64]) -> Vec<f64> {
        assert_eq!(rhs.len(), self.n);
        let n = self.n;
        let mut y = rhs.to_vec();
        for i in 0..n {
            let start = self.row_ptr[i];
            let end = self.row_ptr[i + 1];
            for p in start..end {
                let j = self.col_indices[p];
                if j >= i {
                    break;
                }
                y[i] -= self.lu_values[p] * y[j];
            }
        }
        for i in (0..n).rev() {
            let start = self.row_ptr[i];
            let end = self.row_ptr[i + 1];
            let mut diag = 1.0;
            for p in start..end {
                let j = self.col_indices[p];
                if j == i {
                    diag = self.lu_values[p];
                } else if j > i {
                    y[i] -= self.lu_values[p] * y[j];
                }
            }
            if diag.abs() > 1e-60 {
                y[i] /= diag;
            }
        }
        y
    }
}
/// Compressed Sparse Column (CSC) matrix.
///
/// Stores a sparse matrix in CSC format with column pointers, row indices,
/// and values arrays. Efficient for column access and certain direct solvers.
#[derive(Debug, Clone)]
pub struct CscMatrix {
    /// Column pointer array (length = ncols + 1).
    pub col_ptr: Vec<usize>,
    /// Row indices for non-zero entries.
    pub row_indices: Vec<usize>,
    /// Non-zero values.
    pub values: Vec<f64>,
    /// Number of rows.
    pub nrows: usize,
    /// Number of columns.
    pub ncols: usize,
}
impl CscMatrix {
    /// Build a CSC matrix from coordinate (triplet) format.
    ///
    /// Duplicate entries are summed.
    pub fn from_triplets(nrows: usize, ncols: usize, triplets: &[(usize, usize, f64)]) -> Self {
        let mut map: HashMap<(usize, usize), f64> = HashMap::new();
        for &(r, c, v) in triplets {
            assert!(r < nrows, "row index {r} out of bounds for {nrows} rows");
            assert!(c < ncols, "col index {c} out of bounds for {ncols} cols");
            *map.entry((r, c)).or_insert(0.0) += v;
        }
        let mut entries: Vec<((usize, usize), f64)> = map.into_iter().collect();
        entries.sort_by_key(|&((r, c), _)| (c, r));
        let mut col_ptr = vec![0usize; ncols + 1];
        let mut row_indices = Vec::with_capacity(entries.len());
        let mut values = Vec::with_capacity(entries.len());
        for &((r, c), v) in &entries {
            col_ptr[c + 1] += 1;
            row_indices.push(r);
            values.push(v);
        }
        for i in 1..=ncols {
            col_ptr[i] += col_ptr[i - 1];
        }
        Self {
            col_ptr,
            row_indices,
            values,
            nrows,
            ncols,
        }
    }
    /// Get the value at (row, col).
    pub fn get(&self, row: usize, col: usize) -> f64 {
        assert!(row < self.nrows);
        assert!(col < self.ncols);
        let start = self.col_ptr[col];
        let end = self.col_ptr[col + 1];
        for idx in start..end {
            if self.row_indices[idx] == row {
                return self.values[idx];
            }
        }
        0.0
    }
    /// Number of stored non-zero entries.
    pub fn nnz(&self) -> usize {
        self.values.len()
    }
    /// Multiply by a dense vector: y = A * x.
    pub fn mul_vec(&self, x: &[f64]) -> Vec<f64> {
        assert_eq!(x.len(), self.ncols, "vector length must equal ncols");
        let mut y = vec![0.0; self.nrows];
        for (col, &x_col) in x.iter().enumerate().take(self.ncols) {
            let start = self.col_ptr[col];
            let end = self.col_ptr[col + 1];
            for idx in start..end {
                y[self.row_indices[idx]] += self.values[idx] * x_col;
            }
        }
        y
    }
    /// Transpose this CSC matrix, returning a new CSC matrix.
    pub fn transpose(&self) -> CscMatrix {
        let mut triplets = Vec::with_capacity(self.nnz());
        for col in 0..self.ncols {
            let start = self.col_ptr[col];
            let end = self.col_ptr[col + 1];
            for idx in start..end {
                triplets.push((col, self.row_indices[idx], self.values[idx]));
            }
        }
        CscMatrix::from_triplets(self.ncols, self.nrows, &triplets)
    }
    /// Convert to CSR format.
    pub fn to_csr(&self) -> CsrMatrix {
        let mut triplets = Vec::with_capacity(self.nnz());
        for col in 0..self.ncols {
            let start = self.col_ptr[col];
            let end = self.col_ptr[col + 1];
            for idx in start..end {
                triplets.push((self.row_indices[idx], col, self.values[idx]));
            }
        }
        CsrMatrix::from_triplets(self.nrows, self.ncols, &triplets)
    }
}