Example #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))
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_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
Example #4
0
 def metric_to_Christoffel_1st(self):
     from sympy.diffgeom import metric_to_Christoffel_1st
     return metric_to_Christoffel_1st(self.metric)
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
Example #6
0
 def metric_to_Christoffel_1st(self):
     from sympy.diffgeom import metric_to_Christoffel_1st
     return metric_to_Christoffel_1st(self.metric)
Example #7
0
def diffgeometryEx2(request):
    typesym = request.GET.get('typesym')
    metric_input = request.GET.get('metric')

    if not check_args(typesym, metric_input):
        return {'is_valid': False}

    def get_unique(lst):
        lst = set(lst)
        lst = list(lst)
        lst.sort()
        return lst

    def replace_in_metric(m, lst1, lst2):
        for i in range(len(lst1)):
            m = m.replace(lst1[i], lst2[i])
        return m

    TP = TensorProduct
    R = [R2.x, R2.y]
    dR2 = [TP(R2.dx, R2.dx), TP(R2.dy, R2.dy)]
    dR = (TP(R2.dx, R2.dy) + TP(R2.dy, R2.dx)) / 2

    metric = re.sub(r'\^', '**', str(metric_input))
    metric = re.sub(r'd\*s\*\*2=', '', metric)

    # get vars from string-metric
    variables = re.findall(r'd\*(\w+)', metric)
    variables = get_unique(variables)
    # get differentials from string-metric and replace 'd*x' to 'dx'
    zamena_diff = re.findall(r'd\*\w+', metric)
    zamena_diff = get_unique(zamena_diff)

    zamena_diff_new = [el.replace('*', '') for el in zamena_diff]

    metric = replace_in_metric(metric, zamena_diff, zamena_diff_new)
    # get differentials at the 2 power
    diffs2 = re.findall(r'd\w+\*\*2', metric)
    diffs2 = get_unique(diffs2)
    # get differentials 'dx*dy' and replace to 'dxdy'
    diffs_diff = re.findall(r'd\w+\*d\w+', metric)
    diffs_diff = get_unique(diffs_diff)

    diffs_diff_new = [el.replace('*', '') for el in diffs_diff]

    metric = replace_in_metric(metric, diffs_diff, diffs_diff_new)
    # get sympy-eval from string-metric
    transformations = standard_transformations + (function_exponentiation, implicit_application,)
    try:
        metric_sym = parse_expr(metric, transformations=transformations)
    except:
        return {'is_valid': False}

    metric_input = latex(metric_sym)
    # replace variables to 'R2.x' and "R2.y'
    metric_sym = replace_in_metric(metric_sym, variables, R)
    # replace differentials at the 2 power to 'TP(dx, dx)' and 'TP(dy, dy)'
    metric_sym = replace_in_metric(metric_sym, diffs2, dR2)
    # replace 'dxdy' to 'TP(dx, dy)'
    for el in diffs_diff_new:
        metric_sym = metric_sym.replace(el, dR)
    metric_sym = expand(metric_sym)
    #generate metric-matrix
    metric_matrix = [latex(metric_sym.coeff(TP(a, b))) for a in [R2.dx, R2.dy] for b in [R2.dx, R2.dy]]

    if typesym == '1':
        try:
            Christoffel = simplify(metric_to_Christoffel_1st(metric_sym))
        except ValueError:
            return {'is_valid': False}

    elif typesym == '2':
        try:
            Christoffel = simplify(metric_to_Christoffel_2nd(metric_sym))
        except ValueError:
            return {'is_valid': False}

    else:
        return {'is_valid': False}

    Christoffel = list(Christoffel)
    # replace 'R2.x,y' to variables
    index = list(map(''.join, product('12', repeat=3)))
    answer = [('_{%s,%s%s}=' if typesym == '1' else '^%s_{ \ %s%s}=') % (index[i][0], index[i][1], index[i][2]) +
              latex(el.subs([(R[i], variables[i]) for i in range(len(variables))], simultaneous=True))
              for i, el in enumerate(Christoffel)]

    return {'answer': answer, 'metric': metric_input, 'typesym': typesym, 'metric_matrix': metric_matrix, 'is_valid': True}
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))

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