Scipy odeint 没有参数或括号的方法

Method without arguments or parenthesis for Scipy odeint

求助 - 我无法理解自己的代码!哈哈 我是 python 的新手,经过多次试验和错误后,我的代码可以正常工作,但其中有一个特定的部分我不明白。

在下面的代码中,我通过 scipy 的 odeint 函数求解一个相当基本的 ODE。我的目标是在这个蓝图的基础上构建更复杂的系统。

我的问题: 我如何在没有任何参数和右括号的情况下调用方法 .reaction_rate_simple?这在 python 中意味着什么?我应该在某处使用静态方法吗?

如果有人对此有任何反馈 - 也许这是一段糟糕的代码,并且有更好的解决方法!

非常感谢您的回复和帮助!

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

class batch_reator:

    def __init__(self, C_init, volume_reactor, time_init, time_end):
        self.C_init = C_init
        self.volume = volume_reactor
        self.time_init = time_init
        self.time_end = time_end
        self.operation_time = (time_end - time_init)
    
    def reaction_rate_simple(self, concentration_t, t, stoch_factor, order, rate_constant):
        reaction_rate = stoch_factor * rate_constant * (concentration_t ** order)
        return reaction_rate

    def equations_system(self, kinetics):
        dCdt = kinetics
        return dCdt


C_init = 200
time_init, time_end = 0, 1000 
rate_constant, volume_reactor, order, stoch_factor = 0.0001, 10, 1, -1
time_span = np.linspace(time_init, time_end, 100)
 
Batch_basic = batch_reator(C_init, volume_reactor, time_init, time_end)       
kinetics = Batch_basic.reaction_rate_simple

sol = odeint(Batch_basic.equations_system(kinetics), Batch_basic.C_init, time_span, args=(stoch_factor, order, rate_constant))

plt.plot(time_span, sol)
plt.show() 

我假设你指的是行

kinetics = Batch_basic.reaction_rate_simple

您不是在调用它,而是将方法保存为变量,然后将该方法传递给 equations_system(...),这只是 returns 它。我不熟悉 odeint,但根据文档,它接受一个可调用对象,这就是你给它的。

在python函数中,lambdas,类都是可调用的,可以分配给变量并传递给函数,并在需要时调用。

在这种特殊情况下,回调定义来自 odeint docs

func callable(y, t, …) or callable(t, y, …) Computes the derivative of y at t. If the signature is callable(t, y, ...), then the argument tfirst must be set True.

所以前两个参数是odeint传进来的,另外三个是你指定的参数来的