### added theory and udo stuff

parent 3d25206a
Pipeline #105129 failed with stage
in 23 seconds
 class ChristoffelSymbol2ndKind(object): """ Calculate Christoffel Gamma_ij^k symbols of metric g """ def __init__(self, g, x): self.g = g self.x = x def ddu(self, i, j, k): g = self.g x = self.x chr = 0 for m in [0, 1, 2, 3]: chr += ( g.uu(k, m) / 2 * ( g.dd(m, i).diff(x[j]) + g.dd(m, j).diff(x[i]) - g.dd(i, j).diff(x[m]) ) ) return chr.simplify()
This diff is collapsed.
 import numpy as np import sympy as sym from sympy.matrices import dotprodsimp class Metric(object): """ Represents a metric. Parameters ---------- symbols : numpy array or array_like of sympy.core.symbol.Symbol The symbols representing the different dimensions, e.g. `sympy.symbols('r theta z', real=True)`. Note that it is good to provide as much information on the symbols as possible, e.g. in the aforementioned example that 'r' is positive m : Matrix The (covariant) metric represented by a Matrix to_base_point : numpy array or array_like, optional The transformation equation to get from the current coordinate system into the base coordinate system. Should be expressed using the `symbols` and sympy functions for, e.g., the cosine. Note that not providing this may limit the scope on subsequent operations with this metric. Returns ------- Metric the (analytical) metric as derived from the transformation for a point into the base coordinate system """ def __init__(self, symbols, m: sym.Matrix, to_base_point=None): self.gdd = m # covariant print("Inverting metric...") # necessary due to a regression in sympy with dotprodsimp(False): self.guu = m.inv() # contravariant print("Metric successfully inverted") self.symbols = symbols self.to_base_point = to_base_point @staticmethod def fromTransformation(symbols, to_base_point): """ Creates a new metric from the given to_base_point transformation. Parameters ---------- symbols : numpy array or array_like of sympy.core.symbol.Symbol The symbols representing the different dimensions, e.g. `sympy.symbols('r theta z', real=True)` to_base_point : numpy array or array_like The transformation equation to get from the current coordinate system into the base coordinate system. Should be expressed using the `symbols` and sympy functions for, e.g., the cosine. Returns ------- Metric the (analytical) metric as derived from the transformation for a point into the base coordinate system """ if to_base_point is None: raise Exception("You need to specify the point-to-base-transformation!") print("Reconstructing metric from point-to-base-transformation...") dim = len(to_base_point) partial_derivatives = [[0 for i in range(dim)] for i in range(dim)] for i in range(dim): for j in range(dim): partial_derivatives[i][j] = sym.diff(to_base_point[j], symbols[i]) g = [[0 for i in range(dim)] for i in range(dim)] for i in range(dim): for j in range(dim): for k in range(dim): g[i][j] += partial_derivatives[i][k] * partial_derivatives[j][k] g[j][i] = g[i][j] = g[i][j].simplify() print("Metric successfully reconstructed!") return Metric(symbols, sym.Matrix(g), to_base_point) def __str__(self): """ Creates a representative string of the (covariant) metric. Returns ------- str the analytic metric wth covariant components represented by a string """ return "g_dd = " + str(self.gdd) def dd(self, i, j): """ Gets the covariant metric coefficient at the specified position. Parameters ---------- i : int The row index in the metric j : int The column index in the metric Returns ------- sympy expression the (analytical) covariant metric coefficient g_ij """ return self.gdd[i, j] def uu(self, i, j): """ Gets the contravariant metric coefficient at the specified position. Parameters ---------- i : int The row index in the metric j : int The column index in the metric Returns ------- sympy expression the (analytical) contravariant metric coefficient g^ij """ return self.guu[i, j] def dim(self) -> int: """ Gets the dimensionality of the metric. For example, a metric with size 3x3 will be of dimensionality 3. Returns ------- int the dimension of the metric """ return int(np.sqrt(len(self.gdd)))
 import sys from sympy import * import numpy as np from collections import OrderedDict import re from sympy.matrices import dotprodsimp from sympyTensors import Metric r = symbols("r", real=True, positive=True) theta = symbols("theta", real=True) z = symbols("z", real=True) from sympy.vector import gradient from sympy.vector import CoordSys3D c = CoordSys3D("c", transformation="cylindrical", variable_names=("r", "theta", "z")) gradient(c.r)
This diff is collapsed.
 import sys from sympy import * import numpy as np from collections import OrderedDict import re from sympy.matrices import dotprodsimp from sympyTensors import Metric r = symbols("r", real=True, positive=True) theta = symbols("theta", real=True) z = symbols("z", real=True) g = [[1, 0, 0], [0, r ** 2, 0], [0, 0, 1]] # g = [[1,0,0],[0,1,0],[0,0,1]] syms = [r, theta, z] metric = Metric(symbols, Matrix(g)) # sigma = [[1,2,3],[1.5,3,4.5],[2,4,6]] sigma = [[2, 3, 4], [3, 4.5, 6], [4, 6, 8]] result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(3): for j in range(3): result[i][j] += ( sigma[j] * metric.guu[0, i] + sigma[j] * metric.guu[1, i] + sigma[j] * metric.guu[2, i] ) print("sigma^i_j " + str(result)) sigma = result result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(3): for j in range(3): result[i][j] += ( sigma[i] * metric.guu[0, j] + sigma[i] * metric.guu[1, j] + sigma[i] * metric.guu[2, j] ) print("sigma^ij " + str(result)) sigma = result result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(3): for j in range(3): result[i][j] += ( sigma[j] * metric.gdd[0, i] + sigma[j] * metric.gdd[1, i] + sigma[j] * metric.gdd[2, i] ) print("sigma_i^j " + str(result)) sigma = result result = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] for i in range(3): for j in range(3): result[i][j] += ( sigma[i] * metric.gdd[0, j] + sigma[i] * metric.gdd[1, j] + sigma[i] * metric.gdd[2, j] ) print("sigma_ij " + str(result)) print("Done")
 import matplotlib.pyplot as plt import numpy as np import sympy as sym r = np.linspace(-1.6, np.pi, 1000) theta = np.linspace(-np.pi, np.pi, len(r)) plt.plot(r, np.arctan2(theta, r)) plt.plot(r, -2 * np.arctan((r - np.sqrt(r ** 2 + theta ** 2)) / theta)) plt.show()