Beispiel #1
0
    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)
Beispiel #2
0
    def test_infer_matpower_format(self):
        """ Test inference of MATPOWER format from file extension.
        """
        case = Case.load(MP_DATA_FILE) # Format not specified.

        self.assertEqual(len(case.generators), 3)
        self.assertTrue(isinstance(case, Case))
Beispiel #3
0
    def test_load_matpower(self):
        """ Test loading a MATPOWER data file.
        """
        case = Case.load(MP_DATA_FILE, "matpower")

        self.assertEqual(len(case.generators), 3)
        self.assertTrue(isinstance(case, Case))
Beispiel #4
0
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])
Beispiel #5
0
 def setUp(self):
     """ The test runner will execute this method prior to each test.
     """
     self.case = Case.load(join(DATA_DIR, self.case_name,
                                self.case_name + ".pkl"))
     self.case.sort_generators() # ext2int
     self.opf = OPF(self.case, dc=False)
     self.om = self.opf._construct_opf_model(self.case)
     self.solver = PIPSSolver(self.om)
Beispiel #6
0
    def setUp(self):
        """ The test runner will execute this method prior to each test.
        """
        self.case = Case.load(DATA_FILE)

        generators = self.case.generators

        self.offers = [
            Offer(generators[0], 12.0, 20.0),
            Offer(generators[0], 24.0, 50.0),
            Offer(generators[0], 24.0, 60.0),

            Offer(generators[1], 12.0, 20.0),
            Offer(generators[1], 24.0, 40.0),
            Offer(generators[1], 24.0, 70.0),

            Offer(generators[2], 12.0, 20.0),
            Offer(generators[2], 24.0, 42.0),
            Offer(generators[2], 24.0, 80.0),

            Offer(generators[3], 12.0, 20.0),
            Offer(generators[3], 24.0, 44.0),
            Offer(generators[3], 24.0, 90.0),

            Offer(generators[4], 12.0, 20.0),
            Offer(generators[4], 24.0, 46.0),
            Offer(generators[4], 24.0, 75.0),

            Offer(generators[5], 12.0, 20.0),
            Offer(generators[5], 24.0, 48.0),
            Offer(generators[5], 24.0, 60.0)
        ]

        self.bids = [
            Bid(generators[6], 10.0, 100.0),
            Bid(generators[6], 10.0, 70.0),
            Bid(generators[6], 10.0, 60.0),

            Bid(generators[7], 10.0, 100.0),
            Bid(generators[7], 10.0, 50.0),
            Bid(generators[7], 10.0, 20.0),

            Bid(generators[8], 10.0, 100.0),
            Bid(generators[8], 10.0, 60.0),
            Bid(generators[8], 10.0, 50.0)
        ]

        self.mkt = SmartMarket(self.case, self.offers, self.bids,
            locationalAdjustment='dc', auctionType=FIRST_PRICE, priceCap=100.0)
Beispiel #7
0
    def read(self, file_or_filename):
        """ Parses a PSAT data file and returns a case object

            file_or_filename: File object or path to PSAT data file
            return: Case object
        """
        self.file_or_filename = file_or_filename

        logger.info("Parsing PSAT case file [%s]." % file_or_filename)

        t0 = time.time()

        self.case = Case()

        # Name the case
        if isinstance(file_or_filename, basestring):
            name, _ = splitext(basename(file_or_filename))
        else:
            name, _ = splitext(file_or_filename.name)

        self.case.name = name

        bus_array = self._get_bus_array_construct()
        line_array = self._get_line_array_construct()
        # TODO: Lines.con - Alternative line data format
        slack_array = self._get_slack_array_construct()
        pv_array = self._get_pv_array_construct()
        pq_array = self._get_pq_array_construct()
        demand_array = self._get_demand_array_construct()
        supply_array = self._get_supply_array_construct()
        # TODO: Varname.bus (Bus names)

        # Pyparsing case:
        case = \
            ZeroOrMore(matlab_comment) + bus_array + \
            ZeroOrMore(matlab_comment) + line_array + \
            ZeroOrMore(matlab_comment) + slack_array + \
            ZeroOrMore(matlab_comment) + pv_array + \
            ZeroOrMore(matlab_comment) + pq_array + \
            ZeroOrMore(matlab_comment) + demand_array + \
            ZeroOrMore(matlab_comment) + supply_array

        case.parseFile(file_or_filename)

        elapsed = time.time() - t0
        logger.info("PSAT case file parsed in %.3fs." % elapsed)

        return self.case
