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_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 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})')
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) print(v_x(x))
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])) H = polar2.jacobian(rect2, [theta1, theta2, theta3, theta4]) ''' n = Manifold('M', 1) patch3 = Patch('P', n) rect3 = CoordSystem('rect3', patch3)
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
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))