fdtd-coremem/src/render.rs

69 lines
1.9 KiB
Rust
Raw Normal View History

2020-07-13 06:00:54 +00:00
use ansi_term::Color::RGB;
use crate::{consts, Material as _, SimState};
use std::fmt::Write as _;
pub struct NumericTermRenderer;
impl NumericTermRenderer {
pub fn render(&self, state: &SimState) {
for y in 0..state.height() {
for x in 0..state.width() {
let cell = state.get(x, y);
print!(" {:>10.1e}", cell.ex());
}
print!("\n");
for x in 0..state.width() {
let cell = state.get(x, y);
print!("{:>10.1e} {:>10.1e}", cell.ey(), cell.bz());
}
print!("\n");
}
print!("\n");
}
}
2020-07-13 06:00:54 +00:00
pub struct ColorTermRenderer;
fn clamp(v: f32, range: f32) -> f32 {
v.min(range).max(-range)
}
fn norm_color(v: f64) -> u8 {
(v * 64.0 + 128.0).max(0.0).min(255.0) as u8
2020-07-16 07:31:20 +00:00
}
fn curl(x: f64, y: f64) -> f64 {
let c = x * y;
if c >= 0.0 {
c.sqrt()
} else {
-(-c).sqrt()
}
}
2020-07-13 06:00:54 +00:00
impl ColorTermRenderer {
pub fn render(&self, state: &SimState) {
let mut buf = String::new();
2020-07-16 07:31:20 +00:00
let square = "";
for y in 0..state.height() {
for x in 0..state.width() {
let cell = state.get(x, y);
//let r = norm_color(cell.bz() * consts::C);
//let r = 0;
let r = norm_color(cell.mat().mz()*1.0e-2);
let b = (55.0*cell.mat().conductivity()).min(255.0) as u8;
//let b = 0;
//let b = norm_color(cell.ey());
//let g = 0;
//let g = norm_color(cell.ex());
//let g = norm_color(curl(cell.ex(), cell.ey()));
let g = norm_color((cell.bz() * 1.0e4).into());
//let g = norm_color(cell.ey().into());
write!(&mut buf, "{}", RGB(r, g, b).paint(square));
2020-07-16 07:31:20 +00:00
}
write!(&mut buf, "\n");
2020-07-16 07:31:20 +00:00
}
println!("{}\ntime: {:.3e}", buf, state.time());
2020-07-13 06:00:54 +00:00
}
}