Skip to main content

euv_core/game/fluid/sim/
struct.rs

1use super::*;
2
3/// Declarative physical configuration for `LiquidFluidSolver` and `GyroLiquidGameScene`.
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct GyroLiquidConfig {
6    /// Physical glass vessel interior width in SI meters (default `0.12 m`).
7    #[get(type(copy))]
8    pub(crate) vessel_width_m: f32,
9    /// Physical glass vessel interior height in SI meters (default `0.09 m`).
10    #[get(type(copy))]
11    pub(crate) vessel_height_m: f32,
12    /// Fraction of vessel area filled with liquid water (`0.20..=0.85`).
13    #[get(type(copy))]
14    pub(crate) fill_ratio: f32,
15    /// Relative viscosity multiplier (`1.0` = pure water at 20 °C; range `0.2..=6.0`).
16    #[get(type(copy))]
17    pub(crate) viscosity_scale: f32,
18    /// Relative surface tension multiplier (`1.0` = pure water `0.0728 N/m`; range `0.0..=3.0`).
19    #[get(type(copy))]
20    pub(crate) surface_tension_scale: f32,
21    /// Whether Archimedes buoyant floating objects (orb + ice cube) are active.
22    #[get(type(copy))]
23    pub(crate) buoyant_bodies_enabled: bool,
24    /// Number of PBF substeps per fixed simulation tick (`2..=4`).
25    #[get(type(copy))]
26    pub(crate) substeps: u32,
27    /// Number of Jacobi PBF density constraint iterations per substep (`2..=6`).
28    #[get(type(copy))]
29    pub(crate) solver_iterations: u32,
30    /// Target particle count at `fill_ratio == 0.50` (`240..=FLUID_MAX_PARTICLES`).
31    #[get(type(copy))]
32    pub(crate) base_particle_count: usize,
33}
34
35/// Interactive buoyant rigid body experiencing real Archimedes buoyancy, hydrodynamic drag, and wave torque.
36#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
37pub struct BuoyantBody {
38    /// Visual and physical classification (`FloatingOrb` or `IceCube`).
39    #[get(type(copy))]
40    pub(crate) kind: BuoyantBodyKind,
41    /// Center x-coordinate inside the vessel in SI meters.
42    #[get(type(copy))]
43    pub(crate) x: f32,
44    /// Center y-coordinate inside the vessel in SI meters.
45    #[get(type(copy))]
46    pub(crate) y: f32,
47    /// Linear x-velocity in `m/s`.
48    #[get(type(copy))]
49    pub(crate) vx: f32,
50    /// Linear y-velocity in `m/s`.
51    #[get(type(copy))]
52    pub(crate) vy: f32,
53    /// Orientation angle in radians.
54    #[get(type(copy))]
55    pub(crate) angle: f32,
56    /// Angular velocity in `rad/s`.
57    #[get(type(copy))]
58    pub(crate) omega: f32,
59    /// Characteristic radius (half-diagonal / sphere radius) in SI meters.
60    #[get(type(copy))]
61    pub(crate) radius: f32,
62    /// Material mass density in `kg/m³` (`< 1000.0` floats in water).
63    #[get(type(copy))]
64    pub(crate) density_kgm3: f32,
65    /// Live submerged volume fraction in `[0.0, 1.0]` evaluated from the fluid density field.
66    #[get(type(copy))]
67    pub(crate) submerged_fraction: f32,
68}
69
70/// Entrained aerated foam or rising micro-bubble sample captured for optical rendering.
71#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
72pub struct FluidBubble {
73    /// Bubble x-coordinate inside the vessel in SI meters.
74    #[get(type(copy))]
75    pub(crate) x: f32,
76    /// Bubble y-coordinate inside the vessel in SI meters.
77    #[get(type(copy))]
78    pub(crate) y: f32,
79    /// Bubble x-velocity in `m/s`.
80    #[get(type(copy))]
81    pub(crate) vx: f32,
82    /// Bubble y-velocity in `m/s`.
83    #[get(type(copy))]
84    pub(crate) vy: f32,
85    /// Bubble radius in SI meters.
86    #[get(type(copy))]
87    pub(crate) radius: f32,
88    /// Elapsed age in seconds.
89    #[get(type(copy))]
90    pub(crate) age: f32,
91    /// Maximum lifetime in seconds.
92    #[get(type(copy))]
93    pub(crate) lifetime: f32,
94    /// Surface foam intensity factor in `[0.0, 1.0]` (`1.0` = bright crest foam, `0.0` = crisp rising underwater micro-bubble).
95    #[get(type(copy))]
96    pub(crate) foam_factor: f32,
97}
98
99/// Read-only live physics, optical rendering, and sensor telemetry emitted by `GyroLiquidGameScene`.
100#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
101pub struct GyroLiquidStats {
102    /// Active PBF liquid particle count.
103    #[get(type(copy))]
104    pub(crate) particle_count: usize,
105    /// Active entrained foam and rising micro-bubble count.
106    #[get(type(copy))]
107    pub(crate) bubble_count: usize,
108    /// Active floating buoyant body count.
109    #[get(type(copy))]
110    pub(crate) buoyant_body_count: usize,
111    /// Total fluid kinetic energy in millijoules per meter tank depth (`mJ/m`).
112    #[get(type(copy))]
113    pub(crate) kinetic_energy_mj: f32,
114    /// Active screen-aligned gravity x-component in `m/s²`.
115    #[get(type(copy))]
116    pub(crate) gravity_x: f32,
117    /// Active screen-aligned gravity y-component in `m/s²`.
118    #[get(type(copy))]
119    pub(crate) gravity_y: f32,
120    /// Active screen-aligned gravity z-component in `m/s²`.
121    #[get(type(copy))]
122    pub(crate) gravity_z: f32,
123    /// Current device/container roll tilt angle in degrees (`0.0` = upright, `+90.0` = clockwise right-down).
124    #[get(type(copy))]
125    pub(crate) tilt_angle_deg: f32,
126    /// Peak wave free-surface elevation amplitude relative to hydrostatic mean in meters.
127    #[get(type(copy))]
128    pub(crate) peak_wave_height_m: f32,
129    /// Last fixed-update simulation time in milliseconds.
130    #[get(type(copy))]
131    pub(crate) sim_time_ms: f32,
132    /// Last optical liquid frame render time in milliseconds.
133    #[get(type(copy))]
134    pub(crate) render_time_ms: f32,
135    /// Active sensor driving mode (`Auto`, `LiveHardware`, or `VirtualTilt`).
136    #[get(type(copy))]
137    pub(crate) sensor_mode: FluidSensorMode,
138    /// Whether live hardware motion sensor data is actively driving the simulation.
139    #[get(type(copy))]
140    pub(crate) sensor_active: bool,
141    /// Current physical vessel width in SI meters.
142    #[get(type(copy))]
143    pub(crate) vessel_width_m: f32,
144    /// Current physical vessel height in SI meters.
145    #[get(type(copy))]
146    pub(crate) vessel_height_m: f32,
147    /// Current liquid fill ratio (`0.20..=0.85`).
148    #[get(type(copy))]
149    pub(crate) fill_ratio: f32,
150    /// Last optical framebuffer width in pixels.
151    #[get(type(copy))]
152    pub(crate) render_width: u32,
153    /// Last optical framebuffer height in pixels.
154    #[get(type(copy))]
155    pub(crate) render_height: u32,
156}
157
158/// Zero-allocation double-buffered simulation snapshot consumed by `LiquidOpticalRenderer`.
159#[derive(Clone, Data, Debug)]
160pub struct FluidSimSnapshot {
161    /// Physical vessel width in SI meters at snapshot capture time.
162    #[get(type(copy))]
163    pub(crate) vessel_width_m: f32,
164    /// Physical vessel height in SI meters at snapshot capture time.
165    #[get(type(copy))]
166    pub(crate) vessel_height_m: f32,
167    /// Smoothing kernel radius `h` in SI meters.
168    #[get(type(copy))]
169    pub(crate) kernel_radius_m: f32,
170    /// Active fluid particle count (`<= pos_x.len()`).
171    #[get(type(copy))]
172    pub(crate) particle_count: usize,
173    /// Active bubble/foam count (`<= bubbles.len()`).
174    #[get(type(copy))]
175    pub(crate) bubble_count: usize,
176    /// Active buoyant body count (`<= buoyant_bodies.len()`).
177    #[get(type(copy))]
178    pub(crate) buoyant_body_count: usize,
179    /// Active screen-aligned gravity x-component in `m/s²`.
180    #[get(type(copy))]
181    pub(crate) gravity_x: f32,
182    /// Active screen-aligned gravity y-component in `m/s²`.
183    #[get(type(copy))]
184    pub(crate) gravity_y: f32,
185    /// Active screen-aligned gravity z-component in `m/s²`.
186    #[get(type(copy))]
187    pub(crate) gravity_z: f32,
188    /// Monotonic simulation time in seconds for subtle caustic shimmer continuity.
189    #[get(type(copy))]
190    pub(crate) sim_time_secs: f32,
191    /// Pre-allocated particle x-coordinates in SI meters.
192    pub(crate) pos_x: Vec<f32>,
193    /// Pre-allocated particle y-coordinates in SI meters.
194    pub(crate) pos_y: Vec<f32>,
195    /// Pre-allocated particle x-velocities in `m/s`.
196    pub(crate) vel_x: Vec<f32>,
197    /// Pre-allocated particle y-velocities in `m/s`.
198    pub(crate) vel_y: Vec<f32>,
199    /// Pre-allocated particle normalized density ratios (`ρ / ρ₀`).
200    pub(crate) density_ratio: Vec<f32>,
201    /// Pre-allocated active buoyant bodies.
202    pub(crate) buoyant_bodies: Vec<BuoyantBody>,
203    /// Pre-allocated active bubbles and foam particles.
204    pub(crate) bubbles: Vec<FluidBubble>,
205}
206
207/// Real-time SI Position-Based Fluids (PBF) 2D liquid engine with non-inertial rotating-device dynamics, Archimedes buoyancy, and zero steady-state heap allocations.
208#[derive(Clone, Data, Debug)]
209pub struct LiquidFluidSolver {
210    /// Active physical simulation configuration.
211    #[get(type(copy))]
212    pub(crate) config: GyroLiquidConfig,
213    /// Active PBF particle count (`<= FLUID_MAX_PARTICLES`).
214    #[get(type(copy))]
215    pub(crate) particle_count: usize,
216    /// Nominal resting particle spacing `d0` in meters.
217    #[get(type(copy))]
218    pub(crate) rest_spacing_m: f32,
219    /// Particle area `A_p = d0²` in `m²`.
220    #[get(type(copy))]
221    pub(crate) particle_area_m2: f32,
222    /// Particle 2D mass `m_p = ρ₀ * A_p` in `kg/m`.
223    #[get(type(copy))]
224    pub(crate) particle_mass_kgpm: f32,
225    /// Smoothing kernel radius `h` in meters.
226    #[get(type(copy))]
227    pub(crate) kernel_radius_m: f32,
228    /// Precomputed 2D Poly6 normalization factor `4 / (π h⁸)`.
229    #[get(type(copy))]
230    pub(crate) poly6_coeff: f32,
231    /// Precomputed 2D Spiky gradient normalization factor `-30 / (π h⁵)`.
232    #[get(type(copy))]
233    pub(crate) spiky_grad_coeff: f32,
234    /// Precomputed reference Poly6 weight `W_poly6(Δq, h)` for tensile instability `s_corr`.
235    #[get(type(copy))]
236    pub(crate) w_dq: f32,
237    /// Current screen-aligned gravity `(gx, gy, gz)` in `m/s²`.
238    #[get(type(copy))]
239    pub(crate) gravity_x: f32,
240    /// Current screen-aligned gravity y-component in `m/s²`.
241    #[get(type(copy))]
242    pub(crate) gravity_y: f32,
243    /// Current screen-aligned gravity z-component in `m/s²`.
244    #[get(type(copy))]
245    pub(crate) gravity_z: f32,
246    /// Active linear frame acceleration x-component in `m/s²`.
247    #[get(type(copy))]
248    pub(crate) linear_accel_x: f32,
249    /// Active linear frame acceleration y-component in `m/s²`.
250    #[get(type(copy))]
251    pub(crate) linear_accel_y: f32,
252    /// Decaying synthetic shake acceleration x-component in `m/s²`.
253    #[get(type(copy))]
254    pub(crate) shake_accel_x: f32,
255    /// Decaying synthetic shake acceleration y-component in `m/s²`.
256    #[get(type(copy))]
257    pub(crate) shake_accel_y: f32,
258    /// Clockwise container angular velocity `Ω = -gyro_z` in `rad/s`.
259    #[get(type(copy))]
260    pub(crate) angular_vel_cw: f32,
261    /// Previous step clockwise angular velocity for Euler angular acceleration `α = dΩ/dt`.
262    #[get(type(copy))]
263    pub(crate) prev_angular_vel_cw: f32,
264    /// Smoothed clockwise angular acceleration `α` in `rad/s²`.
265    #[get(type(copy))]
266    pub(crate) angular_accel_cw: f32,
267    /// Virtual desktop/simulator roll angle in radians (`0` = upright, `+π/2` = rotated CW right-down).
268    #[get(type(copy))]
269    pub(crate) virtual_roll_rad: f32,
270    /// Virtual desktop/simulator pitch angle in radians (`0` = upright, `+π/2` = lying flat face-up).
271    #[get(type(copy))]
272    pub(crate) virtual_pitch_rad: f32,
273    /// Previous virtual roll angle for deriving virtual rotational velocity `Ω`.
274    #[get(type(copy))]
275    pub(crate) prev_virtual_roll_rad: f32,
276    /// Active container roll tilt angle in radians (derived from active gravity).
277    #[get(type(copy))]
278    pub(crate) active_roll_rad: f32,
279    /// Elapsed simulation time in seconds.
280    #[get(type(copy))]
281    pub(crate) sim_time_secs: f32,
282    /// Deterministic PRNG state word for bubble emission.
283    #[get(type(copy))]
284    pub(crate) rng_state: u64,
285    /// Cached kinetic energy in `mJ/m`.
286    #[get(type(copy))]
287    pub(crate) kinetic_energy_mj: f32,
288    /// Cached peak wave amplitude relative to resting hydrostatic level in meters.
289    #[get(type(copy))]
290    pub(crate) peak_wave_height_m: f32,
291    /// Active spatial hash grid width in cells.
292    #[get(type(copy))]
293    pub(crate) grid_w: usize,
294    /// Active spatial hash grid height in cells.
295    #[get(type(copy))]
296    pub(crate) grid_h: usize,
297    /// Flat SoA particle x-positions in meters.
298    pub(crate) pos_x: Vec<f32>,
299    /// Flat SoA particle y-positions in meters.
300    pub(crate) pos_y: Vec<f32>,
301    /// Flat SoA predicted x-positions during PBF substeps.
302    pub(crate) pred_x: Vec<f32>,
303    /// Flat SoA predicted y-positions during PBF substeps.
304    pub(crate) pred_y: Vec<f32>,
305    /// Flat SoA particle x-velocities in `m/s`.
306    pub(crate) vel_x: Vec<f32>,
307    /// Flat SoA particle y-velocities in `m/s`.
308    pub(crate) vel_y: Vec<f32>,
309    /// Flat SoA PBF Lagrange multipliers `λ_i`.
310    pub(crate) lambda: Vec<f32>,
311    /// Flat SoA evaluated fluid densities `ρ_i` in `kg/m³`.
312    pub(crate) density: Vec<f32>,
313    /// Flat SoA 2D scalar vorticity `ω_i` in `s⁻¹`.
314    pub(crate) vorticity: Vec<f32>,
315    /// Flat SoA PBF position delta x scratch buffer.
316    pub(crate) corr_x: Vec<f32>,
317    /// Flat SoA PBF position delta y scratch buffer.
318    pub(crate) corr_y: Vec<f32>,
319    /// Flat SoA XSPH / vorticity velocity delta x scratch buffer.
320    pub(crate) dvx_scratch: Vec<f32>,
321    /// Flat SoA XSPH / vorticity velocity delta y scratch buffer.
322    pub(crate) dvy_scratch: Vec<f32>,
323    /// Pre-allocated spatial hash cell particle counts.
324    pub(crate) cell_counts: Vec<u32>,
325    /// Pre-allocated spatial hash cell CSR start offsets.
326    pub(crate) cell_starts: Vec<u32>,
327    /// Pre-allocated spatial hash cell write cursors.
328    pub(crate) cell_cursors: Vec<u32>,
329    /// Pre-allocated particle-to-cell index map.
330    pub(crate) particle_cells: Vec<u32>,
331    /// Pre-allocated counting-sorted particle indices grouped by spatial hash cell.
332    pub(crate) sorted_indices: Vec<u32>,
333    /// Flat pre-allocated per-particle neighbor index table (`FLUID_MAX_PARTICLES * FLUID_MAX_NEIGHBORS`).
334    pub(crate) neighbor_indices: Vec<u16>,
335    /// Flat pre-allocated per-particle cached neighbor counts (`<= FLUID_MAX_NEIGHBORS`).
336    pub(crate) neighbor_counts: Vec<u8>,
337    /// Pre-allocated buoyant floating rigid bodies.
338    pub(crate) buoyant_bodies: Vec<BuoyantBody>,
339    /// Active buoyant body count.
340    #[get(type(copy))]
341    pub(crate) buoyant_body_count: usize,
342    /// Pre-allocated entrained micro-bubble & foam pool.
343    pub(crate) bubbles: Vec<FluidBubble>,
344    /// Active bubble/foam count in prefix `[0..bubble_count]`.
345    #[get(type(copy))]
346    pub(crate) bubble_count: usize,
347}