UNIX: Lesson #1

Assignment #1

text editor (vi): Lesson #2

Assignment #2

Fortran: Lesson #3

Assignment #3: Fortran program to plot energy and force for harmonic oscillator potential

Assignment #4: Fortran program to do molecular dynamics for 2 particles interacting with a quadratic potential; plot time evolution of potential, kinetic, and total energies, as well as the distance of separation for the 2 particles

Assignment #5: Fortran program to calculate interaction energy for N particles interacting with pairwise Lennard-Jones potentials

Biosym: (available upon request)

Spartan: (available upon request)

COMPUTER LESSON #1: UNIX

UNIX COMMANDS

Creating, deleting, and moving among directories:
mkdir dirname: make directory
rmdir dirname: remove directory (must be empty)
cd dirname: move to directory dirname, which is located in the current working directory
cd ~afsid/dirname: move to a general directory dirname
cd: move to your home directory
cd ..: move back to previous directory
pwd: print out current working directory

Copying, moving, and deleting files:
rm filename: remove file called filename
cp: copy (old copy is saved)
mv: move (old copy not saved)

The following gives examples for copying one file to another. The same form is used for moving one file to another location.

General way to copy one file to another (with full path given):
cp ~afsid1/dirname1/filename1 ~afsid2/dirname2/filename2

Shortcuts:

If copying into current directory:
cp ~afsid1/dirname1/filename1 filename2
If filename1 and filename2 are identical:
cp ~afsid1/dirname1/filename1 .

If copying from current directory:
cp filename1 ~afsid2/dirname2/filename2
If filename1 and filename2 are identical:
cp filename1 ~afsid2/dirname2/.

If copying to a directory dirname located in the current working directory:
cp filename dirname

Directing screen output from command to file:
command > filename: if filename does not exist yet
command >! filename: overwrite filename, which already exists
command >> filename: append to filename, which already exists

Printing out files:
To screen:
cat filename: prints out all at once
more filename: prints out one screen at a time; use space bar to move through it

To printer:
lpr filename
lp filename

COMPUTER ASSIGNMENT #1: UNIX

1. Copy this file test.dat below into your home directory


beginning of test.dat:
Computer simulation is important for
all scientists. Everyone should
learn basic computer skills, including
UNIX, a text editor such as vi, and a
programming language such as FORTRAN.
The computer is your friend and will
help you throughout your career.
end of test.dat

2. Make a directory chem647 in your home directory
3. Move (not copy) the file test.dat into your directory chem647
4. Direct the output of the following commands to a file assign1.dat, making sure that
all the information remains in this file

5. Print out assign1.dat and turn it in
COMPUTER LESSON #2: VI

VI COMMANDS

Commands that begin with a :, /, or ? must be followed by a carriage return.

Move cursor:
h: move cursor left
j: move cursor down
k: move cursor up
l: move cursor right
G: go to bottom of file
:# : go to line #
^G: tells you the line number of the current cursor line

Insert text:
i: insert before cursor (end with ESC)
a: append after cursor (end with ESC)
A: append at end of cursor line (end with ESC)
I: insert before first non-blank of cursor line (end with ESC)

Delete text:
x: delete character at current cursor position
dd: delete current line
#dd: delete # lines (starting with cursor line)
D: delete from cursor to end of line

Move text around:
yy: yank current cursor line (without deleting text)
#yy: yank # lines starting with cursor line (without deleting text)
p: put last deleted or yanked text after current cursor position

Search for a string:
/string: move cursor to next occurrence of string (in forward direction)
?string: move cursor to next occurrence of string (in backward direction)
n: repeat last / or ?

Substitute word2 for word1:
:1,$s/word1/word2/g: substitute for entire file
:s/word1/word2/: substitute for first appearance on current cursor line

Save and quit current file:
:w filename: write to filename (if same name, can omit filename)
:q :quit file
:q! :quit file without saving changes since last saved
:wq or ZZ: write file and quit

Insert another file into current file:
:r filename: insert filename into current file

Undo:
u: undo previous command

COMPUTER ASSIGNMENT #2: VI

Copy the file test2.dat from below:

