def cms(s, sel): at = getAtomtypes() if at is None: msg().error( "Atom-types information not available, calculating average.") if s is None: return None s = Structure(s) s.setCarthesian() w = Vector(0.0, 0.0, 0.0) M = 0.0 try: for i, nx, ny, nz in sel: if i < len(s) and i >= 0: try: m = float(s.info.getRecordForAtom(i).mass) except: m = float( at.getRecordForElement( s.info.getRecordForAtom(i).element).mass) w = w + m * (s[i] + nx * s.basis[0] + ny * s.basis[1] + nz * s.basis[2]) M = M + m w = (1.0 / M) * w return w except: msg().exception() msg().error("CMS not aplicable, calculating an average.") return average(s, sel)
def last(s, sel): if s is None: return None s = Structure(s) s.setCarthesian() if len(sel): i, nx, ny, nz = sel[-1] if len(s) > i: return s[i] + nx * s.basis[0] + ny * s.basis[1] + nz * s.basis[2] return None
def readStructure(self, Id, cstructure=False): s = Structure() l = self.fetchone( """SELECT calc_id,scale,comment,a11,a12,a13,a21,a22,a23,a31,a32,a33, species FROM #STRUCT WHERE id=%d""" % (Id)) (calc_id, scale, s.comment, s.basis[0][0], s.basis[0][1], s.basis[0][2], s.basis[1][0], s.basis[1][1], s.basis[1][2], s.basis[2][0], s.basis[2][1], s.basis[2][2], spec) = l s.scaling = [scale] s.setCartesian() l = self.fetchall( "SELECT specie,element,x,y,z FROM #STRUCTPOS WHERE structure_id=%d ORDER BY atomnumber" % (Id)) for spec, element, x, y, z in l: i = s.appendAtom(spec, Vector(x, y, z)) s.info.getRecordForAtom(i).element = str(element) l = self.fetchall( "SELECT x,y,z FROM #STRUCTCONSTRAINTS WHERE structure_id=%d ORDER BY atomnumber" % (Id)) if len(l): s.setSelective() for i in range(len(l)): s.selective[i] = [int(l[i][0]), int(l[i][1]), int(l[i][2])] if cstructure: s = p4vasp.cStructure.Structure(s) return s
def average(s, sel): if s is None: return None s = Structure(s) s.setCarthesian() w = Vector(0.0, 0.0, 0.0) n = 0 for i, nx, ny, nz in sel: if i < len(s) and i >= 0: w = w + s[i] + nx * s.basis[0] + ny * s.basis[1] + nz * s.basis[2] n = n + 1 w = (1.0 / float(n)) * w return w
def storeStructure(self, s, step=None): calc_id = self.calc_id if calc_id is not None and step is not None: for l in self.fetchall( "SELECT id FROM #STRUCT WHERE calc_id=%d AND step=%d" % (calc_id, step)): removeStructure(db, l[0]) info = s.info if len(s.scaling) != 1: s.correctScaling() cmd = "%s, %s," % (von(calc_id), von(step)) cmd += "%14.12f,'%s'," % (s.scaling[0], s.comment) cmd += """%14.12f,%14.12f,%14.12f, %14.12f,%14.12f,%14.12f, %14.12f,%14.12f,%14.12f,%d""" % ( s.basis[0][0], s.basis[0][1], s.basis[0][2], s.basis[1][0], s.basis[1][1], s.basis[1][2], s.basis[2][0], s.basis[2][1], s.basis[2][2], s.types) Id = self.insertRecord( "#STRUCT", "id", "calc_id, step, scale,comment,a11,a12,a13,a21,a22,a23,a31,a32,a33,species", cmd) if s.isSelective(): sel = s.selective for i in range(len(sel)): self.exe( """INSERT INTO #STRUCTCONSTRAINTS (structure_id,calc_id,atomnumber,x,y,z) VALUES (%d,%s,%d,%d,%d,%d)""" % (Id, von(calc_id), i, sel[i][0], sel[i][1], sel[i][2])) sc = Structure() sc.setStructure(s) sc.setCartesian() for i in range(len(s)): si = s.speciesIndex(i) self.exe( """INSERT INTO #STRUCTPOS (structure_id,calc_id,atomnumber,specie,element,x,y,z) VALUES (%d,%s,%d,%d,'%s',%14.12f,%14.12f,%14.12f)""" % (Id, von(calc_id), i, si, info[si].element, sc[i][0], sc[i][1], sc[i][2])) self.commit() return Id
#!/usr/bin/env python # this script extracts the atomic displacements from # the DISP file generated by Phon, # and creates POSCAR files for corresponding displacements from p4vasp.SystemPM import * from p4vasp.Structure import Structure #run=XMLSystemPM('vasprun.xml') #struct=run.FINAL_STRUCTURE struct = Structure("POSCAR") dispfile = open("DISP", 'r') runfile = open("runhf", 'w') runfile.write("#!/bin/bash \n") runfile.write("VASP='vasp' \n\n") disp = [] pos = [] pos.append(struct) pos[len(pos) - 1].write('pos' + repr(len(pos) - 1)) for line in dispfile: str = eval(line) liststr = str.split() listnum = [] for item in liststr: listnum.append(eval(item))
def vectorEqual(x, y, eps): """check wether two vectors (hashable containers of numbers) are equal.""" if len(x) != len(y): return False res = True for (xi, yi) in zip(x, y): res = res & isNearlyZero(xi - yi, eps) # this could be made more efficient' # since they are unequal as soon as a pair of components are not equal return res # name of distortion name = "X3-_sym1" # load a structure from existing POSCAR file: struct = Structure("POSCAR_X3-") print "Loaded POSCAR." cellvolume = struct.getCellVolume() print "Cell volume: ", cellvolume # backup the original structure #struct.write("POSCAR_X3-_ref", newformat=0) # open output file: outfile = open('frozen_phonon_' + name + '.dat', 'w') # load the symmetry vector try: symfile = open("symmetryvector.in", 'r') except: IOError, "symmetryvector.in file not found"
def CalculateElasticConstant(straintype): """Calculate the elastic constant corresponding to straintype using ab-initio energy calculation (VASP). For a C11 deformation, one can use the deformation tensor: [[1 0 0] [0 0 0] [0 0 0]] For a C11-C12 deformation, one can use the deformation tensor: [[1 0 0] [0 -1 0] [0 0 0]] ( 8 symmetry operators) or: [[1 0 0] [0 1 0] [0 0 -2]] (16 symmetry operators) For a C44 deformation, one can use the deformation tensor: [[0 0 0] [0 0 1] [0 1 0]] (4 symmetry operators) or: [[0 1 1] [1 0 1] [1 1 0]] (12 symmetry operators) All these tensors are now listed in the module CubicStandardStrainTensors. """ # load a structure from existing POSCAR file: struct = Structure("POSCAR") cellvolume = struct.getCellVolume() # define a sequence of strain amplitudes: strains=scipy.arange(-0.020, 0.022, 0.002).round(5) # For a C11 deformation, one can use the deformation tensor: # [[1 0 0] # [0 0 0] # [0 0 0]] # # For a C11-C12 deformation, one can use the deformation tensor: # [[1 0 0] # [0 -1 0] # [0 0 0]] # ( 8 symmetry operators) # or: # [[1 0 0] # [0 1 0] # [0 0 -2]] # (16 symmetry operators) # # For a C44 deformation, one can use the deformation tensor: # [[0 0 0] # [0 0 1] # [0 1 0]] # (4 symmetry operators) # or: # [[0 1 1] # [1 0 1] # [1 1 0]] # (12 symmetry operators) # all these tensors are now listed in the module CubicStandardStrainTensors import CubicStandardStrainTensors as cubic # straintype = 'C44_2' tens = cubic.__dict__[straintype] # tens = cubic.__dict__['C44_2'] # tensor = p4mat.Matrix([[1.0, 0.0, 0.0],[0.0, 0.0, 0.0],[0.0, 0.0, 0.0]]) tensor = p4mat.Matrix( tens.tolist() ) # cmd='rsh -. n00 mpirun -np 8 vasp' cmd = 'mr vasp' # Generate the strained POSCAR files GenStrainPoscar(struct, tensor, strains, straintype) # Execute the VASP command on the sequence of POSCAR files CalcStrains(strains, straintype, cmd) print "Finished calculating strains." # Collect the energies from the output vasprun.xml files. runs=[] energies=[] forces=[] for x in strains: runs.append(XMLSystemPM('vasprun_'+straintype+'_'+repr(x)+'.xml')) energies.append(runs[-1].FREE_ENERGY) pass for i in range(len(runs)): print runs[i].FINAL_STRUCTURE.getCellVolume() # for i in range(len(runs)): # forces.append(runs[i].FORCES_SEQUENCE) pass evec=np.array(energies) data=scipy.zeros((len(evec),2), dtype='f') # We build the data to outfile to file, which is used later for fitting # the strains are saved to file in Angstroems, not fractional coordinates # if the structure read from the POSCAR file is "direct", we need to apply scaling: # if struct.isDirect(): # scaling = struct.scaling[0] # data[:,0] = strains * scaling # else the strains are already in units of length (assumed Angstroems): # elif struct.isCartesian(): # data[:,0] = strains data[:,0] = strains data[:,1] = evec datafile=open(straintype+"_data.dat", 'w') scipy.io.write_array(datafile, data) datafile.close() elastConst = FitCubicStrain(straintype+"_data.dat", straintype, cellvolume) print straintype, ": ", elastConst print straintype, " (GPa) : ", elastConst*160.22 print "Finished all." pass # End of CalculateElasticConstant
def updateCoord(self): s=self.getStructure() if s is not None: s=Structure(s) s.setCarthesian() sel=self.sel if len(sel)>1: try: i,nx,ny,nz=sel[0] v1=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz i,nx,ny,nz=sel[1] v2=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz l=(v1-v2).length() self.l1_entry.set_text("%14.8f"%l) except: self.l1_entry.set_text("") else: self.l1_entry.set_text("") if len(sel)>2: try: i,nx,ny,nz=sel[0] a=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz i,nx,ny,nz=sel[1] b=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz i,nx,ny,nz=sel[2] c=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz l=(b-c).length() self.l2_entry.set_text("%14.8f"%l) angle=(a-b).angle(c-b)*180.0/pi self.a2_entry.set_text("%+7.4f"%angle) except: msg().exception() self.l2_entry.set_text("") self.a2_entry.set_text("") else: self.l2_entry.set_text("") self.a2_entry.set_text("") if len(sel)>3: try: i,nx,ny,nz=sel[0] a=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz i,nx,ny,nz=sel[1] b=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz i,nx,ny,nz=sel[2] c=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz i,nx,ny,nz=sel[3] d=s[i]+s.basis[0]*nx+s.basis[1]*ny+s.basis[2]*nz l=(d-c).length() self.l3_entry.set_text("%14.8f"%l) angle=(b-c).angle(d-c)*180.0/pi self.a3_entry.set_text("%+7.4f"%angle) try: dihedral=(c-b).cross(a-b).angle((b-c).cross(d-c))*180.0/pi self.d3_entry.set_text("%+7.4f"%dihedral) except: self.d3_entry.set_text("") except: msg().exception() self.l3_entry.set_text("") self.a3_entry.set_text("") self.d3_entry.set_text("") else: self.l3_entry.set_text("") self.a3_entry.set_text("") self.d3_entry.set_text("")
def vectorEqual(x,y, eps): """check wether two vectors (hashable containers of numbers) are equal.""" if len(x) != len(y): return False res = True for (xi, yi) in zip(x,y): res = res & isNearlyZero(xi-yi, eps) # this could be made more efficient' # since they are unequal as soon as a pair of components are not equal return res # name of distortion name = "X3-_sym1" # load a structure from existing POSCAR file: struct = Structure("POSCAR_X3-") print "Loaded POSCAR." cellvolume = struct.getCellVolume() print "Cell volume: ", cellvolume # backup the original structure #struct.write("POSCAR_X3-_ref", newformat=0) # open output file: outfile = open('frozen_phonon_'+name+'.dat', 'w') # load the symmetry vector try: symfile = open("symmetryvector.in", 'r') except: IOError, "symmetryvector.in file not found"
from p4vasp.db import * from p4vasp.Structure import Structure from sys import stdout db = getDatabase()[0] db.connect() db.addUser() path = "XDATCAR" s = Structure(path) s.write(stdout) f = open(path) for i in range(8): f.readline() #Skip header db.addRecord(keywords="MD,XDATCAR", name=s.comment) step = 0 while 1: for i in range(len(s)): s[i] = map(float, f.readline().split()) step += 1 #s.write(stdout) db.storeStructure(s, step) if f.readline().strip() != "": break