line_energy.py 3.67 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

Martin Reinecke's avatar
Martin Reinecke committed
19
20
21
from __future__ import absolute_import, division, print_function
from ..compat import *

22

23
class LineEnergy(object):
24
25
26
27
    """ 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.
28
29
30

    Parameters
    ----------
31
    line_position : float
Martin Reinecke's avatar
Martin Reinecke committed
32
        Defines the full spatial position of this energy via
33
        self.energy.position = zero_point + line_position*line_direction
Jakob Knollmueller's avatar
Jakob Knollmueller committed
34
    energy : Energy
35
36
        The Energy object which will be evaluated along the given direction.
    line_direction : Field
Martin Reinecke's avatar
Martin Reinecke committed
37
38
39
40
41
        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.).
42

Jakob Knollmueller's avatar
Jakob Knollmueller committed
43
44
    Notes
    -----
45
46
47
48
    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.
49
    """
50

51
    def __init__(self, line_position, energy, line_direction, offset=0.):
52
        super(LineEnergy, self).__init__()
53
        self._line_position = float(line_position)
54
        self._line_direction = line_direction
55

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

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

        Parameters
        ----------
68
        line_position : float
69
            Parameter for the new position on the line direction.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
70
71
72

        Returns
        -------
73
            LineEnergy object at new position with same zero point as `self`.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
74
75

        """
76

Martin Reinecke's avatar
Martin Reinecke committed
77
78
        return LineEnergy(line_position, self._energy, self._line_direction,
                          offset=self._line_position)
79
80

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

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

    @property
95
    def directional_derivative(self):
Martin Reinecke's avatar
Martin Reinecke committed
96
        """
Martin Reinecke's avatar
Martin Reinecke committed
97
        float : The directional derivative at the given `position`.
Martin Reinecke's avatar
Martin Reinecke committed
98
99
        """
        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
            from ..logger import logger
Martin Reinecke's avatar
Martin Reinecke committed
102
            logger.warning("directional derivative has non-negligible "
103
                           "imaginary part: {}".format(res))
Martin Reinecke's avatar
Martin Reinecke committed
104
        return res.real