energy_tests.py 3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
20
from ..sugar import from_random
21

Martin Reinecke's avatar
Martin Reinecke committed
22
23
__all__ = ["check_value_gradient_consistency",
           "check_value_gradient_curvature_consistency"]
24
25


Martin Reinecke's avatar
Martin Reinecke committed
26
def _get_acceptable_energy(E):
27
28
    val = E.value
    if not np.isfinite(val):
29
        raise ValueError
30
    dir = from_random("normal", E.position.domain)
31
32
    dirder = E.gradient.vdot(dir)
    dir *= np.abs(val)/np.abs(dirder)*1e-5
Martin Reinecke's avatar
Martin Reinecke committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    # find a step length that leads to a "reasonable" energy
    for i in range(50):
        try:
            E2 = E.at(E.position+dir)
            if np.isfinite(E2.value) and abs(E2.value) < 1e20:
                break
        except FloatingPointError:
            pass
        dir *= 0.5
    else:
        raise ValueError("could not find a reasonable initial step")
    return E2


def check_value_gradient_consistency(E, tol=1e-6, ntries=100):
48
    for _ in range(ntries):
Martin Reinecke's avatar
Martin Reinecke committed
49
        E2 = _get_acceptable_energy(E)
50
        val = E.value
Martin Reinecke's avatar
Martin Reinecke committed
51
        dir = E2.position - E.position
Philipp Arras's avatar
Philipp Arras committed
52
        # Enext = E2
Martin Reinecke's avatar
Martin Reinecke committed
53
        dirnorm = dir.norm()
54
        for i in range(50):
Martin Reinecke's avatar
Martin Reinecke committed
55
56
            Emid = E.at(E.position + 0.5*dir)
            dirder = Emid.gradient.vdot(dir)/dirnorm
Martin Reinecke's avatar
Martin Reinecke committed
57
58
59
            t1 = (E2.value-val)/dirnorm
            xtol = tol*max(abs(t1), abs(dirder))
            if abs(t1-dirder) < xtol:
Martin Reinecke's avatar
Martin Reinecke committed
60
                break
61
            dir *= 0.5
Martin Reinecke's avatar
Martin Reinecke committed
62
            dirnorm *= 0.5
Martin Reinecke's avatar
Martin Reinecke committed
63
            E2 = Emid
64
        else:
Martin Reinecke's avatar
Martin Reinecke committed
65
66
67
68
69
70
71
            raise ValueError("gradient and value seem inconsistent")
        # E = Enext


def check_value_gradient_curvature_consistency(E, tol=1e-6, ntries=100):
    for _ in range(ntries):
        E2 = _get_acceptable_energy(E)
72
        val = E.value
Martin Reinecke's avatar
Martin Reinecke committed
73
        dir = E2.position - E.position
Philipp Arras's avatar
Philipp Arras committed
74
        # Enext = E2
Martin Reinecke's avatar
Martin Reinecke committed
75
        dirnorm = dir.norm()
76
        for i in range(50):
77
78
79
80
            Emid = E.at(E.position + 0.5*dir)
            dirder = Emid.gradient.vdot(dir)/dirnorm
            dgrad = Emid.curvature(dir)/dirnorm
            if abs((E2.value-val)/dirnorm-dirder) < tol and \
Martin Reinecke's avatar
Martin Reinecke committed
81
               (abs((E2.gradient-E.gradient)/dirnorm-dgrad) < tol).all():
82
83
                break
            dir *= 0.5
Martin Reinecke's avatar
Martin Reinecke committed
84
            dirnorm *= 0.5
85
            E2 = Emid
86
        else:
Martin Reinecke's avatar
Martin Reinecke committed
87
88
            raise ValueError("gradient, value and curvature seem inconsistent")
        # E = Enext