IDAES/idaes-pse

SVD Toolbox display_constraints_including_variable error

Closed this issue · 0 comments

When using display_constraints_including_variable, I got an error:

tbx.display_constraints_including_variable(m.fs.stripper_section.reflux_mixer.rich_solvent_state[0.0].temperature)
Traceback (most recent call last):
  File "c:\users\dallan\repos\idaes-pse\idaes\core\util\model_diagnostics.py", line 1553, in display_constraints_including_variable
    var_idx = var_list.index(variable)
  File "c:\users\dallan\repos\pyomo\pyomo\core\expr\relational_expr.py", line 330, in __bool__
    return super().__bool__()
  File "c:\users\dallan\repos\pyomo\pyomo\core\expr\relational_expr.py", line 47, in __bool__
    raise PyomoException(
pyomo.common.errors.PyomoException: Cannot convert non-constant Pyomo expression (fs.stripper_section.reflux_mixer.rich_solvent_state[0.0].temperature  ==  fs.absorber_section.makeup_mixer.mixed_state[0.0].mole_frac_comp[MEA]) to bool.
This error is usually caused by using a Var, unit, or mutable Param in a
Boolean context such as an "if" statement, or when checking container
membership or equality. For example,
    >>> m.x = Var()
    >>> if m.x >= 1:
    ...     pass
and
    >>> m.y = Var()
    >>> if m.y in [m.x, m.y]:
    ...     pass
would both cause this exception.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\users\dallan\repos\idaes-pse\idaes\core\util\model_diagnostics.py", line 1555, in display_constraints_including_variable
    raise AttributeError(f"Could not find {variable.name} in model.")
AttributeError: Could not find fs.stripper_section.reflux_mixer.rich_solvent_state[0.0].temperature in model.

That variable is in fact in the model. I'm pretty sure that this is the result of using list().index in order to perform the variable lookup. I think that method uses == internally, which leads to the Pyomo error. I'm not sure why it failed this time while it succeeded previously in the same debugging session. You can perform the same lookup by using nlp.get_primal_indices (beware, it takes a list of constraints), or nlp.get_constraint_indices for constraints.

I'll probably end up writing the fix myself.