correlated_fields_simple.py 6.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 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-2020 Max-Planck-Society
# Authors: Philipp Arras
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import numpy as np

from ..domains.power_space import PowerSpace
from ..domains.unstructured_domain import UnstructuredDomain
from ..operators.adder import Adder
from ..operators.contraction_operator import ContractionOperator
from ..operators.distributors import PowerDistributor
from ..operators.harmonic_operators import HarmonicTransformOperator
from ..operators.linear_operator import LinearOperator
Philipp Arras's avatar
Philipp Arras committed
28
from ..operators.normal_operators import LognormalTransform, NormalTransform
29 30 31
from ..operators.operator import Operator
from ..operators.simple_linear_operators import ducktape
from ..sugar import full, makeDomain, makeField, makeOp
Philipp Arras's avatar
Philipp Arras committed
32
from .correlated_fields import (_log_vol, _Normalization,
Philipp Arras's avatar
Philipp Arras committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
                                _relative_log_k_lengths, _SlopeRemover)


class _SimpleTwoLogIntegrations(LinearOperator):
    def __init__(self, target):
        self._target = makeDomain(target)
        assert len(self._target) == 1
        tgt = self._target[0]
        assert isinstance(tgt, PowerSpace)
        self._domain = makeDomain(UnstructuredDomain((2, tgt.shape[0] - 2)))
        self._log_vol = _log_vol(tgt)
        self._capability = self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
        self._check_input(x, mode)
        from_third = slice(2, None)
        no_border = slice(1, -1)
        reverse = slice(None, None, -1)
        if mode == self.TIMES:
            x = x.val
            res = np.empty(self._target.shape)
            res[0] = res[1] = 0
            res[from_third] = np.cumsum(x[1])
            res[from_third] = (res[from_third] +
                               res[no_border])/2*self._log_vol + x[0]
            res[from_third] = np.cumsum(res[from_third])
        else:
            x = x.val_rw()
            res = np.zeros(self._domain.shape)
            x[from_third] = np.cumsum(x[from_third][reverse])[reverse]
            res[0] += x[from_third]
            x[from_third] *= self._log_vol/2.
            x[no_border] += x[from_third]
            res[1] += np.cumsum(x[from_third][reverse])[reverse]
        return makeField(self._tgt(mode), res)


Philipp Arras's avatar
Philipp Arras committed
70
class SimpleCorrelatedField(Operator):
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    def __init__(self,
                 target,
                 offset_mean,
                 offset_std_mean,
                 offset_std_std,
                 fluctuations_mean,
                 fluctuations_stddev,
                 flexibility_mean,
                 flexibility_stddev,
                 asperity_mean,
                 asperity_stddev,
                 loglogavgslope_mean,
                 loglogavgslope_stddev,
                 prefix='',
                 harmonic_partner=None):
        if harmonic_partner is None:
            harmonic_partner = target.get_default_codomain()
        else:
            target.check_codomain(harmonic_partner)
            harmonic_partner.check_codomain(target)
        fluct = LognormalTransform(fluctuations_mean, fluctuations_stddev,
                                   prefix + 'fluctuations', 0)
        flex = LognormalTransform(flexibility_mean, flexibility_stddev,
                                  prefix + 'flexibility', 0)
        asp = LognormalTransform(asperity_mean, asperity_stddev,
                                 prefix + 'asperity', 0)
        avgsl = NormalTransform(loglogavgslope_mean, loglogavgslope_stddev,
                                prefix + 'loglogavgslope', 0)
        zm = LognormalTransform(offset_std_mean, offset_std_std,
                                prefix + 'zeromode', 0)
Philipp Arras's avatar
Philipp Arras committed
101 102 103 104 105 106 107 108 109 110 111 112

        tgt = PowerSpace(harmonic_partner)
        twolog = _SimpleTwoLogIntegrations(tgt)
        dom = twolog.domain[0]
        vflex = np.zeros(dom.shape)
        vasp = np.zeros(dom.shape, dtype=np.float64)
        shift = np.ones(dom.shape, dtype=np.float64)
        vflex[0] = vflex[1] = np.sqrt(_log_vol(tgt))
        vasp[0] = 1
        shift[0] = _log_vol(tgt)**2/12.
        vflex = makeOp(makeField(dom, vflex))
        vasp = makeOp(makeField(dom, vasp))
Philipp Arras's avatar
Philipp Arras committed
113
        shift = makeField(dom, shift)
Philipp Arras's avatar
Philipp Arras committed
114
        vslope = makeOp(makeField(tgt, _relative_log_k_lengths(tgt)))
Philipp Arras's avatar
Philipp Arras committed
115 116

        expander = ContractionOperator(twolog.domain, 0).adjoint
Philipp Arras's avatar
Philipp Arras committed
117
        ps_expander = ContractionOperator(tgt, 0).adjoint
Philipp Arras's avatar
Philipp Arras committed
118
        h_expander = ContractionOperator(harmonic_partner, 0).adjoint
Philipp Arras's avatar
Philipp Arras committed
119 120 121 122
        slope = vslope @ ps_expander @ avgsl
        sig_flex = vflex @ expander @ flex
        sig_asp = vasp @ expander @ asp
        xi = ducktape(dom, None, prefix + 'spectrum')
Philipp Arras's avatar
Philipp Arras committed
123 124
        smooth = xi*sig_flex*(Adder(shift) @ sig_asp).ptw("sqrt")
        smooth = _SlopeRemover(tgt, 0) @ twolog @ smooth
Philipp Arras's avatar
Philipp Arras committed
125
        op = _Normalization(tgt, 0) @ (slope + smooth)
Philipp Arras's avatar
Philipp Arras committed
126 127 128 129 130 131 132

        vol0 = np.zeros(tgt.shape, dtype=np.float64)
        vol1 = np.zeros(tgt.shape, dtype=np.float64)
        vol1[1:] = vol0[0] = target.total_volume
        vol = Adder(makeField(tgt, vol0)) @ makeOp(makeField(tgt, vol1))

        amp = vol @ ((ps_expander @ (zm.ptw("reciprocal")*fluct))*op)
Philipp Arras's avatar
Philipp Arras committed
133
        self._a = amp
Philipp Arras's avatar
Philipp Arras committed
134

135 136
        ht = HarmonicTransformOperator(harmonic_partner, target)
        pd = PowerDistributor(harmonic_partner, amp.target[0])
Philipp Arras's avatar
Philipp Arras committed
137
        xi = ducktape(harmonic_partner, None, prefix + 'xi')
Philipp Arras's avatar
Philipp Arras committed
138
        op = ht(h_expander(zm)*pd(amp)*xi*(1))
139
        if offset_mean is not None:
Philipp Arras's avatar
Philipp Arras committed
140 141 142 143
            op = Adder(full(op.target, float(offset_mean))) @ op
        self.apply = op.apply
        self._domain = op.domain
        self._target = op.target
Philipp Arras's avatar
Philipp Arras committed
144 145 146 147

    @property
    def normalized_amplitudes(self):
        return [self._a]