ibpsa / modelica-ibpsa

Modelica library for building and district energy systems developed within IBPSA Project 1
https://ibpsa.github.io/project1
142 stars 84 forks source link

water index computed using initial algorithm #759

Closed Mathadon closed 7 years ago

Mathadon commented 7 years ago

In IBPSA.Utilities.Psychrometrics.TWetBul_TDryBulXi the Medium water index is computed using

initial algorithm
   iWat:=-1;
     for i in 1:Medium.nX loop
       if Modelica.Utilities.Strings.isEqual(string1=Medium.substanceNames[i],
                                             string2="Water", caseSensitive=false) then
         iWat :=i;
       end if;
     end for;

this is causing problems when doing optimisations since the water medium index is unknown at translation time.

This can be resolved using

parameter Integer iWat = sum( { (if Modelica.Utilities.Strings.isEqual(string1=Medium.substanceNames[i],string2="Water", caseSensitive=false) then i else 0) for i in 1:Medium.nX})

but that in turn leads to an error when checking the model:

Index 1 of Xi[iWat] is 0, which is outside of range 1 to 0.

This can be resolved by setting as default medium IBPSA.Media.Airinstead of Modelica.Media.Interfaces.PartialCondensingGases.

I propose to make these changes.

thorade commented 7 years ago

might be related: #675 and #689

mwetter commented 7 years ago

@Mathadon Would

parameter Integer iWat(min=1, start=1) = ...

help?

Mathadon commented 7 years ago

That works too! But it's not really a proper solution, is it?

Mathadon commented 7 years ago

But then dymola complains:

Index 1 of Xi[iWat] is 1, which is outside of range 1 to 0.

and, more surprisingly:

The parameter wetBul.iWat has the attribute fixed = false
indicating its value to be calculated from initial conditions.
Error: However, it is used to specify a structural property and
it must be possible to evaluate it at translation.

somehow dymola (and apparently also JModelica) does accept this without the start and min attributes.

mwetter commented 7 years ago

If this causes issues we can't work around, I would be fine with changing the protected parameter to a public constant of the from

  constant Integer iWat=1 "Index of water in medium composition vector"
    annotation (HideResult=true);

and then add an assert to make sure that water is at the right index.

However, you still have the same problem with IBPSA.Fluid.Sensors.TraceSubstances where such a fix won't work.

Mathadon commented 7 years ago

That's an idea too.

What is wrong with the solution that I proposed, though?

mwetter commented 7 years ago

What exactly is your solution? You wrote above "dymola complains"... and "somehow dymola (and apparently also JModelica) does accept this " of which I don't know what "this" is.

If you have a working solution that satisfies the tools, then that's fine and please post a pull request or commit to review.

Mathadon commented 7 years ago

In the initial post I suggested

parameter Integer iWat = sum( { (if Modelica.Utilities.Strings.isEqual(string1=Medium.substanceNames[i],string2="Water", caseSensitive=false) then i else 0) for i in 1:Medium.nX})

I'll make a pull request.