def test_bus_indexes(self): """ Test the from/to bus index property. """ c = Case(name="c") bus1 = Bus(name="Bus 1") bus2 = Bus(name="Bus 2") bus3 = Bus(name="Bus 3") c.buses = [bus1, bus2, bus3] # Append to list. branch1 = Branch(bus3, bus1) c.branches.append(branch1) self.assertEqual(c.buses.index(branch1.from_bus), 2) self.assertEqual(c.buses.index(branch1.to_bus), 0) # Set list. branch2 = Branch(bus2, bus3) branch3 = Branch(bus2, bus1) c.branches = [branch2, branch3] self.assertEqual(c.buses.index(branch2.from_bus), 1) self.assertEqual(c.buses.index(branch2.to_bus), 2) # Move branch. branch2.from_bus = bus1 self.assertEqual(c.buses.index(branch2.from_bus), 0)
def __init__(self, voltage=satu, busa=None, busb=None, busc=None, base_kv=1.0, base_mva=1.0, ps=0, loads=[]): Bus.__init__(self) self.Busa = busa self.Busb = busb self.Busc = busc self.base_kv = base_kv * ones(3) self.base_mva = base_mva self.typeG = normalNode self.loadS = zeros(3, dtype=complex) # self.loads = loads self.gens = [] self.sourceS = zeros(3) # please assume generation as negative load self.shuntS = zeros(3) self.E = satu * A # balanced unity voltages. E is line to neutral voltage self.connected_from_branch = [ ] # self is connected to root from branch self.connected_to_branch = [] # self is connected to branch self.loadI = zeros(3) self.sourceI = zeros(3) self.childI = zeros(3) self.shuntI = zeros(3) self.totalI = zeros(3) self.Iqpv = zeros( 3) # the reactive current injected by a generator in pv-node
def setUp(self): """ The test runner will execute this method prior to each test. """ g0_points = [(0.0, 0.0), (12.0, 240.0), (36.0, 1200.0), (60.0, 2400.0)] g1_points = [(0.0, 0.0), (12.0, 240.0), (36.0, 1200.0), (60.0, 2400.0)] g2_points = [(-30.0, 0.0), (-20.0, 1000.0), (-10., 2000.), (0., 3000.)] g3_points = [(0.0, 0.0), (12.0, 240.0), (36.0, 1200.0), (60.0, 2400.0)] g4_points = [(-30.0, 0.0), (-20.0, 1000.0), (-10., 2000.), (0., 3000.)] g0 = Generator(Bus(), p=10.0, q=0.0, q_max=60.0, q_min=-15.0, p_max=60.0, p_min=10.0, p_cost=g0_points) g1 = Generator( Bus(), p=10.0, q=0.0, q_max=60.0, q_min=-15.0, p_max=60.0, p_min=12.0, # c_startup=100.0, p_cost=g1_points) g2 = Generator(Bus(), p=-30.0, q=-15.0, q_max=0.0, q_min=-15.0, p_max=0.0, p_min=-30.0, p_cost=g2_points) # vload g3 = Generator(Bus(), p=10.0, q=0.0, q_max=60.0, q_min=-15.0, p_max=60.0, p_min=12.0, p_cost=g3_points) g4 = Generator(Bus(), p=-30.0, q=7.5, q_max=7.5, q_min=0.0, p_max=0.0, p_min=-30.0, p_cost=g4_points) self.all_generators = [g0, g1, g2, g3, g4] self.generators = [g for g in self.all_generators if not g.is_load] self.vloads = [g for g in self.all_generators if g.is_load]
def test_poly_to_pwl(self): """ Test cost model conversion from polynomial to piece-wise linear. """ g = Generator(Bus(), p_min=0.0, p_max=80.0, p_cost=(0.02, 2.0, 0.0)) g.poly_to_pwl(n_points=10) self.assertEqual(g.pcost_model, PW_LINEAR) self.assertEqual(len(g.p_cost), 10) self.assertAlmostEqual(g.p_cost[2][0], 17.78, places=2) self.assertAlmostEqual(g.p_cost[2][1], 41.88, places=2) self.assertAlmostEqual(g.p_cost[6][0], 53.33, places=2) self.assertAlmostEqual(g.p_cost[6][1], 163.56, places=2) g.p_min = 10.0 g.pcost_model = POLYNOMIAL g.p_cost = (0.02, 2.0, 0.0) g.poly_to_pwl(n_points=10) self.assertEqual(len(g.p_cost), 10) self.assertAlmostEqual(g.p_cost[1][0], 10.0, places=2) self.assertAlmostEqual(g.p_cost[1][1], 22.0, places=2) self.assertAlmostEqual(g.p_cost[9][0], 80.0, places=2) self.assertAlmostEqual(g.p_cost[9][1], 288.0, places=2)
def test_total_polynomial_cost(self): """ Test total cost calculation with polynomial cost model. """ g = Generator(Bus(), p_max=100.0, p_min=20.0, p_cost=(0.06, 0.6, 6.0)) self.assertEqual(g.pcost_model, POLYNOMIAL) self.assertEqual(g.total_cost(5.0), 10.5) self.assertEqual(g.total_cost(6.0), 11.76)
def get1Bus(): """ Returns a simple one bus case. """ bus1 = Bus(name="Bus1", p_demand=80.0) g1 = Generator(bus1, name="G1", p_max=60.0, p_min=0.0) g2 = Generator(bus1, name="G2", p_max=100.0, p_min=0.0) return Case(name="1Bus", buses=[bus1], generators=[g1, g2])
def test_reset(self): """ Test initialisation of bus result attributes. """ bus = Bus() bus.v_magnitude = 0.95 bus.v_angle = 15.0 bus.p_lmbda = 50.0 bus.q_lmbda = 20.0 bus.mu_vmin = 10.0 bus.mu_vmax = 10.0 bus.reset() self.assertEqual(bus.v_magnitude, 0.95) self.assertEqual(bus.v_angle, 15.0) self.assertEqual(bus.p_lmbda, 0.0) self.assertEqual(bus.q_lmbda, 0.0) self.assertEqual(bus.mu_vmin, 0.0) self.assertEqual(bus.mu_vmax, 0.0)
def test_total_piecewise_linear_cost(self): """ Test total cost calculation with piecewise linear cost model. """ p0 = (0.0, 0.0) p1 = (40.0, 100.0) p2 = (100.0, 400.0) g = Generator(Bus(), p_max=100.0, p_min=20.0, p_cost=[p0, p1, p2]) self.assertEqual(g.pcost_model, PW_LINEAR) self.assertAlmostEqual(g.total_cost(30.0), 75.0, places=4) self.assertAlmostEqual(g.total_cost(60.0), 200.0, places=4)
def __init__(self,voltage=satu,busa=None,busb=None,busc=None,base_kv=1.0,base_mva=1.0,ps=0,loads=[]): Bus.__init__(self) self.Busa=busa self.Busb=busb self.Busc=busc self.base_kv=base_kv*ones(3) self.base_mva=base_mva self.typeG=normalNode self.loadS=zeros(3,dtype=complex) # self.loads=loads self.gens=[] self.sourceS=zeros(3) # please assume generation as negative load self.shuntS=zeros(3) self.E=satu*A# balanced unity voltages. E is line to neutral voltage self.connected_from_branch=[] # self is connected to root from branch self.connected_to_branch=[] # self is connected to branch self.loadI=zeros(3) self.sourceI=zeros(3) self.childI=zeros(3) self.shuntI=zeros(3) self.totalI=zeros(3) self.Iqpv=zeros(3) # the reactive current injected by a generator in pv-node
def push_bus(self, tokens): """ Adds a Bus object to the case. """ logger.debug("Pushing bus data: %s" % tokens) bus = Bus() bus.name = tokens["bus_no"] bus.v_magnitude = tokens["v_magnitude"] bus.v_angle = tokens["v_angle"] bus.v_magnitude = tokens["v_magnitude"] bus.v_angle = tokens["v_angle"] self.case.buses.append(bus)
def test_reset(self): """ Test initialisation of bus result attributes. """ branch = Branch(Bus(), Bus()) branch.p_from = 25.0 branch.p_to = -25.0 branch.q_from = -9.0 branch.q_to = 9.0 branch.mu_s_from = 90.0 branch.mu_s_to = 0.0 branch.mu_angmin = 60.0 branch.mu_angmax = 0.0 branch.reset() self.assertEqual(branch.p_from, 0.0) self.assertEqual(branch.p_to, 0.0) self.assertEqual(branch.q_from, 0.0) self.assertEqual(branch.q_to, 0.0) self.assertEqual(branch.mu_s_from, 0.0) self.assertEqual(branch.mu_s_to, 0.0) self.assertEqual(branch.mu_angmin, 0.0) self.assertEqual(branch.mu_angmax, 0.0)
def forwardSweep(self): self.updateCoefficients(self) self.VL=(self.Vs - self.b*self.IL)/self.a def backwardSweep(self): self.updateCoefficients(self) self.Is=self.c*self.VL + self.d*self.IL def updateVset(self,Vset): self.Vset=Vset self.Vset_min= self.Vset-self.bandwidth/2. self.Vset_max= self.Vset+self.bandwidth/2. # Testing block used during implementation process. # 1. based on example 7.4 on page 170 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ busa=Bus() busb=Bus() ## Substation transformer data kVArated=5000 connection='DYg' Vprimary=115 Vsecondary=4.16 ## The equivalent line impedance from regulator to the load center Z=0.3 +1j*0.9 Vs = 4160./sqrt(3) # the rated line-to-ground voltage ot the substation transformer
# Pylon Tutorial "Power Flow" # # Author: Richard Lincoln, [email protected] #------------------------------------------------------------------------------ """ Import "sys" so the report can be written to stdout. """ import sys """ Import the logging module """ import logging """ and set up a basic configuration. """ logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) """ The "pylon" package contains classes for defining a power system model and power flow solvers. """ from pylon import \ Case, Bus, Branch, Generator, NewtonPF, FastDecoupledPF, REFERENCE """ Start by building up a one branch case with two generators """ bus1 = Bus(type=REFERENCE) g1 = Generator(bus1, p=80.0, q=10.0) """ and fixed load at the other. """ bus2 = Bus(p_demand=60.0, q_demand=4.0) g2 = Generator(bus2, p=20.0, q=0.0) """ Connect the two buses """ line = Branch(bus1, bus2, r=0.05, x=0.01) """ and add it all to a new case. """ case = Case(buses=[bus1, bus2], branches=[line], generators=[g1, g2]) """ Choose to solve using either Fast Decoupled method """ solver = FastDecoupledPF(case) """ or Newton's method """ solver = NewtonPF(case) """ and then call the solver. """ solver.solve() """ Write the case out to view the results. """
First import the necessary components from Pylon. """ from pylon import Case, Bus, Branch, Generator, OPF, REFERENCE """ Import "sys" for writing to stdout. """ import sys """ Import the logging module """ import logging """ and set up a basic configuration. """ logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) """ Create two generators, specifying their marginal cost. """ bus1 = Bus(p_demand=100.0, type=REFERENCE) g1 = Generator(bus1, p_min=0.0, p_max=80.0, p_cost=[(0., 0.), (80., 4800.)]) bus2 = Bus() g2 = Generator(bus2, p_min=0.0, p_max=60.0, p_cost=[(0., 0.), (60., 4500.)]) """ Connect the two generator buses """ line = Branch(bus1, bus2, r=0.05, x=0.25, b=0.06) """ and add it all to a case. """ case = Case(buses=[bus1, bus2], branches=[line], generators=[g1, g2]) """ Non-linear AC optimal power flow """ dc = False """ or linearised DC optimal power flow may be selected. """ dc = True
from pyreto import \ MarketExperiment, ParticipantEnvironment, ProfitTask, SmartMarket from pyreto.renderer import ExperimentRenderer from pybrain.tools.shortcuts import buildNetwork from pybrain.rl.agents import LearningAgent from pybrain.rl.learners import ENAC logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(levelname)s: %(message)s") """ Create a simple case. """ g1 = Generator(name="G1", p_max=60.0, p_min=0.0) g2 = Generator(name="G2", p_max=100.0, p_min=0.0) bus1 = Bus(name="Bus1", generators=[g1, g2], p_demand=80.0, q_demand=0.0) case = Case(name="1Bus", buses=[bus1]) """ The market will clear submitted offers/bids and return dispatch info. """ mkt = SmartMarket(case) agents = [] tasks = [] for g in bus1.generators: """ Create an environment for each agent with an asset and a market. """ env = ParticipantEnvironment(g, mkt, n_offbids=2) """ Create a task for the agent to achieve. """ task = ProfitTask(env) """ Build an artificial neural network for the agent. """ net = buildNetwork(task.outdim, task.indim, bias=False, outputbias=False) # net._setParameters(array([9])) """ Create a learning agent with a learning algorithm. """
def _parse_file(self, file): """ Parses the given file. """ case = Case() file.seek(0) case.base_mva = float(file.next().split(",")[1].split("/")[0]) case.name = "%s %s" % (file.next().strip(), file.next().strip()) bustype_map = {1: "PQ", 2: "PV", 3: "ref", 4: "isolated"} # I, 'NAME', BASKV, IDE, GL, BL, AREA, ZONE, VM, VA, OWNER bus_data = file.next().split(",") while bus_data[0].strip()[0] != "0": bus = Bus() i = int(bus_data[0].strip()) self.bus_map[i] = bus bus._i = i bus.name = bus_data[1].strip("'").strip() bus.v_base = float(bus_data[2]) bus.type = bustype_map[int(bus_data[3])] bus.g_shunt = float(bus_data[4]) bus.b_shunt = float(bus_data[5]) bus.v_magnitude = float(bus_data[8]) bus.v_angle = float(bus_data[9]) case.buses.append(bus) bus_data = file.next().split(",") # I, ID, STATUS, AREA, ZONE, PL, QL, IP, IQ, YP, YQ, OWNER load_data = file.next().split(",") while load_data[0].strip()[0] != "0": bus = self.bus_map[int(load_data[0].strip())] bus.p_demand += float(load_data[5]) bus.q_demand += float(load_data[6]) load_data = file.next().split(",") #I,ID,PG,QG,QT,QB,VS,IREG,MBASE,ZR,ZX,RT,XT,GTAP,STAT,RMPCT,PT,PB,O1,F1 gen_data = file.next().split(",") while gen_data[0].strip()[0] != "0": bus = self.bus_map[int(gen_data[0].strip())] g = Generator(bus) g.p = float(gen_data[2]) g.q = float(gen_data[3]) g.q_max = float(gen_data[4]) g.q_min = float(gen_data[5]) g.v_magnitude = float(gen_data[6]) g.base_mva = float(gen_data[8]) g.online = bool(int(gen_data[14])) g.p_max = float(gen_data[16]) g.p_min = float(gen_data[17]) case.generators.append(g) gen_data = file.next().split(",") # I,J,CKT,R,X,B,RATEA,RATEB,RATEC,GI,BI,GJ,BJ,ST,LEN,O1,F1,...,O4,F4 branch_data = file.next().split(",") while branch_data[0].strip()[0] != "0": from_bus = self.bus_map[abs(int(branch_data[0]))] to_bus = self.bus_map[abs(int(branch_data[1]))] l = Branch(from_bus, to_bus) l.r = float(branch_data[3]) l.x = float(branch_data[4]) l.b = float(branch_data[5]) l.rate_a = float(branch_data[6]) l.rate_b = float(branch_data[7]) l.rate_c = float(branch_data[8]) # l.online = bool(int(branch_data[13])) case.branches.append(l) branch_data = file.next().split(",") # I,J,K,CKT,CW,CZ,CM,MAG1,MAG2,NMETR,'NAME',STAT,O1,F1,...,O4,F4 # R1-2,X1-2,SBASE1-2 # WINDV1,NOMV1,ANG1,RATA1,RATB1,RATC1,COD1,CONT1,RMA1,RMI1,VMA1,VMI1,NTP1,TAB1,CR1,CX1 # WINDV2,NOMV2 trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": trx_data2 = file.next().split(",") trx_data3 = file.next().split(",") trx_data4 = file.next().split(",") # second winding if len(trx_data2) < 5: from_bus = self.bus_map[abs(int(trx_data[0]))] to_bus = self.bus_map[abs(int(trx_data[1]))] l = Branch(from_bus, to_bus) l.name = trx_data[10].strip("'").strip() l.online = bool(int(trx_data[11])) l.b = float(trx_data[8]) l.r = float(trx_data2[0]) l.x = float(trx_data2[1]) l.ratio = float(trx_data3[0]) l.phase_shift = float(trx_data3[2]) rate_a = float(trx_data3[3]) if rate_a != 0.0: l.rate_a = rate_a rate_b = float(trx_data3[4]) if rate_b != 0.0: l.rate_b = rate_b rate_c = float(trx_data3[5]) if rate_c != 0.0: l.rate_c = rate_c case.branches.append(l) trx_data = file.next().split(",") else: # I,J,K,CKT,CW,CZ,CM,MAG1,MAG2,NMETR,'NAME',STAT,O1,F1,...,O4,F4 # R1-2,X1-2,SBASE1-2,R2-3,X2-3,SBASE2-3,R3-1,X3-1,SBASE3-1,VMSTAR,ANSTAR # WINDV1,NOMV1,ANG1,RATA1,RATB1,RATC1,COD1,CONT1,RMA1,RMI1,VMA1,VMI1,NTP1,TAB1,CR1,CX1 # WINDV2,NOMV2,ANG2,RATA2,RATB2,RATC2,COD2,CONT2,RMA2,RMI2,VMA2,VMI2,NTP2,TAB2,CR2,CX2 # WINDV3,NOMV3,ANG3,RATA3,RATB3,RATC3,COD3,CONT3,RMA3,RMI3,VMA3,VMI3,NTP3,TAB3,CR3,CX3 trx_data5 = file.next().split(",") # third winding # Three-winding transformers are modelled as a group of three # two-winding transformers with a fictitious neutral bus. tmp_bus = Bus() tmp_bus.name = "n" + tmp_bus.name tmp_bus._i = len(case.buses) + 1 bus1 = self.bus_map[abs(int(trx_data[0]))] bus2 = self.bus_map[abs(int(trx_data[1]))] bus3 = self.bus_map[abs(int(trx_data[2]))] l1 = Branch(tmp_bus, bus1) l2 = Branch(tmp_bus, bus2) l3 = Branch(tmp_bus, bus3) b = float(trx_data[8]) # MAG2 l1.b = b # / 3.0 # l2.b = b / 3.0 # l3.b = b / 3.0 on = bool(int(trx_data[11])) l1.online = on l2.online = on l3.online = on r12 = float(trx_data2[0]) x12 = float(trx_data2[1]) r23 = float(trx_data2[3]) x23 = float(trx_data2[4]) r31 = float(trx_data2[6]) x31 = float(trx_data2[7]) l1.r = 0.5 * (r12 + r31 - r23) l1.x = 0.5 * (x12 + x31 - x23) l2.r = 0.5 * (r12 + r23 - r31) l2.x = 0.5 * (x12 + x23 - x31) l3.r = 0.5 * (r23 + r31 - r12) l3.x = 0.5 * (x23 + x31 - x12) for l in [l1, l2, l3]: if abs(l.x) < 1e-5: logger.warning("Zero branch reactance [%s]." % l.name) l.x = self.xtol if abs(complex(l.r, l.x)) < 0.00001: logger.warning("Zero branch impedance [%s]." % l.name) l1.ratio = float(trx_data3[0]) l1.phase_shift = float(trx_data3[2]) l2.ratio = float(trx_data4[0]) l2.phase_shift = float(trx_data4[2]) l3.ratio = float(trx_data5[0]) l3.phase_shift = float(trx_data5[2]) rate_a1 = float(trx_data3[3]) rate_b1 = float(trx_data3[4]) rate_c1 = float(trx_data3[5]) if rate_a1 > 0.0: l1.rate_a = rate_a1 if rate_b1 > 0.0: l1.rate_b = rate_b1 if rate_c1 > 0.0: l1.rate_c = rate_c1 rate_a2 = float(trx_data4[3]) rate_b2 = float(trx_data4[4]) rate_c2 = float(trx_data4[5]) if rate_a2 > 0.0: l2.rate_a = rate_a2 if rate_b2 > 0.0: l2.rate_b = rate_b2 if rate_c2 > 0.0: l2.rate_c = rate_c2 rate_a3 = float(trx_data5[3]) rate_b3 = float(trx_data5[4]) rate_c3 = float(trx_data5[5]) if rate_a3 > 0.0: l3.rate_a = rate_a3 if rate_b2 > 0.0: l3.rate_b = rate_b3 if rate_c2 > 0.0: l3.rate_c = rate_c3 case.buses.append(tmp_bus) case.branches.append(l1) case.branches.append(l2) case.branches.append(l3) trx_data = file.next().split(",") # Area interchange data. # I, ISW, PDES, PTOL, 'ARNAME' trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring area interchange data.") trx_data = file.next().split(",") # Two-terminal DC line data. # I,MDC,RDC,SETVL,VSCHD,VCMOD,RCOMP,DELTI,METER,DCVMIN,CCCITMX,CCCACC # IPR,NBR,ALFMX,ALFMN,RCR,XCR,EBASR,TRR,TAPR,TMXR,TMNR,STPR,ICR,IFR,ITR,IDR,XCAPR # IPI,NBI,GAMMX,GAMMN,RCI,XCI,EBASI,TRI,TAPI,TMXI,TMNI,STPI,ICI,IFI,ITI,IDI,XCAPI trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring two-terminal DC line data.") trx_data = file.next().split(",") # VSC DC line data. # 'NAME', MDC, RDC, O1, F1, ... O4, F4 # IBUS,TYPE,MODE,DOCET,ACSET,ALOSS,BLOSS,MINOSS,SMAX,IMAX,PWF,MAXQ,MINQ, # REMOT,RMPCT trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring VSC DC line data.") trx_data = file.next().split(",") # Switched shunt data. # I,MODSW,VSWHI,VSWLO,SWREM,RMPCT,'RMIDNT',BINIT,N1,B1,N2,B2,...N8,B8 trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": bus = self.bus_map[abs(int(trx_data[0]))] bus.b_shunt += float(trx_data[7]) trx_data = file.next().split(",") # Transformer impedance correction table. # I, T1, F1, T2, F2, T3, F3, ... T11, F11 trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring transformer X correction table data.") trx_data = file.next().split(",") # Multi-terminal dc line data. # I, NCONV, NDCBS, NDCLN, MDC, VCONV, VCMOD, VCONVN # IB,N,ANGMX,ANGMN,RC,XC,EBAS,TR,TAP,TPMX,TPMN,TSTP,SETVL,DCPF,MARG,CNVCOD # IDC, IB, IA, ZONE, 'NAME', IDC2, RGRND, OWNER # IDC, JDC, DCCKT, RDC, LDC trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring multi-terminal dc line data.") trx_data = file.next().split(",") # Multisection line data. # I,J,ID,DUM1,DUM2,...DUM9 trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring multisection line data.") trx_data = file.next().split(",") # Zone data. # I,'ZONAME' trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring zone data.") trx_data = file.next().split(",") # Interarea transfer data. # ARFROM, ARTO, TRID, PTRAN trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring interarea transfer data.") trx_data = file.next().split(",") # Owner data. # I,'OWNAME' trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring owner data.") trx_data = file.next().split(",") # FACTS device data. # N,I,J,MODE,PDES,QDES,VSET,SHMX,TRMX,VTMN,VTMX,VSMX,IMX,LINX,RMPCT,OWNER,SET1,SET2,VSREF trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring FACTS device data.") trx_data = file.next().split(",") return case
def test_offers(self): """ Test conversion of cost function to price/quantity offers. case6ww.m q = 50.0000 37.5000 37.5000 37.5000 37.5000 37.5000 28.1250 28.1250 28.1250 28.1250 45.0000 33.7500 33.7500 33.7500 33.7500 p = 11.9355 12.4019 12.8016 13.2014 13.6011 10.6664 11.2498 11.7498 12.2499 12.7500 11.1665 11.7500 12.2502 12.7503 13.2505 case30pwl.m q = 12 24 24 12 24 24 12 24 24 12 24 24 12 24 24 12 24 24 p = 12 36 76 20 44 84 20 44 84 12 36 76 20 44 84 12 36 76 """ places = 4 g = Generator(Bus(), p_min=50.0, p_max=200.0) g.pcost_model = "poly" g.p_cost = (0.00533, 11.669, 213.1) poly_offers = g.get_offers() self.assertEqual(len(poly_offers), 5) self.assertAlmostEqual(poly_offers[0].quantity, 50.0, places) self.assertAlmostEqual(poly_offers[0].price, 11.9355, places) self.assertAlmostEqual(poly_offers[3].quantity, 37.5, places) self.assertAlmostEqual(poly_offers[3].price, 13.2014, places) g = Generator(Bus(), p_min=0.0, p_max=80.0) g.pcost_model = "pwl" g.p_cost = [(0, 0), (12, 144), (36, 1008), (60, 2832)] pwl_offers = g.get_offers() self.assertEqual(len(pwl_offers), 3) self.assertAlmostEqual(pwl_offers[0].quantity, 12.0, places) self.assertAlmostEqual(pwl_offers[0].price, 12.0, places) self.assertAlmostEqual(pwl_offers[2].quantity, 24.0, places) self.assertAlmostEqual(pwl_offers[2].price, 76.0, places)
from VoltReg3 import VoltReg3, VoltReg1 from tool import * import inspect thisfilename = inspect.getfile(inspect.currentframe()) print '\n\nExecuting:', thisfilename a = cos(2 * pi / 3) + 1j * sin(2 * pi / 3) A = array([1.0, a, a * a]) / 3.0 # creating three 120-shifted unity voltage zeroload = Load3() # default load MVAbase = 6.0 kVbase = 12.47 / sqrt(3) # line-to-ground kV Zbase = kVbase**2 / MVAbase #in the present of transformer, zbase as well as kVbase on both side of transformer are differents. # Buses busa = Bus() bus1 = RootBus3(busa, busa, busa, base_kv=kVbase) bus2 = Bus3(busa, busa, busa, base_kv=kVbase) bus3 = Bus3(busa, busa, busa, base_kv=kVbase) bus4 = Bus3(busa, busa, busa, base_kv=kVbase) busvr = Bus3(busa, busa, busa, base_kv=kVbase) bus5 = Bus3(busa, busa, busa, base_kv=kVbase) # load at bus4 ##Sa=.75/MVAbase ##pfa=0.85 ##Sb=1./MVAbase ##pfb=0.9 ##Sc=1.25/MVAbase ##pfc=0.95
def _parse_file(self, file): """ Parses the given file. """ case = Case() file.seek(0) case.base_mva = float(file.next().split(",")[1].split("/")[0]) case.name = "%s %s" % (file.next().strip(), file.next().strip()) bustype_map = {1: "PQ", 2: "PV", 3: "ref", 4: "isolated"} # I, 'NAME', BASKV, IDE, GL, BL, AREA, ZONE, VM, VA, OWNER bus_data = file.next().split(",") while bus_data[0].strip()[0] != "0": bus = Bus() i = int(bus_data[0].strip()) self.bus_map[i] = bus bus._i = i bus.name = bus_data[1].strip("'").strip() bus.v_base = float(bus_data[2]) bus.type = bustype_map[int(bus_data[3])] bus.g_shunt = float(bus_data[4]) bus.b_shunt = float(bus_data[5]) bus.v_magnitude = float(bus_data[8]) bus.v_angle = float(bus_data[9]) # bus.area = 1; # hcui7 added case.buses.append(bus) bus_data = file.next().split(",") # I, ID, STATUS, AREA, ZONE, PL, QL, IP, IQ, YP, YQ, OWNER load_data = file.next().split(",") while load_data[0].strip()[0] != "0": bus = self.bus_map[int(load_data[0].strip())] bus.p_demand += float(load_data[5]) bus.q_demand += float(load_data[6]) load_data = file.next().split(",") # I,ID,PG,QG,QT,QB,VS,IREG,MBASE,ZR,ZX,RT,XT,GTAP,STAT,RMPCT,PT,PB,O1,F1 gen_data = file.next().split(",") while gen_data[0].strip()[0] != "0": bus = self.bus_map[int(gen_data[0].strip())] g = Generator(bus) g.p = float(gen_data[2]) g.q = float(gen_data[3]) g.q_max = float(gen_data[4]) g.q_min = float(gen_data[5]) g.v_magnitude = float(gen_data[6]) g.base_mva = float(gen_data[8]) g.online = bool(int(gen_data[14])) g.p_max = float(gen_data[16]) g.p_min = float(gen_data[17]) case.generators.append(g) gen_data = file.next().split(",") # I,J,CKT,R,X,B,RATEA,RATEB,RATEC,GI,BI,GJ,BJ,ST,LEN,O1,F1,...,O4,F4 branch_data = file.next().split(",") while branch_data[0].strip()[0] != "0": from_bus = self.bus_map[abs(int(branch_data[0]))] to_bus = self.bus_map[abs(int(branch_data[1]))] l = Branch(from_bus, to_bus) l.r = float(branch_data[3]) l.x = float(branch_data[4]) l.b = float(branch_data[5]) l.rate_a = float(branch_data[6]) l.rate_b = float(branch_data[7]) l.rate_c = float(branch_data[8]) # l.online = bool(int(branch_data[13])) case.branches.append(l) branch_data = file.next().split(",") # I,J,K,CKT,CW,CZ,CM,MAG1,MAG2,NMETR,'NAME',STAT,O1,F1,...,O4,F4 # R1-2,X1-2,SBASE1-2 # WINDV1,NOMV1,ANG1,RATA1,RATB1,RATC1,COD1,CONT1,RMA1,RMI1,VMA1,VMI1,NTP1,TAB1,CR1,CX1 # WINDV2,NOMV2 trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": trx_data2 = file.next().split(",") trx_data3 = file.next().split(",") trx_data4 = file.next().split(",") # second winding if len(trx_data2) < 5: from_bus = self.bus_map[abs(int(trx_data[0]))] to_bus = self.bus_map[abs(int(trx_data[1]))] l = Branch(from_bus, to_bus) l.name = trx_data[10].strip("'").strip() l.online = bool(int(trx_data[11])) l.b = float(trx_data[8]) l.r = float(trx_data2[0]) l.x = float(trx_data2[1]) l.ratio = float(trx_data3[0]) l.phase_shift = float(trx_data3[2]) rate_a = float(trx_data3[3]) if rate_a != 0.0: l.rate_a = rate_a rate_b = float(trx_data3[4]) if rate_b != 0.0: l.rate_b = rate_b rate_c = float(trx_data3[5]) if rate_c != 0.0: l.rate_c = rate_c case.branches.append(l) trx_data = file.next().split(",") else: # I,J,K,CKT,CW,CZ,CM,MAG1,MAG2,NMETR,'NAME',STAT,O1,F1,...,O4,F4 # R1-2,X1-2,SBASE1-2,R2-3,X2-3,SBASE2-3,R3-1,X3-1,SBASE3-1,VMSTAR,ANSTAR # WINDV1,NOMV1,ANG1,RATA1,RATB1,RATC1,COD1,CONT1,RMA1,RMI1,VMA1,VMI1,NTP1,TAB1,CR1,CX1 # WINDV2,NOMV2,ANG2,RATA2,RATB2,RATC2,COD2,CONT2,RMA2,RMI2,VMA2,VMI2,NTP2,TAB2,CR2,CX2 # WINDV3,NOMV3,ANG3,RATA3,RATB3,RATC3,COD3,CONT3,RMA3,RMI3,VMA3,VMI3,NTP3,TAB3,CR3,CX3 trx_data5 = file.next().split(",") # third winding # Three-winding transformers are modelled as a group of three # two-winding transformers with a fictitious neutral bus. tmp_bus = Bus() tmp_bus.name = "n" + tmp_bus.name tmp_bus._i = len(case.buses) + 1 bus1 = self.bus_map[abs(int(trx_data[0]))] bus2 = self.bus_map[abs(int(trx_data[1]))] bus3 = self.bus_map[abs(int(trx_data[2]))] l1 = Branch(tmp_bus, bus1) l2 = Branch(tmp_bus, bus2) l3 = Branch(tmp_bus, bus3) b = float(trx_data[8]) # MAG2 l1.b = b # / 3.0 # l2.b = b / 3.0 # l3.b = b / 3.0 on = bool(int(trx_data[11])) l1.online = on l2.online = on l3.online = on r12 = float(trx_data2[0]) x12 = float(trx_data2[1]) r23 = float(trx_data2[3]) x23 = float(trx_data2[4]) r31 = float(trx_data2[6]) x31 = float(trx_data2[7]) l1.r = 0.5 * (r12 + r31 - r23) l1.x = 0.5 * (x12 + x31 - x23) l2.r = 0.5 * (r12 + r23 - r31) l2.x = 0.5 * (x12 + x23 - x31) l3.r = 0.5 * (r23 + r31 - r12) l3.x = 0.5 * (x23 + x31 - x12) for l in [l1, l2, l3]: if abs(l.x) < 1e-5: logger.warning("Zero branch reactance [%s]." % l.name) l.x = self.xtol if abs(complex(l.r, l.x)) < 0.00001: logger.warning("Zero branch impedance [%s]." % l.name) l1.ratio = float(trx_data3[0]) l1.phase_shift = float(trx_data3[2]) l2.ratio = float(trx_data4[0]) l2.phase_shift = float(trx_data4[2]) l3.ratio = float(trx_data5[0]) l3.phase_shift = float(trx_data5[2]) rate_a1 = float(trx_data3[3]) rate_b1 = float(trx_data3[4]) rate_c1 = float(trx_data3[5]) if rate_a1 > 0.0: l1.rate_a = rate_a1 if rate_b1 > 0.0: l1.rate_b = rate_b1 if rate_c1 > 0.0: l1.rate_c = rate_c1 rate_a2 = float(trx_data4[3]) rate_b2 = float(trx_data4[4]) rate_c2 = float(trx_data4[5]) if rate_a2 > 0.0: l2.rate_a = rate_a2 if rate_b2 > 0.0: l2.rate_b = rate_b2 if rate_c2 > 0.0: l2.rate_c = rate_c2 rate_a3 = float(trx_data5[3]) rate_b3 = float(trx_data5[4]) rate_c3 = float(trx_data5[5]) if rate_a3 > 0.0: l3.rate_a = rate_a3 if rate_b2 > 0.0: l3.rate_b = rate_b3 if rate_c2 > 0.0: l3.rate_c = rate_c3 case.buses.append(tmp_bus) case.branches.append(l1) case.branches.append(l2) case.branches.append(l3) trx_data = file.next().split(",") # Area interchange data. # I, ISW, PDES, PTOL, 'ARNAME' trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring area interchange data.") trx_data = file.next().split(",") # Two-terminal DC line data. # I,MDC,RDC,SETVL,VSCHD,VCMOD,RCOMP,DELTI,METER,DCVMIN,CCCITMX,CCCACC # IPR,NBR,ALFMX,ALFMN,RCR,XCR,EBASR,TRR,TAPR,TMXR,TMNR,STPR,ICR,IFR,ITR,IDR,XCAPR # IPI,NBI,GAMMX,GAMMN,RCI,XCI,EBASI,TRI,TAPI,TMXI,TMNI,STPI,ICI,IFI,ITI,IDI,XCAPI trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring two-terminal DC line data.") trx_data = file.next().split(",") # VSC DC line data. # 'NAME', MDC, RDC, O1, F1, ... O4, F4 # IBUS,TYPE,MODE,DOCET,ACSET,ALOSS,BLOSS,MINOSS,SMAX,IMAX,PWF,MAXQ,MINQ, # REMOT,RMPCT trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring VSC DC line data.") trx_data = file.next().split(",") # Switched shunt data. # I,MODSW,VSWHI,VSWLO,SWREM,RMPCT,'RMIDNT',BINIT,N1,B1,N2,B2,...N8,B8 trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": bus = self.bus_map[abs(int(trx_data[0]))] bus.b_shunt += float(trx_data[7]) trx_data = file.next().split(",") # Transformer impedance correction table. # I, T1, F1, T2, F2, T3, F3, ... T11, F11 trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring transformer X correction table data.") trx_data = file.next().split(",") # Multi-terminal dc line data. # I, NCONV, NDCBS, NDCLN, MDC, VCONV, VCMOD, VCONVN # IB,N,ANGMX,ANGMN,RC,XC,EBAS,TR,TAP,TPMX,TPMN,TSTP,SETVL,DCPF,MARG,CNVCOD # IDC, IB, IA, ZONE, 'NAME', IDC2, RGRND, OWNER # IDC, JDC, DCCKT, RDC, LDC trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring multi-terminal dc line data.") trx_data = file.next().split(",") # Multisection line data. # I,J,ID,DUM1,DUM2,...DUM9 trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring multisection line data.") trx_data = file.next().split(",") # Zone data. # I,'ZONAME' trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring zone data.") trx_data = file.next().split(",") # Interarea transfer data. # ARFROM, ARTO, TRID, PTRAN trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring interarea transfer data.") trx_data = file.next().split(",") # Owner data. # I,'OWNAME' trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring owner data.") trx_data = file.next().split(",") # FACTS device data. # N,I,J,MODE,PDES,QDES,VSET,SHMX,TRMX,VTMN,VTMX,VSMX,IMX,LINX,RMPCT,OWNER,SET1,SET2,VSREF trx_data = file.next().split(",") while trx_data[0].strip()[0] != "0": logger.warning("Ignoring FACTS device data.") trx_data = file.next().split(",") return case