Phase selection during solidification of liquid magnesium via ab initio molecular dynamics simulations