Modeling and Optimal Control of Energy Distribution Grids