Skip to main content

qualia_core_db/audio/qac/numerical/
elastic_virieux.rs

1//! QAC Mode `0x0001` (`elastic_stress_velocity_continuum`) Wire Adapter & 3D Virieux Elastic Facade.
2//!
3//! Re-exports the canonical 3D Virieux 6-component Cauchy stress-velocity solver, active Hamilton
4//! quaternion stress rotation, and Mode 03 normal-incidence impedance interface from
5//! [`etalon_sim::acoustics_continuum::elastic_virieux`] and the WGSL compute kernels from
6//! [`etalon_wgsl_forge::numerical::physics::acoustic_fdtd`], adding QAC batch wire codecs and
7//! `f64` $\leftrightarrow$ `f32` GPU state buffer adapters.
8
9pub use etalon_sim::acoustics_continuum::elastic_virieux::{
10    step_elastic_virieux_in_place, CauchyStress6, ElasticBoundaryCondition, ElasticMedium,
11    ElasticStressVelocityRecord, ElasticVirieuxConfig, ElasticVirieuxState,
12    HamiltonQuaternionXyzw, NormalIncidenceInterface, TensorStrain6,
13    ELASTIC_STRESS_VELOCITY_RECORD_STRIDE, ESV_DEFINED_VALIDITY_MASK, ESV_VALID_DISPLACEMENT,
14    ESV_VALID_STRESS, ESV_VALID_VELOCITY, MODE_ELASTIC_STRESS_VELOCITY_CONTINUUM,
15};
16#[cfg(feature = "gpu-runtime")]
17pub use etalon_wgsl_forge::numerical::physics::acoustic_fdtd::{
18    elastic_virieux_fdtd_step_cpu, elastic_virieux_fdtd_step_gpu,
19    elastic_virieux_fdtd_step_with_context, ElasticVirieuxFdtdGpuParams, ElasticVirieuxGpuBoundary,
20    ELASTIC_VIRIEUX_CELL_STRIDE, ELASTIC_VIRIEUX_FDTD_ENTRY, ELASTIC_VIRIEUX_FDTD_WGSL,
21};
22
23use super::fdtd_fluid::NumericalError;
24
25/// Encode a slice of Mode `0x0001` [`ElasticStressVelocityRecord`] values into contiguous 64-byte wire records.
26pub fn encode_elastic_stress_velocity_records(
27    records: &[ElasticStressVelocityRecord],
28) -> Result<Vec<u8>, NumericalError> {
29    let mut out = Vec::with_capacity(records.len() * ELASTIC_STRESS_VELOCITY_RECORD_STRIDE);
30    for rec in records {
31        let bytes = rec.to_wire_bytes()?;
32        out.extend_from_slice(&bytes);
33    }
34    Ok(out)
35}
36
37/// Decode a contiguous byte slice of 64-byte Mode `0x0001` wire records into [`ElasticStressVelocityRecord`] values.
38pub fn decode_elastic_stress_velocity_records(
39    bytes: &[u8],
40) -> Result<Vec<ElasticStressVelocityRecord>, NumericalError> {
41    if bytes.len() % ELASTIC_STRESS_VELOCITY_RECORD_STRIDE != 0 {
42        return Err(NumericalError::WireEncoding(
43            "ElasticStressVelocityRecord stream byte length must be a multiple of 64",
44        ));
45    }
46    let count = bytes.len() / ELASTIC_STRESS_VELOCITY_RECORD_STRIDE;
47    let mut out = Vec::with_capacity(count);
48    for i in 0..count {
49        let off = i * ELASTIC_STRESS_VELOCITY_RECORD_STRIDE;
50        out.push(ElasticStressVelocityRecord::from_wire_bytes(
51            &bytes[off..off + ELASTIC_STRESS_VELOCITY_RECORD_STRIDE],
52        )?);
53    }
54    Ok(out)
55}
56
57/// Convert an [`ElasticVirieuxConfig`] into [`ElasticVirieuxFdtdGpuParams`] for WGSL compute execution.
58#[cfg(feature = "gpu-runtime")]
59pub fn elastic_config_to_gpu_params(
60    cfg: &ElasticVirieuxConfig,
61) -> Result<ElasticVirieuxFdtdGpuParams, NumericalError> {
62    cfg.validate()?;
63    let boundary = match cfg.boundary {
64        ElasticBoundaryCondition::Periodic => ElasticVirieuxGpuBoundary::Periodic,
65        ElasticBoundaryCondition::FreeSurface => ElasticVirieuxGpuBoundary::FreeSurface,
66    };
67    Ok(ElasticVirieuxFdtdGpuParams {
68        dims: [cfg.dims[0] as u32, cfg.dims[1] as u32, cfg.dims[2] as u32],
69        spacing_m: [
70            cfg.spacing_m[0] as f32,
71            cfg.spacing_m[1] as f32,
72            cfg.spacing_m[2] as f32,
73        ],
74        dt_s: cfg.dt_s as f32,
75        density_kg_m3: cfg.medium.density_kg_m3 as f32,
76        bulk_modulus_pa: cfg.medium.bulk_modulus_pa as f32,
77        shear_modulus_pa: cfg.medium.shear_modulus_pa as f32,
78        boundary,
79    })
80}
81
82/// Pack an [`ElasticVirieuxState`] into the flat `f32` storage buffer layout
83/// (`[vx, vy, vz, sxx, syy, szz, sxy, syz, sxz]` per cell) consumed by [`ELASTIC_VIRIEUX_FDTD_WGSL`].
84#[cfg(feature = "gpu-runtime")]
85pub fn pack_elastic_virieux_state_f32(state: &ElasticVirieuxState) -> Vec<f32> {
86    let n = state.vx.len();
87    let mut out = Vec::with_capacity(n * ELASTIC_VIRIEUX_CELL_STRIDE);
88    for i in 0..n {
89        out.push(state.vx[i] as f32);
90        out.push(state.vy[i] as f32);
91        out.push(state.vz[i] as f32);
92        out.push(state.sxx[i] as f32);
93        out.push(state.syy[i] as f32);
94        out.push(state.szz[i] as f32);
95        out.push(state.sxy[i] as f32);
96        out.push(state.syz[i] as f32);
97        out.push(state.sxz[i] as f32);
98    }
99    out
100}
101
102/// Unpack a flat `f32` storage buffer (`[vx, vy, vz, sxx, syy, szz, sxy, syz, sxz]` per cell)
103/// back into an [`ElasticVirieuxState`].
104#[cfg(feature = "gpu-runtime")]
105pub fn unpack_elastic_virieux_state_f32(
106    flat: &[f32],
107    state: &mut ElasticVirieuxState,
108) -> Result<(), NumericalError> {
109    let n = state.vx.len();
110    if flat.len() != n * ELASTIC_VIRIEUX_CELL_STRIDE {
111        return Err(NumericalError::BufferLengthMismatch {
112            buffer: "unpack_elastic_virieux_state_f32",
113            expected: n * ELASTIC_VIRIEUX_CELL_STRIDE,
114            actual: flat.len(),
115        });
116    }
117    for i in 0..n {
118        let base = i * ELASTIC_VIRIEUX_CELL_STRIDE;
119        state.vx[i] = flat[base] as f64;
120        state.vy[i] = flat[base + 1] as f64;
121        state.vz[i] = flat[base + 2] as f64;
122        state.sxx[i] = flat[base + 3] as f64;
123        state.syy[i] = flat[base + 4] as f64;
124        state.szz[i] = flat[base + 5] as f64;
125        state.sxy[i] = flat[base + 6] as f64;
126        state.syz[i] = flat[base + 7] as f64;
127        state.sxz[i] = flat[base + 8] as f64;
128    }
129    state.step_index += 1;
130    Ok(())
131}