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(){
    i++;
    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
            photons.addChild(photon[p]);
            // 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;
                electrons.addChild(electron[m]);
                // ..and hence flies away!
            }
            photons.removeChild(photon[p]);
            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
            electrons.removeChild(electron[m]);
            delete electron[m];
            // the electron disappears since it is being absorbed by the anode plate!
            if (m > 4){m = 0;}
        }
    }
}

Similar Posts

  • العقل السليم ≠ Common Sense

    في الكتب العلمية المترجمة إلى العربية، تُترجم عبارة “common sense” خطأً إلى “العقل السليم” أو “المنطق السليم”، مما ينشئ تناقضاً منطقياً خطيراً. فالمصطلح الإنكليزي يشير إلى الحدس اليومي والتصورات البديهية، بينما “العقل السليم” بالعربية يعني الاستدلال المنطقي الصارم. عندما نقرأ أن ميكانيك الكم “يناقض العقل السليم”، يبدو وكأن الفيزياء الحديثة تتعارض مع المنطق ذاته، بينما الحقيقة أنها تتعارض فقط مع حدسنا المبني على خبرتنا اليومية. هذه المغالطة الترجمية قوّضت صورة الفيزياء في الثقافة العربية وأضعفت الثقة بالعلم الحديث.

  • | |

    Photoelectric Effect Experiment Simulation

    With this comprehensive and realistic-like photoelectric effect experiment simulation, you will be able to illustrate the following:
    The variations of the photocurrent versus potential.
    The variations of the photocurrent versus light intensity.
    The variation of the kinetic energy of the ejected electrons versus the incident light frequency.
    It comes with a graph where you can trace each type of variation as you vary the parameters of the experiment.
    Plus, you can experiment and discover more with this simulation.

  • |

    Conservation of linear momentum

    This demonstration explains conservation of linear momentum through cannon recoil. When a cannon fires a bullet, it recoils backward with much lower velocity due to its greater mass, illustrating the inverse relationship between mass and velocity. The post provides complete mathematical derivations showing that total momentum remains constant in an isolated system where net external force is zero.

  • A new publication based on the Virtual Oscilloscope simulation

    The paper: Using a web-based and stand-alone oscilloscope for physics experiment during Covid-19 pandemic, Mahizah Ismail et al (2023),  Phys. Educ. 58 015006, is based on the Virtual Oscilloscope simulation. This paper was authored by Mahizah Ismail, Farid Minawi, Wan Zul Adli Wan Mokhtar, Noraihan L Abdul Rashid and Ahmad K Ariffin.
    The article DOI: https://iopscience.iop.org/article/10.1088/1361-6552/ac95eb

  • Super AI perception beyond human reality

    Given the accelerated development of AI, there might soon be a ‘Super AI’. And I am not talking about an AI with super faster processing or super deeper analysis. I am talking about an AI that may develop a form of “perception” fundamentally different from human ways of perceiving reality. To us, its view of the world may appear almost metaphysical.

Leave a Reply

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

two × five =