Вы находитесь на странице: 1из 12

Fortran

Installation, Compiling, Coding

Fortran77

Several homework assignments are going to be computational,


where students will be provided a Fortran77 program to compile,
modify, and play with

Why Fortran77?

very easy to learn

suited for scientific computations

compilers freely available for PC/Mac

only language I know

How to install Fortran77 compilers in PC and Mac


How to compile and run Fortran77 programs
How to code in Fortran77

Fortran Installation for Mac OS X


Step 1: Xcode installation
Command line program development often uses Unix utilities when
developing and compiling a project. These utilities are available as part of
the Xcode Tools that come with OS X. However, they are not installed by
default. To install Xcode use the following instructions:

Insert Mac OS X Installation DVD. Navigate to the Optional Installs: Xcode Tools
folder.

Double-click XcodeTools.mpkg. Follow the prompts, but at Installation Type, click


the Customize button.

Uncheck all the components and check only Unix Development Support. In
particular, Developer Tools Essentials should be unchecked (saving you 2.5GB of
disk space)!

Provide your password for the installation to proceed.


The latest version of Xcode Tools can be obtained by registering for a free ADC
membership and then downloading the latest version of Xcode from Apple
(caution: it is > 1GB!).

Fortran Installation for Mac OSX


Step 2: g77 Compiler installation

download g77-intel-bin.tar.gz (Intel Mac only) from

OR download g77-bin.tar.gz (PowerPC only) from

http://prdownloads.sourceforge.net/hpc/g77-intel-bin.tar.gz?download

http://prdownloads.sourceforge.net/hpc/g77-bin.tar.gz?download

If your browser did not automatically unzip the file, then


> gunzip g77-bin-intel.tar.gz

Install the distribution


> sudo tar -xvf g77-bin.tar -C /
which puts everything in /usr/local

G77 documentation is available at http://gcc.gnu.org/onlinedocs/ and


another useful site is: http://hpc.sourceforge.net/

Note that the above procedures refer to installation of fortran77 binaries;


you dont want to install fortran77 from source code (only for experts).

Fortran Installation for the new Mavericks


Mac OS

The fortran77 compiler g77 does not apparently work well for the
Mavericks OS on Macs.

So, you should download the gfortan compiler instead

http://prdownloads.sourceforge.net/hpc/gfortran-4.8-bin.tar.gz?download

If your browser did not automatically unzip the file, then


> gunzip gfortran-4.8-bin.tar.gz

Install the distribution


> sudo tar -xvf gfortran-4.8-bin.tar -C /
which puts everything in /usr/local

Fortran Installation for Windows


FORCE compiler

Advantages of FORCE

Not only a compiler, but also an editor


Compatible with Windows XP, 7, Vista
Compiles 77, 90, 95, etc

Install Force209GFortranSetup.exe
http://force.lepsch.com/2009/05/
downloads.html

Begin installation. Use Full installation, with


extra examples/documents.

Make sure to associate ALL .f like files to


FORCE

Compiling Fortran programs using the g77


compiler on Mac OS X

Open a Terminal it should be in your /Applications/Utilities/ folder or directory


Go to the program directory using the cd command, e.g., if your programs are
located in /Users/garya/NANO110/ directory (folder), then type cd /Users/
garya/NANO110 at the command prompt

Compile the program using the g77 command if you fortran77 compiler and the
gfortan command if you have gfortran compiler, i.e., if the program is named
nano.f and it uses a subroutine named sub.f, then type g77 nano.f sub.f at
the command prompt, which should produce an executable called a.out within the
same directory

Execute the compiled program by typing ./a.out at the command prompt,


which should start to produce the output

To visualize the program, you can use any text editor. I personally prefer to use
the text editor that comes with Xcode, which colors the text according to the
fortran program commands

Compiling Fortran programs using the


FORCE compiler on PCs

Double click on the FORCE icon on your


terminal and it should open the editor on
the right

Click on the File menu and use the


open to upload your program into the
FORCE editor

To compile and execute the program,


you just need to press the green play
button

This should open a command box that


displays the screen output of your
program. If the program is designed to
run based on user-provided parameters,
you can enter those in the dialog box

Fortran code in the Xcode editor


C
C

THIS SUBROUTINE CALCULATES THE TOTAL !


PAIR-WISE POTENTIAL !

"
"
"

"SUBROUTINE TOTALENERGY(RX,RY,RZ,EPOT)!
"IMPLICIT NONE!
C

RXIJ=RXI-RX(J)!
RYIJ=RYI-RY(J)!
RZIJ=RZI-RZ(J)!

MINIMUM IMAGE CONVENTION!

"INCLUDE 'param.h'!
"REAL*8

RX(NATOM), RY(NATOM), RZ(NATOM)!

"REAL*8

EPOT, ELRC!

"REAL*8
"REAL*8
"REAL*8
"REAL*8
"INTEGER

R6I,R12I, PI!
RXIJ,RYIJ,RZIJ,RIJSQ!
RXI, RYI, RZI!
LSIMBOX, LJCUTSQ!
I,J!

