Document Sample

IDL Tutorial The purpose of this tutorial is to introduce you to the IDL programming language, and to present you with sufficient tools to complete your first homework assignment. This is not intended to present a complete explanation of IDL. If there are topics not covered in this tutorial which you find interesting or necessary, use the IDL help or consult with Dr. Parker or the TA. To begin using MS Windows, click on the „start‟ button at the lower left of the screen. Select the „programs‟ option, then the „IDL 5.1‟ folder, and then the „IDL‟ program. Doing this will start IDLDE, the IDL Development Interface. The IDLDE window consists of various parts. A detailed explanation of IDLDE can be accessed by selecting the Help option from the Menu Bar and selecting the following items in order: Help -> Contents -> UsingIDL -> IDL for Windows -> The Main IDL Window 1 We are now prepared to experiment with IDLDE and eventually begin programming. From the Input Line, type: print, 3*5 And you will see the number 15 displayed in the Output Log. Note: IDL is NOT case sensitive. The following commands: (print, 3*5) (Print, 3*5) (PRINT, 3*5) are all equivalent. We can define a variable and give it calculated values as follows: From the Input Line, type: A=3*5 print, A A = Sqrt(A) print, A And you will see the numbers 15 and 3.87 in the output log. If you forget what the type and value of a variable are, you can type Help, (varname) to display the current status and value of the variable. Try typing Help, A. Since most of the problems that we encounter in this course cannot be solved with one or two line commands, it is necessary to write out our instructions in the form of “procedures”. We can later “compile” these procedures and execute them from the input line, or from another procedure. If the concepts of “procedures” or “compile” are not familiar to you at this time, their meaning will become clear in the following examples. You can use a text editor such as Notepad to edit your procedures, or you can use the IDL Editor (the latter option is usually easier). To use the IDL Editor, Click inside the Editor Window. You can then type in IDL commands, but they will not be immediately executed. When you have finished writing your procedure, select the „File‟ option from the Menu Bar, and then select „Save‟. The name you choose for the file must be the same as the name of your main procedure. Then you can select the „Run‟ option from the Menu Bar and select „Compile (filename)‟. Then if no errors occurred during compilation you may again select the „Run‟ option from the Menu Bar and select „Run (filename)‟. This will execute your procedure. All output will be displayed in the Output Log, and any necessary input must be entered on the Input Line. 2 From the Menu Bar, select „File‟ and then „New‟. This will open the Editor Window. Try typing this example in the Editor Window: PRO example a=5 ;sets a to 5 b=13.5 ;and b to 13.5 c=a+b ;adds a and b and puts the result in c d=a*sin(b) ;sets d to the product of a and Sine(b) e=b*exp(a) ;sets e to the product of b and Exp(a) print,a,b,c,d,e ;prints the results END Save this as „example‟, then compile and run it. The Output Log should display the following results. 5 13.5000 18.5000 4.01892 2003.58 Select „File‟ from the Menu Bar, and select „Close‟ to exit the „example‟ procedure. We are now prepared to implement the radioactive decay routine that is explained in chapter 1 of the Giordano text. The structure is the same as that set forth on pages 4-9. IDL is however structurally different than “True Basic”, so there will be some necessary differences. The basic outline is this: Calculate N(t) through 100 time- Input No, , t steps using Euler‟s method (p.3) Plot Results Like in the text, we shall break our program up into four parts: the three subroutines (we will call them procedures in IDL) and a main routine. Unlike “True Basic”, we need to define the procedures in the code before we can call them. So lets begin with a small header to document our program. ;Radioactive Decay Model ;Euler Method ;Reference: Giordano pp 8-9 ;Samuel Jones May, 18 1998 The semicolon (;) tells IDL that all of the information on the line following the semicolon is to be ignored by the processor. Therefore any comment that you add to your program should be preceded by a semicolon. Next we need a procedure to read in the information our simulation will need. This procedure will be called from the main routine and will pass back the values that it receives. 3 pro initialize,nuclei,t,tau,dt print, "Initial number of nuclei:" read, x nuclei(0)=x t(0)=0 print, "Time constant (tau):" read, tau print, "Timestep:" read, dt end „pro‟ signals the beginning of the procedure initialize. The values it will return are nuclei, t, tau, dt. The variables nuclei and t will be defined in the main procedure to be one dimensional arrays of size 100 containing decimal values. Tau and dt are also defined as decimal quantities. This procedure prompts for the value of No, and then reads the result into the variable x. The first element of nuclei is then set to x. This is necessary because IDL will not allow array elements to be directly read in from the keyboard using the read function. The time array, t, has its first value set to zero. The procedure then reads in and stores tau and dt. These values are passed back to the calling routine when the END keyword is reached. Next we will take the values that were received from the initialize procedure and pass them through a procedure to calculate the value of N (nuclei) for the next 100 points in time. pro calc,nuclei,t,tau,dt for i=0, (n_elements(t)-2) do begin nuclei(i+1)=nuclei(i)-(nuclei(i)*(dt/tau)) t(i+1)=t(i)+dt endfor end The calc procedure takes in the arrays nuclei and t, and also the values tau and dt. It executes a „for loop‟ which means that it begins setting i to zero and running the lines until the endfor statement. Once it reaches the endfor statement, it increments i by one and repeats the commands between for and endfor. Once i reaches the number inside the parenthesis the loop terminates. For our case, that terminating number is the number of elements in the array t. We know that this is 100, because we define it to be so, but using the IDL command „n_elements‟ is more general because now our procedure will work on an array of any size, and not only those with 100 elements. We use (n_elements(t) – 2) because IDL counts an array of 100 elements from element 0 to element 99. So we desire for the last calculation our loop makes to be on the 99th element, not the 100th. 4 The next line is the Euler method for advancing N in time. It is explained in the text. We also wish to advance the time variable. This is a simple addition of dt for each timestep. Once the for loop has executed the specified number of cycles, it terminates. The procedure then executes the END command, which terminates the procedure and returns the calculated values to the calling procedure. The next step is to graphically present our results. In this graphics procedure there is also an implementation of the exact solution to our decay problem, so that it can be compared to the numerical result. pro displey,nuclei,t,tau,dt exactsol=fltarr(100) exactsol(0)=nuclei(0) for i=0, (n_elements(exactsol)-2) do begin exactsol(i+1)=(exactsol(0)*exp(-(i+1)*dt/tau)) endfor plot, t, nuclei, yrange = [0, max(nuclei)], $ title='RadioActive Decay - Euler Method', xtitle = 'Time', $ ytitle='Number of Nuclei', xrange=[0, max(t)],/xstyle oplot, t, exactsol, line=1 end This procedure is named „displey‟ because display is a reserved keyword in IDL. The procedure receives the arrays nuclei and t, and the variables tau and dt. It then creates an array of decimal numbers of 100 elements to contain the exact solution. The procedure then sets its first element equal to the first element of our numerical solution array. Then it goes through a for loop to define the values of N(t) according to the exact solution: N(t) = Noe-t/ . Then we can use the IDL plot command to graph our data, and the command oplot to graph the exact solution on the same plot. For more information on plot and oplot, type ?plot or ?oplot at the input line. We are now ready for the main routine. We shall call it decay. pro decay nuclei=fltarr(100) t=fltarr(100) initialize,nuclei,t,tau,dt calc,nuclei,t,tau,dt displey,nuclei,t,tau,dt end Decay begins by creating two arrays of floating numbers each with 100 elements. These will be the arrays containing the number of nuclei and the time at each timestep. Decay then calls the procedure initialize to set up the initial conditions for our problem. Then it calls calc to execute the loop of Euler‟s solution for N(t). Next Decay calls the display procedure to graph our results. Once this is complete, IDL executes the END command and the program terminates. 5 So the final program looks like this: ;Radioactive Decay Model ;Euler Method ;Reference: Giordano pp 8-9 ;Samuel Jones 18/may/98 pro initialize,nuclei,t,tau,dt print, "Initial number of nuclei:" read, x nuclei(0)=x t(0)=0 print, "Time constant (tau):" read, tau print, "Timestep:" read, dt end pro calc,nuclei,t,tau,dt for i=0, (n_elements(t)-2) do begin nuclei(i+1)=nuclei(i)-(nuclei(i)*(dt/tau)) t(i+1)=t(i)+dt endfor end pro displey,nuclei,t,tau,dt exactsol=fltarr(100) exactsol(0)=nuclei(0) for i=0, (n_elements(exactsol)-2) do begin exactsol(i+1)=(exactsol(0)*exp(-(i+1)*dt/tau)) endfor plot, t, nuclei, yrange = [0, max(nuclei)], $ title='RadioActive Decay - Euler Method', xtitle = 'Time', $ ytitle='Number of Nuclei', xrange=[0, max(t)],/xstyle oplot, t, exactsol, line=1 end pro decay nuclei=fltarr(100) t=fltarr(100) initialize,nuclei,t,tau,dt calc,nuclei,t,tau,dt displey,nuclei,t,tau,dt end Type this in the IDL Editor, save it as „decay‟. You may then compile and run it (see page 2). Assuming that you have made no typing errors this should compile. Once it is running, type the following when prompted from the Output Log: Initial number of nuclei: 10000 Time constant (tau): 20 Timestep: 5 The output should show dots representing the numeric solution to the decay, and a solid line showing the exact solution. It should look a lot like this: 6 Notice that the Euler method gives a result that exhibits some numerical error. But for the most part it follows the exact solution very closely. Now that the procedure is running, make modifications to the code. If you want to perform a function that was not covered in this brief tutorial, search for it in „Help‟ from the Menu Bar. Once you feel comfortable with the code, you should be prepared to complete the first homework assignment. 7

DOCUMENT INFO

Shared By:

Categories:

Tags:
how to, Photoshop Tutorials, web page, King Tut, web pages, Table of Contents, Text Effects, the Next, Tutorial works, DENVER ART MUSEUM

Stats:

views: | 17 |

posted: | 12/24/2010 |

language: | English |

pages: | 7 |

OTHER DOCS BY niusheng11

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.