当我在谷歌上寻找答案时,我找不到满意的答案。所以我做了一个简单的gist https://gist.github.com/thomaslima/d8e795c908f334931354da95acb97e54使用概念验证解决方案tqdm https://github.com/tqdm/tqdm项目。希望对您有帮助。
编辑:版主要求我对上面链接中发生的情况进行解释。
首先,我使用的是 scipy 的 OOP 版本odeint
(solve_ivp
)但你可以将其调整回odeint
。说你想从时间上整合T0
to T1
并且您希望显示每 0.1% 的进度。您可以修改您的 ode 函数以采用两个额外的参数,一个pbar
(进度条)和state
(集成的当前状态)。就像这样:
def fun(t, y, omega, pbar, state):
# state is a list containing last updated time t:
# state = [last_t, dt]
# I used a list because its values can be carried between function
# calls throughout the ODE integration
last_t, dt = state
# let's subdivide t_span into 1000 parts
# call update(n) here where n = (t - last_t) / dt
time.sleep(0.1)
n = int((t - last_t)/dt)
pbar.update(n)
# we need this to take into account that n is a rounded number.
state[0] = last_t + dt * n
# YOUR CODE HERE
dydt = 1j * y * omega
return dydt
这是必要的,因为函数本身必须知道它所在的位置,但是 scipy 的odeint
并没有真正为函数提供这个上下文。然后,您可以集成fun
使用以下代码:
T0 = 0
T1 = 1
t_span = (T0, T1)
omega = 20
y0 = np.array([1], dtype=np.complex)
t_eval = np.arange(*t_span, 0.25/omega)
with tqdm(total=1000, unit="‰") as pbar:
sol = solve_ivp(
fun,
t_span,
y0,
t_eval=t_eval,
args=[omega, pbar, [T0, (T1-T0)/1000]],
)
请注意,任何可变的东西(如列表)args
被实例化一次并且可以在函数内更改。我建议这样做而不是使用全局变量。
这将显示一个进度条,如下所示:
100%|█████████▉| 999/1000 [00:13<00:00, 71.69‰/s]