Network Module
The Net
module provides comprehensive functionality for creating and manipulating power system network models in Julia. It includes features for defining buses, branches, transformers, prosumers, and shunts, as well as methods for running power flow analysis and managing network modifications.
Creating a Network
Start by creating a new network object:
using Sparlectra
# Create a new network with a name and base MVA
net = Net(name = "example_network", baseMVA = 100.0)
Adding Components
Buses
# Add buses with different types (PQ, PV, Slack)
addBus!(net = net, busName = "B1", busType = "PQ", vn_kV = 110.0, vm_pu = 1.0, va_deg = 0.0)
addBus!(net = net, busName = "B2", busType = "PQ", vn_kV = 110.0, vm_pu = 1.0, va_deg = 0.0)
addBus!(net = net, busName = "B3", busType = "PQ", vn_kV = 110.0, vm_pu = 1.0, va_deg = 0.0)
addBus!(net = net, busName = "B4", busType = "PQ", vn_kV = 110.0, vm_pu = 1.0, va_deg = 0.0)
addBus!(net = net, busName = "B5", busType = "Slack", vn_kV = 110.0, vm_pu = 1.0, va_deg = 0.0)
AC Lines
# Add AC lines with physical parameters
addACLine!(net = net, fromBus = "B1", toBus = "B2", length = 25.0, r = 0.2, x = 0.39)
addACLine!(net = net, fromBus = "B1", toBus = "B3", length = 25.0, r = 0.2, x = 0.39)
# Add AC lines using the PI model (directly with per-unit values)
addPIModelACLine!(net = net, fromBus = "B3", toBus = "B4", r_pu = 0.05, x_pu = 0.2, b_pu = 0.01, status = 1)
Transformers
# Add a two-winding transformer
add2WTrafo!(
net = net,
fromBus = "B2",
toBus = "B4",
sn_mva = 100.0,
vk_percent = 10.0,
vkr_percent = 0.5,
pfe_kw = 20.0,
i0_percent = 0.1
)
# Add a transformer using the PI model
addPIModelTrafo!(
net = net,
fromBus = "B4",
toBus = "B5",
r_pu = 0.01,
x_pu = 0.1,
b_pu = 0.0,
status = 1,
ratio = 1.05
)
Prosumers (Generators and Loads)
# Add loads
addProsumer!(net = net, busName = "B1", type = "ENERGYCONSUMER", p = 1.0, q = 2.0)
addProsumer!(net = net, busName = "B2", type = "ENERGYCONSUMER", p = 1.0, q = 2.0)
# Add a slack generator
addProsumer!(
net = net,
busName = "B5",
type = "SYNCHRONMASCHINE",
referencePri = "B5",
vm_pu = 1.0,
va_deg = 0.0
)
# Add a PV generator
addProsumer!(
net = net,
busName = "B1",
type = "GENERATOR",
p = 1.1,
q = 2.0,
vm_pu = 1.02
)
Shunts
# Add a shunt to a bus
addShunt!(net = net, busName = "B1", pShunt = 0.0, qShunt = 1.0)
Running Power Flow
# Set parameters
tol = 1e-6
maxIte = 10
# Run power flow
etime = @elapsed begin
ite, erg = runpf!(net, maxIte, tol, 0)
end
# Check results and calculate losses
if erg != 0
@warn "Power flow did not converge"
else
calcNetLosses!(net)
printACPFlowResults(net, etime, ite, tol)
end
Modifying the Network
Updating Component Parameters
# Update a branch's parameters
brVec = getNetBranchNumberVec(net = net, fromBus = "B1", toBus = "B2")
updateBranchParameters!(
net = net,
branchNr = brVec[1],
branch = BranchModel(
r_pu = 0.02,
x_pu = 0.2,
b_pu = 0.01,
g_pu = 0.0,
ratio = 1.0,
angle = 0.0,
sn_MVA = 100.0
)
)
# Update bus powers
addBusLoadPower!(net = net, busName = "B1", p = 2.0, q = 1.0)
addBusGenPower!(net = net, busName = "B5", p = 3.0, q = 1.5)
addBusShuntPower!(net = net, busName = "B2", p = 0.0, q = 1.0)
Setting Branch Status
# Change a branch's status (in-service or out-of-service)
setNetBranchStatus!(net = net, branchNr = brVec[1], status = 0) # 0 = out of service
Removing Components
See the Component Removal documentation for details on removing components from networks.
Handling Isolated Buses
# Mark isolated buses in the network
markIsolatedBuses!(net = net, log = true)
# Clear (remove) isolated buses
clearIsolatedBuses!(net = net)
Validating the Network
Always validate your network after making significant modifications:
result, msg = validate!(net = net)
if !result
@error "Network is invalid: $msg"
end
API Reference
Sparlectra.add2WTrafo!
— MethodAdd a two-winding transformer to the network.
Arguments
net::Net
: The network to which the transformer will be added.fromBus::String
: The name of the bus where the transformer originates.toBus::String
: The name of the bus where the transformer terminates.sn_mva::Float64
: Rated power of the transformer.vk_percent::Float64
: Voltage regulation percent of the transformer.vkr_percent::Float64
: Voltage regulation percent of the transformer.pfe_kw::Float64
: Iron loss of the transformer.i0_percent::Float64
: No-load current percent of the transformer.status::Int
: The status of the transformer. Default is 1.
Sparlectra.addACLine!
— MethodaddACLine!: Adds an AC line segment to the network.
Parameters:
net::Net
: Network object.fromBus::String
: Name of the "from" bus.toBus::String
: Name of the "to" bus.length::Float64
: Length of the line segment.r::Float64
: Resistance per Meter of the line segment.x::Float64
: Reactance per Meter of the line segment.b::Union{Nothing,Float64} = nothing
: Susceptance per Meter of the line segment (default is nothing).c_nf_per_km::Union{Nothing,Float64} = nothing
: Capacitance per Meter of the line segment in nF/km (default is nothing).tanδ::Union{Nothing,Float64} = nothing
: Tangent of the loss angle (default is nothing).ratedS::Union{Nothing, Float64}= nothing
: Rated power of the line segment in MVA (default is nothing).status::Int = 1
: Status of the line segment (default is 1).
Sparlectra.addBranch!
— MethodaddBranch!: Adds a branch to the network.
Parameters:
net::Net
: Network object.from::Int
: Index of the "from" bus.to::Int
: Index of the "to" bus.branch::AbstractBranch
: Branch object to add.status::Int = 1
: Status of the branch (default is 1).ratio::Union{Nothing,Float64} = nothing
: Ratio of the branch (default is nothing).side::Union{Nothing,Int} = nothing
: Side of the branch (default is nothing).vn_kV::Union{Nothing,Float64} = nothing
: Nominal voltage of the branch in kV (default is nothing).
Sparlectra.addBus!
— MethodaddBus!: Adds a bus to the network.
Parameters:
net::Net
: Network object.busName::String
: Name of the bus.busType::String
: Type of the bus (e.g., "Slack", "PQ", "PV").vn_kV::Float64
: Nominal voltage of the bus in kV.vm_pu::Float64 = 1.0
: Voltage magnitude of the bus in per unit (default is 1.0).va_deg::Float64 = 0.0
: Voltage angle of the bus in degrees (default is 0.0).vmin_pu::Union{Nothing,Float64} = nothing
: Minimum voltage limit in per unit (default is network's vmin_pu).vmax_pu::Union{Nothing,Float64} = nothing
: Maximum voltage limit in per unit (default is network's vmax_pu).isAux::Bool = false
: Boolean indicating if the bus is auxiliary (default is false).oBusIdx::Union{Nothing,Int} = nothing
: Original bus index (default is nothing).zone::Union{Nothing,Int} = nothing
: Zone index (default is nothing).area::Union{Nothing,Int} = nothing
: Area index (default is nothing).ratedS::Union{Nothing,Float64} = nothing
: Rated power of the bus in MVA (default is nothing).
Sparlectra.addBusGenPower!
— MethodUpdate the active and reactive power of a generator connected to a bus in the network.
Arguments
net::Net
: The network object.busName::String
: The name of the bus.p::Union{Nothing, Float64}
: The active power to update. Default isnothing
.q::Union{Nothing, Float64}
: The reactive power to update. Default isnothing
.
Note: the corresponding prosumer object will not be updated.
Examples
net = run_acpflow(max_ite= 7,tol = 1e-6, casefile='a_case.m') # run the power flow on the network and get the network object
updateBusPower!(net = net, busName = "Bus1", p = 0.5, q = 0.2) # Update the power of Bus1 to 0.5 MW and 0.2 MVar
run_net_acpflow(net = net, max_ite= 7,tol = 1e-6) # rerun the power flow with the updated network
Sparlectra.addBusLoadPower!
— MethodUpdate the active and reactive power of a load connected to a bus in the network.
Arguments
net::Net
: The network object.busName::String
: The name of the bus.p::Union{Nothing, Float64}
: The active power to update. Default isnothing
.q::Union{Nothing, Float64}
: The reactive power to update. Default isnothing
.
Note: the corresponding prosumer object will not be updated.
Examples
net = run_acpflow(max_ite= 7,tol = 1e-6, casefile='a_case.m') # run the power flow on the network and get the network object
updateBusPower!(net = net, busName = "Bus1", p = 0.5, q = 0.2) # Update the power of Bus1 to 0.5 MW and 0.2 MVar
run_net_acpflow(net = net, max_ite= 7,tol = 1e-6) # rerun the power flow with the updated network
Sparlectra.addBusShuntPower!
— MethodUpdate the active and reactive power of a shunt connected to a bus in the network.
Arguments
net::Net
: The network object.busName::String
: The name of the bus.p::Float64
: The active power to update.q::Float64
: The reactive power to update.
Examples
net = run_acpflow(max_ite= 7,tol = 1e-6, casefile='a_case.m') # run the power flow on the network and get the network object
updateBusPower!(net = net, busName = "Bus1", p = 0.5, q = 0.2) # Update the power of Bus1 to 0.5 MW and 0.2 MVar
run_net_acpflow(net = net, max_ite= 7,tol = 1e-6) # rerun the power flow with the updated network
Sparlectra.addPIModelACLine!
— MethodaddPIModelACLine!(; net::Net, fromBus::String, toBus::String, r_pu::Float64, x_pu::Float64, b_pu::Float64, status::Int, ratedS::Union{Nothing,Float64}=nothing)
Adds a PI model AC line to the network.
Arguments
net::Net
: The network.fromBus::String
: The name of the bus where the line starts.toBus::String
: The name of the bus where the line ends.r_pu::Float64
: The per unit resistance of the line.x_pu::Float64
: The per unit reactance of the line.b_pu::Float64
: The per unit total line charging susceptance of the line.status::Int
: The status of the line. 1 = in service, 0 = out of service.ratedS::Union{Nothing,Float64}
: The rated power of the line.
Example
addPIModelACLine!(net = network, fromBus = "Bus1", toBus = "Bus2", r_pu = 0.01, x_pu = 0.1, b_pu = 0.02, status = 1, ratedS = 100.0)
Sparlectra.addPIModelTrafo!
— MethodAdd a transformer with PI model to the network.
Arguments
net::Net
: The network to which the transformer will be added.fromBus::String
: The name of the bus where the transformer originates.toBus::String
: The name of the bus where the transformer terminates.r_pu::Float64
: The per-unit resistance of the transformer.x_pu::Float64
: The per-unit reactance of the transformer.b_pu::Float64
: The per-unit susceptance of the transformer.status::Int
: The status of the transformer.ratedU::Union{Nothing, Float64}
: Rated voltage of the transformer. Default isnothing
.ratedS::Union{Nothing, Float64}
: Rated apparent power of the transformer. Default isnothing
.ratio::Union{Nothing, Float64}
: Ratio of the transformer. Default isnothing
.shift_deg::Union{Nothing, Float64}
: Phase shift angle of the transformer. Default isnothing
.isAux::Bool
: Whether the transformer is an auxiliary transformer. Default isfalse
.
Sparlectra.addProsumer!
— MethodAdd a prosumer (combination of a producer and consumer) to the network.
Arguments
net::Net
: The network to which the prosumer will be added.busName::String
: The name of the bus where the prosumer is connected.type::String
: The type of the prosumer.p::Union{Nothing, Float64}
: Active power produced or consumed. Default isnothing
.q::Union{Nothing, Float64}
: Reactive power produced or consumed. Default isnothing
.pMin::Union{Nothing, Float64}
: Minimum active power. Default isnothing
.pMax::Union{Nothing, Float64}
: Maximum active power. Default isnothing
.qMin::Union{Nothing, Float64}
: Minimum reactive power. Default isnothing
.qMax::Union{Nothing, Float64}
: Maximum reactive power. Default isnothing
.referencePri::Union{Nothing, String}
: Reference bus for the prosumer. Default isnothing
.vm_pu::Union{Nothing, Float64}
: Voltage magnitude setpoint. Default isnothing
.va_deg::Union{Nothing, Float64}
: Voltage angle setpoint. Default isnothing
.
Sparlectra.addShunt!
— MethodaddShunt!: Adds a shunt to the network.
Parameters:
net::Net
: Network object.busName::String
: Name of the bus to which the shunt is added.pShunt::Float64
: Active power of the shunt in MW.qShunt::Float64
: Reactive power of the shunt in MVar.in_service::Int = 1
: Indicator for shunt's in-service status (default is 1).
Sparlectra.geNetBusIdx
— MethodgeNetBusIdx: Gets the index of a bus in the network.
Parameters:
net::Net
: Network object.busName::String
: Name of the bus.
Returns:
Int
: Index of the bus in the network.
Sparlectra.getBusType
— MethodGet the type of a specific bus in the network.
Arguments
net::Net
: The network from which to retrieve the bus type.busName::String
: The name of the bus.
Returns
The type of the specified bus.
Sparlectra.getNetBranch
— MethodgetNetBranch(; net::Net, fromBus::String, toBus::String)::Union{Branch,Nothing}
Retrieves the first branch found between two specified buses in the network.
Arguments
net::Net
: The network.fromBus::String
: The name of the bus where the branch starts.toBus::String
: The name of the bus where the branch ends.
Returns
Union{Branch,Nothing}
: The branch between the specified buses, ornothing
if no such branch exists.
Example
```julia getNetBranch(net = network, fromBus = "Bus1", toBus = "Bus2")
Sparlectra.getNetBranchNumberVec
— MethodsetNetBranchStatus!(; net::Net, branchNr::Int, status::Int)
Sets the status of a branch in the network.
Arguments
net::Net
: The network.branchNr::Int
: The number of the branch.status::Int
: The status of the branch. 1 = in service, 0 = out of service.
Example
brVec = getNetBranchNumberVec(net = net, fromBus = "B1", toBus = "B2")
setNetBranchStatus!(net = net, branchNr = brVec[1], status = 0)
Sparlectra.getNetOrigBusIdx
— MethodgetNetOrigBusIdx: Gets the original index of a bus in the network.
Parameters:
net::Net
: Network object.busName::String
: Name of the bus.
Returns:
Int
: Original index of the bus in the network.
Sparlectra.getShunt!
— MethodgetShunt!(; net::Net, busName::String)::Shunt
Retrieves the shunt at the specified bus in the network.
Arguments
net::Net
: The network.busName::String
: The name of the bus.
Returns
Shunt
: The shunt at the specified bus.
Example
```julia getShunt!(net = network, busName = "Bus1")
Sparlectra.getTotalBusPower
— MethodgetTotalBusPower(; net::Net)::Tuple{Float64, Float64}
Gets the total active and reactive power for the network.
Arguments
net::Net
: The network.
Returns
n::Tuple{Float64, Float64}
:
Example
getTotalBusPower(net = network)
Sparlectra.getTotalLosses
— MethodGet the total losses in the network.
Arguments
net::Net
: The network from which to retrieve the losses.
Returns
A tuple (pLosses::Float64, qLosses::Float64)
containing the total active and reactive power losses in the network.
Sparlectra.get_bus_vn_kV
— MethodGet the voltage magnitude of a specific bus in the network.
Arguments
net::Net
: The network from which to retrieve the voltage magnitude.busName::String
: The name of the bus.
Returns
The voltage magnitude of the specified bus.
Sparlectra.get_vn_kV
— MethodGet the voltage magnitude of a specific bus in the network.
Arguments
net::Net
: The network from which to retrieve the voltage magnitude.busIdx::Int
: The index of the bus.
Returns
The voltage magnitude of the specified bus.
Sparlectra.hasBusInNet
— MethodhasBusInNet: Checks if a bus exists in the network.
Parameters:
net::Net
: Network object.busName::String
: Name of the bus to check.
Returns:
Bool
: True if the bus exists in the network, otherwise false.
Sparlectra.hasShunt!
— MethodhasShunt!(; net::Net, busName::String)::Bool
Checks if a shunt exists at the specified bus in the network.
Arguments
net::Net
: The network.busName::String
: The name of the bus.
Returns
Bool
: True if a shunt exists at the specified bus, false otherwise.
Example
```julia hasShunt!(net = network, busName = "Bus1")
Sparlectra.lockNet!
— MethodLock or unlock the network.
Arguments
net::Net
: The network to be locked or unlocked.locked::Bool
: Boolean indicating whether to lock the network.
Sparlectra.markIsolatedBuses!
— MethodmarkIsolatedBuses!(;net::Net)
Finds and marks isolated buses in the network.
Arguments
net::Net
: The network.
Sparlectra.setNetBranchStatus!
— MethodsetNetBranchStatus!(; net::Net, branchNr::Int, status::Int)
Sets the status of a branch in the network.
Arguments
net::Net
: The network.branchNr::Int
: The number of the branch.status::Int
: The status of the branch. 1 = in service, 0 = out of service.
Example
setNetBranchStatus!(net = network, branchNr = 1, status = 1)
Sparlectra.setTotalBusPower!
— MethodsetTotalBusPower!(; net::Net, p::Float64, q::Float64)
Sets the total active and reactive power at the buses in the network.
Arguments
net::Net
: The network.p::Float64
: The total active power for the network.q::Float64
: The total reactive power for the network.
Example
setTotalBusPower!(net = network, p = 100.0, q = 50.0)
Sparlectra.setTotalLosses!
— MethodSet the total losses in the network.
Arguments
net::Net
: The network to which the losses will be added.pLosses::Float64
: Total active power losses.qLosses::Float64
: Total reactive power losses.
Sparlectra.updateBranchParameters!
— MethodupdateBranchParameters!(;net::Net, fromBus::String, toBus::String, branch::AbstractBranch)
Updates the parameters of a branch in the network.
Arguments
net::Net
: The network.fromBus::String
: The name of the bus where the branch starts.toBus::String
: The name of the bus where the branch ends.branch::BranchModel
: The branch with the updated parameters.
Example
updateBranchParameters!(net = network, fromBus = "Bus1", toBus = "Bus2", branch = updatedBranch)
Sparlectra.validate!
— MethodValidate the network configuration.
Arguments
net::Net
: The network to be validated.
Returns
A tuple (valid::Bool, message::String)
where valid
is a boolean indicating whether the network is valid, and message
is a string containing an error message if the network is invalid.