Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
    def _set_flat_coordinates(self, sys_title, flat_diff):
        from sympy.diffgeom import CoordSystem, Manifold, Patch
        manifold = Manifold("M", self.dim)
        patch = Patch("P", manifold)
        flat_diff = sympy.Matrix(flat_diff)
        N = flat_diff.shape[0]
        coords = []
        for i in range(0, N):
            n = str(flat_diff[i, i]).find('*')
            coord_i = str(flat_diff[i, i])[1:n]
            coords.append(coord_i)
        if self.dim == 4:
            system = CoordSystem(sys_title, patch, [str(coords[0]),str(coords[1]),\
                                                     str(coords[2]),str(coords[3])])
            u, v, w, t = system.coord_functions()
            self.w = w
            self.t = t

        if self.dim == 3:
            system = CoordSystem(sys_title, patch, [str(coords[0]),str(coords[1]),\
                                                    str(coords[2])])
            u, v, w = system.coord_functions()
            self.w = w

        if self.dim == 2:
            system = CoordSystem(
                sys_title, patch,
                [str(coords[0]), str(coords[1])])
            u, v = system.coord_functions()

        self.u, self.v = u, v
        self.system = system
Ejemplo n.º 3
0
    def _set_user_coordinates(self, sys_title, user_coord):
        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, [str(user_coord[0]),str(user_coord[1]),\
                                                     str(user_coord[2]),str(user_coord[3])])
            u, v, w, t = system.coord_functions()
            self.w = w
            self.t = t

        if self.dim == 3:
            system = CoordSystem(
                sys_title, patch,
                [str(user_coord[0]),
                 str(user_coord[1]),
                 str(user_coord[2])])
            u, v, w = system.coord_functions()
            self.w = w

        if self.dim == 2:
            system = CoordSystem(
                sys_title, patch,
                [str(user_coord[0]), str(user_coord[1])])
            u, v = system.coord_functions()

        self.u, self.v = u, v
        self.system = system
Ejemplo n.º 4
0
 def test_from_sympy_coordsystem(self):
     """Test creating from sympy coord system"""
     manifold = Manifold('M', 4)
     origin_patch = Patch('o', manifold)
     sym_cs = _CoordSystem('cartesian', origin_patch, ['x', 'y', 'z'])
     cs = coords.CoordSystem.from_sympy_coordsystem(sym_cs)
     assert isinstance(cs, coords.CoordSystem)
Ejemplo n.º 5
0
 def test__coord_system_symbols(self):
     """Test coord symbol extraction"""
     manifold = Manifold('M', 4)
     origin_patch = Patch('o', manifold)
     x, y, z = symbols('x y z')
     sym_cs = _CoordSystem('cartesian', origin_patch, [x, y, z])
     sym_cs_symbols = coords._coord_system_symbols(sym_cs)
     assert sym_cs_symbols == symbols('x y z')
Ejemplo n.º 6
0
def test_scalar():
    M = Manifold('Reisner-Nordstrom', 4)
    p = Patch('origin', M)
    cs = CoordSystem('spherical', p, ['t', 'r', 'theta', 'phi'])
    t, r, theta, phi = cs.coord_functions()
    f = Function('f')
    assert (ToArray(1, coordinate_system=cs).to_tensor() == 1)
    assert (ToArray(f(r)).to_tensor() == f(r))
Ejemplo n.º 7
0
def test_expand_tensor():
    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)
    assert (expand_tensor(metric) == metric)
Ejemplo n.º 8
0
def test_diffgeom():
    from sympy.diffgeom import Manifold, Patch, CoordSystem, BaseScalarField
    m = Manifold('M', 2)
    assert str(m) == "M"
    p = Patch('P', m)
    assert str(p) == "P"
    rect = CoordSystem('rect', p)
    assert str(rect) == "rect"
    b = BaseScalarField(rect, 0)
    assert str(b) == "rect_0"
Ejemplo n.º 9
0
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"
Ejemplo n.º 10
0
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))"
Ejemplo n.º 11
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)
Ejemplo n.º 12
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)])
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
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])
Ejemplo n.º 16
0
    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
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
    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
Ejemplo n.º 19
0
# 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)
Ejemplo n.º 20
0
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)
Ejemplo n.º 21
0
# 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)
Ejemplo n.º 22
0
from sympy.diffgeom import Manifold, Patch
m = Manifold('M', 3)
p = Patch('P', m)
p in m.patches
Ejemplo n.º 23
0
# 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)
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
# 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)
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
 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'])
# 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.
Ejemplo n.º 29
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)