fft_operator.py 9.03 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
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# 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
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
20
21
from ..domain_tuple import DomainTuple
from ..spaces.rg_space import RGSpace
Martin Reinecke's avatar
Martin Reinecke committed
22
from .linear_operator import LinearOperator
Martin Reinecke's avatar
Martin Reinecke committed
23
24
25
26
from .. import dobj
from .. import utilities
from ..field import Field
from ..spaces.gl_space import GLSpace
Jait Dixit's avatar
Jait Dixit committed
27
28


Jait Dixit's avatar
Jait Dixit committed
29
class FFTOperator(LinearOperator):
30
31
32
    """Transforms between a pair of position and harmonic domains.

    Built-in domain pairs are
Martin Reinecke's avatar
Martin Reinecke committed
33

34
35
36
      - a harmonic and a non-harmonic RGSpace (with matching distances)
      - a HPSpace and a LMSpace
      - a GLSpace and a LMSpace
Martin Reinecke's avatar
Martin Reinecke committed
37

38
39
    Within a domain pair, both orderings are possible.

Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
43
44
    For RGSpaces, the operator provides the full set of operations.
    For the sphere-related domains, it only supports the transform from
    harmonic to position space and its adjoint; if the operator domain is
    harmonic, this will be times() and adjoint_times(), otherwise
    inverse_times() and adjoint_inverse_times()
45
46
47
48
49
50

    Parameters
    ----------
    domain: Space or single-element tuple of Spaces
        The domain of the data that is input by "times" and output by
        "adjoint_times".
51
52
    space: the index of the space on which the operator should act
        If None, it is set to 0 if domain contains exactly one space
53
    target: Space or single-element tuple of Spaces (optional)
54
55
56
57
        The domain of the data that is output by "times" and input by
        "adjoint_times".
        If omitted, a co-domain will be chosen automatically.
        Whenever "domain" is an RGSpace, the codomain (and its parameters) are
Martin Reinecke's avatar
Martin Reinecke committed
58
        uniquely determined.
59
60
        For GLSpace, HPSpace, and LMSpace, a sensible (but not unique)
        co-domain is chosen that should work satisfactorily in most situations,
Martin Reinecke's avatar
Martin Reinecke committed
61
        but for full control, the user should explicitly specify a codomain.
62
    """
63

64
65
    def __init__(self, domain, target=None, space=None):
        super(FFTOperator, self).__init__()
66
67

        # Initialize domain and target
Martin Reinecke's avatar
Martin Reinecke committed
68
        self._domain = DomainTuple.make(domain)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
69
        self._space = utilities.infer_space(self._domain, space)
70

Martin Reinecke's avatar
Martin Reinecke committed
71
        adom = self._domain[self._space]
72
        if target is None:
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
73
            target = adom.get_default_codomain()
74

Martin Reinecke's avatar
Martin Reinecke committed
75
        self._target = [dom for dom in self._domain]
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
76
77
78
79
        self._target[self._space] = target
        self._target = DomainTuple.make(self._target)
        adom.check_codomain(target)
        target.check_codomain(adom)
Jait Dixit's avatar
Jait Dixit committed
80

Martin Reinecke's avatar
Martin Reinecke committed
81
82
83
84
85
        if isinstance(adom, RGSpace):
            self._applyfunc = self._apply_cartesian
            self._capability = self._all_ops
            import pyfftw
            pyfftw.interfaces.cache.enable()
Martin Reinecke's avatar
Martin Reinecke committed
86
        else:
Martin Reinecke's avatar
Martin Reinecke committed
87
88
89
90
            from pyHealpix import sharpjob_d
            self._applyfunc = self._apply_spherical
            hspc = adom if adom.harmonic else target
            pspc = target if adom.harmonic else adom
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
91
92
            self.lmax = hspc.lmax
            self.mmax = hspc.mmax
Martin Reinecke's avatar
Martin Reinecke committed
93
94
95
96
97
98
99
100
101
102
103
            self.sjob = sharpjob_d()
            self.sjob.set_triangular_alm_info(self.lmax, self.mmax)
            if isinstance(pspc, GLSpace):
                self.sjob.set_Gauss_geometry(pspc.nlat, pspc.nlon)
            else:
                self.sjob.set_Healpix_geometry(pspc.nside)
            if adom.harmonic:
                self._capability = self.TIMES | self.ADJOINT_TIMES
            else:
                self._capability = (self.INVERSE_TIMES |
                                    self.INVERSE_ADJOINT_TIMES)
Martin Reinecke's avatar
Martin Reinecke committed
104

Martin Reinecke's avatar
Martin Reinecke committed
105
106
    def apply(self, x, mode):
        self._check_input(x, mode)
107
        if np.issubdtype(x.dtype, np.complexfloating):
Martin Reinecke's avatar
Martin Reinecke committed
108
109
            return (self._applyfunc(x.real, mode) +
                    1j*self._applyfunc(x.imag, mode))
Martin Reinecke's avatar
Martin Reinecke committed
110
        else:
Martin Reinecke's avatar
Martin Reinecke committed
111
112
113
114
115
116
117
118
119
120
121
122
123
            return self._applyfunc(x, mode)

    def _apply_cartesian(self, x, mode):
        """
        RG -> RG transform method.

        Parameters
        ----------
        x : Field
            The field to be transformed
        """
        from pyfftw.interfaces.numpy_fft import fftn
        axes = x.domain.axes[self._space]
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
124
        tdom = self._target if x.domain == self._domain else self._domain
