我正在调用一个在每次通过for循环时使用odeint
的函数(遗憾的是,我无法破坏该循环中的任何内容)。但是,事情的进展比我希望的要慢得多。这是代码:
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
问题是,我在每次运行时调用get_STM
大约8640次,这导致232217次调用dxdt_interface
,大约占总计算时间的70%,每次调用5ms get_STM
的{{1}}(其中99.9%来自odeint
)。
我是SciPy集成技术的新手,根据odeint
的{{3}},我无法弄清楚如何加快速度。我调查dxdt_interface
和documentation,但我无法让它工作,因为dxdt
和Amat
是象征性的。
是否有任何加速odeint
我缺少的技术?
编辑:下面包含Amat
和dxdt
个功能。请注意,这些不会在我的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
答案 0 :(得分:0)
将dxdt
和Amat
作为黑框,您可以做很多事情来加快速度。一种可能性是简化调用它们。 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
来电。