You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于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变量,因此未触发报错。

按以下步骤修改即可解决:

  1. 在MyMedia包中添加specificEnthalpy函数的实现:
redeclare function extends specificEnthalpy "Return specific enthalpy"
  algorithm
    h := cp_const*(state.T - reference_T);
    annotation (smoothOrder=1, Inline=true);
end specificEnthalpy;
  1. 修正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;
  1. 确认BaseProperties中的方程逻辑:由于你的ThermodynamicState仅包含p和T,state.p = p和state.T = T的赋值是合理的,无需修改。

修改后重新编译模型,PartialTestModel即可正常运行。

内容的提问来源于stack exchange,提问作者quaternio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 23:50:04