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

/// 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)
    }
}

impl<T> Tensor<T, ContravariantIndex>
where
    T: MetricSystem,
    T::Dimension: Pow<U1> + Pow<U2> + Pow<U3> + Unsigned,
    Exp<T::Dimension, U1>: ArrayLength<f64>,
    Exp<T::Dimension, U2>: ArrayLength<f64>,
    Exp<T::Dimension, U3>: ArrayLength<f64>,
{
    pub fn square(&self) -> f64 {
        let g = T::g(self.get_point());
        let temp = inner!(_, _; U1, U2; g, self.clone());
        *inner!(_, _; U0, U1; temp, self.clone())
    }

    pub fn normalize(&mut self) {
        let len = self.square().abs().sqrt();
        for i in 0..T::Dimension::to_usize() {
            self[i] /= len;
        }
    }
}

impl<T> Tensor<T, CovariantIndex>
where
    T: MetricSystem,
    T::Dimension: Pow<U1> + Pow<U2> + Pow<U3> + Unsigned,
    Exp<T::Dimension, U1>: ArrayLength<f64>,
    Exp<T::Dimension, U2>: ArrayLength<f64>,
    Exp<T::Dimension, U3>: ArrayLength<f64>,
{
    pub fn square(&self) -> f64 {
        let g = T::inv_g(self.get_point());
        let temp = inner!(_, _; U1, U2; g, self.clone());
        *inner!(_, _; U0, U1; temp, self.clone())
    }

    pub fn normalize(&mut self) {
        let len = self.square().abs().sqrt();
        for i in 0..T::Dimension::to_usize() {
            self[i] /= len;
        }
    }
}