aeon-tensor 0.3.3

Helper package for working with tensor-based PDEs in aeon-tk binaries
Documentation
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Module containing common operations on manifolds with metrics.

use crate::{
    Gen, Sym, SymSym, SymVec, Tensor, TensorIndex, TensorStorageOwned, TensorStorageRef, VecSym,
    VecSymVec,
};

mod dims;

pub use dims::d2;

pub trait Space<const N: usize>: Clone + Copy {
    type VecStore: TensorStorageOwned + Default + Clone;
    type MatStore: TensorStorageOwned + Default + Clone;
    type SymStore: TensorStorageOwned + Default + Clone;
    type SymVecStore: TensorStorageOwned + Default + Clone;
    type SymSymStore: TensorStorageOwned + Default + Clone;
    type SymVecVecStore: TensorStorageOwned + Default + Clone;

    /// Sums over all indices of the given rank.
    fn sum<const R: usize>(f: impl Fn([usize; R]) -> f64) -> f64 {
        let mut result = 0.0;
        <Gen as TensorIndex<N, R>>::for_each_index(|idx| result += f(idx));
        result
    }

    /// Constructs an `N` dimensional vector.
    fn vector(f: impl Fn([usize; 1]) -> f64) -> Tensor<N, 1, Gen, Self::VecStore> {
        Tensor::from_fn(f)
    }

    /// Constructs an `N` dimensional symmetric matrix.
    fn symmetric(f: impl Fn([usize; 2]) -> f64) -> Tensor<N, 2, Sym, Self::SymStore> {
        Tensor::from_fn(f)
    }

    /// Constructs an `N` dimensional general matrix.
    fn matrix(f: impl Fn([usize; 2]) -> f64) -> Tensor<N, 2, Gen, Self::MatStore> {
        Tensor::from_fn(f)
    }
}

/// Space where all tensors are stored statically and unboxed.
#[derive(Clone, Copy)]
pub struct Static;

/// The number of components of an `n` dimensional symmetric matrix.
const fn sym(n: usize) -> usize {
    n * (n + 1) / 2
}

macro_rules! impl_space {
    ($N:literal) => {
        impl Space<$N> for Static {
            type VecStore = [f64; const { $N }];
            type MatStore = [f64; const { $N * $N }];
            type SymStore = [f64; const { sym($N) }];
            type SymVecStore = [f64; const { sym($N) * $N }];
            type SymSymStore = [f64; const { sym($N) * sym($N) }];
            type SymVecVecStore = [f64; const { sym($N) * $N * $N }];
        }
    };
}

impl_space!(1);
impl_space!(2);

/// A metric (along with first and second derivatives) defined on a point on a manifold.
pub struct Metric<const N: usize, S: Space<N>> {
    pub value: Tensor<N, 2, Sym, S::SymStore>,
    pub derivs: Tensor<N, 3, SymVec, S::SymVecStore>,
    pub derivs2: Tensor<N, 4, SymSym, S::SymSymStore>,
}

impl<const N: usize, S: Space<N>> Metric<N, S> {
    /// Constructs a new metric from the constituent components and partial derivatives.
    pub fn new(
        value: Tensor<N, 2, Sym, S::SymStore>,
        derivs: Tensor<N, 3, SymVec, S::SymVecStore>,
        derivs2: Tensor<N, 4, SymSym, S::SymSymStore>,
    ) -> Self {
        Self {
            value,
            derivs,
            derivs2,
        }
    }
}

impl<S: Space<2>> Metric<2, S> {
    /// Computes the determinate of a metric.
    pub fn det(&self) -> MetricDet<2, S> {
        let value = self.value[[0, 0]] * self.value[[1, 1]] - self.value[[1, 0]].powi(2);
        let derivs = Tensor::from_fn(|[a]| {
            self.derivs[[0, 0, a]] * self.value[[1, 1]]
                + self.value[[0, 0]] * self.derivs[[1, 1, a]]
                - 2.0 * self.value[[0, 1]] * self.derivs[[0, 1, a]]
        });

        MetricDet { value, derivs }
    }

