Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Not sure if I understood the requirement correctly, but here's the code:

import collections

ierr, (machnum, ) = psspy.amachint(sid=-1, flag=1, string = 'NUMBER')
ierr, (machid, machname) = psspy.amachchar(sid=-1, flag=1, string = ['ID', 'NAME'])
ierr, (pgen, pmin, pmax, ) = psspy.amachreal(sid=-1, flag=1, string = ['PGEN', 'PMIN', 'PMAX'])

# remove white spaces
machid = [i.strip() for i in machid]
machname = [i.strip() for i in machname]

# group machines by ID
machines_by_id = collections.defaultdict(list)
for num, name, mid, pg in zip(machnum, machname, machid, pgen):
    machines_by_id[mid].append((num, name, pg))

# print only machines with same IDs
for mid, mach_list in machines_by_id.items():
    if len(mach_list) > 1:
        print(f"\nMachines with ID '{mid}':")
        for num, name, pg in mach_list:
            print(f"  Bus {num}, Name {name}, Pgen = {pg:.2f} MW")

# function to create subsystem of buses which have at least one machine with the required/defined ID
def create_subsystem(mid):

    if mid.strip() not in machines_by_id:
        print(f"No machines found with ID {mid}")
        return

    buses = [num for num, _, _ in machines_by_id[mid]]
    ierr = psspy.bsys(0, 0, [0.0, 999.0], 0, [], len(buses), buses, 0, [], 0, [])
    if ierr == 0:
        print(f"Subsystem created for machines with ID '{mid}': {buses}")
    else:
        print(f"Error creating subsystem for ID '{mid}', ierr={ierr}")

# create subsystem 
mid = '1'
create_subsystem(mid)

Not sure if I understood the requirement correctly, but here's the code:

import collections

ierr, (machnum, ) = psspy.amachint(sid=-1, flag=1, string = 'NUMBER')
ierr, (machid, machname) = psspy.amachchar(sid=-1, flag=1, string = ['ID', 'NAME'])
ierr, (pgen, pmin, pmax, ) = psspy.amachreal(sid=-1, flag=1, string = ['PGEN', 'PMIN', 'PMAX'])

# remove white spaces
machid = [i.strip() for i in machid]
machname = [i.strip() for i in machname]

# group machines by ID
machines_by_id = collections.defaultdict(list)
for num, name, mid, pg pg, pmn, pmx in zip(machnum, machname, machid, pgen):
pgen, pmin, pmax):
    machines_by_id[mid].append((num, name, pg))
pg, pmn, pmx))

# print only machines with same IDs
for mid, mach_list in machines_by_id.items():
    if len(mach_list) > 1:
        print(f"\nMachines with ID '{mid}':")
        for num, name, pg pg, pmn, pmx in mach_list:
            print(f"  Bus {num}, Name {name}, Pgen = {pg:.2f} MW, Pmin = {pmn:.2f} MW, Pmax = {pmx:.2f} MW")

# function to create subsystem of buses which have at least one machine with the required/defined ID
def create_subsystem(mid):

    if mid.strip() not in machines_by_id:
        print(f"No machines found with ID {mid}")
        return

    buses = [num for num, _, _, _, _ in machines_by_id[mid]]
    ierr = psspy.bsys(0, 0, [0.0, 999.0], 0, [], len(buses), buses, 0, [], 0, [])
    if ierr == 0:
        print(f"Subsystem created for machines with ID '{mid}': {buses}")
    else:
        print(f"Error creating subsystem for ID '{mid}', ierr={ierr}")

# create subsystem 
mid = '1'
create_subsystem(mid)