/**
* Calculations for vehicle emissions
* @module
*/
import { requiredKeys } from '../../../../../keys/required';
import { kmToMiles } from '../../../../../maths/conversions';
/**
*
* @returns {struct}
*/
const checkRequiredKeys = (skv_config, required_keys) => {
const rst_required_keys = requiredKeys(skv_config, required_keys);
// check to see if any keys missing
if (
!rst_required_keys.success
|| rst_required_keys.data.length > 0
) {
throw new Error(`Missing required keys: ${rst_required_keys.data} for running.vehicle.journey`);
}
};
/** *******************************************
*************** CALCULATIONS ****************
******************************************** /
/**
* Production uplift to give an estimate for how much co2 is produced to create
* and transport the fuel
* @param {number} co2_g_per_mile
* @returns {number} uplift in co2 grams per mile
*/
const productionUplift = (co2_g_per_mile, percentage) => co2_g_per_mile * (percentage / 100);
/**
* For regular petrol and diesel cars and PHEVs in CS mode
* @param {struct} skv_config
* @returns {number}
*/
const tailpipeCO2gPerMile = (co2_gpkm) => {
// convert vehicle's co2 grams per km to co2 grams per mile
// equivalence is set to true so that 1km = 1mile which increases the value
// because a mile is > than a km
const co2_g_per_mile = kmToMiles(co2_gpkm, true);
return co2_g_per_mile;
};
/**
* Pass in Weighted Combined CO2 g per mile and remove the electric portion to just
* leave the ICE CO2 g per mile
* @param {number} wc_co2_g_per_mile most likely co2_gpkm which is tailpipe figures converted to miles
* @param {number} wc_mpg
* @param {number} mpg
* @param {number} battery_range
* @returns {number}
*/
const iceCO2gPerMileFromWCCO2gPerMile = (wc_co2_g_per_mile, wc_mpg, mpg) => wc_co2_g_per_mile * (wc_mpg / mpg);
/**
* calculate kWh used per mile
* @param {number} battery_total_capacity_kwh
* @param {number} battery_range_miles
* @returns {number}
*/
function energyConsumedPerMile(battery_total_capacity_kwh, battery_range_miles) {
return battery_total_capacity_kwh / battery_range_miles;
}
/**
* calculate co2 g per mile emitted from combination of co2 from tariff and energy used from battery
* @param {number} tariff_co2_gpkwh
* @param {number} kwh_per_mile
* @returns {number}
*/
function co2gPerMileFromBatteryEnergy(tariff_co2_gpkwh, kwh_per_mile) {
return tariff_co2_gpkwh * kwh_per_mile;
}
/**
* For Electrics and electric portion of phevs
* @param {struct} skv_config tariff_co2_gpkwh, battery_range_miles, battery_total_capacity_kwh
* @returns {struct}
*/
const calculateElectricCO2gPerMile = (skv_config) => {
const required_keys = ['tariff_co2_gpkwh', 'battery_range_miles', 'battery_total_capacity_kwh'];
checkRequiredKeys(skv_config, required_keys);
const kwh_per_mile = energyConsumedPerMile(skv_config.battery_total_capacity_kwh, skv_config.battery_range_miles);
return co2gPerMileFromBatteryEnergy(skv_config.tariff_co2_gpkwh, kwh_per_mile);
};
export {
tailpipeCO2gPerMile,
productionUplift,
calculateElectricCO2gPerMile,
iceCO2gPerMileFromWCCO2gPerMile,
energyConsumedPerMile,
co2gPerMileFromBatteryEnergy,
};