# How does the statistical definition of entropy reduce to heat engine entropy?

In statistical mechanics, entropy is defined in terms of the probability distribution of the microstates of the system, by the Gibbs formula, $S = -k_B\sum_i P_i \ln P_i$. How does that reduce to $dS = \frac{\delta Q}{T}$ in case of heat engines or other classical thermodynamical systems? Also, is $dE = TdS$? If yes, how?

Let's assume we have a Boltzmann distribution,

$$p_i = \frac{1}{Z}e^{-\beta E_i}$$

with $\beta = (k_B T)^{-1}$ With this, it follows from the Gibbs formula that

$$dS = -k_B \sum_i dp_i \ln p_i = -k_B \sum_i dp_i (-\beta E_i -\ln Z)$$

Using $\sum_i dp_i = 0$ we find

$$dS = \frac{1}{T}\sum_i E_i dp_i = \frac{1}{T}\sum_i d(E_ip_i) - d(E_i)p_i$$

The first term in the Equation is the change in total Energy $dE$, and the second term is work done on the system for small changes $\delta w$. Using the first law of thermodynamics $dE = \delta w + \delta Q$ you find

$$dS = \frac{\delta Q}{T}$$

If you assume no work is done on or by the system, the third equation will give you

$$dS = \frac{dE}{T}$$