Beispiel #8
0
def getPickledCase():
    """ Returns a test power system case.
    """
    # Read case from data file.
    return Case.load(DATA_FILE)
Beispiel #9
0
# 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. """
case.save_rst(sys.stdout)
Beispiel #10
0
__author__ = 'Richard Lincoln, [email protected]'
""" This example demonstrates profiling an OPF problem. """

from os.path import join, dirname

import cProfile
import pstats

import pylon.case
from pylon import Case, OPF  #@UnusedImport

# Define a path to the data file.
CASE_FILE = join(dirname(pylon.case.__file__), "test", "data", "case30pwl.pkl")

# Load the data file.
case = Case.load(CASE_FILE)

cProfile.run("OPF(case, dc=False, opt={'verbose': True}).solve()", "opf_prof")

p = pstats.Stats("opf_prof")
#p.sort_stats('name').print_stats()
p.sort_stats('cumulative').print_stats(20)
#p.sort_stats('time').print_stats(20)
#p.sort_stats('time', 'cum').print_stats(.5, 'init')
Beispiel #11
0
""" 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. """
case.save_rst(sys.stdout)
Beispiel #12
0
 def setUp(self):
     """ The test runner will execute this method prior to each test.
     """
     self.case = Case.load(join(DATA_DIR, self.case_name, self.case_name + ".pkl"))
Beispiel #13
0
 def setUp(self):
     """ The test runner will execute this method prior to each test.
     """
     self.case = Case.load(POLY_FILE)
     self.opf = OPF(self.case)
Beispiel #14
0
    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
