clear ;

max_row_idx = 162 ;
max_clm_idx = 162 ;
numBlocks = 4 ;
ugx = 0 ;
ucx = 0 ;

bb_max_row_idx = max_row_idx / sqrt ( numBlocks ) ; 
bb_max_clm_idx = max_clm_idx / sqrt ( numBlocks ) ;
N0 = bb_max_row_idx * bb_max_clm_idx ;

for j=1:numBlocks
    for k=1:numBlocks
        Xconn(j,k).x = sparse( N0 , N0) ;
    end
end

%% East , West

max_Grow_idx = sqrt(numBlocks) ;
max_Gclm_idx = max_Grow_idx ;

% build gidTable 
gidGTable=[];
gidGTable=buildGidTable(gidGTable,max_Grow_idx);

gidTable = [];
gidTable = buildGidTable(gidTable,bb_max_row_idx) ;

%% East West Big Blocks
for j=1:max_Grow_idx-1
        for k=1:max_Gclm_idx
        %% East West little blocks
         tmp=sparse(N0,N0);
         tmp1=sparse(N0,N0);
         for m=1:max_row_idx
             for n=1:bb_max_clm_idx
                gx1 = bb_max_clm_idx ;
                gy1 = n ;
                gx2 = 1 ;
                gy2 = n ;
                tmp = set_loc1( gx1, gy1, gx2, gy2, bb_max_row_idx, tmp ) ;
                tmp1 = set_loc1( gx2 , gy2 , gx1 , gy1 , bb_max_row_idx , tmp1 ) ;
             end
         end
        gid1=getGid(j,k,max_Grow_idx);
        gid2=getGid(j+1,k,max_Grow_idx);
        Xconn(gid1,gid2).x = Xconn(gid1,gid2).x + tmp;
        Xconn(gid2,gid1).x = Xconn(gid2,gid1).x + tmp1;
        end

end
% North South Big Blocks
for j=1:max_Grow_idx
        for k=1:max_Gclm_idx-1
        %% North South little blocks
        tmp=sparse(N0,N0);
        tmp1=sparse(N0,N0); 
         for m=1:bb_max_row_idx
             for n=1:bb_max_clm_idx
                gx1 = n;
                gy1 = bb_max_row_idx;
                gx2 = n ;
                gy2 = 1 ;
                tmp = set_loc1( gx1 , gy1, gx2, gy2, bb_max_row_idx, tmp ) ;
                tmp1 = set_loc1( gx2 , gy2 , gx1 , gy1 , bb_max_row_idx , tmp1 ) ;
             end
         end
        gid1=getGid(j,k,max_Grow_idx);
        gid2=getGid(j,k+1,max_Grow_idx);
        Xconn(gid1,gid2).x = Xconn(gid1,gid2).x + tmp;
        Xconn(gid2,gid1).x = Xconn(gid2,gid1).x + tmp1;
        end
end

for i=1:numBlocks
GXconnTot(i).x = sparse( N0 , N0 );
CXconnTot(i).x = sparse ( N0 , N0 ) ;
end

for i=1:numBlocks
    for j=1:numBlocks
           for k=1:N0
               
               tot = nnz ( Xconn(i,j).x ( k, :)) ;
               GXconnTot(i).x(k,k) = GXconnTot(i).x(k,k) + tot * ugx ;
               CXconnTot(i).x(k,k) = CXconnTot(i).x(k,k) + tot * ucx ;
           end
    end
end


    GXconn1(1:N0, 1:N0) = GXconnTot(1).x ;
    CXconn1(1:N0, 1:N0) = CXconnTot(1).x ;
    
    GXconn2(1:N0, 1:N0) = GXconnTot(2).x ;
    CXconn2(1:N0, 1:N0) = CXconnTot(2).x ;
    
    GXconn3(1:N0, 1:N0) = GXconnTot(3).x ;
    CXconn3(1:N0, 1:N0) = CXconnTot(3).x ;
    
    GXconn4(1:N0, 1:N0) = GXconnTot(4).x ;
    CXconn4(1:N0, 1:N0) = CXconnTot(4).x ;


dlmwrite ( 'Gxconn1.out', full(GXconn1) ) ;
dlmwrite ( 'Gxconn2.out', full(GXconn2) ) ;
dlmwrite ( 'Gxconn3.out', full(GXconn3) ) ;
dlmwrite ( 'Gxconn4.out', full(GXconn4) ) ;

dlmwrite ( 'CXconn1.out', full(CXconn1) ) ;
dlmwrite ( 'CXconn2.out', full(CXconn2) ) ;
dlmwrite ( 'CXconn3.out', full(CXconn3) ) ;
dlmwrite ( 'CXconn4.out', full(CXconn4) ) ;

