energy_sum.py 2.61 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
20

21
from ..compat import *
22
from ..utilities import memo, my_lincomb, my_lincomb_simple
Philipp Arras's avatar
Philipp Arras committed
23
from .energy import Energy
24
25
26


class EnergySum(Energy):
27
    def __init__(self, position, energies, factors):
28
        super(EnergySum, self).__init__(position=position)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        self._energies = tuple(e.at(position) for e in energies)
        self._factors = tuple(factors)

    @staticmethod
    def make(energies, factors=None):
        if factors is None:
            factors = (1,)*len(energies)
        # unpack energies
        eout = []
        fout = []
        EnergySum._unpackEnergies(energies, factors, 1., eout, fout)
        for e in eout[1:]:
            if not e.position.isEquivalentTo(eout[0].position):
                raise ValueError("position mismatch")
        return EnergySum(eout[0].position, eout, fout)

    @staticmethod
    def _unpackEnergies(e_in, f_in, prefactor, e_out, f_out):
        for e, f in zip(e_in, f_in):
            if isinstance(e, EnergySum):
                EnergySum._unpackEnergies(e._energies, e._factors, prefactor*f,
                                          e_out, f_out)
            else:
                e_out.append(e)
                f_out.append(prefactor*f)
54
55

    def at(self, position):
56
        return self.__class__(position, self._energies, self._factors)
57
58
59
60

    @property
    @memo
    def value(self):
61
62
        return my_lincomb_simple(map(lambda v: v.value, self._energies),
                                 self._factors)
63
64
65
66

    @property
    @memo
    def gradient(self):
67
        return my_lincomb(map(lambda v: v.gradient, self._energies),
68
                          self._factors)
69
70
71

    @property
    @memo
Martin Reinecke's avatar
Martin Reinecke committed
72
73
    def metric(self):
        return my_lincomb(map(lambda v: v.metric, self._energies),
74
                          self._factors)