Ab initio explanation of the incommensurate phase transition in BaMnF4