Martin Reinecke's avatar
Martin Reinecke committed
125
126
127
128
129
130
131
132
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
        oldax = dobj.distaxis(x.val)
        if oldax not in axes:  # straightforward, no redistribution needed
            ldat = dobj.local_data(x.val)
            ldat = utilities.hartley(ldat, axes=axes)
            tmp = dobj.from_local_data(x.val.shape, ldat, distaxis=oldax)
        elif len(axes) < len(x.shape) or len(axes) == 1:
            # we can use one Hartley pass in between the redistributions
            tmp = dobj.redistribute(x.val, nodist=axes)
            newax = dobj.distaxis(tmp)
            ldat = dobj.local_data(tmp)
            ldat = utilities.hartley(ldat, axes=axes)
            tmp = dobj.from_local_data(tmp.shape, ldat, distaxis=newax)
            tmp = dobj.redistribute(tmp, dist=oldax)
        else:  # two separate, full FFTs needed
            # ideal strategy for the moment would be:
            # - do real-to-complex FFT on all local axes
            # - fill up array
            # - redistribute array
            # - do complex-to-complex FFT on remaining axis
            # - add re+im
            # - redistribute back
            rem_axes = tuple(i for i in axes if i != oldax)
            tmp = x.val
            ldat = dobj.local_data(tmp)
            ldat = utilities.my_fftn_r2c(ldat, axes=rem_axes)
            if oldax != 0:
                raise ValueError("bad distribution")
            ldat2 = ldat.reshape((ldat.shape[0],
                                  np.prod(ldat.shape[1:])))
            shp2d = (x.val.shape[0], np.prod(x.val.shape[1:]))
            tmp = dobj.from_local_data(shp2d, ldat2, distaxis=0)
            tmp = dobj.transpose(tmp)
            ldat2 = dobj.local_data(tmp)
            ldat2 = fftn(ldat2, axes=(1,))
            ldat2 = ldat2.real+ldat2.imag
            tmp = dobj.from_local_data(tmp.shape, ldat2, distaxis=0)
            tmp = dobj.transpose(tmp)
            ldat2 = dobj.local_data(tmp).reshape(ldat.shape)
            tmp = dobj.from_local_data(x.val.shape, ldat2, distaxis=0)
        Tval = Field(tdom, tmp)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
165
166
        if mode & (LinearOperator.TIMES | LinearOperator.ADJOINT_TIMES):
            fct = self._domain[self._space].scalar_dvol()
Martin Reinecke's avatar
Martin Reinecke committed
167
        else:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
168
            fct = self._target[self._space].scalar_dvol()
Martin Reinecke's avatar
Martin Reinecke committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        if fct != 1:
            Tval *= fct

        return Tval

    def _slice_p2h(self, inp):
        rr = self.sjob.alm2map_adjoint(inp)
        assert len(rr) == ((self.mmax+1)*(self.mmax+2))//2 + \
                          (self.mmax+1)*(self.lmax-self.mmax)
        res = np.empty(2*len(rr)-self.lmax-1, dtype=rr[0].real.dtype)
        res[0:self.lmax+1] = rr[0:self.lmax+1].real
        res[self.lmax+1::2] = np.sqrt(2)*rr[self.lmax+1:].real
        res[self.lmax+2::2] = np.sqrt(2)*rr[self.lmax+1:].imag
        return res/np.sqrt(np.pi*4)

    def _slice_h2p(self, inp):
        res = np.empty((len(inp)+self.lmax+1)//2, dtype=(inp[0]*1j).dtype)
        assert len(res) == ((self.mmax+1)*(self.mmax+2))//2 + \
                           (self.mmax+1)*(self.lmax-self.mmax)
        res[0:self.lmax+1] = inp[0:self.lmax+1]
        res[self.lmax+1:] = np.sqrt(0.5)*(inp[self.lmax+1::2] +
                                          1j*inp[self.lmax+2::2])
        res = self.sjob.alm2map(res)
        return res/np.sqrt(np.pi*4)

    def _apply_spherical(self, x, mode):
        axes = x.domain.axes[self._space]
        axis = axes[0]
        tval = x.val
        if dobj.distaxis(tval) == axis:
            tval = dobj.redistribute(tval, nodist=(axis,))
        distaxis = dobj.distaxis(tval)

        p2h = not x.domain[self._space].harmonic
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
203
        tdom = self._target if x.domain == self._domain else self._domain
Martin Reinecke's avatar
Martin Reinecke committed
204
205
206
207
208
209
210
211
212
213
        func = self._slice_p2h if p2h else self._slice_h2p
        idat = dobj.local_data(tval)
        odat = np.empty(dobj.local_shape(tdom.shape, distaxis=distaxis),
                        dtype=x.dtype)
        for slice in utilities.get_slice_list(idat.shape, axes):
            odat[slice] = func(idat[slice])
        odat = dobj.from_local_data(tdom.shape, odat, distaxis)
        if distaxis != dobj.distaxis(x.val):
            odat = dobj.redistribute(odat, dist=dobj.distaxis(x.val))
        return Field(tdom, odat)
Jait Dixit's avatar
Jait Dixit committed
214

215
216
217
218
    @property
    def domain(self):
        return self._domain

Jait Dixit's avatar
Jait Dixit committed
219
220
221
222
    @property
    def target(self):
        return self._target

223
    @property
Martin Reinecke's avatar
Martin Reinecke committed
224
    def capability(self):
Martin Reinecke's avatar
Martin Reinecke committed
225
        return self._capability