Skip to main content

export_element/
export_element.rs

1//! Export element data for Python cross-validation
2
3use section_properties::fea::{Tri6, gauss_points, shape_function};
4use section_properties::geometry::Point;
5use std::fs::File;
6use std::io::Write;
7
8fn format_array(arr: &[f64]) -> String {
9    arr.iter()
10        .map(|v| format!("{:.10}", v))
11        .collect::<Vec<_>>()
12        .join(", ")
13}
14
15fn format_matrix(mat: &[[f64; 6]; 6]) -> String {
16    let mut result = String::new();
17    for i in 0..6 {
18        result.push_str("    [");
19        for j in 0..6 {
20            result.push_str(&format!("{:.10}", mat[i][j]));
21            if j < 5 {
22                result.push_str(", ");
23            }
24        }
25        if i < 5 {
26            result.push_str("],\n");
27        } else {
28            result.push_str("]\n");
29        }
30    }
31    result
32}
33
34fn main() {
35    // Create a simple channel-like element for comparison
36    let points = [
37        Point::new(0.0, 0.0),
38        Point::new(10.0, 0.0),
39        Point::new(0.0, 2.0),
40        Point::new(5.0, 0.0),
41        Point::new(5.0, 1.0),
42        Point::new(0.0, 1.0),
43    ];
44
45    let tri6 = Tri6::from_points(
46        0,
47        points,
48        [0, 1, 2, 3, 4, 5],
49        200_000.0, // E = 200 GPa
50        76_923.0,  // G
51        7850.0,    // density
52    )
53    .unwrap();
54
55    let (k_el, f_el, c_el) = tri6.torsion_properties();
56    let gps = gauss_points(6);
57
58    // Compute Gauss point details
59    let mut gauss_details = String::new();
60    for i in 0..6 {
61        let sf = shape_function(&tri6.coords, (gps[i].1, gps[i].2, gps[i].3));
62        let n_str = format_array(&sf.n);
63        let b0_str = format_array(&sf.b[0]);
64        let b1_str = format_array(&sf.b[1]);
65
66        gauss_details.push_str(&format!(
67            "    {{\n      \"w\": {:.10},\n      \"eta\": {:.10},\n      \"xi\": {:.10},\n      \"zeta\": {:.10},\n      \"n\": [{}],\n      \"b\": [\n        [{}],\n        [{}]\n      ],\n      \"j\": {:.10},\n      \"x\": {:.10},\n      \"y\": {:.10}\n    }}{}\n",
68            gps[i].0, gps[i].1, gps[i].2, gps[i].3,
69            n_str, b0_str, b1_str,
70            sf.j, sf.x, sf.y,
71            if i < 5 { "," } else { "" }
72        ));
73    }
74
75    // Write to JSON file
76    let mut file = File::create("element_data.json").unwrap();
77    writeln!(file, "{{").unwrap();
78    writeln!(file, "  \"coords\": {{").unwrap();
79    writeln!(
80        file,
81        "    \"x\": [{}],",
82        format_array(&[
83            tri6.coords[0][0],
84            tri6.coords[0][1],
85            tri6.coords[0][2],
86            tri6.coords[0][3],
87            tri6.coords[0][4],
88            tri6.coords[0][5]
89        ])
90    )
91    .unwrap();
92    writeln!(
93        file,
94        "    \"y\": [{}]",
95        format_array(&[
96            tri6.coords[1][0],
97            tri6.coords[1][1],
98            tri6.coords[1][2],
99            tri6.coords[1][3],
100            tri6.coords[1][4],
101            tri6.coords[1][5]
102        ])
103    )
104    .unwrap();
105    writeln!(file, "  }},").unwrap();
106
107    writeln!(file, "  \"k_el\": [\n{}  ],", format_matrix(&k_el)).unwrap();
108    writeln!(file, "  \"f_el\": [{}],", format_array(&f_el)).unwrap();
109    writeln!(file, "  \"c_el\": [{}]", format_array(&c_el)).unwrap();
110    writeln!(file, ",").unwrap();
111
112    // Gauss points
113    writeln!(file, "  \"gauss_points\": [").unwrap();
114    for i in 0..6 {
115        writeln!(
116            file,
117            "    [{:.10}, {:.10}, {:.10}, {:.10}]{}",
118            gps[i].0,
119            gps[i].1,
120            gps[i].2,
121            gps[i].3,
122            if i < 5 { "," } else { "" }
123        )
124        .unwrap();
125    }
126    writeln!(file, "  ],").unwrap();
127
128    writeln!(file, "  \"gauss_details\": [\n{}\n  ]", gauss_details).unwrap();
129    writeln!(file, "}}").unwrap();
130
131    println!("Element data exported to element_data.json");
132}