def __init__( self, substrate=None, gadb=None, popSize=20, zLim=None, compParam=None, matingMethod=None, convergeCrit=None, ): if gadb is not None: self.gadb = connect(gadb) if type(substrate) is str: self.substrate = read(substrate) else: self.substrate = substrate if zLim is not None: self.zLim = zLim if zLim is None: self.zLim = Interface(self.substrate, self.substrate).zLim self.popSize = popSize if convergeCrit is None: self.convergeCrit = 5 * self.popSize else: self.convergeCrit = convergeCrit if chemPotDict is not None: self.chemPotDict = chemPotDict self.iniSize = len(self)
def gen_offspring_box(self, mutRate=0.3, xyzLims=[], bondRejList = None, constrainTop=False, rattleOn=True, growOn=True, leachOn=True, permuteOn = True, transOn = True, transVec=[[-2,2],[-2,2]]): kid, parent = None, None mater, pater = 0, 0 while kid is None: mater, pater = self.choose_parents() a1 = self.gadb.get(id=mater).toatoms() a2 = self.gadb.get(id=pater).toatoms() surf1 = Interface(a1, self.substrate, zLim=self.zLim) surf2 = Interface(a2, self.substrate, zLim=self.zLim) kid = crossover_snsSurf_2d_GC(surf1, surf2, tolerance=0.75) parent = surf1.copy() print('PARENTS: %i and %i'%(mater, pater)) myMutate = '' if srtDist_similar_zz(a1, a2)\ or srtDist_similar_zz(a1, kid.get_allAtoms())\ or srtDist_similar_zz(a2, kid.get_allAtoms()): print(' |- TOO SIMILAR!') mutRate = 1 if np.random.rand() < mutRate: mutType = np.random.choice([0,1,2,3,4], size=1)[0] if mutType == 0 and rattleOn: myMutate = 'rattle' kid.rattleMut() if mutType == 1 and growOn: myMutate = 'grow' kid.growMut_box([l for l in self.chemPotDict], xyzLims=xyzLims, bondRejList=bondRejList, constrainTop=constrainTop) if mutType == 2 and leachOn: myMutate = 'leach' kid.leachMut([l for l in self.chemPotDict]) if mutType == 3 and permuteOn: myMutate = 'permute' kid.permuteMut() if mutType == 4 and transOn: myMutate = 'translate' kid.transMut(transVec=transVec) if len(kid.get_adsList()) <= 1: myMutate = 'init' print(' |- Bare substrate, BAD!') kid = parent.copy() kid.rattleMut() kid.growMut([l for l in self.chemPotDict]) open('label', 'w').write('%i %i %s'%(mater, pater, myMutate)) self.gadb.update(mater, mated=self.gadb.get(id=mater).mated+1) self.gadb.update(pater, mated=self.gadb.get(id=pater).mated+1) return kid
def calc_grandPot(self, atoms, dftene): # myRow = self.gadb.get(id=myID) # myPot = myRow['eV'] myPot = dftene - self.subsPot adsSymbol = Interface(atoms, self.substrate).\ get_adsAtoms().get_chemical_symbols() for s in adsSymbol: if s in self.chemPotDict: myPot -= self.chemPotDict[s] return myPot
def gen_offspring(self, mutRate=0.4): kid = None mater, pater = 0, 0 while kid is None: mater, pater = self.choose_parents() a1 = self.gadb.get(id=mater).toatoms() a2 = self.gadb.get(id=pater).toatoms() surf1 = Interface(a1, self.substrate, zLim=self.zLim) surf2 = Interface(a2, self.substrate, zLim=self.zLim) kid = crossover_snsSurf_2d(surf1, surf2, tolerance=0.75) print('PARENTS: %i and %i' % (mater, pater)) if srtDist_similar_zz(a1, a2)\ or srtDist_similar_zz(a1, kid.get_allAtoms())\ or srtDist_similar_zz(a2, kid.get_allAtoms()): print(' |- TOO SIMILAR!') mutRate = 1 if np.random.rand() < mutRate: print(' |- MUTATION!') kid.transMut() kid.rattleMut() self.gadb.update(mater, mated=self.gadb.get(id=mater).mated + 1) self.gadb.update(pater, mated=self.gadb.get(id=pater).mated + 1) return kid
from ase.db import connect from ase.io import read from gocia.interface import Interface import gocia.utils.report as rp from gocia.utils.visualize import histogram substrate = read(sys.argv[1]) srtDB = connect(sys.argv[2]) baseName = '' eneList = [] for r in srtDB.select([('eV2GM', '<', 5.0)]): a = r.toatoms() eneList.append(r.eV) surf = Interface( allAtoms=a, subAtoms=substrate, ) baseName = surf.tags surf.tags += '-%i' % r.id surf.draw( 'BS', title='#%i, E=%.3f eV, mag=%.2f'%\ (r.id, r.eV2GM, r.mag), ) # myImgList = [f for f in os.listdir() if baseName in f and 'png' in f] # rp.gen_pdf_test( # projName='Global Optimization of Restructured %s'%baseName, # description=\ # '\\begin{itemize}\n'+\ # '\\item From direct BLDA sampling with random rattling of 0.1 Angstrom and Hookean pre-optimization.\n'+\
from gocia.interface import Interface from ase.io import read, write from gocia.utils import conv_formula2list import sys import numpy as np from ase.db import connect import random fn_substrate = sys.argv[1] nsamples = int(sys.argv[2]) s = read(fn_substrate) mycell = np.array(s.get_cell(complete=True)) myLim = np.array([[0, mycell[0][0]], [0, mycell[0][0]], [7.0, 11.0]]) surf = Interface(s, s, zLim=myLim[2]) surf.print() with connect('%s-rand.db' % (fn_substrate.split('.')[0]), append=False) as db: for i in range(nsamples): nB = random.randint(2, 8) nO = random.randint(int(nB / 2), nB) nH = random.randint(1, int(nO / 2) + 1) adatoms = ['B'] * nB + ['O'] * nO + ['H'] * nH print('%i\t%s' % (i, ''.join(adatoms)), end='\t') genAds = boxSample_adatom(surf, adatoms, myLim, toler_BLmin=-0.2, toler_BLmax=0.2, toler_CNmin=1,
import os, sys import ase.io as fio import ase.db as db from gocia.interface import Interface import gocia.utils.report as rp # $ python plotCPK.py asym-4l.vasp Ga3N3.vasp # # Timing: 1.4265 s, ~10% faster than BS subsName = sys.argv[1] surfName = sys.argv[2] baseName = surfName.split('/')[-1].split('.')[0] surf = Interface( tags=baseName, subAtoms=fio.read(subsName), allAtoms=fio.read(surfName), ) surf.info['eV'] = 0.1 surf.print() surf.draw('CPK', title='%s, %.3f eV' % (surf.get_formula(), surf.info['eV'])) surf.draw('BS', title='%s, %.3f eV' % (surf.get_formula(), surf.info['eV']))
from gocia.interface import Interface import sys, os from gocia.ga.crossover import crossover_snsSurf_2d_GC subs = sys.argv[1] surf1 = sys.argv[2] surf2 = sys.argv[3] #surf0 = Interface(subAtoms=subs, allAtoms=subs) surf1 = Interface(allAtoms=surf1, subAtoms=subs) surf2 = Interface(allAtoms=surf2, subAtoms=subs) surf1.print() surf1.draw('CPK') os.system('mv Ga47N51-cpk.png mom.png') surf2.print() surf2.draw('CPK') os.system('mv Ga47N51-cpk.png dad.png') #print(surf1.get_fixBufPos()) #print(surf1.get_fixBufAtoms()) nova = crossover_snsSurf_2d_GC(surf1, surf2) nova.print() nova.write('nova.vasp') nova.draw('CPK') os.system('mv Ga47N51-cpk.png kid.png') tmp = nova.copy() nova = tmp.copy()
def get_extended_atoms(atoms): tmpAtoms = atoms.copy() # tmpAtoms.set_positions(tmpAtoms.get_positions()\ # -atoms.get_cell()[0]/2-atoms.get_cell()[1]/2) tmpAtoms = atoms * [3, 3, 1] tmpAtoms.set_positions(tmpAtoms.get_positions()\ -atoms.get_cell()[0]/1-atoms.get_cell()[1]/1) return tmpAtoms # TODO DUPLICATE CHECK # TODO ADD ATOP # TODO ADD HOLLOW # TODO ADD BRIDGE surf = Interface(allAtoms='calcSlab.vasp', subAtoms='sub-calcSlab.vasp') surf.print() surf = read('out-Cu111.vasp') #surf = read('WBsurf2.vasp') #print(get_surf_byZ(surf, [9.5, 10.5])) #print(get_surf_grid(surf, 20)) surfList = get_surf_byZ(surf, [16, 18]) #surfList = get_surf_grid(surf, 20) print(surfList) del surf[[i for i in range(len(surf)) if i not in surfList]] surf_ext = get_extended_atoms(surf) #surf_ext = surf
import os, sys import ase.io as ai from ase.db import connect from gocia.interface import Interface from gocia.geom import build from gocia.utils import conv_formula2list # $ python directSample.py GaN_4-l_vac-1.vasp 'N N N' 10 # # Timing: 0.7767 s/structure surfName = sys.argv[1] adsInp = conv_formula2list(sys.argv[2]) nSample = int(sys.argv[3]) surf = Interface(tags=surfName.split('.')[0] + ' + ' + sys.argv[2], allAtoms=ai.read(surfName), subAtoms=ai.read(surfName), zLim=None) surf.print() myDB = connect('tmp.db', append=False) for i in range(nSample): print('Structure %i' % i, end='\t') newsurf = build.grow_adatom(surf, adsInp, toler=0.25, doShuffle=True, cnCount=True, sameElemPenalty=0, rattle=True, rattleStdev=0.1, rattleZEnhance=True,
import sys sys.path.append('/home/zisheng/windir/github') from ase.io import read, write import numpy as np from gocia.geom import cart2frac, frac2cart from gocia.interface import Interface surf = Interface('test.vasp', 'substrate.vasp') surf.print() surf.permuteMut() surf.print() surf.write('permute.vasp')