This is code for performing a calculation that should be repeated with different levels and temperatures. I am concerned about the organization of this code as I feel it may contain too many helper functions and seems a little unnecessarily complicated. Only the getCharge()
method needs to be exposed to the browser.
There is sample usage of this code after the initial definition.
var hprVessel = function (level, temp) {
/**
* @params {Number} level - Percentage load for HPR
* @params {Number} temp - temperature of fluid
*/
var liquidCoefficients = [
83.4485407734612,
-0.111449280945489,
0.000100022560084721,
-0.00000206240663899928
];
var vaporCoefficients = [
0.895329190066827,
0.016635773286873,
-0.0000894924196507015,
0.00000206296636296276
];
var vessel = {
// Vessel-specific Params
diameter: 1.9583 * level,
height: 0.2937 * level,
length: 15.9583 * level,
// Calculations
getLiquidVol: function () {
var r = this.diameter/2;
var sectionArea = Math.pow(r, 2) * Math.acos((r - this.height) / r);
var triangleArea = Math.sqrt(Math.pow(r, 2) - Math.pow((r-this.height), 2))*(r-this.height);
return (this.length * sectionArea * triangleArea);
},
getVesselVol: function () {
return (Math.PI * Math.pow(this.diameter, 2) * this.length);
},
getVaporVol: function () {
return (vessel.getVesselVol() - vessel.getLiquidVol());
},
getLiquidCharge: function (temp) {
return (vessel.getLiquidVol() * vessel.calcDensity(liquidCoefficients, temp));
},
getVaporCharge: function (temp) {
return (vessel.getVaporVol() * vessel.calcDensity(vaporCoefficients, temp));
},
chargeHPR: function (temp) {
return (1000 + vessel.getLiquidCharge(temp) + vessel.getVaporCharge(temp));
},
// Helper functions
calcDensity: function (regressionCoefs, temp) {
var liqDensityArray = regressionCoefs.map(function (coef) {
return coef * Math.pow(temp, regressionCoefs.indexOf(coef));
});
return vessel.sum(liqDensityArray);
},
sum: function (arr) {
return arr.reduce( function (prev, current) {
return (prev + current);
}
}
};
return {
getCharge: function() {
return vessel.chargeHPR(temp);
}
};
}
var vessel = hprVessel(1, -30);
var result = vessel.getCharge();