    /// Computes the inverse of a metric.
    pub fn inv(&self, det: &MetricDet<2, S>) -> MetricInv<2, S> {
        let factor = det.value.recip();
        let factor_derivs: Tensor<2, 1, Gen, S::VecStore> =
            Tensor::from_fn(|[a]| -factor.powi(2) * det.derivs[[a]]);

        let mut value = Tensor::new();
        value[[0, 0]] = factor * self.value[[1, 1]];
        value[[1, 0]] = -factor * self.value[[1, 0]];
        debug_assert_eq!(value[[1, 0]], value[[0, 1]]);
        value[[1, 1]] = factor * self.value[[0, 0]];

        let mut derivs = Tensor::new();

        for a in 0..2 {
            derivs[[0, 0, a]] =
                factor_derivs[[a]] * self.value[[1, 1]] + factor * self.derivs[[1, 1, a]];
            derivs[[1, 0, a]] =
                -factor_derivs[[a]] * self.value[[1, 0]] - factor * self.derivs[[1, 0, a]];
            debug_assert_eq!(derivs[[0, 1, a]], derivs[[1, 0, a]]);
            derivs[[1, 1, a]] =
                factor_derivs[[a]] * self.value[[0, 0]] + factor * self.derivs[[0, 0, a]];
        }

        MetricInv { value, derivs }
    }
}

impl<const N: usize, S: Space<N>> Metric<N, S> {
    // Computes killing's equation 𝓛ₓgₐᵦ for the given vector field X.
    pub fn killing(&self, vector: &VectorC1<N, S>) -> Tensor<N, 2, Sym, S::SymStore> {
        SymmetricC1 {
            value: self.value.clone(),
            derivs: self.derivs.clone(),
        }
        .lie_derivative(vector)
    }
}

/// The determinate of a metric, along with partial derivatives.
pub struct MetricDet<const N: usize, S: Space<N>> {
    pub value: f64,
    pub derivs: Tensor<N, 1, Gen, S::VecStore>,
}

/// The inverse of a metric, along with partial derivatives.
pub struct MetricInv<const N: usize, S: Space<N>> {
    pub value: Tensor<N, 2, Sym, S::SymStore>,
    pub derivs: Tensor<N, 3, SymVec, S::SymVecStore>,
}

impl<const N: usize, S: Space<N>> MetricInv<N, S> {
    /// Computes the trace of a fully covariant 2-tensor.
    pub fn cotrace<I: TensorIndex<N, 2>, St: TensorStorageRef>(
        &self,
        matrix: &Tensor<N, 2, I, St>,
    ) -> f64 {
        S::sum(|[a, b]| self.value[[a, b]] * matrix[[a, b]])
    }

    /// Raises the first index of a general r-tensor.
    pub fn raise_first<const R: usize, I: TensorIndex<N, R>, St: TensorStorageOwned + Default>(
        &self,
        tensor: &Tensor<N, R, I, St>,
    ) -> Tensor<N, R, I, St> {
        const {
            if R == 0 {
                panic!("R must be > 0");
            }
        }

        Tensor::from_fn(|idx| {
            S::sum(|[a]| {
                let mut tidx = idx;
                tidx[0] = a;
                self.value[[idx[0], a]] * tensor[tidx]
            })
        })
    }

    /// Raises the last index of a general r-tensor.
    pub fn raise_last<const R: usize, I: TensorIndex<N, R>, St: TensorStorageOwned + Default>(
        &self,
        tensor: &Tensor<N, R, I, St>,
    ) -> Tensor<N, R, I, St> {
        const {
            if R == 0 {
                panic!("R must be > 0");
            }
        }

        Tensor::from_fn(|idx| {
            S::sum(|[a]| {
                let mut tidx = idx;
                tidx[R - 1] = a;
                self.value[[idx[R - 1], a]] * tensor[tidx]
            })
        })
    }
}

/// Christoffel connection symbols and their derivatives defined on
/// a general metric.
pub struct ChristoffelSymbol<const N: usize, S: Space<N>> {
    pub first_kind: Tensor<N, 3, VecSym, S::SymVecStore>,
    pub first_kind_derivs: Tensor<N, 4, VecSymVec, S::SymVecVecStore>,
    pub second_kind: Tensor<N, 3, VecSym, S::SymVecStore>,
    pub second_kind_derivs: Tensor<N, 4, VecSymVec, S::SymVecVecStore>,
}

