使用solve_ivp时有没有办法存储中间值? [英] Is there a way to store intermediate values when using solve_ivp?

查看:39
本文介绍了使用solve_ivp时有没有办法存储中间值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究用 scipy.integrate.solve_ivp 求解的微分方程组.对于这个系统的每个变化项,我组合了不同的中间变量.我正在寻找一种方法来存储这些中间变量,而不仅仅是结果变量.

I'm working on a system of differential equation that I solve with scipy.integrate.solve_ivp. For each variation term of this system I combine different intermediate variables. I'm looking for a way to store those intermediate variables and not only the resulting variables.

我想使用列表或其他类型的可迭代对象将中间值直接存储在传递给 solve_ivp 的 func 中,但这似乎不是最好的方法,并且不允许我使用插值, 使用密集输出时.我也可以使用solve_ivp的结果重新计算这些中间变量,但由于计算有些复杂,直接存储这些值并且只计算一次应该更容易.

I've thinking to use a list or some other kind of iterable to store the intermediate values directly in the func passed to solve_ivp but it doesn't seem to be the best approach and doesn't allow me to use the interpolants, when using a dense_output. I could also recompute those intermediate variables by using the results of solve_ivp, but as the computation are somehow complicated it should be way easier to store directly the values and only compute them only once.

我的 func 函数是这样的:

My func function looks like that :

def dY(t, Y):
    C1, C2 = Y

    dC1 = A(C1, C2) + B(C1, C2)
    dC2 = C(C1, C2) + B(C1, C2)

    return [dC1, dC2]

我想在每个步骤中存储 A、B、C 和 D 的值,以及 C1 和 C2 的值(对于它所说的那些人来说,这是一个化学工程问题,而 A、B、C 和D 是不同的接收器和源项)

I would like to store the values of A, B, C and D at each step, as are the values of C1 and C2 (for those to whom it speaks, it's a chemical engineering problem and A, B, C and D are different sink and source terms)

感谢您的帮助!

推荐答案

使用解决方案的值并在事后调用函数.不要使用存储在 dY 函数中的中间值.刚性 ODE 积分器将在一个时间步长内多次调用该函数,最终会存储无意义的数据.非刚性 ODE 积分器可能也可能在一次尝试中尝试多个时间步,但会遇到相同的问题.

Use the values of the solutions and call the functions after the fact. Do not use intermediate values that get stored inside the dY function. Stiff ODE integrators will call the function multiple times during a time step, you will end up with meaningless data getting stored. Nonstiff ODE integrators might also attempt multiple time steps within one try which will have the same problem.

以下是如何在事后重建 A 和 B 的示例:

Here is an example of how to reconstruct A and B after the fact:

from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt

tspan = (0, 10)
y0 = [1, 2]

def A(C1, C2):
    return 2*C1 + 3*C2 - C1**2
def B(C1, C2):
    return 1*C1 + 4*C2
def C(C1, C2):
    return 1*C1 + 3*C2 - C2**2
def D(C1, C2):
    return 2*C1 + 5*C2
def dY(t, Y):
    C1, C2 = Y

    dC1 = A(C1, C2) + B(C1, C2)
    dC2 = C(C1, C2) + B(C1, C2)

    return [dC1, dC2]
sol = solve_ivp(dY, tspan, y0)

fig,ax = plt.subplots()
ax.plot(sol.t, sol.y[0], label='C1')
ax.plot(sol.t, sol.y[1], label='C2')
ax.legend()
fig2,ax2 = plt.subplots()
ax2.plot(sol.t, A(sol.y[0], sol.y[1]), label='A')
ax2.plot(sol.t, B(sol.y[0], sol.y[1]), label='B')
ax2.legend()

这篇关于使用solve_ivp时有没有办法存储中间值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