function t_opf(quiet)
%T_OPF  Tests for optimal power flow.

%   MATPOWER
%   $Id: t_opf.m,v 1.6 2004/09/07 20:34:36 ray Exp $
%   by Ray Zimmerman, PSERC Cornell
%   Copyright (c) 2004 by Power System Engineering Research Center (PSERC)
%   See http://www.pserc.cornell.edu/matpower/ for more info.

if nargin < 1
    quiet = 0;
end

t_begin(55, quiet);

casefile = 't_case9_opf';
if quiet
    verbose = 0;
else
    verbose = 0;
end
mpopt = mpoption('OUT_ALL', 0, 'VERBOSE', verbose);

%% get solved AC power flow case from MAT-file
load soln9_opf;     %% defines bus_soln, gen_soln, branch_soln

%% run default OPF
t = 'default OPF : ';
[baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
t_ok(success, [t 'success']);
t_is(f, f_soln, 4, [t 'f']);
t_is(bus, bus_soln, 3, [t 'bus']);
t_is(gen, gen_soln, 4, [t 'gen']);
t_is(branch, branch_soln, 4, [t 'branch']);

%% run minopf OPF
if have_fcn('minopf')
    t = 'minopf OPF : ';
    mpopt = mpoption(mpopt, 'OPF_ALG', 500);
    [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
    t_ok(success, [t 'success']);
    t_is(f, f_soln, 2, [t 'f']);
    t_is(bus, bus_soln, 3, [t 'bus']);
    t_is(gen, gen_soln, 2, [t 'gen']);
    t_is(branch, branch_soln, 2, [t 'branch']);
else
    t_skip(5, 'MINOPF not available');
end

%% run fmincon OPF
if have_fcn('fmincon')
    t = 'fmincon OPF : ';
    mpopt = mpoption(mpopt, 'OPF_ALG', 520);
    [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
    t_ok(success, [t 'success']);
    t_is(f, f_soln, 2, [t 'f']);
    t_is(bus, bus_soln, 2, [t 'bus']);
    t_is(gen, gen_soln, 2, [t 'gen']);
    t_is(branch, branch_soln, 2, [t 'branch']);
else
    t_skip(5, 'fmincon not available');
end

%% run constr OPF
if have_fcn('constr')
    t = 'constr OPF : ';
    mpopt = mpoption(mpopt, 'OPF_ALG', 200);
    [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
    t_ok(success, [t 'success']);
    t_is(f, f_soln, 2, [t 'f']);
    t_is(bus, bus_soln, 2, [t 'bus']);
    t_is(gen, gen_soln, 2, [t 'gen']);
    t_is(branch, branch_soln, 2, [t 'branch']);
else
    t_skip(5, 'constr not available');
end

%% run dense LP-based OPF
t = 'dense LP-based OPF : ';
mpopt = mpoption(mpopt, 'OPF_ALG', 220);
[baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
t_ok(success, [t 'success']);
t_is(f, f_soln, 2, [t 'f']);
t_is(bus, bus_soln, 2, [t 'bus']);
t_is(gen, gen_soln, 2, [t 'gen']);
t_is(branch, branch_soln, 2, [t 'branch']);

%% run sparse (relaxed) LP-based OPF
t = 'sparse (relaxed) LP-based OPF : ';
mpopt = mpoption(mpopt, 'OPF_ALG', 240);
[baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
t_ok(success, [t 'success']);
t_is(f, f_soln, 2, [t 'f']);
t_is(bus, bus_soln, 2, [t 'bus']);
t_is(gen, gen_soln, 2, [t 'gen']);
t_is(branch, branch_soln, 2, [t 'branch']);

%% run sparse (full) LP-based OPF
t = 'sparse (full) LP-based OPF : ';
mpopt = mpoption(mpopt, 'OPF_ALG', 260);
[baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
t_ok(success, [t 'success']);
t_is(f, f_soln, 2, [t 'f']);
t_is(bus, bus_soln, 2, [t 'bus']);
t_is(gen, gen_soln, 2, [t 'gen']);
t_is(branch, branch_soln, 2, [t 'branch']);

%% get solved AC power flow case from MAT-file
load soln9_opf_Plim;       %% defines bus_soln, gen_soln, branch_soln, f_soln

%% run fmincon OPF with active power line limits
mpopt = mpoption(mpopt, 'OPF_P_LINE_LIM', 1);
if have_fcn('fmincon')
    t = 'fmincon OPF (P line lim) : ';
    mpopt = mpoption(mpopt, 'OPF_ALG', 520);
    [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
    t_ok(success, [t 'success']);
    t_is(f, f_soln, 2, [t 'f']);
    t_is(bus(:,1:15), bus_soln(:,1:15), 1, [t 'bus']);  %% skip MU_VMAX, MU_VMIN multipliers
    t_is(gen, gen_soln, 1, [t 'gen']);
    t_is(branch, branch_soln, 1, [t 'branch']);
else
    t_skip(5, 'fmincon not available');
end

%% run constr OPF with active power line limits
if have_fcn('constr')
    t = 'constr OPF (P line lim) : ';
    mpopt = mpoption(mpopt, 'OPF_ALG', 200);
    [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
    t_ok(success, [t 'success']);
    t_is(f, f_soln, 2, [t 'f']);
    t_is(bus, bus_soln, 2, [t 'bus']);
    t_is(gen, gen_soln, 2, [t 'gen']);
    t_is(branch, branch_soln, 2, [t 'branch']);
else
    t_skip(5, 'constr not available');
end

%% run sparse (relaxed) LP-based OPF with active power line limits
t = 'sparse (relaxed) LP-based OPF (P line lim) : ';
mpopt = mpoption(mpopt, 'OPF_ALG', 240);
[baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
t_ok(success, [t 'success']);
t_is(f, f_soln, 2, [t 'f']);
t_is(bus, bus_soln, 2, [t 'bus']);
t_is(gen, gen_soln, 2, [t 'gen']);
t_is(branch, branch_soln, 2, [t 'branch']);

mpopt = mpoption(mpopt, 'OPF_P_LINE_LIM', 0);

%% get solved AC power flow case from MAT-file
load soln9_dcopf;       %% defines bus_soln, gen_soln, branch_soln

%% run DC OPF
t = 'DC OPF : ';
[baseMVA, bus, gen, gencost, branch, f, success, et] = rundcopf(casefile, mpopt);
t_ok(success, [t 'success']);
t_is(f, f_soln, 3, [t 'f']);
t_is(bus, bus_soln, 3, [t 'bus']);
t_is(gen, gen_soln, 3, [t 'gen']);
t_is(branch, branch_soln, 3, [t 'branch']);

t_end;

return;
