modelicadymola

how to write derivative of two variables in a Modelica function?


I started coding in modelica recently and I am writing a modelica function. In one of the equation in the alogrithm, I need to write an equation in which the left hand side is taking derivate of two variables but dymola gives me error "Unexpected assignment left-hand side der(rho_c*h_c). left hand side of assignment must be indexed component references." I am providing the modelica function code as

function SuctionSide
  input Modelica.Units.SI.Temperature T_c;
  input Modelica.Units.SI.Temperature T_env;
  input Modelica.Units.SI.Pressure P_suc;
  input Modelica.Units.SI.Volume V_f;
  input Modelica.Units.SI.MassFlowRate mdot_suc;
  input Modelica.Units.SI.MassFlowRate mdot_2;
  input Modelica.Units.SI.Mass m_oil; 
  input Modelica.Units.SI.Mass m_wall; 
  input Modelica.Units.SI.Density rho_c;
  input Modelica.Units.SI.SpecificEnthalpy h_c;
  input Modelica.Units.SI.SpecificEnthalpy h_2;
  input Modelica.Units.SI.CoefficientOfHeatTransfer h_rc;
  input Modelica.Units.SI.CoefficientOfHeatTransfer h_env;
  input Modelica.Units.SI.HeatFlowRate Qdot_ch; //crankcase heat
  input Modelica.Units.SI.Area A_c;
  input Modelica.Units.SI.Area A_env;
  input Modelica.Units.SI.SpecificHeatCapacityAtConstantPressure Cp_oil;
  input Modelica.Units.SI.SpecificHeatCapacityAtConstantPressure Cp_wall;
  input Modelica.Units.SI.Power Wdot_c;
  input Modelica.Units.SI.Power Wdot_th;
  input Modelica.Units.SI.HeatFlowRate Qdot_discharge;
  output Modelica.Units.SI.HeatFlowRate Qdot_c;
  output Modelica.Units.SI.HeatFlowRate Qdot_m;
  output Modelica.Units.SI.HeatFlowRate Qdot_env;
  output Modelica.Units.SI.Temperature T_wall;
algorithm 
  der(rho_c) := (mdot_suc -mdot_2)/V_f;
  der(rho_c * h_c):= ((mdot_suc*h_c) - (mdot_2*h_2) + Qdot_c+Qdot_ch + (der(P_suc)*V_f))/V_f;
  Qdot_c:= h_rc * A_c * (T_wall - T_c);
  der(T_wall) := (Qdot_m + Qdot_discharge -Qdot_c -Qdot_env)/(m_oil * Cp_oil + m_wall *   Cp_wall);
  Qdot_m := Wdot_c + mdot_2 * Wdot_th;
  Qdot_env := A_env * h_env*( T_wall - T_env);
  end SuctionSide;

whereas the errors are shown here as,

I tried to replace the * with comma to (make it like der(a,b) ) see if it is resolved but was not successful. I would be grateful, if you help me correct this function.


Solution

  • I think you, as many beginners, need to come out of the procedural thinking since Modelica is mainly a declarative language. In the algorithm part you write procedural code, as you do, but here you cannot use the language concept "der()". The der() concept belongs to an equation-section, not an algorithm-section.

    So to correct your code you just replace der() with a descriptive variable names as follows

    der_rho_c := (mdot_suc - mdot_d)/V_f;
    

    For better understanding you could look up the concept of der() in a Modelica text book, for instance https://mbe.modelica.university.