function compare(case1, case2)
%COMPARE  Compares two solved power flow cases.
%   compare(case1, case2)
%   Compares two solved power flow (or optimal power flow) cases and prints
%   a summary of the differences.
%

%   MATPOWER
%   $Id: compare.m,v 1.4 2004/11/02 15:31:16 ray Exp $
%   by Ray Zimmerman, PSERC Cornell
%   Copyright (c) 1996-2004 by Power System Engineering Research Center (PSERC)
%   See http://www.pserc.cornell.edu/matpower/ for more info.

%% define named indices into bus, gen, branch 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] = 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] = idx_brch;

%% set up index name matrices
buscols = str2mat(  'BUS_I', ...
                    'BUS_TYPE', ...
                    'PD', ...
                    'QD', ...
                    'GS', ...
                    'BS', ...
                    'BUS_AREA', ...
                    'VM', ...
                    'VA', ...
                    'BASE_KV'   );
buscols = str2mat(  buscols, ...
                    'ZONE', ...
                    'VMAX', ...
                    'VMIN', ...
                    'LAM_P', ...
                    'LAM_Q', ...
                    'MU_VMAX', ...
                    'MU_VMIN'   );

gencols = str2mat(  'GEN_BUS', ...
                    'PG', ...
                    'QG', ...
                    'QMAX', ...
                    'QMIN', ...
                    'VG', ...
                    'MBASE', ...
                    'GEN_STATUS', ...
                    'PMAX', ...
                    'PMIN'      );
gencols = str2mat(  gencols, ...
                    'MU_PMAX', ...
                    'MU_PMIN', ...
                    'MU_QMAX', ...
                    'MU_QMIN'   );

brcols = str2mat(   'F_BUS', ...
                    'T_BUS', ...
                    'BR_R', ...
                    'BR_X', ...
                    'BR_B', ...
                    'RATE_A', ...
                    'RATE_B', ...
                    'RATE_C', ...
                    'TAP', ...
                    'SHIFT'     );
brcols = str2mat(   brcols, ...
                    'BR_STATUS', ...
                    'PF', ...
                    'QF', ...
                    'PT', ...
                    'QT', ...
                    'MU_SF', ...
                    'MU_ST' );

%% read data & convert to internal bus numbering
[baseMVA1, bus1, gen1, branch1, area1, gencost1] = loadcase(case1);
[baseMVA2, bus2, gen2, branch2, area2, gencost2] = loadcase(case2);

%% print results
fprintf('----------------  --------------  --------------  --------------  -----\n');
fprintf(' matrix / col         case 1          case 2        difference     row \n');
fprintf('----------------  --------------  --------------  --------------  -----\n');

%% bus comparison
[temp, i] = max(abs(bus1 - bus2));
[v, gmax] = max(temp);
i = i(gmax);
fprintf('bus\n');
for j = 1:size(buscols, 1)
    [v, i] = max(abs(bus1(:, j) - bus2(:, j)));
    if v
        if j == gmax, s = ' *'; else, s = ''; end
        fprintf('  %-12s%16g%16g%16g%7d%s\n', buscols(j, :), bus1(i, j), bus2(i, j), v, i, s );
    end
end

%% gen comparison
[temp, i] = max(abs(gen1 - gen2));
[v, gmax] = max(temp);
i = i(gmax);
fprintf('\ngen\n');
for j = 1:size(gencols, 1)
    [v, i] = max(abs(gen1(:, j) - gen2(:, j)));
    if v
        if j == gmax, s = ' *'; else, s = ''; end
        fprintf('  %-12s%16g%16g%16g%7d%s\n', gencols(j, :), gen1(i, j), gen2(i, j), v, i, s );
    end
end

%% branch comparison
[temp, i] = max(abs(branch1 - branch2));
[v, gmax] = max(temp);
i = i(gmax);
fprintf('\nbranch\n');
for j = 1:size(brcols, 1)
    [v, i] = max(abs(branch1(:, j) - branch2(:, j)));
    if v
        if j == gmax, s = ' *'; else, s = ''; end
        fprintf('  %-12s%16g%16g%16g%7d%s\n', brcols(j, :), branch1(i, j), branch2(i, j), v, i, s );
    end
end

return;