impl<const N: usize, S: Space<N>> ChristoffelSymbol<N, S> {
    /// Computes the ricci tensor from the christoffel symbols.
    pub fn ricci(&self) -> Tensor<N, 2, Sym, S::SymStore> {
        Tensor::from_eq(|[i, j], [a]| {
            let term1: f64 =
                self.second_kind_derivs[[a, i, j, a]] - self.second_kind_derivs[[a, a, i, j]];
            let term2 = S::sum(|[b]| {
                self.second_kind[[a, a, b]] * self.second_kind[[b, i, j]]
                    - self.second_kind[[a, i, b]] * self.second_kind[[b, a, j]]
            });

            term1 + term2
        })
    }
}

impl<const N: usize, S: Space<N>> Metric<N, S> {
    /// Computes Christoffel_symbols for a given metric.
    pub fn christoffel_symbol(&self, inv: &MetricInv<N, S>) -> ChristoffelSymbol<N, S> {
        let first_kind = Tensor::from_fn(|[a, b, c]| {
            0.5 * (self.derivs[[a, c, b]] + self.derivs[[b, a, c]] - self.derivs[[b, c, a]])
        });
        let first_kind_derivs = Tensor::from_fn(|[a, b, c, d]| {
            0.5 * (self.derivs2[[a, c, b, d]] + self.derivs2[[b, a, c, d]]
                - self.derivs2[[b, c, a, d]])
        });

        let second_kind =
            Tensor::from_eq(|[a, b, c], [m]| inv.value[[a, m]] * first_kind[[m, b, c]]);

        let second_kind_derivs = Tensor::from_eq(|[a, b, c, d], [m]| {
            inv.derivs[[a, m, d]] * first_kind[[m, b, c]]
                + inv.value[[a, m]] * first_kind_derivs[[m, b, c, d]]
        });

        ChristoffelSymbol {
            first_kind,
            first_kind_derivs,
            second_kind,
            second_kind_derivs,
        }
    }
}

/// A C1 scalar field at a single point.
#[derive(Debug)]
pub struct ScalarC1<const N: usize, S: Space<N>> {
    pub value: f64,
    pub derivs: Tensor<N, 1, Gen, S::VecStore>,
}

impl<const N: usize, S: Space<N>> ScalarC1<N, S> {
    pub fn gradient(&self, _connect: &ChristoffelSymbol<N, S>) -> Tensor<N, 1, Gen, S::VecStore> {
        self.derivs.clone()
    }

    pub fn lie_derivative(&self, flow: &VectorC1<N, S>) -> f64 {
        S::sum(|[a]| flow.value[[a]] * self.derivs[[a]])
    }
}

impl<const N: usize, S: Space<N>> From<ScalarC2<N, S>> for ScalarC1<N, S> {
    fn from(value: ScalarC2<N, S>) -> Self {
        Self {
            value: value.value,
            derivs: value.derivs,
        }
    }
}

impl<const N: usize, S: Space<N>> Default for ScalarC1<N, S> {
    fn default() -> Self {
        Self {
            value: Default::default(),
            derivs: Default::default(),
        }
    }
}

impl<const N: usize, S: Space<N>> Clone for ScalarC1<N, S> {
    fn clone(&self) -> Self {
        Self {
            value: self.value.clone(),
            derivs: self.derivs.clone(),
        }
    }
}

/// A C2 scalar field at a single point.
pub struct ScalarC2<const N: usize, S: Space<N>> {
    pub value: f64,
    pub derivs: Tensor<N, 1, Gen, S::VecStore>,
    pub derivs2: Tensor<N, 2, Sym, S::SymStore>,
}

impl<const N: usize, S: Space<N>> ScalarC2<N, S> {
    pub fn gradient(&self, _connect: &ChristoffelSymbol<N, S>) -> Tensor<N, 1, Gen, S::VecStore> {
        self.derivs.clone()
    }

    pub fn lie_derivative(&self, flow: &VectorC1<N, S>) -> f64 {
        S::sum(|[a]| flow.value[[a]] * self.derivs[[a]])
    }

