gpt4 book ai didi

modelica - 如何消除 Dymola/Modelica 中的死代码

转载 作者:行者123 更新时间:2023-12-02 19:23:24 26 4
gpt4 key购买 nike

我正在尝试精简一个非常复杂的模型以提高性能,并注意到当我在信号总线中添加或删除变量时,性能会发生很大的变化,尤其是多体框架。

我想知道是否有任何设置可以消除不涉及从模型生成输出的代码。

我尝试将总线连接器设置为“ protected ”,以确保它不会成为输出,但计算它们的代码仍在生成。

我也尝试了这些标志,但它并没有消除死代码:

Advanced.Embedded.OptimizeForOutputs=true;
Advanced.SubstituteVariablesUsedOnce=true;
Evaluate=true;
Advanced.EvaluateAlsoTop=true;
Advanced.SubstituteVariablesUsedOnce=true;

这是一个复制场景的简单模型:

simple modelica model

model TestBusConnector
extends Modelica.Icons.Example;
protected
Modelica.Blocks.Examples.BusUsage_Utilities.Interfaces.ControlBus controlBus
annotation (Placement(transformation(extent={{-20,-20},{20,20}})));
public
Modelica.Blocks.Sources.Sine sine(freqHz=1)
annotation (Placement(transformation(extent={{-40,-50},{-20,-30}})));
Modelica.Blocks.Sources.Constant const(k=0)
annotation (Placement(transformation(extent={{-10,50},{10,70}})));
Modelica.Blocks.Interfaces.RealOutput y
annotation (Placement(transformation(extent={{90,-10},{110,10}})));
equation
connect(y, const.y) annotation (Line(points={{100,0},{60,0},{60,60},{11,60}}, color={0,0,127}));
connect(sine.y, controlBus.testBusVariable)
annotation (Line(points={{-19,-40},{0,-40},{0,0}}, color={0,0,127}));
annotation (experiment(__Dymola_fixedstepsize=0.001, __Dymola_Algorithm="Euler"),
__Dymola_experimentFlags(Advanced(
InlineMethod=0,
InlineOrder=2,
InlineFixedStep=0.001)),
__Dymola_experimentSetupOutput(
states=false,
derivatives=false,
inputs=false,
outputs=false,
auxiliaries=false,
equidistant=false,
events=false));
end TestBusConnector;

从 Dymola 2019 FD01 生成的代码如下所示:

include <dsblock6.c>

PreNonAliasNew(0)
StartNonAlias(0)
DeclareVariable("sine.amplitude", "Amplitude of sine wave", 1, 0.0,0.0,0.0,0,513)
DeclareVariable("sine.freqHz", "Frequency of sine wave [Hz]", 1, 0.0,0.0,0.0,0,513)
DeclareVariable("sine.phase", "Phase of sine wave [rad|deg]", 0, 0.0,0.0,0.0,0,513)
DeclareVariable("sine.offset", "Offset of output signal", 0, 0.0,0.0,0.0,0,513)
DeclareVariable("sine.startTime", "Output = offset for time < startTime [s]", 0,\
0.0,0.0,0.0,0,513)
DeclareVariable("sine.y", "Connector of Real output signal", 0.0, 0.0,0.0,0.0,0,512)
DeclareVariable("const.k", "Constant output value", 0, 0.0,0.0,0.0,0,513)
DeclareVariable("const.y", "Connector of Real output signal", 0, 0.0,0.0,0.0,0,513)
DeclareOutput("y", "", 0, 0.0, 0.0,0.0,0.0,0,513)
DeclareAlias2("controlBus.testBusVariable", "Connector of Real output signal", \
"sine.y", 1, 5, 5, 1028)
EndNonAlias(0)

#define DymolaHaveUpdateInitVars 1
#include <dsblock5.c>

DYMOLA_STATIC void UpdateInitVars(double*time, double* X_, double* XD_, double* U_, double* DP_, int IP_[], Dymola_bool LP_[], double* F_, double* Y_, double* W_, double QZ_[], double duser_[], int iuser_[], void*cuser_[],struct DYNInstanceData*did_,int initialCall) {
}
StartDataBlock
EndDataBlock

翻译后的 modelica 代码 (dsmodel.mof) 仍然包含正弦 block 的计算。

// Translated Modelica model generated by Dymola from Modelica model 
// TEMP.TEST.TestBusConnector


// -----------------------------------------------------------------------------

// Initial Section
sine.amplitude := 1;
sine.freqHz := 1;
sine.phase := 0;
sine.offset := 0;
sine.startTime := 0;
const.k := 0;
const.y := 0;
y := 0.0;




// -----------------------------------------------------------------------------

// Conditionally Accepted Section
sine.y := (if time < 0 then 0 else sin(6.283185307179586*time));

// -----------------------------------------------------------------------------

// Eliminated alias variables
// To have eliminated alias variables listed, set
// Advanced.OutputModelicaCodeWithAliasVariables = true
// before translation. May give much output.

理想情况下,我希望模型转换为:

y := 0.0;

最佳答案

其他答案不起作用的原因是您的模型与您的问题不一致:“我想知道是否有任何设置可以消除不涉及从模型生成输出的代码。”

通过将控制总线连接到 sine.y,您隐式地创建了一个输出,因此 sine.y 参与从模型生成输出。

可以通过以下方式之一来避免这种情况:

  • 删除 sine.y 和 controlBus 之间的连接
  • 更改要保护的controlBus
  • 进行更改,使 controlBus 不在顶层

关于modelica - 如何消除 Dymola/Modelica 中的死代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152661/

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