def get_defect_restart_indiv(Optimizer, indiv):
    """
    Function to generate an structopt Individual class object containing 
    	a defect structure from a previously existing structure
    Inputs:
    	Optimizer = structopt Optimizer class
    	indiv = ASE Atoms object containing the previously existing structure
    Outputs:
    	individ = structopt Individual class object containing defect structure data
    """
    if not Optimizer.solidbulk:
		#Initialize Bulk - Generate or load positions of bulk solid
		try:
		    rank = MPI.COMM_WORLD.Get_rank()
		except:
		    rank = 0
		outfilename = os.path.join(os.path.join(os.getcwd(),Optimizer.filename+'-rank'+repr(rank)),'Bulkfile.xyz')
		if Optimizer.evalsolid:
			bulk1, PureBulkEnpa, stro = gen_solid(Optimizer.solidfile,
				Optimizer.solidcell,outfilename,Optimizer.calc,Optimizer.calc_method)
			Optimizer.output.write(stro)
		else:
			bulk1 = gen_solid(Optimizer.solidfile,Optimizer.solidcell,outfilename)
			PureBulkEnpa = 0
		natomsbulk = len(bulk1)
		Optimizer.solidbulk = bulk1.copy()
		Optimizer.summary.write('CIBS Run Pure Bulk Energy per Atom:'+
			repr(PureBulkEnpa)+'\n')
		Optimizer.purebulkenpa = PureBulkEnpa
		Optimizer.natomsbulk = natomsbulk
    indiv.set_cell(Optimizer.solidcell)
    indiv.set_pbc(True)
    if Optimizer.restart_ints == 0:
        outt = find_defects(indiv,Optimizer.solidbulk,Optimizer.sf)
    else:
        indicop = [atm for atm in indiv if atm.symbol != 'X']
        indiv = Atoms(cell=Optimizer.solidcell, pbc=True)
        for atm in indicop:
            indiv.append(atm)
        outt=[indiv[0:Optimizer.restart_ints],indiv[Optimizer.restart_ints::], Atoms(),
        	Atoms(),'Assuming first '+repr(Optimizer.restart_ints)+' are interstitials\n']
    indi = outt[0].copy()
    bulki = outt[1].copy()
    individ = Individual(indi)
    individ.bulko = bulki.copy()
    individ.bulki = bulki.copy()
    individ.purebulkenpa = Optimizer.purebulkenpa
    individ.natomsbulk = Optimizer.natomsbulk
    individ.vacancies = outt[2].copy()
    individ.swaps = outt[3].copy()
    Optimizer.output.write(outt[4])
    return individ
