Skip to main content

qualia_core_db/specialized_libs/physics_simulation/
results.rs

1use super::*;
2
3/// Physics simulation result
4#[derive(Debug, Clone)]
5pub struct PhysicsSimulationResult<T> {
6    pub result: T,
7    pub simulation_time: u64,
8    pub solver_time: u64,
9    pub data_time: u64,
10    pub convergence_info: ConvergenceInfo,
11    pub performance_info: PerformanceInfo,
12}
13
14/// Convergence information
15#[derive(Debug, Clone)]
16pub struct ConvergenceInfo {
17    pub converged: bool,
18    pub iterations: u32,
19    pub residual_norm: f64,
20    pub convergence_rate: f64,
21    pub final_error: f64,
22}
23
24/// Performance information
25#[derive(Debug, Clone)]
26pub struct PerformanceInfo {
27    pub cpu_utilization: f64,
28    pub memory_utilization: f64,
29    pub network_utilization: f64,
30    pub io_utilization: f64,
31    pub parallel_efficiency: f64,
32}
33
34// ============================================================================
35// Genuine simulations for the declared `SimulationType` domains.
36//
37// Every method below marshals an initial state into slices and hands the actual
38// time-integration / eigenproblem to a tested solver in `crate::solvers`:
39//   * `integrate_dopri5`     — adaptive Dormand–Prince RK45 (vector ODE systems)
40//   * `integrate_symplectic` — Störmer–Verlet / Ruth / Yoshida (separable Hamiltonians)
41//   * `symmetric_eigen`      — cyclic-Jacobi symmetric eigensolver
42// The physics (forces, Laplacians, Hamiltonians) is set up here; the numerics are not.
43// ============================================================================
44
45/// Trajectory + landing diagnostics for `run_projectile_motion` (ParticlePhysics).
46#[derive(Debug, Clone)]
47pub struct ProjectileResult {
48    /// Sampled `[t, x, y, vx, vy]` rows along the flight.
49    pub trajectory: Vec<[f64; 5]>,
50    /// Horizontal distance at ground return (interpolated y=0). No-drag: v0²·sin(2θ)/g.
51    pub range: f64,
52    /// Peak height reached.
53    pub max_height: f64,
54    /// Time of flight to ground return (interpolated).
55    pub time_of_flight: f64,
56    /// Whether the projectile returned to y=0 within `max_time`.
57    pub landed: bool,
58    pub steps_accepted: u32,
59    pub steps_rejected: u32,
60}
61
62/// Result of `run_harmonic_oscillator` (StructuralDynamics / spring–mass), symplectic.
63#[derive(Debug, Clone)]
64pub struct OscillatorResult {
65    pub times: Vec<f64>,
66    pub positions: Vec<f64>,
67    pub velocities: Vec<f64>,
68    /// Analytic period 2π·√(m/k).
69    pub analytic_period: f64,
70    /// Period measured from the integrated trajectory (mean crossing interval).
71    pub measured_period: f64,
72    pub energy_initial: f64,
73    pub energy_final: f64,
74    /// Max |E−E₀| over the run — the bounded symplectic energy drift.
75    pub max_energy_drift: f64,
76}
77
78/// Result of `run_pendulum` (nonlinear rigid-body dynamics).
79#[derive(Debug, Clone)]
80pub struct PendulumResult {
81    pub times: Vec<f64>,
82    pub angles: Vec<f64>,
83    pub angular_velocities: Vec<f64>,
84    /// Small-angle period 2π·√(L/g).
85    pub small_angle_period: f64,
86    pub measured_period: f64,
87    pub energy_initial: f64,
88    pub energy_final: f64,
89    pub steps_accepted: u32,
90    pub steps_rejected: u32,
91}
92
93/// Result of `run_nbody_gravitation` (Astrophysics), direct-sum Newtonian gravity.
94#[derive(Debug, Clone)]
95pub struct NBodyResult {
96    pub num_bodies: usize,
97    pub times: Vec<f64>,
98    /// Position snapshots; each is the flat `[x0,y0,x1,y1,…]` vector at that time.
99    pub position_snapshots: Vec<Vec<f64>>,
100    pub final_positions: Vec<f64>,
101    pub final_velocities: Vec<f64>,
102    pub energy_initial: f64,
103    pub energy_final: f64,
104    /// |E_final − E_initial| / |E_initial|.
105    pub energy_drift_rel: f64,
106    pub angular_momentum_initial: f64,
107    pub angular_momentum_final: f64,
108    pub steps_accepted: u32,
109    pub steps_rejected: u32,
110}
111
112/// Result of `run_heat_diffusion_1d` (HeatTransfer), insulated (Neumann) ends.
113#[derive(Debug, Clone)]
114pub struct HeatDiffusionResult {
115    pub times: Vec<f64>,
116    pub snapshots: Vec<Vec<f64>>,
117    pub final_temperature: Vec<f64>,
118    pub initial_mean: f64,
119    pub final_mean: f64,
120    /// max_i |u_i − mean| in the final field (→ 0 as the profile relaxes).
121    pub max_deviation_from_mean: f64,
122    pub steps_accepted: u32,
123    pub steps_rejected: u32,
124}
125
126/// Result of `run_wave_equation_1d` (CEM — 1D scalar wave / plane-wave field), fixed ends.
127#[derive(Debug, Clone)]
128pub struct WaveResult {
129    pub times: Vec<f64>,
130    /// Displacement (field) snapshots.
131    pub snapshots: Vec<Vec<f64>>,
132    pub final_displacement: Vec<f64>,
133    pub energy_initial: f64,
134    pub energy_final: f64,
135    pub steps_accepted: u32,
136    pub steps_rejected: u32,
137}
138
139/// Result of `run_molecular_dynamics` (MolecularDynamics), Lennard-Jones in 2D.
140#[derive(Debug, Clone)]
141pub struct MolecularDynamicsResult {
142    pub num_particles: usize,
143    pub times: Vec<f64>,
144    pub final_positions: Vec<f64>,
145    pub final_velocities: Vec<f64>,
146    pub energy_initial: f64,
147    pub energy_final: f64,
148    pub energy_drift_rel: f64,
149    /// Instantaneous kinetic temperature (reduced units, kB=1): 2·KE/dof.
150    pub temperature: f64,
151    pub steps_accepted: u32,
152    pub steps_rejected: u32,
153}
154
155/// Result of `run_quantum_stationary_states_1d` (QuantumMechanics), finite-difference
156/// time-independent Schrödinger eigenproblem solved by `symmetric_eigen`.
157#[derive(Debug, Clone)]
158pub struct QuantumSpectrumResult {
159    /// Lowest `num_levels` energy eigenvalues, ascending.
160    pub eigenvalues: Vec<f64>,
161    pub num_grid_points: usize,
162    pub dx: f64,
163}
164
165/// Result of `run_logistic_growth` (Biophysics — population dynamics).
166#[derive(Debug, Clone)]
167pub struct PopulationDynamicsResult {
168    pub times: Vec<f64>,
169    pub population: Vec<f64>,
170    pub carrying_capacity: f64,
171    pub growth_rate: f64,
172    pub steps_accepted: u32,
173    pub steps_rejected: u32,
174}
175
176/// Result of `run_advection_diffusion_1d` (MultiPhysics — coupled transport + diffusion).
177#[derive(Debug, Clone)]
178pub struct AdvectionDiffusionResult {
179    pub times: Vec<f64>,
180    pub snapshots: Vec<Vec<f64>>,
181    pub final_field: Vec<f64>,
182    pub advection_velocity: f64,
183    pub diffusion_coeff: f64,
184    /// Σ_i u_i·dx at t=0 and t=end (conserved under the periodic scheme).
185    pub initial_total: f64,
186    pub final_total: f64,
187    pub steps_accepted: u32,
188    pub steps_rejected: u32,
189}
190
191/// Simulation result
192#[derive(Debug, Clone)]
193pub struct SimulationResult {
194    pub node_id: String,
195    pub fields: Vec<PhysicsField>,
196    pub convergence_info: ConvergenceInfo,
197    pub performance_info: PerformanceInfo,
198}