Multiscale scheme for the optimal use of residues for the production of biogas