Theory and simulation of the charging of dust in plasmas