Example #1
0
    def __init__(self,
                 twoform: Expr = None,
                 matrix: Array = None,
                 coord_system: coords.CoordSystem = None,
                 components: Tuple[Expr, ...] = None):
        """Create a Metric"""
        if twoform is None and matrix is None:
            raise ValueError(
                'Must specify either twoform or matrix to produce metric')

        # Construct twoform if none given
        if twoform is None:
            if not isinstance(matrix, MatrixBase):
                matrix = Matrix(matrix)
            if coord_system is None:
                raise ValueError(
                    'Must specify coord system if constructing metric from matrix'
                )
            twoform = matrix_to_twoform(matrix, coord_system.base_oneforms(
            ))  # TODO check ordering of base oneforms?

        # Construct matrix if none given
        if matrix is None:
            matrix = twoform_to_matrix(twoform)
            coord_system = coords.CoordSystem.from_twoform(twoform)

        # Set instance attributes
        self._twoform = twoform
        self._matrix = matrix
        self._inverse = None  # lazy caching of inverse matrix
        self.coord_system = coord_system
        self.components = components
Example #2
0
def test_helpers_and_coordinate_dependent():
    one_form = R2.dr + R2.dx
    two_form = Differential(R2.x * R2.dr + R2.r * R2.dx)
    three_form = Differential(R2.y * two_form) + Differential(
        R2.x * Differential(R2.r * R2.dr))
    metric = TensorProduct(R2.dx, R2.dx) + TensorProduct(R2.dy, R2.dy)
    metric_ambig = TensorProduct(R2.dx, R2.dx) + TensorProduct(R2.dr, R2.dr)
    misform_a = TensorProduct(R2.dr, R2.dr) + R2.dr
    misform_b = R2.dr**4
    misform_c = R2.dx * R2.dy
    twoform_not_sym = TensorProduct(R2.dx, R2.dx) + TensorProduct(R2.dx, R2.dy)
    twoform_not_TP = WedgeProduct(R2.dx, R2.dy)

    one_vector = R2.e_x + R2.e_y
    two_vector = TensorProduct(R2.e_x, R2.e_y)
    three_vector = TensorProduct(R2.e_x, R2.e_y, R2.e_x)
    two_wp = WedgeProduct(R2.e_x, R2.e_y)

    assert covariant_order(one_form) == 1
    assert covariant_order(two_form) == 2
    assert covariant_order(three_form) == 3
    assert covariant_order(two_form + metric) == 2
    assert covariant_order(two_form + metric_ambig) == 2
    assert covariant_order(two_form + twoform_not_sym) == 2
    assert covariant_order(two_form + twoform_not_TP) == 2

    assert contravariant_order(one_vector) == 1
    assert contravariant_order(two_vector) == 2
    assert contravariant_order(three_vector) == 3
    assert contravariant_order(two_vector + two_wp) == 2

    raises(ValueError, lambda: covariant_order(misform_a))
    raises(ValueError, lambda: covariant_order(misform_b))
    raises(ValueError, lambda: covariant_order(misform_c))

    assert twoform_to_matrix(metric) == Matrix([[1, 0], [0, 1]])
    assert twoform_to_matrix(twoform_not_sym) == Matrix([[1, 0], [1, 0]])
    assert twoform_to_matrix(twoform_not_TP) == Matrix([[0, -1], [1, 0]])

    raises(ValueError, lambda: twoform_to_matrix(one_form))
    raises(ValueError, lambda: twoform_to_matrix(three_form))
    raises(ValueError, lambda: twoform_to_matrix(metric_ambig))

    raises(ValueError, lambda: metric_to_Christoffel_1st(twoform_not_sym))
    raises(ValueError, lambda: metric_to_Christoffel_2nd(twoform_not_sym))
    raises(ValueError, lambda: metric_to_Riemann_components(twoform_not_sym))
    raises(ValueError, lambda: metric_to_Ricci_components(twoform_not_sym))
