function [xsol,dsol,r,Jx,Jd,val,ter,dir,p,z0,St,Co,fnames,stypes,options] = getNetworkPars_v1(x0,d0)
%[xsol,dsol,r,Jx,Jd,val,ter,dir,p,z0,St,Co,fnames,stypes,options] = getNetworkPars_v1(x0,d0)
%INPUTS: 
%   x0 = state at t=0
%   d0 = state derivative at t=0
%OUTPUTS: 
%   various parameters and test values
%DESCRIPTION OF PARAMETER VECTOR:
% p{1} = [X1 X2 Vm Vp ns Qm Qp];
% p{2} = [tauT];
% p{3} = [tauC];
% p{4} = [Tp Ps Pt];
% p{5} = [C nCT];
% p{6} = [Xs Xm Xr Rs Rr ws Jg Tm];
%DESCRIPTION OF CONTINUOUS STATE VECTOR
% x0{1} = [V0R V0I V2R V2I V3R V3I Pd Qd IsR IsI IcR IcI];
% x0{2} = [xTT yTT];
% x0{3} = [xTC yTC];
% x0{4} = [xd Pd Qd V3R V3I];
% x0{5} = [IcR IcI V2R V2I];
% x0{6} = [IsR IsI IrR IrI wr V2R V2I];
%DESCRIPTION OF DISCRETE SYSTEM STATE VECTOR
% z0{1} = [nT nTq];
% z0{2} = [tresT onT];
% z0{3} = [tresC onC];
% z0{4} = [];
% z0{5} = [nC nCq];
% z0{6} = [];

%PHYSICAL MEANING OF ALL OF THE ABOVE QUANTITIES AND MODULES
%MODULE 1: [network with voltmeter]
%-parameters
%   (V0R, V0I) = real and imaginary parts of voltage phasor at base node
%   (X1,X2) = line impedences
%   Vm = minimum voltage at node 2
%   Vp = maximum voltage at node 2
%   Qm = minimum active power at node 2
%   Qp = maximum active power at node 2
%   nsT = tap step size
%-continous states
%   (V2R,V2I) = real and imaginary parts of voltage phasor at node 2
%   (V3R,V3I) = real and imaginary parts of voltage phasor at node 3
%   (Pd,Qd) = active and reactive load power consumption
%   (IsR,IsI) = wind current consumption, real and imaginary stator currents
%   (IcR,IcI) = capacitor current consumptions, real and imaginary parts
%-discrete states
%   nT = current tap position
%   nTq = requested tap position
%MODULE 2: [timer for tap changer]
%-parameters
%   tauT = timer stop time
%-continous states
%   xTT = total timer on time 
%   yTT = time elapsed since last reset
%-discrete states
%   tresT = time of last reset
%   onT = timer setting (1 = timer on, 0 = timer off)
%MODULE 3: [timer for capacitor bank]
%-parameters
%   tauC = timer stop time
%-continous states
%   xTC = total timer on time 
%   yTC = time elapsed since last reset
%-discrete states
%   tresC = time of last reset
%   onC = timer setting (1 = timer on, 0 = timer off)
%MODULE 4: [load]
%-parameters
%   Tp = load recovery time constant
%   Ps = steady state power demand
%   Pt = load perturbation factor 
%-continous states
%   xd = load demand state
%   Pd = load power demand
%   (V3R,V3I) = node two real and imaginary voltages
%-discrete states
%   no discrete states
%MODULE 5: [capacitor bank]
%-parameters
%   C = capacitance of a single capacitor
%   nCT = number of capacitors in bank
%-continuous states
%   (IcR,IcI) = current injection from capacitor bank
%   (V2R,V2I) = real and imaginary parts of voltage phasor at node 2
%-discrete states
%   nC = number of connected capacitors
%   nCq = requested capacitor position
%MODULE 6: [wind turbine]
%-parameters
%   Xs = stator impedance
%   Xm = mutual impedance
%   Xr = rotor impedance
%   Rs = stator resistance
%   Rr = rotor resistance
%   ws = stator frequency
%   Jg = gear box and turbine rotational inertia
%   Tm = mechanical torque from turbine
%-continuous states
%   (IsR,IsI) = wind current injection, real and imaginary stator currents
%   (IrR,IrI) = real and imaginary rotor currents
%   wr = rotor rotational frequency
%   (V2R,V2I) = real and imaginary parts of voltage phasor at node 2
%-discrete states
%   no discrete states


