geometry.py 12.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
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) ) 
12
   >>> geom.addSpecies( "Si", "http://www.quantum-simulation.org/potentials/sg15_oncv/upf/Si_ONCV_PBE-1.1.upf" ) 
13
14
15
16
17
18
19
20

   .. 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) : 
Marco Govoni's avatar
Marco Govoni committed
21
      self.cell = {}
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
      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
Marco Govoni's avatar
Marco Govoni committed
43
44
         - the pseudopotentials of all species are defined 
         - the pseudopotentials do not contain a mix of upf and xml formats
45
46
47
48
49
50
51
      """
      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()
Marco Govoni's avatar
Marco Govoni committed
52
      isValid = isValid and self.pseudoFormat in ["upf","xml"]
53
54
      return isValid
   #
55
   def addSpecies(self, symbol, url) : 
56
57
58
59
60
      """Adds a species.
   
      :param symbol: chemical symbol 
      :type symbol: string
      :param url: url 
Marco Govoni's avatar
Marco Govoni committed
61
      :type url: string
62
63
64
65
66
   
      :Example:

      >>> from westpy import * 
      >>> geom = Geometry()
67
      >>> geom.addSpecies( "Si", "http://www.quantum-simulation.org/potentials/sg15_oncv/upf/Si_ONCV_PBE-1.1.upf" ) 
68
69
70
      
      .. note:: You can use this method to add either upf or xml pseudopotentials. However it is forbidded to mix them.  
      """
71
72
      from westpy import extractFileNamefromUrl
      fname = extractFileNamefromUrl(url)
Marco Govoni's avatar
Marco Govoni committed
73
      this_pseudo_format = None
74
75
76
77
78
79
80
81
82
83
84
85
86
87
      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's avatar
Marco Govoni committed
88
89
      self.species[symbol]["format"] = this_pseudo_format
      self.__updatePseudoFormat()
90
91
      self.isSet["species"] = True
   #
Marco Govoni's avatar
Marco Govoni committed
92
93
94
95
96
97
98
99
100
101
102
103
   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'
   #
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
   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
      # 
Marco Govoni's avatar
Marco Govoni committed
124
125
126
127
128
129
130
      self.cell["a1"] = np.array(a1, float) * units 
      self.cell["a2"] = np.array(a2, float) * units 
      self.cell["a3"] = np.array(a3, float) * units 
      self.cell["volume"] = np.dot(self.cell["a1"], np.cross(self.cell["a2"], self.cell["a3"]))
      self.cell["b1"] = 2.0 * np.pi * np.cross(self.cell["a2"], self.cell["a3"]) / self.cell["volume"]
      self.cell["b2"] = 2.0 * np.pi * np.cross(self.cell["a3"], self.cell["a1"]) / self.cell["volume"]
      self.cell["b3"] = 2.0 * np.pi * np.cross(self.cell["a1"], self.cell["a2"]) / self.cell["volume"]
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
      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
   #
Lan Huang's avatar
Lan Huang committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
   def addFracCoordAtom(self, symbol, frac_coord):
      """adds a single atom by fractional coords
      :param symbol: chemical symbol
      :type symbol: string
      :param position: position
      :type position: 3-dim tuple

      :Example:

      >>> from westpy import *
      >>> geom = Geometry()
      >>> geom.addFracCoordAtom( "Si", (0,1/3.0,2/3.0) )
      """
      if not self.isSet["cell"]:
         print('Set cell first!')
         return
      import numpy as np
      from westpy import Atom, Bohr
      M = np.matrix([self.cell['a1'], self.cell['a2'], self.cell['a3']]).tolist()
      self.atoms.append(Atom(symbol, tuple(np.array(frac_coord)@M), units=Bohr))
      self.isSet["atoms"] = True


Lan Huang's avatar
Lan Huang committed
176
   def __addAtomsFromXYZLines(self, lines, decode=True ) :
177
178
179
180
      """Adds atoms from XYZ lines.
   
      :param lines: lines read from XYZ file (only one image)
      :type lines: list of string
