Completed
Push — master ( 668558...d8c699 )
by Ben
01:16
created

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