%specify parameter values [do not change]
X1 = 0.03; 
X2 = .05; 
Vm = 1.065;
Vp = 1.075;
Qm = -.25;
Qp = .25;
nsT = 0.075;
nsC = .2;
tauT = .5;
tauC = .5;
Tp = 5;
Ps = 3;
Pt = .4;
C = 3.4;
nCT = 5;
Xs = .0742;
Xm = .27626;
Xr = .1761;
Rs = .00121;
Rr = .04;
ws = 1;
Jg = 16;
Tm = 8.1212;

%specify a guess for initial continuous states
V0R = 1;
V0I = 0;
V2R = 1;
V2I = 0;
V3R = 1;
V3I = 0;

xTT = 0;
yTT = 0;
xTC = 0;
yTC = 0;

wr = 1.003*ws; %frequency must be above network frequency

IsR = .5;
IsI = .5;
IcR = .5;
IcI = .5;

IrR = 0;
IrI = 1;


xd = Ps - Pt*(V3R^2 + V3I^2); %value at eq.
Pd = Ps; %value at eq.
Qd = .01;

%specify intial discrete states
nT = .85;%[.7,1]
tresT = 0;
onT = 0;
tresC = 0;
onC = 0;
nC = 2.875;%[2.5 2.87 3]

%specify initial DES states
q0 = ones(3,1);


%collect all values into cell arrays
p{1} = [X1 X2 Vm Vp nsT Qm Qp nsC];
p{2} = [tauT];
p{3} = [tauC];
p{4} = [Tp Ps Pt];
p{5} = [C nCT];
p{6} = [Xs Xm Xr Rs Rr ws Jg Tm];

z0{1} = [nT nT];
z0{2} = [tresT onT];
z0{3} = [tresC onC];
z0{4} = [];
z0{5} = [nC nC];
z0{6} = [];

%state cell array includes state names to allow automatic module
%interconnection
x{1,1} = [V0R V0I V2R V2I V3R V3I Pd Qd IsR IsI IcR IcI];
x{1,2} = {'V0R' 'V0I' 'V2R' 'V2I' 'V3R' 'V3I' 'Pd' 'Qd' 'IsR' 'IsI' 'IcR' 'IcI'};
x{1,3} = {'C' 'C' 'A' 'A' 'A' 'A' 'A' 'A' 'A' 'A' 'A' 'A'};
x{2,1} = [xTT yTT];
x{2,2} = {'xTT' 'yTT'};
x{2,3} = {'C' 'A'};
x{3,1} = [xTC yTC];
x{3,2} = {'xTC' 'yTC'};
x{3,3} = {'C' 'A'};
x{4,1} = [xd Pd Qd V3R V3I];
x{4,2} = {'xd' 'Pd' 'Qd' 'V3R' 'V3I'};
x{4,3} = {'C' 'A' 'A' 'A' 'A'};
x{5,1} = [IcR IcI V2R V2I];
x{5,2} = {'IcR' 'IcI' 'V2R' 'V2I'};
x{5,3} = {'A' 'A' 'A' 'A'};
x{6,1} = [IsR IsI IrR IrI wr V2R V2I];
x{6,2} = {'IsR' 'IsI' 'IrR' 'IrI' 'wr' 'V2R' 'V2I'};
x{6,3} = {'A' 'A' 'A' 'A' 'C' 'A' 'A'};


%specify ode, jacobian, event function handles
fnames = ...
    {@dNetwork @eNetwork @jNetwork;...
    @dTimerT @eTimerT @jTimerT;...
    @dTimerC @eTimerC @jTimerC;...
    @dLoad '' @jLoad;...
    @dCapacitor '' @jCapacitor;...
    @dTurbine '' @jTurbine};

%reorganize system algebraic and continuous states
stypes = [];
for i = 1:size(x,1)
    stypes = [stypes strcmp(x{i,3},'C')];
end

%define system events
events{1} = {'vm','vp','vn','qm','qp','qn'};
events{2} = {'ttstop'};
events{3} = {'tcstop'};
events{4} = {};
events{5} = {};
events{6} = {};
%reorganize system events into a single cell array
sevents = [];
for i = 1:length(events)
    sevents = [sevents events{i}];
end

%compute modular state structure from state array x
[St, Co] = StateStructure(x);

%===============BELOW SECTION IS UP TO YOU TO COMPLETE================
%compute the system residue at t=0
r = odefun(...);
%compute the system Jacobian at t = 0
[Jx,Jd] = jfun(...);
%compute the system event function, i.e., val, ter, dir at t=0
[val,ter,dir] = efun(...);
%set ode15i options
options = odeset(...);
%execute a single run of continuous dynamic from t = 0 to t = 6
sol = ode15i(...);

%retrieve the values of the state and the state derivative at t = [0:.1:6]
[xsol,dsol] = deval(...);



