File: std.py

package info (click to toggle)
gyoto 2.0.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,444 kB
  • sloc: cpp: 42,330; sh: 4,512; python: 3,436; xml: 2,865; makefile: 691; ansic: 346
file content (356 lines) | stat: -rw-r--r-- 12,481 bytes parent folder | download | duplicates (3)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import numpy
import unittest
import gyoto.core
import gyoto.std
import gyoto.metric
import inspect

class TestChernSimons(unittest.TestCase):

    def test_setInitCoord(self):
        gg=gyoto.std.ChernSimons()
        zeta=0.5
        gg.dzetaCS(zeta)
        self.assertTrue((gg.dzetaCS() == zeta))

class TestDeformedTorus(unittest.TestCase):

    def test_DeformedTorus(self):
        ao=gyoto.core.Astrobj("DeformedTorus")
        ao=gyoto.std.DeformedTorus()
        sp=gyoto.std.BlackBody()
        ao.spectrum(sp)
        r=4.
        ao.largeRadius(r)
        self.assertTrue((ao.largeRadius() == r))

class TestDynamicalDiskBolometric(unittest.TestCase):

    def test_DynamicalDiskBolometric(self):
        ao=gyoto.core.Astrobj("DynamicalDiskBolometric")
        ao=gyoto.std.DynamicalDiskBolometric()
        self.assertTrue(True)

class TestEquatorialHotSpot(unittest.TestCase):

    def test_EquatorialHotSpot(self):
        ao=gyoto.core.Astrobj("EquatorialHotSpot")
        ao=gyoto.std.EquatorialHotSpot()
        r=4.
        ao.spotRadSize(r)
        self.assertTrue((ao.spotRadSize() == r))
        kind="RadialBeaming"
        ao.beaming(kind)
        self.assertTrue((ao.beaming() == kind))
        self.assertRaises(gyoto.core.Error, ao.beaming, "foo")

class TestInflateStar(unittest.TestCase):

    def test_InflateStar(self):
        ao=gyoto.core.Astrobj("InflateStar")
        ao=gyoto.std.InflateStar()
        ao.radius(0.)
        ao.radiusStop(2.)
        ao.timeInflateInit(0.)
        ao.timeInflateStop(2.)
        self.assertTrue(abs(ao.radiusAt(1.)-1.) < 1e-6)

class TestOscilTorus(unittest.TestCase):

    def test_OscilTorus(self):
        ao=gyoto.core.Astrobj("OscilTorus")
        ao=gyoto.std.OscilTorus()
        r=4.
        ao.largeRadius(r)
        self.assertTrue((ao.largeRadius() == r))
        perturbkind="Vertical"
        ao.perturbKind(perturbkind)
        self.assertTrue((ao.perturbKind() == perturbkind))

class TestRezzollaZhidenko(unittest.TestCase):

    def test_mass(self):
        gg=gyoto.std.RezzollaZhidenko()
        m=2.
        gg.mass(m)
        self.assertTrue((gg.mass() == m))

    def test_aparam(self):
        gg=gyoto.std.RezzollaZhidenko()
        a=(1., 2., 3., 4.)
        gg.aparam(a)
        self.assertTrue((gg.aparam() == a))

class TestHayward(unittest.TestCase):

    def test_mass(self):
        gg=gyoto.std.Hayward()
        m=2.
        gg.mass(m)
        self.assertTrue((gg.mass() == m))

    def test_charge(self):
        gg=gyoto.std.Hayward()
        b=0.5
        gg.charge(b)
        self.assertTrue((gg.charge() == b))

    def test_gmunu(self):
        metric=gyoto.std.Hayward()
        I=numpy.zeros((4,4))
        for i in range(4):
            I[i,i]=1.
        for r in (-2, 0.5, 6):
            pos=(10, r, numpy.pi/4, numpy.pi/3)
            g=metric.gmunu(pos)
            gup=metric.gmunu_up(pos)
            ggup=numpy.linalg.multi_dot((g, gup))
            for mu in range(4):
                for nu in range(4):
                    self.assertAlmostEqual(metric.gmunu(pos, mu, nu), g[mu, nu])
                    self.assertAlmostEqual(metric.gmunu_up(pos, mu, nu), gup[mu, nu], 7,
                                           "mu: {}, nu: {}".format(mu, nu))
                    self.assertAlmostEqual(ggup[mu, nu],I[mu,nu])


class TestStar(unittest.TestCase):

    def test_setInitCoord(self):
        st=gyoto.std.Star()
        gg=gyoto.std.KerrBL()
        st.metric(gg)
        pos_list=(600., 9., 1.5707999999999999741, 0)
        vel_list=(0., 0., 0.037037)
        st.setInitCoord(pos_list, vel_list)
        dst=gyoto.core.vector_double()
        dst2=gyoto.core.vector_double()
        st.getInitialCoord(dst)
        st.setPosition(pos_list)
        st.setVelocity(vel_list)
        st.getInitialCoord(dst2)
        self.assertTrue((numpy.asarray(dst) == numpy.asarray(dst2)).all())

