Skip to main content

runmat_analysis_core/
lib.rs

1//! Solver-agnostic analysis problem model and validation contracts.
2
3pub mod problem {
4    pub mod bc;
5    pub mod domains;
6    pub mod interfaces;
7    pub mod loads;
8    pub mod material_assignment;
9    pub mod materials;
10    pub mod model;
11    pub mod steps;
12    pub mod structure;
13}
14pub mod field;
15pub mod validate;
16
17pub use field::{AnalysisField, AnalysisFieldValues, DeviceFieldRef};
18pub use problem::bc::{BoundaryCondition, BoundaryConditionKind};
19pub use problem::domains::{
20    CfdDomain, CfdSolveFamily, CfdTimeProfilePoint, ElectroRegionConductivityScale,
21    ElectroThermalDomain, ElectroTimeProfilePoint, ElectromagneticDomain,
22    ThermoFieldInterpolationMode, ThermoFieldSource, ThermoMechanicalDomain,
23    ThermoRegionTemperatureDelta, ThermoTimeProfilePoint,
24};
25pub use problem::interfaces::{
26    AnalysisInterface, AnalysisInterfaceKind, ConjugateHeatTransferInterfaceModel,
27    ContactInterfaceModel, FluidStructureInterfaceModel,
28};
29pub use problem::loads::{LoadCase, LoadKind};
30pub use problem::material_assignment::{EvidenceConfidence, MaterialAssignment};
31pub use problem::materials::{
32    ConductivityFrequencyPoint, MaterialAcousticModel, MaterialElectricalModel,
33    MaterialMechanicalModel, MaterialModel, MaterialPlasticModel, MaterialThermalModel,
34};
35pub use problem::model::{AnalysisModel, AnalysisModelId, ReferenceFrame};
36pub use problem::steps::{AnalysisStep, AnalysisStepKind};
37pub use problem::structure::{
38    BeamElementModel, BeamSectionModel, ShellElementModel, ShellSectionModel, StructuralElement,
39    StructuralElementKind, StructuralModel, StructuralNode,
40};
41pub use validate::{validate_model, validate_model_against_geometry, AnalysisValidationError};
42
43#[cfg(test)]
44mod tests {
45    use runmat_geometry_core::UnitSystem;
46
47    use super::*;
48
49    fn valid_model() -> AnalysisModel {
50        AnalysisModel {
51            model_id: AnalysisModelId("analysis_model_1".to_string()),
52            geometry_id: "geo:model_1".to_string(),
53            geometry_revision: 1,
54            units: UnitSystem::Meter,
55            frame: ReferenceFrame::Global,
56            materials: vec![MaterialModel {
57                material_id: "mat_steel".to_string(),
58                name: "Steel".to_string(),
59                mechanical: MaterialMechanicalModel {
60                    youngs_modulus_pa: 200e9,
61                    poisson_ratio: 0.3,
62                    density_kg_per_m3: 7850.0,
63                },
64                thermal: MaterialThermalModel::default(),
65                acoustic: None,
66                electrical: None,
67                plastic: None,
68            }],
69            material_assignments: Vec::new(),
70            structural: None,
71            thermo_mechanical: None,
72            electro_thermal: None,
73            electromagnetic: None,
74            cfd: None,
75            interfaces: Vec::new(),
76            boundary_conditions: vec![BoundaryCondition {
77                bc_id: "bc_fixed_root".to_string(),
78                region_id: "root".to_string(),
79                kind: BoundaryConditionKind::Fixed,
80            }],
81            loads: vec![LoadCase {
82                load_id: "load_tip".to_string(),
83                region_id: "tip".to_string(),
84                kind: LoadKind::Force {
85                    fx: 0.0,
86                    fy: -1000.0,
87                    fz: 0.0,
88                },
89            }],
90            steps: vec![AnalysisStep {
91                step_id: "step_static".to_string(),
92                kind: AnalysisStepKind::Static,
93            }],
94        }
95    }
96
97    #[test]
98    fn missing_material_bc_load_validation_failures() {
99        let mut model = valid_model();
100        model.materials.clear();
101        assert_eq!(
102            validate_model(&model).expect_err("expected material validation failure"),
103            AnalysisValidationError::MissingMaterials
104        );
105
106        let mut model = valid_model();
107        model.boundary_conditions.clear();
108        assert_eq!(
109            validate_model(&model).expect_err("expected boundary condition validation failure"),
110            AnalysisValidationError::MissingBoundaryConditions
111        );
112
113        let mut model = valid_model();
114        model.loads.clear();
115        assert_eq!(
116            validate_model(&model).expect_err("expected load validation failure"),
117            AnalysisValidationError::MissingLoads
118        );
119    }
120
121    #[test]
122    fn invalid_moment_vectors_fail_validation() {
123        let mut model = valid_model();
124        model.loads[0].kind = LoadKind::Moment {
125            mx: f64::NAN,
126            my: 0.0,
127            mz: 1.0,
128        };
129        assert_eq!(
130            validate_model(&model).expect_err("expected nonfinite moment validation failure"),
131            AnalysisValidationError::InvalidMomentVector {
132                load_id: "load_tip".to_string()
133            }
134        );
135
136        let mut model = valid_model();
137        model.loads[0].kind = LoadKind::Moment {
138            mx: 0.0,
139            my: 0.0,
140            mz: 0.0,
141        };
142        assert_eq!(
143            validate_model(&model).expect_err("expected zero moment validation failure"),
144            AnalysisValidationError::ZeroMomentVector {
145                load_id: "load_tip".to_string()
146            }
147        );
148    }
149
150    #[test]
151    fn unit_frame_mismatch_rejection() {
152        let mut model = valid_model();
153        model.units = UnitSystem::Inch;
154        let err =
155            validate_model_against_geometry(&model, UnitSystem::Meter, &ReferenceFrame::Global)
156                .expect_err("expected unit mismatch");
157        assert!(matches!(
158            err,
159            AnalysisValidationError::UnitMismatch {
160                model: UnitSystem::Inch,
161                geometry: UnitSystem::Meter
162            }
163        ));
164
165        let mut model = valid_model();
166        model.frame = ReferenceFrame::Local("fixture_frame".to_string());
167        let err =
168            validate_model_against_geometry(&model, UnitSystem::Meter, &ReferenceFrame::Global)
169                .expect_err("expected frame mismatch");
170        assert!(matches!(
171            err,
172            AnalysisValidationError::FrameMismatch {
173                model: ReferenceFrame::Local(_),
174                geometry: ReferenceFrame::Global
175            }
176        ));
177    }
178
179    #[test]
180    fn valid_model_is_accepted() {
181        let model = valid_model();
182        validate_model(&model).expect("model should be valid");
183        validate_model_against_geometry(&model, UnitSystem::Meter, &ReferenceFrame::Global)
184            .expect("model should match geometry context");
185    }
186
187    #[test]
188    fn moment_load_kind_serializes_as_snake_case() {
189        let load = LoadCase {
190            load_id: "tip_moment".to_string(),
191            region_id: "tip".to_string(),
192            kind: LoadKind::Moment {
193                mx: 1.0,
194                my: 2.0,
195                mz: 3.0,
196            },
197        };
198
199        let json = serde_json::to_value(&load).expect("load should serialize");
200        assert_eq!(json["kind"]["moment"]["mx"], 1.0);
201        assert_eq!(json["kind"]["moment"]["my"], 2.0);
202        assert_eq!(json["kind"]["moment"]["mz"], 3.0);
203
204        let decoded: LoadCase = serde_json::from_value(json).expect("load should deserialize");
205        assert_eq!(decoded, load);
206    }
207
208    #[test]
209    fn prescribed_rotation_bc_serializes_as_snake_case() {
210        let bc = BoundaryCondition {
211            bc_id: "root_rotation".to_string(),
212            region_id: "root".to_string(),
213            kind: BoundaryConditionKind::PrescribedRotation {
214                rx: 0.0,
215                ry: 0.0,
216                rz: 0.125,
217            },
218        };
219
220        let json = serde_json::to_value(&bc).expect("bc should serialize");
221        assert_eq!(json["kind"]["prescribed_rotation"]["rx"], 0.0);
222        assert_eq!(json["kind"]["prescribed_rotation"]["ry"], 0.0);
223        assert_eq!(json["kind"]["prescribed_rotation"]["rz"], 0.125);
224
225        let decoded: BoundaryCondition =
226            serde_json::from_value(json).expect("bc should deserialize");
227        assert_eq!(decoded, bc);
228    }
229
230    #[test]
231    fn structural_beam_model_round_trips() {
232        let mut model = valid_model();
233        model.structural = Some(StructuralModel {
234            nodes: vec![
235                StructuralNode {
236                    node_id: 1,
237                    coordinates_m: [0.0, 0.0, 0.0],
238                },
239                StructuralNode {
240                    node_id: 2,
241                    coordinates_m: [1.0, 0.0, 0.0],
242                },
243            ],
244            elements: vec![StructuralElement {
245                element_id: "beam_1".to_string(),
246                region_id: "beam_span".to_string(),
247                kind: StructuralElementKind::Beam(BeamElementModel {
248                    node_ids: [1, 2],
249                    section_id: "section_1".to_string(),
250                    reference_axis: [0.0, 0.0, 1.0],
251                }),
252            }],
253            beam_sections: vec![BeamSectionModel {
254                section_id: "section_1".to_string(),
255                area_m2: 2.0e-4,
256                iy_m4: 1.6e-9,
257                iz_m4: 6.4e-9,
258                torsion_j_m4: 2.4e-9,
259                outer_fiber_y_m: 0.01,
260                outer_fiber_z_m: 0.005,
261                torsion_outer_radius_m: 0.011_180_339_887_498_949,
262            }],
263            shell_sections: Vec::new(),
264        });
265
266        let json = serde_json::to_value(&model).expect("model should serialize");
267        assert_eq!(
268            json["structural"]["elements"][0]["kind"]["beam"]["node_ids"][1],
269            2
270        );
271
272        let decoded: AnalysisModel =
273            serde_json::from_value(json).expect("model should deserialize");
274        assert_eq!(decoded, model);
275    }
276
277    #[test]
278    fn structural_shell_model_round_trips() {
279        let mut model = valid_model();
280        model.structural = Some(StructuralModel {
281            nodes: vec![
282                StructuralNode {
283                    node_id: 1,
284                    coordinates_m: [0.0, 0.0, 0.0],
285                },
286                StructuralNode {
287                    node_id: 2,
288                    coordinates_m: [1.0, 0.0, 0.0],
289                },
290                StructuralNode {
291                    node_id: 3,
292                    coordinates_m: [0.0, 1.0, 0.0],
293                },
294            ],
295            elements: vec![StructuralElement {
296                element_id: "shell_1".to_string(),
297                region_id: "shell_panel".to_string(),
298                kind: StructuralElementKind::Shell(ShellElementModel {
299                    node_ids: [1, 2, 3],
300                    section_id: "panel_2mm".to_string(),
301                    reference_axis: [1.0, 0.0, 0.0],
302                }),
303            }],
304            beam_sections: Vec::new(),
305            shell_sections: vec![ShellSectionModel {
306                section_id: "panel_2mm".to_string(),
307                thickness_m: 0.002,
308                shear_correction: 5.0 / 6.0,
309                drilling_stiffness_scale: 1.0e-4,
310            }],
311        });
312
313        let json = serde_json::to_value(&model).expect("model should serialize");
314        assert_eq!(
315            json["structural"]["elements"][0]["kind"]["shell"]["node_ids"][2],
316            3
317        );
318
319        let decoded: AnalysisModel =
320            serde_json::from_value(json).expect("model should deserialize");
321        assert_eq!(decoded, model);
322    }
323
324    #[test]
325    fn electrical_model_defaults_frequency_response() {
326        let electrical = MaterialElectricalModel::default();
327        assert!(electrical.conductivity_frequency_response.is_empty());
328    }
329}