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_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))
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)
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
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
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_partial_derivative_of_VI_wrt_a_coordinate(self): # note that we not only need the partial derivative of the components # but also the partial derivatives of the base vectors expressed as linear combinations # of those base vectors which are expressed by the cristoffel symbols which in turn can be computed from # known cristoffel symbols of a given coordinate system and the connection # between the coordinates # # A case of special interest is the cartesian coordinate system whose # coordinate functions are given as the partial derivatives of functions # on the manifold in the directions of the set of base vectors e_x,e_y,ez # We therefore start with the cartesian base or equivalently # with the cartesian coordinate system defined by this vectorfield #get the catesian cellar base vectors dim = 3 manyf = Manifold('M', dim) patch = PatchWithMetric('P', manyf) cart = CoordSystem('cart', patch) cc = VectorFieldBase("cc", cart) g = TensorIndexSet("g", [cc, cc]) for i in range(dim): g[i, i] = 1 i = Idx("i") j = Idx("j") patch.setMetric(cart, g) spherical = CoordSystem('spherical', patch) # now connect the two coordsystems by a transformation x, y, z = symbols("x,y,z") r = symbols("r", positive=True, real=True) phi, theta = symbols("phi,theta", real=True) spherical.connect_to(cart, [r, phi, theta], [ r * cos(phi) * sin(theta), r * sin(phi) * sin(theta), r * cos(theta) ], inverse=False) # cellar base bc = VectorFieldBase("bc", spherical) # roof base br = OneFormFieldBase(bc) x = TensorIndexSet("x", [bc]) r = Symbol("r") x[0] = r**2 i = Idx("i") print(type(x[i])) #res=Derivative(x[i],r,evaluate=True) raise (Exception( "The following line hangs we have to check the creation of the ingredients in new tests: metrics in the new coordinate system simple derivative without coord transformation" )) res = diff(x[i], r) print((res))
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 test_getWithContraction(self): n = 3 m = Manifold('M', n) patch = PatchWithMetric('P', m) cart = CoordSystem('cart', patch) bc = VectorFieldBase("bc", cart) br = OneFormFieldBase(bc) #full trace x = TensorIndexSet("x", [bc, br]) x[0, 0] = 3 x[1, 1] = 4 i = Idx('i') self.assertEqual(x[i, i], 7) # partial trace x = TensorIndexSet("x", [bc, br, bc, br]) y = TensorIndexSet("y") x[0, 0, 0, 0] = 3 x[1, 1, 0, 0] = 4 i, j, k = map(Idx, ['i', 'j', 'k']) print("before test") y[j, k] = x[i, i, j, k] self.assertEqual(y.bases, [bc, br]) self.assertEqual(y[0, 0], 7) self.assertEqual(x[i, i, j, j], 7) x = TensorIndexSet("x", [bc, br, bc, br, bc]) y = TensorIndexSet("y") x[0, 0, 0, 0, 0] = 3 x[1, 1, 0, 0, 0] = 4 i, j, k, l = map(Idx, ['i', 'j', 'k', 'l']) z = x[i, i, j, j, 0] self.assertEqual(z, 7)
def test_free_symbols(self): n = 3 m = Manifold('M', n) patch = PatchWithMetric('P', m) cart = CoordSystem('cart', patch) # cellar base bc = VectorFieldBase("bc", cart) # roof base br = OneFormFieldBase(bc) g = TensorIndexSet("g", [bc, bc]) for i in range(n): g[i, i] = 1 patch.setMetric("cart", g) x = TensorIndexSet("x", [bc]) r, phi = symbols("r,phi") x[0] = r**2 x[1] = phi**2 i = Idx("i") res = x[i].free_symbols self.assertEqual(res, set({phi, r})) x = TensorIndexSet("x", [br]) r, phi = symbols("r,phi") x[0] = r**2 i = Idx("i") res = x[i].free_symbols self.assertEqual(res, set({r}))
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 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 test_derivedMetricInNewCoordSystem3d(self): # A metric is a property of a patch so # it is shared by all coordinate systems # on this patch # We define the typical euklidian metric # by the metric tensor w.r.t the cartesian # base vectors # system and then derive the form of # the metric tensors w.r.t to the new base #get the catesian cellar base vectors dim = 3 manyf = Manifold('M', dim) patch = PatchWithMetric('P', manyf) cart = CoordSystem('unitbase', patch) cc = VectorFieldBase("cc", cart) g = TensorIndexSet("g", [cc, cc]) for i in range(dim): g[i, i] = 1 i = Idx("i") j = Idx("j") patch.setMetric(cart, g) longVecs = CoordSystem('longCellarBaseVectors', patch) # now connect the two coordsystems by a transformation x, y, z = symbols("x,y,z") u, v, w = symbols("u v w", real=True) lu, lv, lw = symbols("lu lv lw", real=True) longVecs.connect_to(cart, [u, v, w], [lu * u, lv * v, lw * w], inverse=False) #another coordsystem metric = patch.getMetricRepresentation('longCellarBaseVectors') print(metric.bases) #pprint(metric[0,0]) self.assertEqual(metric[0, 0], lu**(-2)) self.assertEqual(metric[1, 1], lv**(-2)) self.assertEqual(metric[2, 2], lw**(-2)) spherical = CoordSystem('spherical', patch) # now connect the two coordsystems by a transformation r = symbols("r", positive=True, real=True) phi, theta = symbols("phi,theta", real=True) spherical.connect_to(cart, [r, phi, theta], [ r * cos(phi) * sin(theta), r * sin(phi) * sin(theta), r * cos(theta) ], inverse=False) metric = patch.getMetricRepresentation('spherical') print(metric.bases) print(metric.data) raise (Exception("There must be a fixture for this"))
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"
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 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_getSetItems(self): n = 2 m = Manifold('M', n) patch = PatchWithMetric('P', m) cart = CoordSystem('cart', patch) bc = VectorFieldBase("cart_st", cart) br = OneFormFieldBase(bc) i, j, k, l = map(Idx, ['i', 'j', 'k', 'l']) x = TensorIndexSet("x", [br]) y = TensorIndexSet("y") x[0] = 10 x[1] = 20 y[i] = x[i] self.assertEqual(y[0], 10) self.assertEqual(y[1], 20) # actually the names of the free indices should not be important # as long as the indices have the same range y[j] = x[i] self.assertEqual(y[0], 10) self.assertEqual(y[1], 20) # two free indices on both sides x2 = TensorIndexSet("x2", [br, br]) y2 = TensorIndexSet("y2") x2[0, 0] = 10 x2[1, 1] = 20 y2[k, l] = x2[i, j] self.assertEqual(y2[0, 0], 10) self.assertEqual(y2[1, 1], 20) # now change the range of one index and get an Exception j = Idx("j", (0, 4)) with self.assertRaises(IndexRangeException): y[j] = x[i] # this should also happen when more than one Index is involved with self.assertRaises(IndexRangeException): y2[k, l] = x2[i, j] #1 common index the other one renamed j = Idx("j") x2[0, 0] = 0 x2[0, 1] = 1 x2[1, 0] = 10 x2[1, 1] = 11 y2[i, l] = x2[i, j] self.assertEqual(y2[0, 0], 0) self.assertEqual(y2[0, 1], 1) self.assertEqual(y2[1, 0], 10) self.assertEqual(y2[1, 1], 11) # with changed order y2[l, i] = x2[i, j] self.assertEqual(y2[0, 0], 0) self.assertEqual(y2[0, 1], 10) self.assertEqual(y2[1, 0], 1) self.assertEqual(y2[1, 1], 11)
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
def test_mult(self): n = 2 m = Manifold('M', n) patch = PatchWithMetric('P', m) cart = CoordSystem('cart', patch) bc = VectorFieldBase("cart_st", cart) br = OneFormFieldBase(bc) ## cases with contraction res = TensorIndexSet("res") x = TensorIndexSet("x", [br]) A = TensorIndexSet("A", [bc, br]) i, j, k, l = map(Idx, ['i', 'j', 'k', 'l']) x[0] = 3 A[0, 0] = 2 print("befor mult") res[j] = A[i, j] * x[i] print("after mult") print("res.bases") print(res.bases) self.assertEqual(res[0], 6) self.assertEqual(res.bases, [br]) with self.assertRaises(ContractionIncompatibleBaseException): ## bases are not compatible res[j] = A[i, j] * x[j] x = TensorIndexSet("x", [br]) A = TensorIndexSet("A", [bc, br]) x[0] = 10 x[1] = 20 A[0, 0] = 1 A[1, 0] = 2 A[0, 1] = 3 A[1, 1] = 4 res[j] = A[i, j] * x[i] self.assertEqual(res[0], 50) self.assertEqual(res[1], 110) ## cases without contraction res = TensorIndexSet("res") res[i, j, k] = A[i, j] * x[k] self.assertEqual(res[0, 0, 0], 10) res = TensorIndexSet("res") with self.assertRaises(IncompatibleShapeException): res[j] = A[i, j] * x[k] res = TensorIndexSet("res") x = TensorIndexSet("x", [br]) A = TensorIndexSet("A", [bc, br, bc]) with self.assertRaises(IncompatibleShapeException): res[i, j, l] = A[i, j, k] * x[k]
def test_setWithWrongShape(self): x = TensorIndexSet("x") x[1] = 1 with self.assertRaises(IncompatibleShapeException): x[1, 1] = 1 n = 3 m = Manifold('M', n) patch = PatchWithMetric('P', m) cart = CoordSystem('cart', patch) bc = VectorFieldBase("bc", cart) br = OneFormFieldBase(bc) x = TensorIndexSet("x", [bc, br, bc, br]) with self.assertRaises(IncompatibleShapeException): x[0, 0] = 3
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 __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 setUp(s): # we assume that we know the matrix connecting # the new cellar base vectors to the old cellar base vectors n = 2 m = Manifold('M', n) P = PatchWithMetric('P', m) cart = CoordSystem('cart', P) # oldcellar base s.bc = VectorFieldBase("bc", cart) # old roof base s.br = OneFormFieldBase(s.bc) # new cellar base s.Bc = VectorFieldBase("Bc") # new roof base s.Br = OneFormFieldBase(s.Bc) a, b = symbols("a,b") s.symbols = [a, b] M = Matrix([[a, 0], [0, b]]) #columns contain representation of new # base vectors w.r.t. old s.Bc.connect(s.bc, M)
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 _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
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) def test_subs(): assert s1.subs(s1,s2) == s2
# 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)
def test_inverse_transformations(): p, q, r, s = symbols('p q r s') relations_quarter_rotation = {('first', 'second'): (q, -p)} R2_pq = CoordSystem('first', R2_origin, [p, q], relations_quarter_rotation) R2_rs = CoordSystem('second', R2_origin, [r, s], relations_quarter_rotation) # The transform method should derive the inverse transformation if not given explicitly assert R2_rs.transform(R2_pq) == Matrix([[-R2_rs.symbols[1]], [R2_rs.symbols[0]]]) a, b = symbols('a b', positive=True) relations_uninvertible_transformation = {('first', 'second'): (-a, )} R2_a = CoordSystem('first', R2_origin, [a], relations_uninvertible_transformation) R2_b = CoordSystem('second', R2_origin, [b], relations_uninvertible_transformation) # The transform method should throw if it cannot invert the coordinate transformation. # This transformation is uninvertible because there is no positive a, b satisfying a = -b with raises(NotImplementedError): R2_b.transform(R2_a) c, d = symbols('c d') relations_ambiguous_inverse = {('first', 'second'): (c**2, )} R2_c = CoordSystem('first', R2_origin, [c], relations_ambiguous_inverse) R2_d = CoordSystem('second', R2_origin, [d], relations_ambiguous_inverse) # The transform method should throw if it finds multiple inverses for a coordinate transformation. with raises(ValueError): R2_d.transform(R2_c)
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) print(polar2.jacobian(rect2, [theta1, theta2, theta3, theta4]))
def test_coordsys_transform(): # test inverse transforms p, q, r, s = symbols('p q r s') rel = {('first', 'second'): [(p, q), (q, -p)]} R2_pq = CoordSystem('first', R2_origin, [p, q], rel) R2_rs = CoordSystem('second', R2_origin, [r, s], rel) r, s = R2_rs.symbols assert R2_rs.transform(R2_pq) == Matrix([[-s], [r]]) # inverse transform impossible case a, b = symbols('a b', positive=True) rel = {('first', 'second'): [(a,), (-a,)]} R2_a = CoordSystem('first', R2_origin, [a], rel) R2_b = CoordSystem('second', R2_origin, [b], rel) # This transformation is uninvertible because there is no positive a, b satisfying a = -b with raises(NotImplementedError): R2_b.transform(R2_a) # inverse transform ambiguous case c, d = symbols('c d') rel = {('first', 'second'): [(c,), (c**2,)]} R2_c = CoordSystem('first', R2_origin, [c], rel) R2_d = CoordSystem('second', R2_origin, [d], rel) # The transform method should throw if it finds multiple inverses for a coordinate transformation. with raises(ValueError): R2_d.transform(R2_c) # test indirect transformation a, b, c, d, e, f = symbols('a, b, c, d, e, f') rel = {('C1', 'C2'): [(a, b), (2*a, 3*b)], ('C2', 'C3'): [(c, d), (3*c, 2*d)]} C1 = CoordSystem('C1', R2_origin, (a, b), rel) C2 = CoordSystem('C2', R2_origin, (c, d), rel) C3 = CoordSystem('C3', R2_origin, (e, f), rel) a, b = C1.symbols c, d = C2.symbols e, f = C3.symbols assert C2.transform(C1) == Matrix([c/2, d/3]) assert C1.transform(C3) == Matrix([6*a, 6*b]) assert C3.transform(C1) == Matrix([e/6, f/6]) assert C3.transform(C2) == Matrix([e/3, f/2]) a, b, c, d, e, f = symbols('a, b, c, d, e, f') rel = {('C1', 'C2'): [(a, b), (2*a, 3*b + 1)], ('C3', 'C2'): [(e, f), (-e - 2, 2*f)]} C1 = CoordSystem('C1', R2_origin, (a, b), rel) C2 = CoordSystem('C2', R2_origin, (c, d), rel) C3 = CoordSystem('C3', R2_origin, (e, f), rel) a, b = C1.symbols c, d = C2.symbols e, f = C3.symbols assert C2.transform(C1) == Matrix([c/2, (d - 1)/3]) assert C1.transform(C3) == Matrix([-2*a - 2, (3*b + 1)/2]) assert C3.transform(C1) == Matrix([-e/2 - 1, (2*f - 1)/3]) assert C3.transform(C2) == Matrix([-e - 2, 2*f]) # old signature uses Lambda a, b, c, d, e, f = symbols('a, b, c, d, e, f') rel = {('C1', 'C2'): Lambda((a, b), (2*a, 3*b + 1)), ('C3', 'C2'): Lambda((e, f), (-e - 2, 2*f))} C1 = CoordSystem('C1', R2_origin, (a, b), rel) C2 = CoordSystem('C2', R2_origin, (c, d), rel) C3 = CoordSystem('C3', R2_origin, (e, f), rel) a, b = C1.symbols c, d = C2.symbols e, f = C3.symbols assert C2.transform(C1) == Matrix([c/2, (d - 1)/3]) assert C1.transform(C3) == Matrix([-2*a - 2, (3*b + 1)/2]) assert C3.transform(C1) == Matrix([-e/2 - 1, (2*f - 1)/3]) assert C3.transform(C2) == Matrix([-e - 2, 2*f])
def NS2D(case,Show="no",type = 'no'): x = symbols('x[0]') y = symbols('x[1]') PrintStr("NS Exact Solution:",3,"-") if Show == "yes": case = 1 if case == 1: u = sin(y)*exp(x) v = cos(y)*exp(x) p = sin(x)*cos(y) if Show == "yes": case +=1 print "Case ",case-1,":\n" Print2D(u,v,p,"NS") if case == 2: u = pow(y,3) v = pow(x,3) p = pow(x,2) if Show == "yes": case +=1 print "Case ",case-1,":\n" Print2D(u,v,p,"NS") if case == 3: u = sin(y)*exp(x+y)+cos(y)*exp(x+y) v = -sin(y)*exp(x+y) p = pow(x,3)*sin(y)+exp(x+y) if Show == "yes": case +=1 print "Case ",case-1,":\n" Print2D(u,v,p,"NS") if case == 4: uu = y*x*exp(x+y) u = diff(uu,y) v = -diff(uu,x) p = sin(x)*exp(y) if Show == "yes": case +=1 print "Case ",case-1,":\n" Print2D(u,v,p,"NS") if case == 5: # uu = y*x*exp(x+y) u = y#**2 v = x#**2 p = x if Show == "yes": case +=1 print "Case ",case-1,":\n" Print2D(u,v,p,"NS") if case == 6: # uu = y*x*exp(x+y) u = y**4 v = x**4 p = x**3 if Show == "yes": case +=1 print "Case ",case-1,":\n" Print2D(u,v,p,"NS") if case ==7: u = y*(y-1)*y*(y-1) v = x*(x-1)*x*(x-1) p = x if case == 8: l = 0.54448373678246 omega = (3./2)*np.pi # r, theta = symbols('r, theta') m = Manifold('M', 2) patch = Patch('P', m) rect = CoordSystem('rect', patch) polar = CoordSystem('polar', patch) phi = symbols('phi') rho = symbols('rho') polar.connect_to(rect, [rho, phi], [rho*cos(phi), rho*sin(phi)]) psi = (sin((1+l)*phi)*cos(l*omega))/(1+l) - cos((1+l)*phi) - (sin((1-l)*phi)*cos(l*omega))/(1-l) + cos((1-l)*phi) psi_prime = diff(psi, phi) psi_3prime = diff(psi, phi, phi, phi) u = rho**l*((1+l)*sin(phi)*psi + cos(phi)*psi_prime) v = rho**l*(-(1+l)*cos(phi)*psi + sin(phi)*psi_prime) p = -rho**(l-1)*((1+l)**2*psi_prime + psi_3prime)/(1-l) # print u # ssss u = u.subs(phi, atan2(y,x)) v = v.subs(phi, atan2(y,x)) p = p.subs(phi, atan2(y,x)) u = u.subs(rho, sqrt(x*x + y*y)) v = v.subs(rho, sqrt(x*x + y*y)) p = p.subs(rho, sqrt(x*x + y*y)) if case == 9: u = 1 v = 1 p = 1 if case == 10: u = y*(y-1)*exp(y) v = x*(x-1)*exp(x) p = y*(y-1)*x*(x-1)*exp(x+y) if case == 11: uu = 100*x**2*(x-1)**2*y**2*(y-1)**2*cos(x) u = diff(uu, y) v = -diff(uu, x) p = y*(y-1)*x*(x-1)*exp(x) if u: f = 1 else: print "No case selected" return if abs(diff(u, x) + diff(v, y)) > 1e-6: return L1 = diff(u,x,x) + diff(u,y,y) L2 = diff(v,x,x) + diff(v,y,y) A1 = u*diff(u,x) + v*diff(u,y) A2 = u*diff(v,x) + v*diff(v,y) P1 = diff(p,x) P2 = diff(p,y) # print u # print v # print class Vec(Expression): def __init__(self, u ,v, X, Y): self.u = u self.v = v self.X = X self.Y = Y def eval_cell(self, values, x, ufc_cell): values[0] = self.u.subs({self.X:x[0], self.Y:x[1]}).evalf() values[1] = self.v.subs({self.X:x[0], self.Y:x[1]}).evalf() def value_shape(self): return (2,) class Scal(Expression): def __init__(self, p, X, Y): self.p = p self.X = X self.Y = Y def eval_cell(self, values, x, ufc_cell): values[0] = self.p.subs({self.X:x[0], self.Y:x[1]}).evalf() # u0 = Vec(u ,v, x, y) # p0 = Scal(p, x, y) u0 = Expression((ccode(u).replace('M_PI','pi'),ccode(v).replace('M_PI','pi')), degree=4) p0 = Expression(ccode(p).replace('M_PI','pi'), degree=4) # Laplacian = Vec(L1, L2, x, y) # Advection = Vec(A1, A2, x, y) # gradPres = Vec(P1, P2, x, y) Laplacian = Expression((ccode(L1).replace('M_PI','pi'),ccode(L2).replace('M_PI','pi')), degree=4) Advection = Expression((ccode(A1).replace('M_PI','pi'),ccode(A2).replace('M_PI','pi')), degree=4) gradPres = Expression((ccode(P1).replace('M_PI','pi'),ccode(P2).replace('M_PI','pi')), degree=4) if Show == "no": Print2D(u,v,p,"NS") if type == "MHD": return u, v, p, u0, p0, Laplacian, Advection, gradPres else: return u0, p0, Laplacian, Advection, gradPres
def test_deprecated(): with warns_deprecated_sympy(): cs_wname = CoordSystem('cs', p, ['a', 'b']) assert cs_wname == cs_wname.func(*cs_wname.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)
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. 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
def main(): # A unit circle in \RR^2: S1 = Manifold('S1', 1) # Coordinate charts :math:`(U_i^\pm, \phi_i^\pm)` for S1: # 0, 1 -> x0, x1 # p, m -> +, - phip0 = CoordSystem('phip0', Patch('Up0', S1), ['x1']) phim0 = CoordSystem('phim0', Patch('Um0', S1), ['x1']) phip1 = CoordSystem('phip1', Patch('Up1', S1), ['x0']) phim1 = CoordSystem('phim1', Patch('Um1', S1), ['x0']) intersections = ( (phip0, phip1), (phip0, phim1), (phim0, phip1), (phim0, phim1),) # Transition maps: x0, x1 = symbols('x:2', real=True) phip0.connect_to(phip1, [x1], [sqrt(1 - x1**2)], inverse=False) phip0.connect_to(phim1, [x1], [sqrt(1 - x1**2)], inverse=False) phim0.connect_to(phip1, [x1], [-sqrt(1 - x1**2)], inverse=False) phim0.connect_to(phim1, [x1], [-sqrt(1 - x1**2)], inverse=False) phip1.connect_to(phip0, [x0], [sqrt(1 - x0**2)], inverse=False) phip1.connect_to(phim0, [x0], [sqrt(1 - x0**2)], inverse=False) phim1.connect_to(phip0, [x0], [-sqrt(1 - x0**2)], inverse=False) phim1.connect_to(phim0, [x0], [-sqrt(1 - x0**2)], inverse=False) # Jacobian matrices: for map0, map1 in intersections: print(f'Jacobian {map0.name} -> {map1.name}: ', map0.jacobian(map1, ['x1'])) print(f'Jacobian {map1.name} -> {map0.name}: ', map1.jacobian(map0, ['x0'])) # Transition maps: data = ( (phip0, 1/2, phip1), (phip0, -1/2, phim1), (phim0, 1/2, phip1), (phim0, -1/2, phim1), (phip1, 1/2, phip0), (phip1, -1/2, phim0), (phim1, 1/2, phip0), (phim1, -1/2, phim0),) for cfrom, t, cto in data: print(f'{cfrom.name}({t:7.4f}) = ' f'{cto.name}({cto.point_to_coords(cfrom.point([t]))[0]:7.4f})')