vehicle/running/emissions/components/fuelandelectric/co2gpm.js

  1. /**
  2. * Calculate journey, fuel and electric ppm, total mileages
  3. *
  4. * @module
  5. * @since 0.1.21
  6. */
  7. import { isPHEV } from '../../../../classification';
  8. import { getMPGofficalRatio } from '../../../journey/calculate';
  9. import { co2_kg_per_litre as skv_co2_kg_per_litre, production_uplift_percentage } from '../../../../../maths/constants';
  10. import { kmToMiles } from '../../../../../maths/conversions';
  11. import { getIsCSMode } from '../../../util';
  12. import {
  13. productionUplift,
  14. tailpipeCO2gPerMile,
  15. iceCO2gPerMileFromWCCO2gPerMile,
  16. calculateElectricCO2gPerMile,
  17. } from './calculate';
  18. /**
  19. * Co2 grams per mile for PHEVs
  20. * @param {struct} skv_config
  21. * @returns {number}
  22. * @module
  23. */
  24. const iceCO2gPerMileForPHEVs = (skv_config) => {
  25. // if (skv_config.is_cs_mode) {
  26. const skv_fuel_cfg = {
  27. // work out charge sustaining mode first
  28. co2_kg_per_litre: skv_co2_kg_per_litre[skv_config.fuel_type],
  29. mpg: skv_config.mpg,
  30. wc_mpg: skv_config.wc_mpg,
  31. tailpipe_co2_gpkm: skv_config.tailpipe_co2_gpkm,
  32. is_cs_mode: skv_config.is_cs_mode,
  33. battery_range_miles: skv_config.battery_range_miles,
  34. };
  35. // get the weighted combined co2 g per mile (i.e electric + ICE)
  36. const wc_co2_g_per_mile = kmToMiles(skv_fuel_cfg.tailpipe_co2_gpkm, true);
  37. // remove the electric portion from the weighted combined co2 g per mile
  38. return iceCO2gPerMileFromWCCO2gPerMile(wc_co2_g_per_mile, skv_config.wc_mpg, skv_config.mpg);
  39. };
  40. /**
  41. * calculate grams for ICE portion
  42. * @param {struct} skv_config ice_pp_litre, mpg, vat_percentage
  43. * @returns {struct}
  44. */
  45. const iceCO2gPerMile = (skv_config) => {
  46. const skv_return = {
  47. tail_pipe: 0,
  48. production: 0,
  49. };
  50. const skv_default_cfg = {
  51. mpg: '',
  52. mpg_user: '',
  53. // weighted combined mpg
  54. wc_mpg: '',
  55. // wltp what comes out of tailpipe
  56. tailpipe_co2_gpkm: '',
  57. fuel_type: '',
  58. // is charging sustaining mode likely passed in when calculating a journey
  59. is_cs_mode: false,
  60. // for removing the electric portion from weighted combined
  61. battery_range_miles: '',
  62. };
  63. // replace default values with the ones from the config passed in
  64. const skv_cfg = Object.assign(skv_default_cfg, skv_config);
  65. if (isPHEV(skv_cfg.mpg, skv_cfg.battery_range_miles)) {
  66. skv_return.tail_pipe = iceCO2gPerMileForPHEVs(skv_cfg);
  67. skv_return.production = productionUplift(skv_return.tail_pipe, production_uplift_percentage);
  68. } else {
  69. skv_return.tail_pipe = tailpipeCO2gPerMile(skv_cfg.tailpipe_co2_gpkm);
  70. skv_return.production = productionUplift(skv_return.tail_pipe, production_uplift_percentage);
  71. }
  72. // at this point we've calculated co2 gram per mile based on official figures
  73. // if the user has provided a custom mpg then apply as a ratio
  74. if (skv_config.mpg_user) {
  75. const ratio = getMPGofficalRatio(skv_config.mpg, skv_config.mpg_user);
  76. skv_return.tail_pipe *= ratio;
  77. skv_return.production *= ratio;
  78. }
  79. return skv_return;
  80. };
  81. /**
  82. * calculate grams for Electric portion
  83. * @param {struct} skv_config tariff_co2_gpkwh, battery_range_miles, battery_total_capacity_kwh
  84. * @returns {struct}
  85. */
  86. const electricCO2gPerMile = (skv_config) => {
  87. const skv_return = {
  88. production: 0,
  89. };
  90. skv_return.production = calculateElectricCO2gPerMile(skv_config);
  91. return skv_return;
  92. };
  93. /**
  94. * get co2 grams on electric and ice
  95. * @param {struct} skv_config
  96. * @returns {struct}
  97. */
  98. const getCO2gPerMile = (skv_config) => {
  99. let skv_ice_result = {
  100. tail_pipe: 0,
  101. production: 0,
  102. };
  103. let skv_electric_result = {
  104. production: 0,
  105. };
  106. const skv_default = {
  107. total_miles: '',
  108. // ICE
  109. tailpipe_co2_gpkm: '',
  110. fuel_type: '',
  111. mpg: '',
  112. wc_mpg: '',
  113. // Electric
  114. tariff_co2_gpkwh: '',
  115. battery_range_miles: '',
  116. battery_total_capacity_kwh: '',
  117. };
  118. // replace default values with the ones from the config passed in
  119. const skv_local_config = Object.assign(skv_default, skv_config);
  120. // Charge Sustaining mode for phevs
  121. skv_local_config.is_cs_mode = getIsCSMode(skv_config);
  122. if (skv_local_config.mpg > 0) {
  123. skv_ice_result = iceCO2gPerMile(skv_local_config);
  124. }
  125. if (skv_config.battery_range_miles > 0) {
  126. skv_electric_result = electricCO2gPerMile(skv_local_config);
  127. }
  128. return {
  129. electric: skv_electric_result,
  130. ice: skv_ice_result,
  131. };
  132. };
  133. export {
  134. getCO2gPerMile,
  135. iceCO2gPerMile,
  136. electricCO2gPerMile,
  137. iceCO2gPerMileForPHEVs,
  138. };