    pub fn hessian(&self, connect: &ChristoffelSymbol<N, S>) -> Tensor<N, 2, Sym, S::SymStore> {
        Tensor::from_fn(|[a, b]| {
            let term1 = self.derivs2[[a, b]];
            let term2 = S::sum(|[d]| -connect.second_kind[[d, a, b]] * self.derivs[[d]]);
            term1 + term2
        })
    }
}

impl<const N: usize, S: Space<N>> Default for ScalarC2<N, S> {
    fn default() -> Self {
        Self {
            value: Default::default(),
            derivs: Default::default(),
            derivs2: Default::default(),
        }
    }
}

impl<const N: usize, S: Space<N>> Clone for ScalarC2<N, S> {
    fn clone(&self) -> Self {
        Self {
            value: self.value.clone(),
            derivs: self.derivs.clone(),
            derivs2: self.derivs2.clone(),
        }
    }
}

/// A C1 vector field at a single point.
pub struct VectorC1<const N: usize, S: Space<N>> {
    pub value: Tensor<N, 1, Gen, S::VecStore>,
    pub derivs: Tensor<N, 2, Gen, S::MatStore>,
}

impl<const N: usize, S: Space<N>> VectorC1<N, S> {
    pub fn gradient(&self, connect: &ChristoffelSymbol<N, S>) -> Tensor<N, 2, Gen, S::MatStore> {
        Tensor::from_fn(|[a, c]| {
            let term1 = self.derivs[[a, c]];
            let term2 = S::sum(|[d]| -connect.second_kind[[d, a, c]] * self.value[[d]]);
            term1 + term2
        })
    }

    pub fn lie_derivative(&self, flow: &VectorC1<N, S>) -> Tensor<N, 1, Gen, S::VecStore> {
        Tensor::from_fn(|[a]| {
            S::sum(|[i]| {
                flow.value[[i]] * self.derivs[[a, i]] + flow.derivs[[i, a]] * self.value[[i]]
            })
        })
    }
}

impl<const N: usize, S: Space<N>> Default for VectorC1<N, S> {
    fn default() -> Self {
        Self {
            value: Default::default(),
            derivs: Default::default(),
        }
    }
}

impl<const N: usize, S: Space<N>> Clone for VectorC1<N, S> {
    fn clone(&self) -> Self {
        Self {
            value: self.value.clone(),
            derivs: self.derivs.clone(),
        }
    }
}

/// A C1 symmetric matrix field at a single point.
pub struct SymmetricC1<const N: usize, S: Space<N>> {
    pub value: Tensor<N, 2, Sym, S::SymStore>,
    pub derivs: Tensor<N, 3, SymVec, S::SymVecStore>,
}

impl<const N: usize, S: Space<N>> SymmetricC1<N, S> {
    pub fn gradient(
        &self,
        connect: &ChristoffelSymbol<N, S>,
    ) -> Tensor<N, 3, SymVec, S::SymVecStore> {
        Tensor::from_fn(|[i, j, k]| {
            self.derivs[[i, j, k]]
                - S::sum(|[m]| connect.second_kind[[m, i, k]] * self.value[[m, j]])
                - S::sum(|[m]| connect.second_kind[[m, j, k]] * self.value[[m, i]])
        })
    }

    pub fn lie_derivative(&self, flow: &VectorC1<N, S>) -> Tensor<N, 2, Sym, S::SymStore> {
        Tensor::from_fn(|[i, j]| {
            S::sum(|[m]| {
                flow.value[[m]] * self.derivs[[i, j, m]]
                    + self.value[[m, j]] * flow.derivs[[m, i]]
                    + self.value[[i, m]] * flow.derivs[[m, j]]
            })
        })
    }
}

impl<const N: usize, S: Space<N>> Default for SymmetricC1<N, S> {
    fn default() -> Self {
        Self {
            value: Default::default(),
            derivs: Default::default(),
        }
    }
}

impl<const N: usize, S: Space<N>> Clone for SymmetricC1<N, S> {
    fn clone(&self) -> Self {
        Self {
            value: self.value.clone(),
            derivs: self.derivs.clone(),
        }
    }
}