用odein求ODEs的执行时间

2024-10-01 09:19:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想打印出方程组每t+dt的解的执行时间,其中dt是.01秒

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import time
from datetime import timedelta


# function that returns dz/dt
def model(z,t):
  start_time = time.clock()

  dxdt = z[1]
  dydt =  -1.2*(z[0]+z[1]) + 0.2314*z[0] + 0.6918*z[1] - 0.6245*abs(z[0])*z[1] + 0.0095*abs(z[1])*z[1] + 0.0214*z[0]*z[0]*z[0]
  dzdt = [dxdt,dydt]


#Print
 elapsed_time_secs = time.clock()-start_time
 msg = "Execution took: %s secs (Wall clock time)" % 
 timedelta(seconds=round(elapsed_time_secs,5))
 print(msg)


 return dzdt

# initial condition
 z0 = [0,0]

# time points
 t = np.linspace(0,10,num=1000)

# solve ODE
 z = odeint(model,z0,t)

由于我的时间间隔是.01(10/1000),从技术上讲,我应该有1000行输出语句,但由于某些原因,打印的行要少得多。这取决于设置的初始条件。对于[0,0],大约打印10行。对于[1,2],大约打印了400行。我不明白为什么会这样。你知道吗


Tags: fromimportmodeltimeasnp时间dt
1条回答
网友
1楼 · 发布于 2024-10-01 09:19:36

你在做一些奇怪的事情,你在测量ODE函数的执行时间。由于您的一些算术运算需要大约100个时钟周期,最多1000个时钟周期,并且您的处理器以每秒10^9个时钟周期工作,因此您的调用将需要大约1e-6秒,这在给定的输出格式中是看不到的。有时您会看到一些解释器开销和JIT编译时间。你知道吗

odeint与内部自适应步长一起工作,请求的输出从内部步长内插。内部步长可以小于输入时间数组的时间步长,或者对于常量解等无聊解,内部步长可以非常大。ODE函数将只在内部步骤调用,而不是在所有请求的输出时间调用。如果将tz添加到打印的值中,您将看到。你知道吗

(0.0, array([ 0.,  0.]), 'Execution took: 0:00:00.000010 secs (Wall clock time)')
(1.221926641140105e-06, array([ 0.,  0.]), 'Execution took: 0:00:00 secs (Wall clock time)')
(2.44385328228021e-06, array([ 0.,  0.]), 'Execution took: 0:00:00.000010 secs (Wall clock time)')
(0.01222171026468333, array([ 0.,  0.]), 'Execution took: 0:00:00.000010 secs (Wall clock time)')
(0.02444097667608438, array([ 0.,  0.]), 'Execution took: 0:00:00.000010 secs (Wall clock time)')
(0.03666024308748543, array([ 0.,  0.]), 'Execution took: 0:00:00.000010 secs (Wall clock time)')
(0.15885290720149592, array([ 0.,  0.]), 'Execution took: 0:00:00.000010 secs (Wall clock time)')
(0.28104557131550645, array([ 0.,  0.]), 'Execution took: 0:00:00 secs (Wall clock time)')
(0.403238235429517, array([ 0.,  0.]), 'Execution took: 0:00:00 secs (Wall clock time)')
(1.625164876569622, array([ 0.,  0.]), 'Execution took: 0:00:00 secs (Wall clock time)')
(2.8470915177097273, array([ 0.,  0.]), 'Execution took: 0:00:00 secs (Wall clock time)')
(4.069018158849833, array([ 0.,  0.]), 'Execution took: 0:00:00 secs (Wall clock time)')
(16.288284570250884, array([ 0.,  0.]), 'Execution took: 0:00:00 secs (Wall clock time)')

相关问题 更多 >