
function [ sens1 , xout1 ] = sim1 ( Gg , Cg , Bg , sigEdgeCong , edgeBB )

global s0 p0 layer maxLevel numBlocks max_row_idx max_clm_idx q0 
global simlength inlength
global ugH ucH  ugV ucV  ugHX ucHX  ugVX ucVX  ugx ucx
global h VPini
global minPGpitch
global SHinit numEdges numEdgesPerBB numBlocks minPGpitch
global alpha beta gamma1 gamma2

srclen=simlength;
for i=1:srclen
    idx(i)=i;
end

Ix=ones(srclen,1);
Vini = 1e-2 ;
u.xq = 0.05 * ones (p0*numBlocks,1) ;
A.xq = Gg.xq + 1/h * Cg.xq ;

v0.xq = zeros( 2*layer*q0*(maxLevel+1)*numBlocks , 1 );
v0.xq ( 1 : (layer*q0) , 1 ) = Vini ;
v0.xq ( layer*q0*(maxLevel+1) + 1 : layer*q0*(maxLevel+1) + layer*q0 , 1 ) = Vini ;
v0.xq ( 2*layer*q0*(maxLevel+1) + 1 : 2*layer*q0*(maxLevel+1) + layer*q0 , 1 ) = Vini ;
v0.xq ( 3*layer*q0*(maxLevel+1) + 1 : 3*layer*q0*(maxLevel+1) + layer*q0 , 1 ) = Vini ;
v0.xq ( 4*layer*q0*(maxLevel+1) + 1 : 4*layer*q0*(maxLevel+1) + layer*q0 , 1 ) = Vini ;
v0.xq ( 5*layer*q0*(maxLevel+1) + 1 : 5*layer*q0*(maxLevel+1) + layer*q0 , 1 ) = Vini ;
v0.xq ( 6*layer*q0*(maxLevel+1) + 1 : 6*layer*q0*(maxLevel+1) + layer*q0 , 1 ) = Vini ;
v0.xq ( 7*layer*q0*(maxLevel+1) + 1 : 7*layer*q0*(maxLevel+1) + layer*q0 , 1 ) = Vini ;

for i=1:simlength

    if(inlength(i)==1)
            b0.xq = 1/h * Cg.xq * v0.xq(:,end) + Bg.xq * u.xq ;
    else
            b0.xq = 1/h * Cg.xq * v0.xq(:,end) ;
    end

    v0.xq=[v0.xq , A.xq\b0.xq];   %%24x24 * 24x12
    xout1=v0.xq;   %%% q0 x simlength
end

idx=1;

for jj=1:numBlocks
    for kk=1:maxLevel
        sens1 ( idx , 1 ) = min( mean ( v0.xq ( (jj-1)*(maxLevel+1)*layer*q0 + kk*q0*layer + 1 : (jj-1)*(maxLevel+1)*layer*q0 + kk*q0*layer + q0*layer , : )))
        idx=idx+1;
    end
end

        
%         1 2
%         4 5
%         7 8
% 	sens1 ( 1 , 1 ) = max(mean( v0.xq ( layer*q0 + 1 : layer*q0+layer*q0 , : )))
% 	sens1 ( 2 , 1 ) = max(mean( v0.xq ( layer*q0*2 + 1 : layer*q0*2 + layer*q0 , :)))
% 
% 	sens1 ( 3 , 1 ) = max(mean( v0.xq ( layer*q0*4 + 1 : layer*q0*4 + layer*q0 , :)))
% 	sens1 ( 4 , 1 ) = max(mean( v0.xq ( layer*q0*5 + 1 : layer*q0*5 + layer*q0 , :)))
% 
% 	sens1 ( 5 , 1 ) = max(mean( v0.xq ( layer*q0*7 + 1 : layer*q0*7 + layer*q0 , :)))
% 	sens1 ( 6 , 1 ) = max(mean( v0.xq ( layer*q0*8 + 1 : layer*q0*8 + layer*q0 , :)))
% 
% 	sens1 ( 7 , 1 ) = max(mean( v0.xq ( layer*q0*10 + 1 : layer*q0*10 + layer*q0 , :)))
% 	sens1 ( 8 , 1 ) = max(mean( v0.xq ( layer*q0*11 + 1 : layer*q0*11 + layer*q0 , :)))
% 

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% only add SH size to the edges in the current Level
% %% find edge congestion by adding signal congestion + PG/THERMAL congestion
% 
% PGandSigEdgeCong = ones ( numEdges , 1 ) ;
% if ( currLevel == maxLevel | currLevel == 1 | currLevel == 2)
%     for i=1:numEdges
% 	currBB = edgeBB ( i, 1 ) ;
% 	edgeLevel = routEdgeLevel ( i , 1 ) ;
% 	if ( currBB ~=0 & edgeLevel > 0 )
% 		PGandSigEdgeCong ( i , 1 ) = SH ( currBB , edgeLevel ) / minPGpitch ;
% 		PGandSigEdgeCong ( i , 1 ) = ceil ( PGandSigEdgeCong ( i , 1 ) ) ;
% 	end
%     end
%         sens= sens/norm(sens)
%         sensTot = sens
% 
% else
% 
% PGandSigEdgeCong = zeros ( numEdges , 1 ) ;
% for i=1:numEdges
%     currBB = edgeBB ( i , 1 ) ;
%     edgeLevel = routEdgeLevel ( i , 1 ) ;
%     if ( currBB ~= 0  & edgeLevel > 0)	  
%     PGandSigEdgeCong ( i , 1 ) = sigEdgeCong ( i , 1 ) + SH ( currBB , edgeLevel ) / minPGpitch ;
%     else
%     PGandSigEdgeCong ( i , 1 ) = sigEdgeCong ( i , 1 ) + 1 ; 
%     end
% end
% 
% totBBcong = zeros ( numBlocks , 1 ) ;
% maxCong = zeros ( numBlocks , 1 ) ;
% avgBBcong = zeros ( numBlocks , 1 ) ;
% %% find avg. and max edge SIGNAL congestion for each BB
% 
% for i=1:numEdges
%     currBB = edgeBB( i , 1 ) ;
%     if ( currBB ~= 0 ) %% if edge not in a BB, don't count it
%         totBBcong ( currBB , 1 ) = totBBcong ( currBB , 1 ) + sigEdgeCong ( i , 1 ) ;
% 
%         if ( sigEdgeCong ( i , 1 ) > maxCong ( currBB , 1 ) )
%             maxCong( currBB , 1 ) = sigEdgeCong( i  , 1 ) ;
%         end
%     end
% 
% end
% for i=1:numBlocks
% 	totBBcong(i,1)
% 	numEdgesPerBB
%     avgBBcong ( i , 1 ) = totBBcong( i , 1 ) / numEdgesPerBB ;
% end
% 
% sensMaxCong = maxCong/norm(maxCong)
% sensAvgCong = avgBBcong/norm(avgBBcong) 
% 
% 
% sensTot = (alpha * sens) + gamma1 * sensAvgCong + gamma2 * sensMaxCong 
% 
% end
