I once read that it is easy to warm things up, but it is much harder to cool something down. True. Almost everything you do produces some heat and, as a matter of fact, usually it is more challenging not to let your energy spoils into a heat-waste.
So, how to cool an object down? Of course, you should use a fridge.
A fridge is a heat pump. It pumps heat from its interior into outside world. It makes its interior colder but also heats up everything around. This process will spend some energy because the heat doesnt want to move from a cold object to a warm object by itself. Instead it must be pushed somehow. However, it turns up that the process is quite efficient.
In fact, a heat pump can also be used to warm things up. Sometimes it happens to be more efficient to use a heat pump instead of direct transformation of energy into a heat. Yes, you are right that energy can be transformed into heat with 100% efficiency, but heat pumps are even better say, 300% or 400%. No kidding.
A basic gas-compression heat pump
I will try to describe a simple process of a gas-compression heat pump. A heat pump is a straightforward application of thermodynamic.
Imagine that there is a well-isolated room that we want to warm up. The outside temperature is lower than the inside temperature so we cant simply open windows and let the heat in. Instead we have to pump it somehow.
The temperature inside the room (T3) is lower than the temperature outside (T1)
How does our heat pump machine look like? It is a simple cylinder with a piston filled with some gas that has some nice properties (non-flammable, non-corrosive, environment-friendly ). Using the piston we can compress or decompress gas inside the cylinder. The gas is never exchanged with environment.
Our heat pump - a cylinder, a piston and some gas inside
First, suppose that we had our cylinder outside for a while and the temperature of the gas inside it is equalized to the outside temperature T1. Now we dress the cylinder into some heat-insulation clothing so it cannot exchange heat with its surrounding any more. Then we compress the gas inside the cylinder. Both things will happen: the pressure of the gas will increase and the temperature of the gas will increase. This compression is a completely adiabatic process as there is no heat exchange with surrounding. We have to compress the gas enough to increase its temperature (T2) to a degree greater than the temperature inside of our room (T3).
Compression stroke - the gas inside the cylinder is compressed to increase its temperature from T1 to T2
The diagram on the right shows the adiabatic curve for such compression. The gas state goes from P1, V1, T1 to P2, V2, T2
After that we bring the cylinder inside and remove the heat-insulation clothing. The cylinder gas is hotter than the inside temperature so we will wait until the temperature equalizes (this is an isochoric process as there is no change in volume). As a result, the inside of the room will be warmed a tiny bit.
We bring hot cylinder inside the room and wait until its temperature drops to the room temperature. The cylinder transfers
heat to the room. This is an isochoric process - no volume change - see diagram to the right.
The only thing that is left to do now is to return at the beginning and start the cycle again. To return to the beginning state, we again put the heat-insulation clothing to the cylinder. Now we decompress the gas (an adiabatic process, again). As a result the temperature of the gas inside cylinder will drop low (quite lower than the outside temperature). By the way, the reason why are we always putting the heat-insulation clothing on while we are doing compression or decompression is because we want to simplify our math a pure adiabatic process math is simple.
Decompresion stroke - another adiabatic process. The temperature of the gas inside cylinder will now drop very low
See diagram to the right. The gas state goes from P3, V3, T3 to P4, V4, T4
Finally, we move our cylinder outside and remove the heat-insulation clothing. We now have to wait some time until the temperature of the gas equalizes to the outside temperature T1 (isochoric process) and then we can start a new cycle. As you can see, the heat energy is here transferred from sorrounding to the gas inside the cylinder and will be "pumped" into the room in the next cycle - that is why the device is called a heat pump.
Finally we return to the beginnig of the cycle - we bring the cold cylinder outside and we now wait until its temperature rises to
the outside temperature. Again, this is an isochoric process. The gas state goes from P4, V4, T4 back to P1, V1, T1
Repeating the described cycle many times over again, makes the inside of the room warmer. The question is whether this is more efficient than direct usage of energy to warm the room? Remember, we had to use some energy to compress the gas. On the other hand, during the decompression, we could recover some.
We are going to calculate two things. First, we will calculate how much energy (work done) we need to spend to make compression/decompression of the gas in each cycle. In our P-V diagram, the compression cycle is represented by the curve from P1, V1, T1 to P2, V2, T2. The decompresion cycle is represented by the curve from P3, V3, T3 to P4, V4, T4. The area between these two curves is the net work we need to spend in each heat pump cycle. Note also that V1 is equal to V4 and V2 is equal to V3.
Second, we will calculate how much energy (heat) is transferred to the room in each cycle. The energy is transferred to the room only in the isochoric process that is represented in our P-V diagram by straight line that goes from P2, V2, T2 down to P3, V3, T3.
By comparing these two obtained values we will know the efficiency of our simple heat pump. Concretely we want to find the efficiency depending on T1 and T3 temperature difference (inside and outside temperature) and the temperature T2 which we will also use as a parameter in our equations.
Okay, Let's start. How do we calculate the net energy (work done) needed for compression and decompression? First we will calculate how much energy is needed to make the compression and then we will calculate how much of it can be recovered during the decompression the difference is our net work. Both of these two processes, the compression and the decompression, are adiabatic processes. Therefore, it would be reasonable if we first derive some general equations regarding an adiabatic process to simplify our calculations.
We will start using the following simple equations that describe an adiabatic process:
P represents pressure of gas [Pascal]
T represents temperature of gas [Kelvin]
V represents volume of gas [cubic-meter]
k non-dimensional constant that depends on the type of gas: k=Cp/Cv (it is greater than 1)
The equation  is a very basic gas equation that that is derived from the ideal gas equation PV=nRT. As 'n' doesn't change then PV/T is a constant. The equation  describes an adiabatic process - we are not deriving it here.
In the case of an adiabatic process (where the state of a gas was changed from Pa, Va, Ta to Pb, Vb, Tb) we can calculate energy (work done) by calculating the area under the adiabatic curve in the P-V diagram.
Equation  tells us how much energy is needed/released in an adiabatic process depending on starting and ending volumes (Va and Vb). Here we are using the convention that calculated energy is going to be positive if it must be given to a system, while it is going to be negative if it is produced by a system.
Lets also calculate how does that energy depends on starting and ending temperatures (Ta and Tb). First we will need some preparations:
And now we can combine equations  and  to get the result:
Whoow, the equation  is even simpler than equation . We see that the energy depends linearly on the temperature change in an adiabatic process.
Back to our story... Now we can use equations  and  to calculate energy needs in our particular case - the simple heat pump. First the compression cycle... It is easy. We know that the temperature of the gas will change from T1 (outside temperature) to T2. We can simply use the equation  and we have:
Where W1 is the energy (work done) needed to compress the gas from P1, V1, T1 to P2, V2, T2.
Okay, now the decompression cycle. It is somewhat more complicated. The gas changes its state form P3, V3=V2, T3 to P4, V4=V1, T4. We will first use some math to calculate P3 and V3=V2 and then we will insert these results into equation .
We have calculated the energy that we can recover in the decompression cycle equation . Compare it to equation  and you will see that they are very similar the only difference is in factor T3/T2. As T3 is somewhat lower than T2 we see that the energy we can recover would be somewhat smaller that the energy we have to spend during the compression stroke.
Now we sum equation  (positive energy) and equation  (negative energy) and we have the net energy (work done) that we put into our system in each cycle.
Obtaining the equation  was only the first part in our calculations. We know now how much work we have to do in each cycle. Now we need to know how much energy (heat) is actually transferred into our room in each cycle. We will start using following widely known equations:
Cv molar specific heat for constant volume [joule per kelvin per mole]
Cp molar specific heat for constatn pressure [joule per kelvin per mole]
R is ideal gas constant [8.314 joule per kelvin per mole]
Equation  is the ideal gas equation. Equation  describes relation between heat and temperature in an isochoric process. Cv and Cp depend on the type of gas (as well as constant k that we already used).
Using equations - it is easy to calculate the energy released during an general isochoric process:
In our particular case, by substitution into equation  we get:
The fun stuff here is that the equation  is virtually identical to the equation . (At first I was very confused with this result because it means that if you heat a gas using adiabatic compression, and then you leave it to cool down again in an isochoric process, the released heat energy will be exactly the same as the work done in the compression stroke. So, no more energy has left to the cooled gas. This was strange to me because it seemed that the compressed gas, although cooled, still posses some energy inside the ability to do work. However, I remembered later that even though it can do work, it can do it only if it expands and thus cools down further spending its own heat energy.)
Now we have all that is needed to calculate the efficiency of our heat pump. We will simply divide equation  with equation .
The obtained result is very nice and short! But, of course, the T2 cannot be any lower than the T3. Thus we can say:
In the efficiency equation above we used symbol delta-T for the temerature difference T3-T1. We see that as the difference between the inside temperature and the outside temperature becomes larger, the heat pump becomes less and less efficient. You can imagine that, when other loses are also taken into account, when the outside temperature drops below some level it is no more efficient to pump the heat from outside into your room. (After that it becomes more efficient to convert your energy directly into the heat. Or, alternatively, you can pump the heat from some source that is never so cold - like water or ground).
True, the above result is almost purely academic. In the real world we will always have lots of other loses that we will have to deal with.
1. Unfortunately the temperature T2 must be reasonably higher than temperature T3. If we try to make the difference smaller, we will be able to transfer only a smaller amount of energy into the room in each cycle. To compensate we could either have to use large-volume cylinder (more expensive equipment) or repeat the cycle more frequently (limited because in each cycle we have to wait for temperatures to equalize - twice).
2. We cannot wait forever until the temperature of the gas equalizes to surrounding temperature. We only can wait some limited amount of time (until the temperature nearly-equalizes). This also limits our efficiency. We can compensate using large-volume cylinder or using some forced heat exchangers (but this also wastes some energy).
3. Obviously there is no perfect heat-insulating material that we can dress our cylinder into during the compression/decompression cycle and thus we will obviously have some heat leakages. This will further reduce our efficiency.
4. No such thing as ideal gas. We used equations for an ideal gas.
5. Mechanical loses in our machinery. They are always present.
6. Hardly we can recover all the available energy in the decompression stroke... By the way, how can we recover it anyway? Well, we could use some sort of electric motor that will act as a motor during the compression stroke and as a generator during the decompression stroke. Or alternatively, we can couple two contra-phased cylinders while one compresses the other one decompresses (it would be a rather challenging job to make such a mechanism work for our simple heat pump example :).
Real world heat pump
A gas-compression heat pump can be employed in a real world and often it is. Usually then, it uses the Stirling cycle.
However, the most common heat pump is a phase-change heat pump as used in your fridge. During the cycle it changes a gas to liquid and vice versa.
The compressor pumps a gas from cold (low-pressure) to hot (high-pressure) side. The gas then flows through the hot-side-heat-exchanger and gets cooled down. As it cools down, it condensates into liquid. Then the liquid flows through an orifice (the expansion valve) back to the cold side. While it passes the orifice, its pressure and temperature both drop greatly. Further it flows through the cold-side-heat-exchanger where it warms up and evaporates to gaseous phase again. So, the cycle is closed.
Every time a liquid evaporates into gaseous phase it takes some heat from its surrounding. On the other hand, when it condensates to liquid it gives the heat out.
At first you may think that this process is not very efficient because during the decompression phase no energy is recovered (as the simple orifice cannot recover any energy). How much energy is actually wasted this way? We can try to draw a P-V diagram of this orifice-decompression process. Again the wasted energy would be equal to the area below the curve in the P-V diagram. However, although the pressure can get quite some value, it is limited by the power of the compressor. On the other hand, volume difference is really small remember that it is a liquid that passes through the orifice, and liquids dont change their volumes much when you change a pressure on them. So we can conclude that the energy we are losing is rather small and shouldnt affect the overall efficiency much.