energy_sum.py 2.55 KB
 Martin Reinecke committed May 30, 2018 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 . # # 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. `````` Martin Reinecke committed Jul 01, 2018 19 20 ``````from builtins import * from ..utilities import memo, my_lincomb_simple, my_lincomb `````` Philipp Arras committed Jun 28, 2018 21 ``````from .energy import Energy `````` Martin Reinecke committed May 30, 2018 22 23 24 `````` class EnergySum(Energy): `````` 25 `````` def __init__(self, position, energies, factors): `````` Martin Reinecke committed May 30, 2018 26 `````` super(EnergySum, self).__init__(position=position) `````` 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 `````` 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) `````` Martin Reinecke committed May 30, 2018 52 53 `````` def at(self, position): `````` 54 `````` return self.__class__(position, self._energies, self._factors) `````` Martin Reinecke committed May 30, 2018 55 56 57 58 `````` @property @memo def value(self): `````` Martin Reinecke committed Jul 01, 2018 59 60 `````` return my_lincomb_simple(map(lambda v: v.value, self._energies), self._factors) `````` Martin Reinecke committed May 30, 2018 61 62 63 64 `````` @property @memo def gradient(self): `````` Martin Reinecke committed Jul 01, 2018 65 66 `````` return my_lincomb(map(lambda v: v.gradient, self._energies), self._factors).lock() `````` Martin Reinecke committed May 30, 2018 67 68 69 `````` @property @memo `````` Martin Reinecke committed Jul 03, 2018 70 71 `````` def metric(self): return my_lincomb(map(lambda v: v.metric, self._energies), `````` Martin Reinecke committed Jul 01, 2018 72 `` self._factors)``