(TAI_1.0.1thermostat2(heaterBSolutionStart$heaterBTimeStepEndheaterRateB heaterIndicatorB$sensorTemperatureBfindArgumentB&Ú 1.0.1thermostat2&¦ var TB = -1; var dTB = -1; var sensorElemB = -1; var powerOnB = -1; var powerOffB = -1; var heaterOnB = false; var heatRateB = 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 heaterBSolutionStart( userString ) { TB = findArgumentB( "TB", userString ); dTB = findArgumentB( "dTB", userString ); sensorElemB = findArgumentB( "sensorElemB", userString ); powerOnB = findArgumentB( "powerOnB", userString ); powerOffB = findArgumentB( "powerOffB", userString ); System.println("Got user String set B: "+(TB)+","+(dTB)+","+(sensorElemB)+","+(powerOnB)+","+(powerOffB)) if ((TB > -273.15) && (dTB > 0) && (sensorElemB > 0) && (powerOnB >= 0) && (powerOffB >= 0)) { heatRateB = 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 heaterBTimeStepEnd( userString ) { if(heaterOnB) System.println("The heaterB is currently ON"); else System.println("The heaterB is currently OFF"); var sensorTempB = 0.0; sensorTempB = sensorTemperatureB(); System.println("sensor temperature B = " + (sensorTempB) + " C"); if (sensorTempB < (TB-dTB)) { System.println(" Turning heater B ON" ); // Turn heater B on heaterOnB = true; } else if (sensorTempB > (TB+dTB)) { System.println(" Turning heater B OFF" ); // Turn heater B off heaterOnB = false; } else { // Keep the current setting for heater B } 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 fan is on or off and returns // the appropriate heat rate. function heaterRateB( userString, inputData ) { //Adding stuff to get part and element areas var inputData = arguments[1]; var partArea = inputData.partArea; var elemArea = inputData.nodeArea; var powerElement = 0.0 //Adding stuff to get part and element areas if (heaterOnB) { //Adding stuff to get part and element areas powerElement = powerOnB*(elemArea/partArea); //Adding stuff to get part and element areas return powerElement; } else { return powerOffB; } } //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 heaterIndicatorB( userString, inputData ) { //Adding stuff to get part and element areas var inputData = arguments[1]; var HeatOnIndexB = (255+273.2); var HeatOffIndexB = 273.2; if (heaterOnB) { return HeatOnIndexB; } else { return HeatOffIndexB; } } //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. // Retrieves the FRONT SURFACE temperature of the element function sensorTemperatureB() { var temp = 20; var api = new API; var elemNodeIdx = new ReturnValueInt; var status = api.elementSurfaceNode( sensorElemB, 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 " + sensorElemB ); } 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 findArgumentB(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 ); }