# Contingency Analysis¶

See the module `pypsa.contingency`

.

Contingency analysis is concerned with the behaviour of the power system after contingencies such as the outage of particular branches. Only branch outages and the resulting effects on linear power flow are considered here; extensions for non-linear power flow and generator outages may be added in the future.

## Branch Outage Distribution Factors (BODF)¶

`sub_network.caculate_BODF()`

calculates the matrix of Branch Outage
Distribution Factors (BODF) and stores it as
`sub_network.BODF`

. (BODF are also called Line Outage Distribution
Factors (LODF) in the literature, but in PyPSA other passive branches
such as transformers are also included.)

The BODF gives the change of flow on the branches in the network following a branch outage, based on the linear power flow.

For the outage of branch , let be the flows before the outage and be the flows after the outage. Then the BODF is defined by

The BODF can be computed fairly directly from the Power Transfer Distribution Factors (PTDF). First build the branch PTDF from the PTDF and incidence matrix

gives the change in flow on branch if a unit of power is injected at the from-bus of branch and withdrawn from the to-bus of branch . If branch is the only branch connecting two regions, then , since the power can only flow between the two ends of the branch through the branch itself.

The off-diagonal entries of the BODF are given by:

If is the only branch connecting two regions, so that the regions become disconnected after the outage of , then and becomes singular; this case must be treated separately since, for example, each region will need its own slack.

The diagonal entries of the BODF are simply:

## Linear Power Flow Contingency Analysis¶

`network.lpf_contingency(snapshot, branch_outages)`

computes a base
case linear power flow (LPF) with no outages for `snapshot`

, and
then cycles through the list of branches in `branch_outages`

and
computes the line flows after the outage of that branch using the BODF.

The function returns a pandas.DataFrame `p0`

with the flows in each
case in each column of the DataFrame.

## Security-Constrained Linear Optimal Power Flow (SCLOPF)¶

The Security-Constrained Linear Optimal Power Flow (SCLOPF) builds on the Linear Optimal Power Flow (LOPF) described in Optimal Power Flow by including additional constraints that branches may not become overloaded after the outage of a selection of branches.

The SCLOPF is called with the method:

```
network.sclopf(snapshots,branch_outages,**kwargs)
```

where `branch_outages`

is a list of the branches whose outages
should not overload the network. `**kwargs`

are all the same
arguments that may be passed to `network.lopf()`

. (Note that
`network.sclopf()`

is implemented by adding a function to
`network.lopf()`

via the `extra_functionality`

keyword.)

For each potential outage of a branch add a set of constraints for all other branches in the sub-network that they do not become overloaded beyond their capacity :

This applies for all snapshots considered in the optimisation.

### Inputs and Outputs¶

See LOPF in Optimal Power Flow.