extra.py 5.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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.

19
from __future__ import absolute_import, division, print_function
Philipp Arras's avatar
Philipp Arras committed
20

21
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
22 23

from ..compat import *
Martin Reinecke's avatar
Martin Reinecke committed
24
from ..field import Field
Martin Reinecke's avatar
Martin Reinecke committed
25
from ..linearization import Linearization
Philipp Arras's avatar
Philipp Arras committed
26
from ..sugar import from_random
27

Martin Reinecke's avatar
Martin Reinecke committed
28
__all__ = ["consistency_check", "check_value_gradient_consistency",
Martin Reinecke's avatar
Martin Reinecke committed
29
           "check_value_gradient_metric_consistency"]
30

Philipp Arras's avatar
Philipp Arras committed
31

Martin Reinecke's avatar
Martin Reinecke committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
def _assert_allclose(f1, f2, atol, rtol):
    if isinstance(f1, Field):
        return np.testing.assert_allclose(f1.local_data, f2.local_data,
                                          atol=atol, rtol=rtol)
    for key, val in f1.items():
        _assert_allclose(val, f2[key], atol=atol, rtol=rtol)


def _adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol):
    needed_cap = op.TIMES | op.ADJOINT_TIMES
    if (op.capability & needed_cap) != needed_cap:
        return
    f1 = from_random("normal", op.domain, dtype=domain_dtype)
    f2 = from_random("normal", op.target, dtype=target_dtype)
    res1 = f1.vdot(op.adjoint_times(f2))
    res2 = op.times(f1).vdot(f2)
    np.testing.assert_allclose(res1, res2, atol=atol, rtol=rtol)


def _inverse_implementation(op, domain_dtype, target_dtype, atol, rtol):
    needed_cap = op.TIMES | op.INVERSE_TIMES
    if (op.capability & needed_cap) != needed_cap:
        return
    foo = from_random("normal", op.target, dtype=target_dtype)
    res = op(op.inverse_times(foo))
    _assert_allclose(res, foo, atol=atol, rtol=rtol)

    foo = from_random("normal", op.domain, dtype=domain_dtype)
    res = op.inverse_times(op(foo))
    _assert_allclose(res, foo, atol=atol, rtol=rtol)


def _full_implementation(op, domain_dtype, target_dtype, atol, rtol):
    _adjoint_implementation(op, domain_dtype, target_dtype, atol, rtol)
    _inverse_implementation(op, domain_dtype, target_dtype, atol, rtol)


def consistency_check(op, domain_dtype=np.float64, target_dtype=np.float64,
                      atol=0, rtol=1e-7):
    _full_implementation(op, domain_dtype, target_dtype, atol, rtol)
    _full_implementation(op.adjoint, target_dtype, domain_dtype, atol, rtol)
    _full_implementation(op.inverse, target_dtype, domain_dtype, atol, rtol)
    _full_implementation(op.adjoint.inverse, domain_dtype, target_dtype, atol,
                         rtol)


Martin Reinecke's avatar
Martin Reinecke committed
78
def _get_acceptable_location(op, loc, lin):
Martin Reinecke's avatar
Martin Reinecke committed
79
    if not np.isfinite(lin.val.sum()):
Martin Reinecke's avatar
Martin Reinecke committed
80 81 82 83
        raise ValueError('Initial value must be finite')
    dir = from_random("normal", loc.domain)
    dirder = lin.jac(dir)
    if dirder.norm() == 0:
Martin Reinecke's avatar
Martin Reinecke committed
84
        dir = dir * (lin.val.norm()*1e-5)
Martin Reinecke's avatar
Martin Reinecke committed
85
    else:
Martin Reinecke's avatar
Martin Reinecke committed
86
        dir = dir * (lin.val.norm()*1e-5/dirder.norm())
Martin Reinecke's avatar
Martin Reinecke committed
87 88 89 90
    # Find a step length that leads to a "reasonable" location
    for i in range(50):
        try:
            loc2 = loc+dir
91
            lin2 = op(Linearization.make_var(loc2, lin.want_metric))
Martin Reinecke's avatar
Martin Reinecke committed
92 93 94 95 96 97 98 99 100
            if np.isfinite(lin2.val.sum()) and abs(lin2.val.sum()) < 1e20:
                break
        except FloatingPointError:
            pass
        dir = dir*0.5
    else:
        raise ValueError("could not find a reasonable initial step")
    return loc2, lin2

Martin Reinecke's avatar
Martin Reinecke committed
101

Martin Reinecke's avatar
Martin Reinecke committed
102
def _check_consistency(op, loc, tol, ntries, do_metric):
Martin Reinecke's avatar
Martin Reinecke committed
103
    for _ in range(ntries):
104
        lin = op(Linearization.make_var(loc, do_metric))
Martin Reinecke's avatar
Martin Reinecke committed
105
        loc2, lin2 = _get_acceptable_location(op, loc, lin)
Martin Reinecke's avatar
Martin Reinecke committed
106
        dir = loc2-loc
Martin Reinecke's avatar
Martin Reinecke committed
107 108 109 110
        locnext = loc2
        dirnorm = dir.norm()
        for i in range(50):
            locmid = loc + 0.5*dir
111
            linmid = op(Linearization.make_var(locmid, do_metric))
Martin Reinecke's avatar
Martin Reinecke committed
112 113
            dirder = linmid.jac(dir)
            numgrad = (lin2.val-lin.val)
Martin Reinecke's avatar
Martin Reinecke committed
114
            xtol = tol * dirder.norm() / np.sqrt(dirder.size)
Martin Reinecke's avatar
Martin Reinecke committed
115 116
            cond = (abs(numgrad-dirder) <= xtol).all()
            if do_metric:
Martin Reinecke's avatar
Martin Reinecke committed
117 118
                dgrad = linmid.metric(dir)
                dgrad2 = (lin2.gradient-lin.gradient)
Martin Reinecke's avatar
Martin Reinecke committed
119 120
                cond = cond and (abs(dgrad-dgrad2) <= xtol).all()
            if cond:
Martin Reinecke's avatar
Martin Reinecke committed
121 122 123
                break
            dir = dir*0.5
            dirnorm *= 0.5
Martin Reinecke's avatar
Martin Reinecke committed
124
            loc2, lin2 = locmid, linmid
Martin Reinecke's avatar
Martin Reinecke committed
125 126 127
        else:
            raise ValueError("gradient and value seem inconsistent")
        loc = locnext
Martin Reinecke's avatar
Martin Reinecke committed
128 129


Martin Reinecke's avatar
Martin Reinecke committed
130 131 132 133
def check_value_gradient_consistency(op, loc, tol=1e-8, ntries=100):
    _check_consistency(op, loc, tol, ntries, False)


Martin Reinecke's avatar
Martin Reinecke committed
134
def check_value_gradient_metric_consistency(op, loc, tol=1e-8, ntries=100):
Martin Reinecke's avatar
Martin Reinecke committed
135
    _check_consistency(op, loc, tol, ntries, True)