beginning of test2.dat
I could not, would not, on a boat.
I will not, will not, with a goat.
I will not eat them in the rain.
I will not eat them on a train.
Not in the dark! Not in a tree!
Not in a car! You let me be!
I do not like them in a box.
I do not like them with a fox.
I will not eat them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them ANYWHERE!
I do not like green eggs and ham!
I do not like them, Sam-I-am.
end of test2.dat

Use vi to edit the file as follows (in the order given):

Print out the file assign2.dat and turn in.

COMPUTER LESSON #3: FORTRAN

FORTRAN COMMANDS


General form of a FORTRAN statement:
characters 1-5 statement number -- only necessary if you refer to this line in the program
character 6 continuation character -- blank unless previous line continues on current line
characters7-72 FORTRAN statement

Beginning and end of program:
First line:
program name

Last line:
end

Declare variable types:
real*8
integer

Arithmetic operations:
addition: a+b
subtraction: a-b
multiplication: a*b
division: a/b
exponentiation: a**b

Priority of operations:
1. parentheses
2. exponentiation
3. multiplication and division
4. addition and subtraction
If 2 operations have the same priority, all operations except exponentiation are performed
left to right. Two sequential exponentiations are performed right to left.

Relations:
is equal to: .eq.
is not equal to: .ne.
is greater than: .gt.
is less than: .lt.
is greater than or equal to: .ge.
is less than or equal to: .le.

Input and output:
read(5,*) x,y,z
(5 indicates default input, * indicates default format for types x,y,z)

write(6,100) x,y,z
(6 indicates default output, 100 indicates line of format statement)

run program prog.x with input file in.dat and output file out.dat:
prog.x < in.dat > out.dat

Format:
Integers:
iw: w represents total width (number of positions)

Real numbers in decimal form:
fw.d: w represents total width (number of positions) including decimal point
d represents number of these positions to the right of the decimal point
f6.3: XX.XXX

Space:
x

Numbers are right-adjusted within the specified width w.
A preceding integer indicates the quantity of numbers of that type.

100 format(3f6.3) 3 real numbers of width 6 on one line
100 format(3i5) 3 integers of width 5 on one line
100 format(f6.3,2x,f6.3,2x,f6.3) 3 real numbers of width 6, each separated by 2 spaces

Loops:
do index=initial,limit
set of statements
enddo

do while condition
set of statements
enddo

if condition then
set of statements
endif

if condition then
set of statements
else
set of statements
endif

Arrays: (1-dimensional)
group of storage locations (row of data); each element is of the type declared at the beginning

Declaration:
real*8 ar(20)
integer ar(20)

Assignment of elements:
ar(1)=2.0
x=ar(2)

Input and output of elements:
read(5,*) ar(10)
write(6,100) ar(1),ar(2),ar(3)

Subroutines:


subroutine name(argument list)
set of statements
return
end

Call subroutine from main program:
call name(argument list)

Arguments in the call statement must match those in the subroutine.

Compile: (fortran code in prog.f, executable in prog.x)
f77 -o prog.x prog.f

Run: (input and output files in.dat and out.dat, respectively)
prog.x < in.dat > out.dat

Plot:
To get into program type:
xvgr (return)

To read in file and plot:
Go to Files pulldown and choose Read Sets
Choose your file from Contents, which should result in the following:
File: output.dat (the file you chose from Contents)
Change File type to be as follows:
File type: X Y1 Y2 ...
Use the defaults for the other options
Click on Accept and then on Done

Then go to View pulldown and choose Autoscale
Change Autoscale axis to be as follows:
Autoscale axis: All
Click on Accept and then on Done

To plot another file on the same axes, just repeat the above process to read in file and plot.

To print:
Go to Files pulldown and choose Print

To quit:
Go to Files pulldown and choose Exit

EXAMPLE PROGRAMS:
c These programs read in an initial value for r and the step size
c and prints out r and r**2 for 101 points

Sample input file:
-0.5
0.01
c Example of use of do loop, reading input, and writing output.
program testp1
real*8 r,step
integer i

read(5,*) r
read(5,*) step
do i=1,101
write(6,100) r,r*r
r=r+step
enddo
100 format(f9.5,x,f9.5)
end
*******************************************
c Example of use of 1-dimensional arrays.
program testp2
real*8 r,step,ar(200)
integer i

read(5,*) r
read(5,*) step
do i=1,101
ar(i)=r
r=r+step
enddo
do i=1,101
write(6,100) ar(i),ar(i)*ar(i)
enddo
100 format(f9.5,x,f9.5)
end
*******************************************
c Example of use of subroutines.
program testp3
real*8 r,step,ar(200)
integer i

