Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

New Medium Model

New Medium Model

Hi
I'm trying to creating a new medium model using the TemplateMedium package. I've created this simple model:


model Example
  inner Modelica.Fluid.System system annotation(
    Placement(visible = true, transformation(origin = {-270, 84}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Fluid.Sources.MassFlowSource_T boundary( m_flow = 10, nPorts = 1)  annotation(
    Placement(visible = true, transformation(origin = {-224, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Fluid.Sources.FixedBoundary boundary1(nPorts = 1)  annotation(
    Placement(visible = true, transformation(origin = {44, -2}, extent = {{-10, -10}, {10, 10}}, rotation = 180)));
  Modelica.Fluid.Pipes.StaticPipe pipe(diameter = 5e-2, length = 10)  annotation(
    Placement(visible = true, transformation(origin = {-96, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(boundary.ports[1], pipe.port_a) annotation(
    Line(points = {{-214, 0}, {-108, 0}, {-108, 0}, {-106, 0}}, color = {0, 127, 255}));
  connect(pipe.port_b, boundary1.ports[1]) annotation(
    Line(points = {{-86, 0}, {34, 0}, {34, -2}, {34, -2}}, color = {0, 127, 255}));

annotation(
    uses(Modelica(version = "3.2.2")));
end Example;


I want to use a medium with specific properties so I've created a new package from the TemplateMedium package, copying the TemplateMedium package in the new one (removing the "partial" keyword from the package, as suggested by OpenModelica). This is the new Medium package:


package TemplateMedium "Template for media models"
 
  extends Modelica.Media.Interfaces.PartialMedium(
    final mediumName="NameOfMedium",
    final substanceNames={mediumName},
    final singleState=true,
    final reducedX=true,
    final fixedX=true,
    Temperature(
      min=273,
      max=450,
      start=323),
      pressure(min=1e5,
      max=3e5,
      start=2e5));

 
  constant SpecificHeatCapacity cp_const
    "Constant specific heat capacity at constant pressure";

 



  redeclare model extends BaseProperties(final standardOrderComponents=true)
    "Base properties of medium"

  equation
    d = 1;
    h = cp_const*298.15;
    u = h - 2e5/d;
    MM = 0.024;
    R = 8.3144/MM;
    state.p = p;
    state.T = T;
  end BaseProperties;


  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 := 10 - state.T*0.3 + state.p*0.2;
    annotation (Documentation(info="<html>

</html>"));
  end dynamicViscosity;

  redeclare function extends thermalConductivity
    "Return thermal conductivity"
  algorithm
    lambda := 1;
    annotation (Documentation(info="<html>

</html>"));
  end thermalConductivity;

  redeclare function extends specificEntropy "Return specific entropy"
  algorithm
    s := 2;
    annotation (Documentation(info="<html>

</html>"));
  end specificEntropy;

  redeclare function extends specificHeatCapacityCp
    "Return specific heat capacity at constant pressure"
  algorithm
    cp := 4;
    annotation (Documentation(info="<html>

</html>"));
  end specificHeatCapacityCp;

  redeclare function extends specificHeatCapacityCv
    "Return specific heat capacity at constant volume"
  algorithm
    cv := 4.2;
    annotation (Documentation(info="<html>

</html>"));
  end specificHeatCapacityCv;

  redeclare function extends isentropicExponent "Return isentropic exponent"
    extends Modelica.Icons.Function;
  algorithm
    gamma := 1;
    annotation (Documentation(info="<html>

</html>"));
  end isentropicExponent;

  redeclare function extends velocityOfSound "Return velocity of sound"
    extends Modelica.Icons.Function;
  algorithm
    a := 3;
    annotation (Documentation(info="<html>

</html>"));
  end velocityOfSound;

  annotation (Documentation(info="<html>
<p>
This package is a <b>template</b> for <b>new medium</b> models. For a new
medium model just make a copy of this package, remove the
\"partial\" keyword from the package and provide
the information that is requested in the comments of the
Modelica source.
</p>
</html>"));
end TemplateMedium;


Before simulating the model I've redeclared the package in the model:


model Example
  inner Modelica.Fluid.System system annotation(
    Placement(visible = true, transformation(origin = {-270, 84}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Fluid.Sources.MassFlowSource_T boundary(redeclare package Medium =
  Modelica.Media.Interfaces.TemplateMedium, m_flow = 10, nPorts = 1)  annotation(
    Placement(visible = true, transformation(origin = {-224, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Fluid.Sources.FixedBoundary boundary1(redeclare package Medium =
  Modelica.Media.Interfaces.TemplateMedium,nPorts = 1)  annotation(
    Placement(visible = true, transformation(origin = {44, -2}, extent = {{-10, -10}, {10, 10}}, rotation = 180)));
  Modelica.Fluid.Pipes.StaticPipe pipe(redeclare package Medium =
  Modelica.Media.Interfaces.TemplateMedium,diameter = 5e-2, length = 10)  annotation(
    Placement(visible = true, transformation(origin = {-96, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(boundary.ports[1], pipe.port_a) annotation(
    Line(points = {{-214, 0}, {-108, 0}, {-108, 0}, {-106, 0}}, color = {0, 127, 255}));
  connect(pipe.port_b, boundary1.ports[1]) annotation(
    Line(points = {{-86, 0}, {34, 0}, {34, -2}, {34, -2}}, color = {0, 127, 255}));

annotation(
    uses(Modelica(version = "3.2.2")));
end Example;


When I check the model I have this error:
Cannot resolve type of expression Example.pipe.flowModel.Medium.pressure(BOX(pipe.flowModel.states[i + 1])) -
Example.pipe.flowModel.Medium.pressure(BOX(pipe.flowModel.states[i])). The operands have types #Real, #Real in component <NO_COMPONENT>.


What is wrong in my model? How can I create a new medium model in OpenModelica? Please help me!!!!

Edited by: Sover1 - Sep-22-20 08:50:18

Re: New Medium Model

Have you ever gotten feedback, input or found a solution? I am facing the very same problem and error.

Re: New Medium Model

Hi,
I think that there are two different problems here. One is the definition of the Medium: yes you can define a Medium extending directly from Modelica.....PartialMedium, but it is quite time consuming as you need to redefine all the needed functions. Normally it is easier to extend from a more elaborated partial medium. As I see that you are using fixed values for almost all physical properties, I have selected an extension from PartialSimpleMedium. And also take into account that the values for physical and thermodynamic propeties must be in basic SI units, and referenced to mass and not to moles. You can extend also from more complex models as for example PartialLinearFluid. You can look at how it is done at Modelica.Media.CompressibleLiquids.LinearColdWater.
The second point is related with the use of the medium. If you define a medium package, you must use it, not the partial medium from which it extends.
It follows a medium definition and its use. I have checked that the example runs but I have not checked if it has been build correctly.

package SimpleMedium
  extends Modelica.Media.Interfaces.PartialSimpleMedium(
    mediumName="simple medium",
    cp_const=2000,
    cv_const=2000,
    d_const=1020,
    eta_const=1.0e-3,
    lambda_const=0.1,
    a_const=1000,
    T_min=273.15,
    T_max=573.15,
    T0=273.15,
    MM_const=0.024);
end SimpleMedium;

model Example
  inner Modelica.Fluid.System system annotation(
    Placement(visible = true, transformation(origin = {-270, 84}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Fluid.Sources.MassFlowSource_T boundary(redeclare package Medium =
  SimpleMedium, m_flow = 10, nPorts = 1)  annotation(
    Placement(visible = true, transformation(origin = {-224, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Fluid.Sources.FixedBoundary boundary1(redeclare package Medium =
  SimpleMedium,nPorts = 1)  annotation(
    Placement(visible = true, transformation(origin = {44, -2}, extent = {{-10, -10}, {10, 10}}, rotation = 180)));
  Modelica.Fluid.Pipes.StaticPipe pipe(redeclare package Medium =
  SimpleMedium,diameter = 5e-2, length = 10)  annotation(
    Placement(visible = true, transformation(origin = {-96, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(boundary.ports[1], pipe.port_a) annotation(
    Line(points = {{-214, 0}, {-108, 0}, {-108, 0}, {-106, 0}}, color = {0, 127, 255}));
  connect(pipe.port_b, boundary1.ports[1]) annotation(
    Line(points = {{-86, 0}, {34, 0}, {34, -2}, {34, -2}}, color = {0, 127, 255}));

annotation(
    uses(Modelica(version = "3.2.2")));
end Example;

Carlos

Re: New Medium Model

Thanks for the response and advise. Much appreciated.

I will try to implement the same and see how it does. I do plan to use a more complicated fluid / medium, but was trying to start with small steps.

I used to work with Simulation X which had a 'Wizard' to create a new medium / fluid that could be named, referenced and used later. The OpenModelica ways are still very new to me. Assume I will have to write the definitions of the new fluid directly into my model and remove all reference to other medium / fluids.
many thanks,
Thomas

There are 0 guests and 0 other users also viewing this topic