correlated_fields.py 12.4 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Philipp Arras's avatar
Philipp Arras committed
15
# Authors: Philipp Frank, Philipp Arras
Martin Reinecke's avatar
Martin Reinecke committed
16
#
17
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Philipp Arras's avatar
Philipp Arras committed
18

Philipp Arras's avatar
Philipp Arras committed
19 20
import numpy as np
from numpy.testing import assert_allclose
21

Philipp Arras's avatar
Philipp Arras committed
22
from ..domain_tuple import DomainTuple
Philipp Arras's avatar
Philipp Arras committed
23 24
from ..domains.power_space import PowerSpace
from ..domains.unstructured_domain import UnstructuredDomain
Philipp Arras's avatar
Philipp Arras committed
25
from ..extra import check_jacobian_consistency, consistency_check
26
from ..field import Field
Philipp Arras's avatar
Philipp Arras committed
27
from ..operators.adder import Adder
28
from ..operators.contraction_operator import ContractionOperator
Philipp Arras's avatar
Philipp Arras committed
29
from ..operators.distributors import PowerDistributor
Philipp Arras's avatar
Philipp Arras committed
30
from ..operators.endomorphic_operator import EndomorphicOperator
Martin Reinecke's avatar
Martin Reinecke committed
31
from ..operators.harmonic_operators import HarmonicTransformOperator
Philipp Arras's avatar
Philipp Arras committed
32
from ..operators.linear_operator import LinearOperator
Philipp Arras's avatar
Philipp Arras committed
33 34
from ..operators.operator import Operator
from ..operators.simple_linear_operators import VdotOperator, ducktape
Philipp Arras's avatar
Philipp Arras committed
35 36 37
from ..operators.value_inserter import ValueInserter
from ..sugar import from_global_data, from_random, full, makeDomain

38 39
def _lognormal_moments(mean, sig):
    mean, sig = float(mean), float(sig)
Philipp Arras's avatar
Philipp Arras committed
40 41 42
    assert sig > 0
    logsig = np.sqrt(np.log((sig/mean)**2 + 1))
    logmean = np.log(mean) - logsig**2/2
43 44 45 46 47
    return logmean, logsig

def _lognormal_moment_matching(mean, sig, key):
    key = str(key)
    logmean, logsig = _lognormal_moments(mean, sig)
Philipp Arras's avatar
Philipp Arras committed
48 49 50 51 52 53 54
    return _normal(logmean, logsig, key).exp()

def _normal(mean, sig, key):
    return Adder(Field.scalar(mean)) @ (
        sig*ducktape(DomainTuple.scalar_domain(), None, key))


Philipp Frank's avatar
Philipp Frank committed
55 56 57 58
class _SlopeRemover(EndomorphicOperator):
    def __init__(self,domain,logkl):
        self._domain = makeDomain(domain)
        self._sc = logkl / float(logkl[-1])
Philipp Arras's avatar
Philipp Arras committed
59

Philipp Frank's avatar
Philipp Frank committed
60
        self._capability = self.TIMES | self.ADJOINT_TIMES
Philipp Arras's avatar
Philipp Arras committed
61

Philipp Frank's avatar
Philipp Frank committed
62 63 64 65 66 67
    def apply(self,x,mode):
        self._check_input(x,mode)
        x = x.to_global_data()
        if mode == self.TIMES:
            res = x - x[-1] * self._sc
        else:
Philipp Frank's avatar
Philipp Frank committed
68
            res = np.zeros(x.shape,dtype=x.dtype)
Philipp Frank's avatar
Philipp Frank committed
69 70 71 72 73 74 75 76 77 78 79
            res += x
            res[-1] -= (x*self._sc).sum()
        return from_global_data(self._tgt(mode),res)

def _make_slope_Operator(smooth,loglogavgslope):
    tg = smooth.target
    logkl = _log_k_lengths(tg[0])
    assert logkl.shape[0] == tg[0].shape[0] - 1
    logkl -= logkl[0]
    logkl = np.insert(logkl, 0, 0)
    noslope = _SlopeRemover(tg,logkl) @ smooth
Philipp Frank's avatar
Philipp Frank committed
80 81 82
    # FIXME Move to tests
    consistency_check(_SlopeRemover(tg,logkl))

Philipp Frank's avatar
Philipp Frank committed
83
    _t = VdotOperator(from_global_data(tg, logkl)).adjoint
Philipp Frank's avatar
Philipp Frank committed
84
    return _t @ loglogavgslope + noslope
Philipp Arras's avatar
Philipp Arras committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

def _log_k_lengths(pspace):
    return np.log(pspace.k_lengths[1:])

class _TwoLogIntegrations(LinearOperator):
    def __init__(self, target):
        self._target = makeDomain(target)
        self._domain = makeDomain(
            UnstructuredDomain((2, self.target.shape[0] - 2)))
        self._capability = self.TIMES | self.ADJOINT_TIMES
        if not isinstance(self._target[0], PowerSpace):
            raise TypeError
        logk_lengths = _log_k_lengths(self._target[0])
        self._logvol = logk_lengths[1:] - logk_lengths[:-1]

    def apply(self, x, mode):
        self._check_input(x, mode)
        if mode == self.TIMES:
            x = x.to_global_data()
            res = np.empty(self._target.shape)
            res[0] = 0
            res[1] = 0
            res[2:] = np.cumsum(x[1])
            res[2:] = (res[2:] + res[1:-1])/2*self._logvol + x[0]
            res[2:] = np.cumsum(res[2:])
            return from_global_data(self._target, res)
        else:
            x = x.to_global_data_rw()
            res = np.zeros(self._domain.shape)
            x[2:] = np.cumsum(x[2:][::-1])[::-1]
            res[0] += x[2:]
            x[2:] *= self._logvol/2.
117 118
            x[1:-1] += x[2:]
            res[1] += np.cumsum(x[2:][::-1])[::-1]
Philipp Arras's avatar
Philipp Arras committed
119 120 121 122 123 124 125 126 127 128 129 130
            return from_global_data(self._domain, res)


class _Normalization(Operator):
    def __init__(self, domain):
        self._domain = self._target = makeDomain(domain)
        hspace = self._domain[0].harmonic_partner
        pd = PowerDistributor(hspace, power_space=self._domain[0])
        cst = pd.adjoint(full(pd.target, 1.)).to_global_data_rw()
        cst[0] = 0
        self._cst = from_global_data(self._domain, cst)
        self._specsum = _SpecialSum(self._domain)
Philipp Arras's avatar
Philipp Arras committed
131 132
        # FIXME Move to tests
        consistency_check(self._specsum)
Philipp Arras's avatar
Philipp Arras committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

    def apply(self, x):
        self._check_input(x)
        amp = x.exp()
        spec = (2*x).exp()
        # FIXME This normalizes also the zeromode which is supposed to be left
        # untouched by this operator
        return self._specsum(self._cst*spec)**(-0.5)*amp


class _SpecialSum(EndomorphicOperator):
    def __init__(self, domain):
        self._domain = makeDomain(domain)
        self._capability = self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
        self._check_input(x, mode)
        return full(self._tgt(mode), x.sum())


class CorrelatedFieldMaker:
    def __init__(self):
        self._amplitudes = []

    def add_fluctuations_from_ops(self, target, fluctuations, flexibility,
                                  asperity, loglogavgslope, key):
        """
        fluctuations > 0
        flexibility > 0
        asperity > 0
        loglogavgslope probably negative
        """
        assert isinstance(fluctuations, Operator)
        assert isinstance(flexibility, Operator)
        assert isinstance(asperity, Operator)
        assert isinstance(loglogavgslope, Operator)
        target = makeDomain(target)
        assert len(target) == 1
        assert isinstance(target[0], PowerSpace)

        twolog = _TwoLogIntegrations(target)
        dt = twolog._logvol
        sc = np.zeros(twolog.domain.shape)
        sc[0] = sc[1] = np.sqrt(dt)
        sc = from_global_data(twolog.domain, sc)
        expander = VdotOperator(sc).adjoint
        sigmasq = expander @ flexibility

        dist = np.zeros(twolog.domain.shape)
        dist[0] += 1.
        dist = from_global_data(twolog.domain, dist)
        scale = VdotOperator(dist).adjoint @ asperity

        shift = np.ones(scale.target.shape)
        shift[0] = dt**2/12.
        shift = from_global_data(scale.target, shift)
        scale = sigmasq*(Adder(shift) @ scale).sqrt()

        smooth = twolog @ (scale*ducktape(scale.target, None, key))
Philipp Frank's avatar
Philipp Frank committed
192
        smoothslope = _make_slope_Operator(smooth,loglogavgslope)
Philipp Frank's avatar
Philipp Frank committed
193
        #smoothslope = smooth
Philipp Frank's avatar
Philipp Frank committed
194
        
Philipp Arras's avatar
Philipp Arras committed
195 196 197
        # move to tests
        assert_allclose(
            smooth(from_random('normal', smooth.domain)).val[0:2], 0)
Philipp Arras's avatar
Philipp Arras committed
198
        consistency_check(twolog)
Philipp Arras's avatar
Philipp Arras committed
199 200
        check_jacobian_consistency(smooth, from_random('normal',
                                                       smooth.domain))
Philipp Arras's avatar
Philipp Arras committed
201 202
        check_jacobian_consistency(smoothslope,
                                   from_random('normal', smoothslope.domain))
Philipp Arras's avatar
Philipp Arras committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        # end move to tests

        normal_ampl = _Normalization(target) @ smoothslope
        vol = target[0].harmonic_partner.get_default_codomain().total_volume
        arr = np.zeros(target.shape)
        arr[1:] = vol
        expander = VdotOperator(from_global_data(target, arr)).adjoint
        mask = np.zeros(target.shape)
        mask[0] = vol
        adder = Adder(from_global_data(target, mask))
        ampl = adder @ ((expander @ fluctuations)*normal_ampl)

        # Move to tests
        # FIXME This test fails but it is not relevant for the final result
        # assert_allclose(
        #     normal_ampl(from_random('normal', normal_ampl.domain)).val[0], 1)
        assert_allclose(ampl(from_random('normal', ampl.domain)).val[0], vol)
Philipp Arras's avatar
Philipp Arras committed
220 221
        op = _Normalization(target)
        check_jacobian_consistency(op, from_random('normal', op.domain))
Philipp Arras's avatar
Philipp Arras committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        # End move to tests

        self._amplitudes.append(ampl)

    def add_fluctuations(self, target, fluctuations_mean, fluctuations_stddev,
                         flexibility_mean, flexibility_stddev, asperity_mean,
                         asperity_stddev, loglogavgslope_mean,
                         loglogavgslope_stddev, prefix):
        fluctuations_mean = float(fluctuations_mean)
        fluctuations_stddev = float(fluctuations_stddev)
        flexibility_mean = float(flexibility_mean)
        flexibility_stddev = float(flexibility_stddev)
        asperity_mean = float(asperity_mean)
        asperity_stddev = float(asperity_stddev)
        loglogavgslope_mean = float(loglogavgslope_mean)
        loglogavgslope_stddev = float(loglogavgslope_stddev)
        prefix = str(prefix)
        assert fluctuations_stddev > 0
        assert fluctuations_mean > 0
        assert flexibility_stddev > 0
        assert flexibility_mean > 0
        assert asperity_stddev > 0
        assert asperity_mean > 0
        assert loglogavgslope_stddev > 0

        fluct = _lognormal_moment_matching(fluctuations_mean,
                                           fluctuations_stddev,
                                           prefix + 'fluctuations')
        flex = _lognormal_moment_matching(flexibility_mean, flexibility_stddev,
                                          prefix + 'flexibility')
        asp = _lognormal_moment_matching(asperity_mean, asperity_stddev,
                                         prefix + 'asperity')
254
        avgsl = _normal(loglogavgslope_mean, loglogavgslope_stddev,
Philipp Arras's avatar
Philipp Arras committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
                        prefix + 'loglogavgslope')
        self.add_fluctuations_from_ops(target, fluct, flex, asp, avgsl,
                                       prefix + 'spectrum')

    def finalize_from_op(self, zeromode):
        raise NotImplementedError

    def finalize(self,
                 offset_amplitude_mean,
                 offset_amplitude_stddev,
                 prefix,
                 offset=None):
        """
        offset vs zeromode: volume factor
        """
        offset_amplitude_stddev = float(offset_amplitude_stddev)
        offset_amplitude_mean = float(offset_amplitude_mean)
        assert offset_amplitude_stddev > 0
        assert offset_amplitude_mean > 0
        if offset is not None:
            offset = float(offset)
        hspace = makeDomain(
            [dd.target[0].harmonic_partner for dd in self._amplitudes])

        azm = _lognormal_moment_matching(offset_amplitude_mean,
                                         offset_amplitude_stddev,
                                         prefix + 'zeromode')
        foo = np.ones(hspace.shape)
        zeroind = len(hspace.shape)*(0,)
        foo[zeroind] = 0
        azm = Adder(from_global_data(hspace, foo)) @ ValueInserter(
            hspace, zeroind) @ azm

        ht = HarmonicTransformOperator(hspace, space=0)
        pd = PowerDistributor(hspace, self._amplitudes[0].target[0], 0)
        for i in range(1, len(self._amplitudes)):
            ht = HarmonicTransformOperator(ht.target, space=i) @ ht
            pd = pd @ PowerDistributor(
                pd.domain, self._amplitudes[i].target[0], space=i)

        spaces = tuple(range(len(self._amplitudes)))
        a = ContractionOperator(pd.domain,
                                spaces[1:]).adjoint(self._amplitudes[0])
        for i in range(1, len(self._amplitudes)):
            a = a*(ContractionOperator(pd.domain, spaces[:i] + spaces[
                (i + 1):]).adjoint(self._amplitudes[i]))

        A = pd @ a
        return ht(azm*A*ducktape(hspace, None, prefix + 'xi'))

    @property
    def amplitudes(self):
        return self._amplitudes
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

    def effective_total_fluctuation(self,fluctuations_means,
                                    fluctuations_stddevs,
                                    nsamples = 100):
        namps = len(fluctuations_means)
        xis = np.random.normal(size=namps*nsamples).reshape((namps,nsamples))
        
        q=np.ones(nsamples)
        for i in range(len(fluctuations_means)):
            m, sig = _lognormal_moments(fluctuations_means[i],
                                        fluctuations_stddevs[i])
            f = np.exp(m + sig*xis[i])
            q *= (1.+ f**2)
        q = np.sqrt(q-1.)
        return np.mean(q), np.std(q)