summaryrefslogtreecommitdiff
path: root/curvature.py
blob: e9d8e175da067db7753ef1b4f044ebd814df2ad6 (plain)
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
# -*- coding: utf-8 -*-

import numpy as np

from . import diff
from . import utils

def calc_christoffel(x, z, metric):
    """
    Calculate Christoffel symbols

     i    1 il /                       \
    Γ   = -γ   | ∂ γ   + ∂ γ   - ∂ γ   |
     jk   2    \  j kl    k jl    l jk /

    using finite differences.

    :param array_like x: 1D array of x coordinates.
    :param array_like z: 1D array of z-coordinates.
    :param array_like metric: 4D array of spatial metric values at the grid
                              formed by x and z. metric[i, j, k, l] is the ijth
                              component of the metric at the point (X=x[l],
                              Z=z[k]).
    :rtype: array_like, shape (3, 3, 3, z.shape[0], x.shape[0])
    :return: Christoffel symbols, first axis is the upper index, following two
             axes are the two lower indices, final two axes correspond to the z
             and x grid points respectively.
    """
    X, Z  = np.meshgrid(x, z)

    dmetric = np.zeros((3,) + metric.shape)
    dmetric[0] = diff.fd4(metric, 3, x[1] - x[0])
    dmetric[2] = diff.fd4(metric, 2, z[1] - z[0])

    dmetric[1, 0, 0] = 0.0
    dmetric[1, 1, 1] = 0.0
    dmetric[1, 2, 2] = 0.0
    dmetric[1, 0, 1] = np.where(np.abs(X) > 1e-8, (metric[0, 0] - metric[1, 1]) / X, dmetric[0, 0, 0] - dmetric[0, 1, 1])
    dmetric[1, 1, 0] = dmetric[1, 0, 1]
    dmetric[1, 0, 2] = 0.0
    dmetric[1, 2, 0] = 0.0
    dmetric[1, 1, 2] = np.where(np.abs(X) > 1e-8, metric[0, 2] / X, dmetric[0, 0, 2])
    dmetric[1, 2, 1] = dmetric[1, 1, 2]

    metric_u = utils.matrix_invert(metric)

    Gamma = np.empty_like(dmetric)
    for i in range(3):
        for j in range(3):
            for k in range(3):
                Gamma[i, j, k] = 0.5 * np.einsum('k...,k...', metric_u[i], dmetric[j, k] + dmetric[k, j] - dmetric[:, k, j])

    return Gamma