Persistent wintertime inversions over cities are the most critical conditions for air quality, and also one of the most challenging situations to simulate with a meteorological model. In this study, the ability of the meteorological Weather Research and Forecasting (WRF) model, coupled with the multilayer urban canopy parameterization BEP-BEM (Building Effect Parameterization - Building Energy Model), to simulate the evolution of the Planetary Boundary Layer (PBL) structure over the city of Madrid, Spain, during a multiday inversion episode, is assessed. The model results are evaluated against airport soundings, fourteen meteorological stations within and around the urban area, and remotely sensed surface temperatures. The study indicates that the PBL structure is determined by the interaction between the urban and rural heat and momentum fluxes, the topography, and the downward turbulent transport of heat. The best air temperature spatial distribution is obtained when the 6th order horizontal filter is applied only to wind and not to temperature, and when the soil moisture is reduced to 25% of the initial value provided by the global scale model. However, the comparison against satellite surface temperature data indicates that with such a dry soil the model underestimates the surface temperatures during the night and overestimates them during the day in rural areas. This compensating error points to a likely deficiency in the PBL and surface schemes during the persistent inversions. In urban areas, the simulations with the urban canopy parameterization tend to overestimate the nocturnal surface and air temperatures, while they reproduce wind speed correctly. Finally, a new methodology, based on a comparison of the differences between maximum and minimum temperatures for couples of stations, to assess the model's capability to reproduce the spatial variability of air temperature, is introduced, and the results show that the use of the multilayer urban canopy scheme and the adjustment of the soil moisture are both needed to improve the reproduction of such spatial distribution.