コード例 #1
0
    //Box dimensions are integer values from 1 to 10
    //Box dimensions are updated with a slider, the properties of which are set in the editor
    private void UpdateBoxDimensions()
    {
        //get the old V/T ratio so that if pressure is constant the temperature can be updated
        float box_dim = box.transform.localScale.x;
        float VT      = box_dim * box_dim * box_dim / T;

        float new_scale = box_slider.value;

        //box dimensions are limited, so constant pressure can only apply whilst the box is free to change volume
        if (new_scale == box_slider.minValue || new_scale == box_slider.maxValue)
        {
            isPressureConstant.isOn = false;
        }
        //check if this should be a constant pressure calculation - if so then also update the temperature
        if (isPressureConstant.isOn)
        {
            T = Mathf.Round((new_scale * new_scale * new_scale) / VT); //ideal gas law for constant pressure
            temp_slider.SetWithCallback(T, false);                     //set the slider without calling the event handler

            //now set the new temperature and reset the particle system
            //update the most probable speed
            vp = Mathf.Sqrt(2 * k * T / m);
            //update the gas particles. since temp has changed need to recalculate the speeds and then set them
            CalculateAndSetSpeeds(m, T, N, divs, vp);
        }


        box.transform.localScale = new Vector3(new_scale, new_scale, new_scale);
        IdealGasParticlesSystem.Clear();
        var s = IdealGasParticlesSystem.shape;                                    //the shape module which should be a box

        s.scale = new Vector3(90f * new_scale, 90f * new_scale, 90f * new_scale); //make the scale 90% of the box size but also multiply by 100
        IdealGasParticlesSystem.Emit(N);

        //reset the particles to the correct speeds - since this is only affecting volume, speeds do not change, so no
        //need to recalculate (if constant pressure then calculation is done in above if statement).
        SetParticleSpeeds(N, vp);

        //Update graphs
        PlotPVTGraphPoint();
    }
コード例 #2
0
    //Updates the temperature and then resets the particle speeds
    //if input field was used to change the temperature then run with input = 0; if slider, then input = 1
    //If pressure is set to be constant then when updating temperature, also need to update the volume accordingly.
    private void UpdateTemperature(int input)
    {
        //get the current V/T ratio, before the new temp is set
        float box_dim = box.transform.localScale.x;
        float VT      = box_dim * box_dim * box_dim / T;

        float temp;

        if (input == 0)
        {
            if (float.TryParse(temp_input.text, out temp))
            {
                if (temp >= min_temp && temp <= max_temp)
                {
                    T = temp;
                    temp_slider.value = T;
                }
                else
                {
                    temp_input.text = T.ToString("F0");
                }
            }
        }
        //if input is not 0 then the slider was used to change temperature
        //the slider properties have been set in the Unity editor
        else
        {
            T = temp_slider.value;
            temp_input.text = T.ToString("F0");
        }

        //if pressure is constant then this temperature change should cause a volume change in proportion
        //The volume needs to change without calling the box_slider on value changed event since that would start an infinite loop
        if (isPressureConstant.isOn)
        {
            float new_V     = VT * T;   //ideal gas law for constant pressure
            float new_scale = Mathf.Pow(new_V, (1 / 3f));
            //set the box slider to the new value but not not call the update volume function
            //only do this if the scale the box will be set to is within the min/max box scales
            //if it isn't then switch off pressure being constant
            if (new_scale >= box_slider.minValue && new_scale <= box_slider.maxValue)
            {
                box_slider.SetWithCallback(Mathf.Pow(new_V, (1 / 3f)), false);      //assign the new box dimension - this will call the update volume method

                //now set the new volume and update particles
                box.transform.localScale = new Vector3(new_scale, new_scale, new_scale);
                IdealGasParticlesSystem.Clear();
                var s = IdealGasParticlesSystem.shape;                                    //the shape module which should be a box
                s.scale = new Vector3(90f * new_scale, 90f * new_scale, 90f * new_scale); //make the scale 90% of the box size but also multiply by 100
                IdealGasParticlesSystem.Emit(N);
                //reset the particles to the correct speeds
                SetParticleSpeeds(N, vp);
                //Update graphs
                //PlotPVTGraphPoint();
            }
            else
            {
                isPressureConstant.isOn = false;
            }
        }

        //update the most probable speed
        vp = Mathf.Sqrt(2 * k * T / m);
        //update the gas particles
        CalculateAndSetSpeeds(m, T, N, divs, vp);

        //Update graphs
        PlotPVTGraphPoint();
    }