A fluid-dynamical model of differentiation and layering in magma chambers