def test_parse_rho_and_derivs_polarized_with_wrong_shape(self, shape): with self.assertRaisesRegex( ValueError, r'Wrong shape for rho_and_derivs. ' rf'Expected \(2, 6, \*\), got \({shape}\)'): evaluators.parse_rho_and_derivs(np.zeros(shape), omega=0., polarized=True)
def test_eval_exc_with_b97_u(self, use_jax): rho_and_derivs = np.random.rand(6, 5) quantities, lda_energies = evaluators.parse_rho_and_derivs( 0.5 * rho_and_derivs, omega=0., polarized=False) # 0.5: spin factor parameters = copy.deepcopy(xc_functionals.B97_PARAMETERS) u_x = gga.u_b97( np.sqrt(quantities['x2']), gamma=parameters['parameters_x'].pop('gamma'), polarized=True) u_css = gga.u_b97( np.sqrt(quantities['x2']), gamma=parameters['parameters_css'].pop('gamma'), polarized=True) u_cos = gga.u_b97( np.sqrt(quantities['x2']), gamma=parameters['parameters_cos'].pop('gamma'), polarized=True) e_xc = xc_functionals.b97_u.eval_exc( **parameters, e_lda_x=lda_energies['x'], e_lda_css=lda_energies['css'], e_lda_cos=lda_energies['cos'], features_x={'u': u_x}, features_css={'u': u_css}, features_cos={'u': u_cos}, use_jax=use_jax ) np.testing.assert_allclose( e_xc, _get_expected_e_xc_unpolarized(rho_and_derivs, xc_name='B97'))
def test_eval_exc_with_wb97mv_polarized(self, wb97mv_form, parameters): rho_and_derivs = np.random.rand(2, 6, 5) quantities, lda_energies = evaluators.parse_rho_and_derivs( rho_and_derivs, omega=0.3, polarized=True) e_xc = np.sum(wb97mv_form.eval_exc( **parameters, e_lda_x=lda_energies['x'], e_lda_css=lda_energies['css'], e_lda_cos=lda_energies['cos'], features={'w': quantities['w'], 'x2': quantities['x2']}, ).reshape(-1, 3), axis=1) np.testing.assert_allclose( e_xc, _get_expected_e_xc_polarized(rho_and_derivs, xc_name='wB97M-V'))
def test_eval_exc_with_wb97mv_unpolarized(self, wb97mv_form, parameters): rho_and_derivs = np.random.rand(6, 5) quantities, lda_energies = evaluators.parse_rho_and_derivs( 0.5 * rho_and_derivs, omega=0.3, polarized=False) # 0.5: spin factor e_xc = wb97mv_form.eval_exc( **parameters, e_lda_x=lda_energies['x'], e_lda_css=lda_energies['css'], e_lda_cos=lda_energies['cos'], features={'w': quantities['w'], 'x2': quantities['x2']}, ) np.testing.assert_allclose( e_xc, _get_expected_e_xc_unpolarized(rho_and_derivs, xc_name='wB97M-V'))
def test_parse_rho_and_derivs_spin_consistency(self): num_grids = 10 rho_and_derivs_unpolarized = np.random.rand(6, num_grids) rho_and_derivs_polarized = np.repeat( rho_and_derivs_unpolarized[np.newaxis, :, :], repeats=2, axis=0) features_unpolarized, lda_energies_unpolarized = ( evaluators.parse_rho_and_derivs(rho_and_derivs_unpolarized, omega=0.3, polarized=False)) features_polarized, lda_energies_polarized = ( evaluators.parse_rho_and_derivs(rho_and_derivs_polarized, omega=0.3, polarized=True)) for feature_name in features_unpolarized: np.testing.assert_allclose( np.stack([features_unpolarized[feature_name]] * 3, axis=1), features_polarized[feature_name].reshape([-1, 3])) for component in ['x', 'css']: np.testing.assert_allclose( np.stack([ lda_energies_unpolarized[component], lda_energies_unpolarized[component], np.zeros(num_grids) ], axis=1), 2 * lda_energies_polarized[component].reshape([-1, 3])) np.testing.assert_allclose( np.stack([ np.zeros(num_grids), np.zeros(num_grids), lda_energies_unpolarized['cos'], ], axis=1), lda_energies_polarized['cos'].reshape([-1, 3]))
def test_eval_exc_with_b97_x2_polarized(self, b97_x2, parameters, use_jax): rho_and_derivs = np.random.rand(2, 6, 5) quantities, lda_energies = evaluators.parse_rho_and_derivs( rho_and_derivs, omega=0., polarized=True) e_xc = np.sum(b97_x2.eval_exc( **parameters, e_lda_x=lda_energies['x'], e_lda_css=lda_energies['css'], e_lda_cos=lda_energies['cos'], features={'x2': quantities['x2']}, use_jax=use_jax ).reshape(-1, 3), axis=1) np.testing.assert_allclose( e_xc, _get_expected_e_xc_polarized(rho_and_derivs, xc_name='B97'))
def test_eval_exc_with_b97_x2_unpolarized(self, b97_x2, parameters, use_jax): rho_and_derivs = np.random.rand(6, 5) quantities, lda_energies = evaluators.parse_rho_and_derivs( 0.5 * rho_and_derivs, omega=0., polarized=False) # 0.5: spin factor e_xc = b97_x2.eval_exc( **parameters, e_lda_x=lda_energies['x'], e_lda_css=lda_energies['css'], e_lda_cos=lda_energies['cos'], features={'x2': quantities['x2']}, use_jax=use_jax ) np.testing.assert_allclose( e_xc, _get_expected_e_xc_unpolarized(rho_and_derivs, xc_name='B97'))
def test_parse_rho_and_derivs_polarized(self): num_grids = 10 rho_and_derivs = np.zeros([2, 6, num_grids]) features, lda_energies = evaluators.parse_rho_and_derivs( rho_and_derivs, omega=0., polarized=True) for feature_name in ['rho', 'x2', 'u']: np.testing.assert_allclose(features[feature_name], np.zeros(3 * num_grids), atol=1e-7) for feature_name in ['w']: np.testing.assert_allclose(features[feature_name], -np.ones(3 * num_grids)) for lda_energy in lda_energies.values(): np.testing.assert_allclose(lda_energy, np.zeros(3 * num_grids))