Source: chemistry/FreezingPointSolutionModeler.js

/**
 * This class models the freezing point for a solution
 * @module
 */
define([
    './Constants',
    './SolventSolutionModeler',
    './SpecieState',
    './SolventConcentrationSolutionModeler'
], function (Constants, SolventSolutionModeler, SpecieState, SolventConcentrationSolutionModeler) {
    var instance = null;
    /**
     * @class FreezingPointSolutionModeler
     */
    function FreezingPointSolutionModeler () {
        // Freezing point of the solvent in K
        this.freezingPointSolvent = Constants.FREEZING_POINT_OF_WATER;

        // Depression constant of the solvent in K kg/mol
        this.depressionConstantSolvent = Constants.FREEZING_DEPRESSION_CONST_WATER;

        // The default model used when no other model is selected
        this.DEFAULT = 0;

        // The NOMINALFREEZING model assumes that all the solutions behave as pure
        // solvent. Freezing point must be specified or water is assumed.
        this.NOMINALFREEZING = 0;

        // The EVAULATEDFREEZING model considers that is the solvent and all the
        // finite solutes affects the freezing point according to their molality.
        //
        // All species with infinite amount are assumed ficticious and with no effect.
        this.EVALUATEDFREEZING = 1;

        // Stores the simulation model being used
        this.simulationModel = this.DEFAULT;
    };

    /**
     * Returns the freezing point for the solution
     * @param s The solution
     * @returns {number} The freezing point in K
     */
    FreezingPointSolutionModeler.prototype.getValue = function (s) {
        // in K
        var freezingPoint = 0.0;

        // NOMINAL
        if (this.simulationModel === this.NOMINALFREEZING) {
            if (SolventSolutionModeler.getSimulationModel() === SolventSolutionModeler.AQUEOUS) {
                freezingPoint = Constants.FREEZING_POINT_OF_WATER;
            }
            else {
                freezingPoint = this.freezingPointSolvent;
            }
        }
        // EVALUATED
        else if (this.simulationModel === this.EVALUATEDFREEZING) {

            // calculate number of moles of solutes
            var molsSolute = 0.0;

            // start in 1 to avoid solvent
            var i;
            for (i = 1; i < s.getSpeciesCount(); i++) {
                var sn = s.getSpeciesAt(i);

                if (sn.archetype.state !== SpecieState.SOLID
                    && sn.archetype.state !== SpecieState.GAS && !sn.infinite) {
                    molsSolute += sn.moles;
                }
            }

            // calculate the mass of solvent
            var weightSolvent = 0.0;

            // SOLVENTINFINITE
            if (SolventConcentrationSolutionModeler.simulationModel === SolventConcentrationSolutionModeler.SOLVENTINFINITE) {
                // Weight of solvent
                weightSolvent = s.liquidVolume * 1000
                    * SolventSolutionModeler.getSolventDensity();

                // subtract the mass for the mols of water replaced
                var weightWaterReplaced = 0;
                for (i = 0; i < s.getSpeciesCount(); i++) {
                    var sn = s.getSpeciesAt(i);
                    if (sn.archetype.state !== SpecieState.SOLID) {
                        weightWaterReplaced += sn.getWaterReplaced()
                            * SolventSolutionModeler.getSolventMW();
                    }
                }
                if (weightWaterReplaced != 0) {
                    weightSolvent -= weightWaterReplaced;
                }

                // so the weight of solvent is...
                weightSolvent = (weightSolvent < 0) ? 0  :weightSolvent;
            }

            // SOLVENTFINITE
            else if (SolventConcentrationSolutionModeler.simulationModel === SolventConcentrationSolutionModeler.SOLVENTFINITE) {
                var solvent = s.getSpeciesAt(0);
                if (solvent !== null) {
                    weightSolvent = solvent.getWeight();
                }
            }

            if (SolventSolutionModeler.getSimulationModel() === SolventSolutionModeler.AQUEOUS) {
                freezingPoint = Constants.FREEZING_POINT_OF_WATER
                    - Constants.FREEZING_DEPRESSION_CONST_WATER * 1000
                    * molsSolute / weightSolvent;
            }
            else if (SolventSolutionModeler.getSimulationModel() === SolventSolutionModeler.NONAQUEOUS) {
                freezingPoint = this.freezingPointSolvent
                    - this.depressionConstantSolvent * 1000 * molsSolute
                    / weightSolvent;
            }
            else if (SolventSolutionModeler.getSimulationModel() === SolventSolutionModeler.NOSOLVENT) {
                freezingPoint = this.freezingPointSolvent;
            }
        }

        return freezingPoint;
    };

    /**
     * Resets the default status
     */
    FreezingPointSolutionModeler.prototype.setDefault = function () {
        this.simulationModel = this.DEFAULT;
    };

    /**
     * Load the characteristics of the model from the problem file
     * @param model
     * @param freezingPointSolvent
     * @param depressionConstantSolvent
     */
    FreezingPointSolutionModeler.prototype.loadProperties = function(model, freezingPointSolvent, depressionConstantSolvent) {
        if (model.toUpperCase() === "NOMINAL")
            this.simulationModel = this.NOMINALFREEZING;
        else if (model.toUpperCase() === "EVALUATED")
            this.simulationModel = this.EVALUATEDFREEZING;

        var value = 0.0;
        // freezing point in K
        if (freezingPointSolvent !== null) {
            value = parseFloat(freezingPointSolvent);
            if (!isNaN(value))
                this.freezingPointSolvent = value;
        }

        // depression constant in K kg/mol
        if (depressionConstantSolvent !== null) {
            value = parseFloat(depressionConstantSolvent);
            if (!isNaN(value))
                this.depressionConstantSolvent = value;
        }
    };

    FreezingPointSolutionModeler.getInstance = function() {
        if(instance === null) {
            instance = new FreezingPointSolutionModeler();
        }
        return instance;
    };
    return FreezingPointSolutionModeler.getInstance();
});