Lan Huang's avatar
Lan Huang committed
181
182
      :param decode:
      :type bool:
183
184
185
186
187
188
      """
      #
      from westpy import Angstrom 
      natoms = int(lines[0])
      for line in lines[2:2+natoms] : 
         symbol, x, y, z = line.split()[:4]
Lan Huang's avatar
Lan Huang committed
189
190
191
192
         if decode:
            self.addAtom( symbol.decode("utf-8"), (float(x), float(y), float(z)), units=Angstrom )
         else:
            self.addAtom( symbol, (float(x), float(y), float(z)), units=Angstrom )
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
   #
   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()
Lan Huang's avatar
Lan Huang committed
209
      self.__addAtomsFromXYZLines(lines,  decode=False)
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
   #
   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()
Lan Huang's avatar
Lan Huang committed
227
      self.__addAtomsFromXYZLines(lines,  decode=True)
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
   #
   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" )
280
281
      >>> geom.addSpecies( "C", "http://www.quantum-simulation.org/potentials/sg15_oncv/xml/C_ONCV_PBE-1.0.xml")
      >>> geom.addSpecies( "H", "http://www.quantum-simulation.org/potentials/sg15_oncv/xml/H_ONCV_PBE-1.0.xml")
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
      >>> 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" )
314
315
      >>> geom.addSpecies( "C", "http://www.quantum-simulation.org/potentials/sg15_oncv/xml/C_ONCV_PBE-1.0.xml")
      >>> geom.addSpecies( "H", "http://www.quantum-simulation.org/potentials/sg15_oncv/xml/H_ONCV_PBE-1.0.xml")
316
317
318
319
320
321
322
323
      >>> 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() :
324
         download( self.species[key]["url"], fname=self.species[key]["fname"] )
Lan Huang's avatar
Lan Huang committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

   def view(self,  style='stick',width=800,height=800, ix=1, iy=1, iz=1, debug=False):
      """Display simulation box geom in Angstrom.
      ix, iy, iz is the perodic display to system
      style can be line, stick, sphere.

      :param style:
      :param width:
      :param height:
      :param ix:
      :param iy:
      :param iz:
      :param debug:
      :return:
      """
      import py3Dmol
      import numpy as np
      from westpy.units import Angstrom
      BOHR2A = 1.0 / Angstrom
      a1 = self.cell["a1"] * BOHR2A
      a2 = self.cell["a2"] * BOHR2A
      a3 = self.cell["a3"] * BOHR2A
      nat = self.getNumberOfAtoms()
      times = ix * iy * iz
      if times > 1:
         nat *= times
      #generate xyz data
      xyz = str(nat) + '\n\n'
      for atom in self.atoms:
         if times:
            for i in range(ix):
               for j in range(iy):
                  for k in range(iz):
                     xyz += atom.symbol + " " + " ".join(
                        map(str, atom.position * BOHR2A + i * a1 + j * a2 + k * a3)) + '\n'
      #creat viewer
      xyzview = py3Dmol.view(width=width, height=height)
      xyzview.addModel(xyz, 'xyz')
      if debug:
         print(xyz)
      xyzview.setStyle({style: {}})
      #draw the box
      a0 = np.array([0.0, 0.0, 0.0])
      from_ = [a0, a1 + a2, a1 + a3, a2 + a3]
      to_ = [[a1, a2, a3], [a1, a2, a1 + a2 + a3], [a1, a3, a1 + a2 + a3], [a2, a3, a1 + a2 + a3]]
      for frm, li_to in zip(from_, to_):
         x0, y0, z0 = frm
         for to in li_to:
            x1, y1, z1 = to
            xyzview.addLine(
               {'color': 'blue', 'start': {'x': x0, 'y': y0, 'z': z0}, 'end': {'x': x1, 'y': y1, 'z': z1}});
      #show
      xyzview.zoomTo()
      xyzview.show()