gpt4 book ai didi

events - 如何在 Modelica 中使用 bool 开关来防止将库存消耗到零以下?

转载 作者:行者123 更新时间:2023-12-01 03:08:02 25 4
gpt4 key购买 nike

问题描述

我正在构建一个库来支持 System Dynamics (SD)就像在 Modelica 中建模一样。不像freely available library通过 Cellier 等人。我相信人们很可能会利用非因果连接器:通过连接器将股票值(value)作为“潜力”传输,可以构建紧凑的组件(例如,流程 = 流程)。

在 SD 中,我们可能会将重要(“大量”)股票与可能变为负值的信息股票区分开来。为了支持这一点,我对 使用了以下定义海量端口 (这里给出了 StockPort 的定义 - 它的对应物,一个 FlowPort ,只会有 bool 输入变量而不是输出变量,稍后给出):

 connector StockPort "Used to represent stock and flow connections"
Real stock "Current value of material in the stock";
flow Real rate "Flow that affects the stock";
// Boolean switches
output Boolean stopInflow "True indicates that nothing can flow into the stock";
output Boolean stopOutflow "True indicates that nothing can flow out of the stock";
end StockPort;

bool 开关为原料的每个端口指示是否允许填充或排放。

对于“ Material 库存” stopOutflow开关应防止库存排空至零以下。不幸的是,在下面的示例中,这将无法解决:库存将排空略低于零。

使用连接器的最小示例

以下 TestModel使用这些构建块:
  • function constrainedRate( indicated rate, stopInflow, stopOutflow)用于返回符合给定约束(即 bool 开关)的速率
  • connector StockPort如上所述
  • connector FlowPort对应于 StockPort
  • model MaterialStock带有一个 StockPort 的股票组件不得低于零排放
  • model LinearDecline一个带有一个的流元 FlowPort (即 Sink)模拟以恒定速率(此处设置为 1)消耗关联股票的模型

  • 主模型简单地发起一个 stockinitialValue = 5连接到 process线性下降与 declineRate = 1 .
    model TestModel "Stop draining a stock below zero"

    function constrainedRate "Set rate for a port according to signals from stock"
    input Real indicatedRate "Proposed rate for port of flow element";
    input Boolean stopInflow "Signal from connected stock";
    input Boolean stopOutflow "Signal from connected stock";
    output Real actualRate "The rate to use";
    protected
    // check whether indicated rate is negative (e.g. an inflow to the connected stock)
    Boolean indRateIsInflow = indicatedRate < 0;
    algorithm
    // set rate to zero if stopSignal matches character of flow
    actualRate := if indRateIsInflow and stopInflow
    then 0
    elseif not indRateIsInflow and stopOutflow
    then 0
    else indicatedRate;
    end constrainedRate;

    connector FlowPort "Used to represent stock and flow connections"
    Real stock "The current stock level (e.g. Potential) of a connected stock or flow data for special stocks";
    flow Real rate "Flows that affect the material stock";
    input Boolean stopInflow "True indicates that nothing can flow into the stock";
    input Boolean stopOutflow "True indicates that nothing can flow out of the stock";
    end FlowPort;

    connector StockPort "Used to represent stock and flow connections"
    Real stock "Current value of stock";
    flow Real rate "Flow that affects the stock";
    output Boolean stopInflow "True indicates that nothing can flow into the stock";
    output Boolean stopOutflow "True indicates that nothing can flow out of the stock";
    end StockPort;

    model MaterialStock "Stock that cannot be drained below zero"
    StockPort outflow;
    parameter Real initialValue;
    protected
    Real x(start = initialValue);
    equation
    // rate of change for the stock
    der(x) = outflow.rate;
    // ports shall have level information for stock
    outflow.stock = x;
    // inflow to stock is unrestricted
    outflow.stopInflow = false;
    // provide Boolean signal in case of negative stock
    outflow.stopOutflow = x <= 0;
    end MaterialStock;

    model LinearDecline "Decline of stock at a constant rate"
    FlowPort massPort;
    parameter Real declineRate(min = 0) "Rate of decline (positive rate diminishes stock)";
    protected
    // a positive rate should drain the stock (which here matches Modelica's rule for flows)
    Real rate(min = 0);
    equation
    rate = declineRate;
    // observe stock signals and constraints
    assert(rate >= 0, "Rate must be positive and will be set to zero", level = AssertionLevel.warning);
    // set the rate according to constraints given by stock
    massPort.rate = constrainedRate( max(rate, 0), massPort.stopInflow, massPort.stopOutflow );
    end LinearDecline;

    // main model
    MaterialStock stock( initialValue = 5 );
    LinearDecline process( declineRate = 1 );
    equation
    connect( stock.outflow, process.massPort );
    end TestModel;

    使用来自 StartTime = 0 的 DASSL 模拟模型至 StopTime = 10揭示变量 stock.outflow.stock 的预期行为:

    Stock Value

    不幸的是,该值在 t = 5.0 处略低于零。然后。

    不知何故,事件(股票值(value) <= 0)被检测到为时已晚。我能做什么?

    (到目前为止,imo 不优雅的治疗方法是使用 when 事件(状态事件)将 reinit 股票值归零。我在 noEvent 语句和 bool 条件上使用 if 包装器的实验也没有成功了。)

    最佳答案

    你能测试这个解决方案吗?使用 Modelica.Constants.eps 对我有用。我还使用了 Dassl,它适用于不同的步长。
    我更改了以下行:

    // provide Boolean signal in case of negative stock
    outflow.stopOutflow = x <= 0;


    // provide Boolean signal in case of negative stock
    outflow.stopOutflow = x <= Modelica.Constants.eps;

    然后,输出数组(在这篇文章中在 python 中查看):
    Output using a zero instead of eps:
    [ 5.00000000e+00 4.00000000e+00 3.00000000e+00 2.00000000e+00
    1.00000000e+00 0.00000000e+00 -7.37276906e-12 -7.37276906e-12
    -7.37276906e-12 -7.37276906e-12 -7.37276906e-12 -7.37276906e-12
    -7.37276906e-12 -7.37276906e-12]
    Output using eps:
    [5. 4. 3. 2. 1. 0. 0. 0. 0. 0. 0. 0. 0.]

    关于events - 如何在 Modelica 中使用 bool 开关来防止将库存消耗到零以下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54715727/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com