Пример #1
0
def test_coefficients_weighted():
    # Test the coefficients for the weighted least norm model.
    omega = 5.
    order = 6
    n_elec = sp.symbols("n_elec")

    for weight in np.arange(0.01, 1., 0.4):
        _, energy, _, _, alpha, beta, even, odd = make_symbolic_least_norm_model(
            omega, order, weight, True)
        weighted = LeastNormGlobalTool(energy, omega, order, weight, eps=1e-9)

        alpha = sp.Sum(alpha, (n_elec, 2, 15)).doit()
        beta = sp.Sum(beta, (n_elec, 2, 15)).doit()

        # Test Odd Weighted Coefficients
        actual = [
            weighted._coefficients_weighted(j, alpha, beta)
            for j in range(3, 10, 2)
        ]
        desired = [
            odd.subs([("j", j), ("alpha", alpha), ("beta", beta)]).evalf()
            for j in range(3, 10, 2)
        ]
        assert_almost_equal(actual, desired)

        # Test Even Weighted Coefficients
        actual = [
            weighted._coefficients_weighted(j, alpha, beta)
            for j in range(2, 10, 2)
        ]
        desired = [
            even.subs([("j", j), ("alpha", alpha), ("beta", beta)]).evalf()
            for j in range(2, 10, 2)
        ]
        assert_almost_equal(actual, desired)
Пример #2
0
def test_chemical_softness():
    # Test chemical softness for the least norm model.
    omega = -2.
    for order in range(5, 8):
        n_ref, dict_energy, _, expr = make_symbolic_least_norm_model(
            omega, order)
        unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                         omega=omega,
                                         nth_order=order)

        assert_almost_equal(unweighted.softness,
                            1. / expr[2].subs([("n_elec", n_ref)]))

        weight = 0.1
        n_elec = sp.symbols("n_elec")
        n_ref, dict_energy, _, expr, alpha, beta = make_symbolic_least_norm_model(
            omega, order, weight)
        alpha = sp.Sum(alpha, (n_elec, 2, 13)).doit()
        beta = sp.Sum(beta, (n_elec, 2, 13)).doit()
        weighted = LeastNormGlobalTool(dict_energy,
                                       omega,
                                       order,
                                       weight,
                                       eps=1e-9)

        sub = [("n_elec", n_ref), ("alpha", alpha), ("beta", beta)]
        assert_almost_equal(weighted.softness, 1. / expr[2].subs(sub))
Пример #3
0
def test_chemical_hardness():
    # Test chemical hardness of least norm model.
    for omega in np.arange(-2., 2., step=0.5):
        for order in range(5, 10):
            n_ref, dict_energy, _, expr = make_symbolic_least_norm_model(
                omega, order)
            unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                             omega=omega,
                                             nth_order=order)

            assert_almost_equal(unweighted.chemical_hardness,
                                expr[2].subs([("n_elec", n_ref)]))
            assert_almost_equal(unweighted.eta,
                                expr[2].subs([("n_elec", n_ref)]))

            # Test weight least norm model.
            weight = 0.5
            n_elec = sp.symbols("n_elec")
            n_ref, dict_energy, _, expr, alpha, beta = make_symbolic_least_norm_model(
                omega, order, weight)
            alpha = sp.Sum(alpha, (n_elec, 2, 13)).doit()
            beta = sp.Sum(beta, (n_elec, 2, 13)).doit()
            weighted = LeastNormGlobalTool(dict_energy,
                                           omega,
                                           order,
                                           weight,
                                           eps=1e-9)

            desired = expr[2].subs([("n_elec", n_ref), ("alpha", alpha),
                                    ("beta", beta)]).evalf()
            assert_almost_equal(weighted.chemical_hardness, desired)
            assert_almost_equal(weighted.eta, desired)
Пример #4
0
def test_derivative_energy():
    # Test derivative of energy model of unweighted least norm model.
    for omega in np.arange(-2., 2.):
        # Take first five derivatives so I need atleast five terms.
        for order in range(5, 8):
            _, dict_energy, _, expr = make_symbolic_least_norm_model(
                omega, order)
            unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                             omega=omega,
                                             nth_order=order)

            # Go through each derivative up to fifth derivative.
            for d_order in range(1, 6):
                desired_values = [
                    expr[d_order].subs([("n_elec", x)]) for x in range(1, 20)
                ]
                actual_values = [
                    unweighted.energy_derivative(x, d_order)
                    for x in range(1, 20)
                ]
                assert_almost_equal(actual_values, desired_values)

    # Test assertions
    assert_raises(ValueError, unweighted.energy_derivative, 5, -1)
    assert_raises(ValueError, unweighted.energy_derivative, 5, 1.5)
