AC Optimal Power Flow

JuliaGrid utilizes the JuMP package to construct optimal power flow models, allowing users to manipulate these models using the standard functions provided by JuMP. As a result, JuliaGrid supports popular solvers mentioned in the JuMP documentation to solve the optimization problem.

To perform the AC optimal power flow, we first need to have the PowerSystem type that has been created with the AC model. After that, create the AcOptimalPowerFlow type to establish the AC optimal power flow framework using the function:


To solve the AC optimal power flow problem and obtain generator active and reactive power outputs, as well as bus voltage magnitudes and angles, users can use the following function:

After solving the AC optimal power flow, JuliaGrid provides functions for computing powers and currents:

Alternatively, instead of using functions responsible for solving optimal power flow and computing powers and currents, users can use the wrapper function:

Users can also access specialized functions for computing specific types of powers or currents for individual buses, branches, or generators within the power system.


Optimal Power Flow Model

To set up the AC optimal power flow, we begin by creating the model. To illustrate this, consider the following:

using JuMP, Ipopt

system = powerSystem()

@bus(minMagnitude = 0.95, maxMagnitude = 1.05)
addBus!(system; label = "Bus 1", type = 3, active = 0.1, angle = -0.1)
addBus!(system; label = "Bus 2", reactive = 0.01, magnitude = 1.1)

@branch(minDiffAngle = -pi, maxDiffAngle = pi, reactance = 0.5, type = 1)
addBranch!(system; label = "Branch 1", from = "Bus 1", to = "Bus 2", maxFromBus = 0.15)

@generator(maxActive = 0.5, minReactive = -0.1, maxReactive = 0.1, status = 0)
addGenerator!(system; label = "Generator 1", bus = "Bus 1", active = 0.4, reactive = 0.2)
addGenerator!(system; label = "Generator 2", bus = "Bus 2", active = 0.2, reactive = 0.1)

cost!(system; generator = "Generator 1", active = 2, polynomial = [800.0; 200.0; 80.0])
cost!(system; generator = "Generator 2", active = 1, piecewise = [10 12.3; 14.7 16.8; 18 19])

cost!(system; generator = "Generator 1", reactive = 2, polynomial = [2.0])
cost!(system; generator = "Generator 2", reactive = 1, piecewise = [2.0 4.0; 6.0 8.0])

acModel!(system)

Next, the acOptimalPowerFlow function is utilized to formulate the AC optimal power flow problem:

analysis = acOptimalPowerFlow(system, Ipopt.Optimizer)

Optimization Variables

In the AC optimal power flow model, the active and reactive power outputs of the generators are expressed as nonlinear functions of the bus voltage magnitudes and angles. As a result, the variables in this model include the active and reactive power outputs of the generators, as well as the bus voltage magnitudes and angles:

julia> JuMP.all_variables(analysis.method.jump)8-element Vector{VariableRef}:
 magnitude[1]
 magnitude[2]
 angle[1]
 angle[2]
 active[1]
 active[2]
 reactive[1]
 reactive[2]

It is important to note that this is not a comprehensive set of optimization variables. When the cost function is defined as a piecewise linear function comprising multiple segments, as illustrated in the case of the active power output cost for Generator 2, JuliaGrid automatically generates helper optimization variables named actwise and reactwise, and formulates a set of linear constraints to effectively address these cost functions. For the sake of simplicity, we initially assume that Generator 2 is out-of-service. Consequently, the helper variable is not included in the set of optimization variables. However, as we progress through this manual, we will activate the generator, introducing the helper variable and additional constraints to the optimization model.

It is worth emphasizing that in instances where a piecewise linear cost function consists of only a single segment, as demonstrated by the reactive power output cost of Generator 2, the function is modeled as a standard linear function, avoiding the need for additional helper optimization variables.

Please be aware that JuliaGrid maintains references to all variables, which are categorized into six fields:

julia> fieldnames(typeof(analysis.method.variable.voltage))(:magnitude, :angle)
julia> fieldnames(typeof(analysis.method.variable.power))(:active, :reactive, :actwise, :reactwise)

Variable Names

Users have the option to define custom variable names for printing equations, which can help present them in a more compact form. For example:

analysis = acOptimalPowerFlow(system, Ipopt.Optimizer; magnitude = "V", angle = "θ")

Add Variables

Users can easily add new variables to the defined AC optimal power flow model by using the @variable macro:

JuMP.@variable(analysis.method.jump, newVariable)

We can verify that the new variable is included in the defined model by using the function:

julia> JuMP.is_valid(analysis.method.jump, newVariable)true

Delete Variables

The variable can be deleted, but this operation is only applicable if the objective function is either affine or quadratic. To achieve this, we can utilize the delete function provided by the JuMP, as demonstrated below:

JuMP.delete(analysis.method.jump, newVariable)

After deletion, the variable is no longer part of the model:

julia> JuMP.is_valid(analysis.method.jump, newVariable)false

Constraint Functions

JuliaGrid keeps track of all the references to internally formed constraints in the constraint field of the AcOptimalPowerFlow type. These constraints are divided into six fields:

julia> fieldnames(typeof(analysis.method.constraint))(:slack, :balance, :voltage, :flow, :capability, :piecewise)
Info

We suggest that readers refer to the tutorial on AC Optimal Power Flow for insights into the implementation.


Slack Bus Constraint

The slack field contains a reference to the equality constraint associated with the fixed bus voltage angle value of the slack bus. This constraint is set within the addBus! function using the angle keyword:

julia> print(system.bus.label, analysis.method.constraint.slack.angle)Bus 1: θ[1] = -0.1

Users have the flexibility to modify this constraint by changing which bus serves as the slack bus and by adjusting the value of the bus angle. This can be achieved using the updateBus! function, for example:

updateBus!(analysis; label = "Bus 1", type = 1)
updateBus!(analysis; label = "Bus 2", type = 3, angle = -0.2)

Subsequently, the updated slack constraint can be inspected as follows:

julia> print(system.bus.label, analysis.method.constraint.slack.angle)Bus 2: θ[2] = -0.2

Bus Power Balance Constraints

The balance field contains references to the equality constraints associated with the active and reactive power balance equations defined for each bus. These constraints ensure that the total active and reactive power injected by the generators matches the total active and reactive power demanded at each bus.

The constant term in the active power balance equations is determined by the active keyword within the addBus! function, which defines the active power demanded at the bus. We can access the references to the active power balance constraints using the following code snippet:

julia> print(system.bus.label, analysis.method.constraint.balance.active)Bus 1: (-V[1]) * ((V[2] * ((2.0 * sin(θ[1] - θ[2]))))) - 0.1 = 0
Bus 2: (-V[2]) * ((V[1] * ((2.0 * sin(-θ[1] + θ[2]))))) - 0.0 = 0

Similarly, the constant term in the reactive power balance equations is determined by the reactive keyword within the addBus! function, which defines the reactive power demanded at the bus. We can access the references to the reactive power balance constraints using the following code snippet:

julia> print(system.bus.label, analysis.method.constraint.balance.reactive)Bus 1: (-V[1]) * ((2 V[1]) + (V[2] * ( - (2.0 * cos(θ[1] - θ[2]))))) - 0.0 = 0
Bus 2: (-V[2]) * ((2 V[2]) + (V[1] * ( - (2.0 * cos(-θ[1] + θ[2]))))) - 0.01 = 0

During the execution of functions that add or update power system components, these constraints are automatically adjusted to reflect the current configuration of the power system, for example:

updateBus!(analysis; label = "Bus 2", active = 0.5)
updateBranch!(analysis; label = "Branch 1", reactance = 0.25)

The updated set of active power balance constraints can be examined as follows:

julia> print(system.bus.label, analysis.method.constraint.balance.active)Bus 1: (-V[1]) * ((V[2] * ((4.0 * sin(θ[1] - θ[2]))))) - 0.1 = 0
Bus 2: (-V[2]) * ((V[1] * ((4.0 * sin(-θ[1] + θ[2]))))) - 0.5 = 0

Bus Voltage Constraints

The voltage field contains references to the inequality constraints associated with the voltage magnitude and voltage angle difference limits. These constraints ensure that the bus voltage magnitudes and the angle differences between the from-bus and to-bus ends of each branch are within specified limits.

The minimum and maximum bus voltage magnitude limits are set using the minMagnitude and maxMagnitude keywords within the addBus! function. The constraints associated with these limits can be accessed using:

julia> print(system.bus.label, analysis.method.constraint.voltage.magnitude)Bus 1: V[1] ∈ [0.95, 1.05]
Bus 2: V[2] ∈ [0.95, 1.05]

The minimum and maximum voltage angle difference limits between the from-bus and to-bus ends of each branch are set using the minDiffAngle and maxDiffAngle keywords within the addBranch! function. The constraints associated with these limits can be accessed using the following code snippet:

julia> print(system.branch.label, analysis.method.constraint.voltage.angle)Branch 1: θ[1] - θ[2] ∈ [-3.141592653589793, 3.141592653589793]
Info

Please note that if the limit constraints are set to minDiffAngle = -2π and maxDiffAngle = 2π for the corresponding branch, JuliGrid will omit the corresponding inequality constraint.

By employing the updateBus! and updateBranch! functions, users have the ability to modify these constraints:

updateBus!(analysis; label = "Bus 1", minMagnitude = 1.0, maxMagnitude = 1.0)
updateBranch!(analysis; label = "Branch 1", minDiffAngle = -1.7, maxDiffAngle = 1.7)

Subsequently, the updated set of constraints can be examined as follows:

julia> print(system.bus.label, analysis.method.constraint.voltage.magnitude)Bus 1: V[1] = 1
Bus 2: V[2] ∈ [0.95, 1.05]
julia> print(system.branch.label, analysis.method.constraint.voltage.angle)Branch 1: θ[1] - θ[2] ∈ [-1.7, 1.7]

Branch Flow Constraints

The flow field refers to inequality constraints that enforce limits on the apparent power flow, active power flow, or current flow magnitude at the from-bus and to-bus ends of each branch. The type of constraint applied is specified using the type keyword in the addBranch! function:

  • type = 1 active power flow,
  • type = 2 apparent power flow,
  • type = 3 apparent power flow with a squared inequality constraint,
  • type = 4 current flow magnitude,
  • type = 5 current flow magnitude with a squared inequality constraint.
Tip

Squared versions of constraints typically make the optimization problem numerically more robust. However, they often result in slower convergence compared to their non-squared counterparts used in the constraints.

These limits are specified using the minFromBus, maxFromBus, minToBus and maxToBus keywords within the addBranch! function. By default, these limit keywords are associated with apparent power (type = 3).

However, in the example, we configured it to use active power flow by setting type = 1. To access the flow constraints of branches at the from-bus end, we can utilize the following code snippet:

julia> print(system.branch.label, analysis.method.constraint.flow.from)Branch 1:  - ((V[1]*V[2]) * ((-4.0 * sin(θ[1] - θ[2])))) ∈ [0, 0.15]
Info

If the branch flow limits are set to minFromBus = 0.0 and maxFromBus = 0.0 for the corresponding branch, JuliGrid will omit the corresponding inequality constraint at the from-bus end of the branch. The same applies to the to-bus end if minToBus = 0.0 and maxToBus = 0.0 are set.

Additionally, by employing the updateBranch! function, we have the ability to modify these specific constraints:

updateBranch!(analysis; label = "Branch 1", minFromBus = -0.15, maxToBus = 0.15)

The updated set of flow constraints can be examined as follows:

julia> print(system.branch.label, analysis.method.constraint.flow.from)Branch 1:  - ((V[1]*V[2]) * ((-4.0 * sin(θ[1] - θ[2])))) ∈ [-0.15, 0.15]
julia> print(system.branch.label, analysis.method.constraint.flow.to)Branch 1: - ((V[1]*V[2]) * ( - (-4.0 * sin(θ[1] - θ[2])))) ∈ [0, 0.15]
Tip

In typical scenarios, minFromBus is equal to minToBus, and maxFromBus is equal to maxToBus. However, we allow these values to be defined separately for greater flexibility, enabling, among other things, the option to apply constraints on only one side of the branch.


Generator Power Capability Constraints

The capability field contains references to the inequality constraints associated with the minimum and maximum active and reactive power outputs of the generators.

The constraints associated with the minimum and maximum active power output limits of the generators are defined using the minActive and maxActive keywords within the addGenerator! function. To access the constraints associated with these limits, we can use the following code snippet:

julia> print(system.generator.label, analysis.method.constraint.capability.active)Generator 1: active[1] = 0
Generator 2: active[2] = 0

Similarly, the constraints associated with the minimum and maximum reactive power output limits of the generators are specified using the minReactive and maxReactive keywords within the addGenerator! function. To access these constraints, we can use the following code snippet:

julia> print(system.generator.label, analysis.method.constraint.capability.reactive)Generator 1: reactive[1] = 0
Generator 2: reactive[2] = 0

As demonstrated, the active and reactive power outputs of Generator 1 and Generator 2 are currently fixed at zero due to previous actions that set these generators out-of-service. However, we can modify these specific constraints by utilizing the updateGenerator! function, as shown below:

updateGenerator!(analysis; label = "Generator 1", status = 1)
updateGenerator!(analysis; label = "Generator 2", status = 1, minActive = 0.1)

Subsequently, the updated set of constraints can be examined as follows:

julia> print(system.generator.label, analysis.method.constraint.capability.active)Generator 1: active[1] ∈ [0, 0.5]
Generator 2: active[2] ∈ [0.1, 0.5]
julia> print(system.generator.label, analysis.method.constraint.capability.reactive)Generator 1: reactive[1] ∈ [-0.1, 0.1] Generator 2: reactive[2] ∈ [-0.1, 0.1]
Info

This representation may not fully capture the generator's power output behavior due to the tradeoff between active and reactive power outputs. JuliaGrid can incorporate this tradeoff in its optimization model. For more information, see the tutorial on Power Capability Constraints.


Power Piecewise Constraints

In cost modeling, the piecewise field serves as a reference to the inequality constraints associated with piecewise linear cost functions. These constraints are defined using the cost! function with active = 1 or reactive = 1.

In our example, only the active power cost of Generator 2 is modeled as a piecewise linear function with two segments, and JuliaGrid takes care of setting up the appropriate inequality constraints for each segment:

julia> print(system.generator.label, analysis.method.constraint.piecewise.active)Generator 2: 0.9574468085106385 active[2] - actwise[2] ≤ -2.7255319148936152
Generator 2: 0.6666666666666663 active[2] - actwise[2] ≤ -7.000000000000007

It is worth noting that these constraints can also be automatically updated using the cost! function. Readers can find more details in the section discussing the objective function.

As mentioned at the beginning, piecewise linear cost functions with multiple segments will also introduce helper variables that are added to the objective function. In this specific example, the helper variable is:

julia> analysis.method.variable.power.actwise[2]actwise[2]

Add Constraints

Users can effortlessly introduce additional constraints into the defined AC optimal power flow model by utilizing the addBranch! or addGenerator! functions. Specifically, if a user wishes to include a new branch or generator in an already defined PowerSystem and AcOptimalPowerFlow type:

addBranch!(analysis; label = "Branch 2", from = "Bus 1", to = "Bus 2", reactance = 1)
addGenerator!(analysis; label = "Generator 3", bus = "Bus 2", active = 2, status = 1)

This will affect all constraints related to branches and generators, but it will also update balance constraints to configure the optimization model to match the current state of the power system. For example, we can observe the following updated constraints:

julia> print(system.branch.label, analysis.method.constraint.voltage.angle)Branch 1: θ[1] - θ[2] ∈ [-1.7, 1.7]
Branch 2: θ[1] - θ[2] ∈ [-3.141592653589793, 3.141592653589793]
julia> print(system.generator.label, analysis.method.constraint.capability.active)Generator 1: active[1] ∈ [0, 0.5] Generator 2: active[2] ∈ [0.1, 0.5] Generator 3: active[3] ∈ [0, 0.5]

Add User-Defined Constraints

Users also have the option to include their custom constraints within the established AC optimal power flow model by employing the @constraint macro. For example, the addition of a new constraint can be achieved as follows:

JuMP.@constraint(analysis.method.jump, 0 <= analysis.method.variable.power.active[3] <= 0.3)

Delete Constraints

To delete a constraint, users can make use of the delete function from the JuMP package. When handling constraints that have been internally created, users can refer to the constraint references stored in the constraint field of the AcOptimalPowerFlow type.

For example, if the intention is to eliminate constraints related to the capability of Generator 3, we can use:

JuMP.delete(analysis.method.jump, analysis.method.constraint.capability.active[3])
Info

In the event that a user deletes a constraint and subsequently executes a function that updates bus, branch, or generator parameters, and if the deleted constraint is affected by these functions, JuliaGrid will automatically reinstate that constraint. Users should exercise caution when deleting constraints, as this action is considered potentially harmful since it operates independently of power system data.


Objective Function

The objective function of the AC optimal power flow is formulated using polynomial and piecewise linear cost functions associated with the generators, defined using the cost! functions.

In the provided example, the objective function to be minimized in order to obtain optimal values for the active and reactive power outputs of the generators, as well as the bus voltage magnitudes and angles, is as follows:

julia> JuMP.objective_function(analysis.method.jump)800 active[1]² + 200 active[1] + actwise[2] + reactive[2] + 84

The objective function is stored in the variable analysis.objective, where it is organized to separate its quadratic and nonlinear components.


Update Objective Function

By utilizing the cost! functions, users have the flexibility to modify the objective function by adjusting polynomial or piecewise linear coefficients or by changing the type of polynomial or piecewise linear function employed. For example, consider Generator 1, which employs a quadratic polynomial cost function for active power. We can redefine the cost function for this generator as a cubic polynomial and thereby define a nonlinear objective function:

cost!(analysis; generator = "Generator 1", active = 2, polynomial = [63; 25; 4; 0.5])

This leads to an updated objective function, which can be examined as follows:

julia> JuMP.objective_function(analysis.method.jump)(25 active[1]² + actwise[2] + reactive[2] + 4 active[1] + 4.5) + (63.0 * (active[1] ^ 3.0))

User-Defined Objective Function

Users can modify the objective function using the set_objective_function function from the JuMP package. This operation is considered destructive because it is independent of power system data; however, in certain scenarios, it may be more straightforward than using the cost! function for updates. Moreover, using this methodology, users can combine a defined function with a newly defined expression.

In this context, we can utilize the saved objective function within the objective field of the AcOptimalPowerFlow type. For example, we can easily eliminate nonlinear parts and alter the quadratic component of the objective:

expr = 5.0 * analysis.method.variable.power.active[1]^2
JuMP.set_objective_function(analysis.method.jump, analysis.method.objective.quadratic - expr)

We can now observe the updated objective function as follows:

julia> JuMP.objective_function(analysis.method.jump)20 active[1]² + actwise[2] + reactive[2] + 4 active[1] + 4.5

Setup Initial Values

In JuliaGrid, the assignment of initial primal and dual values for optimization variables and constraints takes place when the solve! function is executed.


Initial Primal Values

Initial primal values are determined based on the generator and voltage fields within the AcOptimalPowerFlow type. By default, these values are initially established using the active and reactive power outputs of the generators and the initial bus voltage magnitudes and angles:

julia> generator = analysis.power.generator;
julia> print(system.generator.label, generator.active, generator.reactive)Generator 1: 0.4, 0.2 Generator 2: 0.2, 0.1 Generator 3: 2.0, 0.0
julia> print(system.bus.label, analysis.voltage.magnitude, analysis.voltage.angle)Bus 1: 1.0, -0.1 Bus 2: 1.1, 0.0

Users have the flexibility to adjust these values according to their specifications, which will then be used as the initial primal values when executing the solve! function.


Using AC Power Flow

In this perspective, users have the capability to conduct the AC power flow analysis and leverage the resulting solution to configure initial primal values. Here is an illustration of how this can be achieved:

flow = newtonRaphson(system)
powerFlow!(flow; power = true)

After obtaining the solution, we can use the active and reactive power outputs of the generators, along with bus voltage magnitudes and angles, to set the initial values:

setInitialPoint!(analysis, flow)

Initial Dual Values

Dual variables, often referred to as Lagrange multipliers or Kuhn-Tucker multipliers, represent the shadow prices or marginal costs associated with constraints. The assignment of initial dual values occurs when the solve! function is executed. Initially, the initial dual values are unknown, but users can access and manually set them. For example:

analysis.method.dual.balance.active[1] = 0.4

Optimal Power Flow Solution

To establish the AC optimal power flow problem, we utilize the acOptimalPowerFlow function. After setting up the problem, we can use the solve! function to compute the optimal values for the active and reactive power outputs of the generators and the bus voltage magnitudes angles:

solve!(analysis)

By executing this function, we will obtain the solution with the optimal values for the active and reactive power outputs of the generators, as well as the bus voltage magnitudes and angles.

julia> generator = analysis.power.generator;
julia> print(system.generator.label, generator.active, generator.reactive)Generator 1: -9.247258673536948e-9, 0.05412183967036393 Generator 2: 0.30000000302359475, -0.10000000753747516 Generator 3: 0.30000000622366385, 0.05846400294254813
julia> print(system.bus.label, analysis.voltage.magnitude, analysis.voltage.angle)Bus 1: 1.0, -0.22021610371406802 Bus 2: 0.9893778000071517, -0.2

Objective Value

To obtain the objective value of the optimal power flow solution, we can use the objective_value function:

julia> JuMP.objective_value(analysis.method.jump)11.599999889994324

Dual Variables

The values of the dual variables are stored in the dual field of the AcOptimalPowerFlow type. For example:

julia> analysis.method.dual.balance.active[1]0.6666666922547244

Wrapper Function

JuliaGrid provides a wrapper function for AC optimal power flow analysis and also supports the computation of powers and currents using the powerFlow! function:

analysis = acOptimalPowerFlow(system, Ipopt.Optimizer)
powerFlow!(analysis; verbose = 1)
EXIT: The optimal solution was found.

Users can utilize the functions printBusData and printGeneratorData to display results. Additionally, the functions listed in the Print Constraint Data section allow users to print constraint data related to buses, branches, or generators in the desired units. For example:

show = Dict("Active Power Balance" => false)
printBusConstraint(analysis; show)
|---------------------------------------------------------------------------|
| Bus Constraint Data                                                       |
|---------------------------------------------------------------------------|
| Label |            Voltage Magnitude             | Reactive Power Balance |
|       |                                          |                        |
|       | Minimum | Solution | Maximum |      Dual |   Solution |      Dual |
|       |    [pu] |     [pu] |    [pu] | [$/pu-hr] |       [pu] | [$/pu-hr] |
|-------|---------|----------|---------|-----------|------------|-----------|
| Bus 1 |  1.0000 |   1.0000 |  1.0000 |   -0.0000 |    -0.0000 |    0.0000 |
| Bus 2 |  0.9500 |   0.9899 |  1.0500 |    0.0000 |    -0.0000 |    0.0000 |
|---------------------------------------------------------------------------|

Next, users can easily customize the print results for specific constraint, for example:

printBusConstraint(analysis; label = "Bus 1", header = true)
printBusConstraint(analysis; label = "Bus 2", footer = true)

Save Results to a File

Users can also redirect print output to a file. For example, data can be saved in a text file as follows:

open("bus.txt", "w") do file
    printBusConstraint(analysis, file)
end

Save Results to a CSV File

For CSV output, users should first generate a simple table with style = false, and then save it to a CSV file:

using CSV

io = IOBuffer()
printBusConstraint(analysis, io; style = false)
CSV.write("constraint.csv", CSV.File(take!(io); delim = "|"))

Primal and Dual Warm Start

Utilizing the AcOptimalPowerFlow type and proceeding directly to the solver offers the advantage of a warm start. In this scenario, the initial primal and dual values for the subsequent solving step correspond to the solution obtained from the previous step.


Primal Variables

In the previous example, the following solution was obtained, representing the values of the primal variables:

julia> print(system.generator.label, generator.active, generator.reactive)Generator 1: 0.4, 0.2
Generator 2: 0.2, 0.1
Generator 3: 2.0, 0.0
julia> print(system.bus.label, analysis.voltage.magnitude, analysis.voltage.angle)Bus 1: 1.0, -0.22020623425789937 Bus 2: 0.9898609824408063, -0.2

Dual Variables

We also obtained all dual values. Here, we list only the dual variables for one type of constraint as an example:

julia> print(system.generator.label, analysis.method.dual.capability.reactive)Generator 1: -3.9554305281671654e-8
Generator 2: 0.9999999549316296
Generator 3: -4.506837038941835e-8

Modify Optimal Power Flow

Now, let us introduce changes to the power system from the previous example:

updateGenerator!(analysis; label = "Generator 2", maxActive = 0.2)

Next, we want to solve this modified optimal power flow problem. If we use solve! at this point, the primal and dual initial values will be set to the previously obtained values:

powerFlow!(analysis)

As a result, we obtain a new solution:

julia> print(system.generator.label, generator.active, generator.reactive)Generator 1: 0.4, 0.2
Generator 2: 0.2, 0.1
Generator 3: 2.0, 0.0
julia> print(system.bus.label, analysis.voltage.magnitude, analysis.voltage.angle)Bus 1: 1.0, -0.22021629372422769 Bus 2: 0.9893685025776359, -0.2

Reset Primal and Dual Values

Users retain the flexibility to reset initial primal and dual values to their default configurations at any juncture. This can be accomplished by utilizing the active and reactive power outputs of the generators and the initial bus voltage magnitudes and angles extracted from the PowerSystem type, employing the setInitialPoint! function:

setInitialPoint!(analysis)

The primal initial values will now be identical to those that would be obtained if the acOptimalPowerFlow function were executed after all the updates have been applied, while all dual variable values will be removed.


Power and Current Analysis

After obtaining the solution from the AC optimal power flow, we can calculate various electrical quantities related to buses and branches using the power! and current! functions. For instance, let us consider the power system for which we obtained the AC optimal power flow solution:

using Ipopt

system = powerSystem()

@bus(minMagnitude = 0.9, maxMagnitude = 1.1)
addBus!(system; label = "Bus 1", type = 3, magnitude = 1.05, angle = 0.17)
addBus!(system; label = "Bus 2", active = 0.1, reactive = 0.01, conductance = 0.04)
addBus!(system; label = "Bus 3", active = 0.05, reactive = 0.02)

@branch(resistance = 0.5, reactance = 1.0, conductance = 1e-4, susceptance = 0.01)
addBranch!(system; label = "Branch 1", from = "Bus 1", to = "Bus 2", maxFromBus = 0.15)
addBranch!(system; label = "Branch 2", from = "Bus 1", to = "Bus 3", maxFromBus = 0.10)
addBranch!(system; label = "Branch 3", from = "Bus 2", to = "Bus 3", maxFromBus = 0.25)

@generator(maxActive = 0.5, minReactive = -0.1, maxReactive = 0.1)
addGenerator!(system; label = "Generator 1", bus = "Bus 1", active = 3.2, reactive = 0.5)
addGenerator!(system; label = "Generator 2", bus = "Bus 2", active = 0.2, reactive = 0.1)

cost!(system; generator = "Generator 1", active = 2, polynomial = [1100.2; 500; 80])
cost!(system; generator = "Generator 2", active = 1, piecewise = [10 12.3; 14.7 16.8; 18 19])

analysis = acOptimalPowerFlow(system, Ipopt.Optimizer)
powerFlow!(analysis)

We can now utilize the following functions to calculate powers and currents:

power!(analysis)
current!(analysis)

For instance, if we want to show the active power injections and the from-bus current magnitudes, we can employ:

julia> print(system.bus.label, analysis.power.injection.active)Bus 1: -9.618497658937797e-9
Bus 2: 0.08516587147998696
Bus 3: -0.04999999999683577
julia> print(system.branch.label, analysis.current.from.magnitude)Branch 1: 0.026586095723416 Branch 2: 0.023281396182966287 Branch 3: 0.03814141340614029
Info

To better understand the powers and current associated with buses and branches that are calculated by the power! and current! functions, we suggest referring to the tutorials on AC Optimal Power Flow.


Users can utilize any of the print functions outlined in the Print Power System Data or Print Power System Summary. For example, to create a bus data with the desired units, users can use the following function:

@voltage(pu, deg)
@power(MW, MVAr)
show = Dict("Power Generation" => false, "Current Injection" => false)
printBusData(analysis; show)
|-------------------------------------------------------------------------------------------|
| Bus Data                                                                                  |
|-------------------------------------------------------------------------------------------|
| Label |       Voltage       |    Power Demand    |  Power Injection   |    Shunt Power    |
|       |                     |                    |                    |                   |
|   Bus | Magnitude |   Angle |  Active | Reactive |  Active | Reactive | Active | Reactive |
|       |      [pu] |   [deg] |    [MW] |   [MVAr] |    [MW] |   [MVAr] |   [MW] |   [MVAr] |
|-------|-----------|---------|---------|----------|---------|----------|--------|----------|
| Bus 1 |    0.9279 |  9.7403 |  0.0000 |   0.0000 | -0.0000 |   3.0795 | 0.0000 |  -0.0000 |
| Bus 2 |    0.9140 | 11.6477 | 10.0000 |   1.0000 |  8.5166 |  -3.2864 | 3.3417 |  -0.0000 |
| Bus 3 |    0.9000 |  9.1611 |  5.0000 |   2.0000 | -5.0000 |  -2.0000 | 0.0000 |  -0.0000 |
|-------------------------------------------------------------------------------------------|

Active and Reactive Power Injection

To calculate the active and reactive power injection associated with a specific bus, the function can be used:

julia> active, reactive = injectionPower(analysis; label = "Bus 1")(-9.618497658937797e-9, 0.030794636707574978)

Active and Reactive Power Injection from Generators

To calculate the active and reactive power injection from the generators at a specific bus, the function can be used:

julia> active, reactive = supplyPower(analysis; label = "Bus 2")(0.18516587147949892, -0.022863549854815576)

Active and Reactive Power at Shunt Element

To calculate the active and reactive power associated with shunt element at a specific bus, the function can be used:

julia> active, reactive = shuntPower(analysis; label = "Bus 2")(0.03341709360934514, -0.0)

Active and Reactive Power Flow

Similarly, we can compute the active and reactive power flow at both the from-bus and to-bus ends of the specific branch by utilizing the provided functions below:

julia> active, reactive = fromPower(analysis; label = "Branch 2")(0.017184035296255465, 0.013093295137884126)
julia> active, reactive = toPower(analysis; label = "Branch 2")(-0.016754097376909496, -0.02075596468886172)

Active and Reactive Power at Charging Admittances

To calculate the total active and reactive power linked with branch charging admittances of the particular branch, the function can be used:

julia> active, reactive = chargingPower(analysis; label = "Branch 1")(8.482536057962741e-5, -0.008482536057962741)

Active powers indicate active losses within the branch's charging admittances. Moreover, charging admittances injected reactive powers into the power system due to their capacitive nature, as denoted by a negative sign.


Active and Reactive Power at Series Impedance

To calculate the active and reactive power across the series impedance of the branch, the function can be used:

julia> active, reactive = seriesPower(analysis; label = "Branch 2")(0.00034638355278055247, 0.0006927671055611049)

The active power also considers active losses originating from the series resistance of the branch, while the reactive power represents reactive losses resulting from the impedance's inductive characteristics.


Current Injection

To calculate the current injection associated with a specific bus, the function can be used:

julia> magnitude, angle = injectionCurrent(analysis; label = "Bus 1")(0.033185882131190725, -1.400796639138171)

Current Flow

We can compute the current flow at both the from-bus and to-bus ends of the specific branch by using:

julia> magnitude, angle = fromCurrent(analysis; label = "Branch 2")(0.023281396182966287, -0.4811023775573815)
julia> magnitude, angle = toCurrent(analysis; label = "Branch 2")(0.029637799027503, 2.4098006400080925)

Current Through Series Impedance

To calculate the current passing through the series impedance of the branch in the direction from the from-bus end to the to-bus end, we can use the following function:

julia> magnitude, angle = seriesCurrent(analysis; label = "Branch 2")(0.026320469326383697, -0.6228593917886373)