line_energy.py 4.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13 14 15 16 17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18 19


20
class LineEnergy(object):
21 22 23 24
    """ Evaluates an underlying Energy along a certain line direction.

    Given an Energy class and a line direction, its position is parametrized by
    a scalar step size along the descent direction relative to a zero point.
25 26 27

    Parameters
    ----------
28
    line_position : float
Martin Reinecke's avatar
Martin Reinecke committed
29
        Defines the full spatial position of this energy via
30
        self.energy.position = zero_point + line_position*line_direction
Jakob Knollmueller's avatar
Jakob Knollmueller committed
31
    energy : Energy
32
        The Energy object which will be evaluated along the given direction.
33
    line_direction : Field
Martin Reinecke's avatar
Martin Reinecke committed
34 35 36 37 38
        Direction used for line evaluation. Does not have to be normalized.
    offset :  float *optional*
        Indirectly defines the zero point of the line via the equation
        energy.position = zero_point + offset*line_direction
        (default : 0.).
39 40 41

    Attributes
    ----------
42
    line_position : float
43
        The position along the given line direction relative to the zero point.
44
    value : float
Martin Reinecke's avatar
Martin Reinecke committed
45
        The value of the energy functional at the given position
46
    directional_derivative : float
Martin Reinecke's avatar
Martin Reinecke committed
47
        The directional derivative at the given position
48
    line_direction : Field
49 50
        Direction along which the movement is restricted. Does not have to be
        normalized.
51
    energy : Energy
Martin Reinecke's avatar
Martin Reinecke committed
52
        The underlying Energy at the given position
53

Jakob Knollmueller's avatar
Jakob Knollmueller committed
54 55 56 57
    Raises
    ------
    NotImplementedError
        Raised if
58 59
            * value, gradient or curvature of the attribute energy are not
              implemented.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
60 61 62

    Notes
    -----
63 64 65 66
    The LineEnergy is used in minimization schemes in order perform line
    searches. It describes an underlying Energy which is restricted along one
    direction, only requiring the step size parameter to determine a new
    position.
67 68

    """
69

70
    def __init__(self, line_position, energy, line_direction, offset=0.):
71
        super(LineEnergy, self).__init__()
72 73
        self._line_position = float(line_position)
        self._line_direction = line_direction
74

75 76 77 78 79 80
        if self._line_position==float(offset):
            self.energy = energy
        else:
            pos = energy.position \
                + (self._line_position-float(offset))*self._line_direction
            self.energy = energy.at(position=pos)
81

82
    def at(self, line_position):
83
        """ Returns LineEnergy at new position, memorizing the zero point.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
84 85 86

        Parameters
        ----------
87
        line_position : float
88
            Parameter for the new position on the line direction.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
89 90 91

        Returns
        -------
92
            LineEnergy object at new position with same zero point as `self`.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
93 94

        """
95

96
        return self.__class__(line_position,
97
                              self.energy,
98 99
                              self.line_direction,
                              offset=self.line_position)
100 101 102 103 104 105

    @property
    def value(self):
        return self.energy.value

    @property
106 107
    def line_position(self):
        return self._line_position
Martin Reinecke's avatar
Martin Reinecke committed
108 109

    @property
110 111
    def line_direction(self):
        return self._line_direction
112 113

    @property
114 115 116 117 118
    def directional_derivative(self):
        res = self.energy.gradient.vdot(self.line_direction)
        if abs(res.imag) / max(abs(res.real), 1.) > 1e-12:
            print "directional derivative has non-negligible " \
                  "imaginary part:", res
Martin Reinecke's avatar
Martin Reinecke committed
119
        return res.real