使用SymPy Diffgeom计算Christoffel符号时结果全为0的问题
排查SymPy Diffgeom计算Christoffel符号全为0的问题
问题重现
尝试用SymPy的Diffgeom模块计算极坐标系下的Christoffel符号,已定义正确的度量张量,但计算结果全为0,代码如下:
from sympy import symbols, pi, sqrt, atan2, cos, sin from sympy.diffgeom import Manifold, Patch, CoordSystem, Differential, TensorProduct, metric_to_Christoffel_2nd,metric_to_Christoffel_1st,twoform_to_matrix # Define spherical coordinates r, theta = symbols('r theta', nonnegative=True) # Define the relation dictionary for transformations between Cartesian and spherical coordinates relation_dict = { ('Car2D', 'polar'): [(x, y), (sqrt(x**2 + y**2), atan2(y, x))], ('polar', 'Car2D'): [(r, theta), (r*cos(theta), r*sin(theta))] } # Create a manifold, patch, and coordinate systems for spherical coordinates m = Manifold('M', 2) p = Patch('P', m) x, y = symbols('x y', real=True) Car2D = CoordSystem('Car2D', p, (x, y), relation_dict) polar = CoordSystem('Sph', p, (r, theta), relation_dict) # Define differentials for spherical coordinates dr = polar.base_oneform(0) dtheta = polar.base_oneform(1) # Define the metric tensor g = 1*TensorProduct(dr, dr) + r**2 * TensorProduct(dtheta, dtheta) # Compute the Christoffel symbols print(metric_to_Christoffel_2nd(g)) # Print the Christoffel symbols twoform_to_matrix(g)
问题原因
metric_to_Christoffel_2nd函数需要明确指定计算Christoffel符号的坐标系。若不传入coordsys参数,SymPy无法自动识别度量张量所属的坐标系,导致导数计算异常,最终返回全0结果。
你的度量张量虽用极坐标系的基一次形式构建,但未与坐标系建立显式关联,函数无法正确解析计算所需的导数信息。
修正方案
调用metric_to_Christoffel_2nd时,添加coordsys=polar参数,明确指定在极坐标系下计算:
from sympy import symbols, sqrt, atan2, cos, sin from sympy.diffgeom import Manifold, Patch, CoordSystem, TensorProduct, metric_to_Christoffel_2nd, twoform_to_matrix # 定义符号变量 r, theta = symbols('r theta', nonnegative=True) x, y = symbols('x y', real=True) # 坐标变换关系字典 relation_dict = { ('Car2D', 'polar'): [(x, y), (sqrt(x**2 + y**2), atan2(y, x))], ('polar', 'Car2D'): [(r, theta), (r*cos(theta), r*sin(theta))] } # 创建流形、补丁与坐标系 m = Manifold('M', 2) p = Patch('P', m) Car2D = CoordSystem('Car2D', p, (x, y), relation_dict) polar = CoordSystem('Sph', p, (r, theta), relation_dict) # 构建极坐标系下的度量张量 dr = polar.base_oneform(0) dtheta = polar.base_oneform(1) g = TensorProduct(dr, dr) + r**2 * TensorProduct(dtheta, dtheta) # 显式指定坐标系计算第二类Christoffel符号 christoffel_2nd = metric_to_Christoffel_2nd(g, coordsys=polar) print(christoffel_2nd) # 输出度量张量的矩阵形式 print(twoform_to_matrix(g, polar))
正确结果
运行修正后的代码,会得到符合理论的非零Christoffel符号:
- $\Gamma^r_{\theta\theta} = -r$
- $\Gamma^\theta_{r\theta} = \Gamma^\theta_{\theta r} = 1/r$
其余分量为0。
内容的提问来源于stack exchange,提问作者ErikLAndre
相关产品推荐
相关产品推荐

