# Buggy code
def makeYbus(baseMVA, bus, branch):
    """Builds the bus admittance matrix and branch admittance matrices.

    Returns the full bus admittance matrix (i.e. for all buses) and the
    matrices C{Yf} and C{Yt} which, when multiplied by a complex voltage
    vector, yield the vector currents injected into each line from the
    "from" and "to" buses respectively of each line. Does appropriate
    conversions to p.u.

    @see: L{makeSbus}

    @author: Ray Zimmerman (PSERC Cornell)
    @author: Richard Lincoln

    modified by Florian Schaefer (to use numba) (florian.schaefer@uni-kassel.de)
    """
    ## constants
    nb = bus.shape[0]  ## number of buses
    nl = branch.shape[0]  ## number of lines

    ## for each branch, compute the elements of the branch admittance matrix where
    ##
    ##      | If |   | Yff  Yft |   | Vf |
    ##      |    | = |          | * |    |
    ##      | It |   | Ytf  Ytt |   | Vt |
    ##
    Ytt, Yff, Yft, Ytf = branch_vectors(branch, nl)

    ## compute shunt admittance
    ## if Psh is the real power consumed by the shunt at V = 1.0 p.u.
    ## and Qsh is the reactive power injected by the shunt at V = 1.0 p.u.
    ## then Psh - j Qsh = V * conj(Ysh * V) = conj(Ysh) = Gs - j Bs,
    ## i.e. Ysh = Psh + j Qsh, so ...
    ## vector of shunt admittances
    Ysh = (bus[:, GS] + 1j * bus[:, BS]) / baseMVA

    ## build connection matrices
    f = np.real(branch[:, F_BUS]).astype(int)  ## list of "from" buses
    t = np.real(branch[:, T_BUS]).astype(int)  ## list of "to" buses

    ## build Yf and Yt such that Yf * V is the vector of complex branch currents injected
    ## at each branch's "from" bus, and Yt is the same for the "to" bus end
    i = np.hstack([np.arange(nl), np.arange(nl)])  ## double set of row indices

    Yf_x = np.hstack([Yff, Yft])
    Yt_x = np.hstack([Ytf, Ytt])
    col_Y = np.hstack([f, t])

    Yf = coo_matrix((Yf_x, (i, col_Y)), (nl, nb)).tocsr()
    Yt = coo_matrix((Yt_x, (i, col_Y)), (nl, nb)).tocsr()
    Yx, Yj, Yp, nnz = gen_Ybus(
        Yf_x,
        Yt_x,
        Ysh,
        col_Y,
        f,
        t,
        np.argsort(f),
        np.argsort(t),
        nb,
        nl,
        np.arange(nl, dtype=np.int64),
    )
    Ybus = csr_matrix((np.resize(Yx, nnz), np.resize(Yj, nnz), Yp))
    return Ybus, Yf, Yt


# Traceback (ValueError)
import pandapower as pp
import pandapower.networks as ppnw
net = ppnw.case9()
b = pp.create_bus(net, vn_kv=100)
pp.create_ext_grid(net, b)
1
pp.create_load(net, b, p_mw=10)
3
pp.runpp(net)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/michael/Dropbox/mac_git/pandapower/pandapower/run.py", line 225, in runpp
_powerflow(net, **kwargs)
File "/Users/michael/Dropbox/mac_git/pandapower/pandapower/powerflow.py", line 74, in _powerflow
result = _run_pf_algorithm(ppci, net["_options"], **kwargs)
File "/Users/michael/Dropbox/mac_git/pandapower/pandapower/powerflow.py", line 151, in _run_pf_algorithm
result = _run_newton_raphson_pf(ppci, options)
File "/Users/michael/Dropbox/mac_git/pandapower/pandapower/pf/run_newton_raphson_pf.py", line 51, in _run_newton_raphson_pf
ppci, success, iterations = _run_ac_pf_without_qlims_enforced(ppci, options)
File "/Users/michael/Dropbox/mac_git/pandapower/pandapower/pf/run_newton_raphson_pf.py", line 131, in _run_ac_pf_without_qlims_enforced
V, success, iterations, J, Vm_it, Va_it = newton(Ybus, Sbus, V0, pv, pq, ppci, options)
File "/Users/michael/Dropbox/mac_git/pandapower/pandapower/pypower/newtonpf.py", line 87, in newtonpf
F = _evaluate_Fx(Ybus, V, Sbus, pv, pq)
File "/Users/michael/Dropbox/mac_git/pandapower/pandapower/pypower/newtonpf.py", line 132, in _evaluate_Fx
mis = V * conj(Ybus * V) - Sbus
File "/Users/michael/miniconda3/envs/pandapower/lib/python3.7/site-packages/scipy/sparse/base.py", line 499, in __mul__
raise ValueError('dimension mismatch')
ValueError: dimension mismatch

# Fixed code
def makeYbus(baseMVA, bus, branch):
    """Builds the bus admittance matrix and branch admittance matrices.

    Returns the full bus admittance matrix (i.e. for all buses) and the
    matrices C{Yf} and C{Yt} which, when multiplied by a complex voltage
    vector, yield the vector currents injected into each line from the
    "from" and "to" buses respectively of each line. Does appropriate
    conversions to p.u.

    @see: L{makeSbus}

    @author: Ray Zimmerman (PSERC Cornell)
    @author: Richard Lincoln

    modified by Florian Schaefer (to use numba) (florian.schaefer@uni-kassel.de)
    """
    ## constants
    nb = bus.shape[0]  ## number of buses
    nl = branch.shape[0]  ## number of lines

    ## for each branch, compute the elements of the branch admittance matrix where
    ##
    ##      | If |   | Yff  Yft |   | Vf |
    ##      |    | = |          | * |    |
    ##      | It |   | Ytf  Ytt |   | Vt |
    ##
    Ytt, Yff, Yft, Ytf = branch_vectors(branch, nl)

    ## compute shunt admittance
    ## if Psh is the real power consumed by the shunt at V = 1.0 p.u.
    ## and Qsh is the reactive power injected by the shunt at V = 1.0 p.u.
    ## then Psh - j Qsh = V * conj(Ysh * V) = conj(Ysh) = Gs - j Bs,
    ## i.e. Ysh = Psh + j Qsh, so ...
    ## vector of shunt admittances
    Ysh = (bus[:, GS] + 1j * bus[:, BS]) / baseMVA

    ## build connection matrices
    f = np.real(branch[:, F_BUS]).astype(int)  ## list of "from" buses
    t = np.real(branch[:, T_BUS]).astype(int)  ## list of "to" buses

    ## build Yf and Yt such that Yf * V is the vector of complex branch currents injected
    ## at each branch's "from" bus, and Yt is the same for the "to" bus end
    i = np.hstack([np.arange(nl), np.arange(nl)])  ## double set of row indices

    Yf_x = np.hstack([Yff, Yft])
    Yt_x = np.hstack([Ytf, Ytt])
    col_Y = np.hstack([f, t])

    Yf = coo_matrix((Yf_x, (i, col_Y)), (nl, nb)).tocsr()
    Yt = coo_matrix((Yt_x, (i, col_Y)), (nl, nb)).tocsr()
    Yx, Yj, Yp, nnz = gen_Ybus(
        Yf_x,
        Yt_x,
        Ysh,
        col_Y,
        f,
        t,
        np.argsort(f),
        np.argsort(t),
        nb,
        nl,
        np.arange(nl, dtype=np.int64),
    )
    Ybus = csr_matrix((np.resize(Yx, nnz), np.resize(Yj, nnz), Yp), (nb, nb))
    return Ybus, Yf, Yt