use num_dual::DualNum;
use super::lda_c_pw::{pw92_ec, A_MOD};
use crate::families::mgga::{gtv4, Mgga, MggaEnergy, MggaVars};
use crate::families::XcEval;
use crate::func::{Family, FunctionalId, FunctionalInfo, Kind};
use crate::reduced::consts::{FPP_VWN, K_FACTOR_C};
const GAMMA_SS: f64 = 0.06;
const GAMMA_AB: f64 = 0.0031;
const ALPHA_SS: f64 = 0.00515088;
const ALPHA_AB: f64 = 0.00304966;
const CSS: [f64; 5] = [
5.349466e-01,
5.396620e-01,
-3.161217e+01,
5.149592e+01,
-2.919613e+01,
];
const CAB: [f64; 5] = [
6.042374e-01,
1.776783e+02,
-2.513252e+02,
7.635173e+01,
-1.255699e+01,
];
const DSS: [f64; 6] = [
4.650534e-01,
1.617589e-01,
1.833657e-01,
4.692100e-04,
-4.990573e-03,
0.0,
];
const DAB: [f64; 6] = [
3.957626e-01,
-5.614546e-01,
1.403963e-02,
9.831442e-04,
-3.577176e-03,
0.0,
];
const FERMI_D_CNST: f64 = 1e-10;
fn f_pw<N: DualNum<f64> + Copy>(rs: N, z: N, zeta_threshold: f64) -> N {
pw92_ec(rs, z, zeta_threshold, &A_MOD, FPP_VWN)
}
fn b97_g<N: DualNum<f64> + Copy>(gamma: f64, c: &[f64; 5], w: N) -> N {
let gw = N::from(gamma) * w;
let u = gw / (N::from(1.0) + gw);
let mut p = N::from(c[4]);
for &ci in c[..4].iter().rev() {
p = p * u + N::from(ci);
}
p
}
fn fermi_d<N: DualNum<f64> + Copy>(w: N, t: N) -> N {
let eight_t = N::from(8.0) * t;
(eight_t - w) / eight_t
}
fn fermi_d_corrected<N: DualNum<f64> + Copy>(w: N, t: N) -> N {
let arg = N::from(-4.0 / (FERMI_D_CNST * FERMI_D_CNST)) * (t * t);
fermi_d(w, t) * (-arg.exp_m1())
}
pub(crate) struct MggaCM06L {
info: FunctionalInfo,
zeta_threshold: f64,
}
impl MggaCM06L {
fn new() -> Self {
Self {
info: FunctionalInfo {
id: Some(FunctionalId::MggaCM06L),
name: "mgga_c_m06_l",
family: Family::Mgga,
kind: Kind::Correlation,
needs_sigma: true,
needs_lapl: false,
needs_tau: true,
dens_threshold: 1e-12, hybrid: None,
},
zeta_threshold: f64::EPSILON, }
}
pub(crate) fn boxed() -> Box<dyn XcEval> {
Box::new(Mgga(Self::new()))
}
}
impl MggaEnergy for MggaCM06L {
fn info(&self) -> &FunctionalInfo {
&self.info
}
fn f<N: DualNum<f64> + Copy>(&self, v: MggaVars<N>) -> N {
let zt = self.zeta_threshold;
let thr = self.info.dens_threshold;
let MggaVars {
rs,
z,
opz,
omz,
na,
nb,
xs0_sq,
xs1_sq,
t0,
t1,
..
} = v;
let k = N::from(K_FACTOR_C);
let up_screened = na.re() <= thr || opz.re() <= zt;
let dn_screened = nb.re() <= thr || omz.re() <= zt;
let par_up = if up_screened {
N::from(0.0)
} else {
opz / N::from(2.0) * f_pw(rs * (N::from(2.0) / opz).powf(1.0 / 3.0), N::from(1.0), zt)
};
let par_dn = if dn_screened {
N::from(0.0)
} else {
omz / N::from(2.0) * f_pw(rs * (N::from(2.0) / omz).powf(1.0 / 3.0), N::from(-1.0), zt)
};
let perp = f_pw(rs, z, zt) - par_up - par_dn;
let up = if up_screened {
N::from(0.0)
} else {
par_up
* (b97_g(GAMMA_SS, &CSS, xs0_sq) * fermi_d_corrected(xs0_sq, t0)
+ gtv4(ALPHA_SS, &DSS, xs0_sq, N::from(2.0) * (t0 - k)) * fermi_d(xs0_sq, t0))
};
let dn = if dn_screened {
N::from(0.0)
} else {
par_dn
* (b97_g(GAMMA_SS, &CSS, xs1_sq) * fermi_d_corrected(xs1_sq, t1)
+ gtv4(ALPHA_SS, &DSS, xs1_sq, N::from(2.0) * (t1 - k)) * fermi_d(xs1_sq, t1))
};
let w_ab = xs0_sq + xs1_sq;
let zz_ab = N::from(2.0) * (t0 + t1 - N::from(2.0) * k);
let cross = perp * (b97_g(GAMMA_AB, &CAB, w_ab) + gtv4(ALPHA_AB, &DAB, w_ab, zz_ab));
up + dn + cross
}
}
#[cfg(test)]
mod tests {
use super::{f_pw, FPP_VWN};
use crate::functionals::lda_c_pw::{pw92_ec, A_MOD};
use crate::reduced::vars::rs_from_n;
use crate::{Functional, FunctionalId, Spin, XcInput};
fn m06l(spin: Spin) -> Functional {
Functional::new(FunctionalId::MggaCM06L, spin).unwrap()
}
#[test]
fn c_uses_modified_pw92() {
let zt = f64::EPSILON;
let lda = Functional::new(FunctionalId::LdaCPw, Spin::Unpolarized).unwrap();
for &n in &[0.1_f64, 1.0, 7.3, 100.0] {
let rs = rs_from_n(n);
let want = pw92_ec(rs, 0.0_f64, zt, &A_MOD, FPP_VWN);
let got = f_pw(rs, 0.0_f64, zt);
assert!(
(got - want).abs() <= 1e-14 * want.abs().max(1.0),
"n={n}: f_pw {got} vs shared modified pw92_ec {want}"
);
let std = lda.eval(1, &XcInput::lda(&[n])).unwrap().exc[0];
assert!(
(got - std).abs() > 1e-7 * std.abs(),
"n={n}: f_pw unexpectedly equals standard lda_c_pw"
);
}
}
#[test]
fn unpol_derivs_match_finite_difference() {
let f = m06l(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 = m06l(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]
);
}
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]
);
}
assert_eq!(out.vsigma[1], 0.0, "M06-L correlation vsigma_ab must be 0");
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 = m06l(Spin::Unpolarized);
let po = m06l(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 = m06l(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}");
}
}
}