Build simple Fortran Program
Build simple Fortran Program
A. Molecular Dynamics and Monte Carlo simulations are often written in the computer
language Fortran, which stands for The IBM Mathematical Formula Translating System and was
first used in the 1950’s. The Monte Carlo program we’re going to work with is written in
Fortran, so we are going to first spend some time learning the basics of Fortran. There are many
resources on the internet; http://www.cs.umbc.edu/~squire/fortranclass/summary.shtml#Form
gives a long list of basics for the Fortran language, including formatting, and commands. The
commands deal with characters and strings, numbers, and logical true/false (Boolean) statements.
All computer programs, no matter what language they’re written in, need to be compiled by the
language’s compiler. This turns the code into instructions (machine language) that the
computer’s processor can understand. Different types of computers with different processors,
then, need different compilers to run the same Fortran program. The Fortran program itself is a
simple text file that can be edited in simple programs like Notepad or Wordpad. We will do
editing in the MS-DOS shell’s simple editing program. The other main part of writing and
running computer code besides the program and the compiler are input and output files. An
input file is another text file that contains parameters that are input into the program, telling it
specifically what we want it to do; we say that the program reads the input file. The program
outputs (or writes) data to output text files. These files contain the key results of the program.
One such type of output file we’ll be working with are the configuration files that give the xyz
coordinates of all the atoms in the simulation.
5. Now save the program as a text file, but include the full name in quotes to avoid having a
problematic .txt added to your file. After this, when you modify the program repeatedly you can
just ‘save’ it the quick way and it will preserve the right filename.
6. The next thing we have to do is compile the program. We will get out a file with the
suffix ‘.exe’ and then just type that filename (w/o the .exe suffix) to run the program. Loaded on
these computers is the ‘gfortran’ compiler, which is a more modern version of Fortran.
To compile our program, type “gfortran first.f –o first.exe” (you could
also choose your own executable filename, but it often makes sense to just stick with the same
name as the *.f program, which is first.f in our case). If you now search the directory (type
“dir”) you should see the new executable file. If there was a problem with your program, it
won’t compile and you’ll get an error message and possibly some help as to what is wrong.
7. Now you can see your program in action by typing “first” at the command prompt.
8. It is likely that there is too big a space between your name and the word ‘is.’ This is
because ‘name’ is given 10 character spaces due its definition as character*10 in the program.
So even if your name isn’t 10 letters, ‘name’ still contains extra spaces to make 10 characters.
We can fix this in a number of ways, each of which teaches you a bit more about programming.
So, we need to edit the program, so type in “notepad first.f”.
Hitting the up and down arrows on the keyboard will bring up recent DOS commands so you
don’t have to retype everything.
9. Let’s try using the ‘adjustr’ command. Add a line of code right after ‘name’ is read in:
name = adjustr(name)
This scoots your name to the end of the 10 character string, putting spaces in front. Resave and
exit your program (alt-F and ‘x’) and then recompile it. Use the arrow keys to get the gfortran
etc. command so you’re not always retyping it. It will overwrite the previous .exe file, so don’t
worry about renaming it.
10. Now run the program again and you should see the spacing taken care of. There is still an
issue, though, which we can see by going back to the code (notepad first.f) and adding
‘Did you know that ‘, to the format line:
20 format(‘Did you know that ’,A,' is ',ES12.5,' years old?!')
11. Save, recompile, and run the program again. You should now see a space issue between
‘that’ and your name. This is because the extra spaces got put on the other side of the ‘name’
character string. By the way, the ‘A’ that appears in the format line tells the computer that a
character is being printed where the ‘A’ is; that character is the first entry of the write line right
above, ‘name.’ The ES12.5 refers to the formatting in scientific notation of the real number, h.
The 12 sets the total width, including the d-23. The ‘d’ refers to ‘double precision’ which gives
the most significant figures by the computer for the number. The ‘5’ says only 5 decimal places
should be used.
12. Re-edit your program and comment out that ‘adjustr’ line (put a ‘c’ in the first column).
In the write (*,20) line replace ‘name’ with ‘trim(name)’. This will trim off an extra spaces.
Save, recompile, and run to see the improvement in formatting.
13. At the risk of belaboring the point, let’s use one other means of accomplishing the
formatting which also gives us a bit more information. We will find out how many characters
long your name is, and then have a variable format ready to go that incorporates the length. This
is slightly more complicated, but introduces some other programming tricks. Open up the
program (don’t forget the arrow keys to find the command you want) and add these lines:
Right after ‘h’ is defined as Plank’s constant, add a line defining ‘j’:
j = 5
Then, following the ‘read...name’ line and before the now-commented-out ‘adjustr’ line add:
i = len(name) <’len’ is a command that returns the length of the character in ( )>
print *, i,j <this just lets us know the values of i and j>
j = len_trim(name) <’len_trim’ removes the extra spaces before given the
length; this essentially gives the length of your
name>
print *, j <this will show the updated value of j>
You can save, recompile and run to see the effect of the new additions. Does it make sense?
The j was assigned to a value of 5 just to show how that is done and show that it changes after
being set equal to something else.
14. We have the length now, but this integer has to be incorporated somehow into the
formatting statement, which contains a string of characters. Open the program and add two
character*60 variables towards the top of the program, frmt and frmt2. You will also
need to add a character*1 variable called lengthA. (Again, you could call these anything
you want, but for now it’s probably easier if everyone in class uses the same words). The
formatting can work with a single character string, but that variable integer which is the length of
your name needs to be converted to a character. This is accomplished by writing the integer onto
the character. Type in the following line after the ‘print *, j’ line to do this:
write(lengthA,'(i1)') j <the ‘(i1)’ says to expect an integer of one number>
<also, be sure you typed the # 1 and not letter ‘l’>
15. Now we have to create a character string that we can use as one complete format
command. We will have to concatenate (splice) characters together. Type the following lines
after the ‘write(lengthA….’ line you just added:
frmt = "('Did you know that ',A"//lengthA//",' is',ES12.5"
frmt2 = trim(frmt)//",' years old?!')"
We couldn’t put it all in ‘frmt’ right away because the line was too long. Note that we had to
trim the frmt to get rid of any unwanted spaces. The // is the concatenate symbol and simply
joins two characters into one.
16. The last step in this reconfiguration of the formatting is easy. First, let’s put in a print
statement to make sure we’ve concatenated things correctly. After the ‘frmt2=…’ line add:
print *, frmt2
Then, we can comment out the ‘20 format….’ line (put a ‘c’ in front of the 20) and replace the
20 reference in the write line above with ‘frmt2’:
write (*,frmt2) name,h
Save, recompile, and run to make sure it works.
17. Before we leave the formatting nitty-gritty, one last thing to see. Switch the number
format for h to ES11.5 instead of ES12.5. Save, recompile, and run. You should see that we
lose that desired space in the output because it had been due just to 6.62608E-34 having only 11
characters instead of 12.
if (endn.gt.100) then
print*,’Your ending number is just too big…please try again.’
read(*,*) endn <this is a second ‘read’ line just like the one you already have>
else
print*,’That number should work just fine.’
end if
The ‘if’ statement is a conditional statement based on true/false logic. The ‘else’ that follows
should be self-explanatory. The ‘end if’ statement ends the whole ‘if’ deal. Compile and run.
22. You may realize that this still doesn’t work, because even if you initially put in too big a
number, on the second try you can do the same thing and it then accepts it. Verify that this is so.
Something more is needed to make sure you don’t get too big a number (if you really care about
that, which we do now only for teaching purposes).
Add in a ‘go to’ statement now after the second ‘read’ statement in the ‘if’ loop:
if (endn.gt.100) go to 20
Also, you need to put a ‘20’ in the 2nd and 3rd columns in front of the print statement 2 lines
above, so if the second input number is still too high, then the same statement gets printed again
telling you to input a new number. The ‘20’ labels the line so that the ‘go to’ statement tells the
program where to go. You may at this point wish to tell the program to go somewhere, also (ha).
Also, there is the possibility of an ‘else if’ line in an ‘if’ block of code. This allows for multiple
possibilities rather than just the two given by ‘if’ and ‘else’.
23. The last major piece to programming is the ‘subroutine’. This is just a little self-
contained program that services the main program. Different parts of the main program can
make ‘calls’ to the subroutine. The main program will pass variables to the subroutine, and the
subroutine returns those variables after having done a calculation or something with them. For
instance, in the Monte Carlo program there is a subroutine to calculate potential energy. That
routine could be incorporated in the main body of the program, but it is a bit lengthy and because
there are numerous places in the main program that require a calculation of the energy, than it
makes sense to only write it once and stick it in a subroutine.
We will write a short subroutine that modifies the ‘name’ that you input. Type the
following at the very end of your code (even after the ‘end program’ line):