def __init__(self, atoms, outfile=None): unitcell = atoms.get_cell() A = Vector(unitcell[0]) B = Vector(unitcell[1]) C = Vector(unitcell[2]) # lengths of the vectors a = A.length() #*angstroms2bohr b = B.length() #*angstroms2bohr c = C.length() #*angstroms2bohr # angles between the vectors rad2deg = 360. / (2. * math.pi) alpha = B.angle(C) * rad2deg beta = A.angle(C) * rad2deg gamma = A.angle(B) * rad2deg scaledpositions = atoms.get_scaled_positions() chemicalsymbols = [atom.get_symbol() for atom in atoms] input = '' input += 'title \n' input += '0 tolerance\n' input += '2 lattice parameters in lengths and angles\n' input += '%1.3f %1.3f %1.3f %1.3f %1.3f %1.3f\n' % (a, b, c, alpha, beta, gamma) input += '1 3 basis vectors for unit cell\n' input += '1.00 0.00 0.00\n' input += '0.00 1.00 0.00\n' input += '0.00 0.00 1.00\n' input += '%i number of atoms\n' % len(atoms) types = '' for atom in atoms: types += str(atom.get_atomic_number()) + ' ' input += types + '\n' for i, atom in enumerate(atoms): input += '%1.3f %1.3f %1.3f\n' % tuple(scaledpositions[i]) pin, pout = os.popen2('findsym') pin.writelines(input) pin.close() self.output = pout.readlines() pout.close() if outfile: f = open(outfile, 'w') f.writelines(self.output) f.close() if os.path.exists('findsym.log'): os.remove('findsym.log')
def __init__(self,atoms,outfile=None): unitcell = atoms.get_cell() A = Vector(unitcell[0]) B = Vector(unitcell[1]) C = Vector(unitcell[2]) # lengths of the vectors a = A.length()#*angstroms2bohr b = B.length()#*angstroms2bohr c = C.length()#*angstroms2bohr # angles between the vectors rad2deg = 360./(2.*math.pi) alpha = B.angle(C)*rad2deg beta = A.angle(C)*rad2deg gamma = A.angle(B)*rad2deg scaledpositions = atoms.get_scaled_positions() chemicalsymbols = [atom.get_symbol() for atom in atoms] input = '' input += 'title \n' input += '0 tolerance\n' input += '2 lattice parameters in lengths and angles\n' input += '%1.3f %1.3f %1.3f %1.3f %1.3f %1.3f\n' % (a,b,c, alpha,beta,gamma) input += '1 3 basis vectors for unit cell\n' input += '1.00 0.00 0.00\n' input += '0.00 1.00 0.00\n' input += '0.00 0.00 1.00\n' input += '%i number of atoms\n' % len(atoms) types = '' for atom in atoms: types += str(atom.get_atomic_number()) + ' ' input += types + '\n' for i,atom in enumerate(atoms): input += '%1.3f %1.3f %1.3f\n' % tuple(scaledpositions[i]) pin,pout = os.popen2('findsym') pin.writelines(input) pin.close() self.output = pout.readlines() pout.close() if outfile: f = open(outfile,'w') f.writelines(self.output) f.close() if os.path.exists('findsym.log'): os.remove('findsym.log')
def __init__(self, atoms, outfile=None): '''outfile is where the results will be stored if you want them. Otherwise they go into a tempfile that is deleted.''' id, infile = tempfile.mkstemp() if outfile is None: od, ofile = tempfile.mkstemp() else: ofile = outfile unitcell = atoms.get_cell() A = Vector(unitcell[0]) B = Vector(unitcell[1]) C = Vector(unitcell[2]) # lengths of the vectors a = A.length() #*angstroms2bohr b = B.length() #*angstroms2bohr c = C.length() #*angstroms2bohr # angles between the vectors rad2deg = 360. / (2. * math.pi) alpha = B.angle(C) * rad2deg beta = A.angle(C) * rad2deg gamma = A.angle(B) * rad2deg scaledpositions = atoms.get_scaled_positions() chemicalsymbols = [atom.get_symbol() for atom in atoms] f = open(infile, 'w') f.write('P\n') f.write('%1.4f %1.4f %1.4f %1.4f %1.4f %1.4f\n' % (a, b, c, alpha, beta, gamma)) f.write('%i\n' % len(atoms)) for i, atom in enumerate(atoms): f.write('%1.4f %1.4f %1.4f\n' % tuple(scaledpositions[i])) f.write('%s\n\n' % chemicalsymbols[i]) f.close() os.system('sgroup %s %s' % (infile, ofile)) f = open(ofile, 'r') self.output = f.readlines() f.close() os.unlink(infile) os.close(id) if outfile is None: os.unlink(ofile) os.close(od) # you must close the file descriptor or
def __init__(self,atoms,outfile=None): unitcell = atoms.get_cell() A = Vector(unitcell[0]) B = Vector(unitcell[1]) C = Vector(unitcell[2]) # lengths of the vectors a = A.length()#*angstroms2bohr b = B.length()#*angstroms2bohr c = C.length()#*angstroms2bohr # angles between the vectors rad2deg = 360./(2.*math.pi) alpha = B.angle(C)*rad2deg beta = A.angle(C)*rad2deg gamma = A.angle(B)*rad2deg scaledpositions = atoms.get_scaled_positions() chemicalsymbols = [atom.get_symbol() for atom in atoms] input = '' input += '%1.3f %1.3f %1.3f %1.3f %1.3f %1.3f\n' % (a,b,c, alpha,beta,gamma) input += '1 1 0.1 0.1\n' for atom in atoms: sym = atom.get_symbol() group = 1 x,y,z = atom.get_position() #format(a6,i2,6f9.5) input += str(FortranLine((sym, group, x,y,z), FortranFormat('a6,i2,3f9.5')))+'\n' pin,pout = os.popen2('symmol') pin.writelines(input) pin.close() self.output = pout.readlines() pout.close() if outfile: f = open(outfile,'w') f.writelines(self.output) f.close() if os.path.exists('symmol.log'): os.remove('symmol.log')
def __init__(self,atoms,outfile=None): unitcell = atoms.get_cell() A = Vector(unitcell[0]) B = Vector(unitcell[1]) C = Vector(unitcell[2]) # lengths of the vectors a = A.length()#*angstroms2bohr b = B.length()#*angstroms2bohr c = C.length()#*angstroms2bohr # angles between the vectors rad2deg = 360./(2.*math.pi) alpha = B.angle(C)*rad2deg beta = A.angle(C)*rad2deg gamma = A.angle(B)*rad2deg # scaledpositions = atoms.get_scaled_positions() # chemicalsymbols = [atom.get_symbol() for atom in atoms] input = '' input += '%1.3f %1.3f %1.3f %1.3f %1.3f %1.3f\n' % (a,b,c, alpha,beta,gamma) input += '1 1 0.1 0.1\n' for atom in atoms: sym = atom.get_symbol() group = 1 x,y,z = atom.get_position() #format(a6,i2,6f9.5) input += str(FortranLine((sym, group, x,y,z), FortranFormat('a6,i2,3f9.5')))+'\n' pin,pout = os.popen2('symmol') pin.writelines(input) pin.close() self.output = pout.readlines() pout.close() if outfile: f = open(outfile,'w') f.writelines(self.output) f.close() if os.path.exists('symmol.log'): os.remove('symmol.log')
def _explicit_qvectors(self, qMin, qMax, indRange): qVects = [] hkls = [] dimen = len(self.qDirections) for ind in indRange: qVect = Vector() for i in range(dimen): qVect += ind[i] * self.qDirections[i] if qMin < qVect.length() <= qMax: if not qVect in qVects: qVects.append(qVect) hkl = Vector([0, 0, 0]) for i in range(dimen): hkl += ind[i] * self.hkl_dir[i] hkls.append(hkl) if len(qVects) >= self.qVectorsPerShell: break LogMessage( 'info', '%d explicit Q vectors generated for shell [%s,%s].' % (len(qVects), qMin, qMax), ['file', 'console']) return qVects, hkls
def __init__(self, *parameters): """ :param parameters: one of 1) three lattice vectors or 2) six numbers: the lengths of the three lattice vectors (a, b, c) followed by the three angles (alpha, beta, gamma). """ if len(parameters) == 6: self.a, self.b, self.c, self.alpha, self.beta, self.gamma = \ parameters e1 = Vector(self.a, 0, 0) e2 = self.b * Vector(N.cos(self.gamma), N.sin(self.gamma), 0.) e3_x = N.cos(self.beta) e3_y = (N.cos(self.alpha)-N.cos(self.beta)*N.cos(self.gamma)) \ / N.sin(self.gamma) e3_z = N.sqrt(1. - e3_x**2 - e3_y**2) e3 = self.c * Vector(e3_x, e3_y, e3_z) self.basis = (e1, e2, e3) elif len(parameters) == 3: assert isVector(parameters[0]) assert isVector(parameters[1]) assert isVector(parameters[2]) self.basis = list(parameters) e1, e2, e3 = self.basis self.a = e1.length() self.b = e2.length() self.c = e3.length() self.alpha = N.arccos(e2 * e3 / (self.b * self.c)) self.beta = N.arccos(e1 * e3 / (self.a * self.c)) self.gamma = N.arccos(e1 * e2 / (self.a * self.b)) else: raise ValueError("Parameter list incorrect") r = LA.inverse(N.transpose([e1, e2, e3])) self.reciprocal_basis = [Vector(r[0]), Vector(r[1]), Vector(r[2])]
import numpy as np from Scientific.Geometry import Vector A = Vector([1,1,1]) #Scientfic a = np.array([1,1,1]) #numpy B = Vector([0.0,1.0,0.0]) print '|A| = ',A.length() #Scientific Python way print '|a| = ',np.sum(a**2)**0.5 #numpy way print '|a| = ',np.linalg.norm(a) #numpy way 2 print 'ScientificPython angle = ',A.angle(B) #in radians print 'numpy angle = ',np.arccos(np.dot(a/np.linalg.norm(a),B/np.linalg.norm(B))) #cross products print 'Scientific A .cross. B = ',A.cross(B) print 'numpy A .cross. B = ',np.cross(A,B) #you can use Vectors in numpy
def pretty_print(self): ''' __str__ function to print the calculator with a nice summary, e.g. jaspsum ''' # special case for neb calculations if self.int_params['images'] is not None: # we have an neb. s = [] s.append(': -----------------------------') s.append(' VASP NEB calculation from %s' % os.getcwd()) try: images, energies = self.get_neb() for i,e in enumerate(energies): s += ['image {0}: {1: 1.3f}'.format(i,e)] except (VaspQueued): s += ['Job is in queue'] return '\n'.join(s) s = [] s.append(': -----------------------------') s.append(' VASP calculation from %s' % os.getcwd()) if hasattr(self,'converged'): s.append(' converged: %s' % self.converged) try: atoms = self.get_atoms() uc = atoms.get_cell() try: self.converged = self.read_convergence() except IOError: # eg no outcar self.converged = False if not self.converged: try: print self.read_relaxed() except IOError: print False if self.converged: energy = atoms.get_potential_energy() forces = atoms.get_forces() else: energy = np.nan forces = [np.array([np.nan, np.nan, np.nan]) for atom in atoms] if self.converged: if hasattr(self,'stress'): stress = self.stress else: stress = None else: stress = None # get a,b,c,alpha,beta, gamma from Scientific.Geometry import Vector A = Vector(uc[0,:]) B = Vector(uc[1,:]) C = Vector(uc[2,:]) a = A.length() b = B.length() c = C.length() alpha = B.angle(C)*180/np.pi beta = A.angle(C)*180/np.pi gamma = B.angle(C)*180/np.pi volume = atoms.get_volume() s.append(' Energy = %f eV' % energy) s.append('\n Unit cell vectors (angstroms)') s.append(' x y z length') s.append(' a0 [% 3.3f % 3.3f % 3.3f] %3.3f' % (uc[0][0], uc[0][1], uc[0][2], A.length())) s.append(' a1 [% 3.3f % 3.3f % 3.3f] %3.3f' % (uc[1][0], uc[1][1], uc[1][2], B.length())) s.append(' a2 [% 3.3f % 3.3f % 3.3f] %3.3f' % (uc[2][0], uc[2][1], uc[2][2], C.length())) s.append(' a,b,c,alpha,beta,gamma (deg): %1.3f %1.3f %1.3f %1.1f %1.1f %1.1f' % (a, b, c, alpha, beta,gamma)) s.append(' Unit cell volume = {0:1.3f} Ang^3'.format(volume)) if stress is not None: s.append(' Stress (GPa):xx, yy, zz, yz, xz, xy') s.append(' % 1.3f % 1.3f % 1.3f % 1.3f % 1.3f % 1.3f' % tuple(stress)) else: s += [' Stress was not computed'] constraints = None if hasattr(atoms, 'constraints'): from ase.constraints import FixAtoms, FixScaled constraints = [[None, None, None] for atom in atoms] for constraint in atoms.constraints: if isinstance(constraint, FixAtoms): for i, constrained in enumerate(constraint.index): if constrained: constraints[i] = [True, True, True] if isinstance(constraint, FixScaled): constraints[constraint.a] = constraint.mask.tolist() if constraints is None: s.append(' Atom# sym position [x,y,z] tag rmsForce') else: s.append(' Atom# sym position [x,y,z] tag rmsForce constraints') for i,atom in enumerate(atoms): rms_f = np.sum(forces[i]**2)**0.5 ts = ' {0:^4d} {1:^4s} [{2:<9.3f}{3:^9.3f}{4:9.3f}] {5:^6d}{6:1.2f}'.format(i, atom.symbol, atom.x, atom.y, atom.z, atom.tag, rms_f) # VASP has the opposite convention of constrained # Think: F = frozen if constraints is not None: ts += ' {0} {1} {2}'.format('F' if constraints[i][0] is True else 'T', 'F' if constraints[i][1] is True else 'T', 'F' if constraints[i][2] is True else 'T') s.append(ts) s.append('--------------------------------------------------') if self.get_spin_polarized() and self.converged: s.append('Spin polarized: Magnetic moment = %1.2f' % self.get_magnetic_moment(atoms)) except AttributeError: # no atoms pass if os.path.exists('INCAR'): # print all parameters that are set self.read_incar() ppp_list = self.get_pseudopotentials() else: ppp_list=[(None, None, None)] s += ['\nINCAR Parameters:'] s += ['-----------------'] for d in [self.int_params, self.float_params, self.exp_params, self.bool_params, self.list_params, self.dict_params, self.string_params, self.special_params, self.input_params]: for key in d: if key is 'magmom': np.set_printoptions(precision=3) value = textwrap.fill(str(d[key]), width=56, subsequent_indent=' '*17) s.append(' %12s: %s' % (key, value)) elif d[key] is not None: value = textwrap.fill(str(d[key]), width=56, subsequent_indent=' '*17) s.append(' %12s: %s' % (key, value)) s += ['\nPseudopotentials used:'] s += ['----------------------'] for sym,ppp,hash in ppp_list: s += ['{0}: {1} (git-hash: {2})'.format(sym,ppp,hash)] # if ibrion in [5,6,7,8] print frequencies if self.int_params['ibrion'] in [5,6,7,8]: freq,modes = self.get_vibrational_modes() s += ['\nVibrational frequencies'] s += ['mode frequency'] s += ['------------------'] for i,f in enumerate(freq): if isinstance(f, float): s += ['{0:4d}{1: 10.3f} eV'.format(i,f)] elif isinstance(f, complex): s += ['{0:4d}{1: 10.3f} eV'.format(i,-f.real)] return '\n'.join(s)
x, V0, e0, B = analyze_eos() except TypeError: x, V0, e0, B = None, None, None, None magmom = atoms.get_magnetic_moment() # now we want to write out a row of data = [id1, composition, spacegroup, natoms, formation_energy, B, magmom] for i, d in enumerate(data): sh0.write(NROWS0, i, d) # now for sheet 1 - unit cell parameters uc = atoms.get_cell() A = Vector(uc[0, :]) B = Vector(uc[1, :]) C = Vector(uc[2, :]) a = A.length() b = B.length() c = C.length() alpha = B.angle(C) * 180.0 / np.pi beta = A.angle(C) * 180.0 / np.pi gamma = B.angle(C) * 180.0 / np.pi volume = atoms.get_volume() stress = atoms.get_stress() if stress is not None: sxx, syy, szz, sxy, syz, sxz = stress else: sxx, syy, szz, sxy, syz, sxz = [None, None, None, None, None, None] data = [id1, a, b, c, alpha, beta, gamma, volume, sxx, syy, szz, sxy, syz, sxz] for i, d in enumerate(data):
magmom = atoms.get_magnetic_moment() # now we want to write out a row of data = [ id1, composition, spacegroup, natoms, formation_energy, B, magmom ] for i, d in enumerate(data): sh0.write(NROWS0, i, d) # now for sheet 1 - unit cell parameters uc = atoms.get_cell() A = Vector(uc[0, :]) B = Vector(uc[1, :]) C = Vector(uc[2, :]) a = A.length() b = B.length() c = C.length() alpha = B.angle(C) * 180. / np.pi beta = A.angle(C) * 180. / np.pi gamma = B.angle(C) * 180. / np.pi volume = atoms.get_volume() stress = atoms.get_stress() if stress is not None: sxx, syy, szz, sxy, syz, sxz = stress else: sxx, syy, szz, sxy, syz, sxz = [ None, None, None, None, None, None ]
# http://books.google.com/books?id=nJHSqEseuIUC&lpg=PA118&ots=YA9TBldoVH # &dq=reciprocal%20metric%20tensor&pg=PA119#v=onepage # &q=reciprocal%20metric%20tensor&f=false '''Finally, many text books on crystallography use long algebraic formulas for computing the d-spacing with sin and cos, vector lengths, and angles. Below we compute these and use them in the general triclinic structure formula which applies to all the structures. ''' from Scientific.Geometry import Vector import math unitcell = ag.get_cell() A = Vector(unitcell[0]) B = Vector(unitcell[1]) C = Vector(unitcell[2]) # lengths of the vectors a = A.length()#*angstroms2bohr b = B.length()#*angstroms2bohr c = C.length()#*angstroms2bohr # angles between the vectors in radians alpha = B.angle(C) beta = A.angle(C) gamma = A.angle(B) print('') print('a b c alpha beta gamma') print('{0:1.3f} {1:1.3f} {2:1.3f} {3:1.3f} {4:1.3f} {5:1.3f}\n'.format(a,b,c, alpha,beta,gamma)) h, k, l = (1, 1, 1) from math import sin, cos id2 = ((h**2 / a**2 * sin(alpha)**2 + k**2 / b**2 * sin(beta)**2 + l**2 / c**2 * sin(gamma)**2 + 2 * k * l / b / c * (cos(beta) * cos(gamma) - cos(alpha))
# http://books.google.com/books?id=nJHSqEseuIUC&lpg=PA118&ots=YA9TBldoVH # &dq=reciprocal%20metric%20tensor&pg=PA119#v=onepage # &q=reciprocal%20metric%20tensor&f=false '''Finally, many text books on crystallography use long algebraic formulas for computing the d-spacing with sin and cos, vector lengths, and angles. Below we compute these and use them in the general triclinic structure formula which applies to all the structures. ''' from Scientific.Geometry import Vector import math unitcell = ag.get_cell() A = Vector(unitcell[0]) B = Vector(unitcell[1]) C = Vector(unitcell[2]) # lengths of the vectors a = A.length() #*angstroms2bohr b = B.length() #*angstroms2bohr c = C.length() #*angstroms2bohr # angles between the vectors in radians alpha = B.angle(C) beta = A.angle(C) gamma = A.angle(B) print print 'a b c alpha beta gamma' print '{0:1.3f} {1:1.3f} {2:1.3f} {3:1.3f} {4:1.3f} {5:1.3f}\n'.format( a, b, c, alpha, beta, gamma) h, k, l = (1, 1, 1) from math import sin, cos id2 = ((h**2 / a**2 * sin(alpha)**2 + k**2 / b**2 * sin(beta)**2 + l**2 / c**2 * sin(gamma)**2 + 2 * k * l / b / c * (cos(beta) * cos(gamma) - cos(alpha)) + 2 * h * l / a / c *