#[cfg(feature = "extern")]
pub mod ex;
#[cfg(feature = "python")]
pub mod py;
mod test;
pub mod legendre;
use std::f64::consts::PI;
use crate::math::
{
inverse_newton_raphson_powered,
integrate_1d
};
use crate::physics::
{
PLANCK_CONSTANT,
BOLTZMANN_CONSTANT
};
use crate::physics::single_chain::
{
ONE,
ZERO,
POINTS
};
use super::isometric::nondimensional_force as isometric_nondimensional_force;
use super::isometric::nondimensional_helmholtz_free_energy as isometric_nondimensional_helmholtz_free_energy;
use super::isometric::nondimensional_relative_helmholtz_free_energy as isometric_nondimensional_relative_helmholtz_free_energy;
pub struct WLC
{
pub hinge_mass: f64,
pub link_length: f64,
pub number_of_links: u8,
pub persistance_length: f64,
nondimensional_persistance_length: f64,
pub legendre: self::legendre::WLC
}
pub fn end_to_end_length(number_of_links: &u8, link_length: &f64, persistance_length: &f64, force: &f64, temperature: &f64) -> f64
{
link_length*nondimensional_end_to_end_length(number_of_links, &(persistance_length/((*number_of_links as f64)*link_length)), &(force*link_length/BOLTZMANN_CONSTANT/temperature))
}
pub fn end_to_end_length_per_link(number_of_links: &u8, link_length: &f64, persistance_length: &f64, force: &f64, temperature: &f64) -> f64
{
link_length*nondimensional_end_to_end_length_per_link(number_of_links, &(persistance_length/((*number_of_links as f64)*link_length)), &(force*link_length/BOLTZMANN_CONSTANT/temperature))
}
pub fn nondimensional_end_to_end_length(number_of_links: &u8, nondimensional_persistance_length: &f64, nondimensional_force: &f64) -> f64
{
let f = &|nondimensional_end_to_end_length_per_link: &f64| isometric_nondimensional_force(number_of_links, nondimensional_persistance_length, nondimensional_end_to_end_length_per_link);
let fp = &|nondimensional_end_to_end_length_per_link: &f64| (isometric_nondimensional_force(number_of_links, nondimensional_persistance_length, &(nondimensional_end_to_end_length_per_link + 1e-8)) - isometric_nondimensional_force(number_of_links, nondimensional_persistance_length, &(nondimensional_end_to_end_length_per_link - 1e-8)))/2e-8;
let gamma_hat = inverse_newton_raphson_powered(nondimensional_force, f, fp, &0.9999, &1e-5, &100, 2);
if !(0.0..=1.0).contains(&gamma_hat)
{
panic!("Root finding failed!");
}
let scale = (*number_of_links as f64)*nondimensional_force*gamma_hat - isometric_nondimensional_relative_helmholtz_free_energy(nondimensional_persistance_length, &gamma_hat);
let integrand_denominator = |nondimensional_end_to_end_length_per_link: &f64|
{
let beta_delta_psi = isometric_nondimensional_relative_helmholtz_free_energy(nondimensional_persistance_length, nondimensional_end_to_end_length_per_link);
let beta_f_xi = (*number_of_links as f64)*nondimensional_force*nondimensional_end_to_end_length_per_link;
((beta_f_xi - scale - beta_delta_psi).exp() - (-beta_f_xi - scale - beta_delta_psi).exp())*nondimensional_end_to_end_length_per_link/nondimensional_force
};
let integrand_numerator = |nondimensional_end_to_end_length_per_link: &f64|
{
let beta_delta_psi = isometric_nondimensional_relative_helmholtz_free_energy(nondimensional_persistance_length, nondimensional_end_to_end_length_per_link);
let beta_f_xi = (*number_of_links as f64)*nondimensional_force*nondimensional_end_to_end_length_per_link;
let exp_1 = (beta_f_xi - scale - beta_delta_psi).exp();
let exp_2 = (-beta_f_xi - scale - beta_delta_psi).exp();
((exp_1 + exp_2)*(*number_of_links as f64)*nondimensional_end_to_end_length_per_link - (exp_1 - exp_2)/nondimensional_force)*nondimensional_end_to_end_length_per_link/nondimensional_force
};
integrate_1d(&integrand_numerator, &ZERO, &ONE, &POINTS)/integrate_1d(&integrand_denominator, &ZERO, &ONE, &POINTS)
}
pub fn nondimensional_end_to_end_length_per_link(number_of_links: &u8, nondimensional_persistance_length: &f64, nondimensional_force: &f64) -> f64
{
nondimensional_end_to_end_length(number_of_links, nondimensional_persistance_length, nondimensional_force)/(*number_of_links as f64)
}
pub fn gibbs_free_energy(number_of_links: &u8, link_length: &f64, hinge_mass: &f64, persistance_length: &f64, force: &f64, temperature: &f64) -> f64
{
nondimensional_gibbs_free_energy(number_of_links, link_length, hinge_mass, &(persistance_length/((*number_of_links as f64)*link_length)), &(force*link_length/BOLTZMANN_CONSTANT/temperature), temperature)*BOLTZMANN_CONSTANT*temperature
}
pub fn gibbs_free_energy_per_link(number_of_links: &u8, link_length: &f64, hinge_mass: &f64, persistance_length: &f64, force: &f64, temperature: &f64) -> f64
{
nondimensional_gibbs_free_energy_per_link(number_of_links, link_length, hinge_mass, &(persistance_length/((*number_of_links as f64)*link_length)), &(force*link_length/BOLTZMANN_CONSTANT/temperature), temperature)*BOLTZMANN_CONSTANT*temperature
}
pub fn relative_gibbs_free_energy(number_of_links: &u8, link_length: &f64, persistance_length: &f64, force: &f64, temperature: &f64) -> f64
{
nondimensional_relative_gibbs_free_energy(number_of_links, &(persistance_length/((*number_of_links as f64)*link_length)), &(force*link_length/BOLTZMANN_CONSTANT/temperature))*BOLTZMANN_CONSTANT*temperature
}
pub fn relative_gibbs_free_energy_per_link(number_of_links: &u8, link_length: &f64, persistance_length: &f64, force: &f64, temperature: &f64) -> f64
{
nondimensional_relative_gibbs_free_energy_per_link(number_of_links, &(persistance_length/((*number_of_links as f64)*link_length)), &(force*link_length/BOLTZMANN_CONSTANT/temperature))*BOLTZMANN_CONSTANT*temperature
}
pub fn nondimensional_gibbs_free_energy(number_of_links: &u8, link_length: &f64, hinge_mass: &f64, nondimensional_persistance_length: &f64, nondimensional_force: &f64, temperature: &f64) -> f64
{
let f = &|nondimensional_end_to_end_length_per_link: &f64| isometric_nondimensional_force(number_of_links, nondimensional_persistance_length, nondimensional_end_to_end_length_per_link);
let fp = &|nondimensional_end_to_end_length_per_link: &f64| (isometric_nondimensional_force(number_of_links, nondimensional_persistance_length, &(nondimensional_end_to_end_length_per_link + 1e-8)) - isometric_nondimensional_force(number_of_links, nondimensional_persistance_length, &(nondimensional_end_to_end_length_per_link - 1e-8)))/2e-8;
let fpp = &|x: &f64| (fp(&(x + 1e-8)) - fp(&(x - 1e-8)))/2e-8;
let gamma_hat = inverse_newton_raphson_powered(nondimensional_force, f, fp, &0.9999, &1e-5, &100, 2);
if !(0.0..=1.0).contains(&gamma_hat)
{
panic!("Root finding failed!");
}
let scale = (*number_of_links as f64)*nondimensional_force*gamma_hat - isometric_nondimensional_helmholtz_free_energy(number_of_links, link_length, hinge_mass, nondimensional_persistance_length, &gamma_hat, temperature);
let log_integral = if nondimensional_force > &100.0
{
-((8.0*PI.powi(2)/fpp(&gamma_hat)).sqrt()).ln() - (0.5*(*number_of_links as f64)*gamma_hat/nondimensional_force).ln()
}
else
{
let integrand = |nondimensional_end_to_end_length_per_link: &f64|
{
let beta_psi = isometric_nondimensional_helmholtz_free_energy(number_of_links, link_length, hinge_mass, nondimensional_persistance_length, nondimensional_end_to_end_length_per_link, temperature);
let beta_f_xi = (*number_of_links as f64)*nondimensional_force*nondimensional_end_to_end_length_per_link;
0.5*((beta_f_xi - scale - beta_psi).exp() - (-beta_f_xi - scale - beta_psi).exp())*(*number_of_links as f64)*nondimensional_end_to_end_length_per_link/nondimensional_force
};
-(4.0*PI*integrate_1d(&integrand, &ZERO, &ONE, &POINTS)).ln()
};
log_integral - scale - (4.0*(-1.0/nondimensional_persistance_length).exp().acos().sin()*PI.powi(2)*hinge_mass*link_length.powi(2)*BOLTZMANN_CONSTANT*temperature/PLANCK_CONSTANT.powi(2)).ln()
}
pub fn nondimensional_gibbs_free_energy_per_link(number_of_links: &u8, link_length: &f64, hinge_mass: &f64, nondimensional_persistance_length: &f64, nondimensional_force: &f64, temperature: &f64) -> f64
{
nondimensional_gibbs_free_energy(number_of_links, link_length, hinge_mass, nondimensional_persistance_length, nondimensional_force, temperature)/(*number_of_links as f64)
}
pub fn nondimensional_relative_gibbs_free_energy(number_of_links: &u8, nondimensional_persistance_length: &f64, nondimensional_force: &f64) -> f64
{
nondimensional_gibbs_free_energy(number_of_links, &1.0, &1.0, nondimensional_persistance_length, nondimensional_force, &300.0) - nondimensional_gibbs_free_energy(number_of_links, &1.0, &1.0, nondimensional_persistance_length, &ZERO, &300.0)
}
pub fn nondimensional_relative_gibbs_free_energy_per_link(number_of_links: &u8, nondimensional_persistance_length: &f64, nondimensional_force: &f64) -> f64
{
nondimensional_gibbs_free_energy_per_link(number_of_links, &1.0, &1.0, nondimensional_persistance_length, nondimensional_force, &300.0) - nondimensional_gibbs_free_energy_per_link(number_of_links, &1.0, &1.0, nondimensional_persistance_length, &ZERO, &300.0)
}
impl WLC
{
pub fn init(number_of_links: u8, link_length: f64, hinge_mass: f64, persistance_length: f64) -> Self
{
WLC
{
hinge_mass,
link_length,
number_of_links,
persistance_length,
nondimensional_persistance_length: persistance_length/(number_of_links as f64)/link_length,
legendre: self::legendre::WLC::init(number_of_links, link_length, hinge_mass, persistance_length)
}
}
pub fn end_to_end_length(&self, force: &f64, temperature: &f64) -> f64
{
end_to_end_length(&self.number_of_links, &self.link_length, &self.persistance_length, force, temperature)
}
pub fn end_to_end_length_per_link(&self, force: &f64, temperature: &f64) -> f64
{
end_to_end_length_per_link(&self.number_of_links, &self.link_length, &self.persistance_length, force, temperature)
}
pub fn nondimensional_end_to_end_length(&self, nondimensional_force: &f64) -> f64
{
nondimensional_end_to_end_length(&self.number_of_links, &self.nondimensional_persistance_length, nondimensional_force)
}
pub fn nondimensional_end_to_end_length_per_link(&self, nondimensional_force: &f64) -> f64
{
nondimensional_end_to_end_length_per_link(&self.number_of_links, &self.nondimensional_persistance_length, nondimensional_force)
}
pub fn gibbs_free_energy(&self, force: &f64, temperature: &f64) -> f64
{
gibbs_free_energy(&self.number_of_links, &self.link_length, &self.hinge_mass, &self.persistance_length, force, temperature)
}
pub fn gibbs_free_energy_per_link(&self, force: &f64, temperature: &f64) -> f64
{
gibbs_free_energy_per_link(&self.number_of_links, &self.link_length, &self.hinge_mass, &self.persistance_length, force, temperature)
}
pub fn relative_gibbs_free_energy(&self, force: &f64, temperature: &f64) -> f64
{
relative_gibbs_free_energy(&self.number_of_links, &self.link_length, &self.persistance_length, force, temperature)
}
pub fn relative_gibbs_free_energy_per_link(&self, force: &f64, temperature: &f64) -> f64
{
relative_gibbs_free_energy_per_link(&self.number_of_links, &self.link_length, &self.persistance_length, force, temperature)
}
pub fn nondimensional_gibbs_free_energy(&self, nondimensional_force: &f64, temperature: &f64) -> f64
{
nondimensional_gibbs_free_energy(&self.number_of_links, &self.link_length, &self.hinge_mass, &self.nondimensional_persistance_length, nondimensional_force, temperature)
}
pub fn nondimensional_gibbs_free_energy_per_link(&self, nondimensional_force: &f64, temperature: &f64) -> f64
{
nondimensional_gibbs_free_energy_per_link(&self.number_of_links, &self.link_length, &self.hinge_mass, &self.nondimensional_persistance_length, nondimensional_force, temperature)
}
pub fn nondimensional_relative_gibbs_free_energy(&self, nondimensional_force: &f64) -> f64
{
nondimensional_relative_gibbs_free_energy(&self.number_of_links, &self.nondimensional_persistance_length, nondimensional_force)
}
pub fn nondimensional_relative_gibbs_free_energy_per_link(&self, nondimensional_force: &f64) -> f64
{
nondimensional_relative_gibbs_free_energy_per_link(&self.number_of_links, &self.nondimensional_persistance_length, nondimensional_force)
}
}