The author of this document is Peter Jay Salzman. It's released under the GPL (GNU Public License).
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.
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.
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.
. /opt/intel/compiler50/ia32/bin/ifcvars.shIf you're using csh or tcsh, use this command:
source /opt/intel/compiler50/ia32/bin/ifcvars.cshAfter setting up the compiler variables, you can compile your program with:
ifc -o hw hw.f90This 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.fThis 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.
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.cPress 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.cHit 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.
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".
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_profileand you'll never see this problem again.
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 > outfileThere 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:
scriptNow run the program by typing ./a.out (or whatever your executable is named). After the program is done, type:
exitAt 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.
enscript myfile.txtor:
lpr myfile.txtand your printout will appear at the printer in room 106.
DO NOT PRINT YOUR EXECUTABLE! DO NOT PRINT A.OUT! ONLY PRINT TEXT FILES!
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 testerFor 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!