Mathadon / modelica_MPC

MPC-related models for building optimal control
0 stars 2 forks source link

ConstantEffectiveness #8

Open dhblum opened 6 years ago

dhblum commented 6 years ago

Use of IBPSA.Fluid.HeatExchangers.ConstantEffectiveness in JModelica optimization results in the following error:

Java error occurred: 
Exception in thread "main" java.lang.UnsupportedOperationException: If-Statements are unsupported (try to rewrite into if-expressions): if x < 0 then
 y := - y;
end if
    at org.jmodelica.optimica.compiler.FIfStmt.updateExpressionsAccordingToStatement(Unknown Source)
    at org.jmodelica.optimica.compiler.FFunctionDecl.generateFinalExpressionVector(Unknown Source)
    at org.jmodelica.optimica.compiler.FFunctionDecl.toMXFunction_compute(Unknown Source)
    at org.jmodelica.optimica.compiler.FFunctionDecl.toMXFunction(Unknown Source)
    at org.jmodelica.optimica.compiler.FFunctionCall.toMXVector(Unknown Source)
    at org.jmodelica.optimica.compiler.FFunctionCall.toMX(Unknown Source)
    at org.jmodelica.optimica.compiler.FIfExp.toMX(Unknown Source)
    at org.jmodelica.optimica.compiler.FIfExp.toMX(Unknown Source)
    at org.jmodelica.optimica.compiler.FNoEventExp.toMX(Unknown Source)
    at org.jmodelica.optimica.compiler.FEquation.toMXForRhs(Unknown Source)
dhblum commented 6 years ago

The error can be traced to the function IBPSA.Utilities.Math.Functions.BaseClasses.smoothTransition where:

 if x < 0 then
    y := -y;
 end if;

This function is ultimately called because IBPSA.Fluid.HeatExchangers.BaseClasses.PartialEffectiveness.prescribedHeatFlowRate = true, and therefore IBPSA.Fluid.Interfaces.StaticTwoPortConservationEquation.use_m_flowInv = true.

Editing to the following relieves the error:

y := if x >= 0 then a + aX*(b + aX*(c + aX*(d + aX*(e + aX*f)))) else 
 -(a + aX*(b + aX*(c + aX*(d + aX*(e + aX*f)))));
dhblum commented 6 years ago

New error is:

RuntimeError: BUG: Unable to evaluate value of mpc_model.hex.cp1_default.

where mpc_model.hex is the instance of the heat exchanger.

The error is given if using either BuildingMpc.Media.DryAir or Buildings.Media.Air.

Editing the parameter declarations of Fluid.HeatExchangers.BaseClasses.PartialEffectiveness to the following as well as removing the initial equation section relieves the error:

  parameter Modelica.SIunits.SpecificHeatCapacity cp1_default = Medium1.specificHeatCapacityCp(Medium1.setState_pTX(
    Medium1.p_default,
    Medium1.T_default,
    Medium1.X_default))
    "Specific heat capacity of medium 1 at default medium state";
  parameter Modelica.SIunits.SpecificHeatCapacity cp2_default = Medium2.specificHeatCapacityCp(Medium2.setState_pTX(
    Medium2.p_default,
    Medium2.T_default,
    Medium2.X_default))
    "Specific heat capacity of medium 2 at default medium state";
  parameter Modelica.SIunits.ThermalConductance CMin_flow_small = min(m1_flow_small*cp1_default, m2_flow_small*cp2_default)
    "Small value for smoothing of minimum heat capacity flow rate";
Mathadon commented 6 years ago

I knew this topic would come in handy: https://github.com/Mathadon/modelica_MPC/issues/2 your first issue is caused by the fact that JModelica does not allow if\else expressions in the main body of the equations. Although, in fact, I thought that your fix would not work either..

The second problem is probably caused because the optimisation requires values that are only computed during an initial equation (e.g. at initialization). However for optimisation problems, the code is never initialised since an FMU is not even being created. Your fix therefore makes sense. However, it would be better if JModelica would simply support such constructs.