Skip to main content

test_augmented_solve/
test_augmented_solve.rs

1use section_properties::fea::SparseMatrix;
2use section_properties::fea::solvers::SparseLu;
3use serde_json;
4use std::fs::File;
5use std::io::Read;
6
7fn load_json(path: &str) -> serde_json::Value {
8    let mut file = File::open(path).expect("Failed to open file");
9    let mut contents = String::new();
10    file.read_to_string(&mut contents)
11        .expect("Failed to read file");
12    serde_json::from_str(&contents).expect("Failed to parse JSON")
13}
14
15fn main() {
16    // Load the exact augmented system for Angle_100x100 (the one that fails)
17    let data = load_json("rust_augmented_Angle_100x100.json");
18
19    let n_dof = data["n_dof"].as_u64().unwrap() as usize;
20    let A_data = &data["A"];
21    let b: Vec<f64> = serde_json::from_value(data["b"].clone()).unwrap();
22
23    // Build sparse matrix
24    let mut A = SparseMatrix::new(n_dof + 1);
25    let rows: Vec<usize> = serde_json::from_value(A_data["row"].clone()).unwrap();
26    let cols: Vec<usize> = serde_json::from_value(A_data["col"].clone()).unwrap();
27    let data: Vec<f64> = serde_json::from_value(A_data["data"].clone()).unwrap();
28    for ((r, c), v) in rows.into_iter().zip(cols).zip(data) {
29        A.add(r, c, v);
30    }
31    A.compress();
32
33    println!(
34        "Matrix size: {}x{}, nnz: {}",
35        n_dof + 1,
36        n_dof + 1,
37        A_data["data"].as_array().unwrap().len()
38    );
39
40    // Try SparseLU
41    let lu = SparseLu::factor(&A).unwrap();
42
43    // Verify PA = LU
44    let max_diff = lu.verify_pa_eq_lu(&A);
45    println!("max|PA - LU| = {:.2e}", max_diff);
46
47    let x = lu.solve(&b).unwrap();
48
49    println!("Solution x (first 10): {:?}", &x[..10.min(x.len())]);
50    println!("x len: {}", x.len());
51    println!("Last element (lambda): {}", x.last().unwrap());
52
53    // Check if PA = LU holds
54    let max_diff = lu.verify_pa_eq_lu(&A);
55    println!("max|PA - LU| = {:.2e}", max_diff);
56
57    // Compute residual
58    let n = A.n;
59    let mut res = vec![0.0; n];
60    let (rows, cols, vals) = A.triplets();
61    for (&i, (&j, &v)) in rows.iter().zip(cols.iter().zip(vals.iter())) {
62        res[i] += v * x[j];
63    }
64
65    let max_res = res
66        .iter()
67        .zip(b.iter())
68        .map(|(&r, &b)| (r - b).abs())
69        .fold(0.0, f64::max);
70    let b_norm = b.iter().map(|&v| v.abs()).fold(0.0, f64::max);
71    println!(
72        "Max residual: {:.2e}, rel: {:.2e}",
73        max_res,
74        max_res / b_norm.max(1e-300)
75    );
76
77    // Check constraint residual C^T * omega
78    let c_data: Vec<f64> =
79        serde_json::from_value(load_json("rust_augmented_Angle_100x100.json")["C"].clone())
80            .unwrap();
81    let omega = &x[..x.len() - 1];
82    let ct_omega: f64 = c_data
83        .iter()
84        .zip(x.iter().take(n_dof))
85        .map(|(&c, &w)| c * w)
86        .sum();
87    println!("C^T * omega: {:.2e}", ct_omega);
88    println!("Lambda: {:.2e}", x.last().unwrap());
89}