title: Error Messages by using Modelica.Fluid Library for controlled heat storage
body: <p>Dear Modelica Users and Developers,</p>
  
  <p>I am using the Standard Modelica.Fluid library to model a simple heat transfer problem with storages. After several unsuccessfull trials to set up a simple model I now tried to modify the examples until I am able to use it for my purpose.</p>
  
  <p>Unfortunately if I start modifing and adappting things I often run into errors I am not able to solve.</p>
  
  <p>My model is based on Modelica.Fluid.Examples.HeatingSystem. My aim is to build a model with two tanks, two pipes with heat exchange and two pumps which cycle the fluid. The two pumpes shell be controlled to regulate the mass flow in the pipes to provide a constant temperature to the fluid which is then stored in the tanks for further use. From my point of view this task should be a simple one using the Modelica.Fluid Library.</p>
  
  <p>So first of all the question, if there are any good documentations to this Library (exept of the one which is officially, which I am already using). I am looking for some kind of FAQ or similar because my error messeages I get vary widely.</p>
  
  <p>If I introduce a PI controler to meassure the temperature and controll the mass flow in the pipe, I often get following error message: (see also example below and  <a href="http://i.stack.imgur.com/X5gcr.png" rel="nofollow">1</a>)</p>
  
  <pre><code>Cannot reduce the DAE index and select states.
  This is most likely due to non-differentiable functions.
  </code></pre>
  
  <p>Another message is concerning the use of modelicas homotrophy mode and a singular system:</p>
  
  <pre><code>Solving using global homotopy-method.
  The following error was detected at time: 0
  Error: Scalar system is always singular for pump.dp_pump = (homotopy(0.0, 0.0)-pump.W_single)/( -homotopy(1.25*pump.V_flow_single, 0.0)) = 0/-0
  Error: Failed to start model.
  </code></pre>
  
  <p>Or I get somethin similar like this:</p>
  
  <pre><code>[...]abs(y0d) &lt; 1E-015 or abs(y1d) &lt; 1E-015
  The following error was detected at time: 0
  regFun3(): Derivatives at data points do not allow co-monotone interpolation, as both are non-zero, of opposite sign and have an absolute value larger than machine eps (y0d = -0.000690531, y1d = 0.999503). Please correct arguments.
  The stack of functions is:
  Modelica.Fluid.Utilities.regFun3
  massFlowRate_dp_staticHead_Unique16
  massFlowRate_dp_staticHead_Unique16(pipe1.flowModel.dps_fg[3], pipe1.flowModel.rhos[3], pipe1.flowModel.rhos[4], pipe1.flowModel.mus[3], pipe1.flowModel.mus[4], 0.25, pipe1.dimensions[2], 0.0, pipe1.crossAreas[2], pipe1.roughnesses[2], 0.3333333333333333*pipe1.flowModel.dp_small, 4000)
  Non-linear solver will attempt to handle this problem.
  
  ERROR: Failed to solve non-linear system using Newton solver.
  To get more information: Turn on Simulation/Setup/Debug/Nonlinear solver diagnostics/Details
  Solution to systems of equations not found at time = 0
     Nonlinear system of equations number = 1
     Infinity-norm of residue = 20
     Iteration is not making good progress.
     Accumulated number of residue       calc.: 1332
     Accumulated number of symbolic Jacobian calc.: 210
     Last values of solution vector:
  pipe1.state_b.T = 300
     Last values of residual vector:
  { 20 }
  
  Error: could not solve simplified initialization for homotopy method.
  Error: could not solve simplified initialization for homotopy method.
  Error: Failed to start model.
  </code></pre>
  
  <p>Since I would still call me a beginner to Modelica, I reached a "dead point" searching for solutions to this problems. Furthermore it is very hard to understand every model of the fluid library in detail. Maybe somebody could comment some of the error messages or give some advices on how to get into the Modelica Fluid Lib.</p>
  
  <p>I would appreciate every kind of help for this issue. If you would like to get the model file to run it directly I could provide it to you to specifiy the problems.</p>
  
  <p>I would be delighted if you can support me solving this issues.</p>
  
  <p>Many thanks,
  Marius</p>
  
  <p>To give one example: Here <a href="http://i.stack.imgur.com/X5gcr.png" rel="nofollow">1</a> is one of my current problems in detail (which is right now my biggest one). Without the controlled mass flow the model works perfectly. When I introduce the PI-Controller I get the error message:</p>
  
  <blockquote>
    <p>Cannot reduce the DAE index and select states. This is most likely due
    to non-differentiable functions.</p>
    
    <p>Cannot reduce the DAE index and select states. This is most likely due
    to non-differentiable functions.</p>
    
    <p>Failed to reduce the DAE index.</p>
  </blockquote>
  
  <p>The model is shown in Fig. (in comment) and the code is as follows: </p>
  
  <pre><code>model HeatingSystem_modified "Simple model of a heatstorage system"
    extends Modelica.Icons.Example;
     replaceable package Medium =
        CSP.Components.Media.linearMoltenSalt
       constrainedby Modelica.Media.Interfaces.PartialMedium;
  
    Modelica.Fluid.Vessels.OpenTank tank(
      redeclare package Medium = Medium,
      height=2,
      level_start=1,
      massDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
      use_HeatTransfer=true,
      redeclare model HeatTransfer =
          Modelica.Fluid.Vessels.BaseClasses.HeatTransfer.IdealHeatTransfer (k=10),
      ports(each p(start=1e5)),
      T_start=Modelica.SIunits.Conversions.from_degC(20),
      portsData={Modelica.Fluid.Vessels.BaseClasses.VesselPortsData(diameter=0.01),
          Modelica.Fluid.Vessels.BaseClasses.VesselPortsData(diameter=0.01)},
      crossArea=1,
      nPorts=2) annotation (Placement(transformation(extent={{-40,64},{-20,84}},
            rotation=0)));
    Modelica.Fluid.Machines.ControlledPump pump(
      redeclare package Medium = Medium,
      N_nominal=1500,
      use_T_start=true,
      T_start=Modelica.SIunits.Conversions.from_degC(40),
      m_flow_start=0.01,
      m_flow_nominal=0.01,
      allowFlowReversal=false,
      use_m_flow_set=false,
      p_a_start=110000,
      p_b_start=130000,
      p_a_nominal=110000,
      p_b_nominal=130000,
      control_m_flow=true) annotation (Placement(transformation(extent={{20,50},{40,
              70}},     rotation=0)));
  public 
    Modelica.Thermal.HeatTransfer.Sources.FixedHeatFlow burner(
      alpha=-0.5,
      Q_flow=1.6e4,
      T_ref=343.15)
      annotation (Placement(transformation(extent={{-122,-10},{-102,10}},
                                                                     rotation=0)));
    inner Modelica.Fluid.System system(
        m_flow_small=1e-4, energyDynamics=Modelica.Fluid.Types.Dynamics.SteadyStateInitial)
                          annotation (Placement(transformation(extent={{80,80},{100,
              100}},      rotation=0)));
  
  protected 
    Modelica.Blocks.Interfaces.RealOutput T_forward
      annotation (Placement(transformation(extent={{114,-46},{126,-34}},
                                                                       rotation=
             0)));
  public 
    Modelica.Fluid.Sensors.Temperature sensor_T_cold(redeclare package Medium =
          Medium) annotation (Placement(transformation(extent={{92,-50},{112,-30}},
            rotation=0)));
    Modelica.Fluid.Sensors.Temperature sensor_T_hot(redeclare package Medium =
          Medium) annotation (Placement(transformation(extent={{-72,10},{-52,30}},
            rotation=0)));
  
    Modelica.Fluid.Vessels.OpenTank tank1(
      redeclare package Medium = Medium,
      height=2,
      level_start=1,
      massDynamics=Modelica.Fluid.Types.Dynamics.FixedInitial,
      use_HeatTransfer=true,
      redeclare model HeatTransfer =
          Modelica.Fluid.Vessels.BaseClasses.HeatTransfer.IdealHeatTransfer (k=10),
      ports(each p(start=1e5)),
      T_start=Modelica.SIunits.Conversions.from_degC(20),
      portsData={Modelica.Fluid.Vessels.BaseClasses.VesselPortsData(diameter=0.01),
          Modelica.Fluid.Vessels.BaseClasses.VesselPortsData(diameter=0.01)},
      crossArea=1,
      nPorts=2) annotation (Placement(transformation(extent={{20,-48},{40,-28}},
            rotation=0)));
    Modelica.Fluid.Machines.ControlledPump pump1(
      redeclare package Medium = Medium,
      N_nominal=1500,
      use_T_start=true,
      T_start=Modelica.SIunits.Conversions.from_degC(40),
      m_flow_start=0.01,
      allowFlowReversal=false,
      m_flow_nominal=0.1,
      control_m_flow=true,
      p_a_start=110000,
      p_b_start=130000,
      p_a_nominal=110000,
      p_b_nominal=130000,
      use_m_flow_set=true)
                          annotation (Placement(transformation(extent={{-20,-62},{
              -40,-42}},
                       rotation=0)));
    Modelica.Fluid.Pipes.DynamicPipe radiator(
      use_T_start=true,
      redeclare package Medium = Medium,
      length=10,
      T_start=Modelica.SIunits.Conversions.from_degC(40),
      redeclare model HeatTransfer =
          Modelica.Fluid.Pipes.BaseClasses.HeatTransfer.IdealFlowHeatTransfer,
      diameter=0.01,
      nNodes=1,
      redeclare model FlowModel =
          Modelica.Fluid.Pipes.BaseClasses.FlowModels.DetailedPipeFlow,
      use_HeatTransfer=true,
      modelStructure=Modelica.Fluid.Types.ModelStructure.a_v_b,
      p_a_start=110000) annotation (Placement(transformation(extent={{10,10},{-10,
              -10}},
            rotation=90,
          origin={76,0})));
    Modelica.Thermal.HeatTransfer.Sources.FixedTemperature T_ambient(T=system.T_ambient)
      annotation (Placement(transformation(extent={{-7,-7},{7,7}},     rotation=180,
          origin={127,0})));
    Modelica.Thermal.HeatTransfer.Components.ThermalConductor wall(G=1.6e3/20)
      annotation (Placement(transformation(
          origin={102,0},
          extent={{8,-10},{-8,10}},
          rotation=180)));
    Modelica.Fluid.Pipes.DynamicPipe heater(
      redeclare package Medium = Medium,
      use_T_start=true,
      T_start=Modelica.SIunits.Conversions.from_degC(80),
      length=2,
      redeclare model HeatTransfer =
          Modelica.Fluid.Pipes.BaseClasses.HeatTransfer.IdealFlowHeatTransfer,
      diameter=0.01,
      nNodes=1,
      redeclare model FlowModel =
          Modelica.Fluid.Pipes.BaseClasses.FlowModels.DetailedPipeFlow,
      use_HeatTransfer=true,
      modelStructure=Modelica.Fluid.Types.ModelStructure.a_v_b,
      p_a_start=130000) annotation (Placement(transformation(extent={{-10,-10},{10,
              10}},
            rotation=90,
          origin={-80,0})));
    Modelica.Blocks.Sources.RealExpression realExpression(y=90)
      annotation (Placement(transformation(extent={{-72,36},{-52,56}})));
    Modelica.Blocks.Math.Add add(k1=-1, k2=+1)
      annotation (Placement(transformation(extent={{-26,16},{-6,36}})));
    Modelica.Blocks.Continuous.PI PI(
      k=2,
      T=1,
      x_start=0,
      initType=Modelica.Blocks.Types.Init.NoInit)
      annotation (Placement(transformation(extent={{2,16},{22,36}})));
  equation 
  
    connect(radiator.heatPorts[1], wall.port_a) annotation (Line(points={{80.4,-0.1},
            {88,-0.1},{88,9.99201e-016},{94,9.99201e-016},{94,0}}, color={127,0,0}));
    connect(wall.port_b, T_ambient.port) annotation (Line(points={{110,-9.99201e-016},
            {116,-9.99201e-016},{116,0},{120,0}}, color={191,0,0}));
    connect(heater.heatPorts[1], burner.port) annotation (Line(points={{-84.4,0.1},
            {-94,0.1},{-94,0},{-102,0}}, color={127,0,0}));
    connect(pump.port_b, radiator.port_a)
      annotation (Line(points={{40,60},{76,60},{76,10}}, color={0,127,255}));
    connect(pump1.port_a, tank1.ports[1])
      annotation (Line(points={{-20,-52},{28,-52},{28,-48}}, color={0,127,255}));
    connect(pump1.port_b, heater.port_a) annotation (Line(points={{-40,-52},{-80,-52},
            {-80,-10}},           color={0,127,255}));
    connect(sensor_T_cold.T, T_forward)
      annotation (Line(points={{109,-40},{120,-40}}, color={0,0,127}));
    connect(sensor_T_hot.port, heater.port_b)
      annotation (Line(points={{-62,10},{-62,10},{-80,10}}, color={0,127,255}));
    connect(sensor_T_cold.port, radiator.port_b) annotation (Line(points={{102,-50},
            {82,-50},{82,-10},{76,-10}}, color={0,127,255}));
    connect(heater.port_b, tank.ports[1]) annotation (Line(points={{-80,10},{-80,10},
            {-80,56},{-80,60},{-32,60},{-32,64}}, color={0,127,255}));
    connect(pump.port_a, tank.ports[2]) annotation (Line(points={{20,60},{-6,60},{
            -28,60},{-28,64}}, color={0,127,255}));
    connect(add.y, PI.u)
      annotation (Line(points={{-5,26},{-5,26},{0,26}}, color={0,0,127}));
    connect(PI.y, pump1.m_flow_set) annotation (Line(points={{23,26},{30,26},{30,-20},
            {-25,-20},{-25,-43.8}}, color={0,0,127}));
    connect(sensor_T_hot.T, add.u2)
      annotation (Line(points={{-55,20},{-28,20}}, color={0,0,127}));
    connect(realExpression.y, add.u1) annotation (Line(points={{-51,46},{-46,46},{
            -46,32},{-28,32}}, color={0,0,127}));
    connect(radiator.port_b, tank1.ports[2]) annotation (Line(points={{76,-10},{76,
            -10},{76,-34},{76,-50},{76,-52},{32,-52},{32,-48}}, color={0,127,255}));
    annotation (                             Documentation(info="&lt;html&gt;
  &lt;p&gt;
  Simple heating system with a closed flow cycle.
  After 2000s of simulation time the valve fully opens. A simple idealized control is embedded
  into the respective components, so that the heating system can be regulated with the valve:
  the pump controls the pressure, the burner controls the temperature.
  &lt;/p&gt;
  &lt;p&gt;
  One can investigate the temperatures and flows for different settings of &lt;code&gt;system.energyDynamics&lt;/code&gt;
  (see Assumptions tab of the system object).&lt;/p&gt;
  &lt;ul&gt;
  &lt;li&gt;With &lt;code&gt;system.energyDynamics==Types.Dynamics.FixedInitial&lt;/code&gt; the states need to find their steady values during the simulation.&lt;/li&gt;
  &lt;li&gt;With &lt;code&gt;system.energyDynamics==Types.Dynamics.SteadyStateInitial&lt;/code&gt; (default setting) the simulation starts in steady-state.&lt;/li&gt;
  &lt;li&gt;With &lt;code&gt;system.energyDynamics==Types.Dynamics.SteadyState&lt;/code&gt; all but one dynamic states are eliminated.
      The left state &lt;code&gt;tank.m&lt;/code&gt; is to account for the closed flow cycle. It is constant as outflow and inflow are equal
      in a steady-state simulation.&lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;
  Note that a closed flow cycle generally causes circular equalities for the mass flow rates and leaves the pressure undefined.
  This is why the tank.massDynamics, i.e., the tank level determining the port pressure, is modified locally to Types.Dynamics.FixedInitial.
  &lt;/p&gt;
  &lt;p&gt;
  Also note that the tank is thermally isolated against its ambient. This way the temperature of the tank is also
  well defined for zero flow rate in the heating system, e.g., for valveOpening.offset=0 at the beginning of a simulation.
  The pipe however is assumed to be perfectly isolated.
  If steady-state values shall be obtained with the valve fully closed, then a thermal
  coupling between the pipe and its ambient should be defined as well.
  &lt;/p&gt;
  &lt;p&gt;
  Moreover it is worth noting that the idealized direct connection between the heater and the pipe, resulting in equal port pressures,
  is treated as high-index DAE, as opposed to a nonlinear equation system for connected pressure loss correlations. A pressure loss correlation
  could be additionally introduced to model the fitting between the heater and the pipe, e.g., to adapt different diameters.
  &lt;/p&gt;
  
  &lt;img src=\"modelica://Modelica/Resources/Images/Fluid/Examples/HeatingSystem.png\" border=\"1\"
       alt=\"HeatingSystem.png\"&gt;
  &lt;/html&gt;"), experiment(StopTime=6000),
      __Dymola_Commands(file(ensureSimulated=true)=
          "modelica://Modelica/Resources/Scripts/Dymola/Fluid/HeatingSystem/plotResults.mos" 
          "plotResults"));
  end HeatingSystem_modified;
  </code></pre>