<!---
/*
    Engine Design Utility
    Author: David Andrews <david_andrews112@yahoo.com.au>
    Date: Early mornings in Feb 2004
    
    All information provided here is strictly unofficial. No responsibility
    will be taken for anything result from anything to do with this program.
*/

/* 
    TODO

    Provide max piston velocity/accelerations
    Provide crank degrees when max piston velocity/accelerations occur
    Provide crank degree where cam lift is at max
    Provide description of engine based on calculated values?

*/

function prechecks(form){

    //pre-checks

    //check the `class' of block, crank and head all match
    blclass = (form.blocks.options[form.blocks.selectedIndex].text).toString().slice(0,2);
    crclass = (form.cranks.options[form.cranks.selectedIndex].text).toString().slice(0,2);
    heclass = (form.heads.options[form.heads.selectedIndex].text).toString().slice(0,2);
    caclass = (form.cams.options[form.cams.selectedIndex].text).toString().slice(0,2);

    if(blclass != crclass || blclass != heclass || 
        blclass != caclass || heclass != caclass ||
        heclass != crclass || crclass != caclass){
        alert("That Block, Head, Crank, Cam combination is invalid..\nReason: different classes.");
        return false;
    }

    //piston pin diametre = rod pin diametre
    if(form.piston_pin_diametre.value != form.rod_piston_end_bore.value){
        alert("That piston and rod are incompatible.\nReason: piston gudgeon pin is incorrect for its bore.");
        return false;
    }

    //rod big end diametre is bigger then crank rod shaft diametre
    if(form.rod_big_end_bore.value != form.crank_rod_bearing_od.value){
        alert("That rod and crank are incompatible.\nReason: different big end diametres.");
        return false;
    }

    //rod big end thickness is different than crank rod bearing thickness 
    if(form.rod_big_end_thickness.value != form.crank_rod_width.value){
        alert("That rod and crank are incompatible.\nReason: the rod is not the correct thickness for the crank.");
        return false;
    }

    //piston bore = block bore + block overbore
    if(parseFloat(form.piston_bore.value) != parseFloat(form.block_bore.value)+parseFloat(form.block_overbore.value)){
        alert("That piston and block are incompatible.\nReason: different bore sizes.");
        return false;
    }

    //gasket height set
    if(form.gasket_height.value == ""){
        alert("Please set gasket height.\n");
        return false;
    }
    //redline set
    if(form.redline.value == ""){
        alert("Please set redline.\n");
        return false;
    }

    return true;

}

function postchecks(form){

    //post-checks

    //deck_clearance is less than gasket
    if(parseFloat(form.deck_clearance.value)*(-1) > form.gasket_height.value){
        alert("Warning, piston protrudes beyond head gasket.");
    }

    return true;

}
 

function inspect(form){

    //prechecks
    if(!prechecks(form))
        return;

    bore = parseFloat(form.block_bore.value)+parseFloat(form.block_overbore.value);
    bore_area = Math.pow(bore/2,2)*Math.PI;

    //calculate displacement
    displacement = (form.block_cylinders.value*form.crank_stroke.value*bore_area/1000000).toFixed(3);
    form.displacement.value = displacement+" litres";


    //calculate deck clearance
    deck_clearance = parseFloat(form.block_height.value)-(parseFloat(form.rod_length.value)+parseFloat(form.crank_stroke.value/2)+parseFloat(form.piston_pin_height.value));
    form.deck_clearance.value = deck_clearance.toFixed(3)+" mm";

    //calculate compression ratio
    gasket_bore = parseFloat(bore)+parseFloat(1);
    gasket_cc = Math.pow((gasket_bore)/2,2)*Math.PI*form.gasket_height.value/1000;
    deck_clearance_cc = Math.pow((gasket_bore)/2,2)*Math.PI*deck_clearance/1000;
    tdc_volume = parseFloat(form.head_cc.value)-parseFloat(form.piston_cc.value)+parseFloat(gasket_cc)+parseFloat(deck_clearance_cc);
    bdc_volume = (displacement*1000/form.block_cylinders.value)+parseFloat(tdc_volume);
    form.compression_ratio.value = (bdc_volume/tdc_volume).toFixed(3);
   
    //calculate ratios
    form.rod_stroke.value = (form.rod_length.value/form.crank_stroke.value).toFixed(3);
    form.bore_stroke.value = (form.crank_stroke.value/bore).toFixed(3);

    //calculate max rod angle
    form.max_rod_angle.value = ((Math.asin((form.crank_stroke.value/2)/form.rod_length.value))*180/Math.PI).toFixed(3)+" degrees";
   
    //calculate max average piston speed
    form.max_piston_speed.value = (form.crank_stroke.value*2*form.redline.value/1000).toFixed(3)+" m/min";

    pistonposition(form, 90);
    pistonvelocity(form, 45);

    //post checks
    postchecks(form);
   
}

function pistonposition(form, theta){

    O = parseFloat(theta)*Math.PI/180;
    s = parseFloat(form.crank_stroke.value)/2;
    r = parseFloat(form.rod_length.value);
    u = Math.pow(r,2)-Math.pow(s*Math.sin(O),2);

    p = s*Math.cos(O)+(Math.sqrt(u));

    //alert(p);

}

function pistonvelocity(form, theta){

    O = parseFloat(theta)*Math.PI/180;
    s = parseFloat(form.crank_stroke.value)/2;
    r = parseFloat(form.rod_length.value);
    pl1 = Math.pow(r,2)-Math.pow(s*Math.sin(O),2); //u
    pl2 = Math.pow(s,2)*Math.sin(O*2);

    v = -s*Math.sin(O) - (pl2)/Math.sqrt(pl1);

    //alert(pl1);
    //alert(pl2);
    //alert(v);

}

function pistonacceleration(form, theta){

    O = parseFloat(theta)*Math.PI/180;
    s = parseFloat(form.crank_stroke.value)/2;
    r = parseFloat(form.rod_length.value);
    pl1 = Math.pow(r,2)-Math.pow(s*Math.sin(O),2); //u
    pl2 = Math.pow(s,2)*Math.sin(O*2);

    


}



//-->