read(5,*) r
read(5,*) step
call doloop(r,step,ar)
do i=1,101
write(6,100) ar(i),ar(i)*ar(i)
enddo
100 format(f9.5,x,f9.5)
end

subroutine doloop(r,step,ar)
real*8 r,step,ar(200)
integer i
do i=1,101
ar(i)=r
r=r+step
enddo
return
end

COMPUTER ASSIGNMENT #3: FORTRAN

Write a program that calculates the energy and force for a harmonic oscillator potential
V(r)=0.5*k(r-ro)**2
for 101 points evenly spaced (with spacing 2d/100) with first point r=ro-d and last point r=ro+d.

The input file should be as follows:
ro
k
d
The output file should have 3 columns: r, energy, force.

Hints:
The variables are declared as follows:
real*8 k,r,ro,d,energy,force
integer i

The statements in the program that read the input are:
read(5,*) ro
read(5,*) k
read(5,*) d
The statement in the program that prints each line of the output is:
write(6,100) r,energy,force
The format statement is:
100 format(f9.5,x,f9.5,x,f9.5)

The energy and force are calculated as follows:
energy=0.5*k(r-ro)**2
force=-k*(r-ro)
The loop over points should be initialized as follows:
r=ro-d
step=2.0*d/100.0

The loop itself should be as follows:
do i=1,101
calculate energy
calculate force
write output
r=r+step
enddo
Note:
The force is greater the further away from ro (i.e. larger |r-ro|).
The force is greater the larger the curvature (i.e. larger k).
The force pulls the coordinate back to ro (i.e. positive when r<ro and negative when r>ro).
The energy is always positive.

Turn in:
1. Copy of program
2.Copy of output file using input files input1a.dat and input1b.dat from below

input1a.dat
1.4
17.0
0.80

input1b.dat
1.4
34.0
0.80

3. Plot of energy and force as function of r for results from #2 (all on same graph) -- label curves!!
4. Answer question: What is the difference between these input files? How is this reflected in the curves?

COMPUTER ASSIGNMENT #4: FORTRAN


Copy the program md.f below and append it to your program.

beginning of md.f
c This subroutine is part 1 of the velocity Verlet algorithm, where
c dt is the time step. The classical coordinates are moved from time
c t to time t+dt, and the classical velocities are moved from time
c t to time t+dt/2.
subroutine movea(dt,r1,r2,v1,v2,m1,m2,f1,f2)

real*8 dt,r1,r2,v1,v2,m1,m2,f1,f2
real*8 dt2,dtsq2

dt2=dt/2.0
dtsq2=dt*dt2
r1=r1+dt*v1+dtsq2*f1/m1
v1=v1+dt2*f1/m1
r2=r2+dt*v2+dtsq2*f2/m2
v2=v2+dt2*f2/m2

return
end

c This subroutine is part 2 of the velocity Verlet algorithm, where
c dt is the time step. The velocities are moved from time t+dt/2
c to time t+dt.
subroutine moveb(dt,r1,r2,v1,v2,m1,m2,f1,f2)

real*8 dt,r1,r2,v1,v2,m1,m2,f1,f2
real*8 dt2

dt2=dt/2.0
v1=v1+dt2*f1/m1
v2=v2+dt2*f2/m2

return
end
end of md.f

