def test_add_data_block(self): model = param_example.create_model() sens = SensitivityInterface(model, clone_model=False) block = sens._add_data_block() self.assertIs(sens.block.parent_block(), sens.model_instance) self.assertIs(sens.block.ctype, Block) self.assertEqual(sens.block.local_name, sens.get_default_block_name()) with self.assertRaises(RuntimeError) as ex: sens._add_data_block() # We just tried adding the same block twice. self.assertIn("Cannot add component", str(ex.exception)) # Try re-adding the same block, but this time we are prepared # for it to already exist. new_block = sens._add_data_block(existing_block=block) self.assertIsNot(block, new_block) new_block._has_replaced_expressions = True with self.assertRaises(RuntimeError) as ex: sens._add_data_block(existing_block=new_block) # Cannot remove and re-add sensitivity block if expressions # were replaced. self.assertIn("Re-using sensitivity interface", str(ex.exception))
def test_sipopt_equivalent(self): m1 = param_ex.create_model() m1.perturbed_eta1 = Param(initialize = 4.0) m1.perturbed_eta2 = Param(initialize = 1.0) m2 = param_ex.create_model() m2.perturbed_eta1 = Param(initialize = 4.0) m2.perturbed_eta2 = Param(initialize = 1.0) m11 = sipopt(m1,[m1.eta1,m1.eta2], [m1.perturbed_eta1,m1.perturbed_eta2], cloneModel=True) m22 = sensitivity_calculation('sipopt',m2,[m2.eta1,m2.eta2], [m2.perturbed_eta1,m2.perturbed_eta2], cloneModel=True) out1 = StringIO() out2 = StringIO() m11._SENSITIVITY_TOOLBOX_DATA.constList.pprint(ostream=out1) m22._SENSITIVITY_TOOLBOX_DATA.constList.pprint(ostream=out2) self.assertMultiLineEqual(out1.getvalue(), out2.getvalue())
def test_kaug_deprecated(self): m = param_ex.create_model() m.perturbed_eta1 = Param(initialize = 4.0) m.perturbed_eta2 = Param(initialize = 1.0) output = StringIO() with LoggingIntercept(output, 'pyomo.contrib.sensitivity_toolbox', logging.WARNING): kaug(m,[m.eta1,m.eta1], [m.perturbed_eta1,m.perturbed_eta2], cloneModel=False) self.assertIn("DEPRECATED: The kaug function has been deprecated. Use the " "sensitivity_calculation() function with method='k_aug'", output.getvalue().replace('\n', ' '))
def test_constructor_no_clone(self): model = param_example.create_model() sens = SensitivityInterface(model, clone_model=False) self.assertIs(sens._original_model, model) self.assertIs(sens.model_instance, model)