You are on page 1of 3

import numpy as np

class Generator:
def __init__(self, name, bus_number, active_power, voltage):
self.name = name
self.bus_number = bus_number
self.active_power = active_power
self.voltage = voltage

class Load:
def __init__(self, name, bus_number, active_power, reactive_power):
self.name = name
self.bus_number = bus_number
self.active_power = active_power
self.reactive_power = reactive_power

class Line:
def __init__(self, sending_bus, receiving_bus, impedance, charging):
self.sending_bus = sending_bus
self.receiving_bus = receiving_bus
self.impedance = impedance
self.charging = charging

class Transformer:
def __init__(self, primary_bus, secondary_bus, impedance, turns_ratio):
self.primary_bus = primary_bus
self.secondary_bus = secondary_bus
self.impedance = impedance
self.turns_ratio = turns_ratio

def perform_load_flow_analysis(generators, loads, lines, transformers):


# Implement the load flow analysis algorithm
# Calculate the power flows, losses, and update the voltages at each bus
# Iterate until convergence is achieved

# Placeholder code
converged = False
iterations = 0

while not converged:


# Perform load flow calculations and update bus voltages
iterations += 1

# Check for convergence based on specified criteria


if iterations >= 10:
converged = True

# Display the load flow results


display_results(generators, loads,lines)

def display_results(generators, loads, lines):


# Display the load flow results, including voltage magnitudes, active power,
and line parameters

print("Load flow results:")


for generator in generators:
print(f"Generator {generator.name} at Bus {generator.bus_number}: Voltage
{generator.voltage:.2f} p.u.")
for load in loads:
print(f"Load {load.name} at Bus {load.bus_number}: Active Power
{load.active_power:.2f} MW")

for line in lines:


print(f"Line between Bus {line.sending_bus} and Bus {line.receiving_bus}:
Impedance {line.impedance:.2f} p.u., Charging {line.charging:.2f}")

def detect_fault(generators, loads, lines, transformers):


# Simulate a fault in the system by modifying the load or line parameters
# For example, we can increase the reactive power of a load or increase the
impedance of a line
# Perform load flow analysis after introducing the fault to detect its impact
on the system

# Display the load flow results before the fault


print("Load flow results before the fault:")
display_results(generators, loads, lines)

# Take user input for the fault impedance


fault_line_index = int(input("Enter the index of the line to simulate a fault:
"))
fault_impedance = float(input("Enter the fault impedance: "))

# Modify the line to simulate a fault


fault_line = lines[fault_line_index]
fault_line.impedance = fault_impedance

# Perform load flow analysis after the fault


perform_load_flow_analysis(generators, loads, lines, transformers)

# Check for faults based on impedance threshold


for line in lines:
if abs(line.impedance) > 100:
print(f"Fault detected at Line between Bus {line.sending_bus} and Bus
{line.receiving_bus}.")

# Display the load flow results after the fault


print("\nLoad flow results after the fault:")
display_results(generators, loads, lines)

def detect_fault(generators, loads, lines, transformers):


# Simulate a fault in the system by modifying the load or line parameters
# For example, we can increase the reactive power of a load or increase the
impedance of a line
# Perform load flow analysis after introducing the fault to detect its impact
on the system

# Display the load flow results before the fault


print("Load flow results before the fault:")
display_results(generators, loads,lines)

# Take user input for the fault impedance


fault_line_index = int(input("Enter the index of the line to simulate a fault:
"))
fault_impedance = float(input("Enter the fault impedance: "))

# Modify the line to simulate a fault


fault_line = lines[fault_line_index]
fault_line.impedance = fault_impedance

# Perform load flow analysis after the fault


perform_load_flow_analysis(generators, loads, lines, transformers)

# Check for faults based on impedance threshold


for line in lines:
if abs(line.impedance) > 100:
print(f"Fault detected at Line between Bus {line.sending_bus} and Bus
{line.receiving_bus}.")

# Display the load flow results after the fault


print("\nLoad flow results after the fault:")
display_results(generators, loads, lines)

def main():
# Create generators
generator1 = Generator("G1", 1, 100, 1.05)
generator2 = Generator("G2", 2, 150, 1.03)
generators = [generator1, generator2]

# Create loads
load1 = Load("L1", 3, 80, 40)
load2 = Load("L2", 4, 120, 60)
loads = [load1, load2]

# Create lines
line1 = Line(1, 3, 0.1 + 0.2j, 0.05)
line2 = Line(2, 4, 0.08 + 0.15j, 0.04)
lines = [line1, line2]

# Create transformers
transformer1 = Transformer(3, 5, 0.05 + 0.1j, 0.95)
transformers = [transformer1]

# Perform load flow analysis


perform_load_flow_analysis(generators, loads, lines, transformers)

# Simulate a fault and detect its impact


detect_fault(generators, loads, lines, transformers)

if __name__ == "__main__":
main()

You might also like