Пример #5
0
def test_derivative_energy_weighted():
    # Test derivative of energy model of weighted least norm model.
    omega = 2.
    for order in range(5, 7):
        # Test weighted model.
        weight = 0.5
        n_elec = sp.symbols("n_elec")
        _, dict_energy, _, expr, alpha, beta = make_symbolic_least_norm_model(
            omega, order, weight)
        alpha = sp.Sum(alpha, (n_elec, 2, 13)).doit()
        beta = sp.Sum(beta, (n_elec, 2, 13)).doit()
        weighted = LeastNormGlobalTool(dict_energy,
                                       omega,
                                       order,
                                       weight,
                                       eps=1e-9)

        # Go through each derivative up to fifth derivative.
        for d_order in range(1, 6):
            desired_values = [
                expr[d_order].subs([("n_elec", x), ("alpha", alpha),
                                    ("beta", beta)]).evalf()
                for x in range(1, 5)
            ]
            actual_values = [
                weighted.energy_derivative(x, d_order) for x in range(1, 5)
            ]
            assert_almost_equal(actual_values, desired_values, decimal=4)
Пример #6
0
def test_nth_order():
    # Test the number of terms for the unweighted least norm model.
    n_ref = 10
    dict_energy = {n_ref - 1: 25.3, n_ref: 100., n_ref + 1: 50.5}
    unweighted = LeastNormGlobalTool(dict_energy, 0.5, nth_order=5)
    assert_equal(len(unweighted.params), 6)

    weighted = LeastNormGlobalTool(dict_energy, 0.5, nth_order=10, weight=0.5)
    assert_equal(len(weighted.params), 11)
Пример #7
0
def test_hyper_hardness():
    # Test hyperhardness for the least norm model.
    for omega in np.arange(-2., 2., step=0.5):
        for order in range(5, 10):
            n_ref, dict_energy, _, expr = make_symbolic_least_norm_model(
                omega, order)
            unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                             omega=omega,
                                             nth_order=order)

            assert_almost_equal(unweighted.hyper_hardness(2),
                                expr[3].subs([("n_elec", n_ref)]))
            assert_almost_equal(unweighted.hyper_hardness(3),
                                expr[4].subs([("n_elec", n_ref)]))

            # Test weight least norm model.
            weight = 0.5
            n_elec = sp.symbols("n_elec")
            n_ref, dict_energy, _, expr, alpha, beta = make_symbolic_least_norm_model(
                omega, order, weight)
            alpha = sp.Sum(alpha, (n_elec, 2, 13)).doit()
            beta = sp.Sum(beta, (n_elec, 2, 13)).doit()
            weighted = LeastNormGlobalTool(dict_energy,
                                           omega,
                                           order,
                                           weight,
                                           eps=1e-9)

            sub = [("n_elec", n_ref), ("alpha", alpha), ("beta", beta)]
            assert_almost_equal(weighted.hyper_hardness(2),
                                expr[3].subs(sub).evalf())
            assert_almost_equal(weighted.hyper_hardness(3),
                                expr[4].subs(sub).evalf())
Пример #8
0
def test_energy_least_norm():
    # Test energy model of unweighted least norm.
    for omega in np.arange(-10., 10.):
        for order in range(0, 6):
            _, dict_energy, _, expr = make_symbolic_least_norm_model(
                omega, order)
            unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                             omega=omega,
                                             nth_order=order)

            desired_values = [
                expr[0].subs([("n_elec", x)]) for x in range(1, 20)
            ]
            actual_values = [unweighted.energy(x) for x in range(1, 20)]
            assert_almost_equal(actual_values, desired_values)
Пример #9
0
def test_parameters_unweighted():
    # Test parameters of the unweighted least norm.
    for omega in np.arange(-10., 10.):
        for order in range(0, 10):
            _, dict_energy, params, _ = make_symbolic_least_norm_model(
                omega, order)

            unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                             omega=omega,
                                             nth_order=order)
            assert_almost_equal(params, unweighted.params)
