rg_space.py 8.69 KB
Newer Older
1
2
# NIFTY (Numerical Information Field Theory) has been developed at the
# Max-Planck-Institute for Astrophysics.
Marco Selig's avatar
Marco Selig committed
3
##
4
# Copyright (C) 2015 Max-Planck-Society
Marco Selig's avatar
Marco Selig committed
5
##
6
7
# Author: Marco Selig
# Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
Marco Selig's avatar
Marco Selig committed
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.
Marco Selig's avatar
Marco Selig committed
13
##
14
15
16
17
# 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.
Marco Selig's avatar
Marco Selig committed
18
##
19
20
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
Marco Selig's avatar
Marco Selig committed
21
22
23
24
25
26
27
28
29
30

"""
    ..                  __   ____   __
    ..                /__/ /   _/ /  /_
    ..      __ ___    __  /  /_  /   _/  __   __
    ..    /   _   | /  / /   _/ /  /   /  / /  /
    ..   /  / /  / /  / /  /   /  /_  /  /_/  /
    ..  /__/ /__/ /__/ /__/    \___/  \___   /  rg
    ..                               /______/

Marco Selig's avatar
Marco Selig committed
31
    NIFTY submodule for regular Cartesian grids.
Marco Selig's avatar
Marco Selig committed
32
33
34

"""
from __future__ import division
Ultimanet's avatar
Ultimanet committed
35

Marco Selig's avatar
Marco Selig committed
36
import numpy as np
Ultimanet's avatar
Ultimanet committed
37

38
39
from d2o import distributed_data_object,\
                STRATEGIES as DISTRIBUTION_STRATEGIES
40

41
from nifty.spaces.space import Space
csongor's avatar
csongor committed
42

43
from nifty.config import about
44

45
from rg_space_paradict import RGSpaceParadict
Ultimanet's avatar
Ultimanet committed
46

Marco Selig's avatar
Marco Selig committed
47

48
class RGSpace(Space):
Marco Selig's avatar
Marco Selig committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
    """
        ..      _____   _______
        ..    /   __/ /   _   /
        ..   /  /    /  /_/  /
        ..  /__/     \____  /  space class
        ..          /______/

        NIFTY subclass for spaces of regular Cartesian grids.

        Parameters
        ----------
        num : {int, numpy.ndarray}
            Number of gridpoints or numbers of gridpoints along each axis.
        naxes : int, *optional*
            Number of axes (default: None).
        zerocenter : {bool, numpy.ndarray}, *optional*
            Whether the Fourier zero-mode is located in the center of the grid
            (or the center of each axis speparately) or not (default: True).
        hermitian : bool, *optional*
            Whether the fields living in the space follow hermitian symmetry or
            not (default: True).
        purelyreal : bool, *optional*
            Whether the field values are purely real (default: True).
        dist : {float, numpy.ndarray}, *optional*
            Distance between two grid points along each axis (default: None).
        fourier : bool, *optional*
            Whether the space represents a Fourier or a position grid
            (default: False).

        Notes
        -----
        Only even numbers of grid points per axis are supported.
        The basis transformations between position `x` and Fourier mode `k`
        rely on (inverse) fast Fourier transformations using the
        :math:`exp(2 \pi i k^\dagger x)`-formulation.

        Attributes
        ----------
        para : numpy.ndarray
            One-dimensional array containing information on the axes of the
            space in the following form: The first entries give the grid-points
            along each axis in reverse order; the next entry is 0 if the
            fields defined on the space are purely real-valued, 1 if they are
            hermitian and complex, and 2 if they are not hermitian, but
            complex-valued; the last entries hold the information on whether
            the axes are centered on zero or not, containing a one for each
            zero-centered axis and a zero for each other one, in reverse order.
96
        dtype : numpy.dtype
Marco Selig's avatar
Marco Selig committed
97
98
99
100
101
102
103
104
105
106
107
108
109
            Data type of the field values for a field defined on this space,
            either ``numpy.float64`` or ``numpy.complex128``.
        discrete : bool
            Whether or not the underlying space is discrete, always ``False``
            for regular grids.
        vol : numpy.ndarray
            One-dimensional array containing the distances between two grid
            points along each axis, in reverse order. By default, the total
            length of each axis is assumed to be one.
        fourier : bool
            Whether or not the grid represents a Fourier basis.
    """

110
    def __init__(self, shape=(1,), zerocenter=False, distances=None,
111
                 harmonic=False, dtype=np.dtype('float')):
