diff --git a/idaes/core/util/model_diagnostics.py b/idaes/core/util/model_diagnostics.py index f298681de1..ad7166d866 100644 --- a/idaes/core/util/model_diagnostics.py +++ b/idaes/core/util/model_diagnostics.py @@ -1623,7 +1623,7 @@ def display_constraints_including_variable(self, variable, stream=None): # Get index of variable in Jacobian try: var_idx = self.nlp.get_primal_indices([variable])[0] - except (ValueError, PyomoException): + except (KeyError, PyomoException): raise AttributeError(f"Could not find {variable.name} in model.") nonzeros = self.jacobian.getcol(var_idx).nonzero() @@ -1670,7 +1670,7 @@ def display_variables_in_constraint(self, constraint, stream=None): # Get index of variable in Jacobian try: con_idx = self.nlp.get_constraint_indices([constraint])[0] - except ValueError: + except KeyError: raise AttributeError(f"Could not find {constraint.name} in model.") nonzeros = self.jacobian[con_idx, :].nonzero() @@ -1678,7 +1678,9 @@ def display_variables_in_constraint(self, constraint, stream=None): # Build a list of all vars in constraint vars_in_cons = [] for c in nonzeros[1]: - vars_in_cons.append(f"{self.var_list[c].name}: {self.jacobian[(con_idx, c)]:.3e}") + vars_in_cons.append( + f"{self.var_list[c].name}: {self.jacobian[(con_idx, c)]:.3e}" + ) # Write the output _write_report_section( diff --git a/idaes/core/util/tests/test_model_diagnostics.py b/idaes/core/util/tests/test_model_diagnostics.py index fe59206ad9..d6952e93d6 100644 --- a/idaes/core/util/tests/test_model_diagnostics.py +++ b/idaes/core/util/tests/test_model_diagnostics.py @@ -1588,8 +1588,8 @@ def test_display_constraints_including_variable(self): expected = """==================================================================================== The following constraints involve v[1]: - c1: 1.0 - c4: 8.0 + c1: 1.000e+00 + c4: 8.000e+00 ==================================================================================== """ @@ -1653,8 +1653,8 @@ def test_display_variables_in_constraint(self): expected = """==================================================================================== The following variables are involved in c1: - v[1]: 1.0 - v[2]: 2.0 + v[1]: 1.000e+00 + v[2]: 2.000e+00 ==================================================================================== """