function t_insolvablepf(quiet)
%T_INSOLVABLEPF  Test for power flow insolvability condition

%   MATPOWER
%   Copyright (c) 2013-2016 by Power System Engineering Research Center (PSERC)
%   by Daniel Molzahn, PSERC U of Wisc, Madison
%   and Ray Zimmerman, PSERC Cornell
%
%   This file is part of MATPOWER.
%   Covered by the 3-clause BSD License (see LICENSE file for details).
%   See http://www.pserc.cornell.edu/matpower/ for more info.

if nargin < 1
    quiet = 0;
end

num_tests = 8;

t_begin(num_tests, quiet);

[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;
[F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
    TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
    ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
[PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;

casefile = 't_case9mod_opf';
if quiet
    verbose = 0;
else
    verbose = 0;
end

t0 = 'INSOLVABLEPF : ';

%% test an insolvable case
load soln9mod_opf;     %% defines bus_soln, gen_soln, branch_soln, Vslack_min_soln, sigma_soln, etacomp_soln

res = loadcase(casefile);
res.bus = bus_soln;
res.gen = gen_soln;
res.branch = branch_soln;

% Make the case insolvable
mult = 10;
res.bus(:,PD) = mult*res.bus(:,PD);
res.bus(:,QD) = mult*res.bus(:,QD);
res.gen(:,PG) = mult*res.gen(:,PG);

mpopt = mpoption('out.all', 0, 'verbose', verbose);

%% get test results for insolvable case
t = [t0 '(insolvable case) :'];
[insolvable,Vslack_min,sigma,eta,mineigratio] = insolvablepf(res,mpopt);
t_ok(insolvable, [t ' insolvable']);
t_is(Vslack_min, Vslack_min_soln, 3, [t ' Vslack_min']);
t_is(sigma, sigma_soln, 3, [t ' sigma']);
t_is(eta, eta_soln, 3, [t ' eta']);


%% test a solvable case
load soln9mod_opf;     %% defines bus_soln, gen_soln, branch_soln, Vslack_min_soln, sigma_soln, etacomp_soln

res = loadcase(casefile);
res.bus = bus_soln;
res.gen = gen_soln;
res.branch = branch_soln;

%% get test results for insolvable case
t = [t0 '(solvable case) :'];
[insolvable,Vslack_min,sigma,eta,mineigratio] = insolvablepf(res,mpopt);
t_ok(~insolvable, [t ' solvable']);
t_is(Vslack_min, Vslack_min_soln / sqrt(mult), 3, [t ' Vslack_min']);
t_is(sigma, sigma_soln * sqrt(10), 3, [t ' sigma']);
t_is(eta, eta_soln * mult, 3, [t ' eta']);

t_end;
