Optimal design of electrochemical energy storage