Debian Codegraph @

doc/n2_example.py

raw
1 from ase.build import molecule
2 from ase.parallel import paropen
3 from ase.units import Bohr, Ha
4 from gpaw import GPAW, FermiDirac
5 from gpaw.analyse.hirshfeld import HirshfeldPartitioning
6 from gpaw.cluster import Cluster
7
8 from pymbd import molecular_polarizability
9 from pymbd.fortran import MBDGeom
10
11 # make ASE atoms object
12 atoms = Cluster(molecule('N2'))
13
14 # initialize GPAW calculator
15 h = 0.2 # set grid spacing for GPAW calculation
16 atoms.minimal_box(4.0, h=h)
17 c = GPAW(xc='PBE', h=h, nbands=-6, occupations=FermiDirac(width=0.1))
18 atoms.calc = c
19
20 # get the ground state energy and forces
21 energy = atoms.get_potential_energy()
22 forces = atoms.get_forces()
23
24 # calculate the Hirshfeld volumes
25 hf = HirshfeldPartitioning(atoms.calc)
26 volumes = hf.get_effective_volume_ratios()
27
28 # convert input paramenters from ASE to pymbd
29 coords = atoms.positions / Bohr
30 species = atoms.get_chemical_symbols()
31 beta = 0.83 # default for PBE xc-functional
32
33 # static polarizability tensor for nitrogen in Bohr**3
34 alpha = molecular_polarizability(coords, species, volumes, beta)
35
36 # dispersion correction for energy and forces
37 mbd_atoms = MBDGeom(coords, lattice=None)
38 mbd_energy, mbd_forces = mbd_atoms.mbd_energy_species(
39 species, volumes, beta, force=True
40 )
41
42 # convert from Hartree to eV
43 mbd_energy *= Ha
44 # convert from Ha / Bohr to eV/ Angstrom
45 mbd_forces *= Ha / Bohr
46
47 # add dispersion correction to ground state energy to get total energy and forces
48 total_energy = energy + mbd_energy
49 total_forces = forces + mbd_forces
50
51 f = paropen('n2_results', 'a')
52
53 print('Results for nitrogen molecule', file=f)
54 print('Molecular polarizability [Bohr**3]', file=f)
55 print(alpha, file=f)
56
57 print('Energy without dispersion correction [eV]', file=f)
58 print(energy, file=f)
59 print('Dispersion energy [eV]', file=f)
60 print(mbd_energy, file=f)
61 print('Total energy with dispersion correction [eV]', file=f)
62 print(total_energy, file=f)
63
64 print('Forces without dispersion correction [eV/Ang]', file=f)
65 print(forces, file=f)
66 print('Dispersion correction [eV/Ang]', file=f)
67 print(mbd_forces, file=f)
68 print('Total forces with dispersion correction [eV/Ang]', file=f)
69 print(total_forces, file=f)
70
71 f.close()