Completed
Push — master ( de1306...06a50e )
by Ben
01:43
created

test_electron_density()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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