AC Turbo Graph Helper

by x4fab

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/4.1.10/highcharts.js"></script>
<div id="input-section">
  <h1>Input</h1>
  <h6>power.lut:</h6>
  <textarea id="powerlut">
0|100
500|144
1000|147
1500|156
2000|172
2500|180
3000|220
3500|200
4000|198
4500|198
5000|210
5100|232
5500|233
5950|236
6000|232
6500|210
7000|190
7500|100
8000|0
</textarea>
  <h6>engine.ini:</h6>
  <textarea id="engineini" title="Could be only turbos sections">
[TURBO_0]
LAG_DN=0.985
LAG_UP=0.9965
MAX_BOOST=1.000
WASTEGATE=0.71
DISPLAY_MAX_BOOST=0.71
REFERENCE_RPM=3000
GAMMA=5
COCKPIT_ADJUSTABLE=1
</textarea>
<div style="font-size:11px;margin-top:20px;opacity:.5">
  Paste data of specific files or just drag'n'drop them to specific fields
</div>
</div>
<div id="result-section">
  <h1>Result</h1>
  <div id="result-torque"></div>
  <h6>ui_car.json:</h6>
  <label style="font-size:11px">
    Transmission loss: <input id="transmission_loss" type="number" min="0" max="100" value="20">%
  </label>
  <button style="float:right" id="uicar_export">Copy to clipboard</button>
</div>
<footer>
  <div style="font-size:11px;margin-top:20px;opacity:.5">
    <a href="https://ascobash.wordpress.com/">ascobash.wordpress.com</a>
  </div>
</footer>

CSS

html, body { 
  min-width: 1200px;
  overflow: hidden;
}

body {
  font-family: sans-serif;
}

textarea {
  width: 100%;
  height: 150px;
  font-size: 10px;
}

#input-section {
  position: absolute;
  width: 50%;
  padding: 20px;
  overflow: hidden;
  box-sizing: border-box;
}

#result-section {
  position: absolute;
  width: 50%;
  left: 50%;
  padding: 20px;
  overflow: hidden;
  box-sizing: border-box;
}

svg [zIndex="8"] { color: red }

section {
  border: 1px solid gray;
  padding: 20px;
}

input {
  background: transparent;
  border: none;
  font-size: inherit;
  font-family: inherit;
  padding: 0;
  margin: 0;
  text-align: right;
}

footer {
  position: absolute;
  bottom: 20px;
  z-index: -1;
  left: 50%;
  width: 200px;
  margin-left: -100px;
  overflow: hidden;
  box-sizing: border-box;
  
  text-align: center;
}

JavaScript

function Turbo(section) {
  this.maxBoost = +section['MAX_BOOST'];
  this.wastegate = +section['WASTEGATE'];
  this.gamma = +section['GAMMA'];
  this.referenceRpm = +section['REFERENCE_RPM'];
}
Turbo.prototype.calculateMultipler = function(rpm) {
  return Math.min(this.wastegate, this.maxBoost * Math.min(1.0, Math.pow(rpm / this.referenceRpm, this.gamma)));
};
AcUtils = {
  parseLut: function(x) {
    return x.split('\n').map(function(a) {
      return a.trim().split('|');
    }).filter(function(x) {
      return x.length == 2;
    }).map(function(a) {
      return ({
        rpm: +a[0],
        torque: +a[1]
      });
    });
  },
  parseIni: function(x) {
    return x.split(/\[(?=[A-Z\d_])/).slice(1).map(function(a) {
      return a.match(/^([A-Z\d_]+)\]([\s\S]+)/);
    }).filter(function(x) {
      return x;
    }).reduce(function(a, b) {
      a[b[1]] = b[2].split('\n').map(function(x) {
        return x.match(/^\s*(\w+)\s*=\s*([^;]*)/);
      }).filter(function(x) {
        return x;
      }).reduce(function(a, b) {
        return (a[b[1]] = b[2].trim(), a);
      }, {});
      return a;
    }, {});
  }
};
AcMath = {torqueToPower: function(torque, rpm) {
    var bhpToWatts = 745.699872;
    return rpm * torque / (bhpToWatts / Math.PI / 2 * 60);
  }};
function getTorqueValues() {
  var power = AcUtils.parseLut($('#powerlut').val());
  var engine = AcUtils.parseIni($('#engineini').val());
  var turbos = [];
  for (var i = 0,
      section = void 0; section = engine[("TURBO_" + i)]; i++) {
    turbos.push(new Turbo(section));
  }
  power.forEach(function(x) {
    return x.torque *= 1.0 + turbos.reduce(function(a, b) {
      return a + b.calculateMultipler(x.rpm);
    }, 0.0);
  });
  return power;
}
function updateGraph() {
  var power = getTorqueValues();
  $('#result-torque').highcharts({
    chart: {
      type: 'spline',
      backgroundColor: 'transparent',
      style: {fontFamily: 'sans-serif'}
    },
    title: {text: ''},
    xAxis: {title: {text:...