$TAI_1.0.1thermostat(heaterASolutionStart$heaterATimeStepEndheaterRateA heaterIndicatorA$sensorTemperatureAfindArgument(. 1.0.1thermostat'ü// Variables to be read from user defined string in Hook Functions (Analyze>Params>Hook Functions) var T = -1; var dT = -1; var sensorElemA = -1; var powerOnA = -1; var powerOffA = -1; //Variables to be used in this user routine var heaterOnA = false; var heatRateA = 0; // The solution start hook function initializes any global variables needed // by the other functions. This allows a single check to be performed to // see if all variables were set propertly. // If a global variable is not initializes, an error message is printed to the // console window. function heaterASolutionStart( userString ) { T = findArgument( "TA", userString ); dT = findArgument( "dTA", userString ); sensorElemA = findArgument( "sensorElemA", userString ); powerOnA = findArgument( "powerOnA", userString ); powerOffA = findArgument( "powerOffA", userString ); System.println("Got user String set A: "+(T)+","+(dT)+","+(sensorElemA)+","+(powerOnA)+","+(powerOffA)) if ((T > -273.15) && (dT > 0) && (sensorElemA > 0) && (powerOnA >= 0) && (powerOffA >= 0)) { heatRateA = 0; } else { System.println( "Error: Failed to initialize ALL global variables." ); System.println( " See the documentation for this script" ); } } // This hook function is called at the end of the time step to see if the fan should be turned // on or off for the next time step. function heaterATimeStepEnd( userString ) { if(heaterOnA) System.println("The heaterA is currently ON"); else System.println("The heaterA is currently OFF"); var sensorTempA = 0.0; sensorTempA = sensorTemperatureA(); System.println("sensor temperature A = " + (sensorTempA) + " C"); if (sensorTempA < (T-dT)) { System.println(" Turning heater A ON" ); // Turn heater A on heaterOnA = true; } else if (sensorTempA > (T+dT)) { System.println(" Turning heater A OFF" ); // Turn heater A off heaterOnA = false; } else { // Keep the current setting for heater A } System.println(" "); } // This function is used to return the Heat Rate for the for the heater. // This function looks at the flag to see if the heater is on or off and returns // the appropriate heat rate. function heaterRateA( userString, inputData ) { //Adding stuff to get part and element areas var inputData = arguments[1]; var partArea = inputData.partArea; var elemArea = inputData.nodeArea; //System.println("got Part Area "+(partArea)); var powerElement = 0.0; //Adding stuff to get part and element areas if (heaterOnA) { //Adding stuff to get part and element areas powerElement = powerOnA*(elemArea/partArea); //Adding stuff to get part and element areas return powerElement; } else { return powerOffA; } } //Indicator // This function is used to Indicate the Status of the heater. // This function looks at the flag to see if the heater is on or off and returns // a fixed temperature to be used as a color inidcator in the user interface or to be exported as a results file. function heaterIndicatorA( userString, inputData ) { //Adding stuff to get part and element areas var inputData = arguments[1]; var HeatOnIndexA = (240+273.2); var HeatOffIndexA = 273.2; if (heaterOnA) { return HeatOnIndexA; } else { return HeatOffIndexA; } } //End Indicator // This is an internal function used to return the sensor temperature. Currently // this function returns the front temperature of the sensor element. // This function could be extended to compute the average temperature through // the thickness of the element. It could also be extended to use multiple // elements as sensors. function sensorTemperatureA() { var temp = 20; // Retrieves the FRONT SURFACE temperature of the element var api = new API; var elemNodeIdx = new ReturnValueInt; var status = api.elementSurfaceNode( sensorElemA, api.front, elemNodeIdx ); if (status == api.success) { var nodeTemp = new ReturnValueDouble; status = api.getTemperature( elemNodeIdx.value, nodeTemp ); if (status == api.success) temp = nodeTemp.value - 273.15; else System.println( "Failed to retreieve temperature of node " + elemNodeIdx.value ); } else { System.println( "Failed to retrieve the temperature of element " + sensorElemA ); } return temp; } // This is an internal function used to retrieve parameters within the user string. // The user string will contain many parameters used witin this script. The parameters // in the user string are of the following form: // (key1=number) (key2=number) (key3=number)... // where key1, key2, and key3 are strings used when searching the string. function findArgument(key, string) { key = key + "="; var leftIndex = string.indexOf( key ) + key.length; var rightIndex = string.indexOf( ")", leftIndex ); var numberStr = string.substring( leftIndex, rightIndex ); return Number( numberStr ); }