fortran.info Prof. Welsh 2004 Sep The basics of FORTRAN programing ________________________________ Despite its age, FORTRAN remains the fastest language for scientific programming. In addition, a huge amount of astronomical programing is written in FORTRAN and since there's no compelling reason to change, this wealth of legacy code remains quite active. If you were to write a large program from scratch and you didn't need maximum speed, you'd probably use C (or java if portability was the main concern). But if you care about speed and you want to take advantage of programs that already exist, you should consider using FORTRAN. Some "higher-level" programming packages, such as IDL and Matlab or Mathematica can be very useful too. They require much less coding (and hence less chance of bugs!) and many routine tools are pre-packaged (e.g. matrix inversion, FFT, graphics, etc.). The downside is that they can be very expensive, they don't have as much flexibility, and in some cases they can be slower. The following sample FORTRAN programs are designed to help you learn FORTRAN very quickly through examples. But in order for this to work, you must take these programs and experiment with them. You MUST understand every single line of the program. Alter them and run them. Really know what is going on. - heuristic1.for = a "hello world" program with DO loops - heuristic2.for = some simple floating and integer arithmatic - heuristic3.for = using arrays, if-then-else, and subroutines - heuristic4.for = some basic I/O including writing to a file - heuristic5.for = some file I/O with external subroutines - heuristic5.make = file used to compile & link - get_data.for = subroutine to read a 2-column file - write_data.for = subroutine to write a 2-column file - plot1.for = read a file and plot it using PGPLOT - plot1.make = used to compile & link a FORTRAN program with PGPLOT - plot2.for = more complex plotting using PGPLOT - plot2.make = compile & link plot2.for To run or "execute" a program under unix: Just type its name. So to run a program called heuristic1.exe, just type "heuristic1.exe" (without the quotes). There are 2 ways to compile a FORTRAN program: 1) Simply type "f77 -o program.exe program.for" where you replace "program" with the name of your program. If you don't use the "-o" option to name your exectuable file, unix will call it a.out by default. The extention .exe is an old standard; a more modern practice it to give the executable file the name "program" without any extention. 2) Execute ("source") a file with the compile & link link commands in it. This is very useful when you have several the programs to link together and/or if you want to link with libraries. To complile your code, you type "source program.make" where the file "program.make" contains a list of all the code and libraries you want to link together. You have to follow the syntax carefully, especially the continuation command "\" at the end of a line, and the path to libraries. So for example, you'd type "source program.make" and that would do the compiling and linking. For anything beyond trivial programs, this is the preferred way to compile. Note: Under linux, you would use "g77" instead of "f77" to invoke the GNU FORTRAN compiler.