def run_task(self,fw_spec): global nIter nIter = 0 jobID = fw_spec['jobID'] job = db2BulkJob(jobID) setupEnv(job) job.printFields() # confirm job details existsPrecalc = job.precalcxc != 'None' t0 = time.time() def getBulkEnergy(latticeParams): """For a given set of bravais lattice parameters, optimize atomic coordinates and return minimum energy""" global nIter nIter += 1 atoms = job.fromParams(latticeParams) calc = job.calc(restart=True) if existsPrecalc else job.calc() if existsPrecalc: preCalc = job.calc(precalc=True) job.optimizePos(atoms,preCalc,saveWF = True) if job.dftcode =='gpaw': atoms,calc = job.gpawRestart() job.optimizePos(atoms,calc) energy = atoms.get_potential_energy() with paropen('lattice_opt.log','a') as logfile: logfile.write('%s\t%s\n' %(energy,latticeParams)) parprint('%s\t%s\n' %(energy,latticeParams)) if job.dftcode=='gpaw': atoms.calc.write('inp.gpw', mode='all') #for use in getXCContribs io.write('out.traj',atoms) return energy optimizedLatticeParams = fmin(getBulkEnergy,job.iGuess(),ftol=1,xtol=job.xtol) avgtime = (time.time()-t0)/float(nIter)/60.0 #min ### Get info from final state optAtoms = io.read('out.traj') optCell = optAtoms.get_cell() pos = optAtoms.get_scaled_positions() e0 = optAtoms.get_potential_energy() f0 = optAtoms.get_forces() magmom = optAtoms.get_magnetic_moments() if any([x>0 for x in job.magmoms]) else np.zeros(len(optAtoms)) resultDict = {'latticeParams': optimizedLatticeParams,'avgtime':avgtime,'optCell':optCell,'pos':pos,'e0':e0,'f0':f0,'magmom':magmom,'niter':nIter} return FWAction( stored_data=resultDict,mod_spec=[{'_push': resultDict}])
def initialize(jobID, latticeParams): jobObject = db2BulkJob(jobID) bulkObject = db2Bulk(jobObject.bulkpth) calcObject = db2Calc(jobObject.calcid) dft = jobObject.dftCode preCalcObject = deepcopy(calcObject) preCalcObject.xc = jobObject.precalcxc existsPrecalc = str(preCalcObject.xc) != 'None' atoms = bulkObject.fromParams(latticeParams) magmoms = bulkObject.magInit(calcObject.magmom) return (jobObject, bulkObject, calcObject, dft, preCalcObject, preCalcObject, existsPrecalc, atoms, magmoms)
def getXCcontribs(jobID): jobObject = db2BulkJob(jobID) if jobObject.dftCode == 'gpaw': from gpaw import restart from gpaw.xc.bee import BEEFEnsemble atoms, calc = restart('inp.gpw', setups='sg15', xc='mBEEF', convergence={'energy': 5e-4}, txt='mbeef.txt') atoms.get_potential_energy() beef = BEEFEnsemble(calc) return beef.mbeef_exchange_energy_contribs() else: return np.zeros((8, 8))
def run_task(self,fw_spec): jobID,latticeParams = fw_spec['jobID'],fw_spec['latticeParams'] # WHY IS LATTICEPARAMS [[FLOAT]] INSTEAD OF [FLOAT]? job = db2BulkJob(jobID) setupEnv(job) existsPrecalc = job.precalcxc != 'None' optAtoms = job.fromParams(latticeParams[0]) optCell = optAtoms.get_cell() strains = np.linspace(1-job.strain,1+job.strain,9) calc = job.calc(restart=True) if existsPrecalc else job.calc() vol,eng = [],[] for i, strain in enumerate(strains): atoms = deepcopy(optAtoms) atoms.set_cell(optCell*strain,scale_atoms=True) if existsPrecalc: preCalc = job.calc(precalc=True) job.optimizePos(atoms,preCalc,saveWF = True) if job.dftcode =='gpaw': atoms,calc = job.gpawRestart() job.optimizePos(atoms,calc) energy = atoms.get_potential_energy() volume = atoms.get_volume() vol.append(deepcopy(volume)) eng.append(deepcopy(energy)) parprint('%f %f'%(strain,energy)) aHat,quadR2 = quadFit(np.array(deepcopy(vol)),np.array(deepcopy(eng))) try: eos = EquationOfState(vol,eng) v0, e0, b = eos.fit() eos.plot(filename='bulk-eos.png',show=False) b0= b/kJ*1e24 #GPa use this value if EOS doesn't fail except ValueError: # too bad of a fit for ASE to handle b0 = aHat*2*vol[4]*160.2 # units: eV/A^6 * A^3 * 1, where 1 === 160.2 GPa*A^3/eV return FWAction( stored_data= {'b0':b0,'quadR2':quadR2} ,mod_spec=[{'_push': {'b0':b0,'quadR2':quadR2}}])
def run_task(self,fw_spec): jobID = fw_spec['jobID'] job = db2BulkJob(jobID) resultNames = ['optCell','pos','magmom','e0','f0','b0','quadR2','xcContribs','avgtime','niter'] results = [fw_spec[x][0] for x in resultNames] resultDict = {n:fw_spec[n] for n in resultNames} result = BulkResult(*([jobID]+results)) result.addToASEdb(job) insertObject(result) # add result to bulkresult table updateDB('bulkjob','status','id',jobID,'complete') return FWAction(stored_data= resultDict)
def run_task(self,fw_spec): jobID = fw_spec['jobID'] job = db2BulkJob(jobID) setupEnv(job) if job.dftcode == 'gpaw': from gpaw import restart from gpaw.xc.bee import BEEFEnsemble atoms,calc = restart('inp.gpw', setups='sg15', xc='mBEEF', convergence={'energy': 5e-4}, txt='mbeef.txt') atoms.get_potential_energy() beef = BEEFEnsemble(calc) xcContribs = beef.mbeef_exchange_energy_contribs() else: xcContribs = np.zeros((8,8)) return FWAction( stored_data={'xcContribs': xcContribs} ,mod_spec=[{'_push': {'xcContribs': xcContribs}}])
def main(): try: deleteTable('tentative') #WHAT IS GOING ON HERE? except: pass try: createTentativeBulkTable() except: pass domains = [ filteredTrajDomain, filteredXCDomain, filteredPSPDomain, filteredPWDomain, filteredKPTDomain, filteredPreCalcXCDomain, filteredDFTDomain ] combos = product(*domains) ncombo = reduce(mul, [len(x) for x in domains]) for i, c in enumerate(combos): print '%f%' % (i / float(ncombo) * 100) tentativeInput = c[:5] + tuple(defaults) + c[5:] tentativeBulkjob = BulkJob(*tentativeInput) if tentativeBulkjob.xc == 'mBEEF': #defaults slightly different for mBEEF tentativeBulkjob.sigma += 0.1 tentativeBulkjob.sigma -= 0.05 ID, status = insertObject(tentativeBulkjob, tentative=True) print "%d tentative jobs" % countDB('tentative') valid = [ db2BulkJob(x[0], tentative=True) for x in plotQuery('tentative', ['tentative.id'], constraints) ] new = 0 for z in range(len(valid)): ID, status = insertObject(valid[z]) if status == 'inserted': new += 1 print "%d valid jobs, %d are new" % (len(valid), new) deleteTable('tentative')