# NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

## Easy Java Simulations (2001- ) => Optics => Topic started by: Fu-Kwun Hwang on March 08, 2010, 12:23:16 pm

 Title: Ray tracing for laser light Post by: Fu-Kwun Hwang on March 08, 2010, 12:23:16 pm The following simulation is created due to request from Re: Ray tracing (polygon: range from 3-32 faces) (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1434.msg5455#msg5455)The slider at the top can be used to adjust minimum intensity ray to be drawn/calculated in the simulation.[ejsapplet]The range of the X-Y coordinate system is between -100 and 100. number of points =Please enter correct number of pointsThe data points should be enter in clockwise direction!X= enter , separated x coordinateY= enter , separated x coordinateLaser Pointer x=,y=How to calculate the reflected light from incoming ray?Support the vector for the incoming ray is $\vec{a}$, We need to know either the component of incoming ray in the normal direction $\vec{b}$ or the parallel component along the interface $\vec{b}'$ to find the reflected ray vector $\vec{c}$ with  $\vec{c}=\vec{a}-2\vec{b}=2\vec{b}'-\vec{a}$If you know the unit vector $\hat{b}$ , then $\vec{b}=(\vec{a}\cdot\hat{b}) \hat{b}$.If you know the unit vector $\hat{b}'$, then $\vec{b}'=(\vec{a}\cdot\vec{b})\hat{b}'$.The refracted angle can be calculated with $n_1 \sin\theta_1=n_2\sin\theta_2$,where $\sin\theta_1=\frac{\vec{a}\cdot\hat{b}'}{|\vec{a}|}$ or $\cos\theta_1=\frac{\vec{a}\cdot\hat{b}}{|\vec{a}|}$The normal vector $\vec{b}=\vec{a}-\vec{b}'$.The refracted ray can be calculated with $\hat{b}\,\cos\theta_2+\hat{b}'\,\sin\theta_2$(http://forum.phy.ntnu.edu.tw/neditor/popups/pics/20100307_p22904.gif)You can study many different cases with this applet. Please check out attached files for examples. Title: Re: Ray tracing for laser light Post by: liesenbergk on March 11, 2010, 05:17:40 pm Dear Professor,Thanks for your programming efforts. I have been testing the new simulation. What I am missing is the possibility of defining the polygon with coordinates as well as the position of the laser beam. This feature is available in the previous simulation and it is very usefull. I wonder if this could be programmed as well.Kind regards from Brazil!  :) Title: Re: Ray tracing for laser light Post by: Fu-Kwun Hwang on March 11, 2010, 08:48:06 pm O.K. It is done. Title: Re: Ray tracing for laser light Post by: liesenbergk on March 11, 2010, 09:50:40 pm Thanks Professor, that was more than fast!  :)I don't want to abuse, but I am missing the drag mode in this new version...Best regards! Title: Re: Ray tracing for laser light Post by: Fu-Kwun Hwang on March 11, 2010, 11:14:25 pm Because the feature has been implemented before, what I did was copy code from previous page.And I find some free time after I went home tonight. The drag mode should be available now. Title: Re: Ray tracing for laser light Post by: liesenbergk on March 12, 2010, 05:43:00 pm Wow Professor! You're my idol!!! Thanks for your efforts and congratulations for you outstanding skills.  :D Title: Re: Ray tracing for laser light Post by: liesenbergk on March 15, 2010, 06:26:24 pm Dear Professor,The new simulation is very useful for me, especially with the possibility of entering the polygon's defining coordinates.There are, however, some changes which could be very interesting.The first one (please see attached "laser pen handling" picture) concerns on how to handle the laser pen. As it is now, you can drag the pen to coordinates defines by its basis, and then rotate the pen over the axis of the basis. If you want to follow a particular line you have to, in most cases, drag and rotate, in order to avoid the entering of the laser pen's output in the optical body. It would be very nice if the laser pen can be dragged and rotated based on the coordinates of its beam output. It would also be very nice if the laser pen's angle could be entered, as shown in the related picture.The other wished enhancement is the possibility of exporting ray data (please see attached "desired ray data" picture). Here the defining coordinates of the way of the primary beam would be outputted, if possible with its final intensity too.I will understand if you don't have time or if this is to complicated to do. But,... it would be nice!  :)With best regards! Title: Re: Ray tracing for laser light Post by: Fu-Kwun Hwang on March 18, 2010, 10:02:35 pm I justed modified the code so that a new window will popup when you click the arrow of laser pointer or click the check box labeled "table".You can change the angle with slider or text field.I also add a table to show a list of (x,y,I) which represent all rays (include  reflected ray and refracted ray).You should be able to find which one is reflected ray, which one is refracted ray from intensity. Title: Re: Ray tracing for laser light Post by: liesenbergk on March 19, 2010, 05:10:57 pm That is stunning, Master! Thanks a lot. The data table is very useful.Kind regards, Professor! Title: Re: Ray tracing for laser light Post by: tmzoughi on March 28, 2010, 10:42:40 pm This is wonderful simulation and it offers many possibilities. I tried to use it for a particular geometry and I think I have detected a small bug. Or it might be an opportunity for me to learn something new!I am attaching an image of the setting I am using. This is the output of the lines generated:j=0,I=1.0,c=1.7669502000928863,x=-75.51111260566398,y=-21.117714323462188j=0,I=1.0,c=0.26179938779914935,x=-75.51111260566398,y=-21.117714323462188j=1,I=0.057796105403213144,c=-1.8325957145940464,x=-15.075862213492771,y=-4.924137786507226j=2,I=0.9422038945967869,c=0.44556125394332635,x=-15.075862213492771,y=-4.924137786507226j=3,I=0.9422038945967869,c=-0.4455612539433263,x=58.049565975939494,y=30.0j=4,I=0.0733637418622893,c=-2.6960313996464667,x=100.0,y=9.964797822930265j=5,I=0.8688401527344976,c=-0.18820469334913387,x=100.0,y=9.964797822930265j=6,I=0.0733637418622893,c=2.6960313996464667,x=16.320254742697305,y=-29.999999999999993j=7,I=0.02264313020441028,c=2.016357580738223,x=4.221937396573228,y=-24.221937396573228j=8,I=0.05072061165787902,c=2.1447458012139355,x=4.221937396573228,y=-24.221937396573228j=9,I=0.00118179551253197,c=-2.016357580738223,x=-21.67403968794734,y=29.999999999999996j=10,I=0.02146133469187831,c=2.1705020069039946,x=-21.67403968794734,y=29.999999999999996j=11,I=0.006915452246752739,c=-2.1447458012139355,x=-30.83551657260938,y=30.0j=12,I=0.04380515941112628,c=1.7669502000928863,x=-30.83551657260938,y=30.0The first bug seems to be related to the ray corresponding to j=5. Since this represents a beam going from n=1.5 to n=1, the refracted beam should be larger than what what is shown. The same problem happens to beam j=8 when it refracts into beam j=12. The problem does not happen for beam j=7!The other item I don't understand is the fact that we have two beams reflected at x=4.221937396573228,y=-24.221937396573228 (j=7 and j=8)Thanks for your help clarifying this and for all of your amazing work. Title: Re: Ray tracing for laser light Post by: Fu-Kwun Hwang on March 28, 2010, 11:18:07 pm Could you give me the coordinates for those polygon points and light source and angle to test it out! Title: Re: Ray tracing for laser light Post by: tmzoughi on March 29, 2010, 12:07:33 am Thanks. I have attached the xml file that I have used. Taha Title: Re: Ray tracing for laser s Post by: Fu-Kwun Hwang on March 29, 2010, 10:42:47 am Yes. It is a bug. Thank you for reporting it.The last ray should be a total reflection ray. However, the ray was treated emmitted from air to glass, so the code did something wrong.It is fixed now. The above simulation was designed such that you can use javascript to change the shape of the polygon (px,py) and the position of laser point(xs,ys).You do not need to re-compile the EJS. For you case :n=4;px=10,-50,100,100py=-30,30,30,-30xs=-90ys=-25Suppose the name for the EJS applet is "polygonsn". The following two javascript call will set up the above configuration.Code:document.polygonsn._setVariables("n=4;px=10,-50,100,100;py=-30,30,30,-30;xs=-90;ys=-25");//set up the above parametersdocument.polygonsn._model.setit();// Some parameters need to re-calculate according to the above change,this is the method defined as Custom codeEJS variables are decleared as global variables, so you can change it with _setVariables (";" separated variable assignment , use "," to separate indicidual array (variable) values.I just added another option so that user can edit coordinate for polygon in the popup window (when table is checked).There is a second tabbed panel which display coordinate for the polygon points (it is editable).However, only the fist N rows are useful (You can adjust N with slider). Title: Re: Ray tracing for laser light Post by: tmzoughi on March 29, 2010, 04:49:51 pm Thanks. I knew I could modify it with JavaScript but I needed it to pop up for students under that particular configuration. I prefer that they ran it as a JAR. They then can play around and do whatever they want. Thanks for your help with this and all of your great work.Taha Title: Re: Ray tracing for laser light Post by: tmzoughi on March 29, 2010, 05:28:13 pm I just noticed that there is still a problem with the refraction. It still behaves like from air to glass.Taha Title: Re: Ray tracing for laser light Post by: Fu-Kwun Hwang on March 29, 2010, 05:45:12 pm I can not find the bug, please let me know where you found it.The following is the modified version from your case. I also fixed other minor bug. Title: Re: Ray tracing for laser light Post by: tmzoughi on March 29, 2010, 06:51:57 pm Hi Fu-Kwun:Thanks for your help. I am attaching a picture to explain. There are 3 refractions in this case. The first is from air to glass. It works fine. The other two are from glass to air, they don't work properly.  In the picture, I drew the normal in red. I drew the original beam direction in green and the direction of what we should expect for the refracted beam in purple. Title: Re: Ray tracing for laser light Post by: Fu-Kwun Hwang on March 30, 2010, 09:53:32 am Thank you for your help!The mistake has been fixed with new method -- to calculate direction for refracted ray (using vector analysis instead change angle directly).Please download the latest jar and try it again. Please let me know if you find more problems. Title: Re: Ray tracing for laser light Post by: tmzoughi on March 30, 2010, 06:33:21 pm Wonderful it works. Thanks so much for all of your efforts. The new method is instructive.Taha Title: Re: Ray tracing for laser light Post by: vinay on March 31, 2010, 10:12:27 pm sir i am just begining the topic of laser optics ....i want to know why did the meta stable state dosnt take place between two energy states and take place between more than three or three.. Title: Re: Ray tracing for laser light Post by: Fu-Kwun Hwang on April 01, 2010, 06:05:15 am I assumed that you understand about the meaning of energy level of electrons in atom.The electron is likely to be in the lower energy level (ground state).It might be excited to higher energy level with external source.Normallly, there are higher probability to find electron in lower energy state.When an electron are excited to higher energy state, it will fall back to lower energy state in a chort time (around 10-6-10-8 s)The are selection rule between different energy levels. If it satisfy the selection rule, the probability to jump from one state to another state is much higher,otherwise it become very difficult. In the case of  two energy levels state:  If it is easy to jump from lower energy level A to higher energy level B, it is also easy to jump back from B to A. If it is difficult to jump from lower energy level A to higher energy level B, it is also difficult to jump back from B to A.And the probability to find electron in level A is always higher than B, $P_B/P_A = e^{-\Delta E/kT}$ where $\Delta E$ is the energy difference between those two states. In the case of three energy levels state: A , B, C  It might be that it is easy to jump from A to B (the highest energy level), and it is also easy to jump from B back to C However, it is difficult to jump from C back to A (because some selection rule is not satisfied so the probability to make transition is smaller than normal case)Then, it is possible to pump electron from A to B and later find out more electron in energy level C than in energy level A.