use num_dual::DualNum;
use super::mgga_c_m06_l::f_pw;
use crate::families::mgga::{mgga_exchange, Mgga, MggaEnergy, MggaVars};
use crate::families::XcEval;
use crate::func::{Family, FunctionalId, FunctionalInfo, HybridInfo, Kind, Vv10Params};
use crate::reduced::consts::K_FACTOR_C;
pub(crate) type B97MvTerm = (f64, i32, i32);
pub(crate) const GAMMA_X: f64 = 0.004;
pub(crate) const GAMMA_SS: f64 = 0.2;
pub(crate) const GAMMA_OS: f64 = 0.006;
const PAR_X: [B97MvTerm; 5] = [
(1.000, 0, 0),
(1.308, 0, 1),
(1.901, 0, 2),
(0.416, 1, 0),
(3.070, 1, 1),
];
const PAR_SS: [B97MvTerm; 5] = [
(1.000, 0, 0),
(-1.855, 0, 2),
(-5.668, 1, 0),
(-20.497, 3, 2),
(-20.364, 4, 2),
];
const PAR_OS: [B97MvTerm; 5] = [
(1.000, 0, 0),
(1.573, 0, 1),
(-6.298, 0, 3),
(2.535, 1, 0),
(-6.427, 3, 2),
];
pub(crate) fn b97mv_wx_ss<N: DualNum<f64> + Copy>(t: N) -> N {
let k = N::from(K_FACTOR_C);
(k - t) / (k + t)
}
pub(crate) fn b97mv_wx_os<N: DualNum<f64> + Copy>(t0: N, t1: N) -> N {
let k = N::from(K_FACTOR_C);
(t0 * (k - t1) + t1 * (k - t0)) / (t0 * (k + t1) + t1 * (k + t0))
}
pub(crate) fn b97mv_g<N: DualNum<f64> + Copy>(gamma: f64, terms: &[B97MvTerm], x_sq: N, w: N) -> N {
let gw = N::from(gamma) * x_sq;
let u = gw / (N::from(1.0) + gw);
let mut acc = N::from(0.0);
for &(c, wi, uj) in terms {
let mut term = N::from(c);
if wi != 0 {
term *= w.powi(wi);
}
if uj != 0 {
term *= u.powi(uj);
}
acc += term;
}
acc
}
pub(crate) fn b97mv_correlation<N: DualNum<f64> + Copy>(
v: &MggaVars<N>,
dens_threshold: f64,
zeta_threshold: f64,
par_ss: &[B97MvTerm],
par_os: &[B97MvTerm],
) -> N {
let zt = zeta_threshold;
let up_screened = v.na.re() <= dens_threshold || v.opz.re() <= zt;
let dn_screened = v.nb.re() <= dens_threshold || v.omz.re() <= zt;
let par_up = if up_screened {
N::from(0.0)
} else {
v.opz / N::from(2.0)
* f_pw(
v.rs * (N::from(2.0) / v.opz).powf(1.0 / 3.0),
N::from(1.0),
zt,
)
};
let par_dn = if dn_screened {
N::from(0.0)
} else {
v.omz / N::from(2.0)
* f_pw(
v.rs * (N::from(2.0) / v.omz).powf(1.0 / 3.0),
N::from(-1.0),
zt,
)
};
let perp = f_pw(v.rs, v.z, zt) - par_up - par_dn;
let up = if up_screened {
N::from(0.0)
} else {
par_up * b97mv_g(GAMMA_SS, par_ss, v.xs0_sq, b97mv_wx_ss(v.t0))
};
let dn = if dn_screened {
N::from(0.0)
} else {
par_dn * b97mv_g(GAMMA_SS, par_ss, v.xs1_sq, b97mv_wx_ss(v.t1))
};
let cross = perp
* b97mv_g(
GAMMA_OS,
par_os,
(v.xs0_sq + v.xs1_sq) / N::from(2.0),
b97mv_wx_os(v.t0, v.t1),
);
up + dn + cross
}
pub(crate) struct MggaXcB97mV {
info: FunctionalInfo,
zeta_threshold: f64,
}
impl MggaXcB97mV {
pub(crate) fn boxed() -> Box<dyn XcEval> {
Box::new(Mgga(Self {
info: FunctionalInfo {
id: Some(FunctionalId::MggaXcB97mV),
name: "mgga_xc_b97m_v",
family: Family::Mgga,
kind: Kind::ExchangeCorrelation,
needs_sigma: true,
needs_lapl: false,
needs_tau: true,
dens_threshold: 1e-15, hybrid: Some(HybridInfo {
exx_fraction: 0.0,
cam: None,
vv10: Some(Vv10Params { b: 6.0, c: 0.01 }),
}),
},
zeta_threshold: f64::EPSILON,
}))
}
}
impl MggaEnergy for MggaXcB97mV {
fn info(&self) -> &FunctionalInfo {
&self.info
}
fn f<N: DualNum<f64> + Copy>(&self, v: MggaVars<N>) -> N {
let thr = self.info.dens_threshold;
let zt = self.zeta_threshold;
let ex = mgga_exchange(&v, thr, zt, |x_sq, t| {
b97mv_g(GAMMA_X, &PAR_X, x_sq, b97mv_wx_ss(t))
});
ex + b97mv_correlation(&v, thr, zt, &PAR_SS, &PAR_OS)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::func::{DispersionModel, Rung};
use crate::{Functional, Spin, XcInput};
fn b97mv(spin: Spin) -> Functional {
Functional::new(FunctionalId::MggaXcB97mV, spin).unwrap()
}
#[test]
fn metadata_mgga_rung_with_vv10() {
let f = b97mv(Spin::Unpolarized);
let info = f.info();
assert_eq!(info.rung(), Rung::MetaGga);
assert_eq!(f.exx_fraction(), 0.0);
let h = info.hybrid.unwrap();
assert!(h.cam.is_none());
let vv10 = h.vv10.unwrap();
assert_eq!((vv10.b, vv10.c), (6.0, 0.01));
let g = info.grid();
assert_eq!((g.level, g.grid_sensitive), (4, true));
let d = info.dispersion().unwrap();
assert_eq!(d.model, DispersionModel::Vv10);
assert_eq!(d.param_set, "mgga_xc_b97m_v");
}
#[test]
fn ueg_limit_series_collapse() {
assert_eq!(
b97mv_g(GAMMA_X, &PAR_X, 0.0_f64, b97mv_wx_ss(K_FACTOR_C)),
1.0
);
assert_eq!(b97mv_wx_os(K_FACTOR_C, K_FACTOR_C), 0.0);
}
#[test]
fn unpol_derivs_match_finite_difference() {
let f = b97mv(Spin::Unpolarized);
let edens = |n: f64, s: f64, tau: f64| {
n * f
.eval(1, &XcInput::gga(&[n], &[s]).with_tau(&[tau]))
.unwrap()
.exc[0]
};
for &(n, s, tau) in &[
(0.5, 0.1, 0.3),
(2.0, 0.7, 1.5),
(0.3, 0.02, 0.2),
(5.0, 3.0, 8.0),
(1.0, 0.4, 0.06), ] {
let out = f
.eval(1, &XcInput::gga(&[n], &[s]).with_tau(&[tau]))
.unwrap();
let (hn, hs, ht) = (1e-6 * n, 1e-6 * s, 1e-6 * tau);
let fdn = (edens(n + hn, s, tau) - edens(n - hn, s, tau)) / (2.0 * hn);
let fds = (edens(n, s + hs, tau) - edens(n, s - hs, tau)) / (2.0 * hs);
let fdt = (edens(n, s, tau + ht) - edens(n, s, tau - ht)) / (2.0 * ht);
assert!(
(out.vrho[0] - fdn).abs() <= 1e-5 * out.vrho[0].abs().max(1.0),
"vrho n={n} s={s} t={tau}: {} vs {fdn}",
out.vrho[0]
);
assert!(
(out.vsigma[0] - fds).abs() <= 1e-5 * out.vsigma[0].abs().max(1.0),
"vsigma n={n} s={s} t={tau}: {} vs {fds}",
out.vsigma[0]
);
assert!(
(out.vtau[0] - fdt).abs() <= 1e-5 * out.vtau[0].abs().max(1.0),
"vtau n={n} s={s} t={tau}: {} vs {fdt}",
out.vtau[0]
);
}
}
#[test]
fn pol_derivs_match_finite_difference() {
let f = b97mv(Spin::Polarized);
let (na, nb, saa, sab, sbb, ta, tb) = (0.6, 0.3, 0.1, 0.05, 0.08, 0.4, 0.25);
let r = [na, nb];
let s = [saa, sab, sbb];
let t = [ta, tb];
let edens = |r: [f64; 2], s: [f64; 3], t: [f64; 2]| {
(r[0] + r[1]) * f.eval(1, &XcInput::gga(&r, &s).with_tau(&t)).unwrap().exc[0]
};
let out = f.eval(1, &XcInput::gga(&r, &s).with_tau(&t)).unwrap();
for (k, h) in [(0usize, 1e-6 * na), (1, 1e-6 * nb)] {
let (mut rp, mut rm) = (r, r);
rp[k] += h;
rm[k] -= h;
let fd = (edens(rp, s, t) - edens(rm, s, t)) / (2.0 * h);
assert!(
(out.vrho[k] - fd).abs() <= 1e-5 * out.vrho[k].abs().max(1.0),
"vrho[{k}]: {} vs {fd}",
out.vrho[k]
);
}
assert_eq!(out.vsigma[1], 0.0, "B97M-V vsigma_ab must be 0");
for (k, h) in [(0usize, 1e-6 * saa), (2usize, 1e-6 * sbb)] {
let (mut sp, mut sm) = (s, s);
sp[k] += h;
sm[k] -= h;
let fd = (edens(r, sp, t) - edens(r, sm, t)) / (2.0 * h);
assert!(
(out.vsigma[k] - fd).abs() <= 1e-5 * out.vsigma[k].abs().max(1.0),
"vsigma[{k}]: {} vs {fd}",
out.vsigma[k]
);
}
for (k, h) in [(0usize, 1e-6 * ta), (1, 1e-6 * tb)] {
let (mut tp, mut tm) = (t, t);
tp[k] += h;
tm[k] -= h;
let fd = (edens(r, s, tp) - edens(r, s, tm)) / (2.0 * h);
assert!(
(out.vtau[k] - fd).abs() <= 1e-5 * out.vtau[k].abs().max(1.0),
"vtau[{k}]: {} vs {fd}",
out.vtau[k]
);
}
}
#[test]
fn unpol_pol_symmetry_at_zero_polarization() {
let up = b97mv(Spin::Unpolarized);
let po = b97mv(Spin::Polarized);
let (n, s, tau) = (0.8, 0.3, 0.6);
let ou = up
.eval(1, &XcInput::gga(&[n], &[s]).with_tau(&[tau]))
.unwrap();
let op = po
.eval(
1,
&XcInput::gga(&[n / 2.0, n / 2.0], &[s / 4.0, s / 4.0, s / 4.0])
.with_tau(&[tau / 2.0, tau / 2.0]),
)
.unwrap();
assert!((ou.exc[0] - op.exc[0]).abs() <= 1e-11 * ou.exc[0].abs());
assert!((ou.vrho[0] - op.vrho[0]).abs() <= 1e-10 * ou.vrho[0].abs().max(1.0));
assert!((ou.vtau[0] - op.vtau[0]).abs() <= 1e-10 * ou.vtau[0].abs().max(1.0));
}
#[test]
fn edge_outputs_finite() {
let f = b97mv(Spin::Polarized);
let rho = [1.0, 0.0, 0.0, 1.0, 1e-10, 1e-11, 1.0, 1.0, 100.0, 50.0];
let sigma = [
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1e-18, 0.0, 1e-20, 1e6, 1e6, 1e6, 1.0, 0.5, 0.8, ];
let tau = [0.5, 0.0, 0.0, 0.5, 1e-12, 1e-13, 0.5, 0.5, 50.0, 30.0];
let out = f
.eval(5, &XcInput::gga(&rho, &sigma).with_tau(&tau))
.unwrap();
for v in out
.exc
.iter()
.chain(&out.vrho)
.chain(&out.vsigma)
.chain(&out.vtau)
{
assert!(v.is_finite(), "non-finite output: {v}");
}
}
}