modelica / ModelicaStandardLibrary

Free (standard conforming) library to model mechanical (1D/3D), electrical (analog, digital, machines), magnetic, thermal, fluid, control systems and hierarchical state machines. Also numerical functions and functions for strings, files and streams are included.
https://doc.modelica.org
BSD 3-Clause "New" or "Revised" License
452 stars 165 forks source link

Improving assert messages #4399

Open HansOlsson opened 2 months ago

HansOlsson commented 2 months ago

Consider the following model:

model ThreeTanksMod
  extends Modelica.Fluid.Examples.Tanks.ThreeTanks(tank1(T_start=272.151));
  annotation (uses(Modelica(version="4.0.0")));
end ThreeTanksMod;

The problem is that tank1.T slightly drops (about 2.5 mK) during the first 100 s, and thus it triggers the following assert:

  assert(tank1.medium.T >= 272.15 and tank1.medium.T <= 403.15, "
Temperature T (= "  +       String(tank1.medium.T)+" K) is not in the allowed range (
"+    "272.15"+" K <= T <= "+"403.15"+" K) required from medium model \""+   "SimpleLiquidWater"+"\".");

Generating:

Temperature T (= 272.15 K) is not in the allowed range (272.15 K <= T <= 403.15 K) required from medium model "SimpleLiquidWater".

Failed condition: tank1.medium.T >= 272.15 and tank1.medium.T <= 403.15

It is not clear from the error message that the temperate is just outside the range. Note that the first part is a string that is generated according to the Modelica specification.

It could be improved as follows:

Note that there are may be many such messages so we need a consistent update. And it is likely that most cases of hitting the boundaries will generate such messages, as continuous variables are likely to slowly drift over the boundaries.