Пример #10
0
def test_hyper_softness():
    # Test hyper softness for the least norm model.
    omega = 5.
    for order in range(5, 8):
        n_ref, dict_energy, _, expr = make_symbolic_least_norm_model(
            omega, order)
        unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                         omega=omega,
                                         nth_order=order)
        substitute = [("n_elec", n_ref)]

        assert_almost_equal(
            unweighted.hyper_softness(2),
            -expr[3].subs(substitute) / expr[2].subs(substitute)**3)

        weight = 0.2
        n_elec = sp.symbols("n_elec")
        n_ref, dict_energy, _, expr, alpha, beta = make_symbolic_least_norm_model(
            omega, order, weight)
        alpha = sp.Sum(alpha, (n_elec, 2, 13)).doit()
        beta = sp.Sum(beta, (n_elec, 2, 13)).doit()
        weighted = LeastNormGlobalTool(dict_energy,
                                       omega,
                                       order,
                                       weight,
                                       eps=1e-9)

        sub = [("n_elec", n_ref), ("alpha", alpha), ("beta", beta)]
        desired = -expr[3].subs(sub) / expr[2].subs(sub)**3.
        assert_almost_equal(weighted.hyper_softness(2), desired)
Пример #11
0
def test_alpha_and_beta():
    # Test the alpha and beta coefficients for the weighted least norm model.
    omega = 5.
    order = 7
    n_elec = sp.symbols("n_elec")

    for weight in np.arange(0.01, 1., 0.5):
        _, dict_energy, _, _, alpha, beta = make_symbolic_least_norm_model(
            omega, order, weight)
        weighted = LeastNormGlobalTool(dict_energy,
                                       omega,
                                       order,
                                       weight,
                                       eps=1e-9)

        # Test Beta
        actual = [weighted._beta_term(j) for j in range(1, 20)]
        desired = [beta.subs([(n_elec, j)]) for j in range(1, 20)]
        assert_almost_equal(actual, desired)

        # Test Alpha
        actual = [weighted._alpha_term(j) for j in range(1, 20)]
        desired = [alpha.subs([(n_elec, j)]) for j in range(1, 20)]
        assert_almost_equal(actual, desired)
Пример #12
0
def test_energy_weighted_leastnorm():
    # Test energy model of weighted least norm.
    omega = -5.
    for order in range(0, 3):
        # Test weighted model.
        for weight in np.linspace(0.01, 1., 3):
            n_elec = sp.symbols("n_elec")
            _, dict_energy, _, expr, alpha, beta = make_symbolic_least_norm_model(
                omega, order, weight)
            alpha = sp.Sum(alpha, (n_elec, 2, 13)).doit()
            beta = sp.Sum(beta, (n_elec, 2, 13)).doit()
            weighted = LeastNormGlobalTool(dict_energy,
                                           omega,
                                           order,
                                           weight,
                                           eps=1e-9)

            actual = [weighted.energy(n_elec) for n_elec in range(1, 20)]
            desired = [
                expr[0].subs([("n_elec", n_elec), ('alpha', alpha),
                              ('beta', beta)]).evalf()
                for n_elec in range(1, 20)
            ]
            assert_almost_equal(actual, desired, decimal=4)
Пример #13
0
def test_electron_aff_ionization():
    # Test electron affinity and ionization for the least norm model.
    for omega in np.arange(-5., 5.):
        for order in range(5, 10):
            n_ref, dict_energy, _, expr = make_symbolic_least_norm_model(
                omega, order)
            unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                             omega=omega,
                                             nth_order=order)

            substitute = [("n_elec", n_ref)]
            assert_almost_equal(
                unweighted.electron_affinity, expr[0].subs(substitute) -
                expr[0].subs([("n_elec", n_ref + 1.)]))
            assert_almost_equal(
                unweighted.ionization_potential,
                expr[0].subs([("n_elec", n_ref - 1.)]) -
                expr[0].subs(substitute))
Пример #14
0
def test_parameters_weighted():
    # Test parameters of the weighted least norm.
    omega = 5.
    for n_degree in range(0, 5):
        # Test weighted model.
        for weight in np.linspace(0.01, 1., 5):
            n_elec = sp.symbols("n_elec")
            _, dict_energy, params, _, alpha, beta = make_symbolic_least_norm_model(
                omega, n_degree, weight)
            alpha = sp.Sum(alpha, (n_elec, 2, 13)).doit()
            beta = sp.Sum(beta, (n_elec, 2, 13)).doit()
            weighted = LeastNormGlobalTool(dict_energy,
                                           omega,
                                           n_degree,
                                           weight,
                                           eps=1e-9)
            for i in range(2, n_degree):
                assert_almost_equal(
                    params[i].subs([("alpha", alpha), ("beta", beta)]),
                    weighted.params[i])
