Skip to content

Commit

Permalink
run Black
Browse files Browse the repository at this point in the history
  • Loading branch information
dallan-keylogic committed Jan 31, 2025
1 parent 8e26416 commit 7c73af5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
22 changes: 12 additions & 10 deletions idaes/core/util/model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ def display_variables_with_none_value(self, stream=None):

def display_variables_with_none_value_in_activated_constraints(self, stream=None):
"""
Prints a list of variables with values of None that are present in the
mathematical program generated to solve the model. This list includes only
Prints a list of variables with values of None that are present in the
mathematical program generated to solve the model. This list includes only
variables in active constraints that are reachable through active blocks.
Args:
Expand All @@ -652,7 +652,9 @@ def display_variables_with_none_value_in_activated_constraints(self, stream=None
stream=stream,
lines_list=[
f"{v.name}"
for v in variables_with_none_value_in_activated_equalities_set(self._model)
for v in variables_with_none_value_in_activated_equalities_set(
self._model
)
],
title=f"The following variable(s) have a value of None:",

Check warning on line 659 in idaes/core/util/model_diagnostics.py

View workflow job for this annotation

GitHub Actions / Pylint

W1309 (f-string-without-interpolation)

Using an f-string that does not have any interpolated variables
header="=",
Expand All @@ -661,12 +663,14 @@ def display_variables_with_none_value_in_activated_constraints(self, stream=None

def _verify_active_variables_initialized(self, stream=None):
"""
Validate that all variables are initialized (i.e., have values set to
something other than None) before doing further numerical analysis.
Stream argument provided for forward compatibility (in case we want
Validate that all variables are initialized (i.e., have values set to
something other than None) before doing further numerical analysis.
Stream argument provided for forward compatibility (in case we want
to print a list or something).
"""
n_uninit = len(variables_with_none_value_in_activated_equalities_set(self._model))
n_uninit = len(
variables_with_none_value_in_activated_equalities_set(self._model)
)
if n_uninit > 0:
raise RuntimeError(
f"Found {n_uninit} variables with a value of None in the mathematical "
Expand Down Expand Up @@ -973,8 +977,6 @@ def display_overconstrained_set(self, stream=None):

stream.write("=" * MAX_STR_LENGTH + "\n")



def display_variables_with_extreme_jacobians(self, stream=None):
"""
Prints the variables associated with columns in the Jacobian with extreme
Expand Down Expand Up @@ -1385,7 +1387,7 @@ def display_constraints_with_no_free_variables(self, stream=None):
"""
# Although, in principle, this method doesn't require
# all variables to be initialized, its current
# all variables to be initialized, its current
# implementation does.
self._verify_active_variables_initialized(stream=stream)

Expand Down
11 changes: 7 additions & 4 deletions idaes/core/util/model_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1729,16 +1729,17 @@ def number_active_variables_in_deactivated_blocks(block):
"""
return len(active_variables_in_deactivated_blocks_set(block))


def variables_with_none_value_in_activated_equalities_set(block):
"""
Method to return a ComponentSet of all Var components which
Method to return a ComponentSet of all Var components which
have a value of None in the set of activated constraints.
Args:
block : model to be studied
Returns:
A ComponentSet including all Var components which
A ComponentSet including all Var components which
have a value of None in the set of activated constraints.
"""
var_set = ComponentSet()
Expand All @@ -1747,21 +1748,23 @@ def variables_with_none_value_in_activated_equalities_set(block):
var_set.add(v)
return var_set


def number_variables_with_none_value_in_activated_equalities(block):
"""
Method to return the number of Var components which
Method to return the number of Var components which
have a value of None in the set of activated constraints.
Args:
block : model to be studied
Returns:
Number of Var components which
Number of Var components which
have a value of None in the set of activated constraints.
"""

return len(variables_with_none_value_in_activated_equalities_set(block))


# -------------------------------------------------------------------------
# Reporting methods
def report_statistics(block, ostream=None):
Expand Down
32 changes: 17 additions & 15 deletions idaes/core/util/tests/test_model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ def test_vars_near_zero(model):
for i in near_zero_vars:
assert i is model.v1


class TestVariablesWithNoneValue:
def err_msg(self, j):
return (
return (
f"Found {j} variables with a value of None in the mathematical "
"program generated by the model. They must be initialized with non-None "
"values before numerical analysis can proceed. Run "
Expand All @@ -203,23 +204,26 @@ def model(self):
m.y = Var(range(3), initialize=[1, None, 2])
m.z = Var()

m.con_w = Constraint(expr= m.w == 0)
m.con_x = Constraint(expr= m.x == 1)
m.con_w = Constraint(expr=m.w == 0)
m.con_x = Constraint(expr=m.x == 1)

def rule_con_y(b, i):
return b.y[i] == 3

m.con_y = Constraint(range(3), rule=rule_con_y)
m.con_z = Constraint(expr= m.x + m.z == -1)
m.con_z = Constraint(expr=m.x + m.z == -1)

m.block1 = Block()
m.block1.a = Var()
m.block1.b = Var(initialize=7)
m.block1.c = Var(initialize=-5)
m.block1.d = Var()

m.block1.con_a = Constraint(expr= m.block1.a**2 + m.block1.b == 1)
m.block1.con_b = Constraint(expr= m.block1.b + m.block1.c == 3)
m.block1.con_c = Constraint(expr= m.block1.c + m.block1.a == -4)
m.block1.con_a = Constraint(expr=m.block1.a**2 + m.block1.b == 1)
m.block1.con_b = Constraint(expr=m.block1.b + m.block1.c == 3)
m.block1.con_c = Constraint(expr=m.block1.c + m.block1.a == -4)
return m

@pytest.mark.unit
def test_verify_active_variables_initialized(self, model):
diag_tbx = DiagnosticsToolbox(model)
Expand All @@ -228,7 +232,7 @@ def test_verify_active_variables_initialized(self, model):
match=self.err_msg(3),
):
diag_tbx._verify_active_variables_initialized()

model.block1.deactivate()
with pytest.raises(
RuntimeError,
Expand Down Expand Up @@ -267,26 +271,23 @@ def test_error_handling(self, model):
@pytest.mark.unit
def test_display_problematic_constraint_terms(self, model):
"""
This method needs to be split in a separate function
This method needs to be split in a separate function
because it takes a constraint as an argument.
"""
diag_tbx = DiagnosticsToolbox(model)
with pytest.raises(
RuntimeError,
match=self.err_msg(3),
):
diag_tbx.display_problematic_constraint_terms(
model.con_y[1]
)
diag_tbx.display_problematic_constraint_terms(model.con_y[1])
# Right now the error message should show whether or not
# a problematic constraint is called.
with pytest.raises(
RuntimeError,
match=self.err_msg(3),
):
diag_tbx.display_problematic_constraint_terms(
model.con_y[0]
)
diag_tbx.display_problematic_constraint_terms(model.con_y[0])

@pytest.mark.unit
def test_display_variables_with_none_value_in_activated_constraints(self, model):
diag_tbx = DiagnosticsToolbox(model)
Expand All @@ -307,6 +308,7 @@ def test_display_variables_with_none_value_in_activated_constraints(self, model)
"""
assert stream.getvalue() == expected


@pytest.mark.unit
def test_vars_with_none_value(model):
none_value = _vars_with_none_value(model)
Expand Down
28 changes: 19 additions & 9 deletions idaes/core/util/tests/test_model_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ def test_number_derivative_variables():

assert number_derivative_variables(m) == 0


@pytest.mark.unit
def test_uninitialized_variables_in_activated_constraints():
m = ConcreteModel()
Expand All @@ -638,42 +639,51 @@ def test_uninitialized_variables_in_activated_constraints():
m.y = Var(range(3), initialize=[1, None, 2])
m.z = Var()

m.con_w = Constraint(expr= m.w == 0)
m.con_x = Constraint(expr= m.x == 1)
m.con_w = Constraint(expr=m.w == 0)
m.con_x = Constraint(expr=m.x == 1)

def rule_con_y(b, i):
return b.y[i] == 3

m.con_y = Constraint(range(3), rule=rule_con_y)
m.con_z = Constraint(expr= m.x + m.z == -1)
m.con_z = Constraint(expr=m.x + m.z == -1)

m.block1 = Block()
m.block1.a = Var()
m.block1.b = Var(initialize=7)
m.block1.c = Var(initialize=-5)
m.block1.d = Var()

m.block1.con_a = Constraint(expr= m.block1.a**2 + m.block1.b == 1)
m.block1.con_b = Constraint(expr= m.block1.b + m.block1.c == 3)
m.block1.con_c = Constraint(expr= m.block1.c + m.block1.a == -4)
m.block1.con_a = Constraint(expr=m.block1.a**2 + m.block1.b == 1)
m.block1.con_b = Constraint(expr=m.block1.b + m.block1.c == 3)
m.block1.con_c = Constraint(expr=m.block1.c + m.block1.a == -4)

active_uninit_set = ComponentSet([m.y[1], m.z, m.block1.a])

assert variables_with_none_value_in_activated_equalities_set(m) == active_uninit_set
assert number_variables_with_none_value_in_activated_equalities(m) == len(active_uninit_set)
assert number_variables_with_none_value_in_activated_equalities(m) == len(
active_uninit_set
)

m.block1.deactivate()

active_uninit_set = ComponentSet([m.y[1], m.z])

assert variables_with_none_value_in_activated_equalities_set(m) == active_uninit_set
assert number_variables_with_none_value_in_activated_equalities(m) == len(active_uninit_set)
assert number_variables_with_none_value_in_activated_equalities(m) == len(
active_uninit_set
)

m.block1.activate()
m.con_z.deactivate()

active_uninit_set = ComponentSet([m.y[1], m.block1.a])

assert variables_with_none_value_in_activated_equalities_set(m) == active_uninit_set
assert number_variables_with_none_value_in_activated_equalities(m) == len(active_uninit_set)
assert number_variables_with_none_value_in_activated_equalities(m) == len(
active_uninit_set
)


# -------------------------------------------------------------------------
# Objective methods
Expand Down

0 comments on commit 7c73af5

Please sign in to comment.