Example #3
0
def test_helpers_and_coordinate_dependent():
    one_form = R2.dr + R2.dx
    two_form = Differential(R2.x*R2.dr + R2.r*R2.dx)
    three_form = Differential(
        R2.y*two_form) + Differential(R2.x*Differential(R2.r*R2.dr))
    metric = TensorProduct(R2.dx, R2.dx) + TensorProduct(R2.dy, R2.dy)
    metric_ambig = TensorProduct(R2.dx, R2.dx) + TensorProduct(R2.dr, R2.dr)
    misform_a = TensorProduct(R2.dr, R2.dr) + R2.dr
    misform_b = R2.dr**4
    misform_c = R2.dx*R2.dy
    twoform_not_sym = TensorProduct(R2.dx, R2.dx) + TensorProduct(R2.dx, R2.dy)
    twoform_not_TP = WedgeProduct(R2.dx, R2.dy)

    one_vector = R2.e_x + R2.e_y
    two_vector = TensorProduct(R2.e_x, R2.e_y)
    three_vector = TensorProduct(R2.e_x, R2.e_y, R2.e_x)
    two_wp = WedgeProduct(R2.e_x,R2.e_y)

    assert covariant_order(one_form) == 1
    assert covariant_order(two_form) == 2
    assert covariant_order(three_form) == 3
    assert covariant_order(two_form + metric) == 2
    assert covariant_order(two_form + metric_ambig) == 2
    assert covariant_order(two_form + twoform_not_sym) == 2
    assert covariant_order(two_form + twoform_not_TP) == 2

    assert contravariant_order(one_vector) == 1
    assert contravariant_order(two_vector) == 2
    assert contravariant_order(three_vector) == 3
    assert contravariant_order(two_vector + two_wp) == 2

    raises(ValueError, lambda: covariant_order(misform_a))
    raises(ValueError, lambda: covariant_order(misform_b))
    raises(ValueError, lambda: covariant_order(misform_c))

    assert twoform_to_matrix(metric) == Matrix([[1, 0], [0, 1]])
    assert twoform_to_matrix(twoform_not_sym) == Matrix([[1, 0], [1, 0]])
    assert twoform_to_matrix(twoform_not_TP) == Matrix([[0, -1], [1, 0]])

    raises(ValueError, lambda: twoform_to_matrix(one_form))
    raises(ValueError, lambda: twoform_to_matrix(three_form))
    raises(ValueError, lambda: twoform_to_matrix(metric_ambig))

    raises(ValueError, lambda: metric_to_Christoffel_1st(twoform_not_sym))
    raises(ValueError, lambda: metric_to_Christoffel_2nd(twoform_not_sym))
    raises(ValueError, lambda: metric_to_Riemann_components(twoform_not_sym))
    raises(ValueError, lambda: metric_to_Ricci_components(twoform_not_sym))
Example #4
0
def test_H2():
    TP = sympy.diffgeom.TensorProduct
    R2 = sympy.diffgeom.rn.R2
    y = R2.y
    dy = R2.dy
    dx = R2.dx
    g = (TP(dx, dx) + TP(dy, dy)) * y**(-2)
    automat = twoform_to_matrix(g)
    mat = diag(y**(-2), y**(-2))
    assert mat == automat

    gamma1 = metric_to_Christoffel_1st(g)
    assert gamma1[0][0][0] == 0
    assert gamma1[0][0][1] == -y**(-3)
    assert gamma1[0][1][0] == -y**(-3)
    assert gamma1[0][1][1] == 0

    assert gamma1[1][1][1] == -y**(-3)
    assert gamma1[1][1][0] == 0
    assert gamma1[1][0][1] == 0
    assert gamma1[1][0][0] == y**(-3)

    gamma2 = metric_to_Christoffel_2nd(g)
    assert gamma2[0][0][0] == 0
    assert gamma2[0][0][1] == -y**(-1)
    assert gamma2[0][1][0] == -y**(-1)
    assert gamma2[0][1][1] == 0

    assert gamma2[1][1][1] == -y**(-1)
    assert gamma2[1][1][0] == 0
    assert gamma2[1][0][1] == 0
    assert gamma2[1][0][0] == y**(-1)

    Rm = metric_to_Riemann_components(g)
    assert Rm[0][0][0][0] == 0
    assert Rm[0][0][0][1] == 0
    assert Rm[0][0][1][0] == 0
    assert Rm[0][0][1][1] == 0

    assert Rm[0][1][0][0] == 0
    assert Rm[0][1][0][1] == -y**(-2)
    assert Rm[0][1][1][0] == y**(-2)
    assert Rm[0][1][1][1] == 0

    assert Rm[1][0][0][0] == 0
    assert Rm[1][0][0][1] == y**(-2)
    assert Rm[1][0][1][0] == -y**(-2)
    assert Rm[1][0][1][1] == 0

    assert Rm[1][1][0][0] == 0
    assert Rm[1][1][0][1] == 0
    assert Rm[1][1][1][0] == 0
    assert Rm[1][1][1][1] == 0

    Ric = metric_to_Ricci_components(g)
    assert Ric[0][0] == -y**(-2)
    assert Ric[0][1] == 0
    assert Ric[1][0] == 0
    assert Ric[0][0] == -y**(-2)

    ## scalar curvature is -2
    #TODO - it would be nice to have index contraction built-in
    R = (Ric[0][0] + Ric[1][1]) * y**2
    assert R == -2

    ## Gauss curvature is -1
    assert R / 2 == -1
