Python Context Manager to ensure data isn't lost due to an error during a simulation run.