Sei sulla pagina 1di 7

function []=mainmain()

format;
load -mat rc_line.dat;
%LINELEM= [86 0 1 0 2 3 5.0e-10 2.0e+00 2.0e-9 2 2.5e-9 0; 82 100 1 2 0 0 0 0 0
0 0 0 ; 67 5.0e-13 2 0 NaN 0 0 0 0 0 0 0 ];
%NODES=[101 102];
% to check if file is loaded the following command is given
%disp(LINELEM);
global BetaP;
%BetaP=0.5*MU*COX*30e-6*0.35e-6;
global deltaT;
deltaT= 1e-10;%1.0e-9;
global simTime;
simTime= 0;%1.0e-5;
global timeStep;
timeStep=deltaT;
global NODES1;
NODES1=NODES;
%NODES=[103 105 ];
[LIN_mat_rowNumber,LIN_mat_cols]=size(LINELEM); % number of linear elemts
%[NLN_mat_rowNumber, Cols]=size(NLNELEM);
%[NLN_mat_size,LIN_mat_cols]=size(NLNELEM); %number of non linear elements
global MNA_size;
MNA_size = numel(NODES1);% calculating number of nodes
disp(MNA_size);
global numberOfNodes;
numberOfNodes= numel(NODES);
global DC_NV;
DC_NV=zeros(numberOfNodes,1);
global Y_Mat; % creation of zero matrix for storing stamp values
Y_Mat = zeros(MNA_size);
global v_Mat ; %creation of v matrix
v_Mat = zeros (MNA_size, 1);
global J_Mat; % creation of J matrix
J_Mat= zeros(MNA_size,1);
global branchCurrent; % to store branch currents
branchCurrent=zeros(MNA_size,1);
global storeCurrent; % Index used to stamp voltage, needed to get last index use
d
storeCurrent=0;
global storeCurrentIndex;
storeCurrentIndex=1;
%capIndexStore = 1;%% Stamp Volatge Last index used
global New_MNA_size;
New_MNA_size= MNA_size;
global localVolt; % to be used for storing instant voltages in stampCap()
localVolt=0;
global localCur; % to be used for storing instant curent in stampCap()
localCur=0;
global timeElapsed; % to be used for storing instant curent in stampCap()
timeElapsed=0;
global capCount;
% to be used for storing instant count of caps to find curr
ent for localCur
capCount=0;
global b;
global iterationC; % to be used for iteration count such that new iterations cou
d be done w/o creating new memory
iterationC=0;
global iterationL; % for inductor
iterationL=0;

global capIndexStoreIndex;
capIndexStoreIndex=1;
global localVolti;
localVolti=0;
global caps;% this will store all the capacitor values
global loopInt3;
loopInt3=1;
global final_out;
final_out=1;
global variable;
variable=0;
global nodeVoltage;
nodeVoltage=0;
global t;% used in trans function to set voltages
t=0;
global RUN; RUN=0;
notToBeUsedVaribale4=1;

%do_DC(LINELEM,NLNELEM, LIN_mat_rowNumber);% for running rc_mesh this is


%commented
do_DC(LINELEM, LIN_mat_rowNumber);% solves the dc operating point of system
b=linsolve(Y_Mat, J_Mat);
for j=1:numberOfNodes
DC_NV(j,1)=b(j,1); % This is for storing DC_Node volatges. DC_Node Currents
can be stored in similar way if required
end
% DC Branch Currents
% for j=1:numberOfNodes
%
branchCurrent(j,1)=;
% end
%disp(b);
disp('DC Y_Mat'); disp(Y_Mat);
disp('DC_NV:');disp(DC_NV);
disp('DC J_Mat=');disp(J_Mat);
% END OF DC__END OF DC__END OF DC__END OF DC__END OF DC__END OF DC__END OF DC__
END OF DC__
while (timeElapsed<=simTime) %% iterations for transient values
New_MNA_size= MNA_size;
do_Trans(LINELEM, LIN_mat_rowNumber, timeElapsed);
RUN=1;% to stop capacitors from stamping again
storeCurrentIndex=2;
b=linsolve(Y_Mat, J_Mat);
disp('Y_Mat=');disp(Y_Mat);disp('b');disp(b);disp('J_Mat');disp(J_Mat);
timeElapsed=timeStep + timeElapsed;
nodeVoltage(notToBeUsedVaribale4,1)=b(2,1);
notToBeUsedVaribale4=notToBeUsedVaribale4+1;
end
for j=1:numberOfNodes
DC_NV(j,1)=b(j,1); % This is for storing DC_Node volatges. This is for Tran
s DC Node Voltages
end

