function [E,DENS] = dmrgbh (JA0,JB0,UA0,UB0,UAB0,TA0,TB0,rhoA,rhoB,extraA,extraB,N,maxA,maxB,tmax0,wlimit0,slimit0,mdens)

%
% DMRG for the two-species Bose-Hubbard model
%
% JA0,JB0 - hopping parameters for species A and B
% UAA0,UBB0,UAB0 - interaction parameters
% TA0,TB0 - external harmonic trapping parameter
% rhoA,rhoB - lattice fillings
% extraA,extraB - extra particles to filling (particle = +1, hole = -1, etc.)
% N - number of lattice sites
% maxA,maxB - maximal number of particles on one site
% tmax0 - maximum number of states retained in DMRG
% wlimit0 - truncation weight limit
% slimit - sweeping convergence limit
% mdens - whether calculate particle density (true/false)
%

global JA JB UA UB UAB POTA POTB tmax wlimit slimit;

JA = JA0;
JB = JB0;
UA = UA0;
UB = UB0;
UAB = UAB0;
tmax = tmax0;
wlimit = wlimit0;
slimit = slimit0;

if rhoA == -1
    ntotalA = -1;
else
    ntotalA = round(N*rhoA+extraA);
end;

if rhoB == -1
    ntotalB = -1;
else
    ntotalB = round(N*rhoB+extraB);
end;

X = 1:N;
POTA = TA0*(X-N/2-0.5).^2;
POTB = TB0*(X-N/2-0.5).^2;

for j = 1:N
    S(j) = createsite (maxA,maxB,j);
end

L(1) = S(1);
R(1) = S(N);

disp('Warmup ...');

[L,R] = warmup (N,L,R,S,rhoA,rhoB,extraA,extraB);

disp('Full system sweeps ...');

E0 = Inf;
DE = 1;
while abs(DE) > slimit
    [L,R] = sweep (N,L,R,S,ntotalA,ntotalB);
    E = meas (N,L,R,S,ntotalA,ntotalB);
    DE = E - E0;
    E0 = E;
    disp (['DE = ' num2str(DE)]);
end

if mdens
    DENS = measdens(N,L,R,S,ntotalA,ntotalB);
else
    DENS = 0;
end
