Skip to content

Commit

Permalink
add exception tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avdudchenko committed Nov 22, 2024
1 parent 5ce566e commit 695b9cf
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 9 deletions.
25 changes: 16 additions & 9 deletions idaes/core/util/model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,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 @@ -1802,11 +1805,6 @@ def report_numerical_issues(self, stream=None):
"""
if stream is None:
stream = sys.stdout
if len(greybox_block_set(self._model)) != 0:
raise NotImplementedError(
"Model contains Greybox models, which are not supported by Diagnostics toolbox at the moment"
)

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

warnings, next_steps = self._collect_numerical_warnings(jac=jac, nlp=nlp)
Expand Down Expand Up @@ -1951,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 @@ -2393,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 @@ -3491,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
96 changes: 96 additions & 0 deletions idaes/core/util/tests/test_model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,30 @@ def model(self):

return m

@pytest.mark.component
def test_with_grey_box(self):

class BasicGrayBox(ExternalGreyBoxModel):
def input_names(self):
return ["a1", "a2", "a3"]

def output_names(self):
return ["o1", "o2"]

def equality_constraint_names(self):
return ["a_sum"]

def evaluate_equality_constraints(self):
a1 = self._input_values[0]
a2 = self._input_values[1]
return [a1 * 0.5 + a2]

m = ConcreteModel()

m.gb = ExternalGreyBoxBlock(external_model=BasicGrayBox())
with pytest.raises(NotImplementedError):
DiagnosticsToolbox(model=m)

@pytest.mark.component
def test_display_external_variables(self, model):
dt = DiagnosticsToolbox(model=model.b)
Expand Down Expand Up @@ -1911,6 +1935,30 @@ def test_svd_callback_domain(self, dummy_problem):
svd = SVDToolbox(dummy_problem, svd_callback=dummy_callback2)
assert svd.config.svd_callback is dummy_callback2

@pytest.mark.component
def test_with_grey_box(self):

class BasicGrayBox(ExternalGreyBoxModel):
def input_names(self):
return ["a1", "a2", "a3"]

def output_names(self):
return ["o1", "o2"]

def equality_constraint_names(self):
return ["a_sum"]

def evaluate_equality_constraints(self):
a1 = self._input_values[0]
a2 = self._input_values[1]
return [a1 * 0.5 + a2]

m = ConcreteModel()

m.gb = ExternalGreyBoxBlock(external_model=BasicGrayBox())
with pytest.raises(NotImplementedError):
SVDToolbox(model=m)

@pytest.mark.unit
def test_init(self, dummy_problem):
svd = SVDToolbox(dummy_problem)
Expand Down Expand Up @@ -2360,6 +2408,30 @@ def test_init(self, model):
assert dh.degenerate_set == {}
assert dh.irreducible_degenerate_sets == []

@pytest.mark.component
def test_with_grey_box(self):

class BasicGrayBox(ExternalGreyBoxModel):
def input_names(self):
return ["a1", "a2", "a3"]

def output_names(self):
return ["o1", "o2"]

def equality_constraint_names(self):
return ["a_sum"]

def evaluate_equality_constraints(self):
a1 = self._input_values[0]
a2 = self._input_values[1]
return [a1 * 0.5 + a2]

m = ConcreteModel()

m.gb = ExternalGreyBoxBlock(external_model=BasicGrayBox())
with pytest.raises(NotImplementedError):
DegeneracyHunter2(model=m)

@pytest.mark.unit
def test_get_solver(self, model):
dh = DegeneracyHunter2(model, solver="ipopt", solver_options={"maxiter": 50})
Expand Down Expand Up @@ -2617,6 +2689,30 @@ def model(self):

return m

@pytest.mark.component
def test_with_grey_box(self):

class BasicGrayBox(ExternalGreyBoxModel):
def input_names(self):
return ["a1", "a2", "a3"]

def output_names(self):
return ["o1", "o2"]

def equality_constraint_names(self):
return ["a_sum"]

def evaluate_equality_constraints(self):
a1 = self._input_values[0]
a2 = self._input_values[1]
return [a1 * 0.5 + a2]

m = ConcreteModel()

m.gb = ExternalGreyBoxBlock(external_model=BasicGrayBox())
with pytest.raises(NotImplementedError):
IpoptConvergenceAnalysis(model=m)

@pytest.mark.unit
def test_init(self, model):
ca = IpoptConvergenceAnalysis(model)
Expand Down

0 comments on commit 695b9cf

Please sign in to comment.