disp('Y_Mat after last iteration');


disp(Y_Mat);
disp('NV after last iteration');
disp(DC_NV);
%disp('node voltage of first node cumulative:');
[sizeOfFinal,avein]=size(nodeVoltage);
disp( sizeOfFinal);
t1=deltaT:deltaT:simTime; disp('size of t1=');x=size(t1); disp(x);
plot(t1,nodeVoltage);
end

function []=do_DC(LINELEM, LIN_mat_rowNumber)


global Y_Mat;
global v_Mat;
global J_Mat;
global New_MNA_size;
%global b;
for i=1:LIN_mat_rowNumber
if(LINELEM(i,1)== 82) %RESISTOR____RESISTOR____RESISTOR____RESISTOR____RESIS
TOR____RESISTOR____RESISTOR____
%fprintf('cond= %f row=%d col=%d',1/LINELEM(i,2), LINELEM(i,3), LINELEM(
i,4) );
stampCond((1/LINELEM(i,2)), LINELEM(i,3), LINELEM(i,4));
elseif (LINELEM(i,1)== 73) %CURENT__CURENT__CURENT__CURENT__CURENT__CURENT__
CURENT__CURENT__CURENT__CURENT__
stampCur(LINELEM(i,2), LINELEM(i,3), LINELEM(i,4));
elseif (LINELEM(i,1)== 86) %VOLTAGE__VOLTAGE__VOLTAGE__VOLTAGE__VOLTAGE__VOL
TAGE__VOLTAGE__VOLTAGE__VOLTAGE__
New_MNA_size=New_MNA_size+1;
Y_Mat(New_MNA_size,1)=0;
Y_Mat(1,New_MNA_size)=0;
v_Mat(New_MNA_size,1)=0; % New v_Mat formation
J_Mat(New_MNA_size,1)=0; % New row for J_Mat
stampVol(LINELEM(i,2), LINELEM(i,3), LINELEM(i,4));
elseif(LINELEM(i,1)== 67) %CAPACITOR____CAPACITOR____CAPACITOR____CAPACITOR_
___CAPACITOR____CAPACITOR____
stampCond(0, LINELEM(i,3), LINELEM(i,4));
elseif(LINELEM(i,1)== 76) %INDUCTOR____INDUCTOR____INDUCTOR____INDUCTOR____I
NDUCTOR____INDUCTOR____INDUCTOR____
stampCond(inf, LINELEM(i,3), LINELEM(i,4));
end
end
for i=1:LIN_mat_rowNumber
end

end
function []=do_Trans(LINELEM, LIN_mat_rowNumber,time)
global MNA_size;

global NODES1;
MNA_size = numel(NODES1); % Re-initializing MNA_size to start Trans response
global New_MNA_size;
New_MNA_size= MNA_size;
global Y_Mat;
global J_Mat;
global v_Mat;
global capIndexStore;
global deltaT;
global b;
global iterationC;
global simTime;
global t;
global RUN; %for capacitors to stop stamping after first iteration
global storeCurrent;
global storeCurrentIndex;
for i=1:LIN_mat_rowNumber
if (LINELEM(i,1)== 86) %VOLTAGE__VOLTAGE__VOLTAGE__VOLTAGE__VOLTAGE__VOLTAG
E__VOLTAGE__VOLTAGE__VOLTAGE__
if (LINELEM(i,5)==2)
simTime= LINELEM(i,5+(2*(LINELEM(i,6)))); % calculates the simu
lation time for ckt
New_MNA_size=New_MNA_size+1;
%test
if ((time+deltaT)>= LINELEM(i,7+2*t))
t=t+1;
if(t==(LINELEM(i,6)))
t=t-1;
end
end
if((time+deltaT)< LINELEM(i,7+2*t) )
if(t==0)
SLOPE= LINELEM(i,8)/LINELEM(i,7);
stampVol((deltaT+time)*SLOPE, LINELEM(i,3), LINELEM(i,4)
);
else
SLOPE= (LINELEM(i,8+2*t)-LINELEM(i,8+2*(t-1)))/ (LINELE
M(i,7+2*t)-LINELEM(i,7+2*(t-1)));
stampVol((((deltaT+time-LINELEM(i,7+2*(t-1)))*SLOPE)+ LI
NELEM(i,8+2*(t-1))), LINELEM(i,3), LINELEM(i,4));
end
end
end
elseif ((LINELEM(i,1)== 67)) %CAPACITOR__CAPACITOR__CAPACITOR__CAPACITOR__CA
PACITOR__CAPACITOR__CAPACITOR__
if(RUN==0)
if (iterationC==0)
if(LINELEM(i,4)<1)
stampCur((2*LINELEM(i,2)*(b(LINELEM(i,3))))/deltaT
, LINELEM(i,3), LINELEM(i,4));
%stampCur( (b(LINELEM(i,3)))*(LINELEM(i,2)/deltaT)
, LINELEM(i,3), LINELEM(i,4));
stampCond(2*(LINELEM(i,2)/deltaT), LINELEM(i,3),
LINELEM(i,4));
iterationC=1;

