function om = add_vars(om, name, N, v0, vl, vu)
%ADD_VARS  Adds a set of variables to the model
%   om = add_vars(om, name, N, v0, vl, vu)
%   Adds a set of variables to the model, where N is the number of
%   variables in the set, v0 is the initial value of those variables,
%   and vl and vu are the lower and upper bounds on the variables.

%   MATPOWER
%   $Id: add_vars.m,v 1.3 2009/11/04 15:58:44 ray Exp $
%   by Ray Zimmerman, PSERC Cornell
%   Copyright (c) 2008 by Power System Engineering Research Center (PSERC)
%   See http://www.pserc.cornell.edu/matpower/ for more info.

%% prevent duplicate named var sets
if isfield(om.var.idx.N, name)
    error('@opf_model/add_vars: variable set named ''%s'' already exists', name);
end

%% initialize args and assign defaults
if nargin < 6
    vu = [];
    if nargin < 5
        vl = [];
        if nargin < 4
            v0 = [];
        end
    end
end
if isempty(v0)
    v0 = zeros(N, 1);           %% init to zero by default
end
if isempty(vl)
    vl = -Inf * ones(N, 1);     %% unbounded below by default
end
if isempty(vu)
    vu = Inf * ones(N, 1);      %% unbounded above by default
end

%% add info about this var set
om.var.idx.i1.(name)  = om.var.N + 1;   %% starting index
om.var.idx.iN.(name)  = om.var.N + N;   %% ending index
om.var.idx.N.(name)   = N;              %% number of vars
om.var.data.v0.(name) = v0;             %% initial value
om.var.data.vl.(name) = vl;             %% lower bound
om.var.data.vu.(name) = vu;             %% upper bound

%% update number of vars and var sets
om.var.N  = om.var.idx.iN.(name);
om.var.NS = om.var.NS + 1;

%% put name in ordered list of var sets
om.var.order{om.var.NS} = name;
