Skip to main content

euv_core/game/fluid_kit/eulerian/
struct.rs

1use super::*;
2
3/// Declarative physical and numerical configuration for `MacFluidGrid2D` and `MacFluidGrid3D`.
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct MacFluidConfig {
6    /// Uniform physical cell spacing `dx` in meters.
7    #[get(type(copy))]
8    pub(crate) cell_size_m: f32,
9    /// Velocity/scalar advection scheme (`SemiLagrangian` or `MacCormack`).
10    #[get(type(copy))]
11    pub(crate) advection: AdvectionScheme,
12    /// Incompressible pressure Poisson solver (`RedBlackGaussSeidel` or `ConjugateGradient`).
13    #[get(type(copy))]
14    pub(crate) pressure_solver: PressureSolverMode,
15    /// Maximum pressure solver iterations executed per projection.
16    #[get(type(copy))]
17    pub(crate) pressure_iterations: u32,
18    /// Thermal buoyancy strength (`+Y` acceleration per unit temperature above `ambient_temperature`).
19    #[get(type(copy))]
20    pub(crate) thermal_buoyancy: f32,
21    /// Ambient reference temperature used by thermal buoyancy.
22    #[get(type(copy))]
23    pub(crate) ambient_temperature: f32,
24    /// Dense dye/smoke downward gravity coefficient along `-Y`.
25    #[get(type(copy))]
26    pub(crate) dye_gravity: f32,
27    /// Vorticity confinement coefficient preserving small-scale eddies.
28    #[get(type(copy))]
29    pub(crate) vorticity_epsilon: f32,
30    /// Exponential dye/density fade rate per second.
31    #[get(type(copy))]
32    pub(crate) dye_dissipation: f32,
33    /// Exponential temperature relaxation rate toward `ambient_temperature` per second.
34    #[get(type(copy))]
35    pub(crate) temperature_dissipation: f32,
36    /// Mild velocity viscosity damping rate per second.
37    #[get(type(copy))]
38    pub(crate) velocity_damping: f32,
39}
40
41/// Pre-allocated scratch buffers enabling zero steady-state heap allocations in `MacFluidGrid2D::step`.
42#[derive(Clone, Data, Debug, Default, PartialEq)]
43pub(crate) struct MacScratch2D {
44    /// Primary advection scratch for staggered `u` faces.
45    pub(crate) u_scratch: Vec<f32>,
46    /// Secondary MacCormack scratch for staggered `u` faces.
47    pub(crate) u_aux: Vec<f32>,
48    /// Primary advection scratch for staggered `v` faces.
49    pub(crate) v_scratch: Vec<f32>,
50    /// Secondary MacCormack scratch for staggered `v` faces.
51    pub(crate) v_aux: Vec<f32>,
52    /// Primary cell-centered scalar scratch for dye/temperature advection.
53    pub(crate) scalar_scratch: Vec<f32>,
54    /// Secondary cell-centered MacCormack scratch for dye/temperature advection.
55    pub(crate) scalar_aux: Vec<f32>,
56    /// Cell-centered 2D scalar curl buffer for vorticity confinement.
57    pub(crate) curl: Vec<f32>,
58    /// Conjugate Gradient residual vector `r`.
59    pub(crate) cg_r: Vec<f32>,
60    /// Conjugate Gradient search direction vector `p`.
61    pub(crate) cg_p: Vec<f32>,
62    /// Conjugate Gradient matrix-vector product `A * p`.
63    pub(crate) cg_ap: Vec<f32>,
64}
65
66/// Incompressible 2D Eulerian staggered Marker-and-Cell (MAC) smoke/water/ink fluid grid solver.
67#[derive(Clone, Data, Debug, PartialEq)]
68pub struct MacFluidGrid2D {
69    /// Horizontal cell count `nx`.
70    #[get(type(copy))]
71    pub(crate) nx: usize,
72    /// Vertical cell count `ny`.
73    #[get(type(copy))]
74    pub(crate) ny: usize,
75    /// Sanitized solver configuration.
76    #[get(type(copy))]
77    pub(crate) config: MacFluidConfig,
78    /// Staggered horizontal velocity component `u` (`(nx + 1) * ny`).
79    pub(crate) u: Vec<f32>,
80    /// Staggered vertical velocity component `v` (`nx * (ny + 1)`).
81    pub(crate) v: Vec<f32>,
82    /// Cell-centered pressure solution field (`nx * ny`).
83    pub(crate) pressure: Vec<f32>,
84    /// Cell-centered discrete divergence diagnostic field (`nx * ny`).
85    pub(crate) divergence: Vec<f32>,
86    /// Cell-centered dye / smoke / ink density field (`nx * ny`).
87    pub(crate) dye: Vec<f32>,
88    /// Cell-centered temperature field driving thermal buoyancy (`nx * ny`).
89    pub(crate) temperature: Vec<f32>,
90    /// Solid obstacle cell mask (`0` = fluid, `1` = solid obstacle, `nx * ny`).
91    pub(crate) solid: Vec<u8>,
92    /// Pre-allocated advection, vorticity, and Poisson scratch arrays.
93    pub(crate) scratch: MacScratch2D,
94    /// Accumulated simulation time in seconds.
95    #[get(type(copy))]
96    pub(crate) sim_time_secs: f32,
97}
98
99/// Pre-allocated scratch buffers enabling zero steady-state heap allocations in `MacFluidGrid3D::step`.
100#[derive(Clone, Data, Debug, Default, PartialEq)]
101pub(crate) struct MacScratch3D {
102    /// Primary advection scratch for staggered `u` faces.
103    pub(crate) u_scratch: Vec<f32>,
104    /// Secondary MacCormack scratch for staggered `u` faces.
105    pub(crate) u_aux: Vec<f32>,
106    /// Primary advection scratch for staggered `v` faces.
107    pub(crate) v_scratch: Vec<f32>,
108    /// Secondary MacCormack scratch for staggered `v` faces.
109    pub(crate) v_aux: Vec<f32>,
110    /// Primary advection scratch for staggered `w` faces.
111    pub(crate) w_scratch: Vec<f32>,
112    /// Secondary MacCormack scratch for staggered `w` faces.
113    pub(crate) w_aux: Vec<f32>,
114    /// Primary cell-centered scalar scratch.
115    pub(crate) scalar_scratch: Vec<f32>,
116    /// Secondary cell-centered MacCormack scratch.
117    pub(crate) scalar_aux: Vec<f32>,
118    /// Cell-centered 3D vorticity vector X component.
119    pub(crate) vort_x: Vec<f32>,
120    /// Cell-centered 3D vorticity vector Y component.
121    pub(crate) vort_y: Vec<f32>,
122    /// Cell-centered 3D vorticity vector Z component.
123    pub(crate) vort_z: Vec<f32>,
124    /// Cell-centered 3D vorticity magnitude `|omega|`.
125    pub(crate) vort_mag: Vec<f32>,
126    /// Conjugate Gradient residual vector `r`.
127    pub(crate) cg_r: Vec<f32>,
128    /// Conjugate Gradient search direction vector `p`.
129    pub(crate) cg_p: Vec<f32>,
130    /// Conjugate Gradient matrix-vector product `A * p`.
131    pub(crate) cg_ap: Vec<f32>,
132}
133
134/// Incompressible 3D Eulerian staggered Marker-and-Cell (MAC) smoke/water fluid grid solver.
135#[derive(Clone, Data, Debug, PartialEq)]
136pub struct MacFluidGrid3D {
137    /// Cell count along X (`nx`).
138    #[get(type(copy))]
139    pub(crate) nx: usize,
140    /// Cell count along Y (`ny`).
141    #[get(type(copy))]
142    pub(crate) ny: usize,
143    /// Cell count along Z (`nz`).
144    #[get(type(copy))]
145    pub(crate) nz: usize,
146    /// Sanitized solver configuration.
147    #[get(type(copy))]
148    pub(crate) config: MacFluidConfig,
149    /// Staggered X-face velocity `u` (`(nx + 1) * ny * nz`).
150    pub(crate) u: Vec<f32>,
151    /// Staggered Y-face velocity `v` (`nx * (ny + 1) * nz`).
152    pub(crate) v: Vec<f32>,
153    /// Staggered Z-face velocity `w` (`nx * ny * (nz + 1)`).
154    pub(crate) w: Vec<f32>,
155    /// Cell-centered pressure solution field (`nx * ny * nz`).
156    pub(crate) pressure: Vec<f32>,
157    /// Cell-centered discrete divergence diagnostic field (`nx * ny * nz`).
158    pub(crate) divergence: Vec<f32>,
159    /// Cell-centered volumetric smoke/dye density field (`nx * ny * nz`).
160    pub(crate) dye: Vec<f32>,
161    /// Cell-centered volumetric temperature field (`nx * ny * nz`).
162    pub(crate) temperature: Vec<f32>,
163    /// Solid obstacle cell mask (`0` = fluid, `1` = solid obstacle, `nx * ny * nz`).
164    pub(crate) solid: Vec<u8>,
165    /// Pre-allocated advection, vorticity, and Poisson scratch arrays.
166    pub(crate) scratch: MacScratch3D,
167    /// Accumulated simulation time in seconds.
168    #[get(type(copy))]
169    pub(crate) sim_time_secs: f32,
170}
171
172/// Bundled dimensions and cell size for 2D MAC helper calls staying within clippy argument limits.
173#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
174pub(crate) struct MacDims2D {
175    /// Horizontal cell count `nx`.
176    #[get(type(copy))]
177    pub(crate) nx: usize,
178    /// Vertical cell count `ny`.
179    #[get(type(copy))]
180    pub(crate) ny: usize,
181    /// Uniform physical cell spacing `dx`.
182    #[get(type(copy))]
183    pub(crate) dx: f32,
184}
185
186/// Bundled dimensions and cell size for 3D MAC helper calls staying within clippy argument limits.
187#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
188pub(crate) struct MacDims3D {
189    /// Cell count along X.
190    #[get(type(copy))]
191    pub(crate) nx: usize,
192    /// Cell count along Y.
193    #[get(type(copy))]
194    pub(crate) ny: usize,
195    /// Cell count along Z.
196    #[get(type(copy))]
197    pub(crate) nz: usize,
198    /// Uniform physical cell spacing `dx`.
199    #[get(type(copy))]
200    pub(crate) dx: f32,
201}
202
203/// Disjoint row-range mutable pointer wrapper for parallel bicubic MAC dye rasterization on native targets.
204#[cfg(not(target_arch = "wasm32"))]
205#[derive(Clone, Copy)]
206pub(crate) struct MacRgbaMutView {
207    /// Base raw pointer to destination RGBA8 bytes.
208    pub(crate) ptr: *mut u8,
209}