Fix MPI file writing

When writing out to the output file the old MPI version would open the file multiple times and write out the result multiple times. Now only rank 0 writes.