Marco Selig's avatar
Marco Selig committed
112
113
114
115
116
117
118
119
120
121
122
123
        """
            Sets the attributes for an rg_space class instance.

            Parameters
            ----------
            num : {int, numpy.ndarray}
                Number of gridpoints or numbers of gridpoints along each axis.
            naxes : int, *optional*
                Number of axes (default: None).
            zerocenter : {bool, numpy.ndarray}, *optional*
                Whether the Fourier zero-mode is located in the center of the
                grid (or the center of each axis speparately) or not
Ultimanet's avatar
Ultimanet committed
124
                (default: False).
Marco Selig's avatar
Marco Selig committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
            hermitian : bool, *optional*
                Whether the fields living in the space follow hermitian
                symmetry or not (default: True).
            purelyreal : bool, *optional*
                Whether the field values are purely real (default: True).
            dist : {float, numpy.ndarray}, *optional*
                Distance between two grid points along each axis
                (default: None).
            fourier : bool, *optional*
                Whether the space represents a Fourier or a position grid
                (default: False).

            Returns
            -------
            None
        """
141

theos's avatar
theos committed
142
143
144
145
        self.paradict = RGSpaceParadict(shape=shape,
                                        zerocenter=zerocenter,
                                        distances=distances,
                                        harmonic=harmonic)
146
        self.dtype = np.dtype(dtype)
theos's avatar
theos committed
147
148
149
150

    @property
    def harmonic(self):
        return self.paradict['harmonic']
151

152
    def copy(self):
153
154
        return RGSpace(dtype=self.dtype, harmonic=self.harmonic,
                       **self.paradict.parameters)
155

156
157
    @property
    def shape(self):
158
        return self.paradict['shape']
Marco Selig's avatar
Marco Selig committed
159

160
161
162
    @property
    def dim(self):
        return reduce(lambda x, y: x*y, self.shape)
163

164
165
166
    @property
    def total_volume(self):
        return self.dim * reduce(lambda x, y: x*y, self.paradict['distances'])
Marco Selig's avatar
Marco Selig committed
167

168
    def weight(self, x, power=1, axes=None, inplace=False):
169
        weight = reduce(lambda x, y: x*y, self.paradict['distances'])**power
170
171
172
173
174
175
        if inplace:
            x *= weight
            result_x = x
        else:
            result_x = x*weight
        return result_x
Marco Selig's avatar
Marco Selig committed
176

177
    def compute_k_array(self, distribution_strategy):
Marco Selig's avatar
Marco Selig committed
178
        """
179
180
            Calculates an n-dimensional array with its entries being the
            lengths of the k-vectors from the zero point of the grid.
Marco Selig's avatar
Marco Selig committed
181
182
183

            Parameters
            ----------
184
            None : All information is taken from the parent object.
Marco Selig's avatar
Marco Selig committed
185
186
187

            Returns
            -------
188
            nkdict : distributed_data_object
Marco Selig's avatar
Marco Selig committed
189
        """
190
191
192
193
194
195
196
197
198
199
200
201
202
        shape = self.shape
        # prepare the distributed_data_object
        nkdict = distributed_data_object(
                        global_shape=shape,
                        dtype=np.float128,
                        distribution_strategy=distribution_strategy)

        if distribution_strategy in DISTRIBUTION_STRATEGIES['slicing']:
            # get the node's individual slice of the first dimension
            slice_of_first_dimension = slice(
                                    *nkdict.distributor.local_slice[0:2])
        elif distribution_strategy in DISTRIBUTION_STRATEGIES['not']:
            slice_of_first_dimension = slice(0, shape[0])
Ultimanet's avatar
Ultimanet committed
203
        else:
204
            raise ValueError(about._errors.cstring(
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
                "ERROR: Unsupported distribution strategy"))
        dists = self._compute_k_array_helper(slice_of_first_dimension)
        nkdict.set_local_data(dists)

        return nkdict

    def _compute_k_array_helper(self, slice_of_first_dimension):
        dk = self.paradict['distances']
        shape = self.shape

        inds = []
        for a in shape:
            inds += [slice(0, a)]

        cords = np.ogrid[inds]

        dists = ((np.float128(0) + cords[0] - shape[0] // 2) * dk[0])**2
        # apply zerocenterQ shift
        if self.paradict['zerocenter'][0] == False:
            dists = np.fft.fftshift(dists)
        # only save the individual slice
        dists = dists[slice_of_first_dimension]
        for ii in range(1, len(shape)):
            temp = ((cords[ii] - shape[ii] // 2) * dk[ii])**2
            if self.paradict['zerocenter'][ii] == False:
                temp = np.fft.fftshift(temp)
            dists = dists + temp
        dists = np.sqrt(dists)
        return dists