<gh_stars>1-10
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# pyDFTD3 -- Python implementation of Grimme's D3 dispersion correction.
# Copyright (C) 2020 <NAME> and contributors.
#
# This file is part of pyDFTD3.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# For information on the complete list of contributors to the
# pyDFTD3, see: <http://github.com/bobbypaton/pyDFTD3/>

from jax import jacfwd
import numpy as np
import pytest

from dftd3.data import get_r2r4, get_rcov, get_rab, read_coefficients
from dftd3.d3 import d3, d3_bj


def example_parameters():
    coordinates = [
        4.013766952115675,
        -0.42094027389798216,
        0.00413283103637627,
        2.3765970541469086,
        -2.0698869450806012,
        -0.07118787287212185,
        3.2393004941192927,
        2.023463933082604,
        0.03394892984384988,
        6.0906345455037165,
        -0.5931547951632049,
        0.053637986344994996,
        1.4072147949401792,
        2.121187340208405,
        -0.01083379987724973,
        -4.042144969341675,
        0.3453228829877872,
        -0.03309288390901749,
        -2.557736751902669,
        2.1345099093928828,
        -0.013709963040196544,
        -3.055607776368039,
        -2.0196183404172974,
        0.06482138555545441,
        -6.124857485635758,
        0.33042050276242674,
        -0.1200826466422177,
        -1.2231422422377085,
        -1.952500937619412,
        0.14005516206218147,
    ]

    charges = [6, 8, 8, 1, 1, 6, 8, 8, 1, 1]

    rab = get_rab()
    rcov = get_rcov()
    r2r4 = get_r2r4()
    coefficients = read_coefficients()

    return charges, coordinates, rab, rcov, r2r4, coefficients


def test_undifferentiated():
    charges, coordinates, rab, rcov, r2r4, coefficients = example_parameters()

    rs6 = 1.261
    rs8 = 1.0
    s6 = 1.0
    s8 = 1.703
    d3_au = d3(charges, coordinates, rs6, rs8, s6, s8, rab, rcov, r2r4, coefficients)
    assert d3_au == pytest.approx(-0.005259455231125385)


def test_undifferentiated_damping():
    charges, coordinates, rab, rcov, r2r4, coefficients = example_parameters()

    a1 = 0.3981
    a2 = 4.4211
    s6 = 1.0
    s8 = 1.9889
    d3_au = d3_bj(charges, coordinates, a1, a2, s6, s8, rab, rcov, r2r4, coefficients)
    assert d3_au == pytest.approx(-0.009129483884195357)


def test_derivatives():
    charges, coordinates, rab, rcov, r2r4, coefficients = example_parameters()

    rs6 = 1.261
    rs8 = 1.0
    s6 = 1.0
    s8 = 1.703

    f = lambda coordinates: d3(
        charges, coordinates, rs6, rs8, s6, s8, rab, rcov, r2r4, coefficients
    )
    d = jacfwd

    derivatives = d(f)(coordinates)
    derivatives = np.array(derivatives)

    ref = [
        0.000349716853,
        0.000062661785,
        -0.000003311506,
        0.000439833817,
        0.000557690553,
        0.000032711342,
        0.000609561955,
        -0.000303544148,
        -0.000003631289,
        -0.000501863558,
        0.000031792046,
        -0.000013434572,
        -0.000033137414,
        -0.000168498263,
        -0.000008039125,
        -0.000386522043,
        -0.000065823761,
        0.000002757262,
        -0.000310805767,
        -0.000580921020,
        0.000004944208,
        -0.000558542699,
        0.000283457649,
        -0.000014030170,
        0.000506038036,
        0.000017788128,
        0.000021991826,
        -0.000114279180,
        0.000165397030,
        -0.000019957978,
    ]

    for x_own, x_ref in zip(derivatives, ref):
        assert x_own == pytest.approx(x_ref)


def test_derivatives_damping():
    charges, coordinates, rab, rcov, r2r4, coefficients = example_parameters()

    a1 = 0.3981
    a2 = 4.4211
    s6 = 1.0
    s8 = 1.9889

    f = lambda coordinates: d3_bj(
        charges, coordinates, a1, a2, s6, s8, rab, rcov, r2r4, coefficients
    )
    d = jacfwd

    derivatives = d(f)(coordinates)
    derivatives = np.array(derivatives)

    ref = [
        0.000423636407,
        -0.000013304710,
        -0.000001829666,
        0.000327950989,
        -0.000203052957,
        -0.000007080509,
        0.000288283663,
        0.000134917380,
        0.000000956567,
        0.000095919442,
        -0.000022089153,
        0.000000914397,
        0.000149535193,
        0.000158727869,
        -0.000001062320,
        -0.000427595718,
        0.000012621136,
        -0.000001757827,
        -0.000341842526,
        0.000202703383,
        -0.000001443181,
        -0.000285631580,
        -0.000137238735,
        0.000006350364,
        -0.000094892511,
        0.000017099502,
        -0.000003292373,
        -0.000135363360,
        -0.000150383715,
        0.000008244549,
    ]

    for x_own, x_ref in zip(derivatives, ref):
        assert x_own == pytest.approx(x_ref)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# pyDFTD3 -- Python implementation of Grimme's D3 dispersion correction.