Beispiel #15
0
    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. """
    agent = LearningAgent(module=net, learner=ENAC())
Beispiel #16
0
from pylon import Case, OPF

from pybrain.rl.agents import LearningAgent
from pybrain.rl.learners import ENAC, Reinforce
from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.plotting import MultilinePlotter
from pybrain.structure import TanhLayer, LinearLayer #@UnusedImport

logger = logging.getLogger()
for handler in logger.handlers: logger.removeHandler(handler)
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.DEBUG)

# Saved case formats are recognised by file extension.
case = Case.load("../data/case6ww.pkl")

#case.generators[0].p_cost = (0.0, 9.0, 200.0)
#case.generators[1].p_cost = (0.0, 2.0, 200.0)
#case.generators[2].p_cost = (0.0, 5.0, 200.0)

case.generators[0].p_cost = (0.0, 5.0, 200.0)
case.generators[1].p_cost = (0.0, 7.0, 200.0)
case.generators[2].p_cost = (0.0, 2.0, 200.0)

case.generators[0].c_shutdown = 100.0

case.generators[0].p_max = 100.0
case.generators[1].p_max = 70.0
case.generators[2].p_max = 70.0
Beispiel #17
0
def getPickledCase():
    """ Returns a test power system case.
    """
    # Read case from data file.
    return Case.load(DATA_FILE)
Beispiel #18
0
    def __init__(self, SpeedLimit=0.5, RandomChanges=10):
        """initialize our simulation"""
        print "Simulation.init()"
        self.translate = [
            1001,
            1002,
            1003,
            1004,
            1032,
            1034,
            1101,
            1102,
            1131,
            1201,
            1202,
            1232,
            1301,
            1302,
            1303,
            1331,
            1333,
            1401,
            1402,
            1403,
            1431,
            2000,
            2030,
            2100,
            2130,
            2201,
            2202,
            2203,
            2233,
            2301,
            2302,
            2332,
            2400,
            2401,
            2402,
            2403,
            2404,
            2405,
            2406,
            2407,
            2408,
            2409,
            2410,
            2411,
            2438,
            2501,
            2502,
            2503,
            2533,
            2600,
            2601,
            2602,
            2603,
            2604,
            2605,
            2606,
            2607,
            2608,
            2609,
            2610,
            2611,
            2612,
            2613,
            2614,
            2615,
            2616,
            2617,
            2618,
            2619,
            2620,
            2621,
            2630,
            2631,
            2634,
            2637,
            2638,
            2901,
            2902,
            3101,
            3102,
            3103,
            3104,
            3105,
            3133,
            3135,
            3201,
            3202,
            3203,
            3204,
            3205,
            3234,
            3301,
            3302,
            3303,
            3304,
            3305,
            3333,
            3401,
            3402,
            3403,
            3404,
            3405,
            3432,
            3433,
            3501,
            3531,
            3601,
            3631,
            3701,
            3731,
            3801,
            3802,
            3803,
            3804,
            3805,
            3806,
            3831,
            3835,
            3836,
            3891,
            3892,
            3893,
            3894,
            3895,
            3896,
            3897,
            3901,
            3902,
            3903,
            3904,
            3905,
            3906,
            3907,
            3908,
            3909,
            3910,
            3911,
            3912,
            3913,
            3914,
            3915,
            3916,
            3917,
            3918,
            3919,
            3920,
            3921,
            3922,
            3923,
            3924,
            3925,
            3926,
            3931,
            3932,
            3933,
            4001,
            4002,
            4003,
            4004,
            4005,
            4006,
            4007,
            4008,
            4009,
            4010,
            4031,
            4035,
            4039,
            4090,
            4091,
            4092,
            4093,
            4094,
            4095,
            4096,
            4097,
            4101,
            4102,
            4103,
            4104,
            4131,
            4132,
            4201,
            4202,
            4203,
            4204,
            4231,
            4232,
            5001,
            5002,
            5003,
            5004,
            5031,
            5032,
            6101,
            6102,
            6103,
            6104,
            6132,
            6201,
            6202,
            6203,
            6204,
            6205,
            6231,
            6235,
            6301,
            6302,
            6303,
            6304,
            6305,
            6333,
            6335,
            6401,
            6402,
            6403,
            6404,
            6433,
            6501,
            6502,
            6503,
            6504,
            6505,
            6506,
            6507,
            6508,
            6509,
            6510,
            6533,
            7001,
            7002,
            7031,
            7032,
            8001,
            8002,
            8003,
            8004,
            8005,
            8033,
            8034,
        ]

        self.SpeedLimit = SpeedLimit  # don't spam solver
        self.RandomChanges = RandomChanges  # only 1 in X chance of new random data

        # Setup sim from master file
        psse_raw = open(IMPORT_FILE)
        self.simCase = Case.load_psse(psse_raw)
        self.simSolver = NewtonPF(self.simCase)
        self.simSolver.solve()
        psse_raw.close()

        # Setup async actions
        self.asyncActions = GetAsyncActions()
        self.asyncActions.setDaemon(True)  # so ctrl-c works
        self.asyncActions.start()
Beispiel #19
0
""" This example demonstrates how to solve an OPF problem. """

import sys
import logging

from os.path import join, dirname

import pylon.case
from pylon import Case, OPF

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

# Define a path to the data file.
CASE_FILE = join(dirname(pylon.case.__file__), "test", "data", "case30pwl",
                 "case30pwl.pkl")

# Load the data file.
case = Case.load(CASE_FILE)

case.generators[2].pwl_to_poly()

# Use DC formulation?
dc = False

# Solve DC optimal power flow.
OPF(case, dc, opt={"verbose": True}).solve()

# Print a report to screen.
case.save_rst(sys.stdout)
Beispiel #20
0
    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