Cogeneration and trigeneration systems