Skip to content

Commit

Permalink
Add support for Greybox model DOF counitng in degrees_of_freedom func…
Browse files Browse the repository at this point in the history
…tion (#1512)

* first commit

* typo fixes

* Update model_statistics.py

* updates to tests

* moved where we count greybox var

* add additonal functions to get grayboxblocks

* initial addi tion of grayboxs stats to dignostic tools

* Update test_model_statistics.py

* udpate to diagonotsics

* Update test_model_diagnostics.py

consistency fix

* Update model_diagnostics.py

* addressign comments #1

* added errors for greybox

* add exception tests

* Update model_statistics.py

* Update idaes/core/util/model_statistics.py

Co-authored-by: Adam Atia <[email protected]>

---------

Co-authored-by: Adam Atia <[email protected]>
Co-authored-by: Keith Beattie <[email protected]>
  • Loading branch information
3 people authored Dec 26, 2024
1 parent f92c750 commit 2afe182
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 24 deletions.
52 changes: 45 additions & 7 deletions idaes/core/util/model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
deactivated_objectives_set,
variables_in_activated_constraints_set,
variables_not_in_activated_constraints_set,
number_activated_greybox_equalities,
number_deactivated_greybox_equalities,
activated_greybox_block_set,
deactivated_greybox_block_set,
greybox_block_set,
unfixed_greybox_variables,
greybox_variables,
degrees_of_freedom,
large_residuals_set,
variables_near_bounds_set,
Expand Down Expand Up @@ -486,7 +493,10 @@ def __init__(self, model: BlockData, **kwargs):
"model argument must be an instance of a Pyomo BlockData object "
"(either a scalar Block or an element of an indexed Block)."
)

if len(greybox_block_set(model)) != 0:
raise NotImplementedError(
"Model contains Greybox models, which are not supported by Diagnostics toolbox at the moment"
)
self._model = model
self.config = CONFIG(kwargs)

Expand Down Expand Up @@ -1746,7 +1756,12 @@ def report_structural_issues(self, stream=None):

# Potential evaluation errors
# TODO: High Index?
if len(greybox_block_set(self._model)) != 0:
raise NotImplementedError(
"Model contains Greybox models, which are not supported by Diagnostics toolbox at the moment"
)
stats = _collect_model_statistics(self._model)

warnings, next_steps = self._collect_structural_warnings()
cautions = self._collect_structural_cautions()

Expand Down Expand Up @@ -1790,7 +1805,6 @@ def report_numerical_issues(self, stream=None):
"""
if stream is None:
stream = sys.stdout

jac, nlp = get_jacobian(self._model, scaled=False)

warnings, next_steps = self._collect_numerical_warnings(jac=jac, nlp=nlp)
Expand Down Expand Up @@ -1935,7 +1949,10 @@ def __init__(self, model: BlockData, **kwargs):
"model argument must be an instance of a Pyomo BlockData object "
"(either a scalar Block or an element of an indexed Block)."
)

if len(greybox_block_set(model)) != 0:
raise NotImplementedError(
"Model contains Greybox models, which are not supported by Diagnostics toolbox at the moment"
)
self._model = model
self.config = SVDCONFIG(kwargs)

Expand Down Expand Up @@ -2377,7 +2394,10 @@ def __init__(self, model, **kwargs):
"model argument must be an instance of a Pyomo BlockData object "
"(either a scalar Block or an element of an indexed Block)."
)

if len(greybox_block_set(model)) != 0:
raise NotImplementedError(
"Model contains Greybox models, which are not supported by Diagnostics toolbox at the moment"
)
self._model = model
self.config = DHCONFIG(kwargs)

Expand Down Expand Up @@ -3475,7 +3495,10 @@ def __init__(self, model, **kwargs):
"model argument must be an instance of a Pyomo BlockData object "
"(either a scalar Block or an element of an indexed Block)."
)

if len(greybox_block_set(model)) != 0:
raise NotImplementedError(
"Model contains Greybox models, which are not supported by Diagnostics toolbox at the moment"
)
self.config = self.CONFIG(kwargs)

self._model = model
Expand Down Expand Up @@ -4402,8 +4425,8 @@ def _collect_model_statistics(model):
f"(External: {len(ext_fixed_vars_in_constraints)})"
)
stats.append(
f"{TAB}Activated Equality Constraints: {len(activated_equalities_set(model))} "
f"(Deactivated: {len(deactivated_equalities_set(model))})"
f"{TAB}Activated Equality Constraints: {len(activated_equalities_set(model))+number_activated_greybox_equalities(model)} "
f"(Deactivated: {len(deactivated_equalities_set(model))+number_deactivated_greybox_equalities(model)})"
)
stats.append(
f"{TAB}Activated Inequality Constraints: {len(activated_inequalities_set(model))} "
Expand All @@ -4414,6 +4437,21 @@ def _collect_model_statistics(model):
f"(Deactivated: {len(deactivated_objectives_set(model))})"
)

# Only show graybox info if they are present
if len(greybox_block_set(model)) != 0:
stats.append(f"{TAB}GreyBox Statistics")
stats.append(
f"{TAB* 2}Activated GreyBox models: {len(activated_greybox_block_set(model))} "
f"(Deactivated: {len(deactivated_greybox_block_set(model))})"
)
stats.append(
f"{TAB* 2}Activated GreyBox Equalities: {number_activated_greybox_equalities(model)} "
f"(Deactivated: {number_deactivated_greybox_equalities(model)})"
)
stats.append(
f"{TAB* 2}Free Variables in Activated GreyBox Equalities: {len(unfixed_greybox_variables(model))} (Fixed: {len(greybox_variables(model)-unfixed_greybox_variables(model))})"
)

return stats


Expand Down
Loading

0 comments on commit 2afe182

Please sign in to comment.