Mechanism for the formation of density gradients through semipermeable membranes