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
use super::coordinates::{CoordinateSystem, Point};
use super::tensors::{ContravariantIndex, CovariantIndex, InnerProduct, InvTwoForm, Tensor, TwoForm};
use generic_array::ArrayLength;
use typenum::{Exp, Pow};
use typenum::consts::{U1, U2, U3};

/// Trait representing the metric properties of the coordinate system
pub trait MetricSystem: CoordinateSystem
    where <Self as CoordinateSystem>::Dimension: Pow<U2> + Pow<U3>,
          Exp<<Self as CoordinateSystem>::Dimension, U2>: ArrayLength<f64>,
          Exp<<Self as CoordinateSystem>::Dimension, U3>: ArrayLength<f64>
{
    /// Returns the metric tensor at a given point.
    fn g(point: &Point<Self>) -> TwoForm<Self>;

    /// Returns the inverse metric tensor at a given point.
    ///
    /// The default implementation calculates the metric and then inverts it. A direct
    /// implementation may be desirable for more performance.
    fn inv_g(point: &Point<Self>) -> InvTwoForm<Self> {
        Self::g(point).inverse().unwrap()
    }

    /// Returns the partial derivatives of the metric at a given point.
    ///
    /// The default implementation calculates them numerically. A direct implementation
    /// may be desirable for performance.
    fn dg(point: &Point<Self>) -> Tensor<Self, (CovariantIndex, (CovariantIndex, CovariantIndex))> {
        let d = Self::dimension();
        let mut result = Tensor::zero(point.clone());
        let h = Self::small(point);

        for j in 0..d {
            let mut x = point.clone();
            x[j] = x[j] - h;
            let g1 = Self::g(&x);

            x[j] = x[j] + h * 2.0;
            let g2 = Self::g(&x);

            for coord in g1.iter_coords() {
                // calculate dg_i/dx^j
                let index = [coord[0], coord[1], j];
                result[&index[..]] = (g2[&*coord] - g1[&*coord]) / (2.0 * h);
            }
        }

        result
    }

    /// Returns the covariant Christoffel symbols (with three lower indices).
    ///
    /// The default implementation calculates them from the metric. A direct implementation
    /// may be desirable for performance.
    fn covariant_christoffel
        (point: &Point<Self>)
         -> Tensor<Self, (CovariantIndex, (CovariantIndex, CovariantIndex))> {
        let dg = Self::dg(point);
        let mut result = Tensor::<Self,
                                  (CovariantIndex,
                                   (CovariantIndex,
                                    CovariantIndex))>::zero(point.clone());

        for i in result.iter_coords() {
            result[&*i] = 0.5 *
                          (dg[&*i] + dg[&[i[0], i[2], i[1]][..]] - dg[&[i[1], i[2], i[0]][..]]);
        }

        result
    }

    /// Returns the Christoffel symbols.
    ///
    /// The default implementation calculates them from the metric. A direct implementation
    /// may be desirable for performance.
    fn christoffel(point: &Point<Self>)
                   -> Tensor<Self, (ContravariantIndex, (CovariantIndex, CovariantIndex))> {
        let ig = Self::inv_g(point);
        let gamma = Self::covariant_christoffel(point);

        <InvTwoForm<Self> as InnerProduct<Tensor<Self,
                                                                      (CovariantIndex,
                                                                       (CovariantIndex,
                                                                        CovariantIndex))>,
                                                               U1,
                                                               U2>>::inner_product(ig, gamma)
    }
}