class TestMinkowski(unittest.TestCase):

    def _compute_r_norm(self, met, st, pos, v, tmax=1e6):
        t, x, y, z, tdot, xdot, ydot, zdot=self._compute_orbit(met, st, pos, v, tmax)
        if met.spherical():
            r=x
        else:
            r=numpy.sqrt(x**2+y**2+z**2)
        n=t.size
        norm=numpy.asarray([met.norm([t[i], x[i], y[i], z[i]],
                                     [tdot[i], xdot[i], ydot[i], zdot[i]])
                            for  i in range(n)])
        return r, norm

    def _compute_orbit(self, met, st, pos, v, tmax=1e6):
        st.initCoord(numpy.append(pos, v))
        st.xFill(tmax)
        n=st.get_nelements()
        t=numpy.ndarray(n)
        x=numpy.ndarray(n)
        y=numpy.ndarray(n)
        z=numpy.ndarray(n)
        tdot=numpy.ndarray(n)
        xdot=numpy.ndarray(n)
        ydot=numpy.ndarray(n)
        zdot=numpy.ndarray(n)
        st.get_t(t)
        st.getCoord(t, x, y, z, tdot, xdot, ydot, zdot)
        return t, x, y, z, tdot, xdot, ydot, zdot

    def test_Keplerian(self):
        met=gyoto.std.Minkowski()
        met.keplerian(True)
        st=gyoto.std.Star()
        st.metric(met)

        # Cartesian coordinates
        # particlae is at x=1000:
        pos=[0., 1000., 0., 0.]
        v=met.circularVelocity(pos)
        r, norm=self._compute_r_norm(met, st, pos, v)
        self.assertLess( numpy.abs(r-1000.).max(), 1e-6)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-6 )

        # same velocity at z=1000:
        pos=[0., 0., 0., 1000.]
        r, norm=self._compute_r_norm(met, st, pos, v)
        self.assertLess( numpy.abs(r-1000.).max(), 1e-6)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-6 )

        # same velocity at x=z=1000/sqrt(2)
        pos=[0., 1000./numpy.sqrt(2.), 0., 1000./numpy.sqrt(2.)]
        r, norm=self._compute_r_norm(met, st, pos, v)
        self.assertLess( numpy.abs(r-1000.).max(), 1e-6)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-6 )

        # a elliptical orbit
        pos=[0., 1000., 0., 0.]
        v3=[0, 0.015, 0]
        tdot=met.SysPrimeToTdot(pos, v3)
        v=[tdot, v3[0]*tdot, v3[1]*tdot, v3[2]/tdot]
        r, norm=self._compute_r_norm(met, st, pos, v)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-6 )

        # circular orbit starting at x=3
        pos=[0., 3., 0., 0.]
        v=met.circularVelocity(pos)
        st.deltaMaxOverR(0.1)
        r, norm=self._compute_r_norm(met, st, pos, v, tmax=50.)
        self.assertLess( numpy.abs(r-3.).max(), 1e-6)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-6 )

        # Spherical coordinates
        st=gyoto.std.Star()
        st.metric(met)
        met.spherical(True)

        # again starting at x=1000
        pos=[0., 1000., numpy.pi*0.5, 0.]
        v=met.circularVelocity(pos)
        r, norm=self._compute_r_norm(met, st, pos, v)
        self.assertLess( numpy.abs(r-1000.).max(), 1e-6)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-6 )

        # elliptical orbit
        v[3] = v[3]/2.
        r, norm=self._compute_r_norm(met, st, pos, v)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-3 )

        # circular orbit starting at x=z=1000/sqrt(2)
        pos=[0., 1000., numpy.pi/2., 0.]
        v=met.circularVelocity(pos)
        pos[2]=numpy.pi/4.
        v[3]=v[3]/numpy.sin(numpy.pi/4.)
        r, norm=self._compute_r_norm(met, st, pos, v)
        self.assertLess( numpy.abs(r-1000.).max(), 1e-6)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-6 )

        # circular orbit starting at x=3
        pos=[0., 3., numpy.pi*0.5, 0.]
        st.deltaMaxOverR(0.1)
        st.initCoord(numpy.append(pos, v))
        v=met.circularVelocity(pos)
        r, norm=self._compute_r_norm(met, st, pos, v, tmax=50.)
        self.assertLess( numpy.abs(r-3.).max(), 1e-6)
        self.assertLess( numpy.abs(norm+1.).max(), 1e-6 )

