def test_curvature(): #For testing curvature conventions M = Manifold('Reisner-Nordstrom', 4) p = Patch('origin', M) cs = CoordSystem('spherical', p, ['t', 'r', 'theta', 'phi']) t, r, theta, phi = cs.coord_functions() dt, dr, dtheta, dphi = cs.base_oneforms() f = Function('f') metric = f(r)**2 * TP(dt, dt) - f(r)**(-2) * TP(dr, dr) - r**2 * TP( dtheta, dtheta) - r**2 * sin(theta)**2 * TP(dphi, dphi) ch_2nd = metric_to_Christoffel_2nd(metric) G = ToArray(metric) rm = TensorArray(components=metric_to_Riemann_components(metric), variance=[-1, 1, 1, 1], coordinate_system=cs) v = [Function(f) for f in ['v0', 'v1', 'v2', 'v3']] V = TensorArray(components=[f(t, r, theta, phi) for f in v], variance=[-1], coordinate_system=cs) dV = V.covD(ch_2nd) ddV = dV.covD(ch_2nd) #Commuted covariant derivative: D2V = ddV - ddV.braid(0, 1) rm = TensorArray(components=metric_to_Riemann_components(metric), variance=[-1, 1, 1, 1], coordinate_system=cs) rmv = rm.TensorProduct(V).contract(1, 4).braid(0, 1).braid(1, 2) rmvtensor = [(I, simplify(rmv.tensor[I])) for I in rmv.indices] rmvtensor = [(I, coeff) for (I, coeff) in rmvtensor if coeff != 0] rmvtensor.sort() D2Vtensor = [(I, simplify(D2V.tensor[I])) for I in D2V.indices] D2Vtensor = [(I, v) for (I, v) in D2Vtensor if v != 0] D2Vtensor.sort() for (a, b) in zip(rmvtensor, D2Vtensor): assert (a == b)
def test_covar_deriv(): ch = metric_to_Christoffel_2nd(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy)) cvd = BaseCovarDerivativeOp(R2_r, 0, ch) assert cvd(R2.x) == 1 assert cvd(R2.x * R2.e_x) == R2.e_x cvd = CovarDerivativeOp(R2.x * R2.e_x, ch) assert cvd(R2.x) == R2.x assert cvd(R2.x * R2.e_x) == R2.x * R2.e_x
def test_covar_deriv(): ch = metric_to_Christoffel_2nd(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy)) cvd = BaseCovarDerivativeOp(R2_r, 0, ch) assert cvd(R2.x) == 1 assert cvd(R2.x*R2.e_x) == R2.e_x cvd = CovarDerivativeOp(R2.x*R2.e_x, ch) assert cvd(R2.x) == R2.x assert cvd(R2.x*R2.e_x) == R2.x*R2.e_x
def test_covar_deriv(): ch = metric_to_Christoffel_2nd(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy)) cvd = BaseCovarDerivativeOp(R2_r, 0, ch) assert cvd(R2.x) == 1 # This line fails if the cache is disabled: assert cvd(R2.x * R2.e_x) == R2.e_x cvd = CovarDerivativeOp(R2.x * R2.e_x, ch) assert cvd(R2.x) == R2.x assert cvd(R2.x * R2.e_x) == R2.x * R2.e_x
def Christoffel_2nd(g=None,metric=None): # either g is supplied as arugment or the two-form from sympy.abc import u,v from sympy.diffgeom import metric_to_Christoffel_2nd from sympy import asin, acos, atan, cos, log, ln, exp, cosh, sin, sinh, sqrt, tan, tanh if metric is None: # if metric is not specified as two_form import tensor as t R = t.Riemann(g,g.shape[0]) metric = R.metric C = sym.Matrix(eval(str(metric_to_Christoffel_2nd(metric)))) return C
def test_covD(): M = Manifold('Reisner-Nordstrom', 4) p = Patch('origin', M) cs = CoordSystem('spherical', p, ['t', 'r', 'theta', 'phi']) t, r, theta, phi = cs.coord_functions() dt, dr, dtheta, dphi = cs.base_oneforms() f = Function('f') metric = f(r)**2 * TP(dt, dt) - f(r)**(-2) * TP(dr, dr) - r**2 * TP( dtheta, dtheta) - r**2 * sin(theta)**2 * TP(dphi, dphi) ch_2nd = metric_to_Christoffel_2nd(metric) G = ToArray(metric) assert (G.covD(ch_2nd).to_tensor() == 0)
def Christoffel_2nd( g=None, metric=None): # either g is supplied as arugment or the two-form from sympy.abc import u, v from sympy.diffgeom import metric_to_Christoffel_2nd from sympy import asin, acos, atan, cos, log, ln, exp, cosh, sin, sinh, sqrt, tan, tanh if metric is None: # if metric is not specified as two_form import tensor as t R = t.Riemann(g, g.shape[0]) metric = R.metric C = sym.Matrix(eval(str(metric_to_Christoffel_2nd(metric)))) return C
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))
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))
# Auxiliar terms for the metric. dt_2 = TP(dt, dt) dr_2 = TP(dr, dr) dtheta_2 = TP(dtheta, dtheta) dphi_2 = TP(dphi, dphi) factor = (1 - r_s / r) # Build the metric metric = factor * c**2 * dt_2 - 1 / factor * dr_2 - r**2 * ( dtheta_2 + sin(theta)**2 * dphi_2) metric = factor * c**2 * dt_2 - 1 / factor * dr_2 - r**2 * ( dtheta_2 + sin(theta)**2 * dphi_2) metric = metric / c**2 # Get the Christoffel symbols of the second kind. christoffel = metric_to_Christoffel_2nd(metric) # Let's print this in an elegant way ;) # for i in range(4): # for j in range(4): # for k in range(4): # if christoffel[i, j, k] != 0: # display(Math('\Gamma^{}_{{{},{}}} = '.format(i, j, k) + latex(christoffel[i, j, k]))) ## Specify c and r_s christoffel_ = christoffel.subs({c: 2, r_s: 1}) def F(t, y): u = y[0:4] v = y[4:8]
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 metric_to_Christoffel_2nd(self): from sympy.diffgeom import metric_to_Christoffel_2nd return metric_to_Christoffel_2nd(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
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.rn import R2 LieDerivative(R2.e_x, R2.y) LieDerivative(R2.e_x, R2.x) LieDerivative(R2.e_x, R2.e_x) # The Lie derivative of a tensor field by another tensor field is equal to their commutator: LieDerivative(R2.e_x, R2.e_r) LieDerivative(R2.e_x + R2.e_y, R2.x) tp = TensorProduct(R2.dx, R2.dy) LieDerivative(R2.e_x, tp) LieDerivative(R2.e_x, tp).doit() #BaseCovarDerivativeOp from sympy.diffgeom.rn import R2, R2_r from sympy.diffgeom import BaseCovarDerivativeOp from sympy.diffgeom import metric_to_Christoffel_2nd, TensorProduct TP = TensorProduct ch = metric_to_Christoffel_2nd(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy)) ch cvd = BaseCovarDerivativeOp(R2_r, 0, ch) cvd(R2.x) cvd(R2.x*R2.e_x) #CovarDerivativeOp from sympy.diffgeom.rn import R2 from sympy.diffgeom import CovarDerivativeOp from sympy.diffgeom import metric_to_Christoffel_2nd, TensorProduct TP = TensorProduct ch = metric_to_Christoffel_2nd(TP(R2.dx, R2.dx) + TP(R2.dy, R2.dy)) ch cvd = CovarDerivativeOp(R2.x*R2.e_x, ch) cvd(R2.x) cvd(R2.x*R2.e_x) #intcurve_series