test_lm_space.py 2.95 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
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
16
17
#
# 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
from __future__ import division
Jait Dixit's avatar
Jait Dixit committed
20
21
import unittest
import numpy as np
22
from numpy.testing import assert_, assert_equal, assert_raises,\
Martin Reinecke's avatar
Martin Reinecke committed
23
        assert_allclose
Philipp Arras's avatar
Philipp Arras committed
24
import nifty5 as ift
Jait Dixit's avatar
Jait Dixit committed
25
26
from test.common import expand

Martin Reinecke's avatar
Martin Reinecke committed
27
# [lmax, expected]
28
CONSTRUCTOR_CONFIGS = [
29
        [5, None, {
Jait Dixit's avatar
Jait Dixit committed
30
31
32
33
            'lmax': 5,
            'mmax': 5,
            'shape': (36,),
            'harmonic': True,
Martin Reinecke's avatar
Martin Reinecke committed
34
            'size': 36,
Jait Dixit's avatar
Jait Dixit committed
35
            }],
36
        [7, 4, {
Jait Dixit's avatar
Jait Dixit committed
37
            'lmax': 7,
38
39
            'mmax': 4,
            'shape': (52,),
Jait Dixit's avatar
Jait Dixit committed
40
            'harmonic': True,
Martin Reinecke's avatar
Martin Reinecke committed
41
            'size': 52,
Jait Dixit's avatar
Jait Dixit committed
42
            }],
43
        [-1, 28, {
Jait Dixit's avatar
Jait Dixit committed
44
45
46
47
            'error': ValueError
            }]
    ]

Theo Steininger's avatar
Theo Steininger committed
48

49
def _k_length_array_helper(index_arr, lmax):
Martin Reinecke's avatar
Martin Reinecke committed
50
51
52
53
    if index_arr <= lmax:
        index_half = index_arr
    else:
        if (index_arr - lmax) % 2 == 0:
Martin Reinecke's avatar
Martin Reinecke committed
54
            index_half = (index_arr + lmax)//2
Martin Reinecke's avatar
Martin Reinecke committed
55
        else:
Martin Reinecke's avatar
Martin Reinecke committed
56
            index_half = (index_arr + lmax + 1)//2
Martin Reinecke's avatar
Martin Reinecke committed
57

58
59
    m = np.ceil(((2*lmax + 1) - np.sqrt((2*lmax + 1)**2 -
                 8*(index_half - lmax)))/2).astype(int)
Martin Reinecke's avatar
Martin Reinecke committed
60

61
    return index_half - m*(2*lmax + 1 - m)//2
Martin Reinecke's avatar
Martin Reinecke committed
62

Theo Steininger's avatar
Theo Steininger committed
63

64
65
def get_k_length_array_configs():
    da_0 = [_k_length_array_helper(idx, 5) for idx in np.arange(36)]
Martin Reinecke's avatar
Martin Reinecke committed
66
    return [[5, da_0]]
67
68


69
class LMSpaceInterfaceTests(unittest.TestCase):
Jait Dixit's avatar
Jait Dixit committed
70
71
    @expand([['lmax', int],
            ['mmax', int],
Martin Reinecke's avatar
Martin Reinecke committed
72
            ['size', int]])
73
    def test_property_ret_type(self, attribute, expected_type):
Martin Reinecke's avatar
Martin Reinecke committed
74
        l = ift.LMSpace(7, 5)
75
        assert_(isinstance(getattr(l, attribute), expected_type))
Jait Dixit's avatar
Jait Dixit committed
76
77
78


class LMSpaceFunctionalityTests(unittest.TestCase):
79
    @expand(CONSTRUCTOR_CONFIGS)
80
    def test_constructor(self, lmax, mmax, expected):
81
82
        if 'error' in expected:
            with assert_raises(expected['error']):
Martin Reinecke's avatar
Martin Reinecke committed
83
                ift.LMSpace(lmax, mmax)
Jait Dixit's avatar
Jait Dixit committed
84
        else:
Martin Reinecke's avatar
Martin Reinecke committed
85
            l = ift.LMSpace(lmax, mmax)
Martin Reinecke's avatar
Martin Reinecke committed
86
            for key, value in expected.items():
87
                assert_equal(getattr(l, key), value)
Jait Dixit's avatar
Jait Dixit committed
88

89
    def test_dvol(self):
Martin Reinecke's avatar
Martin Reinecke committed
90
        assert_allclose(ift.LMSpace(5).dvol, 1.)
Jait Dixit's avatar
Jait Dixit committed
91

92
93
    @expand(get_k_length_array_configs())
    def test_k_length_array(self, lmax, expected):
Martin Reinecke's avatar
Martin Reinecke committed
94
        l = ift.LMSpace(lmax)
Martin Reinecke's avatar
Martin Reinecke committed
95
        assert_allclose(l.get_k_length_array().to_global_data(), expected)