function power_flow(filename)
%POWER_FLOW Function to perform a power-flow analysis.
% Function power_flow reads a data set describing a power system
% from a disk file, and performs a power-flow analysis on it.
% The only argument is the name of the input file. There are
% three types of lines in the input file. The "SYSTEM" line
% specifies the name of the power system and the base apparent
% power of the power system in MVA. Its form is:
%
% SYSTEM name baseMVA tol
%
% where
% name = The name of the power system
% baseMVA = The base MVA of the power system
% tol = Voltage tolerance
%
% The "BUS" line specifies the characteristics of a bus on the
% power system. Its form is:
%
% BUS name type volts Pgen Qgen Pload Qload Qcap
%
% where
% name = The name of the bus
% type = The type of the bus, one of PQ, PV, or SL
% Vbus = The initial voltage guess for PQ busses
% The fixed magnitude of voltage PV busses
% The fixed magnitude at an angle of 0 deg for Sl busses
% Pgen = Is the real power generation in MW at the bus
% Qgen = Is the reactive power generation in MVAR at the bus
% Pload = Is the real power load in MW at the bus
% Qload = Is the reactive power load in MVAR at the bus
% Qcap = Is the reactive power of capacitors in MVAR at the bus
%
% The "LINE" line specifies the characteristics of a transmission
% line on the power system. Note that the impedance of the
% transformers in series with the transmission line should also
% be lumped into these terms. Its form is:
%
% LINE from to Rse Xse Gsh Bsh Rating
%
% where
% from = The name of the "from" bus
% to = The type of the "to" bus
% Rse = Per-unit series resistance
% Xse = Per-unit series reactance
% Gsh = Per-unit shunt conductance
% Bsh = Per-unit shunt susceptance
% Rating = Max power rating of the line in MVAR
%
% The lines should appear in the order:
%
% SYSTEM ...
% BUS ...
% LINE ...
%
% The program reads the data from the input file and solves for the
% voltages at every bus. Then, it generates a report giving the
% voltages and power flows throughout the system.
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 01/12/00 S. J. Chapman Original code
% Get the power system data
[bus, line, system] = read_system(filename);
% Byild Ybus
ybus = build_ybus(bus, line);
% Solve for the bus voltages
[bus, n_iter] = solve_system(bus, ybus);
% Display results
report_system(1, bus, line, system, ybus, n_iter);
%================================================================
%================================================================
function [bus, line, system] = read_system(filename)
%READ_SYSTEM Read a power system from disk.
% Function read_system reads a data set describing a power system
% from a disk file. There are three types of lines in the input
% file. The "SYSTEM" line specifies the name of the power system
% and the base apparent power of the power system in MVA. Its
% form is:
%
% SYSTEM name baseMVA tol
%
% where
% name = The name of the power system
% baseMVA = The base MVA of the power system
% tol = Voltage tolerance
%
% The "BUS" line specifies the characteristics of a bus on the
% power system. Its form is:
%
% BUS name type volts Pgen Qgen Pload Qload Qcap
%
% where
% name = The name of the bus
% type = The type of the bus, one of PQ, PV, or SL
% Vbus = The initial voltage guess for PQ busses
% The fixed magnitude of voltage PV busses
% The fixed magnitude at an angle of 0 deg for Sl busses
% Pgen = Is the real power generation in MW at the bus
% Qgen = Is the reactive power generation in MVAR at the bus
% Pload = Is the real power load in MW at the bus
% Qload = Is the reactive power load in MVAR at the bus
% Qcap = Is the reactive power of capacitors in MVAR at the bus
%
% The "LINE" line specifies the characteristics of a transmission
% line on the power system. Note that the impedance of the
% transformers in series with the transmission line should also
% be lumped into these terms. Its form is:
%
% LINE from to Rse Xse Gsh Bsh Rating
%
% where
% from = The name of the "from" bus
% to = The type of the "to" bus
% Rse = Per-unit series resistance
% Xse = Per-unit series reactance
% Gsh = Per-unit shunt conductance
% Bsh = Per-unit shunt susceptance
% Rating = Max power rating of the line in MVAR
%
% The lines should appear in the order:
%
% SYSTEM ...
% BUS ...
% LINE ...
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 01/12/00 S. J. Chapman Original code
% Check for a legal number of input arguments.
msg = nargchk(1,1,nargin);
error(msg);
% Initialise counters
n_system = 0; % Number of SYSTEM cards
n_bus = 0; % Number of BUS cards
n_lines = 0; % Number of LINE cards
n_bad = 0; % Number of INVALID cards
n_comment = 0; % Number of comment lines
i_line = 0; % Current line number
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Open input file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[fid,message] = fopen(filename,'r'); % FID = -1 for failure.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check for error
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if fid < 0
str = ['ERROR: Can''t find system file: ' filename];
error(str);
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File open OK, so read lines.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while feof(fid) == 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get next line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data = fgetl(fid);
i_line = i_line + 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Extract keyword
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
maxlen = min( [ 6 length(data) ] );
keyword = data(1:maxlen);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determine the type of the line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strncmpi(keyword,'SYSTEM',6) == 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a SYSTEM card
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_system = n_system + 1;
% Get system name
blanks = findstr(data,' ');
test = diff(blanks) > 1;
for ii = 1:length(test)
if test(ii) > 0
system.name = data(blanks(ii)+1:blanks(ii+1)-1);
break;
end
end
% Get base MVA
ii = blanks(ii+1);
temp = sscanf(data(ii:length(data)),'%g');
system.baseMVA = temp(1);
% Voltage tolerance
system.v_tol = temp(2);
elseif strncmpi(keyword,'BUS',3) == 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a BUS card
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_bus = n_bus + 1;
% Confirm that a SYSTEM card has preceeded this card
if n_system == 0
error (['ERROR: A SYSTEM card must precede any BUS cards!']);
end
% Get bus name
blanks = findstr(data,' ');
test = diff(blanks) > 1;
for ii = 1:length(test)
if test(ii) > 0
bus(n_bus).name = data(blanks(ii)+1:bl