我想打印bus_lines双列表中的元素。代码的重点是接收 BusData.txt 文件并将元素存储在列表中。
这是数据文件
# Bus Data
# BusID, Type, VoltageMag, VoltageAng, ActivePower, ReactivePower
1, Gen, 1.0, 0.0, 1.0, 0.0
2, Load, 0.0, 0.0, 0.6, 0.3
3, Gen, 1.0, 0.0, 0.0, 0.0
4, Load, 0.0, 0.0, 0.6, 0.2
5, Load, 0.0, 0.0, 0.5, 0.4
# Line Data
# Line, StartBus, EndBus, ImpedanceR, ImpedanceX, Capacitance
1, 1, 2, 0.05, 0.25, 0.05
2, 2, 3, 0.05, 0.25, 0.05
3, 1, 4, 0.02, 0.2, 0.033333
4, 2, 4, 0.02, 0.2, 0.033333
5, 4, 5, 0.01, 0.1, 0.02
class Bus:
idx:int
u:complex
i_inj: complex
bus_type: int #define 0 slack, 1 pv, 2 pq
#y: complex # ?
p: int
q: int
q_min: int
q_max: int
def __init__(self, idx, bus_type, voltage_mag, voltage_ang, p, q, y, q_min, q_max):
self.idx = idx
self.bus_type = bus_type
self.u=complex(voltage_mag*np.cos(voltage_ang),voltage_mag*np.sin(voltage_ang))
self.voltage_mag = voltage_mag
self.voltage_ang = voltage_ang
self.p = p
self.q = q
self.y=y # maybe
self.q_min = q_min
self.q_max = q_max
def set_to_pv(self, voltage_mag, active_power):
self.voltage_mag = voltage_mag
self.bus_type = 1
self.active_power = active_power
print("Bus switched to PV")
def set_to_pq(self, active_power, reactive_power):
self.bus_type = 2
self.active_power = active_power
self.reactive_power = reactive_power
print("Bus switched to PQ")
def switch_pq (self):
if self.q < self.q_min:
self.q = self.q_min
self.set_to_pq(self.p, self.q)
print("Reactive power below limits")
elif self.q > self.q_max:
self.q = self.q_max
self.set_to_pq((self.p, self.q))
print("Reactive power above limits")
def switch_back(self):
if self.q_min <= self.q <= self.q_max:
if self.bus_type == 2:
self.set_to_pv(self.voltage_mag, self.active_power)
print("Bus switched to PV")
class Line:
start_bus: int
end_bus: int
z: complex
y:complex
def __init__(self, line_id, start_bus, end_bus, r, x, capacitance):
self.line_id = line_id # what is this?
self.start_bus = start_bus
self.end_bus = end_bus
self.z = complex(r, x)
try:
self.y=1/self.z
except ZeroDivisionError as err:
self.y=0
print(err, 'at line', start_bus, end_bus)
self.capacitance = capacitance
def read_file(filename):
buses = []
lines = []
section = None
with open(filename, "BusData.txt") as file:
reader = csv.reader(file)
for row in reader:
if "# Bus Data" in row:
section = "bus"
next(reader) # Skip header
elif "# Line Data" in row:
section = "line"
next(reader) # Skip header
elif section == "bus":
bus = Bus(
idx=int(row[0]),
bus_type=row[1],
voltage_mag=float(row[2]),
voltage_ang=float(row[3]),
p=float(row[4]),
q=float(row[5])
#z=complex(row[6],row[7])
)
buses.append(bus)
elif section == "line":
line = Line(
line_id=int(row[0]),
start_bus=int(row[1]),
end_bus=int(row[2]),
r=float(row[3]),
x=float(row[4]),
capacitance=float(row[5])
)
lines.append(line)
buses_lines=[buses,lines]
return buses_lines
我试过这个:
f = open("BusData.txt", "r")
read = f.read()
rf = read_file(read)
print(rf[0][1].idx)
我收到回溯错误或射频,以及无效模式“BusData.txt”。更新:现在我知道它希望我将 q_min 和 q_max 添加到读取文件函数中。问题是我不想从文件中读取它并添加到总线,因为它是指定的限制并且只能被定义。
这里出现了一些问题,我们似乎无法就如何解决这些问题达成共识。与
open()
相关的最初问题是我们将文件名作为有价值的文件名传递,并对文件名进行硬编码。文件的硬编码名称被解释为模式,因此出现初始错误:
更改:
with open(filename, "BusData.txt") as file:
致:
with open(filename, "r") as file:
下一个问题与缺少初始化我们的类的参数有关。如果这些值不在数据中,那么我们需要以某种方式为它们分配默认值。这可以通过多种方式完成。
也许这些默认值可以在命令行或配置文件中传递,并可用于设置您将通过调用传递的变量值以创建
Bus()
的实例。
第二种方法可能是在 init 方法的定义中为这些变量设置默认值,这就是下一个代码块的作用。
import csv
import numpy as np
class Bus:
def __init__(self,
idx,
bus_type,
voltage_mag,
voltage_ang,
p,
q,
y = 0, # give this a default value
q_min = 0, # give this a default value
q_max = 0 # give this a default value
):
self.idx = idx
self.bus_type = bus_type
self.u = complex(voltage_mag*np.cos(voltage_ang),voltage_mag*np.sin(voltage_ang))
self.voltage_mag = voltage_mag
self.voltage_ang = voltage_ang
self.p = p
self.q = q
self.y = y
self.q_min = q_min
self.q_max = q_max
def __str__(self):
return str(self.__dict__)
class Line:
def __init__(self,
line_id,
start_bus,
end_bus,
r,
x,
capacitance
):
self.line_id = line_id # what is this?
self.start_bus = start_bus
self.end_bus = end_bus
self.z = complex(r, x)
try:
self.y=1/self.z
except ZeroDivisionError as err:
self.y=0
print(err, 'at line', start_bus, end_bus)
self.capacitance = capacitance
def __str__(self):
return str(self.__dict__)
def read_file(filename):
buses = []
lines = []
section = None
with open(filename, "r") as file:
reader = csv.reader(file)
for row in reader:
## ----------------
## If the row is blank we can ignore it
## ----------------
if not row:
continue
## ----------------
## ----------------
## flag to collect bus information
## ----------------
if row[0] == "# Bus Data":
section = "bus"
next(reader) ## skip the header
continue
## ----------------
## ----------------
## flag to collect line information
## ----------------
if "# Line Data" in row:
section = "line"
next(reader) ## skip the header
continue
## ----------------
if not section:
raise ValueError("State Machine Error")
if section == "bus":
buses.append(Bus(
idx=int(row[0]),
bus_type=row[1].strip(),
voltage_mag=float(row[2]),
voltage_ang=float(row[3]),
p=float(row[4]),
q=float(row[5])
))
continue
if section == "line":
lines.append(Line(
line_id=int(row[0]),
start_bus=int(row[1]),
end_bus=int(row[2]),
r=float(row[3]),
x=float(row[4]),
capacitance=float(row[5])
))
continue
return [buses,lines]
buses,lines = read_file("BusData.txt")
print("-" * 10)
print("Buses:")
for bus in buses:
print(bus)
print("-" * 10)
print("-" * 10)
print("Lines:")
for line in lines:
print(line)
print("-" * 10)
此代码,当与您最初描述的并置“BusData.txt”一起运行时,将产生:
Buses:
{'idx': 1, 'bus_type': 'Gen', 'u': (1+0j), 'voltage_mag': 1.0, 'voltage_ang': 0.0, 'p': 1.0, 'q': 0.0, 'y': 0, 'q_min': 0, 'q_max': 0}
{'idx': 2, 'bus_type': 'Load', 'u': 0j, 'voltage_mag': 0.0, 'voltage_ang': 0.0, 'p': 0.6, 'q': 0.3, 'y': 0, 'q_min': 0, 'q_max': 0}
{'idx': 3, 'bus_type': 'Gen', 'u': (1+0j), 'voltage_mag': 1.0, 'voltage_ang': 0.0, 'p': 0.0, 'q': 0.0, 'y': 0, 'q_min': 0, 'q_max': 0}
{'idx': 4, 'bus_type': 'Load', 'u': 0j, 'voltage_mag': 0.0, 'voltage_ang': 0.0, 'p': 0.6, 'q': 0.2, 'y': 0, 'q_min': 0, 'q_max': 0}
{'idx': 5, 'bus_type': 'Load', 'u': 0j, 'voltage_mag': 0.0, 'voltage_ang': 0.0, 'p': 0.5, 'q': 0.4, 'y': 0, 'q_min': 0, 'q_max': 0}
----------
----------
Lines:
{'line_id': 1, 'start_bus': 1, 'end_bus': 2, 'z': (0.05+0.25j), 'y': (0.7692307692307693-3.846153846153846j), 'capacitance': 0.05}
{'line_id': 2, 'start_bus': 2, 'end_bus': 3, 'z': (0.05+0.25j), 'y': (0.7692307692307693-3.846153846153846j), 'capacitance': 0.05}
{'line_id': 3, 'start_bus': 1, 'end_bus': 4, 'z': (0.02+0.2j), 'y': (0.495049504950495-4.9504950495049505j), 'capacitance': 0.033333}
{'line_id': 4, 'start_bus': 2, 'end_bus': 4, 'z': (0.02+0.2j), 'y': (0.495049504950495-4.9504950495049505j), 'capacitance': 0.033333}
{'line_id': 5, 'start_bus': 4, 'end_bus': 5, 'z': (0.01+0.1j), 'y': (0.99009900990099-9.900990099009901j), 'capacitance': 0.02}
----------