stim: Simplify the Exp implementation. it's no longer a Stimulus

This commit is contained in:
2022-08-19 04:14:33 -07:00
parent 77124fcdaf
commit ad5f064584
3 changed files with 15 additions and 37 deletions

View File

@@ -22,7 +22,7 @@ use coremem::real::{R32, Real as _};
use coremem::render::CsvRenderer;
use coremem::sim::spirv::{SpirvSim, WgpuBackend};
use coremem::sim::units::{Seconds, Time as _};
use coremem::stim::{CurlVectorField, Exp1, Gated, ModulatedVectorField, Sinusoid, StimExt as _};
use coremem::stim::{CurlVectorField, Exp, Gated, ModulatedVectorField, Sinusoid, StimExt as _};
use log::{error, info, warn};
use serde::{Deserialize, Serialize};
@@ -456,7 +456,7 @@ fn run_sim(id: u32, p: Params, g: Geometries) -> Results {
};
let add_drive_exp_pulse = |driver: &mut Driver<_>, region: &Torus, start: f32, duration: f32, amp: f32| {
let wave = Exp1::new_at(amp, start, 0.5*duration);
let wave = Exp::new_at(amp, start, 0.5*duration);
driver.add_stimulus(ModulatedVectorField::new(
CurlVectorField::new(region.clone()),
wave,

View File

@@ -43,7 +43,7 @@ use coremem::meas;
use coremem::real::{self, Real as _};
use coremem::sim::spirv::{self, SpirvSim};
use coremem::sim::units::Seconds;
use coremem::stim::{CurlVectorField, Exp, Gated, ModulatedVectorField, Shifted, StimExt as _};
use coremem::stim::{CurlVectorField, Exp, Gated, ModulatedVectorField, Scaled, Shifted, StimExt as _};
use coremem::Driver;
type R = real::R32;
@@ -76,7 +76,7 @@ enum ClockState {
ReleaseLow,
Float,
}
type ClockSegment = Shifted<Gated<Exp<f32>>>;
type ClockSegment = Shifted<Gated<Scaled<Exp, f32>>>;
impl ClockState {
fn time_stimulus(&self, params: &Params, cycle: u32)

View File

@@ -475,50 +475,27 @@ impl TimeVarying for Sinusoid {
}
}
/// E field with magnitude that decays exponentially over t.
/// E field that decays exponentially over t.
#[derive(Clone)]
pub struct Exp<A> {
amp: A,
pub struct Exp {
tau: f32,
}
pub type Exp1 = Exp<f32>;
impl<A> Exp<A> {
pub fn new(amp: A, half_life: f32) -> Self {
impl Exp {
pub fn new(half_life: f32) -> Self {
let tau = std::f32::consts::LN_2/half_life;
Self { amp, tau }
Self { tau }
}
pub fn new_at(amp: A, start: f32, half_life: f32) -> Shifted<Gated<Self>> {
Shifted::new(
Gated::new(
Self::new(amp, half_life),
0.0,
half_life*100.0
),
start,
)
pub fn new_at(amp: f32, start: f32, half_life: f32) -> Shifted<Gated<Scaled<Self, f32>>> {
Self::new(half_life).scaled(amp).gated(0.0, half_life*100.0).shifted(start)
}
}
impl<A: TimeVarying> TimeVarying for Exp<A> {
impl TimeVarying for Exp {
fn at(&self, t_sec: f32) -> FieldMags {
self.amp.at(t_sec) * (t_sec * -self.tau).exp()
}
}
impl<A: Stimulus> Stimulus for Exp<A> {
fn at(&self, t_sec: f32, feat_size: f32, loc: Index) -> Fields {
let scale = (t_sec * -self.tau).exp();
let inner = self.amp.at(t_sec, feat_size, loc);
Fields {
e: inner.e * scale,
h: inner.h * scale,
}
}
fn eval_into(&self, t_sec: f32, feat_size: f32, scale: f32, mut into: OffsetDimSlice<&mut [Fields]>) {
let my_scale = (t_sec * -self.tau).exp();
self.amp.eval_into(t_sec, feat_size, scale * my_scale, into.as_mut());
let a = (t_sec * -self.tau).exp();
FieldMags::new_eh(a, a)
}
}
@@ -616,6 +593,7 @@ impl<T: Stimulus> Stimulus for Shifted<T> {
}
#[derive(Clone)]
pub struct Scaled<A, B>(A, B);
impl<A, B> Scaled<A, B> {