warning off;
clear;
clear global;
format long g

global s0 p0 layer maxLevel numBlocks max_row_idx max_clm_idx q0 sp numEdges 
global simlength inlength
global ugHT ucHT  ugVT ucVT  ugHXT ucHXT  ugVXT ucVXT  ugxT ucxT ucV_toGnd
global ugH  ucH   ugV  ucV   ugHX  ucHX   ugVX  ucVX   ugx  ucx
global hT VTini
global h VPini
global SHinit minPGpitch
global alpha beta gamma1 gamma2

p0=1;            %% Num Ports / BB
q0=20;
s0=1e6;
sp=1; 

layer=3;
numBlocks = 4 ;
max_row_idx = 18; % 18 % 162
max_clm_idx = 18 ;
minPGpitch = 5 ;
maxLevel=3 ;

simlength=100;
inlength( 1:simlength )=1 ;

SH(1:numBlocks*maxLevel,1) = 1e-2;
SHinit = 1e-2 ;
step=100;

IMAX_SENS=2;  %% max times we will do thermal/pg optimization on currLevel
IMAX_VCYCLES=1;
numEdges = ( ( max_row_idx - 1 ) * max_row_idx ) * 2 ;
%%%%%%%%%%%%%
%PG
% ugH ~ 1e-5 -> 1e-6
% ucH ~ 1e-9 -> 1e-10
% ugV ~ 1e-3 -> 1e-4
% ucV ~ 1e-11 -> 1e-12

%Thermal
% ugH ~ 1e-6 -> 1e-7
% ucH ~ 1e-10 -> 1e-11
% ugV ~ 1e-4 -> 1e-5
% ucV ~ 1e-11 -> 1e-12

%%%%%%%%%%%%%%%%%%%%%%%%%% POWER/GND  %%%%%%%%%%%%%%  TARGET: 0.5 V
ugx=0;
ucx=0;
ugH=1;
ucH=1e-5;

ugHX=1e-5;
ucHX=1e-15;
ugVX=1e-5;
ucVX=1e-15;
h=1e-4;
VPini=1e-4;
VP=0.5;
q0=20;

% ugx=1e-5;
% ucx=1e-10;


%%%%%%%%%%%%%%% THERMAL %%%%%%%%%%%%%%%  SAVE %%%%%%%%
% ugx=0;
% ucx=0;
% ugH=0.02;
% ucH=1e-5;
% 
% ugHX=1e-10;
% ucHX=1e-15;
% ugVX=1e-10;
% ucVX=1e-15;
% h=1e-4;
% 
% VPini=1e-4;
% VP=0.5;
% q0=20;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

iterV = 0 ;
ovFlow = 1 ;
% while ( ovFlow == 1 & iterV < IMAX_VCYCLES )
iterV = iterV + 1 ;

delta = zeros ( maxLevel*numBlocks , 1 ) ;
sigEdgeCong = zeros ( numEdges , 1 ) ;
display( 'building connections')
[ edgeBB , routEdgeLevel , XgH_BaseGrid , XgV_BaseGrid , XgSH , XgSV ] =  build_connections() ;
display( 'init')
[ Gg, Cg, Bg, Lg, Vtg, GgSBB_tot , CgSBB_tot , GXconn, CXconn ] = init(  XgH_BaseGrid , XgV_BaseGrid , XgSH , XgSV ) ; 

for ii=1:2
display ( 'tri')
[ GgTri, CgTri, BgTri ] = tri ( Gg, Cg, Bg, Vtg, GgSBB_tot, CgSBB_tot, GXconn, CXconn , SH) ;
display ( 'sim1' )
[ sensTot, xout1 ] = sim1( GgTri, CgTri, BgTri, sigEdgeCong, edgeBB );
display ( 'sim2' )
[xout] = sim2 ( xout1 , Lg, delta ) ;

delta  = step * sensTot / norm ( sensTot )
 
SH =  SH  + delta
VPinit=max(max(xout))
plotGraph ( xout ) ;

end
 
%for ii=1:2
 
%[GgTri, CgTri, BgTri] = rctbsx_pg_fn_withRout( Gg, Cg, Bg, Vtg, GgSBB_tot, CgSBB_tot, GXconn, CXconn, SH, currLevel ) ;
%[sensTot, xout1] = rctbsx_pg_sim_fn1_withRout ( GgTri, CgTri, BgTri, sigEdgeCong, edgeBB, routEdgeLevel, SH, currLevel);   
%[ xout , xoutSens ] = rctbsx_pg_sim_fn2_withRout ( xout1 , Lg , delta ) ;
%delta=step*sensTot/norm(sensTot)
%SH ( :, currLevel ) = SH ( : , currLevel ) - delta
%VPinit=max(max(xout))
%plotGraph ( xout ) ;

