function [G,E] = superblock (BL,SL,SR,BR,ntotalA,ntotalB)

%
% Creates the superblock and finds it's ground state
%

global JA JB;

if ntotalA == -1
    MA = ones (BR.m,SR.m,SL.m,BL.m);
else
    [X1,X2,X3,X4] = ndgrid (BR.NTOTA,SR.NTOTA,SL.NTOTA,BL.NTOTA);
    MA = (X1+X2+X3+X4 == ntotalA);
end

if ntotalB == -1
    MB = ones(BR.m,SR.m,SL.m,BL.m);
else
    [X1,X2,X3,X4] = ndgrid (BR.NTOTB,SR.NTOTB,SL.NTOTB,BL.NTOTB);
    MB = (X1+X2+X3+X4 == ntotalB);
end

IND = find (MA & MB);

IBL = speye (BL.m);
ISL = speye (SL.m);
ISR = speye (SR.m);
IBR = speye (BR.m);
IL = speye (BL.m*SL.m);
IR = speye (SR.m*BR.m);

HL = sparse (- JA*(kron(BL.CA,SL.CA') + kron(BL.CA',SL.CA)) - JB*(kron(BL.CB,SL.CB') + kron(BL.CB',SL.CB)) + kron(BL.H,ISL) + kron(IBL,SL.H));
HR = sparse (- JA*(kron(SR.CA,BR.CA') + kron(SR.CA',BR.CA)) - JB*(kron(SR.CB,BR.CB') + kron(SR.CB',BR.CB)) + kron(SR.H,IBR) + kron(ISR,BR.H));
HL1 = sparse (kron(IBL,-JA*SL.CA));
HL2 = sparse (kron(IBL,-JB*SL.CB));
HR1 = sparse (kron(SR.CA',IBR));
HR2 = sparse (kron(SR.CB',IBR));

H1 = subkron (HL,IR,IND);
H2 = subkron (IL,HR,IND);
H3 = subkron (HL1,HR1,IND);
H4 = subkron (HL2,HR2,IND);
H = H1+H2+H3+H3'+H4+H4';

H = (H + H')/2;

opt.issym=1;
opt.isreal=1;
opt.disp=0;

if (size(H,1) < 2)
    V = 1;
    D = H;
else
    [V,D] = eigs(H,1,'sa',opt);
end

E = D(1,1);
G = sparse(IND,1,V(:,1),BL.m*SL.m*BR.m*SR.m,1);