"PI = 3.14159265d0
C

"

RXIJ=RXIJ-LSIMBOX*ANINT(RXIJ/LSIMBOX)

"
"

RYIJ=RYIJ-LSIMBOX*ANINT(RYIJ/LSIMBOX)!
RZIJ=RZIJ-LSIMBOX*ANINT(RZIJ/LSIMBOX)!

"

RIJSQ=RXIJ**2+RYIJ**2+RZIJ**2!

"

IF (RIJSQ.LE.LJCUTSQ) THEN!

"
"
"
"

"! Pi!

R6I= SIGMA**6/(RIJSQ*RIJSQ*RIJSQ) !
R12I= R6I*R6I!
"!
EPOT=EPOT+(-R6I+R12I)!

"SIMULATION BOX LENGTH!


"

ENDIF!

"LSIMBOX=(NATOM/DENS)**(1.0D0/3.0D0)!
"LJCUTSQ=LJCUT*LJCUT!

"

"FORCES CALCULATED FROM LENNARD-JONES POTENTIAL!

"ENDDO!

"EPOT=0.0!

"EPOT=4.0D0*EPSILON*EPOT!

"DO I=1, NATOM-1!


"
"
"

RXI=RX(I)!
RYI=RY(I)!
RZI=RZ(I)!

"

DO J=I+1, NATOM!

ENDDO!

"CALCULATE THE LONG RANGE CORRECTIONS APRIORI!


"ELRC = (1.0/3.0)*(SIGMA/LJCUT)**9 - (SIGMA/LJCUT)**3!
"ELRC = ELRC*(8.0/3.0)*NATOM*PI*EPSILON*DENS*(SIGMA**3)!
"EPOT = EPOT + ELRC!

"RETURN!
"END

Just Enough Fortran to Understand and


Modify an Existing Code

An excellent website for quickly learning basic Fortran77 that will allow you to
understand the provided codes and even modify them slightly

An excellent set of notes on learning Fortran77 that I found online

http://maeresearch.ucsd.edu/arya/FortranGuide.pdf

Fortran77 tutorial

http://www.idris.fr/data/cours/lang/fortran/f90/F77.html

http://www.geo.utexas.edu/courses/387h/Lectures/fortran
%2077%20tutorial.pdf

A quick reference guide on Fortran77

http://www.obliquity.com/computer/fortran/

Example: Calculate the potential energy of


an N-particle cluster of Argon atoms

Lets say we want to calculate the vdW energy of a 3-particle cluster of Argon
atoms such that the three atoms are arranged as an equilateral triangle of side
length 3.5 Angstroms.

The vdW energy of interaction between 2 Argon atoms is given by the LennardJones potential ULJ = 4 [(/r)12 (/r)6], with = 3.41 and = 1.6510-21 J.

Here is what the program should look like:


C
C
C

"Example program for NANO110 that calculates the potential energy!


"of a three-atom cluster composed of Argon atoms arranged in an!
"equilateral triangle configuration of side 3.5 Angstroms !
PROGRAM Energy!
IMPLICIT NONE!

"Define all
"REAL*8
"REAL*8
"REAL*8
"INTEGER

variables!
"
"
"
"I, J

"RX(3), RY(3), RZ(3)!


"SIDE, POTE, SIGMA, EPSILON, PI!
"RXIJ, RYIJ, RZIJ, RIJSQ, R6I, R12I !
"
"
"!

"Specify pi, triangle side length, sigma, and epsilon !


"PI = 3.14159!
"SIDE = 3.5!
"SIGMA = 3.405!
"EPSILON = 1.65E-21!

Example continued
C

"Specify
"RX(1) =
"RY(1) =
"RZ(1) =

the provided coordinates of first Argon atom!


0.0!
0.0!
0.0!

"Specify
"RX(2) =
"RY(2) =
"RZ(2) =

the provided coordinates of second Argon atom!


SIDE!
0.0!
0.0!

"Specify
"RX(3) =
"RY(3) =
"RZ(3) =

the provided coordinates of third Argon atom!


SIDE*COS(PI/3)!
SIDE*COS(PI/6)!
0.0!

"Calculate total potential energy


"!
"POTE = 0.0!
"DO I = 1, 2!
"
"DO J = I+1, 3!
"
"
"RXIJ = RX(I) - RX(J)!
"
"
"RYIJ = RY(I) - RY(J)!
"
"
"RZIJ = RZ(I) - RZ(J)!
"

"

"RIJSQ = RXIJ**2 + RYIJ**2 + RZIJ**2!

"
"
"R6I = SIGMA**6/(RIJSQ*RIJSQ*RIJSQ) !
"
"
"R12I = R6I*R6I!
"
"!
"
"
"POTE = POTE + (R12I - R6I)
"!
"
"ENDDO!
"ENDDO!
"POTE = 4.0*EPSILON*POTE!
C

"Output the total energy!


"WRITE(*,*) 'Total Energy: ', POTE, ' Joules'!
"END

Вам также может понравиться