{"id":496,"date":"2021-12-15T09:02:19","date_gmt":"2021-12-15T09:02:19","guid":{"rendered":"https:\/\/doublelayer.eu\/vilab\/?p=496"},"modified":"2022-06-07T08:35:32","modified_gmt":"2022-06-07T08:35:32","slug":"bader-hirshfeld-analyses","status":"publish","type":"post","link":"https:\/\/doublelayer.eu\/vilab\/2021\/12\/15\/bader-hirshfeld-analyses\/","title":{"rendered":"Bader and Hirshfeld charges with python"},"content":{"rendered":"\n<p>Bader analysis is a fast and simple way of getting atomic charges. It is especially useful for periodic calculations. The analysis can be done on the fly using <kbd>pybader<\/kbd> tool from <a href=https:\/\/pypi.org\/project\/pybader>pypi.org\/project\/pybader<\/a>. I recommend using it within <kbd>conda<\/kbd> environments.<\/p>\n\n\n\n<p>The installation is straightforward: <\/p>\n\n\n\n<p><kbd>pip install pybader<\/kbd><\/p>\n\n\n\n<p>The usage is less obvious. Here is a function for obtaining xmol-type xyz that can obtained with <kbd>GPAW<\/kbd> and visualized with <kbd>ASE<\/kbd>:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>\ndef xyzb(atoms, filename, nCPU):\n  from pybader.io import gpaw\n  from pybader.interface import Bader\n  import os\n  bader = Bader(*gpaw.read_obj(atoms.calc)) # read ASE object 'atoms'\n  bader(threads=nCPU)                       # specify the number of CPUs\n  f = open('{0}.xyz'.format(filename), 'w') # set xmol format\n  b = bader.atoms_charge                    # get number of electrons per atom\n  n = atoms.get_atomic_numbers()            # get atomic numbers\n  a = atoms.get_chemical_symbols()          # get chemical symbols\n  p = atoms.get_positions()                 # get positions of the atoms\n  f.write('{0}\\n'.format(len(a)))\n  f.write('Properties=species:S:1:pos:R:3:charge:R:1\\n') # ensure compatibility with ASE\n  for i in range(len(a)):                   # print symbol, positions, and charge\n    s = '{0}'.format(a[i])\n    x = '{0:.6f}'.format(round(p[i][0],6))\n    y = '{0:.6f}'.format(round(p[i][1],6))\n    z = '{0:.6f}'.format(round(p[i][2],6))\n    c = '{0:.3f}'.format(round(float(n[i]) - float(b[i]),3))\n    f.write('{0:<4}{1:>16}{2:>16}{3:>16}{4:>10}\\n'.format(s,x,y,z,c))\n  f.close()\n  del bader\n  os.remove('bader.p')\n<\/code>\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Similarly one can obtain xmol-type xyz file with Hirshfeld charges:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>\ndef xyzh(atoms, filename):\n  from gpaw.analyse.hirshfeld import HirshfeldPartitioning\n  f = open('{0}.xyz'.format(filename), 'w') # set xmol format\n  a = atoms.get_chemical_symbols()          # get chemical symbols\n  p = atoms.get_positions()                 # get positions of the atoms\n  h = HirshfeldPartitioning(atoms.calc).get_charges()\n  f.write('{0}\\n'.format(len(a)))\n  f.write('Properties=species:S:1:pos:R:3:charge:R:1\\n') # ensure compatibility with ASE\n  for i in range(len(a)):                   # print symbol, positions, and charge\n    s = '{0}'.format(a[i])\n    x = '{0:.6f}'.format(round(p[i][0],6))\n    y = '{0:.6f}'.format(round(p[i][1],6))\n    z = '{0:.6f}'.format(round(p[i][2],6))\n    c = '{0:.3f}'.format(round(h[i],3))\n    f.write('{0:<4}{1:>16}{2:>16}{3:>16}{4:>10}\\n'.format(s,x,y,z,c))\n  f.close()\n<\/code>\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>In the LCAO mode of GPAW one can also get the Mulliken charges. Test before using:<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>\ndef xyzm(atoms, filename):\n  from gpaw.lcao.tools import get_mulliken\n  f = open('{0}.xyz'.format(filename), 'w') # set xmol format\n  a = atoms.get_chemical_symbols()          # get chemical symbols\n  p = atoms.get_positions()                 # get positions of the atoms\n  m = get_mulliken(atoms.calc, range(len(a)))\n  f.write('{0}\\n'.format(len(a)))\n  f.write('Properties=species:S:1:pos:R:3:charge:R:1\\n') # ensure compatibility with ASE\n  for i in range(len(a)):                   # print symbol, positions, and charge\n    s = '{0}'.format(a[i])\n    x = '{0:.6f}'.format(round(p[i][0],6))\n    y = '{0:.6f}'.format(round(p[i][1],6))\n    z = '{0:.6f}'.format(round(p[i][2],6))\n    c = '{0:.3f}'.format(round(m[i],3))\n    f.write('{0:<4}{1:>16}{2:>16}{3:>16}{4:>10}\\n'.format(s,x,y,z,c))\n  f.close()\n<\/code>\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bader analysis is a fast and simple way of getting atomic charges. It is especially useful for periodic calculations. The analysis can be done on the fly using pybader tool from pypi.org\/project\/pybader. I recommend using it within conda environments. The installation is straightforward: pip install pybader The usage is less obvious. Here is a function&hellip; <a class=\"read-more\" href=\"https:\/\/doublelayer.eu\/vilab\/2021\/12\/15\/bader-hirshfeld-analyses\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[48,47,46,50,49],"class_list":["post-496","post","type-post","status-publish","format-standard","hentry","category-know-how","tag-ase","tag-bader","tag-dft","tag-msca","tag-scripts"],"_links":{"self":[{"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/posts\/496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/comments?post=496"}],"version-history":[{"count":20,"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/posts\/496\/revisions"}],"predecessor-version":[{"id":589,"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/posts\/496\/revisions\/589"}],"wp:attachment":[{"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/media?parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/categories?post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/doublelayer.eu\/vilab\/wp-json\/wp\/v2\/tags?post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}