test_field.py 3.26 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
19
20
21
22

import unittest

import numpy as np
from numpy.testing import assert_,\
Martin Reinecke's avatar
Martin Reinecke committed
23
                          assert_almost_equal
24

25
from itertools import product
26
27

from nifty import Field,\
28
                  RGSpace
29

30
from d2o import distributed_data_object
31

Jait Dixit's avatar
Jait Dixit committed
32
from test.common import expand
33
34


Martin Reinecke's avatar
Martin Reinecke committed
35
SPACES = [RGSpace((4,)), RGSpace((5))]
Theo Steininger's avatar
Theo Steininger committed
36
SPACE_COMBINATIONS = [(), SPACES[0], SPACES[1], SPACES]
37
38
39


class Test_Interface(unittest.TestCase):
40
    @expand(product(SPACE_COMBINATIONS,
Martin Reinecke's avatar
Martin Reinecke committed
41
                    [['distribution_strategy', str],
42
43
44
45
46
47
48
49
50
51
                     ['domain', tuple],
                     ['domain_axes', tuple],
                     ['val', distributed_data_object],
                     ['shape', tuple],
                     ['dim', np.int],
                     ['dof', np.int],
                     ['total_volume', np.float]]))
    def test_return_types(self, domain, attribute_desired_type):
        attribute = attribute_desired_type[0]
        desired_type = attribute_desired_type[1]
52
        f = Field(domain=domain)
53
54
        assert_(isinstance(getattr(f, attribute), desired_type))

Martin Reinecke's avatar
Martin Reinecke committed
55

56
57
58
59
60
61
62
63
64
65
class Test_Functionality(unittest.TestCase):
    @expand(product([True, False], [True, False],
                    [True, False], [True, False],
                    [(1,), (4,), (5,)], [(1,), (6,), (7,)]))
    def test_hermitian_decomposition(self, z1, z2, preserve, complexdata,
                                     s1, s2):
        np.random.seed(123)
        r1 = RGSpace(s1, harmonic=True, zerocenter=(z1,))
        r2 = RGSpace(s2, harmonic=True, zerocenter=(z2,))
        ra = RGSpace(s1+s2, harmonic=True, zerocenter=(z1, z2))
Martin Reinecke's avatar
Martin Reinecke committed
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        v = np.random.random(s1+s2)
        if complexdata:
            v = v + 1j*np.random.random(s1+s2)
        f1 = Field(ra, val=v, copy=True)
        f2 = Field((r1, r2), val=v, copy=True)
        h1, a1 = Field._hermitian_decomposition((ra,), f1.val, (0,),
                                                ((0, 1,),), preserve)
        h2, a2 = Field._hermitian_decomposition((r1, r2), f2.val, (0, 1),
                                                ((0,), (1,)), preserve)
        h3, a3 = Field._hermitian_decomposition((r1, r2), f2.val, (1, 0),
                                                ((0,), (1,)), preserve)

        assert_almost_equal(h1.get_full_data(), h2.get_full_data())
        assert_almost_equal(a1.get_full_data(), a2.get_full_data())
        assert_almost_equal(h1.get_full_data(), h3.get_full_data())
        assert_almost_equal(a1.get_full_data(), a3.get_full_data())