Skip to main content

qualia_core_db/specialized_libs/engineering_analysis/
buckling.rs

1use super::*;
2
3/// Buckling analysis
4pub struct BucklingAnalysis {
5    eigenvalue_buckling: EigenvalueBuckling,
6    nonlinear_buckling: NonlinearBuckling,
7}
8
9/// Eigenvalue buckling
10#[derive(Debug, Clone)]
11pub struct EigenvalueBuckling {
12    pub critical_loads: Vec<f64>,
13    pub buckling_modes: Vec<BucklingMode>,
14}
15
16/// Buckling modes
17#[derive(Debug, Clone)]
18pub struct BucklingMode {
19    pub mode_number: u32,
20    pub critical_load: f64,
21    pub mode_shape: Vec<f64>,
22}
23
24/// Nonlinear buckling
25#[derive(Debug, Clone)]
26pub struct NonlinearBuckling {
27    pub load_displacement_curve: Vec<(f64, f64)>,
28    pub post_buckling_behavior: PostBucklingBehavior,
29}
30
31/// Post-buckling behavior
32#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub enum PostBucklingBehavior {
34    Stable,
35    Unstable,
36    SnapThrough,
37}
38impl BucklingAnalysis {
39    /// Creates a default `BucklingAnalysis`.
40    pub fn new() -> Self {
41        Self {
42            eigenvalue_buckling: EigenvalueBuckling::new(),
43            nonlinear_buckling: NonlinearBuckling::new(),
44        }
45    }
46
47    /// Initializes the component; error propagates.
48    pub fn initialize(&mut self) -> Result<(), EngineeringError> {
49        Ok(())
50    }
51
52    /// Borrow the eigenvalue-buckling sub-component.
53    pub fn eigenvalue_buckling(&self) -> &EigenvalueBuckling {
54        &self.eigenvalue_buckling
55    }
56
57    /// Mutably borrow the eigenvalue-buckling sub-component.
58    pub fn eigenvalue_buckling_mut(&mut self) -> &mut EigenvalueBuckling {
59        &mut self.eigenvalue_buckling
60    }
61
62    /// Borrow the nonlinear-buckling sub-component.
63    pub fn nonlinear_buckling(&self) -> &NonlinearBuckling {
64        &self.nonlinear_buckling
65    }
66
67    /// Mutably borrow the nonlinear-buckling sub-component.
68    pub fn nonlinear_buckling_mut(&mut self) -> &mut NonlinearBuckling {
69        &mut self.nonlinear_buckling
70    }
71
72    /// Euler elastic critical buckling loads of a prismatic column:
73    /// `P_cr,n = n²·π²·E·I / (K·L)²` for modes `n = 1..=num_modes`, where `K` is
74    /// the effective-length factor (pinned–pinned = 1.0, fixed–free = 2.0,
75    /// fixed–fixed = 0.5, fixed–pinned ≈ 0.699). Genuine closed-form column
76    /// stability — not a fabricated value. Fills and returns [`EigenvalueBuckling`]:
77    /// `critical_loads` ascending, each [`BucklingMode`] carrying the exact
78    /// buckling half-wave `sin(nπx/L)` sampled at 11 equally-spaced stations.
79    pub fn analyze_euler(
80        &mut self,
81        youngs_modulus: f64,
82        moment_of_inertia: f64,
83        length: f64,
84        effective_length_factor: f64,
85        num_modes: usize,
86    ) -> Result<EigenvalueBuckling, EngineeringError> {
87        if youngs_modulus <= 0.0 || moment_of_inertia <= 0.0 {
88            return Err(EngineeringError::InsufficientData(
89                "Young's modulus and moment of inertia must be positive".to_string(),
90            ));
91        }
92        if length <= 0.0 || effective_length_factor <= 0.0 {
93            return Err(EngineeringError::ValidationError(
94                "length and effective-length factor must be positive".to_string(),
95            ));
96        }
97        if num_modes == 0 {
98            return Err(EngineeringError::InsufficientData(
99                "num_modes must be at least 1".to_string(),
100            ));
101        }
102        let le = effective_length_factor * length;
103        let base = std::f64::consts::PI.powi(2) * youngs_modulus * moment_of_inertia / (le * le);
104        const STATIONS: usize = 11;
105        let mut critical_loads = Vec::with_capacity(num_modes);
106        let mut buckling_modes = Vec::with_capacity(num_modes);
107        for n in 1..=num_modes {
108            let p_cr = (n as f64).powi(2) * base;
109            let shape: Vec<f64> = (0..STATIONS)
110                .map(|s| {
111                    let x = length * s as f64 / (STATIONS as f64 - 1.0);
112                    (n as f64 * std::f64::consts::PI * x / length).sin()
113                })
114                .collect();
115            critical_loads.push(p_cr);
116            buckling_modes.push(BucklingMode {
117                mode_number: n as u32,
118                critical_load: p_cr,
119                mode_shape: shape,
120            });
121        }
122        let eb = EigenvalueBuckling {
123            critical_loads,
124            buckling_modes,
125        };
126        self.eigenvalue_buckling = eb.clone();
127        Ok(eb)
128    }
129
130    /// Euler critical buckling of the member described by `model`: uses the first
131    /// material's Young's modulus, the weak-axis second moment of area of the
132    /// rectangular cross-section `I = min(b·h³, h·b³)/12` from the first two
133    /// geometry dimensions `[b, h]`, the third dimension as the member length `L`,
134    /// and a pinned–pinned effective-length factor `K = 1.0`. Returns the first
135    /// `num_modes` critical loads. Missing/degenerate inputs → `InsufficientData`.
136    pub fn analyze_from_model(
137        &mut self,
138        model: &EngineeringModel,
139        num_modes: usize,
140    ) -> Result<EigenvalueBuckling, EngineeringError> {
141        let material = model.materials.values().next().ok_or_else(|| {
142            EngineeringError::InsufficientData(
143                "model has no material; cannot compute buckling load".to_string(),
144            )
145        })?;
146        let e = material.material_properties.youngs_modulus;
147        let dims = &model.geometry.dimensions;
148        if dims.len() < 3 || dims.iter().take(3).any(|&d| !(d > 0.0)) {
149            return Err(EngineeringError::InsufficientData(
150                "geometry needs three positive dimensions [b, h, L] for column buckling"
151                    .to_string(),
152            ));
153        }
154        let (b, h, l) = (dims[0], dims[1], dims[2]);
155        let i_weak = (b * h * h * h).min(h * b * b * b) / 12.0;
156        self.analyze_euler(e, i_weak, l, 1.0, num_modes)
157    }
158}
159
160impl EigenvalueBuckling {
161    /// Creates a default `EigenvalueBuckling`.
162    pub fn new() -> Self {
163        Self {
164            critical_loads: Vec::new(),
165            buckling_modes: Vec::new(),
166        }
167    }
168}
169
170impl NonlinearBuckling {
171    /// Creates a default `NonlinearBuckling`.
172    pub fn new() -> Self {
173        Self {
174            load_displacement_curve: Vec::new(),
175            post_buckling_behavior: PostBucklingBehavior::Stable,
176        }
177    }
178}