Skip to main content

qualia_core_db/specialized_libs/engineering_analysis/
fluid.rs

1use super::*;
2
3/// Fluid analyzer for fluid dynamics analysis
4pub struct FluidAnalyzer {
5    computational_fluid_dynamics: ComputationalFluidDynamics,
6    pipe_flow: PipeFlow,
7    open_channel_flow: OpenChannelFlow,
8}
9
10/// Computational fluid dynamics
11pub struct ComputationalFluidDynamics {
12    navier_stokes_solver: NavierStokesSolver,
13    turbulence_modeling: TurbulenceModeling,
14    mesh_generator: CFDMeshGenerator,
15}
16
17/// Navier-Stokes solver
18#[derive(Debug, Clone)]
19pub struct NavierStokesSolver {
20    pub solver_type: NSSolverType,
21    pub discretization_scheme: DiscretizationScheme,
22}
23
24/// NS solver types
25#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26pub enum NSSolverType {
27    FiniteVolume,
28    FiniteElement,
29    Spectral,
30    LatticeBoltzmann,
31}
32
33/// Discretization schemes
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
35pub enum DiscretizationScheme {
36    Upwind,
37    Central,
38    HighResolution,
39    TVD,
40}
41
42/// Turbulence modeling
43#[derive(Debug, Clone)]
44pub struct TurbulenceModeling {
45    pub turbulence_model: TurbulenceModel,
46    pub model_parameters: TurbulenceParameters,
47}
48
49/// Turbulence models
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub enum TurbulenceModel {
52    RANS,
53    LES,
54    DNS,
55    Hybrid,
56}
57
58/// Turbulence parameters
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct TurbulenceParameters {
61    pub reynolds_number: f64,
62    pub turbulence_intensity: f64,
63    pub length_scale: f64,
64}
65
66/// CFD mesh generator
67#[derive(Debug, Clone)]
68pub struct CFDMeshGenerator {
69    pub mesh_type: MeshType,
70    pub mesh_refinement: MeshRefinement,
71}
72
73/// Mesh refinement
74#[derive(Debug, Clone)]
75pub struct MeshRefinement {
76    pub refinement_criteria: Vec<RefinementCriterion>,
77    pub refinement_levels: Vec<u32>,
78}
79
80/// Refinement criteria
81#[derive(Debug, Clone)]
82pub struct RefinementCriterion {
83    pub criterion_name: String,
84    pub threshold_value: f64,
85}
86
87/// Pipe flow
88#[derive(Debug, Clone)]
89pub struct PipeFlow {
90    pub pipe_geometry: PipeGeometry,
91    pub flow_regime: FlowRegime,
92    pub pressure_drop: f64,
93}
94
95/// Pipe geometry
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct PipeGeometry {
98    pub diameter: f64,
99    pub length: f64,
100    pub roughness: f64,
101}
102
103/// Flow regimes
104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
105pub enum FlowRegime {
106    Laminar,
107    Turbulent,
108    Transitional,
109}
110
111/// Open channel flow
112#[derive(Debug, Clone)]
113pub struct OpenChannelFlow {
114    pub channel_geometry: ChannelGeometry,
115    pub flow_type: FlowType,
116    pub hydraulic_radius: f64,
117}
118
119/// Channel geometry
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct ChannelGeometry {
122    pub cross_section: CrossSection,
123    pub slope: f64,
124    pub manning_coefficient: f64,
125}
126
127/// Cross sections
128#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
129pub enum CrossSection {
130    Rectangular,
131    Trapezoidal,
132    Circular,
133    Triangular,
134}
135
136/// Flow types
137#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
138pub enum FlowType {
139    Subcritical,
140    Critical,
141    Supercritical,
142}
143impl FluidAnalyzer {
144    /// Creates an analyzer with default CFD and pipe/channel flow state.
145    pub fn new() -> Self {
146        Self {
147            computational_fluid_dynamics: ComputationalFluidDynamics::new(),
148            pipe_flow: PipeFlow::new(),
149            open_channel_flow: OpenChannelFlow::new(),
150        }
151    }
152
153    /// Initializes CFD sub-components; `EngineeringError` propagates.
154    pub fn initialize(&mut self) -> Result<(), EngineeringError> {
155        self.computational_fluid_dynamics.initialize()?;
156        Ok(())
157    }
158
159    /// Borrow the pipe-flow sub-component.
160    pub fn pipe_flow(&self) -> &PipeFlow {
161        &self.pipe_flow
162    }
163
164    /// Mutably borrow the pipe-flow sub-component.
165    pub fn pipe_flow_mut(&mut self) -> &mut PipeFlow {
166        &mut self.pipe_flow
167    }
168
169    /// Borrow the open-channel-flow sub-component.
170    pub fn open_channel_flow(&self) -> &OpenChannelFlow {
171        &self.open_channel_flow
172    }
173
174    /// Mutably borrow the open-channel-flow sub-component.
175    pub fn open_channel_flow_mut(&mut self) -> &mut OpenChannelFlow {
176        &mut self.open_channel_flow
177    }
178
179    /// Fails closed (`ValidationError`) when the model has no dimensions.
180    pub fn validate_model(&self, model: &EngineeringModel) -> Result<(), EngineeringError> {
181        if model.geometry.dimensions.is_empty() {
182            return Err(EngineeringError::ValidationError(
183                "Model must have dimensions".to_string(),
184            ));
185        }
186        Ok(())
187    }
188
189    /// Runs the real 2-D incompressible Navier–Stokes LBM/D2Q9 solver (`cfd::run_cfd`, 32×32 lid-driven-cavity defaults) and converts the solution to `AnalysisResults`. Drive `cfd::run_cfd` directly for other BCs/configs.
190    pub fn analyze(
191        &mut self,
192        model: &EngineeringModel,
193        analysis_type: AnalysisType,
194    ) -> Result<AnalysisResults, EngineeringError> {
195        // Runs the real 2-D incompressible Navier–Stokes solver in `cfd.rs`
196        // (LBM/D2Q9, Chorin-consistent) over the model's [Lx, Ly] domain. This
197        // used to return NotImplemented even though `cfd::run_cfd` was fully
198        // built and tested — the solver was disconnected from its own entry
199        // point. Defaults are the lid-driven cavity (`CfdBc::default`) at the
200        // library-default Reynolds number on a bounded 32×32 grid; a caller
201        // needing other physics can drive `cfd::run_cfd` directly with its own
202        // boundary conditions / solver config.
203        self.validate_model(model)?;
204        let bc = cfd::CfdBc::default();
205        let cfg = cfd::SolverConfig::default();
206        let solution = cfd::run_cfd(model, bc, cfg, 32, 32)?;
207        Ok(cfd::cfd_to_analysis_results(
208            &solution,
209            model,
210            analysis_type,
211        ))
212    }
213}
214
215impl ComputationalFluidDynamics {
216    /// Creates CFD state with default Navier–Stokes solver, turbulence model, and mesh generator.
217    pub fn new() -> Self {
218        Self {
219            navier_stokes_solver: NavierStokesSolver::new(),
220            turbulence_modeling: TurbulenceModeling::new(),
221            mesh_generator: CFDMeshGenerator::new(),
222        }
223    }
224
225    /// No-op returning `Ok(())` — in-memory state needs no setup.
226    pub fn initialize(&mut self) -> Result<(), EngineeringError> {
227        Ok(())
228    }
229
230    /// Borrow the Navier–Stokes solver configuration.
231    pub fn navier_stokes_solver(&self) -> &NavierStokesSolver {
232        &self.navier_stokes_solver
233    }
234
235    /// Mutably borrow the Navier–Stokes solver configuration.
236    pub fn navier_stokes_solver_mut(&mut self) -> &mut NavierStokesSolver {
237        &mut self.navier_stokes_solver
238    }
239
240    /// Borrow the turbulence-modeling configuration.
241    pub fn turbulence_modeling(&self) -> &TurbulenceModeling {
242        &self.turbulence_modeling
243    }
244
245    /// Mutably borrow the turbulence-modeling configuration.
246    pub fn turbulence_modeling_mut(&mut self) -> &mut TurbulenceModeling {
247        &mut self.turbulence_modeling
248    }
249
250    /// Borrow the CFD mesh generator.
251    pub fn mesh_generator(&self) -> &CFDMeshGenerator {
252        &self.mesh_generator
253    }
254
255    /// Mutably borrow the CFD mesh generator.
256    pub fn mesh_generator_mut(&mut self) -> &mut CFDMeshGenerator {
257        &mut self.mesh_generator
258    }
259}
260
261impl NavierStokesSolver {
262    /// Creates a default `NavierStokesSolver` state descriptor.
263    pub fn new() -> Self {
264        Self {
265            solver_type: NSSolverType::FiniteVolume,
266            discretization_scheme: DiscretizationScheme::Upwind,
267        }
268    }
269}
270
271impl TurbulenceModeling {
272    /// Creates default turbulence-modeling state.
273    pub fn new() -> Self {
274        Self {
275            turbulence_model: TurbulenceModel::RANS,
276            model_parameters: TurbulenceParameters::new(),
277        }
278    }
279}
280
281impl TurbulenceParameters {
282    /// Creates default `TurbulenceParameters`.
283    pub fn new() -> Self {
284        Self {
285            reynolds_number: 10000.0,
286            turbulence_intensity: 0.05,
287            length_scale: 1.0,
288        }
289    }
290}
291
292impl CFDMeshGenerator {
293    /// Creates a mesh generator with default refinement state.
294    pub fn new() -> Self {
295        Self {
296            mesh_type: MeshType::Unstructured,
297            mesh_refinement: MeshRefinement::new(),
298        }
299    }
300}
301
302impl MeshRefinement {
303    /// Creates a default `MeshRefinement` descriptor.
304    pub fn new() -> Self {
305        Self {
306            refinement_criteria: Vec::new(),
307            refinement_levels: vec![1, 2, 3],
308        }
309    }
310}
311
312impl PipeFlow {
313    /// Creates a pipe-flow model with default geometry.
314    pub fn new() -> Self {
315        Self {
316            pipe_geometry: PipeGeometry::new(),
317            flow_regime: FlowRegime::Laminar,
318            pressure_drop: 0.0,
319        }
320    }
321}
322
323impl PipeGeometry {
324    /// Creates a default `PipeGeometry`.
325    pub fn new() -> Self {
326        Self {
327            diameter: 0.1,
328            length: 10.0,
329            roughness: 0.0001,
330        }
331    }
332}
333
334impl OpenChannelFlow {
335    /// Creates an open-channel-flow model with default geometry.
336    pub fn new() -> Self {
337        Self {
338            channel_geometry: ChannelGeometry::new(),
339            flow_type: FlowType::Subcritical,
340            hydraulic_radius: 0.05,
341        }
342    }
343}
344
345impl ChannelGeometry {
346    /// Creates a default `ChannelGeometry`.
347    pub fn new() -> Self {
348        Self {
349            cross_section: CrossSection::Rectangular,
350            slope: 0.001,
351            manning_coefficient: 0.025,
352        }
353    }
354}