def test_diffgeom(): from sympy.diffgeom import Manifold, Patch, CoordSystem, BaseScalarField x, y = symbols('x y', real=True) m = Manifold('M', 2) assert str(m) == "M" p = Patch('P', m) assert str(p) == "P" rect = CoordSystem('rect', p, [x, y]) assert str(rect) == "rect" b = BaseScalarField(rect, 0) assert str(b) == "x"
def test_diffgeom(): from sympy.diffgeom import Manifold, Patch, CoordSystem, BaseScalarField m = Manifold('M', 2) p = Patch('P', m) rect = CoordSystem('rect', p) assert srepr( rect ) == "CoordSystem(Str('rect'), Patch(Str('P'), Manifold(Str('M'), Integer(2))), ('rect_0', 'rect_1'))" b = BaseScalarField(rect, 0) assert srepr( b ) == "BaseScalarField(CoordSystem(Str('rect'), Patch(Str('P'), Manifold(Str('M'), Integer(2))), ('rect_0', 'rect_1')), Integer(0))"
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 define_coord_sys(): global rect global polar """定义Manifold, Patch, CoordSystem""" r, theta = symbols('r, theta') x, y = symbols('x, y') m = Manifold('M', 2) # 定义一个patch patch = Patch('P', m) # 为这个patch建立坐标系,一个是笛卡尔,一个是极坐标 rect = CoordSystem('rect', patch) polar = CoordSystem('polar', patch) polar.connect_to(rect, [r, theta], [r * cos(theta), r * sin(theta)])
def test_deprecations(): m = Manifold('M', 2) p = Patch('P', m) with warns_deprecated_sympy(): CoordSystem('Car2d', p, names=['x', 'y']) with warns_deprecated_sympy(): c = CoordSystem('Car2d', p, ['x', 'y']) with warns_deprecated_sympy(): list(m.patches) with warns_deprecated_sympy(): list(c.transforms)
def mobius_strip(): import find_metric as fm import tensor as t g, diff = fm.mobius_strip() R = t.Riemann(g, dim=2, sys_title='mobius_strip', flat_diff=diff) #metric=R.metric from sympy.diffgeom import TensorProduct, Manifold, Patch, CoordSystem manifold = Manifold("M", 2) patch = Patch("P", manifold) system = CoordSystem('mobius_strip', patch, ["u", "v"]) u, v = system.coord_functions() du, dv = system.base_oneforms() from sympy import cos metric = (cos(u / 2)**2 * v**2 / 4 + cos(u / 2) * v + v**2 / 16 + 1) * TensorProduct(du, du) + 0.25 * TensorProduct(dv, dv) C = Christoffel_2nd(metric=metric) return C
def __new__(cls, *args): obj = super(Manifold, cls).__new__(cls) if len(args) >= 1: dependent = args[0] if len(args) >= 2: name = args[1] else: name = 'manifold' obj.name = name obj.dimension = len(dependent) obj._manifold = sympyManifold(obj.name, obj.dimension) obj._patch = Patch('Atlas', obj._manifold) obj._coordsystem = CoordSystem('Coordinates', obj._patch, names=[str(d) for d in dependent]) return obj
def toroidal_coords(manifold: Manifold = None, dim: int = 4): """Create a Toroidal Coordinate system (t ~ R and (x,y,z) ~ S^3) with maximal dimension 4 (can create subspaces too) Args: manifold: Manifold, default None. If specified use this Manifold to take a coordinate patch dim: int, the dimension of the resulting coordinate system. Must be less than or equal to 4, and must match the dimension of the given Manifold if specified. Returns: CoordSystem, the coordinate system """ if manifold is None: manifold = Manifold('M', dim) origin_patch = Patch('o', manifold) return CoordSystem('toroidal', origin_patch, SPHERICAL_SYMBOLS[:dim])
def flat_kerr(a=0, G=1, M=0.5): import find_metric as fm from sympy.diffgeom import CoordSystem, Manifold, Patch, TensorProduct manifold = Manifold("M", 3) patch = Patch("P", manifold) kerr = CoordSystem("kerr", patch, ["u", "v", "w"]) u, v, w = kerr.coord_functions() du, dv, dw = kerr.base_oneforms() g11 = (a**2 * sym.cos(v) + u**2) / (-2 * G * M * u + a**2 + u**2) g22 = a**2 * sym.cos(v) + u**2 g33 = -(1 - 2 * G * M * u / (u**2 + a**2 * sym.cos(v))) # time independent : unphysical ? #g33 = 2*G*M*a**2*sym.sin(v)**4*u/(a**2*sym.cos(v) + u**2)**2 + a**2*sym.sin(v)**2 + sym.sin(v)**2*u**2 metric = g11 * TensorProduct(du, du) + g22 * TensorProduct( dv, dv) + g33 * TensorProduct(dw, dw) C = Christoffel_2nd(metric=metric) return C
def _set_coordinates(self, sys_title): from sympy.diffgeom import CoordSystem, Manifold, Patch manifold = Manifold("M", self.dim) patch = Patch("P", manifold) if self.dim == 4: system = CoordSystem(sys_title, patch, ["u", "v", "w", "t"]) u, v, w, t = system.coord_functions() self.w = w self.t = t if self.dim == 3: system = CoordSystem(sys_title, patch, ["u", "v", "w"]) u, v, w = system.coord_functions() self.w = w if self.dim == 2: system = CoordSystem(sys_title, patch, ["u", "v"]) u, v = system.coord_functions() self.u, self.v = u, v self.system = system
def _dummy_cs(self): """Create a dummy CoordSystem""" manifold = Manifold('M', 3) origin_patch = Patch('o', manifold) return coords.CoordSystem('euclidean', origin_patch, ['x', 'y', 'z'])
from sympy.diffgeom import Manifold, Patch m = Manifold('M', 3) p = Patch('P', m) p in m.patches
# https://krastanov.wordpress.com/2012/06/30/part-1-what-is-a-tensor-and-how-is-it-implemented-in-the-diffgeom-sympy-module/ import numpy import scipy from sympy.diffgeom import Manifold, Patch, CoordSystem m = Manifold('my_manifold', 2) # A 2D manifold called 'my_manifold' p = Patch('my_patch', m) # A patch called 'my_patch' cs_r = CoordSystem('R', p) # A coordinate system called 'R' (for rectangular) point = cs_r.point([1, 1]) # A point with coordinates (1, 1) print(point)
# https://krastanov.wordpress.com/2012/06/30/part-1-what-is-a-tensor-and-how-is-it-implemented-in-the-diffgeom-sympy-module/ import numpy import scipy from sympy.diffgeom import CoordSystem, Manifold, Patch m = Manifold("my_manifold", 2) # A 2D manifold called 'my_manifold' p = Patch("my_patch", m) # A patch called 'my_patch' cs_r = CoordSystem("R", p) # A coordinate system called 'R' (for rectangular) point = cs_r.point([1, 1]) # A point with coordinates (1, 1) print(point)
from sympy.physics.vector import gradient from sympy.physics.vector import ReferenceFrame from sympy import * from sympy.tensor import IndexedBase, Idx from sympy import symbols from sympy import Matrix, I from sympy import symbols, sin, cos, pi from sympy.diffgeom import Manifold, Patch, CoordSystem if __name__ == '__main__': #f = Function('f')(x, y) #g1 = Function('g')(x, y) a1, a2, a3, ao1, ao2, theta1, theta2, theta3, theta4 = symbols( 'a1,a2,a3,ao1,ao2,theta1,theta2,theta3,theta4') m = Manifold('M', 4) patch = Patch('P', m) rect = CoordSystem('rect', patch) polar = CoordSystem('polar', patch) rect in patch.coord_systems polar.connect_to(rect, [theta1, theta2, theta3, theta4], [ a1 * theta1 + a2 * theta2 + a3 * theta3 + (theta1 * ao1 + theta2 * ao2) * theta4 ]) print(polar.jacobian(rect, [theta1, theta2, theta3, theta4])) g = polar.jacobian(rect, [theta1, theta2, theta3, theta4]) patch2 = Patch('P', m) rect2 = CoordSystem('rect2', patch2) polar2 = CoordSystem('polar2', patch2) rect2 in patch2.coord_systems polar2.connect_to(rect2, [theta1, theta2, theta3, theta4], g)
# http://docs.sympy.org/latest/modules/diffgeom.html from sympy import cos, pi, sin, symbols from sympy.diffgeom import CoordSystem, Manifold, Patch from sympy.simplify import simplify r, theta = symbols("r, theta") m = Manifold("M", 2) patch = Patch("P", m) rect = CoordSystem("rect", patch) polar = CoordSystem("polar", patch) print(rect in patch.coord_systems) polar.connect_to(rect, [r, theta], [r * cos(theta), r * sin(theta)]) print(polar.coord_tuple_transform_to(rect, [0, 2])) print(polar.coord_tuple_transform_to(rect, [2, pi / 2])) print(rect.coord_tuple_transform_to(polar, [1, 1]).applyfunc(simplify)) print(polar.jacobian(rect, [r, theta])) p = polar.point([1, 3 * pi / 4]) print(rect.point_to_coords(p)) print(rect.coord_function(0)(p)) print(rect.coord_function(1)(p)) v_x = rect.base_vector(0) x = rect.coord_function(0)
from sympy.diffgeom import Manifold, Patch, CoordSystem, Point from sympy import symbols, Function m = Manifold("m", 2) p = Patch("p", m) cs = CoordSystem("cs", p, ["a", "b"]) cs_noname = CoordSystem("cs", p) x, y = symbols("x y") f = Function("f") s1, s2 = cs.coord_functions() v1, v2 = cs.base_vectors() f1, f2 = cs.base_oneforms() def test_point(): point = Point(cs, [x, y]) assert point == point.func(*point.args) assert point != Point(cs, [2, y]) # TODO assert point.subs(x, 2) == Point(cs, [2, y]) # TODO assert point.free_symbols == set([x, y]) def test_rebuild(): assert m == m.func(*m.args) assert p == p.func(*p.args) assert cs == cs.func(*cs.args) assert cs_noname == cs_noname.func(*cs_noname.args) assert s1 == s1.func(*s1.args) assert v1 == v1.func(*v1.args) assert f1 == f1.func(*f1.args)
from sympy.diffgeom import Manifold, Patch, CoordSystem, Point from sympy import symbols, Function m = Manifold('m', 2) p = Patch('p', m) cs = CoordSystem('cs', p, ['a', 'b']) cs_noname = CoordSystem('cs', p) x, y = symbols('x y') f = Function('f') s1, s2 = cs.coord_functions() v1, v2 = cs.base_vectors() f1, f2 = cs.base_oneforms() def test_point(): point = Point(cs, [x, y]) assert point == point.func(*point.args) assert point != Point(cs, [2, y]) #TODO assert point.subs(x, 2) == Point(cs, [2, y]) #TODO assert point.free_symbols == set([x, y]) def test_rebuild(): assert m == m.func(*m.args) assert p == p.func(*p.args) assert cs == cs.func(*cs.args) assert cs_noname == cs_noname.func(*cs_noname.args) assert s1 == s1.func(*s1.args) assert v1 == v1.func(*v1.args) assert f1 == f1.func(*f1.args)
# http://docs.sympy.org/latest/modules/diffgeom.html from sympy import symbols, sin, cos, pi from sympy.diffgeom import Manifold, Patch, CoordSystem from sympy.simplify import simplify r, theta = symbols('r, theta') m = Manifold('M', 2) patch = Patch('P', m) rect = CoordSystem('rect', patch) polar = CoordSystem('polar', patch) print(rect in patch.coord_systems) polar.connect_to(rect, [r, theta], [r*cos(theta), r*sin(theta)]) print(polar.coord_tuple_transform_to(rect, [0, 2])) print(polar.coord_tuple_transform_to(rect, [2, pi/2])) print(rect.coord_tuple_transform_to(polar, [1, 1]).applyfunc(simplify)) print(polar.jacobian(rect, [r, theta])) p = polar.point([1, 3*pi/4]) print(rect.point_to_coords(p)) print(rect.coord_function(0)(p)) print(rect.coord_function(1)(p)) v_x = rect.base_vector(0) x = rect.coord_function(0)
from sympy.diffgeom import Manifold, Patch, CoordSystem from sympy.diffgeom import TensorProduct as TP from sympy.diffgeom import metric_to_Riemann_components as Riemann from sympy.diffgeom import metric_to_Christoffel_2nd as Christoffel from sympy import Symbol, Function, latex, sin n = 2 M = Manifold('M', n) P = Patch('P', M) coord = CoordSystem('coord', P, ['x%s'%i for i in range(n)]) x = coord.coord_functions() dx = coord.base_oneforms() g = [[1, 0], [0, sin(x[0])**2]] metric = sum([g[i][j]*TP(dx[i], dx[j]) for i in range(n) for j in range(n)]) C = Christoffel(metric) R = Riemann(metric)
# Basic imports and functions from sympy import latex, symbols, sin, cos, pi, simplify from scipy.integrate import solve_ivp from sympy.diffgeom import (Manifold, Patch, CoordSystem, metric_to_Christoffel_2nd, TensorProduct as TP) # def lprint(v): # display(Math(latex(v))) # Create a manifold. M = Manifold('M', 4) # Create a patch. patch = Patch('P', M) # Basic symbols c, r_s = symbols('c r_s') # Coordinate system schwarzchild_coord = CoordSystem('schwarzchild', patch, ['t', 'r', 'theta', 'phi']) # Get the coordinate functions t, r, theta, phi = schwarzchild_coord.coord_functions() # Get the base one forms. dt, dr, dtheta, dphi = schwarzchild_coord.base_oneforms() # Auxiliar terms for the metric.
from sympy import symbols, sin, cos, pi from sympy.diffgeom import Manifold, Patch, CoordSystem r, theta = symbols('r, theta') m = Manifold('M', 2) patch = Patch('P', m) rect = CoordSystem('rect', patch) polar = CoordSystem('polar', patch) rect in patch.coord_systems polar.connect_to(rect, [r, theta], [r*cos(theta), r*sin(theta)]) polar.coord_tuple_transform_to(rect, [0, 2]) polar.coord_tuple_transform_to(rect, [2, pi/2]) rect.coord_tuple_transform_to(polar, [1, 1]) polar.jacobian(rect, [r, theta]) p = polar.point([1, 3*pi/4]) rect.point_to_coords(p) #Define a basis scalar field (i.e. a coordinate function), that takes a point and returns its coordinates. It is an instance of BaseScalarField. rect.coord_function(0)(p) rect.coord_function(1)(p) #Define a basis vector field (i.e. a unit vector field along the coordinate line). Vectors are also differential operators on scalar fields. It is an instance of BaseVectorField. v_x = rect.base_vector(0) x = rect.coord_function(0) v_x(x) v_x(v_x(x)) #Define a basis oneform field: dx = rect.base_oneform(0) dx(v_x) #If you provide a list of names the fields will print nicely: - without provided names: x, v_x, dx #with provided names rect = CoordSystem('rect', patch, ['x', 'y']) rect.coord_function(0), rect.base_vector(0), rect.base_oneform(0)