energy.py 3.71 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
from ..utilities import memo, NiftyMetaBase
20

21

Martin Reinecke's avatar
Martin Reinecke committed
22
class Energy(NiftyMetaBase()):
23
    """ Provides the functional used by minimization schemes.
24

25 26
   The Energy object is an implementation of a scalar function including its
   gradient and curvature at some position.
27 28 29

    Parameters
    ----------
30 31
    position : Field
        The input parameter of the scalar function.
32 33 34

    Notes
    -----
35 36 37 38 39 40
    An instance of the Energy class is defined at a certain location. If one
    is interested in the value, gradient or curvature of the abstract energy
    functional one has to 'jump' to the new position using the `at` method.
    This method returns a new energy instance residing at the new position. By
    this approach, intermediate results from computing e.g. the gradient can
    safely be reused for e.g. the value or the curvature.
41

42 43 44
    Memorizing the evaluations of some quantities (using the memo decorator)
    minimizes the computational effort for multiple calls.

Martin Reinecke's avatar
Martin Reinecke committed
45
    See Also
46 47
    --------
    memo
48 49

    """
50

51
    def __init__(self, position):
52
        super(Energy, self).__init__()
Martin Reinecke's avatar
Martin Reinecke committed
53
        self._position = position.lock()
54 55

    def at(self, position):
Martin Reinecke's avatar
Martin Reinecke committed
56
        """ Returns a new Energy object, initialized at `position`.
57 58 59 60

        Parameters
        ----------
        position : Field
Martin Reinecke's avatar
Martin Reinecke committed
61
            Location in parameter space for the new Energy object.
62 63 64

        Returns
        -------
Martin Reinecke's avatar
Martin Reinecke committed
65
        Energy
66 67
            Energy object at new position.
        """
68 69
        return self.__class__(position)

70 71
    @property
    def position(self):
72
        """
Martin Reinecke's avatar
Martin Reinecke committed
73 74 75 76
        Field : selected location in parameter space.

        The Field location in parameter space where value, gradient and
        curvature are evaluated.
77
        """
78 79
        return self._position

80 81
    @property
    def value(self):
82
        """
Martin Reinecke's avatar
Martin Reinecke committed
83 84
        float : value of the functional.

Martin Reinecke's avatar
Martin Reinecke committed
85
            The value of the energy functional at given `position`.
86
        """
87 88 89 90
        raise NotImplementedError

    @property
    def gradient(self):
91
        """
Martin Reinecke's avatar
Martin Reinecke committed
92
        Field : The gradient at given `position`.
93
        """
94 95
        raise NotImplementedError

Martin Reinecke's avatar
Martin Reinecke committed
96 97 98 99
    @property
    @memo
    def gradient_norm(self):
        """
Martin Reinecke's avatar
Martin Reinecke committed
100
        float : L2-norm of the gradient at given `position`.
Martin Reinecke's avatar
Martin Reinecke committed
101 102 103
        """
        return self.gradient.norm()

104 105
    @property
    def curvature(self):
106
        """
Martin Reinecke's avatar
Martin Reinecke committed
107
        LinearOperator : implicitly defined curvature.
Martin Reinecke's avatar
Martin Reinecke committed
108 109
            A positive semi-definite operator or function describing the
            curvature of the potential at the given `position`.
110
        """
111
        raise NotImplementedError
Martin Reinecke's avatar
stage 1  
Martin Reinecke committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    def longest_step(self, dir):
        """Returns the longest allowed step size along `dir`

        Parameters
        ----------
        dir : Field
            the search direction

        Returns
        -------
        float or None
            the longest allowed step when starting from `self.position` along
            `dir`. If None, the step size is not limited.
        """
        return None