Beispiel #1
0
    def test_container_constructor(self):
        # Custom pint registry:
        um0 = PyomoUnitsContainer(None)
        self.assertIsNone(um0.pint_registry)
        self.assertIsNone(um0._pint_dimensionless)
        um1 = PyomoUnitsContainer()
        self.assertIsNotNone(um1.pint_registry)
        self.assertIsNotNone(um1._pint_dimensionless)
        with self.assertRaisesRegex(
                ValueError,
                'Cannot operate with Unit and Unit of different registries'):
            self.assertEqual(um1._pint_dimensionless,
                             units._pint_dimensionless)
        self.assertIsNot(um1.pint_registry, units.pint_registry)
        um2 = PyomoUnitsContainer(pint_module.UnitRegistry())
        self.assertIsNotNone(um2.pint_registry)
        self.assertIsNotNone(um2._pint_dimensionless)
        with self.assertRaisesRegex(
                ValueError,
                'Cannot operate with Unit and Unit of different registries'):
            self.assertEqual(um2._pint_dimensionless,
                             units._pint_dimensionless)
        self.assertIsNot(um2.pint_registry, units.pint_registry)
        self.assertIsNot(um2.pint_registry, um1.pint_registry)

        um3 = PyomoUnitsContainer(units.pint_registry)
        self.assertIs(um3.pint_registry, units.pint_registry)
        self.assertEqual(um3._pint_dimensionless, units._pint_dimensionless)
Beispiel #2
0
 def test_set_pint_registry(self):
     um = _DeferredUnitsSingleton()
     pint_reg = pint_module.UnitRegistry()
     # Test we can (silently) set the registry if it is the first
     # thing we do
     with LoggingIntercept() as LOG:
         um.set_pint_registry(pint_reg)
     self.assertEqual(LOG.getvalue(), "")
     self.assertIs(um.pint_registry, pint_reg)
     # Test that a no-op set is silent
     with LoggingIntercept() as LOG:
         um.set_pint_registry(pint_reg)
     self.assertEqual(LOG.getvalue(), "")
     # Test that changing the registry generates a warning
     with LoggingIntercept() as LOG:
         um.set_pint_registry(pint_module.UnitRegistry())
     self.assertIn(
         "Changing the pint registry used by the Pyomo Units "
         "system after the PyomoUnitsContainer was constructed",
         LOG.getvalue())
Beispiel #3
0
    def test_pickle(self):
        m = ConcreteModel()
        m.x = Var(units=units.kg)
        m.c = Constraint(expr=m.x**2 <= 10 * units.kg**2)
        log = StringIO()
        with LoggingIntercept(log, 'pyomo.core.base'):
            i = pickle.loads(pickle.dumps(m))
        self.assertEqual("", log.getvalue())
        self.assertIsNot(m.x, i.x)
        self.assertIsNot(m.x._units, i.x._units)
        self.assertTrue(check_units_equivalent(m.x._units, i.x._units))
        self.assertEqual(str(m.c.upper), str(i.c.upper))
        base = StringIO()
        m.pprint(base)
        test = StringIO()
        i.pprint(test)
        self.assertEqual(base.getvalue(), test.getvalue())

        # Test pickling a custom units manager
        um = PyomoUnitsContainer(pint_module.UnitRegistry())
        m = ConcreteModel()
        m.x = Var(units=um.kg)
        m.c = Constraint(expr=m.x**2 <= 10 * um.kg**2)
        log = StringIO()
        with LoggingIntercept(log, 'pyomo.core.base'):
            i = pickle.loads(pickle.dumps(m))
        self.assertIn(
            "pickling a _PyomoUnit associated with a PyomoUnitsContainer "
            "that is not the default singleton "
            "(pyomo.core.base.units_container.units)", log.getvalue())
        self.assertIsNot(m.x, i.x)
        self.assertIsNot(m.x._units, i.x._units)
        # Note that pint is inconsistent when comparing standard units
        # across different UnitRegistry instances: older versions of
        # pint would have them compare "not equal" while newer versions
        # compare equal
        #
        # self.assertNotEqual(m.x._units, i.x._units)
        self.assertEqual(str(m.c.upper), str(i.c.upper))
        base = StringIO()
        m.pprint(base)
        test = StringIO()
        i.pprint(test)
        self.assertEqual(base.getvalue(), test.getvalue())