def __init__(self, specs,systemSpecies = None, directory = None): from aBuild.database.crystal import Crystal #Initialize from a dictionary if isinstance(specs,dict): if self._all_present(specs): self.POTCAR = POTCAR(specs["potcars"]) self.KPOINTS = KPOINTS(specs["kpoints"]) if isinstance(specs["crystal"],Crystal): self.crystal = specs["crystal"] else: self.crystal = Crystal(specs["crystal"],specs["species"]) self.handleSpecialTags(specs) self.INCAR = INCAR(specs["incar"]) else: msg.fatal("I don't have all the necessary information to initialize: {}".format(specs.keys())) #Initialize from a path elif isinstance(specs, str): self.POTCAR = POTCAR(path.join(specs,'POTCAR')) self.KPOINTS = KPOINTS(path.join(specs,'KPOINTS')) self.crystal = Crystal(path.join(specs,'POSCAR'),systemSpecies,crystalSpecies = self.POTCAR.species) self.directory = specs else: msg.fatal("Unable to initialize a VASP object from the data that you passed in:", specs) if directory is not None: self.directory = directory
def from_path(directory, species, filesuffix='.static.xz'): from aBuild.database.crystal import Crystal from aBuild.calculators.vasp import POTCAR, KPOINTS #This routine is called when a path is passed in for initialization. # In this case, we want to read all the relevant information from the aflow.in file. from os import path potcar = POTCAR.from_path(path.join(directory, 'POTCAR' + filesuffix)) if potcar is None: potcar = POTCAR.from_path(path.join(directory, 'POTCAR')) kpoints = KPOINTS.from_path(directory) incar = INCAR.from_path(path.join(directory, 'INCAR' + filesuffix)) if incar is None: incar = INCAR.from_path(path.join(directory, 'INCAR')) crystal = Crystal.from_path( path.join(directory, 'POSCAR' + filesuffix), species) if crystal is None: crystal = Crystal.from_path(path.join(directory, 'POSCAR'), species) aflowobj = AFLOW(incar, kpoints, potcar, crystal, None, directory) aflowobj.set_filesuffix(filesuffix) return aflowobj
def __init__(self, specs, systemSpecies, directory=None): from aBuild.database.crystal import Crystal if isinstance(specs, dict): self.INCAR = INCAR(specs["incar"]) self.POTCAR = POTCAR(specs["potcar"]) self.KPOINTS = KPOINTS(specs["kpoints"]) if isinstance(specs["crystal"], Crystal): self.crystal = specs["crystal"] else: self.crystal = Crystal(specs["crystal"], systemSpecies) elif isinstance(specs, str): self.POTCAR = POTCAR(path.join(specs, 'POTCAR')) self.KPOINTS = KPOINTS(path.join(specs, 'KPOINTS')) self.crystal = Crystal(path.join(specs, 'POSCAR'), systemSpecies, crystalSpecies=self.POTCAR.species) self.directory = specs else: msg.fatal( "Unable to initialize a VASP object from the data that you passed in:", specs) if directory is not None: self.directory = directory
def _init_mlp(self, datafile): from aBuild.database.crystal import Crystal import os from os import path from aBuild.calculators.vasp import VASP with open(datafile, 'r') as f: lines = f.readlines() self.crystals = [] nCrystals = 0 # Get information for pures so I can calculate formation energies root = os.getcwd() pures = [ VASP(path.join(root, 'training_set', 'pure' + x), systemSpecies=self.species) for x in self.species ] puresDict = {} for ispec, spec in enumerate(self.species): pures[ispec].read_results() # puresDict[spec] = pures[ispec].crystal.results["energypatom"] print(pures, 'pures') for index, line in enumerate(lines): if 'BEGIN' in line: indexStart = index elif 'END' in line: indexEnd = index structlines = lines[indexStart:indexEnd + 1] print("Processed {} crystals".format(nCrystals)) nCrystals += 1 thisCrystal = Crystal(structlines, self.species, lFormat='mlp') if thisCrystal.results == None: if thisCrystal.minDist > 1.5: self.crystals.append(thisCrystal) else: thisCrystal.results["fEnth"] = thisCrystal.results[ "energyF"] / thisCrystal.nAtoms - sum([ pures[i].crystal.results["energyF"] / pures[i].crystal.nAtoms * thisCrystal.concentrations[i] for i in range(thisCrystal.nTypes) ]) if thisCrystal.results[ "energyF"] < 100 and thisCrystal.minDist > 1.5: self.crystals.append(thisCrystal) else: print( "Not adding structure {}. Seems like an extreme one." .format(thisCrystal.title)) print("Energy: {}".format( thisCrystal.results["energyF"])) print("MinDist: {}".format(thisCrystal.minDist))
def build_relax_select_input(self): from os import remove, path from aBuild.enumeration import Enumerate from aBuild.database.crystal import Crystal from aBuild.fitting.mtp import MTP from aBuild.utility import unpackProtos, getAllPerms from glob import glob fittingRoot = path.join(self.root, 'fitting', 'mtp') for ilat in range(self.nEnums): lat = self.enumDicts[ilat]["lattice"] enumLattice = Enumerate(self.enumDicts[ilat]) if lat == 'protos': structures = getProtoPaths() for struct in structures: scrambleOrder = getAllPerms(self.knary, justCyclic='uniqueUnaries' in struct) for scramble in scrambleOrder: thisCrystal = Crystal(struct, species=self.species) thisCrystal.scrambleAtoms(scramble) thisMTP = MTP(fittingRoot, dataSet=[thisCrystal], forRelax=True) with open(path.join(fittingRoot, 'to-relax.cfg'), 'a+') as f: f.writelines(thisMTP.lines) else: for struct in range(1, enumLattice.nEnumStructs + 1): enumLattice.generatePOSCAR(struct) thisCrystal = Crystal.fromPOSCAR( enumLattice.root, self.species, filename="poscar.{}.{}".format(lat, struct), title=' '.join([lat, " str #: {}"]).format(struct)) thisMTP = MTP(fittingRoot, dataSet=[thisCrystal], forRelax=True) with open(path.join(fittingRoot, 'to-relax.cfg'), 'a+') as f: f.writelines(thisMTP.lines) delpath = path.join(enumLattice.root, "poscar.{}.{}".format(lat, struct)) remove(delpath) thisMTP.write_relaxin()
def from_path(folderpath, species): from aBuild.database.crystal import Crystal incarobj = INCAR.from_path(path.join(folderpath, 'INCAR')) potcarobj = POTCAR.from_path(path.join(folderpath, 'POTCAR')) kpointsobj = KPOINTS.from_path(path.join(folderpath, 'KPOINTS')) crystal = Crystal.from_path(path.join(folderpath, 'POSCAR'), species) return VASP(incarobj, kpointsobj, potcarobj, crystal, folderpath)
def init_file(self, datafile, systemSpecies, linesformat): from aBuild.database.crystal import Crystal possibleFiles = { 'new_training.cfg': 'mlpadd', 'train.cfg': 'mlptrain', 'structures.in': 'ce' } #selectedFile = path.join(self.root,'new_training.cfg') with open(datafile, 'r') as f: lines = f.readlines() self.crystals = [] nCrystals = 0 for index, line in enumerate(lines): if line == 'BEGIN_CFG\n': # nCrystals += 1 #if numOfStructs is not 'all' and (nCrystals < start or nCrystals > start + numOfStructs): # continue nAtoms = int(lines[index + 2].split()[0]) structlines = lines[index:index + 11 + nAtoms] thisCrystal = Crystal(structlines, systemSpecies, lFormat=linesformat) self.crystals.append(thisCrystal)
def setup_relax_select_input(self): from os import remove, path from aBuild.enumeration import Enumerate from aBuild.database.crystal import Crystal from aBuild.fitting.mtp import MTP from aBuild.utility import unpackProtos, getAllPerms, getProtoPaths from glob import glob self.dataset = "gss" # rename(path.join(self.root,'fitting/mtp') fittingRoot = path.join(self.root, 'fitting', 'mtp') for ilat in range(self.nEnums): lat = self.enumDicts[ilat]["lattice"] if lat == 'protos': structures = getProtoPaths() # subdivide = [structures[x:x+100] for x in range() ] for struct in structures: print("Proto structure:", struct) scrambleOrder = getAllPerms(self.knary, justCyclic='uniqueUnaries' in struct) for scramble in scrambleOrder: thisCrystal = Crystal(struct, self.species) #print("Atom counts before scramble {}".format(thisCrystal.atom_counts)) thisCrystal.scrambleAtoms(scramble) #print("Atom counts after scramble {}".format(thisCrystal.atom_counts)) with open(path.join(fittingRoot, 'to-relax.cfg'), 'a+') as f: f.writelines('\n'.join( thisCrystal.lines('mtprelax'))) else: enumLattice = Enumerate(self.enumDicts[ilat]) for struct in range(1, enumLattice.nConfigs + 1): print("Lattice", lat, "structure:", struct) enumLattice.generatePOSCAR(struct) thisCrystal = Crystal( path.join(enumLattice.root, "poscar.{}.{}".format(lat, struct)), self.species) with open(path.join(fittingRoot, 'to-relax.cfg'), 'a+') as f: f.writelines('\n'.join(thisCrystal.lines('mtprelax'))) delpath = path.join(enumLattice.root, "poscar.{}.{}".format(lat, struct)) remove(delpath) fittingRoot = path.join(self.root, 'fitting', 'mtp') thisMTP = MTP(fittingRoot, settings=self.fitting) thisMTP.calc_grade() thisMTP.write_relaxin()
def __init__(self, specs,systemSpecies,directory = None): #crystal,potential, from aBuild.database.crystal import Crystal # self.crystal = crystal self.species = systemSpecies # self.potential = potential if isinstance(specs,dict): self.crystal = specs["crystal"] self.potential = specs["potential"] elif isinstance(specs, str): self.crystal = Crystal(path.join(specs,'input.in'),systemSpecies) self.settings = self.parse_lammps_settings(specs) self.directory = specs
def from_dictionary(specsDict): from aBuild.database.crystal import Crystal specsDict["potcar"]["build"] = "manual" specsDict["potcar"]["directory"] = None potcarobj = POTCAR(specsDict["potcar"]) kpointsobj = KPOINTS(specsDict["kpoints"]) if isinstance(specsDict["crystal"], Crystal): crystal = specsDict["crystal"] else: print("This just happened!!!") crystal = Crystal(specsDict["crystal"]) specsDict["incar"][ "nAtoms"] = crystal.nAtoms #for ensuring MAGMOM tag is correct incarobj = INCAR(specsDict["incar"]) return VASP(incarobj, kpointsobj, potcarobj, crystal, specsDict["directory"])
def init_enum(enumdicts, systemSpecies): from aBuild.enumeration import Enumerate from aBuild.calculators.vasp import VASP from aBuild.database.crystal import Crystal from aBuild.jobs import Job from random import randrange from aBuild.utility import chdir from numpy import array from os import remove, path # from crystal import Crystal from os import path import os print("Building database from enumerations") crystals = [] # configIndex = startPoint = self._starting_point for eDict in enumdicts: enumController = Enumerate(eDict) if enumController.nEnumStructs == 0: msg.warn( 'There are no enumerated structures for lattice type {}. Not building any VASP folders for them.' .format(eDict["lattice"])) enumController.buildInputFile() enumController.enumerate() # Loop to generate random structures for a given lattice type for i in range(eDict["nconfigs"]): print('Adding {} structure # {} to database'.format( eDict["lattice"], rStruct)) with open('structNums', 'a+') as f: f.write(eDict["name"] + ' ' + str(i) + '\n') enumController.generatePOSCAR(i) poscarpath = path.join( enumController.root, "poscar.{}.{}".format(eDict["name"], rStruct)) thisCrystal = Crystal.from_poscar( poscarpath, systemSpecies ) #title = ' '.join([self.enumDicts[index]["lattice"]," str #: {}"]).format(rStruct) crystals.append(thisCrystal) delpath = path.join( enumController.root, "poscar.{}.{}".format(eDict["name"], rStruct)) remove(delpath) return dataset(crystals)
def randomDisplacements(self, POSCAR): from os import path from aBuild.database.crystal import Crystal toRelax = path.join(self.root, 'fitting', 'mtp', 'to_relax.cfg') for i in range(1000): thisCrystal = Crystal(POSCAR, systemSpecies=self.species) thisCrystal.randomDisplace() print(thisCrystal.lines('mtprelax')) with open(toRelax, 'a+') as f: f.writelines('\n'.join(thisCrystal.lines('mtprelax')))
def init_enum(self, enumdicts, systemSpecies, runGetKpoints=True): from aBuild.enumeration import Enumerate from aBuild.calculators.vasp import VASP from aBuild.database.crystal import Crystal from aBuild.jobs import Job from random import randrange from aBuild.utility import chdir # from crystal import Crystal from os import path import os # if not path.isdir(self.root): # os.mkdir(self.root) print("Building database from enumerations") self.crystals = [] # configIndex = startPoint = self._starting_point for eDict in enumdicts: enumController = Enumerate(eDict) if enumController.nEnumStructs == 0: msg.warn( 'There are no enumerated structures for lattice type {}. Not building any VASP folders for them.' .format(self.enumDicts[index]["lattice"])) enumController.buildInputFile() enumController.enumerate() # Loop to generate random structures for a given lattice type for i in range(eDict["nconfigs"]): rStruct = randrange(1, enumController.nEnumStructs) print('Adding {} structure # {} to database'.format( eDict["lattice"], rStruct)) with open('structNums', 'a+') as f: f.write(eDict["lattice"] + ' ' + str(rStruct) + '\n') #print("Building VASP folder for {} structure #: {}".format(eDict["lattice"],rStruct)) enumController.generatePOSCAR(rStruct) poscarpath = path.join( enumController.root, "poscar.{}.{}".format(eDict["lattice"], rStruct)) thisCrystal = Crystal( poscarpath, systemSpecies=systemSpecies ) #title = ' '.join([self.enumDicts[index]["lattice"]," str #: {}"]).format(rStruct) self.crystals.append(thisCrystal)
class VASP: """Class to handle all of the VASP input and output files. Args: root (str): Path to the calculation folder incar (dict): Dictionary containing the INCAR tags to be used potcars (dict): Dictionary containing the necessary settings to find the correct POTCARS. <directory> : where the potcars are located <> kpoints (dict): KPOINTS settings crystal (CRYSTAL OBJ): Crystal description """ def __init__(self, specs, systemSpecies, directory=None): from aBuild.database.crystal import Crystal if isinstance(specs, dict): self.INCAR = INCAR(specs["incar"]) self.POTCAR = POTCAR(specs["potcar"]) self.KPOINTS = KPOINTS(specs["kpoints"]) if isinstance(specs["crystal"], Crystal): self.crystal = specs["crystal"] else: self.crystal = Crystal(specs["crystal"], systemSpecies) elif isinstance(specs, str): self.POTCAR = POTCAR(path.join(specs, 'POTCAR')) self.KPOINTS = KPOINTS(path.join(specs, 'KPOINTS')) self.crystal = Crystal(path.join(specs, 'POSCAR'), systemSpecies, crystalSpecies=self.POTCAR.species) self.directory = specs else: msg.fatal( "Unable to initialize a VASP object from the data that you passed in:", specs) if directory is not None: self.directory = directory def check_atom_counts_zero(self): from numpy import array if any(self.crystal.atom_counts == 0): print( 'FOUND ZEROS in atom_counts <--------------------------------------------', self.crystal.atom_counts) from numpy import where idxKeep = list(where(self.crystal.atom_counts > 0)[0]) print(idxKeep) print(self.POTCAR.species, 'BEFORe') self.POTCAR.species = list(array(self.POTCAR.species)[idxKeep]) print(self.POTCAR.species, 'AFTER') self.crystal.atom_counts = self.crystal.atom_counts[idxKeep] print(self.crystal.atom_counts) def _check_tag_exists(self, file, tag): from aBuild.utility import grep lines = grep(file, tag) if lines == []: print("Returning False") return False else: return True def _check_file_exists(self, file): files = os.listdir('./') if file in files: return True else: return False def status(self): from os import path from time import time from aBuild.utility import grep import os ctime = time() with chdir(self.directory): outcar = self._check_file_exists('OUTCAR') incar = self._check_file_exists('INCAR') kpoints = self._check_file_exists('KPOINTS') potcar = self._check_file_exists('POTCAR') poscar = self._check_file_exists('POSCAR') if not (incar and kpoints and potcar and poscar): return 'not setup' if incar: relax = grep('INCAR', 'IBRION') if '-1' not in relax or relax is []: static = True else: static = False else: msg.warn("No INCAR found. That seems odd..") outcarfinishtags = self._check_tag_exists( 'OUTCAR', '------------------------ aborting loop because EDIFF is reached ----------------------------------------\n' ) or self._check_tag_exists('OUTCAR', ' writing wavefunctions') if outcar: time = path.getmtime('OUTCAR') sgrcon = grep('OUTCAR', 'SGRCON') if (ctime - time) < 60: folderstat = 'running' else: if static and self._check_tag_exists( 'OUTCAR', '------------------------ aborting loop because EDIFF is reached ----------------------------------------\n' ): print('Tripped 1') folderstat = 'done' elif self._check_tag_exists('OUTCAR', ' writing wavefunctions'): print('Tripped 2') folderstat = 'done' else: folderstat = 'running' else: folderstat = 'not started' # if outcar: #if 'OUTCAR' in files: print("Folder Stat {}".format(folderstat)) return folderstat @staticmethod def from_file(runpath): incar = INCAR.from_file(runpath) kpoint = KPOINTS.from_file(runpath) potcars = POTCARS.from_file(runpath) result = VASP(runpath=runpath, incar=incar, kpoints=kpoints, potcars=potcars) return result def buildFolder(self, runGetKPoints=True): from aBuild.calculators.vasp import POSCAR self.KPOINTS.rGP = runGetKPoints self.INCAR.writeINCAR() print("INCAR built") self.crystal.write('POSCAR_orig') print("POSCAR_orig built") self.check_atom_counts_zero() self.crystal.write('POSCAR') print("POSCAR built") self.KPOINTS.writeKPOINTS() print("KPOINTS built") self.POTCAR.writePOTCAR() print("POTCAR built") def read_forces(self, allIonic=True): with open('POSCAR', 'r') as file: poslines = file.readlines() nAtoms = sum([int(i) for i in poslines[5].split()]) with open('OUTCAR', 'r') as file: lines = file.readlines() n = 0 if allIonic: forces = [] n = 0 for line in lines: if line.rfind('TOTAL-FORCE') > -1: singleItForces = [] for i in range(nAtoms): singleItForces.append( np.array( [float(f) for f in lines[n + i + 2].split()[3:6]])) msg.info('Found forces for {} atoms.'.format(nAtoms)) if not '--' in lines[n + nAtoms + 2]: print(lines[n + nAtoms + 2]) msg.fatal( 'It appears that there are forces for more atoms than I was expecting!' ) if allIonic: forces.append(singleItForces) n += 1 if not allIonic: forces = singleItForces if allIonic and len(forces) == 1: return forces[0] return forces def read_fermi(self): """Method that reads Fermi energy from OUTCAR file""" E_f = None for line in open('OUTCAR', 'r'): if line.rfind('E-fermi') > -1: E_f = float(line.split()[2]) return E_f def read_nbands(self): for line in open('OUTCAR', 'r'): line = self.strip_warnings(line) if line.rfind('NBANDS') > -1: nBands = int(line.split()[-1]) return nBands def read_energy(self, allElectronic=False): energyZ = None energyF = None if allElectronic: energyF = [] energyZ = [] for line in open('OUTCAR', 'r'): # Free energy if line.lower().startswith(' free energy toten') or line.lower( ).startswith(' free energy toten'): if allElectronic: energyF.append(float(line.split()[-2])) else: energyF = float(line.split()[-2]) # Extrapolated zero point energy if line.startswith(' energy without entropy'): if allElectronic: energyZ.append(float(line.split()[-1])) else: energyZ = float(line.split()[-1]) return energyF, energyZ def read_stress(self): stress = None for line in open('OUTCAR'): if line.find(' in kB ') != -1: stress = -np.array([float(a) for a in line.split()[2:]]) stress = stress[[ 0, 1, 2, 4, 5, 3 ]] * 1e-1 * .00624151 # * ase.units.GPa.. Gets me to Giga Pascals return stress def read_results(self, allElectronic=False, allIonic=False): print(self.directory, 'dir') if self.directory is not None and self.status() is 'done': with chdir(self.directory): #print(self.directory) self.crystal.results = {} self.crystal.results["energyF"], self.crystal.results[ "energyZ"] = self.read_energy(allElectronic=allElectronic) self.crystal.results["forces"] = self.read_forces( allIonic=allIonic) self.crystal.results["stress"] = self.read_stress() #self.POTCAR = POTCAR.from_POTCAR() self.crystal.results["species"] = self.POTCAR.species else: self.crystal.results = None msg.info( "Unable to extract necessary information from directory! ({})". format(self.directory))
def _init_mlp(datafile, species): from aBuild.database.crystal import Crystal import os from os import path from aBuild.calculators.vasp import VASP from aBuild.calculators.aflow import AFLOW with open(datafile, 'r') as f: lines = f.readlines() crystals = [] nCrystals = 0 # Get information for pures so I can calculate formation energies root = os.getcwd() trainingRoot = path.join(root, 'training_set') puredirs = [path.join(trainingRoot, 'pure' + x) for x in species] pures = [ AFLOW.from_path(x, species, filesuffix='.relax2.xz') for x in puredirs ] for pure in pures: pure.read_results() # End reading pures count = 0 for index, line in enumerate(lines): if 'BEGIN' in line: indexStart = index elif 'END' in line: count += 1 indexEnd = index structlines = lines[indexStart:indexEnd + 1] if count % 1000 == 0: print("Processed {} crystals".format(nCrystals)) nCrystals += 1 thisCrystal = Crystal.from_lines(structlines, species, 'mlp') # Only add the crystal if mindist is reasonable if thisCrystal.minDist > 1.5: # Only calculate formation energy if I have information about the pures if True not in [ x.crystal is None or x.crystal.results is None or thisCrystal.results["energyF"] is None for x in pures ]: thisCrystal.results["fEnth"] = thisCrystal.results[ "energyF"] / thisCrystal.nAtoms - sum([ pures[i].crystal.results["energyF"] / pures[i].crystal.nAtoms * thisCrystal.concentrations[i] for i in range(thisCrystal.nTypes) ]) # Otherwise, set it to a ridiculus number else: thisCrystal.results["fEnth"] = 1000 thisCrystal.results["distToHull"] = None # Save the crystal for later. crystals.append(thisCrystal) else: msg.warn( "Mindist is pretty small for this one, so I'm not gonna add it" ) return dataset(crystals)
def _init_dataReport(datafile, species, onlyCloseToHull=False, cutoff=5e-3, getCrystallographicInfo=False, enums=None): if getCrystallographicInfo and enumDicts == None: msg.fatal( "Can't extract crystallographic information without the enumeration dictionaries" ) with open(datafile, 'r') as f: lines = f.readlines() del lines[:4] required = [ "lattice", "basis", "atom_types", "crystalSpecies", "latpar", "coordsys", "systemSpecies" ] nAtoms = int((len(lines[0].split()) - 9) / 2) lookup = {} for enum in enums: lookup[enum.lattice.lattice_name] = enum # In case we don't want the full crystallographic information, # let's build an empty crystal object that we can fill up with # the information that we want. templateDict = {} for key in required: templateDict[key] = None templateDict["results"] = {} # crystal = Crystal(templateDict) count = 0 crystals = [] for line in lines: formationEnthalpy = float(line.split()[8]) energyF = float(line.split()[6]) title = ' '.join(line.split()[:6]) atomCounts = list(map(int, line.split()[-nAtoms:])) lattice = str(line.split()[1].split('_')[1]) structNum = int(line.split()[5]) if len(line.split()) == 16: distanceToHull = float(line.split()[9]) else: distanceToHull = None if onlyCloseToHull: if distanceToHull > cutoff: continue if getCrystallographicInfo: crystal = Crystal.fromEnum(lookup[lattice], structNum, species) crystal.results["fEnth"] = formationEnthalpy crystal.results["energyF"] = energyF crystal.results["distToHull"] = distanceToHull else: templateDict["fEnth"] = formationEnergy templateDict["energyF"] = energyF templateDict["distanceToHull"] = distanceToHull templateDict["title"] = title templateDict["atom_counts"] = atomCounts templateDict["crystalSpecies"] = species crystal = Crystal(templateDict) count += 1 print("Read in crystal {}".format(count)) crystals.append(crystal) return dataset(crystals)
if not path.isfile(dataFile): msg.fatal('data file does not exist') data = dataset(dataFile,self.species) <<<<<<< HEAD data.generateConvexHullPlot(plotAll = plotAll) ======= data.generateConvexHullPlot() ############################################################ #generate a .cif file from the POSCAR provided def generate_cif(self,groundStatePoscar): from os import path from aBuild.database.crystal import Crystal #generate the .cif file input = Crystal(groundStatePoscar,self.species) input.generate_cif() ############################################################ >>>>>>> 03893ba98eddd6991de841e54e88613fa8b4165d def errorsReport(self,datafile = None, predictFile = None): from aBuild.database.dataset import dataset from numpy.linalg import norm from numpy import average,array dataSet = dataset(datafile,self.species,lFormat = 'mlp') predictSet = dataset(predictFile,self.species,lFormat = 'mlp') diffsEnergy = [] diffsForces = [] for i in dataSet.crystals:
def build_ToRelax(self, enumDicts, species, AFM=False, start=1, end=None): from aBuild.enumeration import Enumerate from aBuild.utility import unpackProtos, getAllPerms, getProtoPaths from aBuild.database.crystal import Crystal from os import remove, path print('Building to-relax.cfg') print(enumDicts) nEnums = len(enumDicts) knary = len(species) for ilat in range(nEnums): lat = enumDicts[ilat]["name"] if lat == 'protos': structures = getProtoPaths(knary) for struct in structures: print("Proto structure:", struct) scrambleOrder = getAllPerms(knary, justCyclic='uniqueUnaries' in struct) for scramble in scrambleOrder: thisCrystal = Crystal(struct, species) #print("Atom counts before scramble {}".format(thisCrystal.atom_counts)) thisCrystal.scrambleAtoms(scramble) if not thisCrystal.concsOK( concRestrictions=enumDicts[ilat]["concs"]): continue print(thisCrystal.title) mindist = thisCrystal.minDist print( mindist, "actual min dist<-----------------------------------------------------------------------------------------------------" ) if mindist > 2 and thisCrystal.nAtoms < 60: if not AFM: print('Adding to file') #print("Atom counts after scramble {}".format(thisCrystal.atom_counts)) with open(path.join(self.root, 'to-relax.cfg'), 'a+') as f: f.writelines('\n'.join( thisCrystal.lines('mtprelax'))) elif thisCrystal.getAFMPlanes([1, 0, 0]): print("Original Crystal is AFM compatible") with open( path.join(self.root, 'to-relax.cfg_' + str(start)), 'a+') as f: f.writelines('\n'.join( thisCrystal.lines('mtprelax'))) # break else: print("Checking super-periodics") superCrystal = thisCrystal.superPeriodics(2) if superCrystal != []: print('Found a super-Periodic that works') print(superCrystal.basis, 'basis') print(superCrystal.atom_counts, 'atom counts') with open( path.join( self.root, 'to-relax.cfg_' + str(start)), 'a+') as f: f.writelines('\n'.join( superCrystal.lines('mtprelax'))) # break else: enumLattice = Enumerate(enumDicts[ilat]) print(end, 'end') if end == None: end = enumLattice.nConfigs + 1 filetag = '' else: filetag = '_' + str(start) #for struct in range(enumLattice.nConfigs+1): for struct in range(start, end): print("Lattice", lat, "structure:", struct) enumLattice.generatePOSCAR(struct) thisCrystal = Crystal( path.join(enumLattice.root, "poscar.{}.{}".format(lat, struct)), species) if not AFM: print('Writing to file!!!!!', end) with open( path.join(self.root, 'to_relax.cfg' + filetag), 'a+') as f: f.writelines('\n'.join( thisCrystal.lines('mtprelax'))) elif thisCrystal.getAFMPlanes([1, 0, 0]): print("Original Crystal is AFM compatible") with open( path.join(self.root, 'to_relax.cfg' + filetag), 'a+') as f: f.writelines('\n'.join( thisCrystal.lines('mtprelax'))) else: print("Checking super-periodics") superCrystal = thisCrystal.superPeriodics(2) if superCrystal != []: print('Found a super-Periodic that works') with open( path.join(self.root, 'to_relax.cfg' + filetag), 'a+') as f: f.writelines('\n'.join( superCrystal.lines('mtprelax'))) # print(thisCrystal.appMinDist,' approp Min Dist') # print(thisCrystal.minDist, 'actual min dist') # with open(path.join(self.root,'to-relax.cfg'),'a+') as f: # f.writelines('\n'.join(thisCrystal.lines('mtprelax') )) delpath = path.join(enumLattice.root, "poscar.{}.{}".format(lat, struct)) remove(delpath) end = None
def init_enum(self, enumdicts, systemSpecies, runGetKpoints=True): from aBuild.enumeration import Enumerate from aBuild.calculators.vasp import VASP from aBuild.database.crystal import Crystal from aBuild.jobs import Job from random import randrange from aBuild.utility import chdir from numpy import array from os import remove, path # from crystal import Crystal from os import path import os # if not path.isdir(self.root): # os.mkdir(self.root) print("Building database from enumerations") self.crystals = [] # configIndex = startPoint = self._starting_point for eDict in enumdicts: enumController = Enumerate(eDict) if enumController.nEnumStructs == 0: msg.warn( 'There are no enumerated structures for lattice type {}. Not building any VASP folders for them.' .format(self.enumDicts[index]["lattice"])) enumController.buildInputFile() enumController.enumerate() # Loop to generate random structures for a given lattice type for i in range(eDict["nconfigs"]): rStruct = 16254 #randrange(1,enumController.nEnumStructs) print('Adding {} structure # {} to database'.format( eDict["lattice"], rStruct)) with open('structNums', 'a+') as f: f.write(eDict["name"] + ' ' + str(rStruct) + '\n') #print("Building VASP folder for {} structure #: {}".format(eDict["lattice"],rStruct)) enumController.generatePOSCAR(rStruct) poscarpath = path.join( enumController.root, "poscar.{}.{}".format(eDict["name"], rStruct)) thisCrystal = Crystal( poscarpath, systemSpecies=systemSpecies ) #title = ' '.join([self.enumDicts[index]["lattice"]," str #: {}"]).format(rStruct) if self.restrictions is None: self.crystals.append(thisCrystal) elif thisCrystal.getAFMPlanes([1, 0, 0]): print('parent is AFM compatible') self.crystals.append(thisCrystal) import sys sys.exit() else: superCrystal = thisCrystal.superPeriodics(2) if superCrystal != []: print('super periodic structures is AFM compatible') print(superCrystal.minDist, 'minDist') print(superCrystal.basis, ' basis') print(array(superCrystal.Bv_direct), 'direct') print(array(superCrystal.Bv_cartesian), 'cartesian') self.crystals.append(superCrystal) import sys sys.exit() else: print("Can't find an AFM compatible structure") import sys sys.exit() # self.crystals.append(thisCrystal) delpath = path.join( enumController.root, "poscar.{}.{}".format(eDict["name"], rStruct)) remove(delpath)
class VASP: """Class to handle all of the VASP input and output files. Args: specs (dict or str): Either a dictionary containing all of the necessary settings or a path to a folder that contains all of the files needed. root (str): Path to the calculation folder incar (dict): Dictionary containing the INCAR tags to be used potcars (dict): Dictionary containing the necessary settings to find the correct POTCARS. <directory> : where the potcars are located <> kpoints (dict): KPOINTS settings crystal (CRYSTAL OBJ): Crystal description """ def __init__(self, specs,systemSpecies = None, directory = None): from aBuild.database.crystal import Crystal #Initialize from a dictionary if isinstance(specs,dict): if self._all_present(specs): self.POTCAR = POTCAR(specs["potcars"]) self.KPOINTS = KPOINTS(specs["kpoints"]) if isinstance(specs["crystal"],Crystal): self.crystal = specs["crystal"] else: self.crystal = Crystal(specs["crystal"],specs["species"]) self.handleSpecialTags(specs) self.INCAR = INCAR(specs["incar"]) else: msg.fatal("I don't have all the necessary information to initialize: {}".format(specs.keys())) #Initialize from a path elif isinstance(specs, str): self.POTCAR = POTCAR(path.join(specs,'POTCAR')) self.KPOINTS = KPOINTS(path.join(specs,'KPOINTS')) self.crystal = Crystal(path.join(specs,'POSCAR'),systemSpecies,crystalSpecies = self.POTCAR.species) self.directory = specs else: msg.fatal("Unable to initialize a VASP object from the data that you passed in:", specs) if directory is not None: self.directory = directory def _all_present(self,specs): required = ["incar","potcars","kpoints","crystal","species"] for tag in required: if tag not in specs.keys(): return False return True def handleSpecialTags(self,specs): special = ["AFM","FM"] if "FM" in specs.keys(): specs["incar"]["ispin"] = 2 specs["incar"]["magmom"] = '' for idx,species in enumerate(sorted(specs["FM"],reverse = True)): specs["incar"]["magmom"] += ' '.join(map(str, [ specs["FM"][species] ] * self.crystal.atom_counts[idx])) specs["incar"]["magmom"] += ' ' elif "AFM" in specs.keys(): if self.crystal.AFMPlanes == None: self.crystal.getAFMPlanes([1,0,0]) if self.crystal.AFMPlanes == None: msg.info("You supposedly had an AFM crystal, but I'm not finding the planes") return specs["incar"]["ispin"] = 2 #Put in nonzero spin values specs["incar"]["magmom"] = ' '.join(map(str,self.crystal.AFMPlanes)) + ' ' atomsLeft = self.crystal.nAtoms - len(self.crystal.AFMPlanes) specs["incar"]["magmom"] += ' '.join(map(str,[0 for x in range(atomsLeft)])) # VASP does not like to have zeros in the atom_counts list # but I want to keep track of which atoms are in the crystal. # This routine is just here to remove any zeros before I write to # the POSCAR file. def check_atom_counts_zero(self): from numpy import array,any print(self.crystal.atom_counts, 'atom counts') print(any(self.crystal.atom_counts == 0)) if any(self.crystal.atom_counts == 0): from numpy import where idxKeep = list(where( self.crystal.atom_counts > 0)[0]) self.POTCAR.species = list(array(self.POTCAR.species)[idxKeep]) self.crystal.atom_counts = self.crystal.atom_counts[idxKeep] def _check_tag_exists(self,file,tag): from aBuild.utility import grep lines = grep(file,tag) if lines == []: return False else: return True def _check_file_exists(self,file): files = os.listdir('./') if file in files: return True else: return False def status(self): from os import path from time import time from aBuild.utility import grep import os fTagStatic = '------------------------ aborting loop because EDIFF is reached ----------------------------------------\n' fTagRelax = ' writing wavefunctions' ctime = time() print('checking directory {}'.format(self.directory)) with chdir(self.directory): outcar = self._check_file_exists('OUTCAR') incar = self._check_file_exists('INCAR') kpoints = self._check_file_exists('KPOINTS') potcar = self._check_file_exists('POTCAR') poscar = self._check_file_exists('POSCAR') output = self._check_file_exists('vasp_output') oszicar = self._check_file_exists('OSZICAR') inputs = incar and kpoints and potcar and poscar ''' Check to see if the input files are present if they aren't, no need to proceed, just return 'not setup' ''' if not inputs: return 'not setup' ''' If the OUTCAR file is present, we know that we're either running, finished successfully, or finished with errors! ''' elif outcar: # OUTCAR present sgrcon = grep('vasp_output','SGRCON') tooclose = grep('vasp_output','HOPE') finalenergyline = grep('OUTCAR','free energy') generalerror = grep('vasp_output','ERROR') # Check to make sure I've converged electonically. if grep('OSZICAR','DAV:') != []: electronicIteration = int(grep('OSZICAR','DAV:')[-1].split()[1]) else: electronicIteration = 0 if grep('INCAR','nsw') != []: nsw = int(grep('INCAR','nsw')[0].split('=')[1]) if nsw == 0: nsw = 1 else: nsw = 1 if grep('OSZICAR','F=') != []: ionicIteration = int(grep('OSZICAR','F=')[-1].split()[0]) else: ionicIteration = 1 if grep('INCAR','nelm') != []: maxelectronic = grep('INCAR','nelm')[0].split('=')[1] else: maxelectronic = 60 if ionicIteration == nsw and int(electronicIteration) == int(maxelectronic): return 'unconverged' ''' Let's first check to see if this is a static calculation or a relaxation because the tag to check for is different.''' if incar: relax = grep('INCAR','IBRION') if '-1' not in relax or relax is []: static = True else: static = False else: return 'not setup' ''' Check finish tag for static calc''' if static and self._check_tag_exists('OUTCAR', fTagStatic): #finish tag found if finalenergyline != []: #Let's double check return 'done' else: # Apparently not, why? return 'idk' ''' Check finish tag for relax calc''' elif self._check_tag_exists('OUTCAR',fTagRelax): #Looks like it's done if finalenergyline != []: # Let's double check return 'done' else: # Apparently not, why? return 'idk' else: ''' Check how long since the last file write. If it was recent then we're probably running.''' time = path.getmtime('OUTCAR') if (ctime - time) < 3600: # If the OUTCAR was modified in the last hour # the calculation is probably still running. return 'running' elif sgrcon: return 'sgrcon' elif generalerror: return 'error' elif tooclose: return 'warning' else: return 'too long' else: return 'not started' if output: warning = grep('output','RRRRR') != [] or grep('output','AAAAAA') != [] if warning: return 'warning' return folderstat @staticmethod def from_file(runpath): incar = INCAR.from_file(runpath) kpoint = KPOINTS.from_file(runpath) potcars = POTCARS.from_file(runpath) result = VASP(runpath = runpath,incar=incar,kpoints=kpoints,potcars=potcars) return result def buildFolder(self,runGetKPoints = True): from aBuild.calculators.vasp import POSCAR self.KPOINTS.rGP = runGetKPoints self.INCAR.writeINCAR() print("INCAR built") self.crystal.write('POSCAR_orig') print("POSCAR_orig built") self.check_atom_counts_zero() self.crystal.write('POSCAR') print("POSCAR built") self.KPOINTS.writeKPOINTS() print("KPOINTS built") self.POTCAR.writePOTCAR() #print("POTCAR built") def read_forces(self,allIonic = True): with open('POSCAR','r') as file: poslines = file.readlines() if any(c.isalpha() for c in poslines[5].strip()): #It's a CONTCAR nAtoms = sum([int(i) for i in poslines[6].split()]) else: nAtoms = sum([int(i) for i in poslines[5].split()]) with open('OUTCAR', 'r') as file: lines = file.readlines() n = 0 if allIonic: forces = [] n = 0 found = False for line in lines: if line.rfind('TOTAL-FORCE') > -1: found = True singleItForces = [] for i in range(nAtoms): singleItForces.append(np.array([float(f) for f in lines[n + i + 2].split()[3:6]])) msg.info('Found forces for {} atoms.'.format(nAtoms)) if not '--' in lines[n+nAtoms + 2]: msg.fatal('It appears that there are forces for more atoms than I was expecting!') if allIonic: forces.append(singleItForces) n+=1 if not found: msg.info("Couldn't find forces for this calc") return None if not allIonic: forces = singleItForces if allIonic and len(forces) == 1: return forces[0] return forces def read_fermi(self): """Method that reads Fermi energy from OUTCAR file""" E_f = None for line in open('OUTCAR', 'r'): if line.rfind('E-fermi') > -1: E_f = float(line.split()[2]) return E_f def read_nbands(self): for line in open('OUTCAR', 'r'): line = self.strip_warnings(line) if line.rfind('NBANDS') > -1: nBands = int(line.split()[-1]) return nBands def read_energy(self, allElectronic=False): energyZ = None energyF = None if allElectronic: energyF = [] energyZ = [] for line in open('OUTCAR', 'r'): # Free energy if line.lower().startswith(' free energy toten') or line.lower().startswith(' free energy toten'): if allElectronic: energyF.append(float(line.split()[-2])) else: energyF = float(line.split()[-2]) # Extrapolated zero point energy if line.startswith(' energy without entropy'): if allElectronic: energyZ.append(float(line.split()[-1])) else: energyZ = float(line.split()[-1]) return energyF,energyZ def read_stress(self): stress = None for line in open('OUTCAR'): if line.find(' in kB ') != -1: stress = -np.array([float(a) for a in line.split()[2:]]) stress = stress[[0, 1, 2, 4, 5, 3]] * 1e-1 * .00624151# * ase.units.GPa.. Gets me to Giga Pascals return stress def read_results(self, allElectronic = False, allIonic=False): if self.directory is not None and self.status() in ['done','unconverged']: with chdir(self.directory): self.crystal.results = {} self.crystal.results["warning"] = False self.crystal.results["energyF"],self.crystal.results["energyZ"] = self.read_energy(allElectronic=allElectronic) self.crystal.results["forces"] = self.read_forces(allIonic=allIonic) self.crystal.results["stress"] = self.read_stress() #self.POTCAR = POTCAR.from_POTCAR() self.crystal.results["species"] = self.POTCAR.species self.crystal.results["energypatom"] = self.crystal.results["energyF"]/self.crystal.nAtoms if abs(self.crystal.results["energyF"]) > 1000: self.crystal.results["warning"] = True if 'pure' not in self.directory: self.crystal.results["fEnth"] = self.formationEnergy else: self.crystal.results["fEnth"] = 0 else: print(self.status(), 'not reading results') self.crystal.results = None msg.info("Unable to extract necessary information from directory! ({})".format(self.directory)) def add_to_results(self,key,item): if self.crystal.results is None: self.crystal.results = {} self.crystal.results[key] = item @property def formationEnergy(self): pures = [] for i in range(self.crystal.nTypes): pureDir = path.join(path.split(self.directory)[0], 'pure' + self.crystal.species[i]) pureVASP = VASP(pureDir,systemSpecies = self.crystal.species) pureVASP.read_results() pures.append(pureVASP) try: formationEnergy = self.crystal.results["energyF"]/self.crystal.nAtoms - sum( [ pures[i].crystal.results["energyF"]/pures[i].crystal.nAtoms * self.crystal.concentrations[i] for i in range(self.crystal.nTypes)]) except: formationEnergy = 10000 return formationEnergy
def build_ToRelax(self, enums, species, AFM=False, start=1, end=None): from aBuild.enumeration import Enumerate from aBuild.utility import unpackProtos, getAllPerms, getProtoPaths from aBuild.database.crystal import Crystal from os import remove, path print('Building to_relax.cfg') nEnums = len(enums) knary = len(species) for ilat in range(nEnums): lat = enums[ilat].lattice.lattice_name if lat == 'protos': structures = getProtoPaths(knary) for struct in structures: print("Proto structure:", struct) scrambleOrder = getAllPerms(knary, justCyclic='uniqueUnaries' in struct) for scramble in scrambleOrder: thisCrystal = Crystal(struct, species) #print("Atom counts before scramble {}".format(thisCrystal.atom_counts)) thisCrystal.scrambleAtoms(scramble) if not thisCrystal.concsOK( concRestrictions=enums[ilat]["concs"]): continue mindist = thisCrystal.minDist if mindist > 2: with open(path.join(self.root, 'to_relax.cfg'), 'a+') as f: f.writelines('\n'.join( thisCrystal.lines('mtprelax'))) else: enumLattice = enums[ilat] if end == None: end = enumLattice.nConfigs + 1 filetag = '' else: filetag = '_' + str(start) #for struct in range(enumLattice.nConfigs+1): for struct in range(start, end): print("Generating {} crystal structure # {}.".format( lat, struct)) enumLattice.generatePOSCAR(struct) # When initializing a crystal object from a poscar generated by makestr.x, I know that the crystal species is # the same as the system species because it generates zeros in the atom counts list. print('initializing object') thisCrystal = Crystal.from_poscar( path.join(enumLattice.root, "poscar.{}.{}".format(lat, struct)), species) thisCrystal.set_latpar(modify=enumLattice.latticeExpand) with open(path.join(self.root, 'to_relax.cfg' + filetag), 'a+') as f: f.writelines('\n'.join(thisCrystal.lines('mtprelax'))) delpath = path.join(enumLattice.root, "poscar.{}.{}".format(lat, struct)) remove(delpath)
enumController.enumerate() # Loop to generate random structures for a given lattice type <<<<<<< HEAD for i in range(eDict["nconfigs"]): rStruct = 16254#randrange(1,enumController.nEnumStructs) print('Adding {} structure # {} to database'.format(eDict["lattice"],rStruct) ) with open('structNums','a+') as f: f.write(eDict["name"] + ' ' + str(rStruct) + '\n') #print("Building VASP folder for {} structure #: {}".format(eDict["lattice"],rStruct)) enumController.generatePOSCAR(rStruct) poscarpath = path.join(enumController.root,"poscar.{}.{}".format(eDict["name"],rStruct)) thisCrystal = Crystal(poscarpath, systemSpecies = systemSpecies) #title = ' '.join([self.enumDicts[index]["lattice"]," str #: {}"]).format(rStruct) if self.restrictions is None: self.crystals.append(thisCrystal) elif thisCrystal.getAFMPlanes([1,0,0]): print('parent is AFM compatible') self.crystals.append(thisCrystal) import sys sys.exit() else: superCrystal = thisCrystal.superPeriodics(2) if superCrystal != []: print('super periodic structures is AFM compatible') print(superCrystal.minDist, 'minDist') print(superCrystal.basis,' basis') print(array(superCrystal.Bv_direct), 'direct') print(array(superCrystal.Bv_cartesian), 'cartesian')
from os import remove,path print('Building to-relax.cfg') nEnums = len(enumDicts) knary = len(species) for ilat in range(nEnums): lat = enumDicts[ilat]["name"] if lat == 'protos': #for prototype structures, we want to make sure that the mindist is okay structures = getProtoPaths(knary) for struct in structures: print("Proto structure:", struct) scrambleOrder = getAllPerms(knary,justCyclic = 'uniqueUnaries' in struct) for scramble in scrambleOrder: thisCrystal = Crystal(struct,species) #print("Atom counts before scramble {}".format(thisCrystal.atom_counts)) thisCrystal.scrambleAtoms(scramble) if not thisCrystal.concsOK(concRestrictions = enumDicts[ilat]["concs"]): continue print(thisCrystal.title) mindist = thisCrystal.minDist print(mindist, "actual min dist<-----------------------------------------------------------------------------------------------------") <<<<<<< HEAD if mindist > 2 and thisCrystal.nAtoms < 60: if not AFM: print('Adding to file') #print("Atom counts after scramble {}".format(thisCrystal.atom_counts)) with open(path.join(self.root,'to-relax.cfg'),'a+') as f: