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 composite type that has been created with the AC model. After that, create the ACOptimalPowerFlow composite type to establish the AC optimal power flow framework using the function:

To solve the AC optimal power flow problem and acquire bus voltage magnitudes and angles, and generator active and reactive power outputs, make use of the following function:


After obtaining the AC optimal power flow solution, JuliaGrid offers post-processing analysis functions for calculating powers and currents associated with buses and branches:

Furthermore, there are specialized functions dedicated to calculating specific types of powers related to particular buses and branches:

Likewise, there are specialized functions dedicated to calculating specific types of currents related to particular buses or branches:


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 = 2)
addBranch!(system; label = "Branch 1", from = "Bus 1", to = "Bus 2", longTerm = 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; label = "Generator 1", active = 2, polynomial = [800.0; 200.0; 80.0])
cost!(system; label = "Generator 2", active = 1, piecewise = [10.8 12.3; 14.7 16.8; 18 18.1])

cost!(system; label = "Generator 1", reactive = 2, polynomial = [2.0])
cost!(system; label = "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}:
 active[1]
 active[2]
 reactive[1]
 reactive[2]
 magnitude[1]
 magnitude[2]
 angle[1]
 angle[2]

It is important to note that this is not a comprehensive set of optimization variables. When the cost function is defined as a linear piecewise 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 linear piecewise cost function consists of only a single segment, as demonstrated by the reactive power output cost of Generator 2, the function is modelled as a standard linear function, obviating 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))(:active, :reactive, :magnitude, :angle, :actwise, :reactwise)

Add Variables

The user has the ability to easily add new variables to the defined AC optimal power flow model by using the @variable macro from the JuMP package. Here is an example:

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 composite 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: angle[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!(system, analysis; label = "Bus 1", type = 1)
updateBus!(system, 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: angle[2] = -0.2

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: -(magnitude[1] * (0.0 + ((2 magnitude[2]) * sin(angle[1] - angle[2])))) - 0.1 = 0
Bus 2: -(magnitude[2] * (0.0 + ((2 magnitude[1]) * sin(-angle[1] + angle[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: -(magnitude[1] * ((2 magnitude[1]) - ((2 magnitude[2]) * cos(angle[1] - angle[2])))) - 0.0 = 0
Bus 2: -(magnitude[2] * ((2 magnitude[2]) - ((2 magnitude[1]) * cos(-angle[1] + angle[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!(system, analysis; label = "Bus 2", active = 0.5)
updateBranch!(system, analysis; label = "Branch 1", reactance = 0.25)

For example, 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: -(magnitude[1] * (0.0 + ((4 magnitude[2]) * sin(angle[1] - angle[2])))) - 0.1 = 0
Bus 2: -(magnitude[2] * (0.0 + ((4 magnitude[1]) * sin(-angle[1] + angle[2])))) - 0.5 = 0

Voltage Constraints

The voltage field within the model 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: magnitude[1] ∈ [0.95, 1.05]
Bus 2: magnitude[2] ∈ [0.95, 1.05]

Similarly, 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: angle[1] - angle[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.

Additionally, by employing the updateBus! and updateBranch! functions, user has the ability to modify these specific constraints as follows:

updateBus!(system, analysis; label = "Bus 1", minMagnitude = 1.0, maxMagnitude = 1.0)
updateBranch!(system, 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: magnitude[1] = 1
Bus 2: magnitude[2] ∈ [0.95, 1.05]
julia> print(system.branch.label, analysis.method.constraint.voltage.angle)Branch 1: angle[1] - angle[2] ∈ [-1.7, 1.7]

Flow Constraints

The flow field contains references to the inequality constraints associated with the apparent power flow, active power flow, or current flow magnitude limits at the from-bus and to-bus ends of each branch. The type which one of the constraint will be applied is defined according to the type keyword within the addBranch! function, type = 1 for the apparent power flow, type = 2 for the active power flow, or type = 3 for the current flow magnitude. These limits are specified using the longTerm keyword within the addBranch! function.

By default, the longTerm keyword is linked to apparent power (type = 1). However, in the example, we configured it to use active power flow by setting type = 2. 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: ((0) - ((-4 magnitude[1]*magnitude[2]) * sin(angle[1] - angle[2]))) - 0.15 ≤ 0

Similarly, to access the to-bus end flow constraints of branches we can use the following code snippet:

julia> print(system.branch.label, analysis.method.constraint.flow.to)Branch 1: (0.0 + ((-4 magnitude[1]*magnitude[2]) * sin(angle[1] - angle[2]))) - 0.15 ≤ 0
Info

Please note that if the flow constraints are set to longTerm = 0.0 for the corresponding branch, JuliGrid will omit the corresponding inequality constraint.

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

updateBranch!(system, analysis; label = "Branch 1", reactance = 0.8, longTerm = 0.14)

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

julia> print(system.branch.label, analysis.method.constraint.flow.from)Branch 1: ((0) - ((-1.25 magnitude[1]*magnitude[2]) * sin(angle[1] - angle[2]))) - 0.14 ≤ 0
julia> print(system.branch.label, analysis.method.constraint.flow.to)Branch 1: (0.0 + ((-1.25 magnitude[1]*magnitude[2]) * sin(angle[1] - angle[2]))) - 0.14 ≤ 0

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!(system, analysis; label = "Generator 1", status = 1)
updateGenerator!(system, 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 provide the most accurate portrayal of the generator's power output behavior. In reality, there is a tradeoff between the active and reactive power outputs of the generators, and JuliaGrid has the capability to integrate this tradeoff into the optimization model. For more details, please refer to the tutorial on Power Capability Constraints.


Power Piecewise Constraints

In the context of cost modelling, the piecewise field acts as a reference to the inequality constraints associated with linear piecewise cost functions. These constraints are established using the cost! function, with active = 1 or reactive = 1 specified when working with linear piecewise cost functions that consist of multiple segments. In our example, only the active power cost of Generator 2 is modelled as a linear piecewise 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: 1.1538461538461542 active[2] - actwise[2] ≤ 0.16153846153846452
Generator 2: 0.3939393939393941 active[2] - actwise[2] ≤ -11.009090909090908

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, a linear piecewise 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.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!(system, analysis; label = "Branch 2", from = "Bus 1", to = "Bus 2", reactance = 1)
addGenerator!(system, 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: angle[1] - angle[2] ∈ [-1.7, 1.7]
Branch 2: angle[1] - angle[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.0 <= analysis.method.variable.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 linear piecewise 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

JuliaGrid also stores the objective function in a separate variable, which can be accessed by referring to the variable analysis.objective. In this variable, the objective function is organized in a way that separates the quadratic and nonlinear components of the objective function.


Update Objective Function

By utilizing the cost! functions, users have the flexibility to modify the objective function by adjusting polynomial or linear piecewise coefficients or by changing the type of polynomial or linear piecewise 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!(system, analysis; label = "Generator 1", active = 2, polynomial = [631; 257; 40; 5.0])

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

julia> JuMP.objective_function(analysis.method.jump)(257 active[1]² + 40 active[1] + actwise[2] + reactive[2] + 9) + (631.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.active[1] * analysis.method.variable.active[1]
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)252 active[1]² + 40 active[1] + actwise[2] + reactive[2] + 9

Setup Primal Starting Values

In JuliaGrid, the assignment of starting primal values for optimization variables takes place when the solve! function is executed. Starting 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.2

Users have the flexibility to adjust these values according to their specifications, which will then be used as the starting 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 starting primal values. Here is an illustration of how this can be achieved:

flow = newtonRaphson(system)
for iteration = 1:100
    stopping = mismatch!(system, flow)
    if all(stopping .< 1e-8)
        break
    end
    solve!(system, flow)
end

After obtaining the solution, we can calculate the active and reactive power outputs of the generators and utilize the bus voltage magnitudes and angles to set the starting values. In this case, the generator and voltage fields of the ACOptimalPowerFlow type can be employed to store the new starting values:

for (key, value) in system.generator.label
    active, reactive = generatorPower(system, flow; label = key)
    analysis.power.generator.active[value] = active
    analysis.power.generator.reactive[value] = reactive
end

for i = 1:system.bus.number
    analysis.voltage.magnitude[i] = flow.voltage.magnitude[i]
    analysis.voltage.angle[i] = flow.voltage.angle[i]
end

Optimal Power Flow Solution

To establish the AC optimal power flow problem, we can 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. Also, to turn off the solver output within the REPL, we use the set_silent function before calling solve! function. Here is an example:

JuMP.set_silent(analysis.method.jump)
solve!(system, 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.846985885398597e-9, 0.04976946905919965 Generator 2: 0.3000000150593341, -0.10000000423962996 Generator 3: 0.2999999947876518, 0.06577586941092359
julia> print(system.bus.label, analysis.voltage.magnitude, analysis.voltage.angle)Bus 1: 1.0, -0.24541853116946893 Bus 2: 0.9788897103039467, -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)20.027272231047704

Warm Start

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

In the previous example, we obtained the following solution:

julia> print(system.generator.label, generator.active, generator.reactive)Generator 1: -9.846985885398597e-9, 0.04976946905919965
Generator 2: 0.3000000150593341, -0.10000000423962996
Generator 3: 0.2999999947876518, 0.06577586941092359
julia> print(system.bus.label, analysis.voltage.magnitude, analysis.voltage.angle)Bus 1: 1.0, -0.24541853116946893 Bus 2: 0.9788897103039467, -0.2

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

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

Next, solve this new power system. During the execution of the solve! function, the primal starting values will first be set, and these values will be defined according to the values given above.

solve!(system, analysis)

As a result, we obtain a new solution:

julia> print(system.generator.label, generator.active, generator.reactive)Generator 1: -9.846985885398597e-9, 0.04976946905919965
Generator 2: 0.3000000150593341, -0.10000000423962996
Generator 3: 0.2999999947876518, 0.06577586941092359
julia> print(system.bus.label, analysis.voltage.magnitude, analysis.voltage.angle)Bus 1: 1.0, -0.24541853116946893 Bus 2: 0.9788897103039467, -0.2

Users retain the flexibility to reset these initial primal 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 composite type, employing the startingPrimal! function:

startingPrimal!(system, analysis)

These values are precisely identical to what we would obtain if we executed the acOptimalPowerFlow function following all the updates we performed:

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.2

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", longTerm = 0.15)
addBranch!(system; label = "Branch 2", from = "Bus 1", to = "Bus 3", longTerm = 0.10)
addBranch!(system; label = "Branch 3", from = "Bus 2", to = "Bus 3", longTerm = 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; label = "Generator 1", active = 2, polynomial = [1100.2; 500; 80])
cost!(system; label = "Generator 2", active = 1, piecewise = [10.8 12.3; 14.7 16.8; 18 18.1])

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

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

power!(system, analysis)
current!(system, 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.620540583794857e-9
Bus 2: 0.08516606801635107
Bus 3: -0.04999999999897997
julia> print(system.branch.label, analysis.current.from.magnitude)Branch 1: 0.026571245613638255 Branch 2: 0.02327515579762493 Branch 3: 0.038138710710893406
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.

To compute specific quantities for particular components, rather than calculating powers or currents for all components, users can utilize one of the provided functions below.


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(system, analysis; label = "Bus 1")(-9.620540584662218e-9, 0.030765801875305195)

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(system, analysis; label = "Bus 2")(0.18516606801523944, -0.022836348119282302)

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(system, analysis; label = "Bus 2")(0.03341803114531875, -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(system, analysis; label = "Branch 2")(0.017183819173556, 0.013083736295848414)
julia> active, reactive = toPower(system, analysis; label = "Branch 2")(-0.016754074906889488, -0.02074674916342745)

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(system, analysis; label = "Branch 1")(8.482584216518542e-5, -0.008482584216518542)

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(system, analysis; label = "Branch 2")(0.0003461903313633867, 0.0006923806627267733)

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(system, analysis; label = "Bus 1")(0.03315507404284225, -1.4007966394973068)

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(system, analysis; label = "Branch 2")(0.02327515579762493, -0.48075640477812265)
julia> magnitude, angle = toCurrent(system, analysis; label = "Branch 2")(0.029629721662614236, 2.4100114878509347)

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(system, analysis; label = "Branch 2")(0.02631312719398387, -0.6225889445656966)