def test_H2():
    TP = sympy.diffgeom.TensorProduct
    R2 = sympy.diffgeom.rn.R2
    y = R2.y
    dy = R2.dy
    dx = R2.dx
    g = (TP(dx, dx) + TP(dy, dy))*y**(-2)
    automat = twoform_to_matrix(g)
    mat = diag(y**(-2), y**(-2))
    assert mat == automat

    gamma1 = metric_to_Christoffel_1st(g)
    assert gamma1[0, 0, 0] == 0
    assert gamma1[0, 0, 1] == -y**(-3)
    assert gamma1[0, 1, 0] == -y**(-3)
    assert gamma1[0, 1, 1] == 0

    assert gamma1[1, 1, 1] == -y**(-3)
    assert gamma1[1, 1, 0] == 0
    assert gamma1[1, 0, 1] == 0
    assert gamma1[1, 0, 0] == y**(-3)

    gamma2 = metric_to_Christoffel_2nd(g)
    assert gamma2[0, 0, 0] == 0
    assert gamma2[0, 0, 1] == -y**(-1)
    assert gamma2[0, 1, 0] == -y**(-1)
    assert gamma2[0, 1, 1] == 0

    assert gamma2[1, 1, 1] == -y**(-1)
    assert gamma2[1, 1, 0] == 0
    assert gamma2[1, 0, 1] == 0
    assert gamma2[1, 0, 0] == y**(-1)

    Rm = metric_to_Riemann_components(g)
    assert Rm[0, 0, 0, 0] == 0
    assert Rm[0, 0, 0, 1] == 0
    assert Rm[0, 0, 1, 0] == 0
    assert Rm[0, 0, 1, 1] == 0

    assert Rm[0, 1, 0, 0] == 0
    assert Rm[0, 1, 0, 1] == -y**(-2)
    assert Rm[0, 1, 1, 0] == y**(-2)
    assert Rm[0, 1, 1, 1] == 0

    assert Rm[1, 0, 0, 0] == 0
    assert Rm[1, 0, 0, 1] == y**(-2)
    assert Rm[1, 0, 1, 0] == -y**(-2)
    assert Rm[1, 0, 1, 1] == 0

    assert Rm[1, 1, 0, 0] == 0
    assert Rm[1, 1, 0, 1] == 0
    assert Rm[1, 1, 1, 0] == 0
    assert Rm[1, 1, 1, 1] == 0

    Ric = metric_to_Ricci_components(g)
    assert Ric[0, 0] == -y**(-2)
    assert Ric[0, 1] == 0
    assert Ric[1, 0] == 0
    assert Ric[0, 0] == -y**(-2)

    assert Ric == ImmutableDenseNDimArray([-y**(-2), 0, 0, -y**(-2)], (2, 2))

    ## scalar curvature is -2
    #TODO - it would be nice to have index contraction built-in
    R = (Ric[0, 0] + Ric[1, 1])*y**2
    assert R == -2

    ## Gauss curvature is -1
    assert R/2 == -1
# The series in the polar coordinate system:
equations, init_cond = intcurve_diffequ(vector_field, t, start_point, R2_p)
equations
init_cond
#vectors_in_basis
#Transform all base vectors in base vectors of a specified coord basis.
#While the new base vectors are in the new coordinate system basis, any coefficients are kept in the old system.
from sympy.diffgeom import vectors_in_basis
from sympy.diffgeom.rn import R2_r, R2_p
vectors_in_basis(R2_r.e_x, R2_p)
vectors_in_basis(R2_p.e_r, R2_r)
#twoform_to_matrix
from sympy.diffgeom.rn import R2
from sympy.diffgeom import twoform_to_matrix, TensorProduct
TP = TensorProduct
twoform_to_matrix(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy))
twoform_to_matrix(R2.x*TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy))
twoform_to_matrix(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy) - TP(R2.dx, R2.dy)/2)
# metric_to_Christoffel_1st(expr) Return the nested list of Christoffel symbols for the given metric.  This returns the Christoffel symbol of first kind that represents the Levi-Civita connection for the given metric.
from sympy.diffgeom.rn import R2
from sympy.diffgeom import metric_to_Christoffel_1st, TensorProduct
TP = TensorProduct
metric_to_Christoffel_1st(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy))
metric_to_Christoffel_1st(R2.x*TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy))
# metric_to_Riemann_components(expr) Return the components of the Riemann tensor expressed in a given basis.  Given a metric it calculates the components of the Riemann tensor in the canonical basis of the coordinate system in which the metric expression is given.
from sympy import pprint, exp
from sympy.diffgeom.rn import R2
from sympy.diffgeom import metric_to_Riemann_components, TensorProduct
TP = TensorProduct
metric_to_Riemann_components(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy))