Exemplo n.º 1
0
def impure_faults(slab_cell, impurity, write_fn, sys_info, resolution, 
                      basename, dim=2, limits=(1,1), mkdir=False, vacuum=0., 
                      suffix='in', line_vec=np.array([1., 0., 0.]), relax=None):
    '''Runs gamma surface calculations with an impurity (or impurity cluster)
    inserted at the specified atomic sites. <site> gives the index of the atom
    to be replaced by the impurity 
    '''

    mutate.cell_defect(slab_cell, impurity, use_displaced=True)
    
    # create input files for generalised stacking fault calculations
    if dim == 1:
        if limits == (1, 1):
            limits = 1
            
        gsf.gamma_line(slab_cell, line_vec, resolution, write_fn, sys_info, 
                       limits=limits, vacuum=vacuum, basename=basename, suffix=suffix,
                       mkdir=False, relax=relax)
    else: # dim == 2
        gsf.gamma_surface(slab_cell, resolution, write_fn, sys_info, mkdir=mkdir,
                                 basename=basename, suffix=suffix, limits=limits)
                                 
    # return the slab to its original state
    mutate.undo_defect(slab_cell, impurity)
    
    return
Exemplo n.º 2
0
def write_castep(outstream, cas_struc, sys_info, defected=True, to_cart=False,
         add_constraints=False, relax_type=None, impurities=None, do_relax=None, prop=None):
    '''Writes the information in <cas_struc> and <sys_info> to the specified 
    output stream. <prop> is a dummy variable to make input consistent with 
    <write_gulp>.
    
    #!!! Do we need some facility to change the calculation type (eg. from 
    #!!! fixed-cell relaxation to a SCF calculation) without the user having
    #!!! to edit the .param (.cell?) file directly?
    '''
    
    # insert defect(s), if supplied
    if not (impurities is None):
        if mutate.is_single(impurities):
            mutate.cell_defect(cas_struc, impurities, use_displaced=True)
        elif mutate.is_coupled(impurities):
            mutate.cell_defect_cluster(cas_struc, impurities, use_displaced=True)
        else:
            raise TypeError("Supplied defect not of type <Impurity>/<CoupledImpurity>")

    # begin by writing the cell block
    outstream.write('%BLOCK lattice\n')
    cas_struc.writeLattice(outstream.write)
    outstream.write('%ENDBLOCK lattice\n\n')
    outstream.write('FIX_ALL_CELL true\n\n')

    # write the atom block to file
    outstream.write('%BLOCK positions_frac\n')
    # can this be done using <cas_struc.write(outstream, defected=defected)> ?
    for atom in cas_struc:
        atom.write(outstream.write, defected=defected)
        
    outstream.write('%ENDBLOCK positions_frac\n\n')

    # write constraints
    if add_constraints:
        cas_struc.write_constraints(outstream)

    # write pseupotential filenames and Monkhorst-Pack k-point grid
    if sys_info['psps']:
        outstream.write('{}\n\n'.format(sys_info['psps']))

    util.write_kgrid(outstream.write, sys_info['mp_kgrid'])

    outstream.close()

    # now we write the .param file
    # first, extract the root name from <outstream>
    root_name = re.compile('(?P<root>.+)\.cell')
    param_name = root_name.match(outstream.name).group('root') + '.param'
    
    # write simulation parameters to the .param file
    param_file = open(param_name, 'w')
    param_file.write(sys_info['param'])
    param_file.close()
    
    # remove any defects that have been inserted
    if not (impurities is None):
        mutate.undo_defect(cas_struc, impurities)
        
    return
Exemplo n.º 3
0
def write_lammps(outstream,
                 struc,
                 sys_info,
                 defected=True,
                 do_relax=True,
                 to_cart=True,
                 add_constraints=False,
                 relax_type='conv',
                 impurities=None):
    '''Writes structure contained in <lmp_struc> to <outstream>. Note that,
    because atomic coordinates in LAMMPS are, by default, given in cartesian
    coordinates, the write function here sets the default value of <to_cart>
    to be <True>.

    #!!!At present, only crystals with orthogonal lattice vectors are supported.
    '''

    outstream.write('This is the first line of a LAMMPS file\n\n')

    # insert any impurities
    if not (impurities is None):
        if mutate.is_single(impurities):
            mutate.cell_defect(struc, impurities, use_displaced=defected)
        elif mutate.is_coupled(impurities):
            mutate.cell_defect_cluster(struc,
                                       impurities,
                                       use_displaced=defected)
        else:
            raise TypeError(
                "Supplied defect not of type <Impurity>/<CoupledImpurity>")

    # calculate total number of atoms in <lmp_struc> plus any impurities
    outstream.write('  {} atoms\n\n'.format(len(struc)))

    # number of distinct elements
    outstream.write('  {} atom types\n\n'.format(struc.number_of_elements()))

    # simulation cell dimensions
    lattice = struc.getLattice()
    outstream.write(' 0. {:.6f} xlo xhi\n'.format(lattice[0, 0]))
    outstream.write(' 0. {:.6f} ylo yhi\n'.format(lattice[1, 1]))
    outstream.write(' 0. {:.6f} zlo zhi\n'.format(lattice[2, 2]))
    outstream.write(' {:.3f} {:.3f} {:.3f} xy xz yz\n\n'.format(
        lattice[1, 0], lattice[2, 0], lattice[2, 1]))

    # write atoms to file
    outstream.write('Atoms\n\n')
    assign_indices(struc)
    for i, atom in enumerate(struc):
        #atom.set_index(i+1)
        atom.write(outstream,
                   lattice=lattice,
                   defected=defected,
                   to_cart=to_cart)

    # atomic masses
    outstream.write('\nMasses\n\n')
    for species in sys_info['masses']:
        outstream.write('{} {:.2f}\n'.format(species[0], species[1]))

    # change the input script
    read_and_write_data(outstream, sys_info)

    iscript = open('script.{}'.format(outstream.name), 'w')
    iscript.write(sys_info['input_script'])
    iscript.close()

    outstream.close()
    return
Exemplo n.º 4
0
def write_qe(outstream,
             qe_struc,
             sys_info,
             defected=True,
             to_cart=False,
             add_constraints=False,
             relax_type='scf',
             impurities=None,
             do_relax=None,
             prop=None):
    '''Writes crystal structure contained in <qe_struc> to <outstream>.<prop> is
    a dummy variable to make input consistent with <write_gulp>.
    '''

    # if isolated/coupled defects have been supplied, add these to the structure
    if not (impurities is None):
        if mutate.is_single(impurities):
            mutate.cell_defect(qe_struc, impurities, use_displaced=True)
        elif mutate.is_coupled(impurities):
            mutate.cell_defect_cluster(qe_struc,
                                       impurities,
                                       use_displaced=True)
        else:
            raise TypeError(
                "Supplied defect not of type <Impurity>/<CoupledImpurity>")

    # write namelists
    for block in namelists:
        # test that the namelist <block> is not empty
        if not (block in sys_info['namelists'].keys()):
            continue

        # else, write the block
        outstream.write(' {}\n'.format(block))
        for variable in sys_info['namelists'][block]:
            if variable == 'calculation':
                if not (relax_type is None):
                    outstream.write(
                        '    calculation = \'{}\'\n'.format(relax_type))
                else:
                    print("No calculation type specified; defaulting to scf")
                    outstream.write('    calculation = \'scf\'\n')
            elif variable == 'nat':
                outstream.write('    nat = {}\n'.format(len(qe_struc)))
            elif variable == 'ntyp':
                outstream.write('    ntyp = {}\n'.format(
                    qe_struc.number_of_elements()))
            else:
                outstream.write('    {} = {}\n'.format(
                    variable, sys_info['namelists'][block][variable]))
        outstream.write(' /\n')

    # write pseudopotentials
    outstream.write(" ATOMIC_SPECIES\n")
    for psp in sys_info['cards']['ATOMIC_SPECIES']:
        outstream.write(psp + '\n')

    # write atomic coordinates
    outstream.write(' ATOMIC_POSITIONS { crystal }\n')
    qe_struc.write(outstream,
                   defected=defected,
                   add_constraints=add_constraints)

    # write lattice
    outstream.write(' CELL_PARAMETERS')
    outstream.write(' {{ {} }}\n'.format(sys_info['cards']['CELL_PARAMETERS']))
    qe_struc.writeLattice(outstream.write)

    # write k-point grid
    if not sys_info['cards']['K_POINTS']:
        # use the gamma point
        outstream.write(' K_POINTS { gamma }\n')
    else:
        # use automatically generated Monkhorst-Pack grid
        outstream.write(' K_POINTS { automatic }\n')
        grid = sys_info['cards']['K_POINTS']['spacing']
        outstream.write('  {} {} {}'.format(grid[0], grid[1], grid[2]))
        outstream.write(' {}\n'.format(sys_info['cards']['K_POINTS']['shift']))

    outstream.close()

    # finally, remove any impurity atoms that have been appended to <qe_struc>
    if not (impurities is None):
        mutate.undo_defect(qe_struc, impurities)

    return
