mod collision;
mod reflection;
mod relaxation;
mod simulation;
mod space;
mod streaming;
mod thermal;
#[doc(inline)]
pub use collision::*;
#[doc(inline)]
pub use reflection::*;
#[doc(inline)]
pub use relaxation::*;
#[doc(inline)]
pub use simulation::*;
#[doc(inline)]
pub use space::*;
#[doc(inline)]
pub use streaming::*;
#[doc(inline)]
pub use thermal::*;
pub use crate::support::math::FRAC_1_SQRT_3;
pub const SPEED_OF_SOUND: f64 = FRAC_1_SQRT_3;
pub const C2: f64 = SPEED_OF_SOUND * SPEED_OF_SOUND;
pub const C4: f64 = C2 * C2;
#[cfg(test)]
mod tests {
use burn::{
Tensor,
prelude::{
Bool,
ElementConversion,
s,
},
};
use serial_test::serial;
use crate::{
kits::sims::lbm::d2q9::{
collision::bgk_collision_with_spherical_reflection,
relaxation::RelaxationParam,
space,
space::dbg_dist,
streaming::outflow_clipping_stream,
},
support::testing::{
DeviceMemoryGuard,
PerformanceBackend,
default_device,
},
};
#[test]
#[serial]
fn test_closed_box_steps_conserve_mass() {
type B = PerformanceBackend;
let device = default_device();
let _memory = DeviceMemoryGuard::<B>::new(&device);
let k = 5;
let height = 6;
let width = 6;
let debug = false;
let solid_mask: Tensor<B, 2, Bool> = Tensor::full([height, width], false, &device)
.slice_fill(s![0, ..], true)
.slice_fill(s![-1, ..], true)
.slice_fill(s![.., 0], true)
.slice_fill(s![.., -1], true);
let dist_t0: Tensor<B, 4> = Tensor::zeros([height, width, 3, 3], &device)
.slice_fill(s![.., .., 1, 1], 1.0)
.slice_fill(s![1, 1, 1, 1], 3.0)
.slice_fill(s![1, -2, 1, 1], 5.0)
.slice_fill(s![-2, -2, 1, 1], 10.0);
if debug {
dbg_dist("dist_t0", dist_t0.clone());
}
let initial_mass: f64 = dist_t0.clone().sum().into_scalar().elem();
let lbm_tables = space::LbmTables::init(&device);
let mut current = dist_t0.clone();
for t_idx in 1..=k {
let stream_phase = outflow_clipping_stream(current.clone());
if debug {
dbg_dist(format!("stream {t_idx}").as_str(), stream_phase.clone());
}
let thermal_phase = bgk_collision_with_spherical_reflection(
stream_phase,
solid_mask.clone(),
RelaxationParam::Tau(1.0),
None,
&lbm_tables,
);
if debug {
dbg_dist(format!("thermal {t_idx}").as_str(), thermal_phase.clone());
}
current = thermal_phase;
let current_mass: f64 = current.clone().sum().into_scalar().elem();
assert!(
(current_mass - initial_mass).abs() <= 1e-4 * initial_mass,
"step {t_idx}: mass {current_mass} drifted from {initial_mass}"
);
}
}
}