基于TemplateMedium定义新介质时调用partial函数报错求助
问题描述
我按照TemplateMedium的说明定义了一种新介质,但运行PartialTestModel及其他使用该介质的模型时,出现以下错误:
Called function 'MyPackage.TestOfMyMedium.Medium.specificEnthalpy' is partial.
但运行PartialTestModel2时无报错。
我曾参考过OpenModelica论坛的相关讨论,有用户通过对比Annex60库水介质示例、添加reference_XXX与default_XXX参数及smoothOrder注释解决了类似问题,但我无法复现该方案。
以下是我创建的介质包(已根据上述思路修改)及报错的测试模型:
package MyPackage package MyMedia "Template for media models" extends Modelica.Media.Interfaces.PartialMedium( final mediumName="Diesel", final substanceNames={mediumName}, final singleState=false, final reducedX=true, final fixedX=true, Temperature( min=273, max=373, start=323), p_default = 1e5, reference_p = 1e5, reference_T = 278.15, reference_X = {1}); // Provide medium constants here constant SpecificHeatCapacity cp_const=1350 "Constant specific heat capacity at constant pressure -- using approx value at 300K"; redeclare model extends BaseProperties(final standardOrderComponents=true) "Base properties of medium" equation d = density(state); h = cp_const*(T-reference_T); u = h - reference_p/d; MM = 0.025; R_s = 0; state.p = p; state.T = T; end BaseProperties; redeclare function extends density "Return the density" algorithm d := 971 + (((state.p + (-4)*state.T-reference_T + 1700)^(-0.38 + 1))/(-0.38 + 1)) + state.p*(3e-6*(state.T-reference_T)^1.41 + (-0.005)) + (-11)*((state.T-reference_T)^0.5); annotation ( smoothOrder=1, Inline=true); end density; redeclare replaceable record ThermodynamicState "A selection of variables that uniquely defines the thermodynamic state" extends Modelica.Icons.Record; AbsolutePressure p "Absolute pressure of medium"; Temperature T "Temperature of medium"; annotation (Documentation(info="<html> </html>")); end ThermodynamicState; redeclare function extends dynamicViscosity "Return dynamic viscosity" algorithm eta := 0.014773467; annotation (Documentation(info="<html> </html>")); end dynamicViscosity; redeclare function extends thermalConductivity "Return thermal conductivity" algorithm lambda := 0.1; annotation (Documentation(info="<html> </html>")); end thermalConductivity; redeclare function extends specificHeatCapacityCp "Return specific heat capacity at constant pressure" algorithm cp := 1350; annotation (Documentation(info="<html> </html>")); end specificHeatCapacityCp; redeclare function extends isentropicExponent "Return isentropic exponent" extends Modelica.Icons.Function; algorithm gamma := 1.16; annotation (Documentation(info="<html> </html>")); end isentropicExponent; redeclare function extends velocityOfSound "Return velocity of sound" extends Modelica.Icons.Function; algorithm a := 1100; annotation (Documentation(info="<html> </html>")); end velocityOfSound; end MyMedia; model TestOfMyMedium extends Modelica.Media.Examples.Utilities.PartialTestModel( redeclare package Medium = MyPackage.MyMedia); end TestOfMyMedium; end MyPackage;
解决方案
错误的核心原因是你未显式实现specificEnthalpy函数:PartialTestModel会直接调用这个顶层函数,而PartialTestModel2依赖的是BaseProperties中通过方程推导的h变量,因此未触发报错。
按以下步骤修改即可解决:
- 在
MyMedia包中添加specificEnthalpy函数的实现:
redeclare function extends specificEnthalpy "Return specific enthalpy" algorithm h := cp_const*(state.T - reference_T); annotation (smoothOrder=1, Inline=true); end specificEnthalpy;
- 修正
density函数的运算优先级问题:原代码中(-4)*state.T-reference_T的逻辑有误,应改为-4*(state.T - reference_T),修正后的函数:
redeclare function extends density "Return the density" algorithm d := 971 + (((state.p + (-4)*(state.T-reference_T) + 1700)^(-0.38 + 1))/(-0.38 + 1)) + state.p*(3e-6*(state.T-reference_T)^1.41 + (-0.005)) + (-11)*((state.T-reference_T)^0.5); annotation ( smoothOrder=1, Inline=true); end density;
- 确认
BaseProperties中的方程逻辑:由于你的ThermodynamicState仅包含p和T,state.p = p和state.T = T的赋值是合理的,无需修改。
修改后重新编译模型,PartialTestModel即可正常运行。
内容的提问来源于stack exchange,提问作者quaternio
相关产品推荐
相关产品推荐

