Last updated 02 Oct 2002

Physics 105AL Frequently Asked Questions

  1. What am I reading?
  2. Where and when can I get help?
  3. How do I contact the TA? Who is the TA?
  4. What editor should I use?
  5. What language should I use for this class?
  6. How do I compile a fortran 90 program?
  7. How do I compile a C program?
  8. That seems like a lot to type just to compile a program. Are there any shortcuts?
  9. How do I run my compiled executable?
  10. I tried running my program but the computer said something about `command not found'. What do I do?
  11. How do I send my program output to a file?
  12. How do I send my program output to the printer?
  13. My compiler doesn't tell me of any errors but my program is giving me nonsense output!

  1. What am I reading?
    This is a list of frequently asked questions for Physics 105AL. The most recent copy can be found at: www.dirac.org/p/teaching/105-faq.html. Please email me, Peter if you have any comments or questions about this document. I welcome all input, and would appreciate the email, even if it's just to tell me that you found this document useful.

    The author of this document is Peter Jay Salzman. It's released under the GPL (GNU Public License).



  2. When and where can I get help with this course?
    You can get help with your programs Monday, Wednesday and Thursday 4:10pm - 7:00pm in room 106 Phys/Geo. My office hour is Thursday 4:10pm - 5:00pm in room 106 Phys/Geo. You can come to my office hour no matter what section of 105AL you're in.

    In addition, we can set up a private office hour if you need extra help.

    Lastly, you can send your questions to me via email; I usually reply very quickly. The only thing I ask is that you do NOT email me about an assignment the day before it's due. That's one of my biggest pet peeves.



  3. How do I contact the TA? Who is the TA?
    My name is Peter Jay Salzman, and I'm a PhD candidate in physics here at UCD. I research gravitational physics and love it. I'm currently working on a numerical project for Steven Carlip to investigate quantum mechanical self gravitation and gravitational wavefunction collapse. Ask me about it and I'll talk your ear off. To reach me, send email to p@dirac.org. My web site is http://dirac.org/p.

    I'm a Linux zealot; you find my Linux webpages at http://www.dirac.org/linux. I'm also the president and founder of the Linux User Group of Davis. Come ask me about Linux.



  4. What editor should I use?
    The best editor to use is vim, which some people also call vi for historical reasons. You can learn vim from my basic vim tutorial. If you want to learn more, I also have an advanced vim tutorial. Norm Matloff also has his own vim tutorial.



  5. What language should I use for this class?
    Use whatever language you're most comfortable with. I speak many languages fluently. My suggestion is C (not C++) or fortran 90/95. Stay away from fortran 77, what I like to call FORTRAN; it's a dusty and inconvenient language.

    I use C myself, and recommend it because unlike fortran 90/95 (which is a good and convenient language for scientific programming), C is a fantastic general purpose programming language. You can do anything with it; I have yet to see network or game code written in fortran.

    Under no circumstances are you to Mathematica, Matlab or calculators to do your homework unless you are specifically told it's OK. The point is to give you practise using the tools that a real researcher uses. While things like Mathematica are researcher's tools, they are completely inappropriate for most large and difficult applications.



  6. How do I compile a fortran 90 program?
    The room 106 computers have Intel's fortran 90/95 compiler, named ifc, installed. To use it, you first need to set up variables for the compiler. Find out what shell you're using by using the ps command. You're either using bash, csh or tcsh. If you're using bash, use this command:
       . /opt/intel/compiler50/ia32/bin/ifcvars.sh
    
    If you're using csh or tcsh, use this command:
       source /opt/intel/compiler50/ia32/bin/ifcvars.csh
    
    After setting up the compiler variables, you can compile your program with:
       ifc -o hw hw.f90
    
    This tells the compiler that your source code is named hw.f90 and the executable should be called hw. There is documentation for ifc in /opt/intel/compiler50/docs.

    The room 106 computers also have the GNU fortran 77 compiler, named g77, installed. You can use it without setting up compiler variables, unlike ifc. You can compile your programs with g77 with:

       g77 -o hw hw.f
    
    This tells the compiler that your source code is named hw.f and the executable should be called hw. See man g77 for documentation.

    Since I no longer use fortran at all, I can't give much help beyond what's already here.



  7. How do I compile a C program?
    Please read my tutorial on compiling C programs. It's available in postscript, html, docbook



  8. That seems like a lot to type just to compile a program. Are there any shortcuts?
    Yes, there are quite a few shortcuts to make life easy on unix.

    First, there's filename completion. Suppose there's a file called solution-to-problem-1.f90 in your directory. To compile beer, you could type out f90 solution-to-problem-1.f90. With filename completion, all you have to do is is type f90 s and hit the tab key. Once you hit the tab key, unix will fill in the olution-to-problem-1.f90 and you can simply press enter to compile the program.

    There's one fly in the ointment. Suppose you had another file named solution-to-problem-2.f90. If you type f90 s and hit the tab key, how does unix know which file you're talking about? It doesn't. The unix machine will fill in olution-to-problem- and beep at you. You have three options here. 1: You can press a 1 and hit the tab again and unix will fill in .f90. When you hit enter, the file solution-to-problem-1.f90 will be compiled. 2: You can press a 2 and hit the tab again and unix will fill in .f90. When you hit enter, the file solution-to-problem-2.f90 will be compiled. 3: You can press tab again and unix will show you the name of the two files which can be matched by what you typed. You can then choose by typing a 1 or 2 and hitting tab once again to complete the file name.

    Secondly there's command line history. If you find yourself editing, compiling, editing, compiling, etc a program, you can use the up arrow key to cycle through the commands that you last entered. For example, suppose you have a file A_really_cool_program.c and you just entered:

    gcc -Wall -ansi -pedantic -O3 -o A_really_cool_program A_really_cool_program.c
    *whew* So you now have an executable named a.out and you run it. It doesn't work. So you type
    vi A_really_cool_program.c
    (or pico), edit the file and save it. Now you have to type that whole long gcc line again. Instead, press the up arrow key. You'll see:
    vi A_really_cool_program.c
    Press the up arrow key one more time. You'll now see:
    gcc -Wall -ansi -pedantic -O3 -o A_really_cool_program A_really_cool_program.c
    Hit enter and the program will compile. You just traded hitting 3 keys for typing about 80 characters.

    Lastly, makefiles make compiling process very convenient. See my makefile tutorial.



  9. How do I run my compiled executable?
    The job of a compiler is to turn your C source code (which the computer can't execute) into an executable. An executable is a program that the computer can execute. Unless you specify the -o option to gcc, the executable that both fortran and C compilers generate is called a.out (see the FAQ on compiling C programs).

    If your executable is named a.out, you can run your program by simply typing ./a.out.

    For more information, see the FAQ on "command not found".



  10. I tried to run my program but the computer said something about `command not found'. What do I do?
    The short answer is to read the section on running executables. If you're interested, here's the long answer:

    For those who know unix, the current working directory, ".", isn't in your PATH environment variable. To fix this, type:

    export PATH=$PATH:.
    The problem with this is that you'll have to type this every time you log into the computer. To make this change permanent, use vi to edit the file ~/.bash_profile and append this to the last line:
    export PATH=$PATH:.
    After typing this line, save the file and quit the editor. Now you need to activate the change you just made. From the unix prompt, type:
    source .bash_profile
    and you'll never see this problem again.



  11. How do I send my program output to a file?

    You need to put your output into a file before you can email or print it, so it's important to know how to send your program output to a file. Suppose your executable is named a.out. One way to do this is to redirect the output to a file called outfile with the command:

    a.out > outfile
    There will be a new file, called outfile that contains the output of your program. You don't have to use the name "outfile"; use whatever filename you like. However, be careful about redirecting the program output into an already existing file. If you do that, the file's previous contents will be lost. The other way is to use the script command. From your command prompt, type:
    script
    Now run the program by typing ./a.out (or whatever your executable is named). After the program is done, type:
    exit
    At this point you'll see a file called typescript which contains the output of your program.

    Now that you have your output in a file (using either method 1 or method 2), you can print it or email it to someone.



  12. How do I send a file to the printer?
    From the room 106 computers, if you want to print the contents of a textfile named "myfile.txt", use either this command:
       enscript myfile.txt
    
    or:
       lpr myfile.txt
    
    and your printout will appear at the printer in room 106.

    DO NOT PRINT YOUR EXECUTABLE!   DO NOT PRINT A.OUT!   ONLY PRINT TEXT FILES!



  13. My compiler doesn't tell me of any errors but my program is giving me nonsense output!
    I won't cover ALL the things that could've gone wrong, but a common source of error is integer division, although C is more prone to integer division problems than fortran. Here's a little program I wrote to demonstrate the pitfalls of integer and mixed integer/real division in fortran. The comments are the program's output:
    program tester
    implicit none
    	integer, PARAMETER :: dp = selected_real_kind(15,307)
    	real(kind=dp) :: i
    
    	print *, 2/7             ! 0
    	print *, 2.0/7           ! 0.2857143
    	print *, 2/7.0           ! 0.2857143
    
    	print *, 2_dp/7          ! 0
    	print *, 2/7_dp          ! 0
    	print *, 2_dp / 7_dp     ! 0
    
    	print *, 2.0_dp/7        ! 0.2857142857142857
    	print *, 2.0/7_dp        ! 0.2857143
    	print *, 2.0_dp / 7_dp   ! 0.2857142857142857
    
    	print *, 2_dp/7.0        ! 0.2857143
    	print *, 2/7.0_dp        ! 0.2857142857142857     
    	print *, 2_dp/7.0_dp     ! 0.2857142857142857     
    
    end program tester
    
    For the fortran programmers, try to understand why 2.0/7_dp and 2_dp/7.0 return single precision numbers even though I specify double precision. Hint: it's the same reason for both cases; look at the 2nd set of print statements.

    Another pitfall for the fortran novice is that variables in fortran are case insensitive! In other words, C and c are the same variables in fortran. Bleah!







    This document was written with the vi editor on a SuSe 6.2 Linux system at 4:20am. No HTML editor or Microsoft product was used in its creation. Please email me if you have any suggestions on format, content or colours! Just say no to buggy software