def get_defect_indiv_random(Optimizer):
    """
    Function to generate a structopt Individual class structure with a defect structure.
    Inputs:
        Optimizer = structopt Optimizer class object
    Outputs:
        individ = structopt Individual class object containing defect structure data
    """
    #Initialize Bulk - Generate or load positions of bulk solid
    if not Optimizer.solidbulk:
        if 'Island_Method' not in Optimizer.algorithm_type:
            outfilename = os.path.join(
                os.path.join(os.getcwd(), Optimizer.filename), 'Bulkfile.xyz')
        else:
            from mpi4py import MPI
            rank = MPI.COMM_WORLD.Get_rank()
            outfilename = os.path.join(
                os.path.join(os.getcwd(),
                             Optimizer.filename + '-rank' + repr(rank)),
                'Bulkfile.xyz')
        if Optimizer.evalsolid:
            if Optimizer.parallel:
                from MAST.structopt_stem.tools.setup_calculator import setup_calculator
                Optimizer.calc = setup_calculator(Optimizer)
            bulk1, PureBulkEnpa, stro = gen_solid(Optimizer.solidfile,
                                                  Optimizer.solidcell,
                                                  outfilename, Optimizer.calc,
                                                  Optimizer.calc_method)
            Optimizer.output.write(stro)
        else:
            bulk1 = gen_solid(Optimizer.solidfile, Optimizer.solidcell,
                              outfilename)
            PureBulkEnpa = 0
        natomsbulk = len(bulk1)
        Optimizer.solidbulk = bulk1.copy()
        Optimizer.purebulkenpa = PureBulkEnpa
        Optimizer.natomsbulk = natomsbulk
    # Identify nearby atoms for region 2 inclusion
    bulk = Optimizer.solidbulk.copy()
    bulkcom = bulk.get_center_of_mass()
    bulk.translate(-bulkcom)
    if Optimizer.sf != 0:
        bulk.append(Atom(position=[0, 0, 0]))
        nbulk = Atoms(pbc=True, cell=bulk.get_cell())
        nr2 = Atoms(pbc=True, cell=bulk.get_cell())
        for i in range(len(bulk) - 1):
            dist = bulk.get_distance(-1, i)
            if dist <= Optimizer.sf:
                nr2.append(bulk[i])
            else:
                nbulk.append(bulk[i])
    else:
        nbulk = bulk.copy()
        nr2 = Atoms(pbc=True, cell=bulk.get_cell())
    #Update atom list with atoms in region 2
    natlist = []
    for sym, c, m, u in Optimizer.atomlist:
        atsym = [atm for atm in nr2 if atm.symbol == sym]
        natlist.append((sym, len(atsym), m, u))
    # Generate random individual and region 2
    if 'sphere' in Optimizer.generate_flag:
        ind = gen_pop_sphere(Optimizer.atomlist, Optimizer.size)
    elif 'dumbbell' in Optimizer.generate_flag:
        ind = Atoms(cell=[Optimizer.size for i in range(3)], pbc=True)
        for sym, c, m, u in Optimizer.atomlist:
            if c > 0:
                dums = generate_dumbbells(c,
                                          dumbbellsym=sym,
                                          nindiv=1,
                                          solid=Optimizer.solidbulk,
                                          size=Optimizer.size)[0]
                ind.extend(dums)
    else:
        ind = gen_pop_box(Optimizer.atomlist, Optimizer.size)
    nnr2 = gen_pop_sphere(natlist, Optimizer.sf * 2.0)
    nnr2.translate([-Optimizer.sf, -Optimizer.sf, -Optimizer.sf])
    nnr2.set_pbc(True)
    nnr2.set_cell(bulk.get_cell())
    # Initialize class individual with known values
    individ = Individual(ind)
    individ.purebulkenpa = Optimizer.purebulkenpa
    individ.natomsbulk = Optimizer.natomsbulk
    # Combine individual with R2
    icom = ind.get_center_of_mass()
    ind.translate(-icom)
    ind.extend(nnr2)
    ind.set_pbc(True)
    ind.set_cell(bulk.get_cell())
    # Recenter structure
    nbulk.translate(bulkcom)
    ind.translate(bulkcom)
    individ[0] = ind.copy()
    individ.bulki = nbulk.copy()
    individ.bulko = nbulk.copy()
    bulk = nbulk.copy()
    bul = bulk.copy()
    for atm in individ[0]:
        bul.append(atm)
    indices = []
    for sym, c, m, u in Optimizer.atomlist:
        if c < 0:
            if Optimizer.randvacst:
                alist = [one for one in bul if one.symbol == sym]
                count = abs(c)
                while count > 0:
                    indices.append(random.choice(alist).index)
                    count -= 1
            else:
                pos = individ[0][0:Optimizer.natoms].get_center_of_mass()
                count = abs(c)
                bul.append(Atom(position=pos))
                alist = [one for one in bul if one.symbol == sym]
                alistd = [(bul.get_distance(len(bul) - 1,
                                            one.index), one.index)
                          for one in alist]
                alistd.sort(reverse=True)
                bul.pop()
                while count > 0:
                    idx = alistd.pop()[1]
                    indices.append(idx)
                    count -= 1
    if len(indices) != 0:
        nbulklist = [
            at for at in bul
            if at.index not in indices and at.index < len(bulk)
        ]
        nalist = [
            at for at in bul
            if at.index not in indices and at.index >= len(bulk)
        ]
        bulkn = Atoms(cell=bulk.get_cell(), pbc=True)
        for atm in nbulklist:
            bulkn.append(atm)
        individ.bulki = bulkn.copy()
        individ.bulko = bulkn.copy()
        newind = Atoms()
        for atm in nalist:
            newind.append(atm)
        newind.set_cell(individ[0].get_cell())
        newind.set_pbc(True)
        individ[0] = newind
    return individ
