We present a new technique which allows the fully ab initio calculation of
the chemical potential of a substitutional impurity in a high-temperature c
rystal, including harmonic and anharmonic lattice vibrations. The technique
uses the combination of thermodynamic integration and reference models dev
eloped recently for the ab initio calculation of the free energy of liquids
and anharmonic solids. We apply the technique to the case of the substitut
ional oxygen impurity in h.c.p. iron under Earth's core conditions, which e
arlier static ab initio calculations indicated to be thermodynamically very
unstable. Our results show that entropic effects arising from the large Vi
brational amplitude of the oxygen impurity give a major reduction of the ox
ygen chemical potential, so that oxygen dissolved in h.c.p. iron may be sta
bilised at concentrations up a few mol % under core conditions.