class TestStdMetric(unittest.TestCase):
    def pos(self, metric):
        if metric.coordKind() is gyoto.core.GYOTO_COORDKIND_SPHERICAL:
            pos=(10, 6., numpy.pi/4, numpy.pi/3)
        else:
            pos=(10, 6, 2, 4.)
        return pos

    def metric(self, cls):
        metric=cls()
        # All Kerr-like: use non-zero spin
        try:
            metric.spin(0.5)
        except AttributeError:
            pass
        # Chern-Simons: non-zero dzeta
        try:
            metric.dzetaCS(0.5)
        except AttributeError:
            pass
        # Complex: add two metrics
        try:
            metric.append(gyoto.std.KerrBL())
            metric.append(gyoto.std.KerrBL())
        except AttributeError:
            pass
        # Shift: add a submetric
        if (cls == gyoto.std.Shift):
            metric.subMetric(gyoto.std.KerrKS())
            metric.offset((1., 1., 1., 1.))
        return metric

    def invalid(self, classname, cls):
        return (not inspect.isclass(cls)
                or not issubclass(cls, gyoto.core.Metric))

    def test_christoffel(self):
        nspace=gyoto.std
        for classname, cls in inspect.getmembers(nspace):
            if (self.invalid(classname, cls)):
                continue
            metric=self.metric(cls)
            try:
                gyoto.metric.check_christoffel(metric, poslist=(self.pos(metric),), epsilon=1e-7)
            except AssertionError as e:
                self.fail(e.__str__())
            pos=self.pos(metric)
            G=metric.christoffel(pos)
            G2=numpy.ones((4,4,4))
            retval=metric.christoffel(G2, pos)
            self.assertEqual(retval, 0, 'christoffel errors out')
            for a in range(4):
                for mu in range(4):
                    for nu in range(4):
                        self.assertAlmostEqual(metric.christoffel(pos, a, mu, nu), G[a, mu, nu], 7, classname)
                        self.assertAlmostEqual(metric.christoffel(pos, a, mu, nu), G2[a, mu, nu], 7, classname)

    def test_jacobian(self):
        nspace=gyoto.std
        for classname, cls in inspect.getmembers(nspace):
            if (self.invalid(classname, cls)):
                continue
            metric=self.metric(cls)
            pos=self.pos(metric)
            jac=metric.jacobian(pos)
            jacn=gyoto.metric.jacobian_numerical(metric, pos, epsilon=1e-7)
            for a in range(4):
                for m in range(4):
                    for n in range(4):
                        self.assertAlmostEqual(jac[a,m,n],
                                               jacn[a,m,n], 7, classname)

    def test_gmunu(self):
        nspace=gyoto.std
        for classname, cls in inspect.getmembers(nspace):
            if (self.invalid(classname, cls)):
                continue
            metric=self.metric(cls)
            pos=self.pos(metric)
            g=metric.gmunu(pos)
            for mu in range(4):
                for nu in range(4):
                    self.assertAlmostEqual(metric.gmunu(pos, mu, nu), g[mu, nu], 7, classname)

    def test_gmunu_up(self):
        nspace=gyoto.std
        for classname, cls in inspect.getmembers(nspace):
            if (self.invalid(classname, cls)):
                continue
            metric=self.metric(cls)
            pos=self.pos(metric)
            gup=metric.gmunu_up(pos)
            gup2, jac=metric.gmunu_up_and_jacobian(pos)
            g=metric.gmunu(pos)
            gup3=numpy.linalg.inv(g)
            for mu in range(4):
                for nu in range(4):
                    self.assertAlmostEqual(metric.gmunu_up(pos, mu, nu), gup[mu, nu], 7,
                                           "class: {}, mu: {}, nu: {} ({})".format(classname, mu, nu, 'coef/matrix'))
                    self.assertAlmostEqual(gup3[mu, nu], gup2[mu, nu], 7,
                                           "class: {}, mu: {}, nu: {} ({})".format(classname, mu, nu, 'inv(g)/*_and_jacobian'))
                    self.assertAlmostEqual(gup2[mu, nu], gup[mu, nu], 7,
                                           "class: {}, mu: {}, nu: {} ({})".format(classname, mu, nu, 'matrix/*_and_jacobian'))

    def test_gmunu_gmunu_up(self):
        nspace=gyoto.std
        I=numpy.zeros((4,4))
        for i in range(4):
            I[i,i]=1.
        for classname, cls in inspect.getmembers(nspace):
            if (self.invalid(classname, cls)):
                continue
            metric=self.metric(cls)
            pos=self.pos(metric)
            g=metric.gmunu(pos)
            gup=metric.gmunu_up(pos)
            self.assertAlmostEqual(numpy.abs(numpy.linalg.multi_dot((g, gup))-I).max(), 0.)