storeCurrent(storeCurrentIndex,1)=(2*LINELEM(i,2)*
(b(LINELEM(i,3))))/deltaT;
storeCurrentIndex=storeCurrentIndex+1;
else
stampCur((2*LINELEM(i,2)*(b(LINELEM(i,3))-b(LINELEM(i
,4))))/deltaT, LINELEM(i,3), LINELEM(i,4));
%stampCur((b(LINELEM(i,3))-b(LINELEM(i,4)))*(LINELEM(
i,2)/deltaT), LINELEM(i,3), LINELEM(i,4));
stampCond(2*(LINELEM(i,2)/deltaT), LINELEM(i,3), LINE
LEM(i,4));
iterationC=1;
storeCurrent(storeCurrentIndex,1)=(2*LINELEM(i,2)*(b(L
INELEM(i,3))-b(LINELEM(i,4))))/deltaT;
storeCurrentIndex=storeCurrentIndex+1;
end
else
if (LINELEM(i,4)<1)
curr=(b(LINELEM(i,3)))*((LINELEM(i,2)/deltaT)-store
Current(storeCurrentIndex-1,1));
stampCur(curr + (2*LINELEM(i,2)*(b(LINELEM(i,3))))/
deltaT, LINELEM(i,3), LINELEM(i,4)); %volt is used instead of current
stampCond(2*(LINELEM(i,2)/deltaT), LINELEM(i,3), L
INELEM(i,4));
storeCurrent(storeCurrentIndex,1)=(2*LINELEM(i,2)*
(b(LINELEM(i,3))))/deltaT;
storeCurrentIndex=storeCurrentIndex+1;
else
curr=(b(LINELEM(i,3))-b(LINELEM(i,4)))*((LINELEM(i,
2)/deltaT)-storeCurrent(storeCurrentIndex-1,1));
stampCur(curr + (2*LINELEM(i,2)*(b(LINELEM(i,3))-b(
LINELEM(i,4))))/deltaT, LINELEM(i,3), LINELEM(i,4)); %volt is used instead of c
urrent
stampCond(2*(LINELEM(i,2)/deltaT), LINELEM(i,3),
LINELEM(i,4));
storeCurrent(storeCurrentIndex,1)=(2*LINELEM(i,2)*
(b(LINELEM(i,3))-b(LINELEM(i,4))))/deltaT;
storeCurrentIndex=storeCurrentIndex+1;
end
end
else
if (iterationC==0)
if(LINELEM(i,4)<1)
stampCur((2*LINELEM(i,2)*(b(LINELEM(i,3))))/deltaT, LINELE
M(i,3), LINELEM(i,4));
%stampCond(2*(LINELEM(i,2)/deltaT), LINELEM(i,3), LINELEM
(i,4));
iterationC=1;
storeCurrent(storeCurrentIndex,1)=(2*LINELEM(i,2)*(b(LINE
LEM(i,3))))/deltaT;
storeCurrentIndex=storeCurrentIndex+1;
else
stampCur((2*LINELEM(i,2)*(b(LINELEM(i,3))-b(LINELEM(i,4))))/de
ltaT, LINELEM(i,3), LINELEM(i,4));
%stampCond(2*(LINELEM(i,2)/deltaT), LINELEM(i,3), LINELEM(i,4
));
iterationC=1;