Exemplo n.º 5
0
def write_gulp(outstream,
               struc,
               sys_info,
               defected=True,
               do_relax=True,
               to_cart=True,
               add_constraints=False,
               relax_type='conv',
               impurities=None,
               prop=True,
               pfractional=False,
               maxiter=500,
               rI_centre=np.zeros(2),
               transition=False):
    '''Writes the crystal <gulp_struc> to <outstream>. If <defected> is True,
    then it uses the displaced coordinates, otherwise it uses the regular atomic
    coordinates (ie. their locations in a perfect crystal with the provided
    dimensions. If <supercell> is True, then the simulation cell has 3D periodic
    boundary conditions. If not, it is assumed to be a 1D-periodic simulation
    cell, characterised by the cell height.
    
    Note that if <relax> is False, then the variable <relax_type> will be ignored.
    '''

    # determine if boundary conditions are 1D-periodic (polymer) or 3D-periodic
    # (supercell)
    get_class = re.compile(r"<class\s+'(?:\w[\w\d]*\.)*(?P<class>\w[\w\d]*)'>")
    try:
        struc_type = get_class.search(str(struc.__class__)).group('class')
    except IndexError:
        print("Class of <struc> not found.")
        sys.exit(1)

    # insert impurities, if supplied. If <defected> is False, we are using the
    # perfect (ie. dislocation-free) cell, and so we DO NOT make use of the
    # displaced coordinates when setting the coordinates of the impurity atoms
    if not (impurities is None):
        if mutate.is_single(impurities):
            mutate.cell_defect(struc, impurities, use_displaced=defected)
        elif mutate.is_coupled(impurities):
            mutate.cell_defect_cluster(struc,
                                       impurities,
                                       use_displaced=defected)
        else:
            raise TypeError(
                "Supplied defect not of type <Impurity>/<CoupledImpurity>")

    # write simulation cell geometry to file
    if struc_type in rod_classes:
        struc.specifyRegions(rI_centre=rI_centre)

        # polymer cell -> write cell height
        preamble(outstream,
                 do_relax=do_relax,
                 polymer=True,
                 relax_type=relax_type,
                 maxiter=maxiter,
                 add_constraints=add_constraints,
                 transition=transition)
        height = struc.getHeight()
        outstream.write('pcell\n')
        outstream.write('{:.6f} 0\n'.format(height))
        cell_lattice = struc.getBaseCell().getLattice()

        # write atoms to output
        if pfractional:
            writeRegion(struc.getRegionIAtoms(),
                        cell_lattice,
                        outstream,
                        1,
                        defected,
                        coordType='pfractional',
                        add_constraints=add_constraints)
            writeRegion(struc.getRegionIIAtoms(),
                        cell_lattice,
                        outstream,
                        2,
                        defected,
                        coordType='pfractional',
                        add_constraints=add_constraints)
        else:  # cartesian
            writeRegion(struc.getRegionIAtoms(),
                        cell_lattice,
                        outstream,
                        1,
                        defected,
                        add_constraints=add_constraints)
            writeRegion(struc.getRegionIIAtoms(),
                        cell_lattice,
                        outstream,
                        2,
                        defected,
                        add_constraints=add_constraints)
    else:
        # write lattice vectors
        preamble(outstream,
                 do_relax=do_relax,
                 relax_type=relax_type,
                 prop=prop,
                 maxiter=maxiter,
                 transition=transition)
        writeVectors(struc.getLattice(), outstream)

        if relax_type is None or relax_type == '':
            # give strain optimization flags
            outstream.write('0 0 0 0 0 0\n')
            # GULP requires that optimization flags be set for all atoms
            if do_relax:
                add_constraints = True

        # write atoms to output.
        outstream.write('frac\n')
        for atom in struc:
            atom.write(outstream,
                       lattice=struc.getLattice(),
                       defected=defected,
                       to_cart=to_cart,
                       add_constraints=add_constraints)

    # write system specific simulation parameters (interatomic potentials, etc.)
    for line in sys_info:
        if 'output' in line:
            continue
        elif 'dump' in line:
            continue
        else:
            outstream.write('{}\n'.format(line))

    # add restart lines and close the output file
    restart(outstream)
    outstream.close()

    # undo defect insertion
    if not (impurities is None):
        mutate.undo_defect(struc, impurities)
        if struc_type in rod_classes:
            struc.specifyRegions()

    return