Thermoelectric generator at optimal power with external and internal irreversibilities