Simple example
In [16]:
Copied!
import sys
import os
import polars as pl
import numpy as np
# Add the project root directory to PYTHONPATH
project_root = os.path.abspath(os.path.join(os.getcwd(), '..', 'src'))
sys.path.insert(0, project_root)
# Import necessary modules
import groundinsight as gi
from groundinsight.models.core_models import BusType, BranchType
import sys
import os
import polars as pl
import numpy as np
# Add the project root directory to PYTHONPATH
project_root = os.path.abspath(os.path.join(os.getcwd(), '..', 'src'))
sys.path.insert(0, project_root)
# Import necessary modules
import groundinsight as gi
from groundinsight.models.core_models import BusType, BranchType
In [8]:
Copied!
#Test the calculation formula of the impedance
# Create a network
net = gi.create_network(name="MyTestNetwork", frequencies=[0, 50, 250, 350, 450, 550])
net.description = "That's my first test network"
bus_type = BusType(
name="BusTypeFormulaTest",
description="Example bus type with parameters",
system_type="Grounded",
voltage_level=230.0,
impedance_formula="1",
)
bus_type_uw = BusType(
name="BusTypeFormulaTestUW",
description="Example bus type with parameters",
system_type="Grounded",
voltage_level=230.0,
impedance_formula="0.1",
)
self_impedance_formula = "(0.201/1000 + (2*pi*f * 4*pi*10**(-7) / 8) + 1j * (2*pi*f * 4*pi*10**(-7) / (2 * pi)) * (log((2 / exp(1) * l * exp(-l / (exp(1) * (1.8514 / sqrt(2 * pi * f * 4*pi*10**(-7) / rho))))) / (5.5/1000))))*l"
mutual_impedance_formula = "((2*pi*f * 4*pi*10**(-7) / 8) + 1j * (2*pi*f * 4*pi*10**(-7) / (2 * pi)) * (log((2 / exp(1) * l * exp(-l / (exp(1) * (1.8514 / sqrt(2 * pi * f * 4*pi*10**(-7) / rho))))) / (5.5/1000))))*l"
branch_type = BranchType(
name="TestBranchType",
description="A test branch type",
grounding_conductor=True,
self_impedance_formula=self_impedance_formula,
mutual_impedance_formula=mutual_impedance_formula
)
branch_ohl = BranchType(
name="OHLine",
description="An overhead line",
grounding_conductor=False,
self_impedance_formula="NaN",
mutual_impedance_formula="NaN"
)
number_buses = 15
#create buses with a for loop
for i in range(1, number_buses):
gi.create_bus(name=f"bus{i}", type=bus_type, network=net, specific_earth_resistance=100.0)
#create branch
#defining a line length of each branch
line_length = 300
#create bracnches with a for loop
for i in range(1, number_buses-1):
gi.create_branch(name=f"branch{i}", type=branch_type, from_bus=f"bus{i}", to_bus=f"bus{i+1}", length=line_length, specific_earth_resistance=100.0, network=net)
#net.add_bus(bus2, overwrite=True)
#create a faults
fault_scaling = {0:1, 50: 1.0, 250: 1}
faultnames = []
for i in range(1, number_buses):
gi.create_fault(name=f"fault{i}", bus=f"bus{i}", description="A fault at bus {i}", scalings=fault_scaling, network=net)
faultnames.append(f"fault{i}")
#add a source at bus1
source = gi.create_source(name="source1", bus="bus1", values={0:10, 50:60, 250:60, 350:60, 450:60, 550:60}, network=net)
#source2 = gi.create_source(name="source2", bus="bus6", values={0:0, 50:30, 250:30, 350:30}, network=net)
#define the paths of the network
gi.create_paths(network=net)
#for loop over each fault
for i in range(number_buses-1):
# Run fault calculations
gi.run_fault(net, fault_name=f"fault{i+1}")
# Access results
res_bus_df = net.res_buses(fault="fault8")
print(res_bus_df)
res_branch_df = net.res_branches(fault="fault8")
res_branch_branch1 = res_branch_df.filter(pl.col('branch_name') == 'branch2')
res_bus7 = res_bus_df.filter(pl.col('bus_name') == 'bus7')
# Access the reduction factors
result = net.results["fault12"]
reduction_factors = result.reduction_factor.value
# Access the grounding impedances
grounding_impedances = result.grounding_impedance.value
# Print reduction factors
for freq, rf in reduction_factors.items():
print(f"Frequency: {freq} Hz, Reduction Factor: {rf}")
# Print the grounding impedances
for freq, z in grounding_impedances.items():
try:
print(f"Frequency: {freq} Hz, Grounding Impedance: {np.abs(z)}")
except:
print("Exception")
#Test the calculation formula of the impedance
# Create a network
net = gi.create_network(name="MyTestNetwork", frequencies=[0, 50, 250, 350, 450, 550])
net.description = "That's my first test network"
bus_type = BusType(
name="BusTypeFormulaTest",
description="Example bus type with parameters",
system_type="Grounded",
voltage_level=230.0,
impedance_formula="1",
)
bus_type_uw = BusType(
name="BusTypeFormulaTestUW",
description="Example bus type with parameters",
system_type="Grounded",
voltage_level=230.0,
impedance_formula="0.1",
)
self_impedance_formula = "(0.201/1000 + (2*pi*f * 4*pi*10**(-7) / 8) + 1j * (2*pi*f * 4*pi*10**(-7) / (2 * pi)) * (log((2 / exp(1) * l * exp(-l / (exp(1) * (1.8514 / sqrt(2 * pi * f * 4*pi*10**(-7) / rho))))) / (5.5/1000))))*l"
mutual_impedance_formula = "((2*pi*f * 4*pi*10**(-7) / 8) + 1j * (2*pi*f * 4*pi*10**(-7) / (2 * pi)) * (log((2 / exp(1) * l * exp(-l / (exp(1) * (1.8514 / sqrt(2 * pi * f * 4*pi*10**(-7) / rho))))) / (5.5/1000))))*l"
branch_type = BranchType(
name="TestBranchType",
description="A test branch type",
grounding_conductor=True,
self_impedance_formula=self_impedance_formula,
mutual_impedance_formula=mutual_impedance_formula
)
branch_ohl = BranchType(
name="OHLine",
description="An overhead line",
grounding_conductor=False,
self_impedance_formula="NaN",
mutual_impedance_formula="NaN"
)
number_buses = 15
#create buses with a for loop
for i in range(1, number_buses):
gi.create_bus(name=f"bus{i}", type=bus_type, network=net, specific_earth_resistance=100.0)
#create branch
#defining a line length of each branch
line_length = 300
#create bracnches with a for loop
for i in range(1, number_buses-1):
gi.create_branch(name=f"branch{i}", type=branch_type, from_bus=f"bus{i}", to_bus=f"bus{i+1}", length=line_length, specific_earth_resistance=100.0, network=net)
#net.add_bus(bus2, overwrite=True)
#create a faults
fault_scaling = {0:1, 50: 1.0, 250: 1}
faultnames = []
for i in range(1, number_buses):
gi.create_fault(name=f"fault{i}", bus=f"bus{i}", description="A fault at bus {i}", scalings=fault_scaling, network=net)
faultnames.append(f"fault{i}")
#add a source at bus1
source = gi.create_source(name="source1", bus="bus1", values={0:10, 50:60, 250:60, 350:60, 450:60, 550:60}, network=net)
#source2 = gi.create_source(name="source2", bus="bus6", values={0:0, 50:30, 250:30, 350:30}, network=net)
#define the paths of the network
gi.create_paths(network=net)
#for loop over each fault
for i in range(number_buses-1):
# Run fault calculations
gi.run_fault(net, fault_name=f"fault{i+1}")
# Access results
res_bus_df = net.res_buses(fault="fault8")
print(res_bus_df)
res_branch_df = net.res_branches(fault="fault8")
res_branch_branch1 = res_branch_df.filter(pl.col('branch_name') == 'branch2')
res_bus7 = res_bus_df.filter(pl.col('bus_name') == 'bus7')
# Access the reduction factors
result = net.results["fault12"]
reduction_factors = result.reduction_factor.value
# Access the grounding impedances
grounding_impedances = result.grounding_impedance.value
# Print reduction factors
for freq, rf in reduction_factors.items():
print(f"Frequency: {freq} Hz, Reduction Factor: {rf}")
# Print the grounding impedances
for freq, z in grounding_impedances.items():
try:
print(f"Frequency: {freq} Hz, Grounding Impedance: {np.abs(z)}")
except:
print("Exception")
shape: (98, 7) ┌──────────┬────────┬──────────────┬──────────┬─────────────┬──────────┬──────────────┐ │ bus_name ┆ fault ┆ frequency_Hz ┆ EPR_V ┆ EPR_degree ┆ I_bus_A ┆ I_bus_degree │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ str ┆ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │ ╞══════════╪════════╪══════════════╪══════════╪═════════════╪══════════╪══════════════╡ │ bus1 ┆ fault8 ┆ 0 ┆ 1.770003 ┆ 0.0 ┆ 1.770003 ┆ 0.0 │ │ bus1 ┆ fault8 ┆ 50 ┆ 6.627494 ┆ -38.131987 ┆ 6.627494 ┆ -38.131987 │ │ bus1 ┆ fault8 ┆ 250 ┆ 2.492708 ┆ -58.975973 ┆ 2.492708 ┆ -58.975973 │ │ bus1 ┆ fault8 ┆ 350 ┆ 1.968483 ┆ -62.224369 ┆ 1.968483 ┆ -62.224369 │ │ bus1 ┆ fault8 ┆ 450 ┆ 1.638483 ┆ -64.730513 ┆ 1.638483 ┆ -64.730513 │ │ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │ │ bus14 ┆ fault8 ┆ 250 ┆ 0.0252 ┆ -90.944428 ┆ 0.0252 ┆ -90.944428 │ │ bus14 ┆ fault8 ┆ 350 ┆ 0.008701 ┆ -129.086169 ┆ 0.008701 ┆ -129.086169 │ │ bus14 ┆ fault8 ┆ 450 ┆ 0.003511 ┆ -159.282729 ┆ 0.003511 ┆ -159.282729 │ │ bus14 ┆ fault8 ┆ 550 ┆ 0.001574 ┆ 175.997103 ┆ 0.001574 ┆ 175.997103 │ │ bus14 ┆ fault8 ┆ RMS ┆ 0.781144 ┆ null ┆ 0.781144 ┆ null │ └──────────┴────────┴──────────────┴──────────┴─────────────┴──────────┴──────────────┘ Frequency: 0.0 Hz, Reduction Factor: 1.0 Frequency: 50.0 Hz, Reduction Factor: 0.28528843695059924 Frequency: 250.0 Hz, Reduction Factor: 0.061325527398599665 Frequency: 350.0 Hz, Reduction Factor: 0.044106528119782676 Frequency: 450.0 Hz, Reduction Factor: 0.03448275230803479 Frequency: 550.0 Hz, Reduction Factor: 0.028334791662944957 Frequency: 0.0 Hz, Grounding Impedance: 0.1393869499968135 Frequency: 50.0 Hz, Grounding Impedance: 0.24555287671898687 Frequency: 250.0 Hz, Grounding Impedance: 0.4705814735411279 Frequency: 350.0 Hz, Grounding Impedance: 0.5532361298169033 Frequency: 450.0 Hz, Grounding Impedance: 0.6182408531132637 Frequency: 550.0 Hz, Grounding Impedance: 0.6703251603617182
In [6]:
Copied!
#print the impedance of all buses as dataframe
print(net.res_all_impedances())
#print the impedance of all buses as dataframe
print(net.res_all_impedances())
shape: (84, 6) ┌────────────┬───────────┬──────────────┬───────────────────┬───────────────────┬──────────────────┐ │ fault_name ┆ fault_bus ┆ frequency_Hz ┆ grounding_impedan ┆ grounding_impedan ┆ reduction_factor │ │ --- ┆ --- ┆ --- ┆ ce_Ohm ┆ ce_deg ┆ --- │ │ str ┆ str ┆ f64 ┆ --- ┆ --- ┆ f64 │ │ ┆ ┆ ┆ f64 ┆ f64 ┆ │ ╞════════════╪═══════════╪══════════════╪═══════════════════╪═══════════════════╪══════════════════╡ │ fault1 ┆ bus1 ┆ 0.0 ┆ null ┆ null ┆ null │ │ fault1 ┆ bus1 ┆ 50.0 ┆ null ┆ null ┆ null │ │ fault1 ┆ bus1 ┆ 250.0 ┆ null ┆ null ┆ null │ │ fault1 ┆ bus1 ┆ 350.0 ┆ null ┆ null ┆ null │ │ fault1 ┆ bus1 ┆ 450.0 ┆ null ┆ null ┆ null │ │ … ┆ … ┆ … ┆ … ┆ … ┆ … │ │ fault14 ┆ bus14 ┆ 50.0 ┆ 0.00118 ┆ -0.132026 ┆ 0.552383 │ │ fault14 ┆ bus14 ┆ 250.0 ┆ 0.004513 ┆ -0.658631 ┆ 0.144176 │ │ fault14 ┆ bus14 ┆ 350.0 ┆ 0.006233 ┆ -0.921018 ┆ 0.104284 │ │ fault14 ┆ bus14 ┆ 450.0 ┆ 0.007956 ┆ -1.182777 ┆ 0.081627 │ │ fault14 ┆ bus14 ┆ 550.0 ┆ 0.009677 ┆ -1.443896 ┆ 0.06704 │ └────────────┴───────────┴──────────────┴───────────────────┴───────────────────┴──────────────────┘
In [9]:
Copied!
# Plot the results
gi.plot_bus_voltages(result=result, frequencies=[50, 250, 350], show=False);
gi.plot_branch_currents(result=result);
gi.plot_bus_currents(result=result);
#gi.plot_bus_voltages(result=result, frequencies=[50], show=False);
# Plot the results
gi.plot_bus_voltages(result=result, frequencies=[50, 250, 350], show=False);
gi.plot_branch_currents(result=result);
gi.plot_bus_currents(result=result);
#gi.plot_bus_voltages(result=result, frequencies=[50], show=False);
In [ ]:
Copied!