def test_vars(self, frame): # Check that all necessary variables have been constructed and have # the correct values assert isinstance(frame.props[1].flow_mol_phase_comp, Var) assert len(frame.props[1].flow_mol_phase_comp) == 9 for (p, i) in frame.props[1].flow_mol_phase_comp: assert (p, i) in frame.props[1].params._phase_component_set assert frame.props[1].flow_mol_phase_comp[p, i].value == 100 assert frame.props[1].flow_mol_phase_comp[p, i].lb == 0 assert frame.props[1].flow_mol_phase_comp[p, i].ub == 200 assert check_units_equivalent(frame.props[1].flow_mol_phase_comp, pyunits.mol / pyunits.s) assert isinstance(frame.props[1].pressure, Var) assert frame.props[1].pressure.value == 3e5 assert frame.props[1].pressure.lb == 1e5 assert frame.props[1].pressure.ub == 5e5 assert check_units_equivalent(frame.props[1].pressure, pyunits.Pa) assert isinstance(frame.props[1].temperature, Var) assert frame.props[1].temperature.value == 345 assert frame.props[1].temperature.lb == 290 assert frame.props[1].temperature.ub == 400 assert check_units_equivalent(frame.props[1].temperature, pyunits.K) assert isinstance(frame.props[1].mole_frac_phase_comp, Var) assert len(frame.props[1].mole_frac_phase_comp) == 9 for i in frame.props[1].mole_frac_phase_comp: assert i in frame.params._phase_component_set assert frame.props[1].mole_frac_phase_comp[i].value == 1 / 3 assert check_units_equivalent(frame.props[1].mole_frac_phase_comp, None)
def test_convert_vars(self, frame): # Check that all state var values and bounds were converted correctly assert frame.props[1].flow_mol.value == 100 assert frame.props[1].flow_mol.lb == 0 assert frame.props[1].flow_mol.ub == 200 assert check_units_equivalent(frame.props[1].flow_mol, pyunits.mol/pyunits.s) assert frame.props[1].pressure.value == 3e5 assert frame.props[1].pressure.lb == 1e5 assert frame.props[1].pressure.ub == 5e5 assert check_units_equivalent(frame.props[1].pressure, pyunits.Pa) assert frame.props[1].enth_mol.value == 500 assert frame.props[1].enth_mol.lb == 0 assert frame.props[1].enth_mol.ub == 1000 assert check_units_equivalent(frame.props[1].enth_mol, pyunits.J/pyunits.mol) assert frame.props[1].temperature.value == 345 assert frame.props[1].temperature.lb == 290 assert frame.props[1].temperature.ub == 400 assert check_units_equivalent(frame.props[1].temperature, pyunits.K)
def test_convert_vars(self, frame): # Check that all state var values and bounds were converted correctly for (p, i) in frame.props[1].flow_mol_phase_comp: assert frame.props[1].flow_mol_phase_comp[p, i].value == 100 assert frame.props[1].flow_mol_phase_comp[p, i].lb == 0 assert frame.props[1].flow_mol_phase_comp[p, i].ub == 200 assert check_units_equivalent(frame.props[1].flow_mol_phase_comp, pyunits.mol / pyunits.s) assert frame.props[1].pressure.value == 3e5 assert frame.props[1].pressure.lb == 1e5 assert frame.props[1].pressure.ub == 5e5 assert check_units_equivalent(frame.props[1].pressure, pyunits.Pa) assert frame.props[1].temperature.value == 345 assert frame.props[1].temperature.lb == 290 assert frame.props[1].temperature.ub == 400 assert check_units_equivalent(frame.props[1].temperature, pyunits.K) # Check supporting variables assert isinstance(frame.props[1].flow_mol_phase, Expression) assert len(frame.props[1].flow_mol_phase) == 2 assert isinstance(frame.props[1].mole_frac_phase_comp, Var) assert len(frame.props[1].mole_frac_phase_comp) == 6 assert isinstance(frame.props[1].phase_frac, Expression) assert len(frame.props[1].phase_frac) == 2 assert isinstance(frame.props[1].mole_frac_phase_comp_eq, Constraint) assert len(frame.props[1].mole_frac_phase_comp_eq) == 6
def test_assert_units_consistent_equivalent(self): u = units m = ConcreteModel() m.dx = Var(units=u.m, initialize=0.10188943773836046) m.dy = Var(units=u.m, initialize=0.0) m.vx = Var(units=u.m / u.s, initialize=0.7071067769802851) m.vy = Var(units=u.m / u.s, initialize=0.7071067769802851) m.t = Var(units=u.min, bounds=(1e-5, 10.0), initialize=0.0024015570927624456) m.theta = Var(bounds=(0, 0.49 * 3.14), initialize=0.7853981693583533, units=u.radians) m.a = Param(initialize=-32.2, units=u.ft / u.s**2) m.x_unitless = Var() m.obj = Objective(expr=m.dx, sense=maximize) m.vx_con = Constraint(expr=m.vx == 1.0 * u.m / u.s * cos(m.theta)) m.vy_con = Constraint(expr=m.vy == 1.0 * u.m / u.s * sin(m.theta)) m.dx_con = Constraint(expr=m.dx == m.vx * u.convert(m.t, to_units=u.s)) m.dy_con = Constraint( expr=m.dy == m.vy * u.convert(m.t, to_units=u.s) + 0.5 * (u.convert(m.a, to_units=u.m / u.s**2)) * (u.convert(m.t, to_units=u.s))**2) m.ground = Constraint(expr=m.dy == 0) m.unitless_con = Constraint(expr=m.x_unitless == 5.0) assert_units_consistent(m) # check model assert_units_consistent(m.dx) # check var - this should never fail assert_units_consistent( m.x_unitless) # check unitless var - this should never fail assert_units_consistent(m.vx_con) # check constraint assert_units_consistent(m.unitless_con) # check unitless constraint assert_units_equivalent(m.dx, m.dy) # check var assert_units_equivalent(m.x_unitless, u.dimensionless) # check unitless var assert_units_equivalent(m.x_unitless, None) # check unitless var assert_units_equivalent(m.vx_con.body, u.m / u.s) # check constraint assert_units_equivalent(m.unitless_con.body, u.dimensionless) # check unitless constraint assert_units_equivalent(m.dx, m.dy) # check var assert_units_equivalent(m.x_unitless, u.dimensionless) # check unitless var assert_units_equivalent(m.x_unitless, None) # check unitless var assert_units_equivalent(m.vx_con.body, u.m / u.s) # check constraint m.broken = Constraint(expr=m.dy == 42.0 * u.kg) with self.assertRaises(UnitsError): assert_units_consistent(m) assert_units_consistent(m.dx) assert_units_consistent(m.vx_con) with self.assertRaises(UnitsError): assert_units_consistent(m.broken) self.assertTrue(check_units_equivalent(m.dx, m.dy)) self.assertFalse(check_units_equivalent(m.dx, m.vx))
def test_convert_vars(self, frame): # Check that all state var values and bounds were converted correctly assert frame.props[1].flow_mol.value == 100 assert frame.props[1].flow_mol.lb == 0 assert frame.props[1].flow_mol.ub == 200 assert check_units_equivalent(frame.props[1].flow_mol, pyunits.mol/pyunits.s) assert frame.props[1].pressure.value == 3e5 assert frame.props[1].pressure.lb == 1e5 assert frame.props[1].pressure.ub == 5e5 assert check_units_equivalent(frame.props[1].pressure, pyunits.Pa) assert frame.props[1].enth_mol.value == 500 assert frame.props[1].enth_mol.lb == 0 assert frame.props[1].enth_mol.ub == 1000 assert check_units_equivalent(frame.props[1].enth_mol, pyunits.J/pyunits.mol) assert frame.props[1].temperature.value == 345 assert frame.props[1].temperature.lb == 290 assert frame.props[1].temperature.ub == 400 assert check_units_equivalent(frame.props[1].temperature, pyunits.K) # Check supporting variables assert isinstance(frame.props[1].flow_mol_phase, Var) assert len(frame.props[1].flow_mol_phase) == 2 assert isinstance(frame.props[1].mole_frac_phase_comp, Var) assert len(frame.props[1].mole_frac_phase_comp) == 6 assert isinstance(frame.props[1].phase_frac, Var) assert len(frame.props[1].phase_frac) == 2 assert isinstance(frame.props[1].total_flow_balance, Constraint) assert len(frame.props[1].total_flow_balance) == 1 assert isinstance(frame.props[1].component_flow_balances, Constraint) assert len(frame.props[1].component_flow_balances) == 3 assert isinstance(frame.props[1].sum_mole_frac, Constraint) assert len(frame.props[1].sum_mole_frac) == 1 assert isinstance(frame.props[1].sum_mole_frac_out, Constraint) assert isinstance(frame.props[1].phase_fraction_constraint, Constraint) assert len(frame.props[1].phase_fraction_constraint) == 2
def test_vars(self, frame): # Check that all necessary variables have been constructed and have # the correct values assert isinstance(frame.props[1].flow_mol, Var) assert frame.props[1].flow_mol.value is None assert check_units_equivalent(frame.props[1].flow_mol, pyunits.mol/pyunits.s) assert isinstance(frame.props[1].mole_frac_comp, Var) assert len(frame.props[1].mole_frac_comp) == 3 for i in frame.props[1].mole_frac_comp: assert i in frame.props[1].params.component_list assert frame.props[1].mole_frac_comp[i].value == 1/3 assert check_units_equivalent(frame.props[1].mole_frac_comp, None) assert isinstance(frame.props[1].pressure, Var) assert frame.props[1].pressure.value is None assert check_units_equivalent(frame.props[1].pressure, pyunits.Pa) assert isinstance(frame.props[1].enth_mol, Var) assert frame.props[1].enth_mol.value is None assert check_units_equivalent(frame.props[1].enth_mol, pyunits.J/pyunits.mol) assert isinstance(frame.props[1].temperature, Var) assert frame.props[1].temperature.value is None assert check_units_equivalent(frame.props[1].temperature, pyunits.K) assert isinstance(frame.props[1].flow_mol_phase, Var) assert len(frame.props[1].flow_mol_phase) == 3 for i in frame.props[1].flow_mol_phase: assert i in frame.props[1].params.phase_list assert frame.props[1].flow_mol_phase[i].value is None assert check_units_equivalent(frame.props[1].flow_mol_phase, pyunits.mol/pyunits.s) assert isinstance(frame.props[1].phase_frac, Var) assert len(frame.props[1].phase_frac) == 3 for i in frame.props[1].phase_frac: assert i in frame.props[1].params.phase_list assert frame.props[1].phase_frac[i].value == 1/3 assert check_units_equivalent(frame.props[1].phase_frac, None) assert isinstance(frame.props[1].mole_frac_phase_comp, Var) assert len(frame.props[1].mole_frac_phase_comp) == 9 for i in frame.props[1].mole_frac_phase_comp: assert i in [("p1", "c1"), ("p1", "c2"), ("p1", "c3"), ("p2", "c1"), ("p2", "c2"), ("p2", "c3"), ("p3", "c1"), ("p3", "c2"), ("p3", "c3")] assert frame.props[1].mole_frac_phase_comp[i].value == 1/3 assert check_units_equivalent(frame.props[1].mole_frac_phase_comp, None)
def test_vars(self, frame): # Check that all necessary variables have been constructed and have # the correct values assert isinstance(frame.props[1].flow_mol, Expression) assert value(frame.props[1].flow_mol) == 300 assert isinstance(frame.props[1].flow_mol_comp, Var) assert len(frame.props[1].flow_mol_comp) == 3 for i in frame.props[1].flow_mol_comp: assert i in frame.props[1].params.component_list assert frame.props[1].flow_mol_comp[i].value == 100 assert frame.props[1].flow_mol_comp[i].lb == 0 assert frame.props[1].flow_mol_comp[i].ub == 200 assert check_units_equivalent(frame.props[1].flow_mol_comp, pyunits.mol / pyunits.s) assert isinstance(frame.props[1].mole_frac_comp, Var) assert len(frame.props[1].mole_frac_comp) == 3 for i in frame.props[1].mole_frac_comp: assert i in frame.props[1].params.component_list assert frame.props[1].mole_frac_comp[i].value == 1 / 3 assert check_units_equivalent(frame.props[1].mole_frac_comp, None) assert isinstance(frame.props[1].pressure, Var) assert frame.props[1].pressure.value == 3e5 assert frame.props[1].pressure.lb == 1e5 assert frame.props[1].pressure.ub == 5e5 assert check_units_equivalent(frame.props[1].pressure, pyunits.Pa) assert isinstance(frame.props[1].temperature, Var) assert frame.props[1].temperature.value == 345 assert frame.props[1].temperature.lb == 290 assert frame.props[1].temperature.ub == 400 assert check_units_equivalent(frame.props[1].temperature, pyunits.K) assert isinstance(frame.props[1].flow_mol_phase, Var) assert len(frame.props[1].flow_mol_phase) == 2 for i in frame.props[1].flow_mol_phase: assert i in frame.props[1].params.phase_list assert frame.props[1].flow_mol_phase[i].value == 50 assert frame.props[1].flow_mol_phase[i].lb == 0 assert frame.props[1].flow_mol_phase[i].ub == 200 assert check_units_equivalent(frame.props[1].flow_mol_phase, pyunits.mol / pyunits.s) assert isinstance(frame.props[1].phase_frac, Var) assert len(frame.props[1].phase_frac) == 2 for i in frame.props[1].phase_frac: assert i in frame.props[1].params.phase_list assert frame.props[1].phase_frac[i].value == 0.5 assert check_units_equivalent(frame.props[1].phase_frac, None) assert isinstance(frame.props[1].mole_frac_phase_comp, Var) assert len(frame.props[1].mole_frac_phase_comp) == 6 for i in frame.props[1].mole_frac_phase_comp: assert i in [("p1", "c1"), ("p1", "c2"), ("p1", "c3"), ("p2", "c1"), ("p2", "c2"), ("p2", "c3")] assert frame.props[1].mole_frac_phase_comp[i].value == 1 / 3 assert check_units_equivalent(frame.props[1].mole_frac_phase_comp, None)
def test_convert_vars(self, frame): # Check that all state var values and bounds were converted correctly for (p, i) in frame.props[1].flow_mol_phase_comp: assert frame.props[1].flow_mol_phase_comp[p, i].value == 100 assert frame.props[1].flow_mol_phase_comp[p, i].lb == 0 assert frame.props[1].flow_mol_phase_comp[p, i].ub == 200 assert check_units_equivalent(frame.props[1].flow_mol_phase_comp, pyunits.mol / pyunits.s) assert frame.props[1].pressure.value == 3e5 assert frame.props[1].pressure.lb == 1e5 assert frame.props[1].pressure.ub == 5e5 assert check_units_equivalent(frame.props[1].pressure, pyunits.Pa) assert frame.props[1].temperature.value == 345 assert frame.props[1].temperature.lb == 290 assert frame.props[1].temperature.ub == 400 assert check_units_equivalent(frame.props[1].temperature, pyunits.K)
def test_convert_vars(self, frame): # Check that all state var values and bounds were converted correctly assert isinstance(frame.props[1].flow_mol_comp, Var) for i in frame.props[1].flow_mol_comp: assert i in frame.props[1].params.component_list assert frame.props[1].flow_mol_comp[i].value == 100 assert frame.props[1].flow_mol_comp[i].lb == 0 assert frame.props[1].flow_mol_comp[i].ub == 200 assert check_units_equivalent(frame.props[1].flow_mol_comp, pyunits.mol / pyunits.s) assert frame.props[1].pressure.value == 3e5 assert frame.props[1].pressure.lb == 1e5 assert frame.props[1].pressure.ub == 5e5 assert check_units_equivalent(frame.props[1].pressure, pyunits.Pa) assert frame.props[1].temperature.value == 345 assert frame.props[1].temperature.lb == 290 assert frame.props[1].temperature.ub == 400 assert check_units_equivalent(frame.props[1].temperature, pyunits.K)
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())