qualia_core_db/specialized_libs/engineering_analysis/
buckling.rs1use super::*;
2
3pub struct BucklingAnalysis {
5 eigenvalue_buckling: EigenvalueBuckling,
6 nonlinear_buckling: NonlinearBuckling,
7}
8
9#[derive(Debug, Clone)]
11pub struct EigenvalueBuckling {
12 pub critical_loads: Vec<f64>,
13 pub buckling_modes: Vec<BucklingMode>,
14}
15
16#[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#[derive(Debug, Clone)]
26pub struct NonlinearBuckling {
27 pub load_displacement_curve: Vec<(f64, f64)>,
28 pub post_buckling_behavior: PostBucklingBehavior,
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub enum PostBucklingBehavior {
34 Stable,
35 Unstable,
36 SnapThrough,
37}
38impl BucklingAnalysis {
39 pub fn new() -> Self {
41 Self {
42 eigenvalue_buckling: EigenvalueBuckling::new(),
43 nonlinear_buckling: NonlinearBuckling::new(),
44 }
45 }
46
47 pub fn initialize(&mut self) -> Result<(), EngineeringError> {
49 Ok(())
50 }
51
52 pub fn eigenvalue_buckling(&self) -> &EigenvalueBuckling {
54 &self.eigenvalue_buckling
55 }
56
57 pub fn eigenvalue_buckling_mut(&mut self) -> &mut EigenvalueBuckling {
59 &mut self.eigenvalue_buckling
60 }
61
62 pub fn nonlinear_buckling(&self) -> &NonlinearBuckling {
64 &self.nonlinear_buckling
65 }
66
67 pub fn nonlinear_buckling_mut(&mut self) -> &mut NonlinearBuckling {
69 &mut self.nonlinear_buckling
70 }
71
72 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 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 pub fn new() -> Self {
163 Self {
164 critical_loads: Vec::new(),
165 buckling_modes: Vec::new(),
166 }
167 }
168}
169
170impl NonlinearBuckling {
171 pub fn new() -> Self {
173 Self {
174 load_displacement_curve: Vec::new(),
175 post_buckling_behavior: PostBucklingBehavior::Stable,
176 }
177 }
178}