コード例 #1
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))
コード例 #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))
コード例 #3
0
ファイル: tensor.py プロジェクト: imranal/master_thesis
    def find_Ricci_tensor(self, form="simplified"):
        """
        Method determines the Ricci curvature tensor for
        a given metric(which must be in two-form).
        
        form : default value - "simplified"
        If desired, a simplified form is returned. 

        The returned value is a SymPy Matrix.
        """
        from sympy.diffgeom import metric_to_Ricci_components
        metric = self.metric
        RR = metric_to_Ricci_components(metric)
        simpRR = self._tuple_to_list(RR)
        dim = self.dim
        if form == "simplified":
            print 'Performing simplifications on each component....'
            for m in range(dim):
                for i in range(dim):
                    expr = str(RR[m][i])
                    expr = self._symplify_expr(expr)
                    simpRR[m][i] = expr
        self.Ricci = sympy.Matrix(simpRR)
        return self.Ricci
コード例 #4
0
ファイル: tensor.py プロジェクト: imranal/DiffTens
    def find_Ricci_tensor(self,form="simplified"):
        """
        Method determines the Ricci curvature tensor for
        a given metric(which must be in two-form).
        
        form : default value - "simplified"
        If desired, a simplified form is returned. 

        The returned value is a SymPy Matrix.
        """
        from sympy.diffgeom import metric_to_Ricci_components
        metric = self.metric
        RR = metric_to_Ricci_components(metric)
        simpRR = self._tuple_to_list(RR)
        dim = self.dim
        if form=="simplified":
            print 'Performing simplifications on each component....'
            for m in range(dim):
                for i in range(dim):
                    expr = str(RR[m][i])
                    expr = self._symplify_expr(expr)
                    simpRR[m][i] = expr
        self.Ricci = sympy.Matrix(simpRR)      
        return self.Ricci
コード例 #5
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
コード例 #6
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)

    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
コード例 #7
0
metric_diff_form = r**2 * TensorProduct(
    dtheta, dtheta) + r**2 * sin(theta)**2 * TensorProduct(dphi, dphi)
R = metric_to_Riemann_components(metric_diff_form)


def tuple_to_list(t):
    return list(map(tuple_to_list, t)) if isinstance(t, (list, tuple)) else t


simpR = tuple_to_list(R)
for m in range(dim):
    for i in range(dim):
        for j in range(dim):
            for k in range(dim):
                expr = str(R[m][i][j][k])
                expr = trigsimp(expr)
                expr = simplify(expr)
                simpR[m][i][j][k] = expr

# Find the Ricci tensor
from sympy.diffgeom import metric_to_Ricci_components
RR = metric_to_Ricci_components(metric_diff_form)
simpRR = tuple_to_list(RR)
for m in range(dim):
    for i in range(dim):
        expr = str(RR[m][i])
        expr = trigsimp(expr)
        expr = simplify(expr)
        simpRR[m][i] = expr
print simpR, simpRR
コード例 #8
0
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))

non_trivial_metric = exp(2*R2.r)*TP(R2.dr, R2.dr) +         R2.r**2*TP(R2.dtheta, R2.dtheta)
non_trivial_metric
riemann = metric_to_Riemann_components(non_trivial_metric)
riemann[0]
riemann[1]
#metric_to_Ricci_components Return the components of the Ricci tensor expressed in a given basis.  Given a metric it calculates the components of the Ricci 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_Ricci_components, TensorProduct
TP = TensorProduct
metric_to_Ricci_components(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy))
non_trivial_metric = exp(2*R2.r)*TP(R2.dr, R2.dr) +                              R2.r**2*TP(R2.dtheta, R2.dtheta)
non_trivial_metric
metric_to_Ricci_components(non_trivial_metric) #TODO why is this not simpler