line_energy.py 3.58 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
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
16
17
#
# 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
33
        The Energy object which will be evaluated along the given direction.
    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

Jakob Knollmueller's avatar
Jakob Knollmueller committed
40
41
    Notes
    -----
42
43
44
45
    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.
46
    """
47

48
    def __init__(self, line_position, energy, line_direction, offset=0.):
49
        super(LineEnergy, self).__init__()
50
51
        self._line_position = float(line_position)
        self._line_direction = line_direction
52

Martin Reinecke's avatar
PEP8  
Martin Reinecke committed
53
        if self._line_position == float(offset):
Martin Reinecke's avatar
Martin Reinecke committed
54
            self._energy = energy
55
56
57
        else:
            pos = energy.position \
                + (self._line_position-float(offset))*self._line_direction
Martin Reinecke's avatar
Martin Reinecke committed
58
            self._energy = energy.at(position=pos)
59

60
    def at(self, line_position):
61
        """ Returns LineEnergy at new position, memorizing the zero point.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
62
63
64

        Parameters
        ----------
65
        line_position : float
66
            Parameter for the new position on the line direction.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
67
68
69

        Returns
        -------
70
            LineEnergy object at new position with same zero point as `self`.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
71
72

        """
73

Martin Reinecke's avatar
Martin Reinecke committed
74
75
        return LineEnergy(line_position, self._energy, self._line_direction,
                          offset=self._line_position)
76
77

    @property
Martin Reinecke's avatar
Martin Reinecke committed
78
79
80
81
82
83
    def energy(self):
        """
        Energy
            The underlying Energy object
        """
        return self._energy
Martin Reinecke's avatar
Martin Reinecke committed
84
85

    @property
Martin Reinecke's avatar
Martin Reinecke committed
86
87
88
89
90
91
    def value(self):
        """
        float
            The value of the energy functional at given `position`.
        """
        return self._energy.value
92
93

    @property
94
    def directional_derivative(self):
Martin Reinecke's avatar
Martin Reinecke committed
95
96
97
98
99
        """
        float
            The directional derivative at the given `position`.
        """
        res = self._energy.gradient.vdot(self._line_direction)
100
        if abs(res.imag) / max(abs(res.real), 1.) > 1e-12:
Martin Reinecke's avatar
Martin Reinecke committed
101
102
103
            from ..dobj import mprint
            mprint("directional derivative has non-negligible "
                   "imaginary part:", res)
Martin Reinecke's avatar
Martin Reinecke committed
104
        return res.real