def heat(psyname='heat', **fixedVars): b = Block() b.psyname = psyname b.psytype1 = "stream" b.psytype2 = "heat" b.H = myVar(0, (-big, big), "kJ") # extensive quantities b.A = myVar(0, (0, 0), "kg") b.V = myVar(0, (0, 0), "kg") b.W = myVar(0, (0, 0), "kg") for (var, val) in fixedVars.items(): setattr(b, var + "_fixed", myCon(getattr(b, var) == val)) return b
def vapor(psyname='vapor', **fixedVars): b = Block() b.psyname = psyname b.psytype1 = "stream" b.psytype2 = "vapor" b.T = myVar(300, (173, 523), "K") b.t = myVar(25, (-100, 250), "°C") b.H = myVar(0, (-big, big), "kJ") # extensive quantities b.A = myVar(0, (0, 0), "kg") b.V = myVar(0, (-big, big), "kg") b.W = myVar(0, (0, 0), "kg") b.T_convert = myCon(b.T == Tref + b.t) b.H_def = myCon(b.H == (H2O.g.H0(b.T) - H2O.l.H0(Tref)) / H2O.M * b.V) for (var, val) in fixedVars.items(): setattr(b, var + "_fixed", myCon(getattr(b, var) == val)) return b
def test_generate_cuid_string_map(self): model = Block(concrete=True) model.x = Var() model.y = Var([1, 2]) model.V = Var([('a', 'b'), (1, '2'), (3, 4)]) model.b = Block(concrete=True) model.b.z = Var([1, '2']) setattr(model.b, '.H', Var(['a', 2])) model.B = Block(['a', 2], concrete=True) setattr(model.B['a'], '.k', Var()) model.B[2].b = Block() model.B[2].b.x = Var() model.add_component('c tuple', Constraint(Any)) model.component('c tuple')[(1, )] = model.x >= 0 cuids = ( ComponentUID.generate_cuid_string_map(model, repr_version=1), ComponentUID.generate_cuid_string_map(model), ) self.assertEqual(len(cuids[0]), 29) self.assertEqual(len(cuids[1]), 29) for obj in [ model, model.x, model.y, model.y_index, model.y[1], model.y[2], model.V, model.V_index, model.V['a', 'b'], model.V[1, '2'], model.V[3, 4], model.b, model.b.z, model.b.z_index, model.b.z[1], model.b.z['2'], getattr(model.b, '.H'), getattr(model.b, '.H_index'), getattr(model.b, '.H')['a'], getattr(model.b, '.H')[2], model.B, model.B_index, model.B['a'], getattr(model.B['a'], '.k'), model.B[2], model.B[2].b, model.B[2].b.x, model.component('c tuple')[(1, )] ]: self.assertEqual(ComponentUID(obj).get_repr(1), cuids[0][obj]) self.assertEqual(repr(ComponentUID(obj)), cuids[1][obj]) cuids = ( ComponentUID.generate_cuid_string_map(model, descend_into=False, repr_version=1), ComponentUID.generate_cuid_string_map(model, descend_into=False), ) self.assertEqual(len(cuids[0]), 18) self.assertEqual(len(cuids[1]), 18) for obj in [ model, model.x, model.y, model.y_index, model.y[1], model.y[2], model.V, model.V_index, model.V['a', 'b'], model.V[1, '2'], model.V[3, 4], model.b, model.B, model.B_index, model.B['a'], model.B[2], model.component('c tuple')[(1, )] ]: self.assertEqual(ComponentUID(obj).get_repr(1), cuids[0][obj]) self.assertEqual(repr(ComponentUID(obj)), cuids[1][obj]) cuids = ( ComponentUID.generate_cuid_string_map(model, ctype=Var, repr_version=1), ComponentUID.generate_cuid_string_map(model, ctype=Var), ) self.assertEqual(len(cuids[0]), 22) self.assertEqual(len(cuids[1]), 22) for obj in [ model, model.x, model.y, model.y[1], model.y[2], model.V, model.V['a', 'b'], model.V[1, '2'], model.V[3, 4], model.b, model.b.z, model.b.z[1], model.b.z['2'], getattr(model.b, '.H'), getattr(model.b, '.H')['a'], getattr(model.b, '.H')[2], model.B, model.B['a'], getattr(model.B['a'], '.k'), model.B[2], model.B[2].b, model.B[2].b.x ]: self.assertEqual(ComponentUID(obj).get_repr(1), cuids[0][obj]) self.assertEqual(repr(ComponentUID(obj)), cuids[1][obj]) cuids = ( ComponentUID.generate_cuid_string_map(model, ctype=Var, descend_into=False, repr_version=1), ComponentUID.generate_cuid_string_map(model, ctype=Var, descend_into=False), ) self.assertEqual(len(cuids[0]), 9) self.assertEqual(len(cuids[1]), 9) for obj in [ model, model.x, model.y, model.y[1], model.y[2], model.V, model.V['a', 'b'], model.V[1, '2'], model.V[3, 4] ]: self.assertEqual(ComponentUID(obj).get_repr(1), cuids[0][obj]) self.assertEqual(repr(ComponentUID(obj)), cuids[1][obj])
def humid_air(psyname, patm=101325, **fixedVars): ref = { 'A': 1.0, 'H': 45.54956808796108, 'T': 298.15, 'Td': 283.6258531107488, 'Tw': 289.41173683833046, 'V': 0.007912721306430524, 'W': 0, 'cp': 29.602790962509385, 'psyname': 'ha', 'h': 45.54956808796108, 'm3': 0.8587547216582929, 'psat': 3169.7468549523596, 'pvap': 1267.898741980944, 'rh': 0.4, 't': 25.0, 'td': 10.475853110748833, 'tw': 16.261736838330496, 'v': 0.007912721306430524, 'vs': 0.8587547216582929 } b = Block() b.patm = patm b.psyname = psyname b.psytype1 = "stream" b.psytype2 = "humid air" b.t = myVar(ref["t"], (-100, 250), "°C") # intensive quantities b.tw = myVar(ref["tw"], (-100, 250), "°C") b.td = myVar(ref["td"], (-100, 250), "°C") b.T = myVar(ref["T"], (173, 523), "K") b.Tw = myVar(ref["Tw"], (173, 523), "K") b.Td = myVar(ref["Td"], (173, 523), "K") b.psat = myVar(ref["psat"], (0, big), "Pa") b.pvap = myVar(ref["pvap"], (0, big), "Pa") b.rh = myVar(ref["rh"], (0, 2), "-") b.v = myVar(ref["v"], (0, big), "kg/kga") b.h = myVar(ref["h"], (-big, big), "kJ/kga") b.vs = myVar(ref["vs"], (0, big), "m³/kga") b.m3 = myVar(ref["m3"], (0, big), "m³/kga") b.cp = myVar(ref["cp"], (0, big), "J/mola/K") b.H = myVar(ref["H"], (-big, big), "kJ") # extensive quantities b.A = myVar(ref["A"], (0, big), "kg") b.V = myVar(ref["V"], (0, big), "kg") b.W = myVar(ref["W"], (0, 0), "kg") bA1 = 1 b.T_convert = myCon(b.T == Tref + b.t) b.Tw_convert = myCon(b.Tw == Tref + b.tw) b.Td_convert = myCon(b.Td == Tref + b.td) b.Tw_def = myCon( H2O.psat(b.Tw) / patm - b.v / H2O.M / (b.v / H2O.M + bA1 / air.M) == b.cp / H2O.L(Tref) * (b.T - b.Tw)) b.Td_def = myCon(b.v / H2O.M / (bA1 / air.M + b.v / H2O.M) * patm == H2O.psat(b.Td)) b.psat_def = myCon(b.psat == H2O.psat(b.T)) b.pvap_def = myCon(b.pvap == b.v / H2O.M / (bA1 / air.M + b.v / H2O.M) * patm) b.rh_def = myCon(b.psat * b.rh == b.v / H2O.M / (bA1 / air.M + b.v / H2O.M) * patm) b.h_def = myCon(bA1 * b.h == bA1 / air.M * (air.H0(b.T) - air.H0(Tref)) + b.v / H2O.M * (H2O.g.H0(b.T) - H2O.l.H0(Tref))) b.vs_def = myCon(b.vs == (b.v / H2O.M + bA1 / air.M) * Rgas * b.T / patm / bA1 * 1000) b.m3_def = myCon(b.m3 == b.vs * b.A) b.cp_def = myCon(bA1 * b.cp / air.M == bA1 / air.M * air.cp0(b.T) + b.v / H2O.M * H2O.g.cp0(b.T)) b.V_def = myCon(b.V == b.v * b.A) b.H_def = myCon(b.H == b.h * b.A) for (var, val) in fixedVars.items(): setattr(b, var + "_fixed", myCon(getattr(b, var) == val)) return b
def test_generate_cuid_names(self): model = Block(concrete=True) model.x = Var() model.y = Var([1, 2]) model.V = Var([('a', 'b'), (1, '2'), (3, 4)]) model.b = Block(concrete=True) model.b.z = Var([1, '2']) setattr(model.b, '.H', Var(['a', 2])) model.B = Block(['a', 2], concrete=True) setattr(model.B['a'], '.k', Var()) model.B[2].b = Block() model.B[2].b.x = Var() cuids = generate_cuid_names(model) self.assertEqual(len(cuids), 26) for obj in [ model.x, model.y, model.y_index, model.y[1], model.y[2], model.V, model.V_index, model.V['a', 'b'], model.V[1, '2'], model.V[3, 4], model.b, model.b.z, model.b.z_index, model.b.z[1], model.b.z['2'], getattr(model.b, '.H'), getattr(model.b, '.H_index'), getattr(model.b, '.H')['a'], getattr(model.b, '.H')[2], model.B, model.B_index, model.B['a'], getattr(model.B['a'], '.k'), model.B[2], model.B[2].b, model.B[2].b.x ]: assert repr(ComponentUID(obj)) == cuids[obj] del cuids[obj] self.assertEqual(len(cuids), 0) cuids = generate_cuid_names(model, descend_into=False) self.assertEqual(len(cuids), 15) for obj in [ model.x, model.y, model.y_index, model.y[1], model.y[2], model.V, model.V_index, model.V['a', 'b'], model.V[1, '2'], model.V[3, 4], model.b, model.B, model.B_index, model.B['a'], model.B[2] ]: assert repr(ComponentUID(obj)) == cuids[obj] del cuids[obj] self.assertEqual(len(cuids), 0) cuids = generate_cuid_names(model, ctype=Var) self.assertEqual(len(cuids), 21) for obj in [ model.x, model.y, model.y[1], model.y[2], model.V, model.V['a', 'b'], model.V[1, '2'], model.V[3, 4], model.b, model.b.z, model.b.z[1], model.b.z['2'], getattr(model.b, '.H'), getattr(model.b, '.H')['a'], getattr(model.b, '.H')[2], model.B, model.B['a'], getattr(model.B['a'], '.k'), model.B[2], model.B[2].b, model.B[2].b.x ]: assert repr(ComponentUID(obj)) == cuids[obj] del cuids[obj] self.assertEqual(len(cuids), 0) cuids = generate_cuid_names(model, ctype=Var, descend_into=False) self.assertEqual(len(cuids), 8) for obj in [ model.x, model.y, model.y[1], model.y[2], model.V, model.V['a', 'b'], model.V[1, '2'], model.V[3, 4] ]: assert repr(ComponentUID(obj)) == cuids[obj] del cuids[obj] self.assertEqual(len(cuids), 0)