Computer Exercise 2 - Fysik
Computer Exercise 2 - Fysik
Computer Exercise 2 - Fysik
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Computational Materials Physics, spring 2013<br />
<strong>Computer</strong> <strong>Exercise</strong> 2<br />
Paul Erhart 1 , Anders Hellman 2 , Martin Petisme 3<br />
February 19, 2013<br />
Introduction to the GPAW code<br />
The main objective of the second computer exercise is to familiarize yourself with the Grid-based Projector<br />
Augmented Wave (GPAW) 4 code that we will use throughout this course. The GPAW is a density-functional<br />
theory (DFT) Python code based on the projector-augmented wave (PAW) method.<br />
GPAW allows the use of different basis sets, namely, linear combination of numerical atomic orbitals<br />
(LCAO) 5 , plane-wave basis 6 https://wiki.fysik.dtu.dk/gpaw/devel/planewaves.html, and a finite-difference<br />
basis. We will focus on the first two basis sets.<br />
Setting up the Cluster environment<br />
Start up a terminal widow and log on to Chalmers cluster computer, Beda 7 . Just write<br />
ssh -X username@beda.c3se.chalmers.se<br />
note that you must log in from a computer connected to the local network at Chalmers. (External connections<br />
are dropped.)<br />
You will now be in your home directory. First you should make a directory dedicated to your calculations<br />
during the course. It is a good advice to structure your calculations, by for instance, creating sub-directories<br />
so that you can maintain order. Throughout the course you will conduct a number of calculations.<br />
To create a directory, you just write<br />
mkdir nameofdirectory<br />
To change to that particular directory write<br />
cd nameofdirectory<br />
You are now standing in the directory $HOME/nameofdirectory.<br />
Instead of using the login node, we will submit the jobs that we want to have execute on to the queue of<br />
beda. This is most easily done using a submit file that might look something like this:<br />
#!/bin/bash<br />
#PBS -q beda<br />
#PBS -A C3SE001 -10-7<br />
#PBS -W x=FLAGS:ADVRES:TIF035.2<br />
#PBS -N Parallel<br />
#PBS -l nodes=1:ppn=8<br />
#PBS -l walltime =00:10:00<br />
1 erhart@chalmers.se<br />
2 ahell@chalmers.se<br />
3 martin.petisme@chalmers.se<br />
4 https://wiki.fysik.dtu.dk/gpaw/ GPAW is distributed under the GNU General Public License.<br />
5 https://wiki.fysik.dtu.dk/gpaw/documentation/lcao/lcao.html<br />
6 https://wiki.fysik.dtu.dk/gpaw/devel/planewaves.html<br />
7 http://www.c3se.chalmers.se/index.php/Beda
cd $PBS_O_WORKDIR<br />
module load COURSES/TIF035<br />
export GPAW_SETUP_PATH=$PBS_O_WORKDIR:$GPAW_SETUP_PATH<br />
python ./yourscript.py<br />
Send the submit file on to the queue by writing<br />
qsub whateverthenameofthesubmitfile<br />
Setting up the simulation environment<br />
First you should load some necessary modules. Type in your terminal<br />
module load COURSES/TIF035<br />
Now you are ready to start up ase. However, in this exercise much of you calculations will be conducted<br />
on the nodes of beda. Hence, you should prepare python-scripts that contain all the information of the<br />
first-principles calculation you want to do. Use decent editor to write your python scripts, eg. nano, vim,<br />
Emacs or for a more user-friendly interface use gedit.<br />
Now it will by much more convenient to write executable script that you can submit through the<br />
submit-file. Let us use the methane example that you used in exercise 1.<br />
#!/usr/bin/env python<br />
from ase import *<br />
from ase.optimize import BFGS<br />
from ase.structure import molecule<br />
import numpy as np<br />
from gpaw import GPAW<br />
mol = molecule('CH4')<br />
mol.center(vacuum=5)<br />
calc=GPAW(mode='lcao', basis='dzp', h=0.2, xc='PBE', nbands=8, txt='out_task1.txt')<br />
mol.set_calculator(calc)<br />
dyn = BFGS(mol , trajectory='mol_task1.traj',logfile='bfgs_task1.log')<br />
dyn.run(fmax =0.001)<br />
The difference is the type of calculator used. Now you are calling for GPAW. As the DFT calculator is<br />
much more complex than the normal potential codes a number of parameters need to be set before one can<br />
use the GPAW calculator 8 .<br />
In this case the parameters to the calculator contains the ’mode’, which informs the calculator to use<br />
linear combination of atomic orbitals (lcao). The ’basis’ tells the calculator to use a double-z function in<br />
combination with a p-polarization function. This is normally a sufficient basis for the calculations. You<br />
also provide the flavor of the exchange-correlation functional, i.e, ’xc=PBE’ and the ’h’, which relates how<br />
accurate the representation of charge density and wavefunctions should be. The arguments ’nbands’ and ’txt’<br />
sets the number of KS-orbitals in the simulation and the name of the outputfile, respectively.<br />
Note that the ’nbands’ needs to be at least as large as the number of electrons in the atomic system.<br />
Investigate your results by, for instance, using the gui tool included in ase. Open another terminal (you<br />
need to load the software modules as above) and change to the same directory, than enter the following:<br />
!ag mol_task1.traj<br />
8 https://wiki.fysik.dtu.dk/gpaw/documentation/documentation.html<br />
2
Task 1<br />
Measure the C–H distance and the H–C–H angle:<br />
• C–H distance=<br />
• H–C–H angle=<br />
Angstrom<br />
◦<br />
How well does the calculator (with a specific basis) do compared to experiment?<br />
• C–H distance = 1.087 Angstrom<br />
• H–C–H angle = 109.5 ◦<br />
Now let us test the quality of the different basis sets. To do this we will need to generate the basis that<br />
we should test. GPAW provides a simple tool that allow us to do this. For instance, we are interested in<br />
generating new basis for hydrogen and carbon only. Type in your terminal<br />
gpaw -basis -t spz H C<br />
This will generate a spz bas for each elements. The description of the bas will end up in the library in which<br />
you created them. This is the reason for the extra line in your submitionfile ’export xxx’, because now GPAW<br />
to know were to look for the new basis.<br />
It is important to note that the generation of different basis is a research field in itself, hence, there<br />
are a number of difficulties that we are skipping over in this course.<br />
Write a script that tests how well methane properties such as bond distance and angles are calculated<br />
using the single ζ basis, double ζ basis, triple ζ basis and the same basis sets with a polarization function.<br />
#!/usr/bin/env python<br />
from ase import *<br />
from ase.optimize import BFGS<br />
from ase.structure import molecule<br />
from ase.parallel import rank<br />
import numpy as np<br />
from gpaw import GPAW<br />
mol = molecule('CH4')<br />
mol.center(vacuum=5)<br />
basisset=['sz','dz','tz','szp','dzp','tzp']<br />
for bas in basisset:<br />
calc=GPAW(mode='lcao', basis=bas , h=0.2, xc='PBE', nbands=8, txt='out.txt')<br />
mol.set_calculator(calc)<br />
dyn = BFGS(mol , trajectory='mol.'+str(bas)+'.traj',logfile='bfgs.log')<br />
dyn.run(fmax=0.001)<br />
# Measure O-H distance and H-O-H angle:<br />
pC = mol.get_positions()[0]<br />
pH1 = mol.get_positions()[1]<br />
pH2 = mol.get_positions()[2]<br />
angle = np.arccos(np.dot(pH1 - pC, pH2 -pC) /<br />
(np.linalg.norm(pH1 -pC)*np.linalg.norm(pH2 -pC)))*180.0/np.pi<br />
if rank == 0:<br />
print<br />
print 'Current basis set:', bas<br />
print '--------------------'<br />
print 'C-H Distance: ', np.linalg.norm(pH1 - pC), 'Angstrom'<br />
print 'C-H Distance: ', np.linalg.norm(pH2 - pC), 'Angstrom'<br />
print 'H-C-H Angle: ', angle , 'Degrees'<br />
3
If you now look inside the output file (’std.out’) it will contain structural information about the water<br />
molecule and their dependence on the basis functions that you have used in the calculation.<br />
It could also be good to have an estimate on how much CPU time each basis set contributes with. For<br />
this kind of test, the water molecule is to small a system. Instead we recall our favorite buckyball, namely<br />
the C60.<br />
#!/usr/bin/env python<br />
from ase import *<br />
from ase.optimize import BFGS<br />
from ase.structure import molecule<br />
from ase.data.extra_molecules import data<br />
from ase.parallel import rank<br />
import numpy as np<br />
from gpaw import GPAW<br />
import time<br />
basisset=['sz','dz','tz','szp','dzp','tzp']<br />
for bas in basisset:<br />
atoms = molecule('C60',data=data)<br />
atoms.center(vacuum=5)<br />
starttime = time.time()<br />
calc=GPAW(mode='lcao', basis=bas , h=0.2, xc='PBE', nbands=150, txt='out.txt')<br />
atoms.set_calculator(calc)<br />
atoms.get_potential_energy()<br />
endtime = time.time()<br />
walltime = endtime - starttime<br />
if rank == 0:<br />
print 'Current basis set:', bas<br />
print '--------------------'<br />
print 'Wall time:', walltime<br />
The Molecular Vibration Module<br />
As a further test of the accuracy of GPAW calculator, let us return to the vibrations of methane. Write a<br />
script using your favorite basis-set and than calculate the vibrational spectrum of methane.<br />
#!/usr/bin/env python<br />
from ase import *<br />
from ase.optimize import BFGS<br />
from ase.structure import molecule<br />
from ase.parallel import rank<br />
import numpy as np<br />
from gpaw import GPAW<br />
from ase.vibrations import Vibrations<br />
mol = molecule('CH4')<br />
mol.center(vacuum=5)<br />
calc=GPAW(mode='lcao', basis='dzp', h=0.2, xc='PBE', nbands=8, txt='out_task5.txt')<br />
mol.set_calculator(calc)<br />
dyn = BFGS(mol , trajectory='mol_task5.traj',logfile='bfgs_task5.log')<br />
dyn.run(fmax =0.001)<br />
vib = Vibrations(mol)<br />
vib.run()<br />
if rank == 0:<br />
vib.summary()<br />
for n in range(0,len(mol)*3):<br />
vib.write_mode(n)<br />
4
The last line writes out the normal mode into a trajectory file that you can view using the tool ag, by<br />
writing<br />
!ag vib.14.traj<br />
Now you can see and determine which modes are antisymmetric, symmetric, and bending modes by going<br />
through the different trajectory-files (change the number).<br />
Task 2<br />
How is the comparison with experiment? Check on, for instance, the NIST chemistry webbook.<br />
• Degenerate stretch = 3019 cm −1<br />
• Symmetric stretch = 2917 cm −1<br />
• Degenerate deformation mode = 1534 cm −1<br />
• Degenerate deformation mode = 1306 cm −1<br />
Try to use your experience of the quality of the basis set to improve the comparison between calculations<br />
of the vibrational spectra and measurements. What basis set did you use and how did the new vibrational<br />
modes compare to experiment (look in output.txt)?<br />
• Basis set =<br />
• Degenerate stretch =<br />
• Symmetric stretch =<br />
• Degenerate deformation mode =<br />
• Degenerate deformation mode =<br />
Molecular structure of hydrocarbons<br />
Let us now repeat the same calculations as above but for the ethane molecule. We basically only change<br />
’CH4’ to ’C2H6’ in the scripts:<br />
#!/usr/bin/env python<br />
from ase import *<br />
from ase.optimize import BFGS<br />
from ase.structure import molecule<br />
from ase.parallel import rank<br />
import numpy as np<br />
from gpaw import GPAW<br />
atoms = molecule('C2H6')<br />
atoms.center(vacuum=5)<br />
calc=GPAW(mode='lcao', basis='dzp', h=0.2, xc='PBE', nbands=8, txt='out_task5.txt')<br />
atoms.set_calculator(calc)<br />
dyn = BFGS(atoms , trajectory='C2H6.traj', logfile='bfgs_task5.log')<br />
dyn.run(fmax =0.001)<br />
You will next be asked to calculate the conformational energy in ethane. This you do by rotating the<br />
hydrogen atoms on one side of the ethane molecule and calculate the energy required for that process. Inside<br />
the script we also write out the eigenvalues for each rotation into the textfile ”.<br />
5
#!/usr/bin/env python<br />
from ase import *<br />
from ase.io import read<br />
from ase.optimize import BFGS<br />
from ase.io import PickleTrajectory<br />
import numpy as np<br />
from gpaw import GPAW<br />
atoms = read('C2H6.traj')<br />
calc=GPAW(mode='lcao', basis='dzp', h=0.2, xc='PBE', nbands=8, txt='out_task6.txt')<br />
atoms.set_calculator(calc)<br />
traj= PickleTrajectory('EthaneRotation.traj', 'w') # makes a trajectory file<br />
f = open('eigenvalues.txt','w') # Open a file for writing<br />
f.write('# angle eigenvalue1 eigenvalue 2 ...\n')<br />
maxstep=60<br />
for nsteps in range(maxstep):<br />
newatoms=atoms<br />
atoms2rot=atoms[2:5] #These are the H atoms that are supposed to be rotated<br />
angle=2*np.pi/(maxstep)<br />
rotvector = atoms[0]. position - atoms[1]. position<br />
atoms2rot.rotate(rotvector ,angle ,center='COM') #This rotates with angle around<br />
#the z-axis and the center of mass<br />
pos=atoms2rot.get_positions()<br />
for n in range(len(atoms2rot)):<br />
newatoms[n+2]. set_position(pos[n])<br />
newatoms.set_calculator(calc)<br />
traj.write(newatoms)<br />
eigenvalues = calc.get_eigenvalues()<br />
eigenvalues_s = ' '.join([str(i) for i in eigenvalues])<br />
angle_s = str(angle*(nsteps+1)*180/np.pi)<br />
# Write angle and eigenvalues to file<br />
f.write(angle_s + ' ' + eigenvalues_s + '\n')<br />
f.close() # close file<br />
Task 3<br />
Use the ag to look at the trajectory file. How much energy is required to rotate the ethane molecule? The<br />
experimental value of the barrier is 0.13 eV.<br />
To understand where the change in the conformational energy comes from we can plot the eigenvalues<br />
of the KS-system during the rotation. Here we use a script that plots the stored eigenvalues as a function of<br />
rotation. This you should not send to the queue, but instead run on the login node.<br />
#!/usr/bin/env python<br />
import numpy as np<br />
import pylab as plt<br />
data = np.loadtxt('eigenvalues.txt')<br />
angles = data[:,0]<br />
eigenvalues = data[:,1:]<br />
plt.plot(angles , eigenvalues , 'o-')<br />
plt.xlabel('Angle/[degrees]')<br />
plt.ylabel('K-S eigenvalues/[eV]')<br />
plt.show()<br />
How large is the change in eigenvalues (especially the HOMO level)? Given how the total energy is<br />
evaluated in normal DFT codes (see lecture notes) the conformational energy comes from the change in<br />
eigenvalues.<br />
6
1 Home assignment<br />
As a home assignment you will be asked to calculate the ground state structure of C 2 H 4 . You should try<br />
to calculate the conformational energy for ethylene. Your results for the conformational energy should<br />
be analyzed with respect to your knowledge of the molecular orbitals (TIPS: Read about sp3 and sp2<br />
hybridization). You should include a comparison with C 2 H 6 .<br />
Note that you will need to modify the script for C 2 H 6 in order to use it for the Home Assignment.<br />
Furthermore, the task will most probably get you into trouble along the way. Your task is to figure<br />
out why there is a problem with the script and how to circumvent the problem. It is the<br />
understanding of the problem (why it happens) that is most important.<br />
You are suppose to write a report about your first-principles investigation. Needless to say it should be<br />
well written and nicely presented.<br />
• Abstract (Present the task and give the main results. This part should be self-explained)<br />
• Introduction (Give some background and than try to explain the task and how you aim to solve it)<br />
• Computational method (code, calculator parameters, size of cell, elements, length of simulation and<br />
other relevant criteria)<br />
• Results (figures showing energies, perhaps figures showing the snapshots of trajectories, text that<br />
descries the results in a qualitative manner)<br />
• Discussion (Try to bring order in your results, rationalize the results to the best of your ability)<br />
• Conclusions<br />
• Appendix (Add your script(s) that you have used to solve the task)<br />
Remember that the report will be graded! Deadline is Tuesday 26/2 at 12:00.<br />
7