#![warn(missing_docs)]
#![deny(unsafe_code)]
pub use rill_core::Transcendental;
pub mod macros;
mod adapters;
pub mod analysis;
pub mod constants;
mod elements;
pub mod tape;
pub mod wdf;
pub mod cavity;
pub mod modal;
pub mod plate;
pub mod string;
pub use adapters::{ParallelAdapter, SeriesAdapter};
pub use cavity::{CavityArray, HelmholtzCavity};
pub use elements::{Capacitor, Diode, Inductor, OpAmp, Resistor};
pub use modal::ModalModel;
pub use plate::PlateModel;
pub use string::StringModel;
pub trait WdfElement<T: Transcendental>: Send + Sync {
fn port_resistance(&self) -> T;
fn process_incident(&mut self, a: T) -> T;
fn update_state(&mut self);
fn voltage(&self) -> T;
fn current(&self) -> T;
fn reset(&mut self);
}
#[derive(Debug, Clone, Copy)]
pub struct WaveVariables<T: Transcendental> {
pub a: T,
pub b: T,
}
impl<T: Transcendental> WaveVariables<T> {
pub fn new() -> Self {
Self {
a: T::ZERO,
b: T::ZERO,
}
}
pub fn to_voltage_current(&self, port_resistance: T) -> (T, T) {
let two = T::from_f32(2.0);
let v = (self.a + self.b) / two;
let i = (self.a - self.b) / (two * port_resistance);
(v, i)
}
pub fn from_voltage_current(v: T, i: T, port_resistance: T) -> Self {
let a = v + port_resistance * i;
let b = v - port_resistance * i;
Self { a, b }
}
}
impl<T: Transcendental> Default for WaveVariables<T> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wave_variables() {
let wv: WaveVariables<f64> = WaveVariables::new();
assert_eq!(wv.a, 0.0);
assert_eq!(wv.b, 0.0);
}
#[test]
fn test_wave_to_voltage_current() {
let wv: WaveVariables<f64> = WaveVariables { a: 2.0, b: 0.5 };
let (v, i) = wv.to_voltage_current(100.0);
assert!((v - 1.25).abs() < 1e-10);
assert!((i - 0.0075).abs() < 1e-10);
}
#[test]
fn test_voltage_current_to_wave() {
let wv: WaveVariables<f64> = WaveVariables::from_voltage_current(1.0, 0.01, 100.0);
assert!((wv.a - 2.0).abs() < 1e-10);
assert!((wv.b - 0.0).abs() < 1e-10);
}
}