# Code

This is a page for short code blocks that I personally think are nice.

More code is on my Github.

**Work in progress!** Check back in the future.

## Check if an electric power system is radial

A power system is "radial" if there are no loops in the circuit.

If we call \(L\) and \(U\) the lower and upper off-diagonal components the network admittance matrix \(Y \in \mathbb{C}^{n \times n}\), then the network is *not* radial if

or alternatively,

\[ \sum_i \sum_j U_{ij} \geq n,\\ \text{where} \ (i,j) \in \{(i,j): U_{ij} \neq 0 \}. \]This function tells you if a PowerModels.jl network model is radial or not.

```
using LinearAlgebra
"""
Given a network's admittance matrix Y, determine if that network is radial.
In other words, determine if there are no electrical loops.
Params:
Y: NxN Network admittance matrix
"""
function is_radial(Y::AbstractMatrix)
n = size(Y)[1]
#Upper and lower off-diagonal elements
U(A::AbstractMatrix) = [A[i] for i in CartesianIndices(A) if i[1]>i[2]]
L(A::AbstractMatrix) = [A[i] for i in CartesianIndices(A) if i[1]<i[2]]
#Get the nonzero upper and lower off diagonal elements
nz_upper = [1 for y_ij in U(Y) if y_ij != 0]
nz_lower = [1 for y_ij in L(Y) if y_ij != 0]
return !(sum(nz_upper)>n-1 || sum(nz_lower) >n-1)
end
#EXAMPLE: case4_dist admittance matrix
Y = [133.333-266.667im -66.6667+133.333im 0.0+0.0im -65.0407+130.081im;
-66.6667+133.333im 133.333-266.667im -66.6667+133.333im 0.0+0.0im;
0.0+0.0im -66.6667+133.333im 66.6667-133.333im 0.0+0.0im;
-65.0407+130.081im 0.0+0.0im 0.0+0.0im 63.4543-126.909im]
println(is_radial(Y))
```

```
true
```

Thanks Prof. Grijalva for explaining this concept to me.

CC BY-SA 4.0 Samuel Talkington. Last modified: May 05, 2022.
Website built with Franklin.jl and the Julia programming language.