如何强制solve_ivp仅允许集成变量的积极解决方案?

发布于 2025-02-08 17:44:24 字数 716 浏览 1 评论 0原文

随着时间的流逝,我正在对表面上的冰冻进行建模。这需要计算“冷冻/熔化速率”,这可能大于或小于零。 我要施加的约束是,如果表面上的冷冻冰的量为零,如果冻结速率为负,则冰的量不能低于零(物理上不可能)。

是我想做的最小例子。您可以看到我试图强制解决方案值为> = 0的if语句,但是这不起作用。您如何使用solve_ivp执行此操作?

注意:此代码不运行,它只是显示我要做什么的外壳

def df(t,x,*system_constants):

    ##setting up derivative array
    dxdt = np.zeros_like(x)

    # [position][diff eq number]
    # Ice solid deposition rate (dMi/dt), solving for total deposited ice
    dxdt[0][0] = x[0][0] * a

    if x[0][0] < 0:
        x[0][0] = 0

    return dxdt

#create your initial conditions
t0 = 0
tf = 10 
t = (0,tf)

#solve the system
sol = solve_ivp(df, t, x0, args = system_constants, method = 'LSODA', dense_output=True)

I am modelling the freezing of ice on a surface over time. This requires calculating the 'rate of freezing/melting', which can be greater or less than zero. The constraint I want to impose is that if the amount of frozen ice on the surface is zero, and if the rate of freezing is negative, the amount of ice cannot go below zero (physically impossible).

This is a minimal example of what I am trying to do. You can see the if statement where I try to force the solution value to be >= 0, however this does not work. How do you do this with solve_ivp?

Note: this code does not run, it is just a shell to show what I am looking to do

def df(t,x,*system_constants):

    ##setting up derivative array
    dxdt = np.zeros_like(x)

    # [position][diff eq number]
    # Ice solid deposition rate (dMi/dt), solving for total deposited ice
    dxdt[0][0] = x[0][0] * a

    if x[0][0] < 0:
        x[0][0] = 0

    return dxdt

#create your initial conditions
t0 = 0
tf = 10 
t = (0,tf)

#solve the system
sol = solve_ivp(df, t, x0, args = system_constants, method = 'LSODA', dense_output=True)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文