storeCurrent(storeCurrentIndex,1)=(2*LINELEM(i,2)*(b(LINELEM(
i,3))-b(LINELEM(i,4))))/deltaT;
storeCurrentIndex=storeCurrentIndex+1;
end
else
if (LINELEM(i,4)<1)
curr=(b(LINELEM(i,3)))*((LINELEM(i,2)/deltaT)-storeCurrent(
storeCurrentIndex-1,1));
stampCur(curr + (2*LINELEM(i,2)*(b(LINELEM(i,3))))/deltaT,
LINELEM(i,3), LINELEM(i,4)); %volt is used instead of current
% stampCond(2*(LINELEM(i,2)/deltaT), LINELEM(i,3), LINELEM(
i,4));
storeCurrent(storeCurrentIndex,1)=(2*LINELEM(i,2)*(b(LINELEM
(i,3))))/deltaT;
storeCurrentIndex=storeCurrentIndex+1;
else
curr=(b(LINELEM(i,3))-b(LINELEM(i,4)))*((LINELEM(i,2)/delta
T)-storeCurrent(storeCurrentIndex-1,1));
stampCur(curr + (2*LINELEM(i,2)*(b(LINELEM(i,3))-b(LINELEM(
i,4))))/deltaT, LINELEM(i,3), LINELEM(i,4)); %volt is used instead of current
% stampCond(2*(LINELEM(i,2)/deltaT), LINELEM(i,3), LINELEM
(i,4));
storeCurrent(storeCurrentIndex,1)=(2*LINELEM(i,2)*(b(LINEL
EM(i,3))-b(LINELEM(i,4))))/deltaT;
storeCurrentIndex=storeCurrentIndex+1;
end
end
end
elseif((LINELEM(i,1)== 76)) % Inductor__Inductor__Inductor__Inductor__Induc
tor__Inductor__Inductor__
cur= (b(LINELEM(i-1, 3))-b(LINELEM(i, 3)))/LINELEM(i-1, 2)
; % calculates the current in 1 previous node
stampCur( cur+(b(LINELEM(i,3))*deltaT/(2*(LINELEM(i,2)))),
LINELEM(i,3), LINELEM(i,4));
stampCond(deltaT/(LINELEM(i,2)));
end
end
end
% Stamping function for Conductance
function[] = stampCond(condValue, Y_Mat_rowValue, Y_Mat_colValue)
%NODES=[103 101 102 ];
global NODES1;
global MNA_size;
%MNA_size = numel(NODES);% calculating number of nodes
global Y_Mat; % creation of zero matrix for storing stamp values
%Y_Mat = zeros(MNA_size);
if(Y_Mat_rowValue>0 && Y_Mat_colValue>0)
Y_Mat(Y_Mat_rowValue, Y_Mat_rowValue) = Y_Mat(Y_Mat_rowValue, Y_Mat_rowValue)
condValue;
Y_Mat(Y_Mat_rowValue, Y_Mat_colValue) = Y_Mat(Y_Mat_rowValue, Y_Mat_colValue)
condValue;
Y_Mat(Y_Mat_colValue, Y_Mat_rowValue) = Y_Mat(Y_Mat_colValue, Y_Mat_rowValue)
condValue;
Y_Mat(Y_Mat_colValue, Y_Mat_colValue) = Y_Mat(Y_Mat_colValue, Y_Mat_colValue)
condValue;

+
+

elseif (Y_Mat_rowValue>0 && Y_Mat_colValue<=0)


Y_Mat(Y_Mat_rowValue, Y_Mat_rowValue) = Y_Mat(Y_Mat_rowValue, Y_Mat_rowValue
) + condValue;
elseif (Y_Mat_rowValue<=0 && Y_Mat_colValue>0)
Y_Mat(Y_Mat_colValue, Y_Mat_colValue) = Y_Mat(Y_Mat_colValue, Y_Mat_colValu
e) + condValue;
end
end
% Stamping function for Current Elements
function []=stampCur(currentValue, J_Mat_rowValue, J_Mat_colValue)
global NODES1;
%NODES=[103 101 102 ];
global MNA_size;
%MNA_size = numel(NODES);% calculating number of nodes
global J_Mat; % creation of J matrix
%J_Mat= zeros(MNA_size,1);
if(J_Mat_rowValue>0 && J_Mat_colValue >0)
J_Mat(J_Mat_rowValue, 1) = currentValue;
J_Mat(J_Mat_colValue, 1) = -1* currentValue;
% J_Mat(J_Mat_rowValue, 1) = J_Mat(J_Mat_rowValue, 1) + currentValue;
% J_Mat(J_Mat_colValue, 1) = J_Mat(J_Mat_colValue, 1) - currentValue;
elseif(J_Mat_rowValue>0 && J_Mat_colValue <=0)
%
J_Mat(J_Mat_rowValue, 1) = J_Mat(J_Mat_rowValue, 1) + currentValue;
J_Mat(J_Mat_rowValue, 1) = currentValue;
elseif (J_Mat_rowValue<=0 && J_Mat_colValue >0)
%
J_Mat(J_Mat_colValue, 1) = J_Mat(J_Mat_colValue, 1) - currentValue;
J_Mat(J_Mat_colValue, 1) = -1* currentValue;
end
end

Potrebbero piacerti anche