function [x, success] = LPeqslvr(x, om, Ybus, Yf, Yt, Afeq, bfeq, Af, bf, mpopt, il)

%   MATPOWER
%   $Id: LPeqslvr.m,v 1.17 2009/12/08 15:45:55 ray Exp $
%   by Deqiang (David) Gan, PSERC Cornell & Zhejiang University
%	and Ray Zimmerman, PSERC Cornell
%   Copyright (c) 1996-2008 by Power System Engineering Research Center (PSERC)
%   See http://www.pserc.cornell.edu/matpower/ for more info.


%% define named indices into data matrices
[PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
    VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
[GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
    MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
    QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
[PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;

%% options
verbose = mpopt(31);            %% verbose

%% default args
if nargin < 11
    il = [];
end

%% unpack data
mpc = get_mpc(om);
[baseMVA, bus, gen, branch, gencost] = ...
    deal(mpc.baseMVA, mpc.bus, mpc.gen, mpc.branch, mpc.gencost);
vv = get_idx(om);

%% problem dimensions
ny = get_var_N(om, 'y');    %% number of piece-wise linear costs

%% set default constrained lines
if isempty(il)
    nl = size(branch, 1);   %% number of branches
    il = (1:nl);            %% all lines have limits by default
end

%% parse x, update bus, gen
bus(:, VA) = x(vv.i1.Va:vv.iN.Va) * 180/pi;
bus(:, VM) = x(vv.i1.Vm:vv.iN.Vm);
gen(:, PG) = x(vv.i1.Pg:vv.iN.Pg) * baseMVA;
gen(:, QG) = x(vv.i1.Qg:vv.iN.Qg) * baseMVA;

%% turn down verbosity one level for call to power flow
if verbose
    mpopt = mpoption(mpopt, 'VERBOSE', verbose-1);
end

%% get bus index lists of each type of bus
[ref, pv, pq] = bustypes(bus, gen);

%% run the power flow
V = bus(:, VM) .* exp(1j * bus(:, VA) * pi/180);
Sbus = makeSbus(baseMVA, bus, gen);
[V, success, iterations] = newtonpf(Ybus, Sbus, V, ref, pv, pq, mpopt);   %% do NR iteration
[bus, gen, branch] = pfsoln(baseMVA, bus, gen, branch(il,:), Ybus, Yf, Yt, V, ref, pv, pq);   %% post-processing
% printpf(baseMVA, bus, gen, branch, [], success, 0, 1, mpopt);


%% update x
x(vv.i1.Va:vv.iN.Va) = bus(:, VA) * pi/180;
x(vv.i1.Vm:vv.iN.Vm) = bus(:, VM);
x(vv.i1.Pg:vv.iN.Pg) = gen(:, PG) / baseMVA;
x(vv.i1.Qg:vv.iN.Qg) = gen(:, QG) / baseMVA;
if ny > 0
    PgQg = [gen(:, PG); gen(:, QG)];
    ipwl = find(gencost(:, MODEL) == PW_LINEAR);  %% piece-wise linear costs
    x(vv.i1.y:vv.iN.y) = totcost(gencost(ipwl, :), PgQg(ipwl));
end
