geometry.py 9.25 KB
 Marco Govoni committed Jun 24, 2018 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 ``````from __future__ import print_function class Geometry(object) : """Class for representing a set of atoms in a periodic cell. :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.setCell( (1,0,0), (0,1,0), (0,0,1) ) >>> geom.addAtom( "Si", (0,0,0) ) >>> geom.addSpecies( "Si", "Si_ONCV_PBE-1.1.upf", "http://www.quantum-simulation.org/potentials/sg15_oncv/upf/Si_ONCV_PBE-1.1.upf" ) .. note:: Vectors are set in a.u. by default. If you set units=Angstrom a coversion to a.u. will be made. """ # from westpy import Bohr # def __init__(self,cell=None) : self.cell = [] self.atoms = [] self.species = {} self.isSet = {} self.isSet["cell"] = False self.isSet["atoms"] = False self.isSet["species"] = False self.pseudoFormat = None # def __atomsMatchSpecies(self) : """Checks if atoms match species""" matches = self.isSet["species"] and self.isSet["atoms"] for atom in self.atoms : matches = matches and atom.symbol in self.species.keys() return matches # def isValid(self) : """Checks if geometry is valid The method checks that: - the cell is set - at least one atom has been added - the pseudopotentials of all species are defined """ isValid = True for key in self.isSet.keys() : isValid = isValid and self.isSet[key] if( not self.isSet[key] ) : print("ERR: set "+key) isValid = isValid and self.__atomsMatchSpecies() return isValid # def addSpecies(self, symbol, fname, url) : """Adds a species. :param symbol: chemical symbol :type symbol: string :param fname: file name :type fname: string :param url: url `````` Marco Govoni committed Jun 24, 2018 61 `````` :type url: string `````` Marco Govoni committed Jun 24, 2018 62 63 64 65 66 67 68 69 70 `````` :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.addSpecies( "Si", "Si_ONCV_PBE-1.1.upf", "http://www.quantum-simulation.org/potentials/sg15_oncv/upf/Si_ONCV_PBE-1.1.upf" ) .. note:: You can use this method to add either upf or xml pseudopotentials. However it is forbidded to mix them. """ `````` Marco Govoni committed Jun 24, 2018 71 `````` this_pseudo_format = None `````` Marco Govoni committed Jun 24, 2018 72 73 74 75 76 77 78 79 80 81 82 83 84 85 `````` if( fname.endswith("upf") or fname.endswith("UPF")) : this_pseudo_format = "upf" if( fname.endswith("xml") or fname.endswith("XML")) : this_pseudo_format = "xml" assert( this_pseudo_format in ["upf","xml"] ) from mendeleev import element el = element(symbol) self.species[symbol] = {} self.species[symbol]["fname"] = fname self.species[symbol]["url"] = url self.species[symbol]["symbol"] = el.symbol self.species[symbol]["atomic_number"] = el.atomic_number self.species[symbol]["name"] = el.name self.species[symbol]["mass"] = el.mass `````` Marco Govoni committed Jun 24, 2018 86 87 `````` self.species[symbol]["format"] = this_pseudo_format self.__updatePseudoFormat() `````` Marco Govoni committed Jun 24, 2018 88 89 `````` self.isSet["species"] = True # `````` Marco Govoni committed Jun 24, 2018 90 91 92 93 94 95 96 97 98 99 100 101 `````` def __updatePseudoFormat(self) : """pseudo format is either upf, xml or mixed. """ f = [] for key in self.species.keys() : f.append(self.species[key]["format"]) self.pseudoFormat = 'mixed' if( all(form == 'upf' for form in f) ) : self.pseudoFormat = 'upf' if( all(form == 'xml' for form in f) ) : self.pseudoFormat = 'xml' # `````` Marco Govoni committed Jun 24, 2018 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 `````` def setCell(self, a1=(0, 0, 0), a2=(0, 0, 0), a3=(0, 0, 0), units=Bohr ) : """Sets cell, given the three vectors :math:`a_1`, :math:`a_2`, :math:`a_3`. :param a1: :math:`a_1` :type a1: 3-dim tuple :param a2: :math:`a_2` :type a2: 3-dim tuple :param a3: :math:`a_3` :type a3: 3-dim tuple :param units: Units, optional :type units: "Bohr" or "Angstrom" :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.setCell( (1,0,0), (0,1,0), (0,0,1) ) """ import numpy as np # self.cell.append( np.array(a1, float) * units ) self.cell.append( np.array(a2, float) * units ) self.cell.append( np.array(a3, float) * units ) self.isSet["cell"] = True # def addAtom(self, symbol, position, units=Bohr) : """Adds a single atom. :param symbol: chemical symbol :type symbol: string :param position: position :type position: 3-dim tuple :param units: Units, optional :type units: "Bohr" or "Angstrom" :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.addAtom( "Si", (0,0,0) ) """ from westpy import Atom self.atoms.append( Atom(symbol, position, units) ) self.isSet["atoms"] = True # def __addAtomsFromXYZLines(self, lines ) : """Adds atoms from XYZ lines. :param lines: lines read from XYZ file (only one image) :type lines: list of string """ # from westpy import Angstrom natoms = int(lines[0]) for line in lines[2:2+natoms] : symbol, x, y, z = line.split()[:4] self.addAtom( symbol.decode("utf-8"), (float(x), float(y), float(z)), units=Angstrom ) # def addAtomsFromXYZFile(self, fname ) : """Adds atoms from XYZ file (only one image). :param fname: file name :type fname: string :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.addAtomFromXYZFile( "CH4.xyz" ) """ # with open(fname,'r') as file: lines = file.readlines() self.__addAtomsFromXYZLines(lines) # def addAtomsFromOnlineXYZ(self, url ) : """Adds atoms from XYZ file (only one image) located at url. :param url: url :type url: string :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.addAtomsFromOnlineXYZ( "http://www.west-code.org/database/gw100/xyz/CH4.xyz" ) """ # import urllib.request with urllib.request.urlopen(url) as response : lines = response.readlines() self.__addAtomsFromXYZLines(lines) # def getNumberOfAtoms(self) : """Returns number of atoms. :returns: number of atoms :rtype: int :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.addAtomsFromOnlineXYZ( "http://www.west-code.org/database/gw100/xyz/CH4.xyz" ) >>> nat = geom.getNumberOfAtoms() >>> print( nat ) 5 """ nat = len(self.atoms) return nat # def getNumberOfSpecies(self) : """Returns number of species. :returns: number of species :rtype: int :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.addAtomsFromOnlineXYZ( "http://www.west-code.org/database/gw100/xyz/CH4.xyz" ) >>> ntyp = geom.getNumberOfSpecies() >>> print( ntyp ) 2 """ sp = [] for atom in self.atoms : if atom.symbol not in sp : sp.append(atom.symbol) ntyp = len(sp) return ntyp # def getNumberOfElectrons(self) : """Returns number of electrons. :returns: number of electrons :rtype: int :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.addAtomsFromOnlineXYZ( "http://www.west-code.org/database/gw100/xyz/CH4.xyz" ) >>> geom.addSpecies( "C", "C_ONCV_PBE-1.0.xml", "http://www.quantum-simulation.org/potentials/sg15_oncv/xml/C_ONCV_PBE-1.0.xml") >>> geom.addSpecies( "H", "H_ONCV_PBE-1.0.xml", "http://www.quantum-simulation.org/potentials/sg15_oncv/xml/H_ONCV_PBE-1.0.xml") >>> nelec = geom.getNumberOfElectrons() >>> print( nelec ) 8 """ assert( self.__atomsMatchSpecies() ) # nelec = 0 if self.pseudoFormat in ["upf"] : from westpy import listLinesWithKeyfromOnlineText for atom in self.atoms : symbol = atom.symbol url = self.species[symbol]["url"] resp = listLinesWithKeyfromOnlineText(url,"z_valence")[0] this_valence = float(resp.decode("utf-8").split('"')[1]) nelec += this_valence if self.pseudoFormat in ["xml"] : from westpy import listValuesWithKeyFromOnlineXML for atom in self.atoms : symbol = atom.symbol url = self.species[symbol]["url"] this_valence = float(listValuesWithKeyFromOnlineXML(url,"valence_charge")[0]) nelec += this_valence return int(nelec) # def downloadPseudopotentials(self) : """Download Pseudopotentials. :Example: >>> from westpy import * >>> geom = Geometry() >>> geom.addAtomsFromOnlineXYZ( "http://www.west-code.org/database/gw100/xyz/CH4.xyz" ) >>> geom.addSpecies( "C", "C_ONCV_PBE-1.0.xml", "http://www.quantum-simulation.org/potentials/sg15_oncv/xml/C_ONCV_PBE-1.0.xml") >>> geom.addSpecies( "H", "H_ONCV_PBE-1.0.xml", "http://www.quantum-simulation.org/potentials/sg15_oncv/xml/H_ONCV_PBE-1.0.xml") >>> geom.downloadPseudopotentials() .. note:: Pseudopotential files will be downloaded in the current directory. """ assert( self.__atomsMatchSpecies() ) # from westpy import download for key in self.species.keys() : download( self.species[key]["fname"], self.species[key]["url"])``````