Multiblock_Die_Calc/diecalc.js

214 lines
8.7 KiB
JavaScript

// Some constant variables
// Eventually these might become configuration options if this project gets complex enough
var MC_MAX_HEADS = 5;
// This function gets the reduction of area with two provided sizes, and returns it
function getReduction(startSize, finalSize) {
var startArea = Math.PI * ((startSize / 2) * (startSize / 2));
var finalArea = Math.PI * ((finalSize / 2) * (finalSize / 2));
var reduction = ((startArea - finalArea) / startArea) * 100;
return reduction;
}
// This function gets the elongation
function getElongation(startSize, finalSize) {
return (Math.pow(startSize / finalSize, 2) - 1) * 100;
}
// This function will get the average reduction, across a number of dies.
// Used to calculate how many dies we'll need
function getAverageReduction(startSize, finalSize, numDies) {
return (1 - Math.pow((Math.pow(finalSize / startSize, 2)), (1 / numDies))) * 100;
}
// This function gets us the number of dies we need, and returns it.
function getNumDies(startSize, finalSize) {
var reduction = getReduction(startSize, finalSize);
var numDies = 0;
while (reduction > document.getElementById("machineMaxReduction").value) {
numDies++;
reduction = getAverageReduction(startSize, finalSize, numDies);
if (reduction < document.getElementById("machineMaxReduction").value) {
if (numDies > MC_MAX_HEADS) {
numDies--;
reduction = getAverageReduction(startSize, finalSize, numDies);
reduction = Math.round(reduction * 100) / 100;
break;
}
reduction = Math.round(reduction * 100) / 100;
}
}
return numDies;
}
// This function calculates the die needed to go before the coiler
// This is the first die calcuated and dictates how the rest of the machine should be setup
function getPreCoilerDie(finalSize) {
var coilerTarget = document.getElementById("coilerTarget").value;
var s = 0; // this variable counts our steps to get a final pre-coiler die
var reduction = getReduction((finalSize + s), finalSize);
// Iterate until we pass the maximum allowed
while (reduction < coilerTarget) {
s++;
reduction = getReduction((finalSize + s), finalSize);
}
// Once we exceed the max we need to go back one size
if (reduction > coilerTarget) {
s--;
reduction = getReduction((finalSize + s), finalSize);
}
// Round it off and display it
document.getElementById("coilerROA").innerHTML = (Math.round(reduction * 10) / 10).toFixed(1);
document.getElementById("coilerElong").innerHTML = (Math.round(getElongation(finalSize + s, finalSize) * 10) / 10).toFixed(1);
return finalSize + s;
}
function getDies() {
clearScreen(); // "Clear" the screen right away
// Get our values from the web page
var startSize = document.getElementById("startSize").value;
var finalSize = document.getElementById("finalSize").value;
var maxMachineROA = document.getElementById("machineMaxReduction").value;
// If mm is selected we convert it to inches
if (document.getElementById("metric").checked == true) {
startSize = Math.round((startSize * 1000) / 25.4);
}
// Format our numbers to prevent maximum user stupidity
if (startSize < 1.0 && startSize > 0) { // If we have a 'proper' number i.e. ".130"
startSize = startSize * 1000;
} else { // re-format the number if it's 'wrong' i.e. "130"
startSize = Number(startSize);
}
if (finalSize < 1.0 && finalSize > 0) { // If we have a 'proper' number i.e. ".130"
finalSize = finalSize * 1000;
} else { // re-format the number if it's 'wrong' i.e. "130"
finalSize = Number(finalSize);
}
if (finalSize == 0 || finalSize == NaN || startSize == 0 || startSize == NaN) {
console.log("Zero");
return;
}
// Set our final size based on input
document.getElementById("coilerDie").innerHTML = "." + finalSize;
// Get the total Reduction Of Area of the run
var totalROA = getReduction(startSize, finalSize);
// Get the total elongation for the run
var totalElong = getElongation(startSize, finalSize);
// Get the die we need before the coiler
var preCoilerDie = getPreCoilerDie(finalSize);
// Get the rest of the dies...
// Start by calculating how many we need
var machineROA = getReduction(startSize, preCoilerDie);
var numDies = getNumDies(startSize, preCoilerDie);
if (numDies > MC_MAX_HEADS) {
// TODO THROW AN ERROR IF WE TRY OVERLOADING THE MACHINE
numDies = MC_MAX_HEADS;
}
var averageReduction = getAverageReduction(startSize, preCoilerDie, numDies);
// Create an array to store our die values
var dies = [];
dies.push(preCoilerDie);
var i = 0;
while (i < numDies - 1) {
dies[i + 1] = Math.round(dies[i] / Math.sqrt(1 - (averageReduction / 100)));
i++;
}
// This next section updates the webpage with our values //
///////////////////////////////////////////////////////////
for (var j = 0; j < numDies; j++) {
var curBlock = MC_MAX_HEADS - j;
if (numDies < 4) { // This helps formatting, if we have less than 4 blocks running then we need to skip block 5
curBlock--;
}
if (numDies < 3) { // If we only have two blocks running then we need to skip 4 as well as 5
curBlock--;
}
// Working die size
var htmlID = "block" + curBlock + "Working";
document.getElementById(htmlID).innerHTML = "." + dies[j];
// Guide dies
var htmlID = "block" + curBlock + "Guide";
if (dies[j + 1]) {
document.getElementById(htmlID).innerHTML = "." + (dies[j + 1] + 18);
} else { // This will check to see if it's our first die and change the guide to a pressure die
var temp = 10 + Math.floor(startSize / 5) * 5;
document.getElementById(htmlID).innerHTML = "." + temp;
document.getElementById(htmlID).style.fontWeight = "bold"; // Make our pressure die bold
}
// ROA
htmlID = "block" + curBlock + "ROA";
if (dies[j + 1]) {
document.getElementById(htmlID).innerHTML = (Math.round(getReduction(dies[j + 1], dies[j]) * 10) / 10).toFixed(1);
} else {
document.getElementById(htmlID).innerHTML = (Math.round(getReduction(startSize, dies[j]) * 10) / 10).toFixed(1);
}
// ELONGATION
htmlID = "block" + curBlock + "Elong";
if (dies[j + 1]) {
document.getElementById(htmlID).innerHTML = (Math.round(getElongation(dies[j + 1], dies[j]) * 10) / 10).toFixed(1);
} else {
document.getElementById(htmlID).innerHTML = (Math.round(getElongation(startSize, dies[j]) * 10) / 10).toFixed(1);
}
}
// Give the summary of the reduction under the table
if (document.getElementById("metric").checked == true) { // If metric, convert back to mm
document.getElementById("setupSummary").innerHTML = ("Generated die setup for " + (Math.round((startSize * 25.4) / 100) / 10) + " mm (." + startSize + "\") &#x1F80A; ." + finalSize + "\"");
} else { // If our input is imperial/inches then we'll just show it back
document.getElementById("setupSummary").innerHTML = ("Generated die setup for ." + startSize + "\" (" + (Math.round(((startSize) * 25.4) / 100) / 10) + " mm) &#x1F80A; ." + finalSize + "\"");
}
// Write our total ROA and total Elongation for the whole pull
document.getElementById("totalROAResult").innerHTML = ("Total ROA: " + (Math.round(totalROA * 100) / 100) + "%");
document.getElementById("totalElongResult").innerHTML = ("Total Elongation: " + (Math.round(totalElong * 100) / 100) + "%");
}
function clearScreen() {
// This function clears the table back to 'empty' or fresh or whatever you want to call it
for (var j = 1; j <= MC_MAX_HEADS; j++) {
var htmlID = "block" + j + "Working";
document.getElementById(htmlID).innerHTML = ("&nbsp;");
htmlID = "block" + j + "Guide";
document.getElementById(htmlID).innerHTML = ("");
htmlID = "block" + j + "ROA";
document.getElementById(htmlID).innerHTML = ("");
htmlID = "block" + j + "Elong";
document.getElementById(htmlID).innerHTML = ("");
document.getElementById("coilerDie").innerHTML = ("");
document.getElementById("coilerROA").innerHTML = ("");
document.getElementById("coilerElong").innerHTML = ("");
document.getElementById("totalROAResult").innerHTML = ("Total ROA: 0%");
document.getElementById("totalElongResult").innerHTML = ("Total Elongation: 0%");
document.getElementById("setupSummary").innerHTML = ("&nbsp;");
}
}