Mathematical Foundations of Thermodynamics