作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题描述
我正在构建一个库来支持 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;
stopOutflow
开关应防止库存排空至零以下。不幸的是,在下面的示例中,这将无法解决:库存将排空略低于零。
TestModel
使用这些构建块:
function constrainedRate( indicated rate, stopInflow, stopOutflow)
用于返回符合给定约束(即 bool 开关)的速率 connector StockPort
如上所述 connector FlowPort
对应于 StockPort
model MaterialStock
带有一个 StockPort
的股票组件不得低于零排放 model LinearDecline
一个带有一个的流元 FlowPort
(即 Sink)模拟以恒定速率(此处设置为 1)消耗关联股票的模型 stock
与
initialValue = 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
的预期行为:
t = 5.0
处略低于零。然后。
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;
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/
我是一名优秀的程序员,十分优秀!