Based on pfsoln.m from MATPOWER by Ray Zimmerman, developed
        at PSERC Cornell. See U{http://www.pserc.cornell.edu/matpower/} for
        more information.

        Updates buses, generators and branches to match a power flow solution.

def pf_solution(self, Ybus, Yf, Yt, V):
        """ Based on pfsoln.m from MATPOWER by Ray Zimmerman, developed
        at PSERC Cornell. See U{http://www.pserc.cornell.edu/matpower/} for
        more information.

        Updates buses, generators and branches to match a power flow solution.
        """
        buses = self.connected_buses
        branches = self.online_branches
        generators = self.online_generators

        self.reset()
        self.index_buses()
        self.index_branches()

        Va = angle(V)
        Vm = abs(V)
        for i, b in enumerate(buses):
            b.v_angle = Va[i] * 180.0 / pi
            b.v_magnitude = Vm[i]

        # Update Qg for all gens and Pg for swing bus.
#        gbus = [g.bus._i for g in generators]
        refgen = [i for i, g in enumerate(generators)
                  if g.bus.type == REFERENCE]

        # Compute total injected bus powers.
#        Sg = V[gbus] * conj(Ybus[gbus, :] * V)
        Sg = V * conj(Ybus * V)


        # Update Qg for all generators.
#        for i in gbus:
#            g = generators[i]
        for g in generators:
            # inj Q + local Qd
            g.q = Sg.imag[g.bus._i] * self.base_mva + g.bus.q_demand

        # At this point any buses with more than one generator will have
        # the total Q dispatch for the bus assigned to each generator. This
        # must be split between them. We do it first equally, then in proportion
        # to the reactive range of the generator.
        if generators:
            pass

        # Update Pg for swing bus.
        for i in refgen:
            g = generators[i]
            # inj P + local Pd
            g.p = Sg.real[i] * self.base_mva + g.bus.p_demand

        # More than one generator at the ref bus subtract off what is generated
        # by other gens at this bus.
        if len(refgen) > 1:
            pass

        br = [l._i for l in branches]
        f_idx = [l.from_bus._i for l in branches]
        t_idx = [l.to_bus._i for l in branches]

        Sf = V[f_idx] * conj(Yf[br, :] * V) * self.base_mva
        St = V[t_idx] * conj(Yt[br, :] * V) * self.base_mva

        # Complex power at "from" bus.
        for i, l in enumerate(branches):
            l.p_from = Sf[i].real
            l.q_from = Sf[i].imag
            l.p_to = St[i].real
            l.q_to = St[i].imag