Industrial heat pumps, and specifically those using carbon dioxide (CO2) as a refrigerant, can play a key role in the decarbonization of the heating and cooling sector, due to their low global warming potential, toxicity and flammability. However, challenges arise when dealing with the modeling and optimization of CO2 heat pumps under different operating conditions. We address this challenge by presenting a modeling and optimization tool to predict and optimize the operation of heat pumps in off design conditions. The tool improves on the current state-of-the-art in several ways. First, it describes a novel thermodynamic cycle, which features higher performance than conventional heat pumps. Also, it is based on a mathematical model that describes accurately the behavior of CO2 across a wide range of thermodynamic conditions, especially near its critical region, and takes into account effects of motor-cooling, leakages and performance limits. Furthermore, it maximizes the coefficient of performance (COP) of the heat pump via an accurate and computationally-efficient optimization problem. The capabilities of the model are illustrated by looking at different typical heat pump applications based on real-world projects within the heating and cooling sector. Different case studies are considered, showing how the heat pump is optimally operated during the year to maximize its COP while meeting the varying boundary conditions.