The role of Frank Perry was written specifically for Brian Cox by director Rupert Wyatt, who had worked with Cox before and wanted to work with him again. When Cox refused a supporting role in a movie Wyatt offered him and challenged him to write him a good leading role, Wyatt did exactly that.
The trivia item below may give away important plot points.
Director Rupert Wyatt said he based the plot on the Ambrose Bierce short story, "An Occurrence at Owl Creek Bridge". This is the book the lead character Frank Perry is shown reading and shows Rizza near the end of the film.