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(xyz, density.thick_disk, r_sun=r_sun, |
27
|
|
|
**PARAMS['thick_disk']).ne |
28
|
|
|
assert len(ne_disk1) == 100 |
29
|
|
|
assert all(ne_disk1 >= 0) |
30
|
|
|
|
31
|
|
|
ne_disk2 = density.NEobject(xyz, density.thin_disk, |
32
|
|
|
**PARAMS['thin_disk']).ne |
33
|
|
|
assert len(ne_disk2) == 100 |
34
|
|
|
assert all(ne_disk2 >= 0) |
35
|
|
|
|
36
|
|
|
ne_gc = density.NEobject(xyz, density.gc, **PARAMS['galactic_center']).ne |
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, center, ellipsoid, theta) |
47
|
|
|
assert len(density.in_ellipsoid(xyz, center, ellipsoid, theta)) == 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
|
|
View Code Duplication |
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_clumps(xyz) |
69
|
|
|
assert len(ne_clumps) == 100 |
70
|
|
|
ix = ne_clumps.argmax() |
71
|
|
|
assert ne_clumps[ix] == clumps.ne_clumps(xyz[:, ix]) |
72
|
|
View Code Duplication |
|
|
|
|
|
73
|
|
|
|
74
|
|
|
def test_void(): |
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_voids(xyz) |
86
|
|
|
assert len(ne_voids) == 100 |
87
|
|
|
|
88
|
|
|
ix = ne_voids.argmax() |
89
|
|
|
assert ne_voids[ix] == voids.ne_voids(xyz[:, ix]) |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def test_local_ism(): |
93
|
|
|
xyz = (1-2*rand(3, 100)) * 20 |
94
|
|
|
local_ism = density.LocalISM(xyz, **PARAMS) |
95
|
|
|
|
96
|
|
|
assert all(local_ism.electron_density >= 0) |
97
|
|
|
assert len(local_ism.electron_density) == 100 |
98
|
|
|
assert len(local_ism.electron_density) == len(local_ism.wlism) |
99
|
|
|
assert all((local_ism.electron_density > 0) == local_ism.wlism) |
100
|
|
|
assert all((local_ism.flism > 0) == local_ism.wlism) |
101
|
|
|
|
102
|
|
|
def test_DM(): |
103
|
|
|
tol = 1e-3 |
104
|
|
|
d1 = density.NEobject(np.array([0.1503843,7.647129,0.5000018]), |
105
|
|
|
density.thick_disk, r_sun = 8.5, |
106
|
|
|
**PARAMS['thick_disk']) |
107
|
|
|
assert abs(d1.DM(np.array([0,8.5,0])) - 32.36372)/32.36372 < tol |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
d2 = density.NEobject(np.array([0.1503843,7.647129,0.5000018]), |
111
|
|
|
density.thin_disk, |
112
|
|
|
**PARAMS['thin_disk']) |
113
|
|
|
assert abs(d2.DM(np.array([0,8.5,0])) - 0.046937)/0.046937 < tol |
114
|
|
|
|