Source: chemistry/Cooling.js

/**
 * @module
 */
define([
    './Constants'
], function (Constants) {
    /**
     * @class Cooling
     * 
     * Currently, power is fixed to -0.5 kJ/s
     */
    Cooling = function (power=-0.5) {
        // in kJ/sec
        this.power = power;
        this.lastCall = -1;
    };

    /**
     * Start the cooling timer.
     */
    Cooling.prototype.start = function () {
        this.lastCall = new Date().getTime();
    };


    Cooling.prototype.stop = function () {
        this.lastCall = -1;
    };

    /**
     * Returns the change in heat since last call
     * @param source Solution related to this object
     * @returns {number} Heat in kJ
     */
    Cooling.prototype.getHeatSinceLastCall = function (source) {
        var T = source.getTemperature();
        var Ta = Constants.ROOM_TEMPERATURE; // Ambient Temperature.

        if (T < Ta + .01 && T > Ta - .01) {
            source.setThermal(null);
            // What is the purpose of this try / catch block?
            try {
                source.changeTemperature(Constants.ROOM_TEMPERATURE);
            }
            catch (e) {
                console.log(e.stack);
            }
            return 0;
        }
        else {
            // K Value for the equation.
            var k = .05;
            var time = new Date().getTime();

            // Calculate Change in Time in seconds.
            var dt = (time - this.lastCall) / 1000;
            this.lastCall = time;

            var dT = T - Ta;
            var Tfinal = Ta;

            Tfinal += dT * Math.exp(k * -1 * dt);

            // in kJ
            var heat = (Tfinal - T) * source.getDensity()
                * source.getSpecificHeat() * source.getVolume()
                * Constants.CAL_TO_J;

            return heat;
        }
    };
    return Cooling;

});