diff --git a/pyomo/common/errors.py b/pyomo/common/errors.py index 3c82f2b07c1..5940875445d 100644 --- a/pyomo/common/errors.py +++ b/pyomo/common/errors.py @@ -244,3 +244,12 @@ class TemplateExpressionError(ValueError): def __init__(self, template, *args, **kwds): self.template = template super(TemplateExpressionError, self).__init__(*args, **kwds) + + +class EmptyModelError(PyomoException, ValueError): + """ + Exception class used to throw an error for an empty + Pyomo model (i.e., no variables or constraints). + """ + + pass diff --git a/pyomo/contrib/pynumero/interfaces/tests/test_external_grey_box_model.py b/pyomo/contrib/pynumero/interfaces/tests/test_external_grey_box_model.py index 1ea17b5e223..14749d368c8 100644 --- a/pyomo/contrib/pynumero/interfaces/tests/test_external_grey_box_model.py +++ b/pyomo/contrib/pynumero/interfaces/tests/test_external_grey_box_model.py @@ -41,6 +41,7 @@ check_sparse_matrix_specific_order, ) import pyomo.contrib.pynumero.interfaces.tests.external_grey_box_models as ex_models +from pyomo.common.errors import EmptyModelError class TestExternalGreyBoxModel(unittest.TestCase): @@ -615,7 +616,7 @@ def test_error_no_variables(self): m.egb = ExternalGreyBoxBlock() m.egb.set_external_model(ex_models.PressureDropSingleOutput()) m.obj = pyo.Objective(expr=1) - with self.assertRaises(ValueError): + with self.assertRaises(EmptyModelError): pyomo_nlp = PyomoGreyBoxNLP(m) def test_error_fixed_inputs_outputs(self): diff --git a/pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py b/pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py index 053c9aba4ea..1d26a21618c 100644 --- a/pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py +++ b/pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py @@ -41,6 +41,7 @@ check_sparse_matrix_specific_order, ) import pyomo.contrib.pynumero.interfaces.tests.external_grey_box_models as ex_models +from pyomo.common.errors import EmptyModelError class TestExternalGreyBoxAsNLP(unittest.TestCase): @@ -1033,7 +1034,7 @@ def test_error_no_variables(self): m.egb = ExternalGreyBoxBlock() m.egb.set_external_model(ex_models.PressureDropSingleOutput()) m.obj = pyo.Objective(expr=1) - with self.assertRaises(ValueError): + with self.assertRaises(EmptyModelError): pyomo_nlp = PyomoNLPWithGreyBoxBlocks(m) def test_error_fixed_inputs_outputs(self): diff --git a/pyomo/repn/plugins/ampl/ampl_.py b/pyomo/repn/plugins/ampl/ampl_.py index cc99e9cfdae..679dde5f490 100644 --- a/pyomo/repn/plugins/ampl/ampl_.py +++ b/pyomo/repn/plugins/ampl/ampl_.py @@ -22,6 +22,7 @@ from pyomo.common.fileutils import find_library from pyomo.common.gc_manager import PauseGC +from pyomo.common.errors import EmptyModelError from pyomo.opt import ProblemFormat, AbstractProblemWriter, WriterFactory import pyomo.core.expr as EXPR from pyomo.core.expr.numvalue import ( @@ -1361,7 +1362,7 @@ def _print_model_NL( subsection_timer.reset() if len(full_var_list) < 1: - raise ValueError( + raise EmptyModelError( "No variables appear in the Pyomo model constraints or" " objective. This is not supported by the NL file interface" ) diff --git a/pyomo/repn/plugins/nl_writer.py b/pyomo/repn/plugins/nl_writer.py index bc7e703a1a7..2b74b035760 100644 --- a/pyomo/repn/plugins/nl_writer.py +++ b/pyomo/repn/plugins/nl_writer.py @@ -25,7 +25,7 @@ document_kwargs_from_configdict, ) from pyomo.common.deprecation import relocated_module_attribute -from pyomo.common.errors import DeveloperError, InfeasibleConstraintException +from pyomo.common.errors import InfeasibleConstraintException, EmptyModelError from pyomo.common.gc_manager import PauseGC from pyomo.common.timing import TicTocTimer @@ -320,7 +320,7 @@ def __call__(self, model, filename, solver_capability, io_options): if config.symbolic_solver_labels: os.remove(row_fname) os.remove(col_fname) - raise ValueError( + raise EmptyModelError( "No variables appear in the Pyomo model constraints or" " objective. This is not supported by the NL file interface" )