File: test_geod_int.py

package info (click to toggle)
einsteinpy 0.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,712 kB
  • sloc: python: 8,196; makefile: 146
file content (145 lines) | stat: -rw-r--r-- 3,737 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
import warnings
import pytest
from numpy.testing import assert_allclose

from einsteinpy.geodesic import Geodesic
from einsteinpy.geodesic.utils import _kerr
from einsteinpy.integrators import GeodesicIntegrator
from einsteinpy.integrators.utils import _Z


@pytest.mark.parametrize(
    "order, expected",
    [
        (4, (-1.7024143839193153, 1.3512071919596578)),
        (6, (-1.349343516178727, 1.1746717580893635)),
        (8, (-1.2323658786507714, 1.1161829393253857)),
    ]
)
def test__Z(order, expected):
    assert_allclose(_Z(order), expected, atol=1e-8, rtol=1e-8)


def test_str_repr():
    geodint = GeodesicIntegrator(
        metric=_kerr,
        metric_params=(0.9,),
        q0=[2.15, np.pi / 2, 0.],
        p0=[0., 0., 1.5]
    )

    assert str(geodint) == repr(geodint)


def test_runtime_warning1():
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")

        geod = Geodesic(
            metric="Kerr",
            metric_params=(0.9,),
            position=[2.15, np.pi / 2, 0.],
            momentum=[0., 0., 1.5],
            time_like=True,
            steps=4,
            delta=0.5,
            omega=1.  # Unstable integration
        )

        assert len(w) == 2  # 2 warnings to be shown
        assert issubclass(w[-1].category, RuntimeWarning)


def test_runtime_warning2():
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")

        geod = Geodesic(
            metric="Kerr",
            metric_params=(0.9,),
            position=[2.15, np.pi / 2, 0.],
            momentum=[0., 0., 1.5],
            time_like=True,
            steps=4,
            delta=0.5,
            omega=0.01  # Stable integration
        )

        assert len(w) == 0


def test_suppress_runtime_warning():
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")

        geod = Geodesic(
            metric="Kerr",
            metric_params=(0.9,),
            position=[2.15, np.pi / 2, 0.],
            momentum=[0., 0., 1.5],
            time_like=True,
            steps=4,
            delta=0.5,
            omega=1.,  # Unstable integration
            suppress_warnings=True
        )

        assert len(w) == 0


def test_rtol_atol_runtime_warning():
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")

        geod = Geodesic(
            metric="Kerr",
            metric_params=(0.9,),
            position=[2.15, np.pi / 2, 0.],
            momentum=[0., 0., 1.5],
            time_like=True,
            steps=4,
            delta=0.5,
            omega=1.,  # Unstable integration
            rtol=1.,
            atol=1.
        )

        assert len(w) == 0  # 1 warning to be shown


def test_order_NotImplementedError():

    with pytest.raises(NotImplementedError):
        geod = Geodesic(
            metric="Kerr",
            metric_params=(0.9,),
            position=[2.15, np.pi / 2, 0.],
            momentum=[0., 0., 1.5],
            time_like=True,
            steps=4,
            delta=0.5,
            order=5
        )


@pytest.mark.parametrize("order", [4, 6, 8])
def test_higher_order_traits(order):
    geod = Geodesic(
        metric="Kerr",
        metric_params=(0.5,),
        position=[4., np.pi / 2, 0.],
        momentum=[0., 0., 2.],
        time_like=False,
        steps=10,
        delta=0.5,
        order=order,
        return_cartesian=False,
        suppress_warnings=True
    )

    L = geod.momentum[-1]
    theta = geod.position[2]

    assert_allclose(geod.trajectory[1][:, -1], L, atol=1e-4, rtol=1e-4)
    assert_allclose(geod.trajectory[1][:, 2], theta, atol=1e-6, rtol=1e-6)