Skip to content

Commit

Permalink
Fixing bug related to external functions with string arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee94 committed Sep 6, 2024
1 parent 8140a3d commit d10a493
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions idaes/core/util/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,10 +1472,14 @@ def _get_nominal_value_expr_if(self, node, child_nominal_values):

def _get_nominal_value_external_function(self, node, child_nominal_values):
# First, need to get expected magnitudes of input terms, which may be sub-expressions
input_mag = [
self._get_nominal_value_for_sum_subexpression(i)
for i in child_nominal_values
]
input_mag = []
for i in child_nominal_values:
if isinstance(i[0], str):
# Sometimes external functions might have string arguments
# Check here, and return the string if true
input_mag.append(i[0])
else:
input_mag.append(self._get_nominal_value_for_sum_subexpression(i))

# Next, create a copy of the external function with expected magnitudes as inputs
newfunc = node.create_node_with_local_data(input_mag)
Expand Down
11 changes: 11 additions & 0 deletions idaes/core/util/tests/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
CubicThermoExpressions,
CubicType as CubicEoS,
)
from idaes.models.properties import iapws95

__author__ = "John Eslick, Tim Bartholomew"

Expand Down Expand Up @@ -2230,6 +2231,16 @@ def test_constraint(self, m):
expr=m.constraint.expr
) == [21, 0.5 ** (22 + 23 + 24)]

@pytest.mark.component
def test_external_function_w_string_argument(self):
m = pyo.ConcreteModel()
m.properties = iapws95.Iapws95ParameterBlock()
m.state = m.properties.build_state_block([0])

assert sc.NominalValueExtractionVisitor().walk_expression(
expr=m.state[0].temperature
) == [pytest.approx(235.0, rel=1e-8)]


@pytest.fixture(scope="function")
def m():
Expand Down

0 comments on commit d10a493

Please sign in to comment.