The program md.f contains two subroutines for the velocity Verlet algorithm.
Write a program that does molecular dynamics for two particles (in 1-dimension) interacting with a harmonic oscillator potential (as in assignment #3).

Input file should be as follows:
r1 r2 (positions of 2 particles)
v1 v2 (velocities of 2 particles)
m1 m2 (masses of 2 particles)
ro
k
dt (time step)
nstep (number of MD steps)
Output file should contain 5 columns: time, distance between the two particles, potential energy, kinetic energy, total energy.

Hints:
The variables are declared as follows:
real*8 r1,r2,v1,v2,m1,m2,f1,f2,dt
real*8 k,ro,totenergy,kinenergy,potenergy
integer i,nstep

The statements in the program that read the input are:
read(5,*) r1,r2
read(5,*) v1,v2
read(5,*) m1,m2
read(5,*) ro
read(5,*) k
read(5,*) dt
read(5,*) nstep

The statement that writes out each line of the output is:
write(6,100) dt*i,r,potenergy,kinenergy,totenergy

The format statement is:
100 format(f9.5,x,f9.5,x,f9.5,x,f9.5,x,f9.5)

The force on each particle is calculated by:
r=dabs(r2-r1)
f1=-k*(r-ro)*(r1-r2)/r
f2=-k*(r-ro)*(r2-r1)/r
The kinetic energy is:
kinenergy=0.5*m1*v1*v1+0.5*m2*v2*v2
The potential energy is:
potenergy=0.5*k*(r-ro)**2
The total energy is:
totenergy=kinenergy+potenergy

The loop for the MD is as follows:
calculate force
do i=1,nstep
call movea(dt,r1,r2,v1,v2,m1,m2,f1,f2)
calculate force
call moveb(dt,r1,r2,v1,v2,m1,m2,f1,f2)
calculate kinetic energy
calculate potential energy
calculate total energy
write output
enddo

Turn in:
1. Copy of program
2. Copy of output file using input files input2a.dat, input2b.dat, and input2c.dat below
input2a.dat
-0.6 0.6
0.0 0.0
2.0 2.0
1.0
15.0
0.1
100

input2b.dat
-0.6 0.6
0.0 0.0
2.0 2.0
1.0
15.0
0.01
1000

input2c.dat
-0.6 0.6
0.0 0.0
2.0 2.0
1.0
30.0
0.01
1000

3. Plots of distance, kinetic energy, potential energy, and total energy as function of time for results from #2 (3 separate graphs (with 4 curves each) -- one for input2a.dat, one for input2b.dat, and one for input2c.dat) -- label curves and graphs!!!
4. Answer questions:
(a) When the distance r is at a minimum or maximum, is the kinetic energy at a minimum or maximum? How about the potential energy?
(b) When the distance r is at its equilibrium value (r=1.0 in this case), is the kinetic energy at a minimum or maximum? How about the potential energy?
(c) Does data set from input2a.dat or input2b.dat conserve total energy better? Why is this so?
(d) Do the potential and kinetic energies vary more for data set from input2b.dat or input2c.dat? Based on the graphs from Assignment #3, why is this so?

COMPUTER ASSIGNMENT #5: FORTRAN


Write a program that reads in the number of particles N and the 3-dimensional coordinates
for each particle and calculates the potential energy of interaction using a Lennard- Jones potential (with parameters epsilon and sigma from input file).
Input file:
N
x(1) y(1) z(1)
....
x(N) y(N) z(N)
epsilon
sigma
Outpute file: total potential energy

Hints:
Declare variables as follows:

integer N,i,j
real*8 x(100),y(100),z(100),epsilon,sigma
real*8 temp,v,r2,r6,sigma6,sigma12
The statements to read the input file are:
read(5,*) N
do i=1,N
read(5,*) x(i),y(i),z(i)
enddo
read(5,*) epsilon
read(5,*) sigma
The LJ potential is:
r2=(x(i)-x(j))**2+(y(i)-y(j))**2+(z(i)-z(j))**2
r6=r2*r2*r2
sigma6=sigma**6
temp=4.0*epsilon*(-sigma6/r6 + sigma12/(r6*r6))
The potential loop is initialized as follows:
sigma6=sigma**6
sigma12=sigma6*sigma6
v=0.0
The potential loop is as follows:
do i=1,N
do j=1,i
if(i.ne.j) then
set temp equal to LJ potential for particle i interactingwith particle j
v=v+temp
endif
enddo
enddo
write output
The output is as follows:
write(6,100) v
The format statement is
100 format(f12.5)

Turn in:
1. Copy of your program with responses to #2 and #3 written on the top of the first page.
2. The result (just a number) of using input file input3.dat below

input3.dat
8
2.1 -1.8 -1.5
-1.7 1.6 -2.1
-1.6 -1.9 2.0
2.0 2.1 -1.6
-1.9 1.7 1.5
1.7 -1.5 2.0
2.1 2.0 1.9
-1.6 -2.0 -1.7
0.24
3.4

3. Answer question: How many times do you have to calculate the LJ potential for N particles? Typically this loop over nonbond interactions (in which the force is usually also calculated) is the most computationally expensive in molecular dynamics simulations.