def get_population(Optimizer): """ Function to generate a population of structures. Inputs: Optimizer = structopt Optimizer class object Outputs: pop = List of structopt Individual class objects containing new structures. """ index1 = 0 pop = [] for i in range(Optimizer.nindiv): if Optimizer.structure == 'Defect': individ = get_defect_indiv(Optimizer) elif Optimizer.structure == 'Surface': individ = get_surface_indiv(Optimizer) elif Optimizer.structure == 'Crystal': individ = get_crystal_indiv(Optimizer) else: if 'sphere' in Optimizer.generate_flag: ind = gen_pop_sphere(Optimizer.atomlist,Optimizer.size) else: ind = gen_pop_box(Optimizer.atomlist,Optimizer.size) individ = Individual(ind) individ.index = index1 if Optimizer.genealogy: individ.history_index = repr(index1) Optimizer.output.write('Generated cluster individual with natoms = '+ repr(individ[0].get_number_of_atoms())+'\n') pop.append(individ) index1=index1+1 # Generate new atomlist concentrations based on cluster+box if Optimizer.structure == 'Defect': if Optimizer.alloy: concents=[] for ind in pop: cs=[] for sym,c,u,m in Optimizer.atomlist: sylen=[atm for atm in ind[0] if atm.symbol==sym] cs.append(len(sylen)) concents.append(cs) natmlist=[0]*len(Optimizer.atomlist) for i in range(len(concents[0])): alls=[cs[i] for cs in concents] avgall=int(sum(alls)/len(alls)) if avgall>=Optimizer.atomlist[i][1]: natmlist[i]=(Optimizer.atomlist[i][0], avgall, Optimizer.atomlist[i][2],Optimizer.atomlist[i][3]) else: natmlist[i]=(Optimizer.atomlist[i][0], Optimizer.atomlist[i][1], Optimizer.atomlist[i][2],Optimizer.atomlist[i][3]) else: natmlist=[0]*len(Optimizer.atomlist) for i in range(len(Optimizer.atomlist)): atms1=[inds for inds in pop[0][0] if inds.symbol==Optimizer.atomlist[i][0]] natmlist[i]=(Optimizer.atomlist[i][0], len(atms1),Optimizer.atomlist[i][2], Optimizer.atomlist[i][3]) Optimizer.atomlist=natmlist Optimizer.output.write('\n\nNew atomlist concentrations based on cluster+box = '+ repr(Optimizer.atomlist)+'\n') return pop
Optimizer.output.flush() Optimizer.nindiv-=1 if successflag: Optimizer.output.write('Found good individual = {0}\n'.format(indiv)) if Optimizer.structure == 'Defect': individ = get_defect_restart_indiv(Optimizer,indiv) elif Optimizer.structure == 'Surface': individ = get_surface_restart_indiv(Optimizer, indiv) else: cell = indiv.get_cell() if cell[0][0] == 1.0: indiv.set_cell([Optimizer.size,Optimizer.size,Optimizer.size]) individ = Individual(indiv) individ.index = index1 if Optimizer.genealogy: individ.history_index = repr(index1) pop.append(individ) index1 = index1+1 if len(pop) == 0: raise RuntimeError('Unable to load any structures for Restart') # Generate new atomlist concentrations based on cluster+box if Optimizer.structure == 'Defect': if Optimizer.alloy: concents=[] for ind in pop: cs=[] for sym,c,u,m in Optimizer.atomlist: sylen=[atm for atm in ind[0] if atm.symbol==sym] cs.append(len(sylen)) concents.append(cs) natmlist=[0]*len(Optimizer.atomlist)
Optimizer.output.flush() Optimizer.nindiv -= 1 if successflag: Optimizer.output.write( 'Found good individual = {0}\n'.format(indiv)) if Optimizer.structure == 'Defect': individ = get_defect_restart_indiv(Optimizer, indiv) elif Optimizer.structure == 'Surface': individ = get_surface_restart_indiv(Optimizer, indiv) else: indiv.set_cell( [Optimizer.size, Optimizer.size, Optimizer.size]) individ = Individual(indiv) individ.index = index1 if Optimizer.genealogy: individ.history_index = repr(index1) pop.append(individ) index1 = index1 + 1 if len(pop) == 0: raise RuntimeError('Unable to load any structures for Restart') # Generate new atomlist concentrations based on cluster+box if Optimizer.structure == 'Defect': if Optimizer.alloy: concents = [] for ind in pop: cs = [] for sym, c, u, m in Optimizer.atomlist: sylen = [atm for atm in ind[0] if atm.symbol == sym] cs.append(len(sylen)) concents.append(cs) natmlist = [0] * len(Optimizer.atomlist)
def read_individual(indivfile, n=-1): """Function to write the data of an individual class object to a flat file Input: indivfile = String or fileobject for file to be read from n = which individual from file to return. Default is last individual written. optional All Output: returns an individual class object or list of individual class objects depending on value of n """ if isinstance(indivfile, str): indivfile = open(indivfile, 'r') all_lines = indivfile.readlines() indivfile.close() linen = 0 all_indivs = [] while linen < len(all_lines): if '----------' in all_lines[linen]: individ = Individual(Atoms()) elif 'Structure information' in all_lines[linen]: natomstruct = int(all_lines[linen + 1]) atomstruct = Atoms() for i in range(natomstruct): a = all_lines[linen + i + 3].split() sym = a[0] position = [float(a[1]), float(a[2]), float(a[3])] atomstruct.append(Atom(symbol=sym, position=position)) individ = Individual(atomstruct) linen += 2 + natomstruct elif 'structure cell' in all_lines[linen]: cell_line = all_lines[linen].split('=') structcell = eval(cell_line[1]) individ[0].set_cell(structcell) elif 'fitness' in all_lines[linen]: fitline = all_lines[linen].split('=') individ.fitness = float(fitline[1]) elif 'history_index' in all_lines[linen]: line = all_lines[linen].split('=') individ.history_index = line[1].strip() elif 'index' in all_lines[linen]: line = all_lines[linen].split('=') individ.index = float(line[1]) elif 'tenergymx' in all_lines[linen]: line = all_lines[linen].split('=') individ.tenergymx = float(line[1]) elif 'tenergymin' in all_lines[linen]: line = all_lines[linen].split('=') individ.tenergymin = float(line[1]) elif 'energy' in all_lines[linen]: line = all_lines[linen].split('=') individ.energy = float(line[1]) elif 'pressure' in all_lines[linen]: line = all_lines[linen].split('=') # HKK 05-21-2015:: Occassionally found 'Null' pressure during VASP-GA. Need investigation, but setting to 0 pressure for now. if line[1] and line[1].strip(): individ.pressure = float(line[1]) else: individ.pressure = 0.0 elif 'volume' in all_lines[linen]: line = all_lines[linen].split('=') individ.volume = float(line[1]) elif 'force' in all_lines[linen]: line = all_lines[linen].split('=') individ.force = float(line[1]) elif 'purebulkenpa' in all_lines[linen]: line = all_lines[linen].split('=') individ.purebulkenpa = float(line[1]) elif 'natomsbulk' in all_lines[linen]: line = all_lines[linen].split('=') individ.natomsbulk = float(line[1]) elif 'fingerprint' in all_lines[linen]: line = all_lines[linen].split('=') individ.fingerprint = eval(line[1]) elif 'swpalist' in all_lines[linen]: line = all_lines[linen].split('=') individ.swaplist = eval(line[1]) elif 'bulki cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.bulki.set_cell(cell) elif 'bulki' in all_lines[linen]: natoms = int(all_lines[linen + 1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen + i + 3].split() sym = a[0] position = [float(a[1]), float(a[2]), float(a[3])] atomstruct.append(Atom(symbol=sym, position=position)) individ.bulki = atomstruct.copy() linen += 2 + natoms elif 'bulko cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.bulko.set_cell(cell) elif 'bulko' in all_lines[linen]: natoms = int(all_lines[linen + 1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen + i + 3].split() sym = a[0] position = [float(a[1]), float(a[2]), float(a[3])] atomstruct.append(Atom(symbol=sym, position=position)) individ.bulko = atomstruct.copy() linen += 2 + natoms elif 'box cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.box.set_cell(cell) elif 'box' in all_lines[linen]: natoms = int(all_lines[linen + 1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen + i + 3].split() sym = a[0] position = [float(a[1]), float(a[2]), float(a[3])] atomstruct.append(Atom(symbol=sym, position=position)) individ.box = atomstruct.copy() linen += 2 + natoms elif 'vacancies cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.vacancies.set_cell(cell) elif 'vacancies' in all_lines[linen]: natoms = int(all_lines[linen + 1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen + i + 3].split() sym = a[0] position = [float(a[1]), float(a[2]), float(a[3])] atomstruct.append(Atom(symbol=sym, position=position)) individ.vacancies = atomstruct.copy() linen += 2 + natoms elif 'swaps cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.swaps.set_cell(cell) elif 'swaps' in all_lines[linen]: natoms = int(all_lines[linen + 1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen + i + 3].split() sym = a[0] position = [float(a[1]), float(a[2]), float(a[3])] atomstruct.append(Atom(symbol=sym, position=position)) individ.swaps = atomstruct.copy() linen += 2 + natoms elif 'Finish' in all_lines[linen]: all_indivs.append(individ.duplicate()) linen += 1 if n == 'All': return all_indivs else: return all_indivs[n]
def get_population(Optimizer): """ Function to generate a population of structures. Inputs: Optimizer = structopt Optimizer class object Outputs: pop = List of structopt Individual class objects containing new structures. """ index1 = 0 pop = [] for i in range(Optimizer.nindiv): if Optimizer.structure == 'Defect': individ = get_defect_indiv(Optimizer) elif Optimizer.structure == 'Surface': individ = get_surface_indiv(Optimizer) elif Optimizer.structure == 'Crystal': individ = get_crystal_indiv(Optimizer) else: if 'sphere' in Optimizer.generate_flag: ind = gen_pop_sphere(Optimizer.atomlist, Optimizer.size) else: ind = gen_pop_box(Optimizer.atomlist, Optimizer.size) individ = Individual(ind) individ.index = index1 if Optimizer.genealogy: individ.history_index = repr(index1) Optimizer.output.write('Generated cluster individual with natoms = ' + repr(individ[0].get_number_of_atoms()) + '\n') pop.append(individ) index1 = index1 + 1 # Generate new atomlist concentrations based on cluster+box if Optimizer.structure == 'Defect': if Optimizer.alloy: concents = [] for ind in pop: cs = [] for sym, c, u, m in Optimizer.atomlist: sylen = [atm for atm in ind[0] if atm.symbol == sym] cs.append(len(sylen)) concents.append(cs) natmlist = [0] * len(Optimizer.atomlist) for i in range(len(concents[0])): alls = [cs[i] for cs in concents] avgall = int(sum(alls) / len(alls)) if avgall >= Optimizer.atomlist[i][1]: natmlist[i] = (Optimizer.atomlist[i][0], avgall, Optimizer.atomlist[i][2], Optimizer.atomlist[i][3]) else: natmlist[i] = (Optimizer.atomlist[i][0], Optimizer.atomlist[i][1], Optimizer.atomlist[i][2], Optimizer.atomlist[i][3]) else: natmlist = [0] * len(Optimizer.atomlist) for i in range(len(Optimizer.atomlist)): atms1 = [ inds for inds in pop[0][0] if inds.symbol == Optimizer.atomlist[i][0] ] natmlist[i] = (Optimizer.atomlist[i][0], len(atms1), Optimizer.atomlist[i][2], Optimizer.atomlist[i][3]) Optimizer.atomlist = natmlist Optimizer.output.write( '\n\nNew atomlist concentrations based on cluster+box = ' + repr(Optimizer.atomlist) + '\n') return pop
def read_individual(indivfile, n=-1): """Function to write the data of an individual class object to a flat file Input: indivfile = String or fileobject for file to be read from n = which individual from file to return. Default is last individual written. optional All Output: returns an individual class object or list of individual class objects depending on value of n """ if isinstance(indivfile, str): indivfile=open(indivfile, 'r') all_lines = indivfile.readlines() indivfile.close() linen = 0 all_indivs = [] while linen < len(all_lines): if '----------' in all_lines[linen]: individ = Individual(Atoms()) elif 'Structure information' in all_lines[linen]: natomstruct = int(all_lines[linen+1]) atomstruct = Atoms() for i in range(natomstruct): a = all_lines[linen+i+3].split() sym = a[0] position = [float(a[1]),float(a[2]),float(a[3])] atomstruct.append(Atom(symbol=sym,position=position)) individ = Individual(atomstruct) linen += 2+natomstruct elif 'structure cell' in all_lines[linen]: cell_line = all_lines[linen].split('=') structcell = eval(cell_line[1]) individ[0].set_cell(structcell) elif 'fitness' in all_lines[linen]: fitline = all_lines[linen].split('=') individ.fitness = float(fitline[1]) elif 'history_index' in all_lines[linen]: line = all_lines[linen].split('=') individ.history_index = line[1].strip() elif 'index' in all_lines[linen]: line = all_lines[linen].split('=') individ.index = float(line[1]) elif 'tenergymx' in all_lines[linen]: line = all_lines[linen].split('=') individ.tenergymx = float(line[1]) elif 'tenergymin' in all_lines[linen]: line = all_lines[linen].split('=') individ.tenergymin = float(line[1]) elif 'energy' in all_lines[linen]: line = all_lines[linen].split('=') individ.energy = float(line[1]) elif 'pressure' in all_lines[linen]: line = all_lines[linen].split('=') individ.pressure = float(line[1]) elif 'volume' in all_lines[linen]: line = all_lines[linen].split('=') individ.volume = float(line[1]) elif 'force' in all_lines[linen]: line = all_lines[linen].split('=') individ.force = float(line[1]) elif 'purebulkenpa' in all_lines[linen]: line = all_lines[linen].split('=') individ.purebulkenpa = float(line[1]) elif 'natomsbulk' in all_lines[linen]: line = all_lines[linen].split('=') individ.natomsbulk = float(line[1]) elif 'fingerprint' in all_lines[linen]: line = all_lines[linen].split('=') individ.fingerprint = eval(line[1]) elif 'swpalist' in all_lines[linen]: line = all_lines[linen].split('=') individ.swaplist = eval(line[1]) elif 'bulki cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.bulki.set_cell(cell) elif 'bulki' in all_lines[linen]: natoms = int(all_lines[linen+1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen+i+3].split() sym = a[0] position = [float(a[1]),float(a[2]),float(a[3])] atomstruct.append(Atom(symbol=sym,position=position)) individ.bulki = atomstruct.copy() linen += 2+natoms elif 'bulko cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.bulko.set_cell(cell) elif 'bulko' in all_lines[linen]: natoms = int(all_lines[linen+1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen+i+3].split() sym = a[0] position = [float(a[1]),float(a[2]),float(a[3])] atomstruct.append(Atom(symbol=sym,position=position)) individ.bulko = atomstruct.copy() linen += 2+natoms elif 'box cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.box.set_cell(cell) elif 'box' in all_lines[linen]: natoms = int(all_lines[linen+1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen+i+3].split() sym = a[0] position = [float(a[1]),float(a[2]),float(a[3])] atomstruct.append(Atom(symbol=sym,position=position)) individ.box = atomstruct.copy() linen += 2+natoms elif 'vacancies cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.vacancies.set_cell(cell) elif 'vacancies' in all_lines[linen]: natoms = int(all_lines[linen+1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen+i+3].split() sym = a[0] position = [float(a[1]),float(a[2]),float(a[3])] atomstruct.append(Atom(symbol=sym,position=position)) individ.vacancies = atomstruct.copy() linen += 2+natoms elif 'swaps cell' in all_lines[linen]: line = all_lines[linen].split('=') cell = eval(line[1]) individ.swaps.set_cell(cell) elif 'swaps' in all_lines[linen]: natoms = int(all_lines[linen+1]) atomstruct = Atoms() for i in range(natoms): a = all_lines[linen+i+3].split() sym = a[0] position = [float(a[1]),float(a[2]),float(a[3])] atomstruct.append(Atom(symbol=sym,position=position)) individ.swaps = atomstruct.copy() linen += 2+natoms elif 'Finish' in all_lines[linen]: all_indivs.append(individ.duplicate()) linen+=1 if n=='All': return all_indivs else: return all_indivs[n]