Model-Based Predictive Control for building energy management. I: Energy modeling and optimal control