e-Learning Developer

An Inspiring Code

A section of code caught my interest as I was revising the code after finishing the Photoelectric effect simulation, so I paused and gave it some thought. I stopped thinking like a programmer at that point and switched back to thinking like a physicist. How we can simulate physical phenomena with a few lines of code astounded me. These lines portray a tale of creation and vanishing. It describes the creation of a photon, its subsequent disappearance when an electron absorbs it on the cathode plate’s surface, the subsequent departure of the electron from the cathode, and lastly the eventual disappearance of the electron when it reaches the other plate and captured.

If you have some programming skills, you can read this story through the lines of code shown below, or you can read it in the comment lines (the lines preceded by //). 

function movePhotons(){
    if (laserONFlag){
        var productionFactor = Math.ceil(1.5/ sim.calculation.getIntensity());
        if (i % productionFactor === 0){
            photon[++p] = new sim.lib.photon();
            // new photon is created
            photon[p].x = 40;
            photon[p].y = getRndInteger(-31, 31);
            // this is where the photon is created
            // now the photon is ejected
            if (p > 10){p = 0;}
            Kmax = sim.calculation.getKmax(sim.currentElement) * 1.6e-19;
            // this is the maximum possible kinetic energy that the electron
            // that absorbs the photon may possess
            // and it depends on parameters contained in the class "Calculation"
            V0 = tubeLength * Math.sqrt(2 * Kmax / me) * 
            // this is the maximum possible initial velocity that an ejected electron can possess
            (sim.calculation.getLambda() < sim.calculation.getMaxLambda(sim.currentElement));
            // this is a condition: if the wavelength of the incident light beam is below the
            // threshold wavelength of the irradiated element, then an electron may be ejected,
            // otherwise, the energy of the incident photon is not enough for the electron to be
            // released 
            if (sim.calculation.calculateVoltageCoeff(sim.currentElement) === 1){
                v0 = V0;
                // this is when the photocurrent reaches a maximum value, called the saturation current
                // this happens when the potential of the anode plate is high enough
                // to pull all the released electrons and capture them
            } else {
                v0 = getRndNumber(0.7 * V0 , 1.01 * V0);
                // this allows for the fact that the electrons which absorb photons will be ejected
                // with kinetic energy of maximum value V0 that is calculated above (here the randomization
                // between 0.7 * V0 and 1.01 * V0 is not realistic but to allow an adequate visualization
                // for educational purpose only)
                // So probability plays a role here.
            i = 0;
    for (p in photon){
        photon[p].x += 5;
        if (photon[p].x > (380 + photon[p].y * Math.tan(beamAngle))){
            // if a photon reaches the plate
            if (getRndNumber(0,1) > 0.5){
                // Another probabilistic factor here allows for the fact not all the photons hitting
                // the plate get caught and cause the ejection of electrons
                // So probability plays a role here too
                // Note that the speed of photons and electrons in the tube in this simulation and the
                // number of photons that cause ejection of electrons are not accurate but tuned to be
                // clearly visualized so that the learners understand the concept visually.
                electron[++m] = new sim.lib.electron();
                // the electron that absorbed the incident photon and
                // gained enough energy and got lucky to escape from the
                // surface of the plate..
                electron[m].x = -8;
                electron[m].y = photon[p].y / Math.cos(beamAngle);
                // ..starts from the surface of the plate..
                electron[m].alpha = 0.6;
                electron[m].v = v0;
                // ..with an initial speed previously calculated..
                electron[m].v0 = v0;
                // ..and hence flies away!
            delete photon[p];
            // .. and don't forget that the incident photon vanished
            // since it is absorbed by the electron!
            if (p > 4){p = 0;}

function moveElectrons(){
    // Now the electron is flying
    for (m in electron){
        // for every electron which was released from the platee..
        a = (voltageONFlag) * e * sim.calculation.getVoltage() / (me * tubeLength);
        // ..this is its acceleration due to electrostatic force on it
        // caused by the potential difference between the cathode and the anode.
        // notice that when the power supply is off, the flag "voltageONFlag" is
        // false (false is equivalent to 0), so a = 0 (since there is no potential 
        // difference anymore), and hence, the electron is not accelerating 
        electron[m].v += 0.5 * a;
        // this is how much the speed of the electron is increasing (depending on a, the 
        // acceleration)
        // note that if a is zero, then the speed of the electron becomes constant
        electron[m].x -= 2.0e-9 * electron[m].v;
        // this is how fast the electron is moving (depending on the speed)
        // if the speed is constant, then the motion of the electron is uniform,
        // while if the speed is increasing, then the electron is accelerating
        // (remember that this happens when a potential difference is applied between the plates)
        if (electron[m].x < -440 || electron[m].x > -8){
            // if the electron reaches the anode plate
            delete electron[m];
            // the electron disappears since it is being absorbed by the anode plate!
            if (m > 4){m = 0;}

Leave a Comment

Your email address will not be published. Required fields are marked *

19 + 6 =