Modelica中ExternalMedia库自定义函数调用警告问题求助
解决Modelica函数中
state.eta was neither input nor output警告 问题背景
封装自定义函数h_pT调用ExternalMedia库时,编译出现如下警告:
Public component state.eta was neither input nor output
in function user.pT.h_pT.
Component context: state.eta
Component declared as DynamicViscosity eta in Modelica.Media.Interfaces.Types
File: C:/Users/Yao/Desktop/ExternalMedia/Media/BaseClasses/ExternalTwoPhaseMedium.mo, line 49
相关代码示例:
model sss parameter Real h1=3000; parameter Real T=600; Real p1; equation h1*1000=user.pT.h_pT( p1, T, 0); end sss; function h_pT extends Modelica.Icons.Function; input user.total.Medium.AbsolutePressure p "Pressure"; input user.total.Medium.Temperature T "Temperature"; input user.total.Medium.FixedPhase phase = 0 "2 for two-phase, 1 for one-phase, 0 if not known"; user.total.Medium.ThermodynamicState state; output user.total.Medium.SpecificEnthalpy h; algorithm state:= user.total.Medium.setState_pT( p, T, phase); h:=state.h; end h_pT; function total package Medium extends ExternalMedia.Media.CoolPropMedium( mediumName = "Water"); end Medium; end total;
问题原因
Modelica函数要求所有公共可见组件必须明确声明为input或output。此处的state是ThermodynamicState类型的公共变量,该类型包含多个公共成员(比如eta动态粘度),但state仅作为函数内部临时变量,既非输入也非输出,因此触发了编译器的数据流检查警告。
解决方案
将state标记为protected,使其成为函数内部私有变量,编译器就不会检查其内部成员的输入输出属性。
修改后的h_pT函数代码:
function h_pT extends Modelica.Icons.Function; input user.total.Medium.AbsolutePressure p "Pressure"; input user.total.Medium.Temperature T "Temperature"; input user.total.Medium.FixedPhase phase = 0 "2 for two-phase, 1 for one-phase, 0 if not known"; protected user.total.Medium.ThermodynamicState state; output user.total.Medium.SpecificEnthalpy h; algorithm state:= user.total.Medium.setState_pT( p, T, phase); h:=state.h; end h_pT;
额外说明
如果后续需要访问state的其他成员(如密度、粘度等),可直接在protected块中处理,或把需要的属性添加为函数输出,避免触发同类警告。
内容的提问来源于stack exchange,提问作者white
相关产品推荐
相关产品推荐