Beispiel #3
0
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]
def get_defect_restart_indiv(Optimizer, indiv):
    """
    Function to generate an structopt Individual class object containing 
    	a defect structure from a previously existing structure
    Inputs:
    	Optimizer = structopt Optimizer class
    	indiv = ASE Atoms object containing the previously existing structure
    Outputs:
    	individ = structopt Individual class object containing defect structure data
    """
    if not Optimizer.solidbulk:
        #Initialize Bulk - Generate or load positions of bulk solid
        try:
            rank = MPI.COMM_WORLD.Get_rank()
        except:
            rank = 0
        outfilename = os.path.join(
            os.path.join(os.getcwd(),
                         Optimizer.filename + '-rank' + repr(rank)),
            'Bulkfile.xyz')
        if Optimizer.evalsolid:
            bulk1, PureBulkEnpa, stro = gen_solid(Optimizer.solidfile,
                                                  Optimizer.solidcell,
                                                  outfilename, Optimizer.calc,
                                                  Optimizer.calc_method)
            Optimizer.output.write(stro)
        else:
            bulk1 = gen_solid(Optimizer.solidfile, Optimizer.solidcell,
                              outfilename)
            PureBulkEnpa = 0
        natomsbulk = len(bulk1)
        Optimizer.solidbulk = bulk1.copy()
        Optimizer.summary.write('CIBS Run Pure Bulk Energy per Atom:' +
                                repr(PureBulkEnpa) + '\n')
        Optimizer.purebulkenpa = PureBulkEnpa
        Optimizer.natomsbulk = natomsbulk
    indiv.set_cell(Optimizer.solidcell)
    indiv.set_pbc(True)
    if Optimizer.restart_ints == 0:
        outt = find_defects(indiv, Optimizer.solidbulk, Optimizer.sf)
    else:
        indicop = [atm for atm in indiv if atm.symbol != 'X']
        indiv = Atoms(cell=Optimizer.solidcell, pbc=True)
        for atm in indicop:
            indiv.append(atm)
        outt = [
            indiv[0:Optimizer.restart_ints], indiv[Optimizer.restart_ints::],
            Atoms(),
            Atoms(), 'Assuming first ' + repr(Optimizer.restart_ints) +
            ' are interstitials\n'
        ]
    indi = outt[0].copy()
    bulki = outt[1].copy()
    individ = Individual(indi)
    individ.bulko = bulki.copy()
    individ.bulki = bulki.copy()
    individ.purebulkenpa = Optimizer.purebulkenpa
    individ.natomsbulk = Optimizer.natomsbulk
    individ.vacancies = outt[2].copy()
    individ.swaps = outt[3].copy()
    Optimizer.output.write(outt[4])
    return individ