# Copyright (C) 2020 <NAME> and contributors.
#
# This file is part of pyDFTD3.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# For information on the complete list of contributors to the
# pyDFTD3, see: <http://github.com/bobbypaton/pyDFTD3/>

from jax import jacfwd
import numpy as np
import pytest

from dftd3.data import get_r2r4, get_rcov, get_rab, read_coefficients
from dftd3.d3 import d3, d3_bj


def example_parameters():
    coordinates = [
        4.013766952115675,
        -0.42094027389798216,
        0.00413283103637627,
        2.3765970541469086,
        -2.0698869450806012,
        -0.07118787287212185,
        3.2393004941192927,
        2.023463933082604,
        0.03394892984384988,
        6.0906345455037165,
        -0.5931547951632049,
        0.053637986344994996,
        1.4072147949401792,
        2.121187340208405,
        -0.01083379987724973,
        -4.042144969341675,
        0.3453228829877872,
        -0.03309288390901749,
        -2.557736751902669,
        2.1345099093928828,
        -0.013709963040196544,
        -3.055607776368039,
        -2.0196183404172974,
        0.06482138555545441,
        -6.124857485635758,
        0.33042050276242674,
        -0.1200826466422177,
        -1.2231422422377085,
        -1.952500937619412,
        0.14005516206218147,
    ]

    charges = [6, 8, 8, 1, 1, 6, 8, 8, 1, 1]

    rab = get_rab()
    rcov = get_rcov()
    r2r4 = get_r2r4()
    coefficients = read_coefficients()

    return charges, coordinates, rab, rcov, r2r4, coefficients


def test_undifferentiated():
    charges, coordinates, rab, rcov, r2r4, coefficients = example_parameters()

    rs6 = 1.261
    rs8 = 1.0
    s6 = 1.0
    s8 = 1.703
    d3_au = d3(charges, coordinates, rs6, rs8, s6, s8, rab, rcov, r2r4, coefficients)
    assert d3_au == pytest.approx(-0.005259455231125385)


def test_undifferentiated_damping():
    charges, coordinates, rab, rcov, r2r4, coefficients = example_parameters()

    a1 = 0.3981
    a2 = 4.4211
    s6 = 1.0
    s8 = 1.9889
    d3_au = d3_bj(charges, coordinates, a1, a2, s6, s8, rab, rcov, r2r4, coefficients)
    assert d3_au == pytest.approx(-0.009129483884195357)


def test_derivatives():
    charges, coordinates, rab, rcov, r2r4, coefficients = example_parameters()

    rs6 = 1.261
    rs8 = 1.0
    s6 = 1.0
    s8 = 1.703

    f = lambda coordinates: d3(
        charges, coordinates, rs6, rs8, s6, s8, rab, rcov, r2r4, coefficients
    )
    d = jacfwd

    derivatives = d(f)(coordinates)
    derivatives = np.array(derivatives)

    ref = [
        0.000349716853,
        0.000062661785,
        -0.000003311506,
        0.000439833817,
        0.000557690553,
        0.000032711342,
        0.000609561955,
        -0.000303544148,
        -0.000003631289,
        -0.000501863558,
        0.000031792046,
        -0.000013434572,
        -0.000033137414,
        -0.000168498263,
        -0.000008039125,
        -0.000386522043,
        -0.000065823761,
        0.000002757262,
        -0.000310805767,
        -0.000580921020,
        0.000004944208,
        -0.000558542699,
        0.000283457649,
        -0.000014030170,
        0.000506038036,
        0.000017788128,
        0.000021991826,
        -0.000114279180,
        0.000165397030,
        -0.000019957978,
    ]

    for x_own, x_ref in zip(derivatives, ref):
        assert x_own == pytest.approx(x_ref)


def test_derivatives_damping():
    charges, coordinates, rab, rcov, r2r4, coefficients = example_parameters()

    a1 = 0.3981
    a2 = 4.4211
    s6 = 1.0
    s8 = 1.9889

    f = lambda coordinates: d3_bj(
        charges, coordinates, a1, a2, s6, s8, rab, rcov, r2r4, coefficients
    )
    d = jacfwd

    derivatives = d(f)(coordinates)
    derivatives = np.array(derivatives)

    ref = [
        0.000423636407,
        -0.000013304710,
        -0.000001829666,
        0.000327950989,
        -0.000203052957,
        -0.000007080509,
        0.000288283663,
        0.000134917380,
        0.000000956567,
        0.000095919442,
        -0.000022089153,
        0.000000914397,
        0.000149535193,
        0.000158727869,
        -0.000001062320,
        -0.000427595718,
        0.000012621136,
        -0.000001757827,
        -0.000341842526,
        0.000202703383,
        -0.000001443181,
        -0.000285631580,
        -0.000137238735,
        0.000006350364,
        -0.000094892511,
        0.000017099502,
        -0.000003292373,
        -0.000135363360,
        -0.000150383715,
        0.000008244549,
    ]

    for x_own, x_ref in zip(derivatives, ref):
        assert x_own == pytest.approx(x_ref)
#!/usr/bin/env python # -*- coding: utf-8 -*- # # pyDFTD3 -- Python implementation of Grimme's D3 dispersion correction. # Copyright (C) 2020 <NAME> and contributors. # # This file is part of pyDFTD3. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # # For information on the complete list of contributors to the # pyDFTD3, see: <http://github.com/bobbypaton/pyDFTD3/>