首页 文章

如何加快SciPy的odeint?

提问于
浏览
0

我正在调用一个函数,在每次通过for循环时使用 odeint (我可以't break anything out of that loop, sadly). But, things are running much slower than I' d希望 . 这是代码:

def get_STM(t_i, t_f, X_ref_i, dxdt, Amat):
    """Evaluate the state transition matrix rate of change for a given A matrix.
    """

    STM_i = np.eye(X_ref_i.size).flatten()
    args = (dxdt, Amat)
    X_aug_i = np.hstack((X_ref_i, STM_i))
    t = [t_i, t_f]

    # Propogate reference trajectory & STM together!    
    X_aug_f = odeint(dxdt_interface, X_aug_i, t, args=args)
    X_f = X_aug_f[-1, :X_ref_i.size]
    STM_f = X_aug_f[-1, X_ref_i.size:].reshape(X_ref_i.size, X_ref_i.size)

    return X_f, STM_f

def dxdt_interface(X,t,dxdt,Amat):
    """
    Provides an interface between odeint and dxdt
    Parameters :
    ------------
    X : (42-by-1 np array) augmented state (with Phi)
    t : time
    dxdt : (function handle) time derivative of the (6-by-1) state vector
    Amat : (function handle) state-space matrix
    Returns:
    --------
    (42-by-1 np.array) time derivative of the components of the augmented state 
    """
    # State derivative
    Xdot = np.zeros_like(X)
    X_stacked = np.hstack((X[:6], t))
    Xdot_state = dxdt(*(X_stacked))
    Xdot[:6] = Xdot_state[:6].T

    # STM
    Phi = X[6:].reshape((Xdot_state.size, Xdot_state.size))

    # State-Space matrix
    A = Amat(*(X_stacked))
    Xdot[6:] = (A .dot (Phi)).reshape((A.size))

    return Xdot

问题是,我在每次运行时调用_867308_大约8640次,这导致232217次调用 dxdt_interface ,大约占总计算时间的70%,每次调用5ms get_STM (99.9%是由于 odeint ) .

我'm new to SciPy'的集成技术,根据 odeintdocumentation,我无法弄清楚如何加快速度 . 我调查 dxdt_interfaceNumba,但我不能让它工作,因为 dxdtAmat 是象征性的 .

有没有什么技术可以加速 odeint 我错过了?

编辑:下面包含 Amatdxdt 函数 . 请注意,这些不在我的major for循环中调用,它们创建传递给我的 get_STM 函数的符号lambdified函数的句柄(我调用 import sympy as sym ) .

def get_A(use_j3=False):
    """ Returns the jacobian of the state time rate of change
    Parameters
    ----------
    R : Earth's equatorial radius (m)
    theta_dot : Earth's rotation rate (rad/s)
    mu : Earth's standard gravitationnal parameter (m^3/s^2)
    j2 : second zonal harmonic coefficient
    j3 : third zonal harmonic coefficient
    Returns
    ----------    
    A : (function handle) jacobian of the state time rate of change
    """
    theta_dot = EARTH['rotation rate']
    R = EARTH['radius']
    mu = EARTH['mu']
    j2 = EARTH['J2']
    if use_j3:
        j3 = EARTH['J3']
    else:
        j3 = 0

    # Symbolic derivations
    x, y, z, mus, j2s, j3s, Rs, t = sym.symbols('x y z mus j2s j3s Rs t', real=True)
    theta_dots = sym.symbols('theta_dots', real=True)
    xdot,ydot,zdot = sym.symbols('xdot ydot zdot ', real=True)

    X = sym.Matrix([x,y,z,xdot,ydot,zdot])

    A_mat = sym.lambdify( (x,y,z,xdot,ydot,zdot,t), dxdt_s().jacobian(X).subs([
        (theta_dots, theta_dot),(Rs, R),(j2s,j2),(j3s,j3),(mus,mu)]), modules='numpy')

    return A_mat

def Dxdt(use_j3=False):
    """ Returns the time derivative of the state vector
    Parameters
    ----------
    R : Earth's equatorial radius (m)
    theta_dot : Earth's rotation rate (rad/s)
    mu : Earth's standard gravitationnal parameter (m^3/s^2)
    j2 : second zonal harmonic coefficient
    j3 : third zonal harmonic coefficient
    Returns
    ----------    
    dxdt : (function handle) time derivative of the state vector
    """

    theta_dot = EARTH['rotation rate']
    R = EARTH['radius']
    mu = EARTH['mu']
    j2 = EARTH['J2']
    if use_j3:
        j3 = EARTH['J3']
    else:
        j3 = 0

    # Symbolic derivations
    x, y, z, mus, j2s, j3s, Rs, t = sym.symbols('x y z mus j2s j3s Rs t', real=True)
    theta_dots = sym.symbols('theta_dots', real=True)
    xdot,ydot,zdot = sym.symbols('xdot ydot zdot ', real=True)

    dxdt = sym.lambdify( (x,y,z,xdot,ydot,zdot,t), dxdt_s().subs([
        (theta_dots, theta_dot),(Rs, R),(j2s,j2),(j3s,j3),(mus,mu)]), modules='numpy')

    return dxdt

1 回答

  • 0

    使用 dxdtAmat 作为黑盒子,你可以做很多事情来加快速度 . 一种可能性是简化调用它们 . hstack 可能有点矫枉过正 .

    In [355]: def dxdt_quiet(*args):
        x=args
        return x
       .....: 
    In [356]: t=1.23
    In [357]: dxdt_quiet(*xs)
    Out[357]: (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.23)
    In [358]: dxdt_quiet(*tuple(x[:6])+(t,))
    Out[358]: (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.23)
    

    元组方法快得多:

    In [359]: timeit dxdt_quiet(*tuple(x[:6])+(t,))
    100000 loops, best of 3: 5.1 µs per loop
    In [360]: %%timeit
    xs=np.hstack((x[:6],1.234))
    dxdt_quiet(*xs)
       .....: 
    10000 loops, best of 3: 25.4 µs per loop
    

    我会做更多这样的测试来优化 dxdt_interface 调用 .

相关问题