%end


%delta = zeros ( numBlocks, 1 ) ;
%currLevel = 1 ;
%SH(1,2)=6000;
%SH(2,2)=6000;
%SH(3,2)=6000;
%SH(4,2)=6000;
%[Gg, Cg, Bg, Lg, Vtg, GgSBB_tot , CgSBB_tot , GXconn, CXconn ] = rctbsx_pg_init_fn_withRout (  XgH_BaseGrid , XgV_BaseGrid , XgSH , XgSV , SH , currLevel) ; 
%for ii=1:2
 
%[GgTri, CgTri, BgTri] = rctbsx_pg_fn_withRout( Gg, Cg, Bg, Vtg, GgSBB_tot, CgSBB_tot, GXconn, CXconn, SH, currLevel ) ;
%[sensTot, xout1] = rctbsx_pg_sim_fn1_withRout ( GgTri, CgTri, BgTri, sigEdgeCong, edgeBB, routEdgeLevel, SH, currLevel);   
%[ xout , xoutSens ] = rctbsx_pg_sim_fn2_withRout ( xout1 , Lg , delta ) ;
%delta=step*sensTot/norm(sensTot)
%SH ( :, currLevel ) = SH ( : , currLevel ) - delta
%VPinit=max(max(xout))
%plotGraph ( xout ) ;

%end


























 
% xoutSensPrev = xoutSens ;

% % 
%currLevel=2
%delta = zeros ( numBlocks,1) ;
% SHcurr ( 1 , 3 ) = 60 ;
% SHcurr ( 2 , 3 ) = 60 ;
% SHcurr ( 3 , 3 ) = 60 ;
% SHcurr ( 4 , 3 ) = 60 ;
%[Gg, Cg, Bg, Lg, Vtg, GgSBB_tot , CgSBB_tot , GXconn, CXconn ] = rctbsx_pg_init_fn_withRout (  XgH_BaseGrid , XgV_BaseGrid , XgSH , XgSV , SHcurr ,currLevel ) ; 

%for jj=1:2
%[GgTri, CgTri, BgTri] = rctbsx_pg_fn_withRout( Gg, Cg, Bg, Vtg, GgSBB_tot, CgSBB_tot, GXconn, CXconn, SHcurr, currLevel ) ;
%[sensTot, xout1] = rctbsx_pg_sim_fn1_withRout ( GgTri, CgTri, BgTri, sigEdgeCong, edgeBB, routEdgeLevel, SHcurr, currLevel);   
%[ xout ] = rctbsx_pg_sim_fn2_withRout ( xout1 , Lg , delta  ) ;
%delta=step*sensTot/norm(sensTot)
%SH ( :, currLevel ) = SH ( : , currLevel ) - delta
 
%VPinit=max(max(xout))
%plotGraph ( xout ) ;
%end

%  while ( currLevel > 0 ) %% While Corsening
%     iterSens=0;
% 
%     while ( (VPcurrmax > (VPinit*perPGdrop/100))  &&  iterSens<=IMAX_SENS )
%            iterSens=iterSens+1;
%            [GgTri, CgTri, BgTri] = rctbsx_pg_fn_withRout( Gg, Cg, Bg, Vtg, GgSBB_tot, CgSBB_tot, GXconn, CXconn, SH, currLevel ) ;
%            [sensTot, xout1] = rctbsx_pg_sim_fn1_withRout ( GgTri, CgTri, BgTri, sigEdgeCong, edgeBB, routEdgeLevel, SH, currLevel);  
%  
%            
%            [ xout ] = rctbsx_pg_sim_fn2_withRout ( xout1 , Lg , delta , currLevel, iterSens) ;
%            delta=step*sensTot/norm(sensTot);
%            if(currLevel ==3 | currLevel == 2)
%            SH ( :, currLevel ) = SH ( : , currLevel ) - delta
%            else
%               SH ( :, currLevel ) = SH ( : , currLevel ) + delta
%            end
%            
%            VPcurrmax=max(max(xout))
%            plotGraph ( xout ) ;
%     end
%     %% Call Routing
% %   [ sigEdgeCong ] = rctbsx_routControl_corsen_fn ( PGandSigEdgeCong , currLevel ) ;
%     VPinit = VPcurrmax ;
%     VPcurrmax = MAX_PG_BOUNCE ;
%     currLevel = currLevel -1 
% end
% % % 
% % %  [ ovFlag ] = rctbsx_routControl_uncorsen_fn ( PGandSigEdgeCong , currLevel ) ;
% % % end
% % 
%  
