Completed
Push — master ( d8c699...d287d3 )
by Ben
50s
created

test_dist()   A

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
1
2
import os
3
4
import numpy as np
5
from click.testing import CliRunner
6
from numpy.random import rand
7
from numpy.random import randint
8
from scipy import integrate
9
10
from ne2001 import density
11
from ne2001 import utils
12
from ne2001.cli import main
13
14
PARAMS = density.PARAMS
15
r_sun = 8.5
16
17
def test_main():
18
    runner = CliRunner()
19
    result = runner.invoke(main, [])
20
21
    assert result.output == '()\n'
22
    assert result.exit_code == 0
23
24
25
def test_density():
26
    xyz = (1-2*rand(3, 100)) * 20
27
28
    ne_disk1 = density.NEobject(density.thick_disk, r_sun=r_sun,
29
                                **PARAMS['thick_disk']).ne(xyz)
30
    assert len(ne_disk1) == 100
31
    assert all(ne_disk1 >= 0)
32
33
    ne_disk2 = density.NEobject(density.thin_disk, **PARAMS['thin_disk']).ne(xyz)
34
    assert len(ne_disk2) == 100
35
    assert all(ne_disk2 >= 0)
36
37
    ne_gc = density.NEobject(density.gc, **PARAMS['galactic_center']).ne(xyz)
38
    assert len(ne_gc) == 100
39
    assert all(ne_gc >= 0)
40
41
42
def test_geometry():
43
    ellipsoid = rand(3)*20
44
    xyz = (1-2*rand(3, 100)) * 20
45
    theta = rand()*2*np.pi
46
    center = rand(3)*20
47
    assert density.in_ellipsoid(center, ellipsoid, theta)(center)
48
    assert len(density.in_ellipsoid(center, ellipsoid, theta)(xyz)) == 100
49
50
    cylinder = rand(3)*20
51
    xyz = (1-2*rand(3, 100)) * 20
52
    theta = rand()*2*np.pi
53
    center = rand(3)*20
54
    assert len(density.in_cylinder(xyz, center, cylinder, theta)) == 100
55
56
    assert all(density.in_half_sphere(xyz, center, 10)[xyz[-1] < 0] == 0)
57
58
59
def test_clumps():
60
    clumps_file = os.path.join(os.path.split(density.__file__)[0], "data",
61
                               "neclumpN.NE2001.dat")
62
63
    clumps = density.Clumps(clumps_file)
64
65
    clumps = density.Clumps()
66
    xyz = (clumps.xyz.T[randint(0, clumps.gl.size, 100)].T +
67
           (1-2*rand(3, 100))*0.01)
68
69
    ne_clumps = clumps.ne(xyz)
70
    assert len(ne_clumps) == 100
71
    ix = ne_clumps.argmax()
72
    assert ne_clumps[ix] == clumps.ne(xyz[:, ix])
73
74
75
def test_void():
76
    tol = 1e-3
77
    voids_file = os.path.join(os.path.split(density.__file__)[0], "data",
78
                              "nevoidN.NE2001.dat")
79
80
    voids = density.Voids(voids_file)
81
82
    voids = density.Voids()
83
84
    xyz = (voids.xyz.T[randint(0, voids.gl.size, 100)].T +
85
           (1-2*rand(3, 100))*0.01)
86
87
    ne_voids = voids.ne(xyz)
88
    assert len(ne_voids) == 100
89
90
    ix = ne_voids.argmax()
91
    assert ne_voids[ix] == voids.ne(xyz[:, ix])
92
93
    xyz = np.array([-3.4153607E-02,   7.521969,      0.2080137])
94
    DM = 0.9308054
95
    assert abs(voids.DM(xyz) - DM)/DM < tol
96
97
98
def test_local_ism():
99
    tol = 1e-3
100
    xyz = (1-2*rand(3, 100)) * 20
101
    local_ism = density.LocalISM(**PARAMS)
102
103
    assert all(local_ism.ne(xyz) >= 0)
104
    assert len(local_ism.ne(xyz)) == 100
105
    assert (abs(local_ism.DM(np.array([-3.4136981E-02, 7.522445, 0.2079124])) -
106
                2.453550)/2.453550 < tol)
107
108
def test_DM():
109
    tol = 1e-3
110
    d1 = density.NEobject(density.thick_disk, r_sun=8.5,
111
                          **PARAMS['thick_disk'])
112
    xyz = np.array([0.1503843, 7.647129, 0.5000018])
113
    DM = 32.36372
114
    assert abs(d1.DM(xyz) - DM) / DM  < tol
115
116
117
    d2 = density.NEobject(density.thin_disk,
118
                          **PARAMS['thin_disk'])
119
120
    np.array([0.1503843, 7.647129, 0.5000018])
121
    DM = 32.36372
122
123
    xyz = np.array([0.1503843, 7.647129, 0.5000018])
124
    DM = 0.046937
125
    assert abs(d2.DM(xyz) - DM) / DM  < tol
126
127
128
def test_electron_density_quad():
129
    tol = 1e-3
130
    ne = density.ElectronDensity(**PARAMS)
131
    xyz = np.array([-3.4153607E-02,   7.521969,      0.2080137])
132
    DM = 23.98557
133
    assert abs(ne.DM(xyz) - DM)/DM < tol
134
135
136
def test_electron_density_trapz():
137
    tol = 1e-3
138
    ne = density.ElectronDensity(**PARAMS)
139
    xyz = np.array([-3.4153607E-02,   7.521969,      0.2080137])
140
    DM = 23.98557
141
    assert abs(ne.DM(xyz, integrator=integrate.trapz) - DM)/DM < tol
142
143
144
def test_dist():
145
    for i in range(10):
146
        tol = 1e-3
147
        ne = density.ElectronDensity(**PARAMS)
148
        l = rand()*2*np.pi
149
        b = np.arccos(1 - 2*rand())
150
        d = rand()*10
151
        rsun = 8.5
152
        DM = ne.DM(utils.galactic_to_galactocentric(l, b, d, rsun))
153
        assert abs(ne.dist(l, b, DM, rsun) - d)/d < tol, (l, b, d)
154