ibpsa / modelica-ibpsa

Modelica library for building and district energy systems developed within IBPSA Project 1
https://ibpsa.github.io/project1
142 stars 84 forks source link

Inlining (linear) functions of the media packages #227

Closed Mathadon closed 9 years ago

Mathadon commented 9 years ago

Consider the following example:

model AnalyticalSolution

  replaceable package Medium = Annex60.Media.Air;

  Sources.Boundary_pT bou(
    redeclare package Medium = Medium, nPorts=2) 
    "Boundary for pressure boundary condition"
    annotation (Placement(transformation(extent={{-100,-10},{-80,10}})));

  MixingVolumes.MixingVolume vol(
    energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyState,
    massDynamics=Modelica.Fluid.Types.Dynamics.SteadyState,
    allowFlowReversal=false,
    nPorts=2,
    redeclare package Medium = Medium,
    V=1,
    m_flow_nominal=1)
    annotation (Placement(transformation(extent={{0,0},{-20,20}})));
  Movers.BaseClasses.IdealSource idealSource(
    redeclare package Medium = Medium,
    control_m_flow=true,
    allowFlowReversal=false,
    m_flow_small=0.01)
    annotation (Placement(transformation(extent={{-60,-10},{-40,10}})));
  Modelica.Thermal.HeatTransfer.Components.ThermalConductor thermalConductor(G=1000)
    annotation (Placement(transformation(extent={{10,0},{30,20}})));
  Modelica.Thermal.HeatTransfer.Sources.FixedTemperature fixedTemperature(T=273.15)
    annotation (Placement(transformation(extent={{60,0},{40,20}})));
  Modelica.Blocks.Sources.Step  const(
                                     startTime=0.5)
    annotation (Placement(transformation(extent={{-80,20},{-60,40}})));
  MixingVolumes.MixingVolume vol1(
    allowFlowReversal=false,
    nPorts=2,
    redeclare package Medium = Medium,
    V=1,
    m_flow_nominal=1)
    annotation (Placement(transformation(extent={{0,-20},{-20,-40}})));
equation 

  connect(vol.heatPort, thermalConductor.port_a) annotation (Line(
      points={{0,10},{10,10}},
      color={191,0,0},
      smooth=Smooth.None));
  connect(fixedTemperature.port, thermalConductor.port_b) annotation (Line(
      points={{40,10},{30,10}},
      color={191,0,0},
      smooth=Smooth.None));
  connect(idealSource.m_flow_in, const.y) annotation (Line(
      points={{-56,8},{-56,30},{-59,30}},
      color={0,0,127},
      smooth=Smooth.None));
  connect(idealSource.port_a, bou.ports[1]) annotation (Line(
      points={{-60,0},{-70,0},{-70,2},{-80,2}},
      color={0,127,255},
      smooth=Smooth.None));
  connect(idealSource.port_b, vol.ports[1]) annotation (Line(
      points={{-40,0},{-8,0}},
      color={0,127,255},
      smooth=Smooth.None));
  connect(vol.ports[2], vol1.ports[1]) annotation (Line(
      points={{-12,0},{-8,0},{-8,-20}},
      color={0,127,255},
      smooth=Smooth.None));
  connect(vol1.ports[2], bou.ports[2]) annotation (Line(
      points={{-12,-20},{-80,-20},{-80,-2}},
      color={0,127,255},
      smooth=Smooth.None));
  annotation (Diagram(coordinateSystem(preserveAspectRatio=false, extent={{-100,
            -100},{100,100}}), graphics),
    experiment(
      StopTime=1000,
    Icon(coordinateSystem(extent={{-100,-100},{100,100}}, preserveAspectRatio=false)));
end AnalyticalSolution;

When using Annex60.Media.Water Dymola detects a linear system of equations but this can be solved analytically, resulting in:

Sizes of linear systems of equations: {5}
Sizes after manipulation of the linear systems: {0}
Sizes of nonlinear systems of equations: { }
Sizes after manipulation of the nonlinear systems: { }

However, when using Annex60.Media.Air we get:

Sizes of linear systems of equations: { }
Sizes after manipulation of the linear systems: { }
Sizes of nonlinear systems of equations: {5}
Sizes after manipulation of the nonlinear systems: {1}

Dsmodel.mof contains the following system:

algorithm // Torn part
    vol.heatPort.T := temperature_phX_Unique4(bou.p, vol.ports[1].h_outflow, cat
      (1, vol.Xi, {1-sum(vol.Xi)}));
    thermalConductor.dT := vol.heatPort.T-fixedTemperature.T;
    vol.heatPort.Q_flow :=  -thermalConductor.G*thermalConductor.dT;
    vol.dynBal.ports_H_flow[2] := (if semiLinearCondition(noEvent(
      vol.ports[2].m_flow >= 0)) then vol1.ports[1].h_outflow*vol.ports[2].m_flow
       else vol.ports[1].h_outflow*vol.ports[2].m_flow);

  equation // Residual equations
    0 = vol.heatPort.Q_flow+vol.dynBal.ports_H_flow[1]+vol.dynBal.ports_H_flow[2];

the problem seems to be that there is a function in this algorithm: temperature_phX_Unique4 due to this function Dymola is unable to exploit the (linear) problem structure. This can be solved by setting Inline=true in Annex60.Media.Air.temperature_phX:

Sizes of linear systems of equations: {5}
Sizes after manipulation of the linear systems: {0}
Sizes of nonlinear systems of equations: { }
Sizes after manipulation of the nonlinear systems: { }

We should probably identify other functions where inlining may be advantageous.

mwetter commented 9 years ago

@Mathadon This is a good improvement. I verified that your solution works, made a branch (issue227_inline) and added Inline=true for temperature_phX and specificEnthalpy_pTX. I am quite sure that there are other such cases. It would be good if you can look at the library and issue a pull request.

Mathadon commented 9 years ago

I'll have a look at it, possibly after the BS2015 deadline.

mwetter commented 9 years ago

@Mathadon : I added Inline=true to all media functions.