def test_initialize(build_turbine): """Initialize a turbine model""" m = build_turbine hin = iapws95.htpx(T=880, P=2.4233e7) # set inlet m.fs.turb.inlet.enth_mol[0].value = hin m.fs.turb.inlet.flow_mol[0].value = 26000/4.0 m.fs.turb.inlet.pressure[0].value = 2.4233e7 m.fs.turb.initialize(outlvl=1) eq_cons = activated_equalities_generator(m) for c in eq_cons: assert(abs(c.body() - c.lower) < 1e-4) assert(degrees_of_freedom(m)==3) #inlet was't fixed and still shouldn't be
def test_initialize(build_turbine): """Initialize a turbine model""" m = build_turbine # set inlet m.fs.turb.inlet.enth_mol[0].value = 47115 m.fs.turb.inlet.flow_mol[0].value = 15000 m.fs.turb.inlet.pressure[0].value = 8e4 m.fs.turb.outlet.pressure[0].fix(4e4) m.fs.turb.initialize(outlvl=1) eq_cons = activated_equalities_generator(m) for c in eq_cons: assert(abs(c.body() - c.lower) < 1e-4) assert(degrees_of_freedom(m)==2) # inlet was't fixed and still shouldn't be
def test_initialize(build_turbine): """Initialize a turbine model""" m = build_turbine # set inlet m.fs.turb.inlet.enth_mol[0].value = 70000 m.fs.turb.inlet.flow_mol[0].value = 15000 m.fs.turb.inlet.pressure[0].value = 8e6 m.fs.turb.efficiency_isentropic.fix(0.8) m.fs.turb.ratioP.fix(0.7) m.fs.turb.initialize(outlvl=4) eq_cons = activated_equalities_generator(m) for c in eq_cons: assert(abs(c.body() - c.lower) < 1e-4) assert(degrees_of_freedom(m)==3) #inlet was't fixed and still shouldn't be
def test_initialize_by_element_in_range(nmpc): plant = nmpc.plant controller = nmpc.controller time = plant.time sample_time = nmpc.sample_time nmpc.solve_consistent_initial_conditions(plant) assert nmpc.has_consistent_initial_conditions(plant, tolerance=1e-6) assert degrees_of_freedom(plant) == 0 initialize_by_element_in_range(plant, time, 0, 3, dae_vars=plant._NMPC_NAMESPACE.dae_vars, time_linking_vars=plant._NMPC_NAMESPACE.diff_vars.varlist) assert degrees_of_freedom(plant) == 0 for con in activated_equalities_generator(plant): if 'disc_eq' in con.local_name or 'balance' in con.local_name: # (know disc. and balance equations will be directly indexed # by time) index = con.index() if not type(index) is tuple: index = (index,) t_index = index[0] if t_index <= 3: # Equalities in simulated range should not be violated assert abs(value(con.body)-value(con.upper)) < 1e-6 nmpc.simulate_plant(0) # Check that plant simulation matches controller simulation. # Only valid because there is no noise or plant-model-mismatch # and plant/controller have the same time discretizations p_varlist = (plant._NMPC_NAMESPACE.diff_vars.varlist + plant._NMPC_NAMESPACE.alg_vars.varlist + plant._NMPC_NAMESPACE.deriv_vars.varlist) c_varlist = (controller._NMPC_NAMESPACE.diff_vars.varlist + controller._NMPC_NAMESPACE.alg_vars.varlist + controller._NMPC_NAMESPACE.deriv_vars.varlist) for i, pvar in enumerate(p_varlist): for t in time: if t > sample_time or t == 0: continue cvar = c_varlist[i] assert pvar[t].value == approx(cvar[t].value, abs=1e-5)
def test_solve_control_problem(nmpc): controller = nmpc.controller init_obj_value = value(controller._NMPC_NAMESPACE.tracking_objective.expr) nmpc.solve_control_problem() final_obj_value = value(controller._NMPC_NAMESPACE.tracking_objective.expr) # Not always true because initial model might not be feasible assert final_obj_value < init_obj_value for con in activated_equalities_generator(controller): assert abs(value(con.body) - value(con.upper)) < 1e-5 for var in unfixed_variables_generator(controller): if var.lb is not None: assert var.lb - var.value < 1e-6 if var.ub is not None: assert var.value - var.ub < 1e-6
def test_vapor_steady_state_initialize(build_valve_vapor): """Initialize a turbine model""" # set inlet hin = value(iapws95.htpx(T=880 * pyunits.K, P=2.4233e7 * pyunits.Pa)) # set inlet build_valve_vapor.fs.valve.inlet.enth_mol[0].value = hin build_valve_vapor.fs.valve.inlet.flow_mol[0].value = 26000 / 4.0 build_valve_vapor.fs.valve.inlet.pressure[0].value = 2.5e7 build_valve_vapor.fs.valve.Cv.fix(0.01) build_valve_vapor.fs.valve.initialize(outlvl=1) eq_cons = activated_equalities_generator(build_valve_vapor) for c in eq_cons: assert (abs(c.body() - c.lower) < 1e-4) # inlet was't fixed and still shouldn't be assert degrees_of_freedom(build_valve_vapor) == 3
def test_initialize_dyn(build_turbine_dyn): """Initialize a turbine model""" m = build_turbine_dyn hin = value(iapws95.htpx(T=880*pyunits.K, P=2.4233e7*pyunits.Pa)) discretizer = TransformationFactory('dae.finite_difference') discretizer.apply_to(m, nfe=4, wrt=m.fs.time, scheme='BACKWARD') # fix inlet m.fs.turb.inlet.enth_mol.fix(hin) m.fs.turb.inlet.flow_mol.fix(26000/4.0) m.fs.turb.inlet.pressure.fix(2.4233e7) m.fs.turb.flow_coeff[:].fix() m.fs.turb.initialize() eq_cons = activated_equalities_generator(m) for c in eq_cons: assert(abs(c.body() - c.lower) < 1e-4) assert(degrees_of_freedom(m)==0)
def test_initialize_calc_cf(build_turbine): """Initialize a turbine model""" m = build_turbine # set inlet m.fs.turb.inlet.enth_mol[0].value = 47115 m.fs.turb.inlet.flow_mol[0].value = 15000 m.fs.turb.inlet.pressure[0].value = 8e4 m.fs.turb.outlet.pressure[0].fix(4e4) m.fs.turb.initialize(calculate_cf=True) eq_cons = activated_equalities_generator(m) for c in eq_cons: assert abs(c.body() - c.lower) < 1e-4 m.fs.turb.inlet.enth_mol[0].fix() m.fs.turb.inlet.pressure[0].fix() solver.solve(m) assert m.fs.turb.inlet.flow_mol[0].value == pytest.approx(15000) assert degrees_of_freedom(m) == 0
def test_initialize_calc_cf_dyn(build_turbine_dyn): """Initialize a turbine model""" m = build_turbine_dyn discretizer = TransformationFactory('dae.finite_difference') discretizer.apply_to(m, nfe=4, wrt=m.fs.time, scheme='BACKWARD') # set inlet m.fs.turb.inlet.enth_mol.fix(47115) for t in m.fs.turb.inlet.flow_mol: m.fs.turb.inlet.flow_mol[t].value = 15000 m.fs.turb.inlet.pressure.fix(8e4) m.fs.turb.outlet.pressure.fix(4e4) m.fs.turb.flow_coeff.fix() assert degrees_of_freedom(m) == 0 m.fs.turb.initialize(calculate_cf=True) eq_cons = activated_equalities_generator(m) for c in eq_cons: assert abs(c.body() - c.lower) < 1e-4 solver.solve(m) assert m.fs.turb.inlet.flow_mol[0].value == pytest.approx(15000) assert degrees_of_freedom(m) == 0
def test_initialize_dyn2(build_turbine_dyn): """Initialize a turbine model""" m = build_turbine_dyn hin = iapws95.htpx(T=880, P=2.4233e7) discretizer = TransformationFactory('dae.finite_difference') discretizer.apply_to(m, nfe=4, wrt=m.fs.time, scheme='BACKWARD') # fix inlet m.fs.turb.inlet.enth_mol.fix(hin) m.fs.turb.inlet.flow_mol.fix(26000/4.0) m.fs.turb.inlet.pressure.fix(2.4233e7) m.fs.turb.flow_coeff[:].fix() m.fs.turb.ratioP[0].value = 0.6 # used for flow coeff calc m.fs.turb.initialize(calculate_cf=True) eq_cons = activated_equalities_generator(m) for c in eq_cons: assert(abs(c.body() - c.lower) < 1e-4) assert m.fs.turb.ratioP[0].value == pytest.approx(0.6) assert(degrees_of_freedom(m)==0)
def test_initialize_calc_cf(build_turbine): """Initialize a turbine model""" m = build_turbine hin = iapws95.htpx(T=880, P=2.4233e7) # set inlet m.fs.turb.inlet.enth_mol[0].value = hin m.fs.turb.inlet.flow_mol[0].value = 26000/4.0 m.fs.turb.inlet.pressure[0].value = 2.4233e7 m.fs.turb.ratioP[0].value = 0.6 m.fs.turb.initialize(outlvl=1, calculate_cf=True) eq_cons = activated_equalities_generator(m) for c in eq_cons: assert abs(c.body() - c.lower) < 1e-4 m.fs.turb.inlet.enth_mol[0].value = hin m.fs.turb.inlet.flow_mol[0].value = 26000/4.0 m.fs.turb.inlet.pressure[0].value = 2.4233e7 m.fs.turb.inlet.fix() solver.solve(m) assert m.fs.turb.ratioP[0].value == pytest.approx(0.6) assert degrees_of_freedom(m)==0
def test_init(model): # check that the model solved properly and has 0 degrees of freedom assert (degrees_of_freedom(model) == 0) for c in activated_equalities_generator(model): assert (abs(c.body() - c.lower) < 5e-4)
def test_initialize(): """Make a turbine model and make sure it doesn't throw exception""" m = build_turbine_for_run_test() turb = m.fs.turb # Set the inlet of the turbine p = 2.4233e7 hin = iapws95_ph.htpx(T=880, P=p) m.fs.turb.inlet_split.inlet.enth_mol[0].fix(hin) m.fs.turb.inlet_split.inlet.flow_mol[0].fix(26000) m.fs.turb.inlet_split.inlet.pressure[0].fix(p) # Set the inlet of the ip section, which is disconnected # here to insert reheater p = 7.802e+06 hin = iapws95_ph.htpx(T=880, P=p) m.fs.turb.ip_stages[1].inlet.enth_mol[0].value = hin m.fs.turb.ip_stages[1].inlet.flow_mol[0].value = 25220.0 m.fs.turb.ip_stages[1].inlet.pressure[0].value = p for i, s in turb.hp_stages.items(): s.ratioP[:] = 0.88 s.efficiency_isentropic[:] = 0.9 for i, s in turb.ip_stages.items(): s.ratioP[:] = 0.85 s.efficiency_isentropic[:] = 0.9 for i, s in turb.lp_stages.items(): s.ratioP[:] = 0.82 s.efficiency_isentropic[:] = 0.9 turb.hp_split[4].split_fraction[0, "outlet_2"].fix(0.03) turb.hp_split[7].split_fraction[0, "outlet_2"].fix(0.03) turb.ip_split[5].split_fraction[0, "outlet_2"].fix(0.04) turb.ip_split[14].split_fraction[0, "outlet_2"].fix(0.04) turb.ip_split[14].split_fraction[0, "outlet_3"].fix(0.15) turb.lp_split[4].split_fraction[0, "outlet_2"].fix(0.04) turb.lp_split[7].split_fraction[0, "outlet_2"].fix(0.04) turb.lp_split[9].split_fraction[0, "outlet_2"].fix(0.04) turb.lp_split[11].split_fraction[0, "outlet_2"].fix(0.04) # Congiure with reheater for a full test turb.ip_stages[1].inlet.unfix() turb.inlet_split.inlet.flow_mol.unfix() turb.inlet_mix.use_equal_pressure_constraint() turb.initialize(outlvl=1) for t in m.fs.time: m.fs.reheat.inlet.flow_mol[t].value = \ value(turb.hp_split[7].outlet_1_state[t].flow_mol) m.fs.reheat.inlet.enth_mol[t].value = \ value(turb.hp_split[7].outlet_1_state[t].enth_mol) m.fs.reheat.inlet.pressure[t].value = \ value(turb.hp_split[7].outlet_1_state[t].pressure) m.fs.reheat.initialize(outlvl=4) def reheat_T_rule(b, t): return m.fs.reheat.control_volume.properties_out[t].temperature == 880 m.fs.reheat.temperature_out_equation = Constraint( m.fs.reheat.flowsheet().config.time, rule=reheat_T_rule) TransformationFactory("network.expand_arcs").apply_to(m) m.fs.turb.outlet_stage.control_volume.properties_out[0].pressure.fix() assert (degrees_of_freedom(m) == 0) solver.solve(m, tee=True) eq_cons = activated_equalities_generator(m) for c in eq_cons: assert (abs(c.body() - c.lower) < 1e-4) return m