Energy approach to Coulomb gases and log gases