JAGUN KOO
JAGUN KOO

Reputation: 135

How to use Multiple And operator in the if statement in modelica?

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?

Upvotes: 0

Views: 443

Answers (1)

Hans Olsson
Hans Olsson

Reputation: 12517

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.)

Upvotes: 7

Related Questions