From 09e3da933bdab73090c471a32fb455c40601e28e Mon Sep 17 00:00:00 2001 From: Bernard Knueven Date: Thu, 20 Jun 2024 11:59:36 -0600 Subject: [PATCH] fix tests --- .../tests/test_external_grey_box_model.py | 37 +++++++++++++++-- .../tests/test_pyomo_grey_box_nlp.py | 41 +++++++++++++++++-- 2 files changed, 70 insertions(+), 8 deletions(-) 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 0fc342c4e40..c1006068055 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 @@ -1827,18 +1827,47 @@ def create_model_two_equalities_two_outputs(self, external_model): m.egb.outputs['Pout'].setub(70) return m - def test_scaling_all_missing(self): + def test_scaling_all_one(self): m = self.create_model_two_equalities_two_outputs( ex_models.PressureDropTwoEqualitiesTwoOutputs() ) m.obj = pyo.Objective(expr=(m.egb.outputs['Pout'] - 20) ** 2) pyomo_nlp = PyomoGreyBoxNLP(m) fs = pyomo_nlp.get_obj_scaling() + self.assertEqual(fs, 1.0) + + comparison_x_order = [ + 'egb.inputs[Pin]', + 'egb.inputs[c]', + 'egb.inputs[F]', + 'egb.inputs[P1]', + 'egb.inputs[P3]', + 'egb.outputs[P2]', + 'egb.outputs[Pout]', + 'hin', + 'hout', + ] + x_order = pyomo_nlp.variable_names() xs = pyomo_nlp.get_primals_scaling() + comparison_xs = np.asarray([1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.float64) + check_vectors_specific_order( + self, xs, x_order, comparison_xs, comparison_x_order + ) + + comparison_c_order = [ + 'egb.pdrop1', + 'egb.pdrop3', + 'egb.P2_con', + 'egb.Pout_con', + 'incon', + 'outcon', + ] + c_order = pyomo_nlp.constraint_names() cs = pyomo_nlp.get_constraints_scaling() - self.assertIsNone(fs) - self.assertIsNone(xs) - self.assertIsNone(cs) + comparison_cs = np.asarray([1, 1, 1, 1, 1, 1], dtype=np.float64) + check_vectors_specific_order( + self, cs, c_order, comparison_cs, comparison_c_order + ) def test_scaling_pyomo_model_only(self): m = self.create_model_two_equalities_two_outputs( 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 ecadf40e5cf..0263508bb51 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 @@ -2248,18 +2248,51 @@ def create_model_two_equalities_two_outputs(self, external_model): m.egb.outputs['Pout'].setub(70) return m - def test_scaling_all_missing(self): + def test_scaling_all_one(self): m = self.create_model_two_equalities_two_outputs( ex_models.PressureDropTwoEqualitiesTwoOutputs() ) m.obj = pyo.Objective(expr=(m.egb.outputs['Pout'] - 20) ** 2) pyomo_nlp = PyomoNLPWithGreyBoxBlocks(m) + + comparison_x_order = [ + 'egb.inputs[Pin]', + 'egb.inputs[c]', + 'egb.inputs[F]', + 'egb.inputs[P1]', + 'egb.inputs[P3]', + 'egb.outputs[P2]', + 'egb.outputs[Pout]', + 'hin', + 'hout', + ] + x_order = pyomo_nlp.primals_names() + comparison_c_order = [ + 'egb.pdrop1', + 'egb.pdrop3', + 'egb.output_constraints[P2]', + 'egb.output_constraints[Pout]', + 'incon', + 'outcon', + ] + c_order = pyomo_nlp.constraint_names() + fs = pyomo_nlp.get_obj_scaling() + self.assertEqual(fs, 1.0) + xs = pyomo_nlp.get_primals_scaling() + comparison_xs = np.asarray( + [1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.float64 + ) + check_vectors_specific_order( + self, xs, x_order, comparison_xs, comparison_x_order + ) + cs = pyomo_nlp.get_constraints_scaling() - self.assertIsNone(fs) - self.assertIsNone(xs) - self.assertIsNone(cs) + comparison_cs = np.asarray([1, 1, 1, 1, 1, 1], dtype=np.float64) + check_vectors_specific_order( + self, cs, c_order, comparison_cs, comparison_c_order + ) def test_scaling_pyomo_model_only(self): m = self.create_model_two_equalities_two_outputs(