def create_economic_dispatch_approx_model(model_data): md = tx_utils.scale_ModelData_to_pu(model_data) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') load_attrs = md.attributes(element_type='load') shunt_attrs = md.attributes(element_type='shunt') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_in_service_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, _ = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) model.pl.fix() ### declare the fixed shunts at the buses _, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts(buses, shunts) ### declare the generator real power pg_init = { k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg'] } libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max'])) ### declare the p balance libbus.declare_eq_p_balance_ed(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost']) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) model.obj = pe.Objective(expr=obj_expr) return model
def create_soc_relaxation(model_data, use_linear_relaxation=False): model, md = _create_base_relaxation(model_data) if use_linear_relaxation: if not coramin_available: raise ImportError( 'Cannot create SOC relaxation with outer approximation unless coramin is available.' ) model = coramin.relaxations.relax(model, in_place=True, use_fbbt=False) branch_attrs = md.attributes(element_type='branch') bus_pairs = zip_items(branch_attrs['from_bus'], branch_attrs['to_bus']) unique_bus_pairs = list( OrderedDict((val, None) for idx, val in bus_pairs.items()).keys()) libbranch.declare_ineq_soc(model=model, index_set=unique_bus_pairs, use_outer_approximation=use_linear_relaxation) return model, md
def declare_socp_scw(model, index_set, branches, branch_attrs): """ Create the SOCP constraints c_bk^2+s_bk^2<= w_bb*w_kk """ m = model con_set = decl.declare_set("_con_socp_scw_set", model, index_set) m.socp_scw = pe.Constraint(con_set) bus_pairs = zip_items(branch_attrs['from_bus'], branch_attrs['to_bus']) unique_bus_pairs = list(set([val for idx, val in bus_pairs.items()])) for branch_name in con_set: branch = branches[branch_name] from_bus = branch['from_bus'] to_bus = branch['to_bus'] m.socp_scw[branch_name] = \ m.c[(from_bus,to_bus)]**2 + m.s[(from_bus,to_bus)]**2 <= \ m.w[from_bus] * m.w[to_bus]
def create_relaxation_of_polar_acopf(model_data, include_soc=True, use_linear_relaxation=False): if not coramin_available: raise ImportError( 'Cannot create polar relaxation unless coramin is available.') model, md = _create_base_relaxation(model_data) branches = dict(md.elements(element_type='branch')) bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') bus_pairs = zip_items(branch_attrs['from_bus'], branch_attrs['to_bus']) unique_bus_pairs = list( OrderedDict((val, None) for idx, val in bus_pairs.items()).keys()) # declare the polar voltages libbus.declare_var_vm(model, bus_attrs['names'], initialize=bus_attrs['vm'], bounds=zip_items(bus_attrs['v_min'], bus_attrs['v_max'])) va_bounds = {k: (-pi, pi) for k in bus_attrs['va']} libbus.declare_var_va(model, bus_attrs['names'], initialize=bus_attrs['va'], bounds=va_bounds) # fix the reference bus ref_bus = md.data['system']['reference_bus'] ref_angle = md.data['system']['reference_bus_angle'] model.va[ref_bus].fix(radians(ref_angle)) # relate c, s, and vmsq to vm and va libbus.declare_eq_vmsq(model=model, index_set=bus_attrs['names'], coordinate_type=CoordinateType.POLAR) libbranch.declare_eq_c(model=model, index_set=unique_bus_pairs, coordinate_type=CoordinateType.POLAR) libbranch.declare_eq_s(model=model, index_set=unique_bus_pairs, coordinate_type=CoordinateType.POLAR) # declare angle difference limits on interconnected buses libbranch.declare_ineq_angle_diff_branch_lbub( model=model, index_set=branch_attrs['names'], branches=branches, coordinate_type=CoordinateType.POLAR) fbbt(model, deactivate_satisfied_constraints=False) model = coramin.relaxations.relax(model, in_place=True, use_fbbt=False) if not use_linear_relaxation: for b in model.component_data_objects(pe.Block, descend_into=True, active=True, sort=True): if isinstance(b, (coramin.relaxations.BaseRelaxation, coramin.relaxations.BaseRelaxationData)): if polynomial_degree(b.get_rhs_expr()) == 2: if not isinstance( b, (coramin.relaxations.PWMcCormickRelaxation, coramin.relaxations.PWMcCormickRelaxationData)): b.use_linear_relaxation = False b.rebuild() if include_soc: libbranch.declare_ineq_soc( model=model, index_set=unique_bus_pairs, use_outer_approximation=use_linear_relaxation) return model, md
def _create_base_relaxation(model_data): md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace=True) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) bus_pairs = zip_items(branch_attrs['from_bus'], branch_attrs['to_bus']) unique_bus_pairs = list( OrderedDict((val, None) for idx, val in bus_pairs.items())) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, bus_q_loads = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) libbus.declare_var_ql(model, bus_attrs['names'], initialize=bus_q_loads) model.pl.fix() model.ql.fix() ### declare the fixed shunts at the buses bus_bs_fixed_shunts, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts( buses, shunts) libbus.declare_var_vmsq( model=model, index_set=bus_attrs['names'], initialize={k: v**2 for k, v in bus_attrs['vm'].items()}, bounds=zip_items({k: v**2 for k, v in bus_attrs['v_min'].items()}, {k: v**2 for k, v in bus_attrs['v_max'].items()})) libbranch.declare_var_c(model=model, index_set=unique_bus_pairs) libbranch.declare_var_s(model=model, index_set=unique_bus_pairs) ### declare the generator real and reactive power pg_init = { k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg'] } libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max'])) qg_init = { k: (gen_attrs['q_min'][k] + gen_attrs['q_max'][k]) / 2.0 for k in gen_attrs['qg'] } libgen.declare_var_qg(model, gen_attrs['names'], initialize=qg_init, bounds=zip_items(gen_attrs['q_min'], gen_attrs['q_max'])) ### declare the current flows in the branches vr_init = { k: bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]) for k in bus_attrs['vm'] } vj_init = { k: bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]) for k in bus_attrs['vm'] } s_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} s_lbub = dict() for k in branches.keys(): if s_max[k] is None: s_lbub[k] = (None, None) else: s_lbub[k] = (-s_max[k], s_max[k]) pf_bounds = s_lbub pt_bounds = s_lbub qf_bounds = s_lbub qt_bounds = s_lbub pf_init = dict() pt_init = dict() qf_init = dict() qt_init = dict() for branch_name, branch in branches.items(): from_bus = branch['from_bus'] to_bus = branch['to_bus'] y_matrix = tx_calc.calculate_y_matrix_from_branch(branch) ifr_init = tx_calc.calculate_ifr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) ifj_init = tx_calc.calculate_ifj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itr_init = tx_calc.calculate_itr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itj_init = tx_calc.calculate_itj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) pf_init[branch_name] = tx_calc.calculate_p(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) pt_init[branch_name] = tx_calc.calculate_p(itr_init, itj_init, vr_init[to_bus], vj_init[to_bus]) qf_init[branch_name] = tx_calc.calculate_q(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) qt_init[branch_name] = tx_calc.calculate_q(itr_init, itj_init, vr_init[to_bus], vj_init[to_bus]) libbranch.declare_var_pf(model=model, index_set=branch_attrs['names'], initialize=pf_init, bounds=pf_bounds) libbranch.declare_var_pt(model=model, index_set=branch_attrs['names'], initialize=pt_init, bounds=pt_bounds) libbranch.declare_var_qf(model=model, index_set=branch_attrs['names'], initialize=qf_init, bounds=qf_bounds) libbranch.declare_var_qt(model=model, index_set=branch_attrs['names'], initialize=qt_init, bounds=qt_bounds) ### declare the branch power flow constraints libbranch.declare_eq_branch_power(model=model, index_set=branch_attrs['names'], branches=branches) ### declare the pq balances libbus.declare_eq_p_balance(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus) libbus.declare_eq_q_balance(model=model, index_set=bus_attrs['names'], bus_q_loads=bus_q_loads, gens_by_bus=gens_by_bus, bus_bs_fixed_shunts=bus_bs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus) ### declare the thermal limits libbranch.declare_ineq_s_branch_thermal_limit( model=model, index_set=branch_attrs['names'], branches=branches, s_thermal_limits=s_max, flow_type=FlowType.POWER) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost'], q_costs=gen_attrs.get( 'q_cost', None)) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if hasattr(model, 'qg_operating_cost'): obj_expr += sum(model.qg_operating_cost[gen_name] for gen_name in model.qg_operating_cost) model.obj = pe.Objective(expr=obj_expr) return model, md
def declare_eq_branch_power(model, index_set, branches, branch_attrs, coordinate_type=CoordinateType.POLAR): """ Create the equality constraints for the real and reactive power in the branch """ m = model bus_pairs = zip_items(branch_attrs['from_bus'], branch_attrs['to_bus']) unique_bus_pairs = list(set([val for idx, val in bus_pairs.items()])) declare_expr_c(model, unique_bus_pairs, coordinate_type) declare_expr_s(model, unique_bus_pairs, coordinate_type) con_set = decl.declare_set("_con_eq_branch_power_set", model, index_set) m.eq_pf_branch = pe.Constraint(con_set) m.eq_pt_branch = pe.Constraint(con_set) m.eq_qf_branch = pe.Constraint(con_set) m.eq_qt_branch = pe.Constraint(con_set) for branch_name in con_set: branch = branches[branch_name] if not branch['in_service']: continue from_bus = branch['from_bus'] to_bus = branch['to_bus'] if coordinate_type == CoordinateType.POLAR: vmsq_from_bus = m.vm[from_bus]**2 vmsq_to_bus = m.vm[to_bus]**2 elif coordinate_type == CoordinateType.RECTANGULAR: vmsq_from_bus = m.vr[from_bus]**2 + m.vj[from_bus]**2 vmsq_to_bus = m.vr[to_bus]**2 + m.vj[to_bus]**2 g = tx_calc.calculate_conductance(branch) b = tx_calc.calculate_susceptance(branch) bc = branch['charging_susceptance'] tau = 1.0 shift = 0.0 if branch['branch_type'] == 'transformer': tau = branch['transformer_tap_ratio'] shift = math.radians(branch['transformer_phase_shift']) g11 = g / tau**2 g12 = g * math.cos(shift) / tau g21 = g * math.sin(shift) / tau g22 = g b11 = (b + bc / 2) / tau**2 b12 = b * math.cos(shift) / tau b21 = b * math.sin(shift) / tau b22 = b + bc / 2 m.eq_pf_branch[branch_name] = \ m.pf[branch_name] == \ g11 * vmsq_from_bus - \ (g12 * m.c[(from_bus,to_bus)] + g21 * m.s[(from_bus,to_bus)] + b12 * m.s[(from_bus,to_bus)] - b21 * m.c[(from_bus,to_bus)]) m.eq_pt_branch[branch_name] = \ m.pt[branch_name] == \ g22 * vmsq_to_bus - \ (g12 * m.c[(from_bus,to_bus)] + g21 * m.s[(from_bus,to_bus)] - b12 * m.s[(from_bus,to_bus)] + b21 * m.c[(from_bus,to_bus)]) m.eq_qf_branch[branch_name] = \ m.qf[branch_name] == \ -b11 * vmsq_from_bus + \ (b12 * m.c[(from_bus,to_bus)] + b21 * m.s[(from_bus,to_bus)] - g12 * m.s[(from_bus,to_bus)] + g21 * m.c[(from_bus,to_bus)]) m.eq_qt_branch[branch_name] = \ m.qt[branch_name] == \ -b22 * vmsq_to_bus + \ (b12 * m.c[(from_bus,to_bus)] + b21 * m.s[(from_bus,to_bus)] + g12 * m.s[(from_bus,to_bus)] - g21 * m.c[(from_bus,to_bus)])
def create_psv_acopf_model(model_data, include_feasibility_slack=False): md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace=True) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') load_attrs = md.attributes(element_type='load') shunt_attrs = md.attributes(element_type='shunt') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, bus_q_loads = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) libbus.declare_var_ql(model, bus_attrs['names'], initialize=bus_q_loads) model.pl.fix() model.ql.fix() ### declare the fixed shunts at the buses bus_bs_fixed_shunts, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts( buses, shunts) ### declare the polar voltages libbus.declare_var_vm(model, bus_attrs['names'], initialize=bus_attrs['vm'], bounds=zip_items(bus_attrs['v_min'], bus_attrs['v_max'])) libbus.declare_expr_vmsq(model=model, index_set=bus_attrs['names'], coordinate_type=CoordinateType.POLAR) va_bounds = {k: (-pi, pi) for k in bus_attrs['va']} libbus.declare_var_va(model, bus_attrs['names'], initialize=bus_attrs['va'], bounds=va_bounds) ### include the feasibility slack for the bus balances p_rhs_kwargs = {} q_rhs_kwargs = {} if include_feasibility_slack: p_rhs_kwargs, q_rhs_kwargs, penalty_expr = _include_feasibility_slack( model, bus_attrs, gen_attrs, bus_p_loads, bus_q_loads) ### fix the reference bus ref_bus = md.data['system']['reference_bus'] ref_angle = md.data['system']['reference_bus_angle'] model.va[ref_bus].fix(radians(ref_angle)) ### declare the generator real and reactive power pg_init = { k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg'] } libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max'])) qg_init = { k: (gen_attrs['q_min'][k] + gen_attrs['q_max'][k]) / 2.0 for k in gen_attrs['qg'] } libgen.declare_var_qg(model, gen_attrs['names'], initialize=qg_init, bounds=zip_items(gen_attrs['q_min'], gen_attrs['q_max'])) ### declare the current flows in the branches vr_init = { k: bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]) for k in bus_attrs['vm'] } vj_init = { k: bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]) for k in bus_attrs['vm'] } s_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} s_lbub = dict() for k in branches.keys(): if s_max[k] is None: s_lbub[k] = (None, None) else: s_lbub[k] = (-s_max[k], s_max[k]) pf_bounds = s_lbub pt_bounds = s_lbub qf_bounds = s_lbub qt_bounds = s_lbub pf_init = dict() pt_init = dict() qf_init = dict() qt_init = dict() for branch_name, branch in branches.items(): from_bus = branch['from_bus'] to_bus = branch['to_bus'] y_matrix = tx_calc.calculate_y_matrix_from_branch(branch) ifr_init = tx_calc.calculate_ifr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) ifj_init = tx_calc.calculate_ifj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itr_init = tx_calc.calculate_itr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itj_init = tx_calc.calculate_itj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) pf_init[branch_name] = tx_calc.calculate_p(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) pt_init[branch_name] = tx_calc.calculate_p(itr_init, itj_init, vr_init[to_bus], vj_init[to_bus]) qf_init[branch_name] = tx_calc.calculate_q(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) qt_init[branch_name] = tx_calc.calculate_q(itr_init, itj_init, vr_init[to_bus], vj_init[to_bus]) libbranch.declare_var_pf(model=model, index_set=branch_attrs['names'], initialize=pf_init, bounds=pf_bounds) libbranch.declare_var_pt(model=model, index_set=branch_attrs['names'], initialize=pt_init, bounds=pt_bounds) libbranch.declare_var_qf(model=model, index_set=branch_attrs['names'], initialize=qf_init, bounds=qf_bounds) libbranch.declare_var_qt(model=model, index_set=branch_attrs['names'], initialize=qt_init, bounds=qt_bounds) ### declare the branch power flow constraints bus_pairs = zip_items(branch_attrs['from_bus'], branch_attrs['to_bus']) unique_bus_pairs = list( OrderedDict((val, None) for idx, val in bus_pairs.items()).keys()) libbranch.declare_expr_c(model=model, index_set=unique_bus_pairs, coordinate_type=CoordinateType.POLAR) libbranch.declare_expr_s(model=model, index_set=unique_bus_pairs, coordinate_type=CoordinateType.POLAR) libbranch.declare_eq_branch_power(model=model, index_set=branch_attrs['names'], branches=branches) ### declare the pq balances libbus.declare_eq_p_balance(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus, **p_rhs_kwargs) libbus.declare_eq_q_balance(model=model, index_set=bus_attrs['names'], bus_q_loads=bus_q_loads, gens_by_bus=gens_by_bus, bus_bs_fixed_shunts=bus_bs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus, **q_rhs_kwargs) ### declare the thermal limits libbranch.declare_ineq_s_branch_thermal_limit( model=model, index_set=branch_attrs['names'], branches=branches, s_thermal_limits=s_max, flow_type=FlowType.POWER) ### declare the voltage min and max inequalities libbus.declare_ineq_vm_bus_lbub(model=model, index_set=bus_attrs['names'], buses=buses, coordinate_type=CoordinateType.POLAR) ### declare angle difference limits on interconnected buses libbranch.declare_ineq_angle_diff_branch_lbub( model=model, index_set=branch_attrs['names'], branches=branches, coordinate_type=CoordinateType.POLAR) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost'], q_costs=gen_attrs.get( 'q_cost', None)) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if include_feasibility_slack: obj_expr += penalty_expr if hasattr(model, 'qg_operating_cost'): obj_expr += sum(model.qg_operating_cost[gen_name] for gen_name in model.qg_operating_cost) model.obj = pe.Objective(expr=obj_expr) return model, md
def create_riv_acopf_model(model_data, include_feasibility_slack=False): md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace=True) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') load_attrs = md.attributes(element_type='load') shunt_attrs = md.attributes(element_type='shunt') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, bus_q_loads = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) libbus.declare_var_ql(model, bus_attrs['names'], initialize=bus_q_loads) model.pl.fix() model.ql.fix() ### declare the fixed shunts at the buses bus_bs_fixed_shunts, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts( buses, shunts) ### declare the rectangular voltages neg_v_max = map_items(op.neg, bus_attrs['v_max']) vr_init = { k: bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]) for k in bus_attrs['vm'] } libbus.declare_var_vr(model, bus_attrs['names'], initialize=vr_init, bounds=zip_items(neg_v_max, bus_attrs['v_max'])) vj_init = { k: bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]) for k in bus_attrs['vm'] } libbus.declare_var_vj(model, bus_attrs['names'], initialize=vj_init, bounds=zip_items(neg_v_max, bus_attrs['v_max'])) ### include the feasibility slack for the bus balances p_rhs_kwargs = {} q_rhs_kwargs = {} if include_feasibility_slack: p_rhs_kwargs, q_rhs_kwargs, penalty_expr = _include_feasibility_slack( model, bus_attrs, gen_attrs, bus_p_loads, bus_q_loads) ### fix the reference bus ref_bus = md.data['system']['reference_bus'] ref_angle = md.data['system']['reference_bus_angle'] if ref_angle != 0.0: libbus.declare_eq_ref_bus_nonzero(model, ref_angle, ref_bus) else: model.vj[ref_bus].fix(0.0) model.vr[ref_bus].setlb(0.0) ### declare the generator real and reactive power pg_init = { k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg'] } libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max'])) qg_init = { k: (gen_attrs['q_min'][k] + gen_attrs['q_max'][k]) / 2.0 for k in gen_attrs['qg'] } libgen.declare_var_qg(model, gen_attrs['names'], initialize=qg_init, bounds=zip_items(gen_attrs['q_min'], gen_attrs['q_max'])) ### declare the current flows in the branches branch_currents = tx_utils.dict_of_branch_currents(branches, buses) s_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} if_bounds = dict() it_bounds = dict() ifr_init = dict() ifj_init = dict() itr_init = dict() itj_init = dict() for branch_name, branch in branches.items(): from_bus = branch['from_bus'] to_bus = branch['to_bus'] y_matrix = tx_calc.calculate_y_matrix_from_branch(branch) ifr_init[branch_name] = tx_calc.calculate_ifr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) ifj_init[branch_name] = tx_calc.calculate_ifj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itr_init[branch_name] = tx_calc.calculate_itr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itj_init[branch_name] = tx_calc.calculate_itj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) if s_max[branch_name] is None: if_bounds[branch_name] = (None, None) it_bounds[branch_name] = (None, None) else: if_max = s_max[branch_name] / buses[branches[branch_name] ['from_bus']]['v_min'] it_max = s_max[branch_name] / buses[branches[branch_name] ['to_bus']]['v_min'] if_bounds[branch_name] = (-if_max, if_max) it_bounds[branch_name] = (-it_max, it_max) libbranch.declare_var_ifr(model=model, index_set=branch_attrs['names'], initialize=ifr_init, bounds=if_bounds) libbranch.declare_var_ifj(model=model, index_set=branch_attrs['names'], initialize=ifj_init, bounds=if_bounds) libbranch.declare_var_itr(model=model, index_set=branch_attrs['names'], initialize=itr_init, bounds=it_bounds) libbranch.declare_var_itj(model=model, index_set=branch_attrs['names'], initialize=itj_init, bounds=it_bounds) ir_init = dict() ij_init = dict() for bus_name, bus in buses.items(): ir_expr = sum([ ifr_init[branch_name] for branch_name in outlet_branches_by_bus[bus_name] ]) ir_expr += sum([ itr_init[branch_name] for branch_name in inlet_branches_by_bus[bus_name] ]) ij_expr = sum([ ifj_init[branch_name] for branch_name in outlet_branches_by_bus[bus_name] ]) ij_expr += sum([ itj_init[branch_name] for branch_name in inlet_branches_by_bus[bus_name] ]) if bus_gs_fixed_shunts[bus_name] != 0.0: ir_expr += bus_gs_fixed_shunts[bus_name] * vr_init[bus_name] ij_expr += bus_gs_fixed_shunts[bus_name] * vj_init[bus_name] if bus_bs_fixed_shunts[bus_name] != 0.0: ir_expr += bus_bs_fixed_shunts[bus_name] * vj_init[bus_name] ij_expr += bus_bs_fixed_shunts[bus_name] * vr_init[bus_name] ir_init[bus_name] = ir_expr ij_init[bus_name] = ij_expr # TODO: Implement better bounds (?) for these aggregated variables -- note, these are unbounded in old Egret libbus.declare_var_ir_aggregation_at_bus(model=model, index_set=bus_attrs['names'], initialize=ir_init, bounds=(None, None)) libbus.declare_var_ij_aggregation_at_bus(model=model, index_set=bus_attrs['names'], initialize=ij_init, bounds=(None, None)) ### declare the branch current flow constraints libbranch.declare_eq_branch_current(model=model, index_set=branch_attrs['names'], branches=branches) ### declare the ir/ij_aggregation constraints libbus.declare_eq_i_aggregation_at_bus( model=model, index_set=bus_attrs['names'], bus_bs_fixed_shunts=bus_bs_fixed_shunts, bus_gs_fixed_shunts=bus_gs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus) ### declare the pq balances libbus.declare_eq_p_balance_with_i_aggregation( model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, **p_rhs_kwargs) libbus.declare_eq_q_balance_with_i_aggregation( model=model, index_set=bus_attrs['names'], bus_q_loads=bus_q_loads, gens_by_bus=gens_by_bus, **q_rhs_kwargs) ### declare the thermal limits libbranch.declare_ineq_s_branch_thermal_limit( model=model, index_set=branch_attrs['names'], branches=branches, s_thermal_limits=s_max, flow_type=FlowType.CURRENT) ### declare the voltage min and max inequalities libbus.declare_ineq_vm_bus_lbub(model=model, index_set=bus_attrs['names'], buses=buses, coordinate_type=CoordinateType.RECTANGULAR) ### declare angle difference limits on interconnected buses libbranch.declare_ineq_angle_diff_branch_lbub( model=model, index_set=branch_attrs['names'], branches=branches, coordinate_type=CoordinateType.RECTANGULAR) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost'], q_costs=gen_attrs.get( 'q_cost', None)) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if include_feasibility_slack: obj_expr += penalty_expr if hasattr(model, 'qg_operating_cost'): obj_expr += sum(model.qg_operating_cost[gen_name] for gen_name in model.qg_operating_cost) model.obj = pe.Objective(expr=obj_expr) return model, md
def create_btheta_dcopf_model(model_data, include_angle_diff_limits=False, include_feasibility_slack=False): md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace = True) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, _ = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) model.pl.fix() ### declare the fixed shunts at the buses _, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts(buses, shunts) ### declare the polar voltages va_bounds = {k: (-pi, pi) for k in bus_attrs['va']} libbus.declare_var_va(model, bus_attrs['names'], initialize=bus_attrs['va'], bounds=va_bounds ) ### include the feasibility slack for the bus balances p_rhs_kwargs = {} penalty_expr = None if include_feasibility_slack: p_rhs_kwargs, penalty_expr = _include_feasibility_slack(model, bus_attrs, gen_attrs, bus_p_loads) ### fix the reference bus ref_bus = md.data['system']['reference_bus'] ref_angle = md.data['system']['reference_bus_angle'] model.va[ref_bus].fix(radians(ref_angle)) ### declare the generator real power pg_init = {k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg']} libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max']) ) ### declare the current flows in the branches vr_init = {k: bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]) for k in bus_attrs['vm']} vj_init = {k: bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]) for k in bus_attrs['vm']} p_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} p_lbub = {k: (-p_max[k],p_max[k]) for k in branches.keys()} pf_bounds = p_lbub pf_init = dict() for branch_name, branch in branches.items(): from_bus = branch['from_bus'] to_bus = branch['to_bus'] y_matrix = tx_calc.calculate_y_matrix_from_branch(branch) ifr_init = tx_calc.calculate_ifr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) ifj_init = tx_calc.calculate_ifj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) pf_init[branch_name] = tx_calc.calculate_p(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) libbranch.declare_var_pf(model=model, index_set=branch_attrs['names'], initialize=pf_init, bounds=pf_bounds ) ### declare the branch power flow approximation constraints libbranch.declare_eq_branch_power_btheta_approx(model=model, index_set=branch_attrs['names'], branches=branches ) ### declare the p balance libbus.declare_eq_p_balance_dc_approx(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus, approximation_type=ApproximationType.BTHETA, **p_rhs_kwargs ) ### declare the real power flow limits libbranch.declare_ineq_p_branch_thermal_lbub(model=model, index_set=branch_attrs['names'], branches=branches, p_thermal_limits=p_max, approximation_type=ApproximationType.BTHETA ) ### declare angle difference limits on interconnected buses if include_angle_diff_limits: libbranch.declare_ineq_angle_diff_branch_lbub(model=model, index_set=branch_attrs['names'], branches=branches, coordinate_type=CoordinateType.POLAR ) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost'] ) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if include_feasibility_slack: obj_expr += penalty_expr model.obj = pe.Objective(expr=obj_expr) return model, md
def create_ptdf_dcopf_model(model_data, include_feasibility_slack=False,base_point=BasePointType.FLATSTART): md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace = True) data_utils.create_dicts_of_ptdf(md,base_point=base_point) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') load_attrs = md.attributes(element_type='load') shunt_attrs = md.attributes(element_type='shunt') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, _ = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) model.pl.fix() ### declare the fixed shunts at the buses _, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts(buses, shunts) ### declare the generator real power pg_init = {k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg']} libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max']) ) ### include the feasibility slack for the system balance p_rhs_kwargs = {} if include_feasibility_slack: p_rhs_kwargs, penalty_expr = _include_system_feasibility_slack(model, gen_attrs, bus_p_loads) ### declare the current flows in the branches vr_init = {k: bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]) for k in bus_attrs['vm']} vj_init = {k: bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]) for k in bus_attrs['vm']} p_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} p_lbub = {k: (-p_max[k],p_max[k]) for k in branches.keys()} pf_bounds = p_lbub pf_init = dict() for branch_name, branch in branches.items(): from_bus = branch['from_bus'] to_bus = branch['to_bus'] y_matrix = tx_calc.calculate_y_matrix_from_branch(branch) ifr_init = tx_calc.calculate_ifr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) ifj_init = tx_calc.calculate_ifj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) pf_init[branch_name] = tx_calc.calculate_p(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) libbranch.declare_var_pf(model=model, index_set=branch_attrs['names'], initialize=pf_init, bounds=pf_bounds ) ### declare the branch power flow approximation constraints libbranch.declare_eq_branch_power_ptdf_approx(model=model, index_set=branch_attrs['names'], branches=branches, buses=buses, bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts ) ### declare the p balance libbus.declare_eq_p_balance_ed(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, **p_rhs_kwargs ) ### declare the real power flow limits libbranch.declare_ineq_p_branch_thermal_lbub(model=model, index_set=branch_attrs['names'], branches=branches, p_thermal_limits=p_max, approximation_type=ApproximationType.PTDF ) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost'] ) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if include_feasibility_slack: obj_expr += penalty_expr model.obj = pe.Objective(expr=obj_expr) return model, md
def create_ptdf_losses_dcopf_model(model_data, include_feasibility_slack=False, ptdf_options=None): if ptdf_options is None: ptdf_options = dict() lpu.populate_default_ptdf_options(ptdf_options) baseMVA = model_data.data['system']['baseMVA'] lpu.check_and_scale_ptdf_options(ptdf_options, baseMVA) md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace=True) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') load_attrs = md.attributes(element_type='load') shunt_attrs = md.attributes(element_type='shunt') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, _ = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) model.pl.fix() ### declare the fixed shunts at the buses _, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts(buses, shunts) ### declare the generator real power pg_init = { k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg'] } libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max'])) ### include the feasibility slack for the system balance p_rhs_kwargs = {} if include_feasibility_slack: p_rhs_kwargs, penalty_expr = _include_system_feasibility_slack( model, gen_attrs, bus_p_loads) ### declare net withdraw expression for use in PTDF power flows libbus.declare_expr_p_net_withdraw_at_bus( model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, ) ### declare the current flows in the branches p_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} pfl_bounds = {k: (-p_max[k]**2, p_max[k]**2) for k in branches.keys()} pfl_init = {k: 0 for k in branches.keys()} ## Do and store PTDF calculation reference_bus = md.data['system']['reference_bus'] ## We'll assume we have a solution to initialize from base_point = BasePointType.SOLUTION PTDF = data_utils.PTDFLossesMatrix(branches, buses, reference_bus, base_point) model._PTDF = PTDF model._ptdf_options = ptdf_options libbranch.declare_expr_pf( model=model, index_set=branch_attrs['names'], ) libbranch.declare_var_pfl(model=model, index_set=branch_attrs['names'], initialize=pfl_init, bounds=pfl_bounds) ### declare the branch power flow approximation constraints libbranch.declare_eq_branch_power_ptdf_approx( model=model, index_set=branch_attrs['names'], PTDF=PTDF, abs_ptdf_tol=ptdf_options['abs_ptdf_tol'], rel_ptdf_tol=ptdf_options['rel_ptdf_tol'], ) ### declare the branch power loss approximation constraints libbranch.declare_eq_branch_loss_ptdf_approx( model=model, index_set=branch_attrs['names'], PTDF=PTDF, abs_ptdf_tol=ptdf_options['abs_ptdf_tol'], rel_ptdf_tol=ptdf_options['rel_ptdf_tol'], ) ### declare the p balance libbus.declare_eq_p_balance_ed(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, include_losses=branch_attrs['names'], **p_rhs_kwargs) ### declare the real power flow limits libbranch.declare_ineq_p_branch_thermal_lbub( model=model, index_set=branch_attrs['names'], branches=branches, p_thermal_limits=p_max, approximation_type=ApproximationType.PTDF) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost']) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if include_feasibility_slack: obj_expr += penalty_expr model.obj = pe.Objective(expr=obj_expr) return model, md
def create_btheta_dcopf_model(model_data): md = tx_utils.scale_ModelData_to_pu(model_data) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') load_attrs = md.attributes(element_type='load') shunt_attrs = md.attributes(element_type='shunt') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_in_service_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, _ = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) model.pl.fix() ### declare the fixed shunts at the buses _, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts(buses, shunts) ### declare the polar voltages va_bounds = {k: (-pi, pi) for k in bus_attrs['va']} libbus.declare_var_va(model, bus_attrs['names'], initialize=bus_attrs['va'], bounds=va_bounds) ### fix the reference bus ref_bus = md.data['system']['reference_bus'] model.va[ref_bus].fix(0.0) ref_angle = md.data['system']['reference_bus_angle'] if ref_angle != 0.0: raise ValueError('The BTHETA DCOPF formulation currently only supports' ' a reference bus angle of 0 degrees, but an angle' ' of {} degrees was found.'.format(ref_angle)) ### declare the generator real power pg_init = { k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg'] } libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max'])) ### declare the current flows in the branches vr_init = { k: bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]) for k in bus_attrs['vm'] } vj_init = { k: bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]) for k in bus_attrs['vm'] } p_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} p_lbub = {k: (-p_max[k], p_max[k]) for k in branches.keys()} pf_bounds = p_lbub pf_init = dict() for branch_name, branch in branches.items(): from_bus = branch['from_bus'] to_bus = branch['to_bus'] y_matrix = tx_calc.calculate_y_matrix_from_branch(branch) ifr_init = tx_calc.calculate_ifr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) ifj_init = tx_calc.calculate_ifj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) pf_init[branch_name] = tx_calc.calculate_p(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) libbranch.declare_var_pf(model=model, index_set=branch_attrs['names'], initialize=pf_init, bounds=pf_bounds) ### declare the branch power flow approximation constraints libbranch.declare_eq_branch_power_dc_approx( model=model, index_set=branch_attrs['names'], branches=branches, approximation_type=ApproximationType.BTHETA) ### declare the p balance libbus.declare_eq_p_balance_dc_approx( model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus, approximation_type=ApproximationType.BTHETA) ### declare the real power flow limits libbranch.declare_ineq_p_branch_thermal_lbub( model=model, index_set=branch_attrs['names'], branches=branches, p_thermal_limits=p_max, approximation_type=ApproximationType.BTHETA) ### declare angle difference limits on interconnected buses libbranch.declare_ineq_angle_diff_branch_lbub( model=model, index_set=branch_attrs['names'], branches=branches, coordinate_type=CoordinateType.POLAR) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost']) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) model.obj = pe.Objective(expr=obj_expr) return model
def create_socp_acopf_model(model_data, include_feasibility_slack=False): md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace = True) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') load_attrs = md.attributes(element_type='load') shunt_attrs = md.attributes(element_type='shunt') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, bus_q_loads = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) libbus.declare_var_ql(model, bus_attrs['names'], initialize=bus_q_loads) model.pl.fix() model.ql.fix() ### declare the fixed shunts at the buses bus_bs_fixed_shunts, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts(buses, shunts) ### declare the rectangular voltages neg_v_max = map_items(op.neg, bus_attrs['v_max']) vr_init = {k: bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]) for k in bus_attrs['vm']} # libbus.declare_var_vr(model, bus_attrs['names'], initialize=vr_init, # bounds=zip_items(neg_v_max, bus_attrs['v_max']) # ) vj_init = {k: bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]) for k in bus_attrs['vm']} # libbus.declare_var_vj(model, bus_attrs['names'], initialize=vj_init, # bounds=zip_items(neg_v_max, bus_attrs['v_max']) # ) # w variable for socp vj2_init = {k: (bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]))**2 for k in bus_attrs['vm']} vr2_init = {k: (bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]))**2 for k in bus_attrs['vm']} w_init = {k: vr2_init[k]+vj2_init[k] for k in vj2_init} wub = {k:bus_attrs['v_max'][k]**2 for k in bus_attrs['v_max']} wlb = {k:bus_attrs['v_min'][k]**2 for k in bus_attrs['v_min']} ## v_min**2 <= w <= v_max**2 libbus.declare_var_w(model, bus_attrs['names'], initialize = w_init, bounds =zip_items(wlb,wub)) ### include the feasibility slack for the bus balances p_rhs_kwargs = {} q_rhs_kwargs = {} if include_feasibility_slack: p_rhs_kwargs, q_rhs_kwargs, penalty_expr = _include_feasibility_slack(model, bus_attrs, gen_attrs, bus_p_loads, bus_q_loads) ### fix the reference bus # ref_bus = md.data['system']['reference_bus'] # ref_angle = md.data['system']['reference_bus_angle'] # if ref_angle != 0.0: # libbus.declare_eq_ref_bus_nonzero(model, ref_angle, ref_bus) # else: # model.vj[ref_bus].fix(0.0) # model.vr[ref_bus].setlb(0.0) ### declare the generator real and reactive power pg_init = {k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg']} libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max']) ) qg_init = {k: (gen_attrs['q_min'][k] + gen_attrs['q_max'][k]) / 2.0 for k in gen_attrs['qg']} libgen.declare_var_qg(model, gen_attrs['names'], initialize=qg_init, bounds=zip_items(gen_attrs['q_min'], gen_attrs['q_max']) ) ### declare the current flows in the branches s_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} s_lbub = dict() for k in branches.keys(): if s_max[k] is None: s_lbub[k] = (None, None) else: s_lbub[k] = (-s_max[k],s_max[k]) pf_bounds = s_lbub pt_bounds = s_lbub qf_bounds = s_lbub qt_bounds = s_lbub pf_init = dict() pt_init = dict() qf_init = dict() qt_init = dict() cbk_init = dict() sbk_init = dict() cbk_bounds = dict() sbk_bounds = dict() for branch_name, branch in branches.items(): from_bus = branch['from_bus'] to_bus = branch['to_bus'] ba_max = branch['angle_diff_max'] ba_min = branch['angle_diff_min'] y_matrix = tx_calc.calculate_y_matrix_from_branch(branch) ifr_init = tx_calc.calculate_ifr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) ifj_init = tx_calc.calculate_ifj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itr_init = tx_calc.calculate_itr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itj_init = tx_calc.calculate_itj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) pf_init[branch_name] = tx_calc.calculate_p(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) pt_init[branch_name] = tx_calc.calculate_p(itr_init, itj_init, vr_init[to_bus], vj_init[to_bus]) qf_init[branch_name] = tx_calc.calculate_q(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) qt_init[branch_name] = tx_calc.calculate_q(itr_init, itj_init, vr_init[to_bus], vj_init[to_bus]) #SOCP related variable bounds and init cbk_init[from_bus,to_bus] = vr_init[from_bus]*vr_init[to_bus] + vj_init[from_bus]*vj_init[to_bus] sbk_init[from_bus,to_bus] = vr_init[from_bus]*vj_init[to_bus] - vr_init[to_bus]*vj_init[from_bus] if ba_max is None and ba_min is None: cbk_bounds[from_bus,to_bus] = ( bus_attrs['v_min'][from_bus]*bus_attrs['v_min'][to_bus]*min(pe.cos(-math.pi/2),pe.cos(math.pi/2)), bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*1.0) sbk_bounds[from_bus,to_bus] = ( bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.sin(-math.pi/2), bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.sin(math.pi/2)) if ba_max > 0 and ba_min < 0: cbk_bounds[from_bus,to_bus] = (bus_attrs['v_min'][from_bus]*bus_attrs['v_min'][to_bus]*min(pe.cos(ba_max * math.pi/180),pe.cos(ba_min * math.pi/180)), bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*1.0) sbk_bounds[from_bus,to_bus] = ( bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.sin(ba_min * math.pi/180), bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.sin(math.pi/2)) if ba_max <= 0: cbk_bounds[from_bus,to_bus] = (bus_attrs['v_min'][from_bus]*bus_attrs['v_min'][to_bus]*pe.cos(ba_min * math.pi/180), bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.cos(ba_max * math.pi/180)) sbk_bounds[from_bus,to_bus] = ( bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.sin(ba_min * math.pi/180), bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.sin(math.pi/2)) if ba_min >= 0: cbk_bounds[from_bus,to_bus] = (bus_attrs['v_min'][from_bus]*bus_attrs['v_min'][to_bus]*pe.cos(ba_max * math.pi/180), bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.cos(ba_min * math.pi/180)) sbk_bounds[from_bus,to_bus] = ( bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.sin(ba_min * math.pi/180), bus_attrs['v_max'][from_bus]*bus_attrs['v_max'][to_bus]*pe.sin(math.pi/2)) #print(bus_attrs) #print(branch_attrs) libbranch.declare_var_pf(model=model, index_set=branch_attrs['names'], initialize=pf_init, bounds=pf_bounds ) libbranch.declare_var_pt(model=model, index_set=branch_attrs['names'], initialize=pt_init, bounds=pt_bounds ) libbranch.declare_var_qf(model=model, index_set=branch_attrs['names'], initialize=qf_init, bounds=qf_bounds ) libbranch.declare_var_qt(model=model, index_set=branch_attrs['names'], initialize=qt_init, bounds=qt_bounds ) bus_pairs = zip_items(branch_attrs['from_bus'],branch_attrs['to_bus']) unique_bus_pairs = list(set([val for idx,val in bus_pairs.items()])) libbranch.declare_var_c(model = model, index_set = unique_bus_pairs, initialize = cbk_init, bounds = cbk_bounds ) libbranch.declare_var_s(model = model, index_set = unique_bus_pairs, initialize = sbk_init, bounds = sbk_bounds ) ### declare the branch power flow constraints libbranch.declare_eq_branch_power_socp(model=model, index_set=branch_attrs['names'], branches=branches, branch_attrs=branch_attrs, coordinate_type=CoordinateType.RECTANGULAR ) ### declare the pq balances libbus.declare_eq_p_balance_socp(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus, coordinate_type=CoordinateType.RECTANGULAR, **p_rhs_kwargs ) libbus.declare_eq_q_balance_socp(model=model, index_set=bus_attrs['names'], bus_q_loads=bus_q_loads, gens_by_bus=gens_by_bus, bus_bs_fixed_shunts=bus_bs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus, coordinate_type=CoordinateType.RECTANGULAR, **q_rhs_kwargs ) ### declare the thermal limits libbranch.declare_ineq_s_branch_thermal_limit(model=model, index_set=branch_attrs['names'], branches=branches, s_thermal_limits=s_max, flow_type=FlowType.POWER ) ### declare the voltage min and max inequalities # libbus.declare_ineq_vm_bus_lbub(model=model, # index_set=bus_attrs['names'], # buses=buses, # coordinate_type=CoordinateType.RECTANGULAR # ) ### declare angle difference limits on interconnected buses # libbranch.declare_ineq_angle_diff_branch_lbub(model=model, # index_set=branch_attrs['names'], # branches=branches, # coordinate_type=CoordinateType.RECTANGULAR # ) libbranch.declare_socp_scw(model = model, index_set = branch_attrs['names'], branches = branches, branch_attrs = branch_attrs) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost'], q_costs=gen_attrs.get('q_cost', None) ) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if include_feasibility_slack: obj_expr += penalty_expr if hasattr(model, 'qg_operating_cost'): obj_expr += sum(model.qg_operating_cost[gen_name] for gen_name in model.qg_operating_cost) model.obj = pe.Objective(expr=obj_expr) return model, md
def create_copperplate_dispatch_approx_model(model_data, include_feasibility_slack=False): md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace=True) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, _ = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) model.pl.fix() ### declare the fixed shunts at the buses _, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts(buses, shunts) ### declare the generator real power pg_init = { k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg'] } libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max'])) ### include the feasibility slack for the system balance p_rhs_kwargs = {} if include_feasibility_slack: p_rhs_kwargs, penalty_expr = _include_system_feasibility_slack( model, gen_attrs, bus_p_loads) ### declare the p balance libbus.declare_eq_p_balance_ed(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, **p_rhs_kwargs) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost']) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if include_feasibility_slack: obj_expr += penalty_expr model.obj = pe.Objective(expr=obj_expr) return model, md
def create_rsv_acopf_model(model_data): md = tx_utils.scale_ModelData_to_pu(model_data) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') bus_attrs = md.attributes(element_type='bus') branch_attrs = md.attributes(element_type='branch') load_attrs = md.attributes(element_type='load') shunt_attrs = md.attributes(element_type='shunt') inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_in_service_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, bus_q_loads = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, bus_attrs['names'], initialize=bus_p_loads) libbus.declare_var_ql(model, bus_attrs['names'], initialize=bus_q_loads) model.pl.fix() model.ql.fix() ### declare the fixed shunts at the buses bus_bs_fixed_shunts, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts( buses, shunts) ### declare the rectangular voltages neg_v_max = map_items(op.neg, bus_attrs['v_max']) vr_init = { k: bus_attrs['vm'][k] * pe.cos(bus_attrs['va'][k]) for k in bus_attrs['vm'] } libbus.declare_var_vr(model, bus_attrs['names'], initialize=vr_init, bounds=zip_items(neg_v_max, bus_attrs['v_max'])) vj_init = { k: bus_attrs['vm'][k] * pe.sin(bus_attrs['va'][k]) for k in bus_attrs['vm'] } libbus.declare_var_vj(model, bus_attrs['names'], initialize=vj_init, bounds=zip_items(neg_v_max, bus_attrs['v_max'])) ### fix the reference bus ref_bus = md.data['system']['reference_bus'] model.vj[ref_bus].fix(0.0) model.vr[ref_bus].setlb(0.0) ref_angle = md.data['system']['reference_bus_angle'] if ref_angle != 0.0: raise ValueError('The RSV ACOPF formulation currently only supports' ' a reference bus angle of 0 degrees, but an angle' ' of {} degrees was found.'.format(ref_angle)) ### declare the generator real and reactive power pg_init = { k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg'] } libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max'])) qg_init = { k: (gen_attrs['q_min'][k] + gen_attrs['q_max'][k]) / 2.0 for k in gen_attrs['qg'] } libgen.declare_var_qg(model, gen_attrs['names'], initialize=qg_init, bounds=zip_items(gen_attrs['q_min'], gen_attrs['q_max'])) ### declare the current flows in the branches s_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} s_lbub = {k: (-s_max[k], s_max[k]) for k in branches.keys()} pf_bounds = s_lbub pt_bounds = s_lbub qf_bounds = s_lbub qt_bounds = s_lbub pf_init = dict() pt_init = dict() qf_init = dict() qt_init = dict() for branch_name, branch in branches.items(): from_bus = branch['from_bus'] to_bus = branch['to_bus'] y_matrix = tx_calc.calculate_y_matrix_from_branch(branch) ifr_init = tx_calc.calculate_ifr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) ifj_init = tx_calc.calculate_ifj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itr_init = tx_calc.calculate_itr(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) itj_init = tx_calc.calculate_itj(vr_init[from_bus], vj_init[from_bus], vr_init[to_bus], vj_init[to_bus], y_matrix) pf_init[branch_name] = tx_calc.calculate_p(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) pt_init[branch_name] = tx_calc.calculate_p(itr_init, itj_init, vr_init[to_bus], vj_init[to_bus]) qf_init[branch_name] = tx_calc.calculate_q(ifr_init, ifj_init, vr_init[from_bus], vj_init[from_bus]) qt_init[branch_name] = tx_calc.calculate_q(itr_init, itj_init, vr_init[to_bus], vj_init[to_bus]) libbranch.declare_var_pf(model=model, index_set=branch_attrs['names'], initialize=pf_init, bounds=pf_bounds) libbranch.declare_var_pt(model=model, index_set=branch_attrs['names'], initialize=pt_init, bounds=pt_bounds) libbranch.declare_var_qf(model=model, index_set=branch_attrs['names'], initialize=qf_init, bounds=qf_bounds) libbranch.declare_var_qt(model=model, index_set=branch_attrs['names'], initialize=qt_init, bounds=qt_bounds) ### declare the branch power flow constraints libbranch.declare_eq_branch_power( model=model, index_set=branch_attrs['names'], branches=branches, branch_attrs=branch_attrs, coordinate_type=CoordinateType.RECTANGULAR) ### declare the pq balances libbus.declare_eq_p_balance(model=model, index_set=bus_attrs['names'], bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus, coordinate_type=CoordinateType.RECTANGULAR) libbus.declare_eq_q_balance(model=model, index_set=bus_attrs['names'], bus_q_loads=bus_q_loads, gens_by_bus=gens_by_bus, bus_bs_fixed_shunts=bus_bs_fixed_shunts, inlet_branches_by_bus=inlet_branches_by_bus, outlet_branches_by_bus=outlet_branches_by_bus, coordinate_type=CoordinateType.RECTANGULAR) ### declare the thermal limits libbranch.declare_ineq_s_branch_thermal_limit( model=model, index_set=branch_attrs['names'], branches=branches, s_thermal_limits=s_max, flow_type=FlowType.POWER) ### declare the voltage min and max inequalities libbus.declare_ineq_vm_bus_lbub(model=model, index_set=bus_attrs['names'], buses=buses, coordinate_type=CoordinateType.RECTANGULAR) ### declare angle difference limits on interconnected buses libbranch.declare_ineq_angle_diff_branch_lbub( model=model, index_set=branch_attrs['names'], branches=branches, coordinate_type=CoordinateType.RECTANGULAR) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost'], q_costs=gen_attrs.get( 'q_cost', None)) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if hasattr(model, 'qg_operating_cost'): obj_expr += sum(model.qg_operating_cost[gen_name] for gen_name in model.qg_operating_cost) model.obj = pe.Objective(expr=obj_expr) return model
def create_ptdf_dcopf_model(model_data, include_feasibility_slack=False, base_point=BasePointType.FLATSTART, ptdf_options=None): ptdf_options = lpu.populate_default_ptdf_options(ptdf_options) baseMVA = model_data.data['system']['baseMVA'] lpu.check_and_scale_ptdf_options(ptdf_options, baseMVA) md = model_data.clone_in_service() tx_utils.scale_ModelData_to_pu(md, inplace = True) gens = dict(md.elements(element_type='generator')) buses = dict(md.elements(element_type='bus')) branches = dict(md.elements(element_type='branch')) loads = dict(md.elements(element_type='load')) shunts = dict(md.elements(element_type='shunt')) gen_attrs = md.attributes(element_type='generator') ## to keep things in order buses_idx = tuple(buses.keys()) branches_idx = tuple(branches.keys()) inlet_branches_by_bus, outlet_branches_by_bus = \ tx_utils.inlet_outlet_branches_by_bus(branches, buses) gens_by_bus = tx_utils.gens_by_bus(buses, gens) model = pe.ConcreteModel() ### declare (and fix) the loads at the buses bus_p_loads, _ = tx_utils.dict_of_bus_loads(buses, loads) libbus.declare_var_pl(model, buses_idx, initialize=bus_p_loads) model.pl.fix() ### declare the fixed shunts at the buses _, bus_gs_fixed_shunts = tx_utils.dict_of_bus_fixed_shunts(buses, shunts) ### declare the generator real power pg_init = {k: (gen_attrs['p_min'][k] + gen_attrs['p_max'][k]) / 2.0 for k in gen_attrs['pg']} libgen.declare_var_pg(model, gen_attrs['names'], initialize=pg_init, bounds=zip_items(gen_attrs['p_min'], gen_attrs['p_max']) ) ### include the feasibility slack for the system balance p_rhs_kwargs = {} if include_feasibility_slack: p_rhs_kwargs, penalty_expr = _include_system_feasibility_slack(model, gen_attrs, bus_p_loads) ### declare the p balance libbus.declare_eq_p_balance_ed(model=model, index_set=buses_idx, bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, **p_rhs_kwargs ) ### declare net withdraw expression for use in PTDF power flows libbus.declare_expr_p_net_withdraw_at_bus(model=model, index_set=buses_idx, bus_p_loads=bus_p_loads, gens_by_bus=gens_by_bus, bus_gs_fixed_shunts=bus_gs_fixed_shunts, ) ### add "blank" power flow expressions libbranch.declare_expr_pf(model=model, index_set=branches_idx, ) ## Do and store PTDF calculation reference_bus = md.data['system']['reference_bus'] PTDF = data_utils.get_ptdf_potentially_from_file(ptdf_options, branches_idx, buses_idx) if PTDF is None: PTDF = data_utils.PTDFMatrix(branches, buses, reference_bus, base_point, ptdf_options, branches_keys=branches_idx, buses_keys=buses_idx) model._PTDF = PTDF model._ptdf_options = ptdf_options data_utils.write_ptdf_potentially_to_file(ptdf_options, PTDF) if ptdf_options['lazy']: ### add "blank" real power flow limits libbranch.declare_ineq_p_branch_thermal_lbub(model=model, index_set=branches_idx, branches=branches, p_thermal_limits=None, approximation_type=None, ) ### add helpers for tracking monitored branches lpu.add_monitored_branch_tracker(model) else: p_max = {k: branches[k]['rating_long_term'] for k in branches.keys()} ## add all the constraints ### declare the branch power flow approximation constraints libbranch.declare_eq_branch_power_ptdf_approx(model=model, index_set=branches_idx, PTDF=PTDF, abs_ptdf_tol=ptdf_options['abs_ptdf_tol'], rel_ptdf_tol=ptdf_options['rel_ptdf_tol'], ) ### add all the limits libbranch.declare_ineq_p_branch_thermal_lbub(model=model, index_set=branches_idx, branches=branches, p_thermal_limits=p_max, approximation_type=ApproximationType.PTDF, ) ### declare the generator cost objective libgen.declare_expression_pgqg_operating_cost(model=model, index_set=gen_attrs['names'], p_costs=gen_attrs['p_cost'] ) obj_expr = sum(model.pg_operating_cost[gen_name] for gen_name in model.pg_operating_cost) if include_feasibility_slack: obj_expr += penalty_expr model.obj = pe.Objective(expr=obj_expr) return model, md