Skip to content

Commit

Permalink
Adding zero tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee94 committed Oct 25, 2024
1 parent b3098e1 commit 60e71cb
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions idaes/core/util/model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ def svd_sparse(jacobian, number_singular_values):
description="Absolute tolerance to use when checking for canceling additive terms in constraints.",
),
)
CONFIG.declare(
"constraint_term_zero_tolerance",
ConfigValue(
default=1e-10,
domain=NonNegativeFloat,
description="Absolute tolerance to use when determining if a constraint term is equal ot zero.",

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

View workflow job for this annotation

GitHub Actions / Check Spelling

"ot" should be "to" or "of" or "or" or "not".
),
)
CONFIG.declare(
"variable_large_value_tolerance",
ConfigValue(
Expand Down Expand Up @@ -1077,6 +1085,7 @@ def _collect_constraint_mismatches(self, descend_into=True):
walker = ConstraintTermAnalysisVisitor(
term_mismatch_tolerance=self.config.constraint_term_mismatch_tolerance,
term_cancellation_tolerance=self.config.constraint_term_cancellation_tolerance,
term_zero_tolerance=self.config.constraint_term_zero_tolerance,
)

mismatch = []
Expand Down Expand Up @@ -1188,6 +1197,7 @@ def display_problematic_constraint_terms(self, constraint, stream=None):
walker = ConstraintTermAnalysisVisitor(
term_mismatch_tolerance=self.config.constraint_term_mismatch_tolerance,
term_cancellation_tolerance=self.config.constraint_term_cancellation_tolerance,
term_zero_tolerance=self.config.constraint_term_zero_tolerance,
)

_, expr_mismatch, expr_cancellation, _ = walker.walk_expression(constraint.expr)
Expand Down Expand Up @@ -4351,12 +4361,14 @@ def __init__(
self,
term_mismatch_tolerance: float = 1e6,
term_cancellation_tolerance: float = 1e-4,
term_zero_tolerance: float = 1e-10,
):
super().__init__()

# Tolerance attributes
self._log_mm_tol = log10(term_mismatch_tolerance)
self._sum_tol = term_cancellation_tolerance
self._zero_tolerance = term_zero_tolerance

# Placeholders for collecting results
self.canceling_terms = ComponentSet()
Expand All @@ -4381,7 +4393,7 @@ def _generate_sum_combinations(self, inputs):
def _check_sum_cancellations(self, values_list):
# First, strip any terms with value 0 as they do not contribute to cancellation
# We do this to keep the number of possible combinations as small as possible
stripped = [i for i in values_list if i != 0]
stripped = [i for i in values_list if abs(i) >= self._zero_tolerance]

if len(stripped) == 0:
# If the stripped list is empty, there are no non-zero terms
Expand Down Expand Up @@ -4551,17 +4563,18 @@ def _check_sum_expression(self, node, child_data):

# Check for mismatched terms
if len(vals) > 1:
absvals = [abs(v) for v in vals]
vl = max(absvals)
vs = min(absvals)
if vl != vs:
if vs == 0:
diff = log10(vl)
else:
diff = log10(vl / vs)
absvals = [abs(v) for v in vals if abs(v) >= self._zero_tolerance]
if len(absvals) > 0:
vl = max(absvals)
vs = min(absvals)
if vl != vs:
if vs == 0:
diff = log10(vl)
else:
diff = log10(vl / vs)

if diff >= self._log_mm_tol:
self.mismatched_terms.add(node)
if diff >= self._log_mm_tol:
self.mismatched_terms.add(node)

return vals, const, True

Expand Down

0 comments on commit 60e71cb

Please sign in to comment.