Beispiel #5
0
def get_defect_indiv_random(Optimizer):
    """
    Function to generate a structopt Individual class structure with a defect structure.
    Inputs:
        Optimizer = structopt Optimizer class object
    Outputs:
        individ = structopt Individual class object containing defect structure data
    """
    #Initialize Bulk - Generate or load positions of bulk solid
    if not Optimizer.solidbulk:
        if 'Island_Method' not in Optimizer.algorithm_type:
            outfilename = os.path.join(os.path.join(os.getcwd(),Optimizer.filename),'Bulkfile.xyz')
        else:
            from mpi4py import MPI
            rank = MPI.COMM_WORLD.Get_rank()
            outfilename = os.path.join(os.path.join(os.getcwd(),Optimizer.filename+'-rank'+repr(rank)),'Bulkfile.xyz')
        if Optimizer.evalsolid:
            if Optimizer.parallel:
                from MAST.structopt_stem.tools.setup_calculator import setup_calculator
                Optimizer.calc = setup_calculator(Optimizer)
            bulk1, PureBulkEnpa, stro = gen_solid(Optimizer.solidfile,
                Optimizer.solidcell,outfilename,Optimizer.calc,Optimizer.calc_method)
            Optimizer.output.write(stro)
        else:
            bulk1 = gen_solid(Optimizer.solidfile,Optimizer.solidcell,outfilename)
            PureBulkEnpa = 0
        natomsbulk = len(bulk1)
        Optimizer.solidbulk = bulk1.copy()
        Optimizer.purebulkenpa = PureBulkEnpa
        Optimizer.natomsbulk = natomsbulk
    # Identify nearby atoms for region 2 inclusion
    bulk = Optimizer.solidbulk.copy()
    bulkcom = bulk.get_center_of_mass()
    bulk.translate(-bulkcom)
    if Optimizer.sf != 0:
        bulk.append(Atom(position=[0,0,0]))
        nbulk = Atoms(pbc=True, cell=bulk.get_cell())
        nr2 = Atoms(pbc=True, cell=bulk.get_cell())
        for i in range(len(bulk)-1):
            dist = bulk.get_distance(-1,i)
            if dist <= Optimizer.sf:
                nr2.append(bulk[i])
            else:
                nbulk.append(bulk[i])
    else:
        nbulk = bulk.copy()
        nr2 = Atoms(pbc=True, cell=bulk.get_cell())
    #Update atom list with atoms in region 2
    natlist = []
    for sym,c,m,u in Optimizer.atomlist:
        atsym = [atm for atm in nr2 if atm.symbol==sym]
        natlist.append((sym,len(atsym),m,u))
    # Generate random individual and region 2
    if 'sphere' in Optimizer.generate_flag:
        ind = gen_pop_sphere(Optimizer.atomlist,Optimizer.size)
    elif 'dumbbell' in Optimizer.generate_flag:
        ind = Atoms(cell=[Optimizer.size for i in range(3)], pbc=True)
        for sym,c,m,u in Optimizer.atomlist:
            if c > 0:
                dums = generate_dumbbells(c, dumbbellsym=sym, nindiv=1, solid = Optimizer.solidbulk, size=Optimizer.size)[0]
                ind.extend(dums)
    else:
        ind = gen_pop_box(Optimizer.atomlist,Optimizer.size)
    nnr2 = gen_pop_sphere(natlist, Optimizer.sf*2.0)
    nnr2.translate([-Optimizer.sf,-Optimizer.sf,-Optimizer.sf])
    nnr2.set_pbc(True)
    nnr2.set_cell(bulk.get_cell())
    # Initialize class individual with known values
    individ = Individual(ind)
    individ.purebulkenpa = Optimizer.purebulkenpa
    individ.natomsbulk = Optimizer.natomsbulk
    # Combine individual with R2
    icom = ind.get_center_of_mass()
    ind.translate(-icom)
    ind.extend(nnr2)
    ind.set_pbc(True)
    ind.set_cell(bulk.get_cell())
    # Recenter structure
    nbulk.translate(bulkcom)
    ind.translate(bulkcom)
    individ[0] = ind.copy()
    individ.bulki = nbulk.copy()
    individ.bulko = nbulk.copy()
    bulk = nbulk.copy()
    bul = bulk.copy()
    for atm in individ[0]:
        bul.append(atm)
    indices = []
    for sym,c,m,u in Optimizer.atomlist:
        if c < 0:
            if Optimizer.randvacst:
                alist = [one for one in bul if one.symbol==sym]
                count = abs(c)
                while count > 0:
                    indices.append(random.choice(alist).index)
                    count -= 1
            else:
                pos = individ[0][0:Optimizer.natoms].get_center_of_mass()
                count = abs(c)
                bul.append(Atom(position=pos))
                alist = [one for one in bul if one.symbol==sym]
                alistd = [(bul.get_distance(len(bul)-1,one.index),one.index)
                			for one in alist]
                alistd.sort(reverse=True)
                bul.pop()
                while count > 0:
                    idx = alistd.pop()[1]
                    indices.append(idx)
                    count-=1
    if len(indices) !=0:
        nbulklist = [at for at in bul if at.index not in indices and at.index<len(bulk)]
        nalist = [at for at in bul if at.index not in indices and at.index>=len(bulk)]
        bulkn = Atoms(cell=bulk.get_cell(),pbc=True)
        for atm in nbulklist:
            bulkn.append(atm)
        individ.bulki = bulkn.copy()
        individ.bulko = bulkn.copy()
        newind = Atoms()
        for atm in nalist:
            newind.append(atm)
        newind.set_cell(individ[0].get_cell())
        newind.set_pbc(True)									
        individ[0] = newind
    return individ