0

I'm coding the LMTD method in the heat transfer and met some errors when using the and operators more than two times simultaneously.

if (del_T1 > beta) and (del_T2 > beta) and (del_T1<>del_T2) then
    T_LMTD = (del_T1-del_T2) / ( (log(del_T1)) - (log(del_T2)));
  elseif (del_T1 > beta) and (del_T2 > beta) and (del_T1==del_T2) then
    T_LMTD = (del_T1-del_T2) / 2;
  elseif (del_T1 > beta) and (del_T2 < beta) then
    T_LMTD = (del_T1 - beta) / ( (log(del_T1/beta)) * (1 - zeta * (del_T2 - beta)));
  elseif (del_T1 < beta) and (del_T2 > beta) then
    T_LMTD = (del_T2 - beta) / ( (log(del_T2/beta)) * (1 - zeta * (del_T1 - beta)));
  elseif (del_T1 < beta) and (del_T2 < beta) then
    T_LMTD = beta / ( (1 - zeta * (del_T1 - beta))  * (1 - zeta * (del_T2 - beta)));
  else
    T_LMTD = beta / ( (1 - zeta * (del_T1 - beta))  * (1 - zeta * (del_T2 - beta)));
  end if;

Unfortunately, the error messages are printed like below when i simulate the model only when i used the And operators more than two times in one line.

enter image description here

How can i use And operator in multiple times? or Because of other problems?

JAGUN KOO
  • 135
  • 5
  • 1
    'And' can of course be used multiple times. But with this code it's hard to help. Can you provide a full working minimal example? – marco Feb 25 '20 at 08:13

1 Answers1

6

The problem isn't the and-operator but the equality-operators, del_T1<>del_T2 and del_T1==del_T2.

If you had removed the and-parts they would have generated the error message "Non-real equation in continuous time are not legal:".

And if you managed to generate code it would have given the more accurate error message: Variables of type Real cannot be compared for equality.

Which is stated in section 3.5 of the Modelica specification, https://specification.modelica.org/v3.4/Ch3.html#equality-relational-and-logical-operators

A work-around for that is to replace it by "close enough"; e.g. replace del_T1==del_T2 by abs(del_T1-del_T2)<tol, for some tolerance tol.

That gives a working example as:

  Real T_LMTD;
  input Real del_T1;
  input Real del_T2;
  input Real zeta;
  parameter Real beta=0.7;
  parameter Real tol=1e-6;
equation 
 if (del_T1 > beta) and (del_T2 > beta) and abs(del_T1-del_T2)>tol then
    T_LMTD = (del_T1-del_T2) / ( (log(del_T1)) - (log(del_T2)));
  elseif (del_T1 > beta) and (del_T2 > beta) and abs(del_T1-del_T2)<tol then
    T_LMTD = (del_T1-del_T2) / 2;
  elseif (del_T1 > beta) and (del_T2 < beta) then
    T_LMTD = (del_T1 - beta) / ( (log(del_T1/beta)) * (1 - zeta * (del_T2 - beta)));
  elseif (del_T1 < beta) and (del_T2 > beta) then
    T_LMTD = (del_T2 - beta) / ( (log(del_T2/beta)) * (1 - zeta * (del_T1 - beta)));
  elseif (del_T1 < beta) and (del_T2 < beta) then
    T_LMTD = beta / ( (1 - zeta * (del_T1 - beta))  * (1 - zeta * (del_T2 - beta)));
  else
    T_LMTD = beta / ( (1 - zeta * (del_T1 - beta))  * (1 - zeta * (del_T2 - beta)));
  end if;

(Obviously the variable declarations can be changed.)

Hans Olsson
  • 11,123
  • 15
  • 38