Пример #15
0
def test_least_norm_raises():
    # check invalid N0
    omega = 5.
    assert_raises(ValueError, LeastNormGlobalTool, {
        0: -15.0,
        1: -14.4,
        -1: -14.0
    }, omega)
    assert_raises(ValueError, LeastNormGlobalTool, {
        0.3: -15.0,
        1.3: -14.4,
        -0.7: -14.0
    }, omega)
    assert_raises(ValueError, LeastNormGlobalTool, {
        0.98: -15.0,
        1.98: -14.4,
        -0.02: -14.0
    }, omega)
    assert_raises(ValueError, LeastNormGlobalTool, {
        -1.: -15.0,
        0.: -14.9,
        -2.: -14.0
    }, omega)
    assert_raises(ValueError, LeastNormGlobalTool, {
        -2: -15.0,
        -1: -14.9,
        -3: -14.0
    }, omega)
    assert_raises(ValueError, LeastNormGlobalTool, {
        -2: -15.0,
        -1: -14.9,
        -3: -14.0
    }, omega)
    assert_raises(ValueError, LeastNormGlobalTool, {
        0.0: -15.0,
        0.5: -14.9,
        1.0: -14.0
    }, omega)
    assert_raises(ValueError, LeastNormGlobalTool, {
        2.: -15.0,
        3.5: -14.9,
        4.0: -14.0
    }, omega)

    # check invalid inputs
    assert_raises(ValueError,
                  LeastNormGlobalTool, {
                      1: -15.,
                      2: -14.4,
                      3: 20.
                  },
                  omega,
                  weight=10.)
    assert_raises(ValueError, LeastNormGlobalTool, {
        1: -15.,
        2: -14.4,
        3: 20.
    }, omega, 1.1)

    # Test weight and nth order
    least_norm = LeastNormGlobalTool({
        1: -15.,
        2: -14.4,
        3: 20.
    },
                                     omega,
                                     nth_order=5,
                                     weight=0.5)
    assert_equal(least_norm.weight, 0.5)
    assert_equal(least_norm.nth_order, 5)
Пример #16
0
def test_n_max():
    # Test getting the maximum for the unweighted least norm model.
    omega = 0.5
    order = 2
    _, dict_energy, _, _ = make_symbolic_least_norm_model(omega, order)
    unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                     omega=omega,
                                     nth_order=order)

    # Quadratic is unbounded below when a < 0. for a * x**2.
    for third_coefficient in np.arange(-10., -0.01):
        unweighted._params = [5., 2., third_coefficient]
        assert_equal(unweighted._compute_n_max(), np.inf)

    # Quadratic is bounded below when a > 0.
    unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                     omega=omega,
                                     nth_order=order)
    for third_coefficient in np.arange(0.01, 10.):
        unweighted._params = [5., 2., third_coefficient]
        actual = unweighted._compute_n_max()
        minima = max(-2. / (2. * third_coefficient), 0.)
        assert_equal(actual, minima)

    # Cubic, order=3
    order = 3
    _, dict_energy, _, _ = make_symbolic_least_norm_model(omega, order)
    unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                     omega=omega,
                                     nth_order=order)
    unweighted._params = [0., 0., -3., 5.]
    assert_equal(unweighted._compute_n_max(), 0.4)

    # Quadric
    order = 4
    _, dict_energy, _, _ = make_symbolic_least_norm_model(omega, order)
    unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                     omega=omega,
                                     nth_order=order)
    unweighted._params = [-3., 0, -3., 5., 5.]
    assert_almost_equal(unweighted._compute_n_max(), 0.2887, decimal=2)

    # Quantic
    order = 5
    _, dict_energy, _, _ = make_symbolic_least_norm_model(omega, order)
    unweighted = LeastNormGlobalTool(dict_energy=dict_energy,
                                     omega=omega,
                                     nth_order=order)
    unweighted._params = [-3., 0, -3., 5., -5., 10.]
    assert_almost_equal(unweighted._compute_n_max(), 0.4)

    # Leading coefficient is greater than zero, hence goes to infinity
    unweighted._params = [3., 0, 3., -5., 5., 110.]
    assert_almost_equal(unweighted._compute_n_max(), 0.)

    # The first coefficient is negative and thus energy goes to -infinity.
    unweighted._params = [0., 60., -32, -11, 8, -1.]
    assert_almost_equal(unweighted._compute_n_max(), np.inf, decimal=4)

    # Test getting global maxima when having multiple local maxima
    unweighted._params = [0., -60., 32, 11, -8, 1.]
    assert_almost_equal(unweighted._compute_n_max(), 4.35476, decimal=4)