Files
fdtd-coremem/crates/coremem/examples/wavefront.rs
colin 5b99d30cda restructure this multi-crate project to use Cargo's "workspace" feature
this solves an issue in the Nix build, where managing multiple
Cargo.lock files is otherwise tricky. it causes (or fails to fix?) an adjacent issue where
the spirv builder doesn't seem to have everything it needs vendored.
2022-07-05 17:34:21 -07:00

63 lines
2.8 KiB
Rust

//! this example runs a *2-dimensional* simulation.
//! it places a dissipative material (i.e. a conductor) on the left edge of the simulation
//! and then radiates a vertical wavefront from the center of the simulation.
//! it's about the bare-minimum simulation which still does something interesting.
//!
//! note that any practical simulation should probably terminate the simulation space
//! with something that absorbs energy. since this example doesn't, it lets you see what
//! happens when you just use the default boundary conditions.
use coremem::{mat, driver};
use coremem::geom::{Coord as _, Cube, Index, Vec3};
use coremem::units::Seconds;
use coremem::stim::{Stimulus, TimeVarying as _, UniformStimulus};
fn main() {
coremem::init_logging();
// create a 2d simulation with so many grid cells
let width = 401;
let height = 401;
let size = Index::new(width, height, 1 /* depth */);
// each cell represents 1um x 1um x 1um volume
let feature_size = 1e-6;
// Create the simulation "driver" which uses the CPU as backend.
// by default all the computations are done with R32: a f32 which panics on NaN/Inf
// you can parameterize it to use R64, or unchecked f32 -- see src/driver.rs for the definition
let mut driver: driver::CpuDriver = driver::Driver::new(size, feature_size);
// uncomment to use the Spirv/GPU driver. this one is restricted to unchecked f32.
// note: this won't have better perf unless you reduce the y4m/term renderer framerate below.
// let mut driver: driver::SpirvDriver = driver::Driver::new_spirv(size, feature_size);
// create a conductor on the left side.
let conductor = Cube::new(
Index::new(0, 0, 0).to_meters(feature_size),
Index::new(width/10, height, 1).to_meters(feature_size),
);
driver.fill_region(&conductor, mat::IsomorphicConductor::new(200f32));
// create a vertical strip in the center of the simulation which emits a wave.
let center_region = Cube::new(
Index::new(200, height/4, 0).to_meters(feature_size),
Index::new(201, height*3/4, 1).to_meters(feature_size),
);
// emit a constant E/H delta over this region for 100 femtoseconds
let stim = Stimulus::new(
center_region,
UniformStimulus::new(
Vec3::new(2e19, 0.0, 0.0), // E field (per second)
Vec3::new(0.0, 0.0, 2e19/376.730) // H field (per second)
).gated(0.0, 100e-15),
);
driver.add_stimulus(stim);
// render the output to a video and to the terminal (low-res)
let _ = std::fs::create_dir_all("out/examples/wavefront");
driver.add_y4m_renderer("out/examples/wavefront/rendered.y4m", 1, None);
driver.add_term_renderer(4, None);
// finally, run the simulation:
driver.step_until(Seconds(100e-12));
}