Add a way to render the state
Note that this has exposed some scaling error that needs to be fixed.
This commit is contained in:
@@ -1,5 +1,13 @@
|
|||||||
use coremem::SimState;
|
use coremem::SimState;
|
||||||
|
use coremem::render::NumericTermRenderer;
|
||||||
|
use std::{thread, time};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut state = SimState::default();
|
let mut state = SimState::new(16);
|
||||||
state.step();
|
state.impulse_e(10, 1.0);
|
||||||
|
loop {
|
||||||
|
NumericTermRenderer.render(&state);
|
||||||
|
state.step();
|
||||||
|
thread::sleep(time::Duration::from_millis(500));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
23
src/lib.rs
23
src/lib.rs
@@ -5,6 +5,8 @@
|
|||||||
//!
|
//!
|
||||||
//! [1] https://www.eecs.wsu.edu/~schneidj/ufdtd/ufdtd.pdf
|
//! [1] https://www.eecs.wsu.edu/~schneidj/ufdtd/ufdtd.pdf
|
||||||
|
|
||||||
|
pub mod render;
|
||||||
|
|
||||||
// Some things to keep in mind:
|
// Some things to keep in mind:
|
||||||
// B = mu_r*H + M
|
// B = mu_r*H + M
|
||||||
// For a vacuum, B = H
|
// For a vacuum, B = H
|
||||||
@@ -24,8 +26,14 @@ pub struct SimState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SimState {
|
impl SimState {
|
||||||
|
pub fn new(size: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
cells: vec![Cell::default(); size],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn step(&mut self) {
|
pub fn step(&mut self) {
|
||||||
let mut working_cells = self.cells.clone();
|
let mut working_cells = vec![Cell::default(); self.cells.len()];
|
||||||
// first advance all the magnetic fields
|
// first advance all the magnetic fields
|
||||||
for (i, left_cell) in self.cells.iter().enumerate() {
|
for (i, left_cell) in self.cells.iter().enumerate() {
|
||||||
let right_cell = match self.cells.get(i+1) {
|
let right_cell = match self.cells.get(i+1) {
|
||||||
@@ -43,7 +51,12 @@ impl SimState {
|
|||||||
self.cells[i] = right_cell.step_e(left_cell);
|
self.cells[i] = right_cell.step_e(left_cell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get(&self) -> &[Cell] {
|
|
||||||
|
pub fn impulse_e(&mut self, idx: usize, e: f32) {
|
||||||
|
self.cells[idx].ez += e;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cells(&self) -> &[Cell] {
|
||||||
&*self.cells
|
&*self.cells
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,6 +79,12 @@ pub struct Cell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Cell {
|
impl Cell {
|
||||||
|
pub fn ez(&self) -> f32 {
|
||||||
|
self.ez
|
||||||
|
}
|
||||||
|
pub fn by(&self) -> f32 {
|
||||||
|
self.by
|
||||||
|
}
|
||||||
fn step_b(self, right: Cell) -> Self {
|
fn step_b(self, right: Cell) -> Self {
|
||||||
// Maxwell's equation: del x E = -dB/dt
|
// Maxwell's equation: del x E = -dB/dt
|
||||||
// Expands: dB_y/dt = dE_z/dx
|
// Expands: dB_y/dt = dE_z/dx
|
||||||
|
17
src/render.rs
Normal file
17
src/render.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use crate::SimState;
|
||||||
|
|
||||||
|
pub struct NumericTermRenderer;
|
||||||
|
|
||||||
|
impl NumericTermRenderer {
|
||||||
|
pub fn render(&self, state: &SimState) {
|
||||||
|
print!("E: ");
|
||||||
|
for cell in state.cells() {
|
||||||
|
print!("{:>8.1e} ", cell.ez());
|
||||||
|
}
|
||||||
|
print!("\nB: ");
|
||||||
|
for cell in state.cells() {
|
||||||
|
print!("{:>8.1e} ", cell.by());
|
||||||
|
}
|
||||||
|
print!("\n");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user