Beta Decay
Beta Decay

Reputation: 805

Sympy returns "Key Error" when solving a system of ODEs

I am trying to solve the geodesic equation using the initial values:

enter image description here

Where the above is a system of two ODEs. In the code, x1 = T(s) and x2 = P(s) and Γabc = Ga(a,b,c).

My code is as shown below:

from gravipy import *
from sympy import *

def distance(t1,p1,t2,p2):
    init_printing()
    R = 1737
    dimensions = 2
    t,p = symbols("t p")
    x = Coordinates("x",[t,p])
    Metric = diag(R**2, R**2*sin(t)**2)
    g = MetricTensor("g", x, Metric)
    Ga = Christoffel("Ga", g)


    T,P = symbols("T P", cls=Function)
    s = symbols("s")
    ics = {T(1):t1, T(2):t2, P(1):p1, P(2):p2}
    system=[]

    coords = [T(s),P(s)]
    for a in range(dimensions):
        eq = coords[a].diff(s,s)
        for b in range(dimensions):
            for c in range(dimensions):
                christ = Ga(a+1,b+1,c+1).replace("t","T(s)")

                eq += christ * coords[b].diff(s) * coords[c].diff(s)

        system.append(Eq(eq,0))

    print(system)
    T,P = dsolve(system, [T(s), P(s)], ics=ics)

    print(T,P)

    coords=[T(s),P(s)]
    integral = 0
    for mu in range(dimensions):
        for nu in range(dimensions):
            integral += g(mu,nu).replace("t","T(s)") * coords[mu].diff(s) * coords[nu].diff(s)

    print(integrate(sqrt(integral), (s, 1, 2)))

distance(1,1,2,2)

However, when I run the program, it generates the system of equations:

[Eq(-3017169*sin(2*T(s))*Derivative(P(s), s)**2/2 + Derivative(T(s), s, s), 0), Eq(3017169*sin(2*T(s))*Derivative(P(s), s)*Derivative(T(s), s) + Derivative(P(s), s, s), 0)]

Or, more readably:

enter image description here

but then fails when running dsolve(), with the error:

  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\solvers\ode.py", line 584, in dsolve
    match = classify_sysode(eq, func)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\solvers\ode.py", line 1377, in classify_sysode
    if not order[func]:
KeyError: P(s)

Where it alternates between P(s) and T(s). It appears that the error occurs when classifying the ODE, so does this mean that sympy is unable to solve the equations?

Upvotes: 0

Views: 1021

Answers (1)

duffymo
duffymo

Reputation: 308743

Please check the docs, but I believe dsolve is for solving systems of equations using linear algebra and LU decomposition.

I think you want a Runge Kutta 5th order integration method - look into ode.

You have four coupled, first order ODEs:

dM/ds = d^2T/ds^2 = your first equation
dN/ds = d^2P/ds^2 = your second equation
dT/ds = M
dP/ds = N

Upvotes: 1

Related Questions