use crate::constants::universal::SPEED_OF_LIGHT;
use crate::{AmountOfSubstance, Energy, HalfLife, Mass, PhysicsError, Time};
use deep_causality_num::{FromPrimitive, RealField};
pub fn radioactive_decay_kernel<R>(
n0: &AmountOfSubstance<R>,
half_life: &HalfLife<R>,
time: &Time<R>,
) -> Result<AmountOfSubstance<R>, PhysicsError>
where
R: RealField + FromPrimitive,
{
let zero = R::zero();
if half_life.value() == zero {
return Err(PhysicsError::Singularity(
"Radioactive half-life cannot be zero".into(),
));
}
let two = R::from_f64(2.0)
.ok_or_else(|| PhysicsError::NumericalInstability("R::from_f64(2.0) failed".into()))?;
let decay_ratio = time.value() / half_life.value();
let remaining = n0.value() * two.powf(-decay_ratio);
AmountOfSubstance::new(remaining)
}
pub fn binding_energy_kernel<R>(mass_defect: &Mass<R>) -> Result<Energy<R>, PhysicsError>
where
R: RealField + FromPrimitive,
{
let c = R::from_f64(SPEED_OF_LIGHT)
.ok_or_else(|| PhysicsError::NumericalInstability("R::from_f64(c)".into()))?;
let e = mass_defect.value() * c * c;
Energy::new(e)
}