def read_poscar(self,poscar=None): """ read in a poscar file or object This method reads in new structural information into the class. It does not write the POSCAR file which may already exist in the the simulation directory. Args: poscar (:obj:`str` or :obj:`pypospack.crystal.SimulationCell`): structure information file either provided as the location of a POSCAR file, or from an instance of :obj:`pypospack.crystal.SimulationCell` """ if poscar is None: poscar = os.path.join(self.task_directory,'POSCAR') if isinstance(poscar,crystal.SimulationCell): self.poscar = vasp.Poscar(poscar) elif isinstance(poscar,str): self.poscar = vasp.Poscar() self.poscar.read(poscar) else: msg = (\ "argument 'poscar' must be a filename or an instance " "of pypospack.crystal.SimulationCell, passed in {}" ).format(poscar) raise KeyError
def make_bulk_structure(filename_poscar_in, filename_outcar_in=None, supercell=None): poscar_in = vasp.Poscar() poscar_in.read(filename=filename_poscar_in) if filename_outcar_in is not None: # add magnetic moments to the structure outcar_in = vasp.Outcar() outcar_in.read(filename=filename_outcar_in) magnetic_moments = outcar_in.get_magnetic_moments() for i, atom in enumerate(poscar_in.atomic_basis): atom.magmom = magnetic_moments[i][-1] z_distances = [k.position[2] for k in poscar_in.atomic_basis] min_z_distance = min(z_distances) for atom in poscar_in.atomic_basis: atom.position[2] = atom.position[2] - min_z_distance # make supercell if supercell is not None: poscar_out = vasp.Poscar( crystal.make_super_cell(structure=poscar_in, sc=supercell)) else: poscar_out = poscar_in # sort by z_axis new_atomic_basis = [] while len(poscar_out.atomic_basis) > 0: z_distances = [k.position[2] for k in poscar_out.atomic_basis] idx_min = z_distances.index(min(z_distances)) new_atomic_basis.append(copy.deepcopy( poscar_out.atomic_basis[idx_min])) del poscar_out.atomic_basis[idx_min] poscar_out.atomic_basis = new_atomic_basis return poscar_out
def __init__(self, task_name, task_directory, task_type=None, structure_filename='POSCAR', restart=False, fullauto=False): self.is_restart = restart self.is_fullauto = fullauto self.potential = None self.structure = None # set structure attribute self.structure_filename = structure_filename self.structure = vasp.Poscar() self.structure.read(self.structure_filename) self.running_filename = 'running' self.simulation_type = 'gulp' # gulp information self.gulp_input_filename = 'gulp.in' self.gulp_output_filename = 'gulp.out' self.gulp_bin = os.environ['GULP_BIN'] # flowcontrol variables self.results_processed = None # structure information self.structure_filename = structure_filename self.structure = vasp.Poscar() self.structure.read(self.structure_filename) # potential information self.potential = None self._parameters = None self.eam_setfl_filename = None # minimization flags self.is_optimize = True self.is_constant_pressure = True self.is_constant_volume = False # configuration self.configuration = OrderedDict() self.configuration['potential'] = None self.configuration['parameters'] = None # results self.results = OrderedDict() self.results_filename = '{}.results.dat'.format(task_name) # status # initialize the parent class Task.__init__(self, task_name=task_name, task_directory=task_directory, restart=restart)
def test__make_Ni_fcc__cubic_cell(): filename = 'Ni_fcc_cubic.vasp' o = FaceCenteredCubic('Ni', structure_type='cubic') o_vasp_writer = vasp.Poscar(o) o_vasp_writer.write(filename) assert os.path.isfile(filename) o_vasp_reader = vasp.Poscar(o) o_vasp_reader.read(filename)
def test_init_ase_cell(self): atoms = ase.build.bulk('Cu', 'fcc', cubic=True) sim_cell = vasp.Poscar(atoms) #check to see if we produce the correct object assert isinstance(sim_cell, crystal.SimulationCell) #check to see that lattice parameter is correct assert sim_cell.a0 == 1 #check to see that the H matrix is an nd.array assert isinstance(sim_cell.H, np.ndarray) #check to see that the shape of the H matrix is correct assert sim_cell.H.shape == (3, 3) #check to see that the H matrix is correct for i in range(3): for j in range(3): assert abs(atoms.cell[i, j] - sim_cell.H[i, j]) < 1e-6 #check to see that the number of atoms are correct assert len(atoms) == len(sim_cell.atomic_basis) #check to see that the positions are correct for i, a in enumerate(atoms): assert sim_cell.atomic_basis[i].symbol == a.symbol direct_x_ase = crystal.cartesian2direct(a.position, atoms.cell) direct_x_pypospack = sim_cell.atomic_basis[i].position for j in range(3): assert abs(direct_x_ase[j] - direct_x_pypospack[j]) < 1e-6
def __init__(self,task_name,task_directory,restart=True): Task.__init__(self,task_name,task_directory,restart) self.poscar = vasp.Poscar() self.incar = vasp.Incar() self.potcar = vasp.Potcar() self.kpoints = vasp.Kpoints() self.contcar = vasp.Contcar()
def read_poscar_file(self, filename=None): if type(filename) is str: self.structure_filename = filename if self.structure_filename is None: raise ValueError('PhontsInputFile requires a structure file') self.simulation_cell = vasp.Poscar() self.simulation_cell.read(self.structure_filename)
def test_poscar_init_ppp(): """ test if we can build from pypospack.crystal """ symbol = 'Cu' sg = 'fcc' a = 3.6 obj_ppp_1 = crystal.SimulationCell( ase.build.bulk(symbol,sg,a=a,cubic=True)) obj_ppp_2 = vasp.Poscar(obj_ppp_1)
def test_poscar_init_ase(): """ test if we can build from ase """ symbol = 'Cu' sg = 'fcc' a = 3.6 obj_ppp = crystal.SimulationCell( ase.build.bulk(symbol,sg,a=a,cubic=True)) poscar = vasp.Poscar(obj_ppp)
def set_poscar(self,poscar_info): cond1 = isinstance(incar_info,str) cond2 = os.path.isabs(incar_info) if cond1 and cond2: self.poscar.read(poscar) elif isinstance(poscar,pypospack.crystal.SimulationCell): self.poscar = vasp.Poscar(poscar) else: raise ValueError
def vasp_structural_minimization(structure, task_name): assert isinstance(structure, pypospack.crystal.SimulationCell) assert isinstance(task_name, str) vasp_simulation = vasp.VaspSimulation() vasp_simulation.simulation_directory = task_dir vasp_simulation.poscar = vasp.Poscar(structure) vasp_simulation.incar = vasp.Incar() vasp_simulation.potcar = vasp.Potcar() vasp_simulation.kpoints = vasp.Kpoints()
def _create_structure_file(self,src,dst): # read in vasp file src_vasp_structure = vasp.Poscar() src_vasp_structure.read_file(fname_in=src) if self._new_a1 is not None: src_vasp_structure.lattice_parameter = self._new_a1 dst_lmps_structure = StructureFile(obj=src_vasp_structure) dst_lmps_structure.write_file(fname_out=dst)
def __init__(self, taskname, task_dir): self.taskname = taskname self.task_directory = task_dir self.structure_filename = None self.simulation_cell = vasp.Poscar() self.filename = os.path.join(\ self.task_directory, 'gulp.in') self.potential = None self._create_task_directory()
def test_read(self): atoms = ase.build.bulk('Cu', 'fcc', cubic=True) sim_cell_init = vasp.Poscar(atoms) sim_cell_init.write('POSCAR') sim_cell_copy = vasp.Poscar() sim_cell_copy.read('POSCAR') #check to see if we produce the correct object assert isinstance(sim_cell_copy, crystal.SimulationCell) #check to see if the lattice parameter is correct assert sim_cell_copy.a0 == sim_cell_init.a0 #check to see that the H matrix is an nd.array assert isinstance(sim_cell_copy.H, np.ndarray) #check to see that the shape of the H matrix is correct assert sim_cell_copy.H.shape == (3, 3) #check to see that the H matrix is correct for i in range(3): for j in range(3): assert abs(sim_cell_copy.H[i, j] - sim_cell_init.H[i, j]) < 1e-6 #check to see that the number of atoms are correct assert len(sim_cell_init.atomic_basis) == len( sim_cell_copy.atomic_basis) for i, a in enumerate(sim_cell_init.atomic_basis): #check to see that the symbols are correct assert sim_cell_copy.atomic_basis[ i].symbol == sim_cell_init.atomic_basis[i].symbol #check to see that the positions are correct direct_x_init = sim_cell_init.atomic_basis[i].position direct_x_final = sim_cell_copy.atomic_basis[i].position for j in range(3): assert abs(direct_x_init[j] - direct_x_final[j]) < 1e-6
def validate_configuration(configuration): assert not os.path.exists(task_directory) eam_setfl_filename = configuration['potential']['setfl_filename'] if type(eam_setfl_filename) is str: assert os.path.isfile(eam_setfl_filename) else: assert eam_setfl_filename is None structure_filename = configuration['structure']['filename'] assert type(structure_filename) is str assert os.path.isfile(structure_filename) poscar = vasp.Poscar() poscar.read(structure_filename)
class test_poscar_init_fcc_unit(unittest.TestCase): def setUp(self): self.symbol = 'Cu' self.sg = 'fcc' self.a = 3.6 def test_poscar_init_ase(unittest.TestCase): """ test if we can build from ase """ obj_ppp = crystal.SimulationCell( ase.build.bulk(symbol,sg,a=a,cubic=True)) poscar = vasp.Poscar(obj_ppp) self.assert
def __init__(self, task_name, task_directory, task_type='single_point', task_requires=None, structure_filename='POSCAR', restart=False, fullauto=False): self.is_restart = restart self.is_fullauto = fullauto self.potential = None self.structure = None self.structure_filename = structure_filename self.structure = vasp.Poscar() self.structure.read(self.structure_filename) self.lammps_input_filename = 'lammps.in' self.lammps_output_filename = 'lammps.out' self.lammps_structure_filename = 'lammps.structure' self.lammps_potentialmod_filename = 'potential.mod' self.lammps_setfl_filename = None self.lammps_bin = os.environ['LAMMPS_BIN'] # flowcontrol filename self.results = None self.results_filename = 'pypospack.{}.out'.format(task_name) self.process = None self.results_processed = None # configuration self.configuration = OrderedDict() self.task_type = task_type self.conditions_INIT = None self.conditions_CONFIG = None self.conditions_READY = None self.conditions_RUNNING = None self.conditions_POST = None self.conditions_FINISHED = None self.conditions_ERROR = None Task.__init__(self, task_name=task_name, task_directory=task_directory, restart=restart) self.task_requires = copy.deepcopy(task_requires)
def gulp_positions_to_string(self, structure='POSCAR'): """ returns the gulp position section to create simulation cell Args: structure (str): the filename of the POSCAR format file. Default is 'POSCAR'. If an object which subclasses the pyposmat.crystal.SimulationCell class is passed, this method will use this that class instead. Returns str: """ sim_cell = None if isinstance(structure, crystal.SimulationCell): sim_cell = crystal.SimulationCell(poscar) else: sim_cell = vasp.Poscar() try: sim_cell.read(structure) except FileNotFoundError as e: msg = 'PYPOSPACK_ERROR: cannot find the POSCAR file:{}'.format( structure) raise H = sim_cell.H * sim_cell.a0 str_out = "vectors\n" str_out += "{:.10f} {:.10f} {:.10f}\n".format(H[0, 0], H[0, 1], H[0, 2]) str_out += "{:.10f} {:.10f} {:.10f}\n".format(H[1, 0], H[1, 1], H[1, 2]) str_out += "{:.10f} {:.10f} {:.10f}\n".format(H[2, 0], H[2, 1], H[2, 2]) str_out += "fractional\n" for s in sim_cell.symbols: for a in sim_cell.atomic_basis: if a.symbol == s: try: str_out += "{} core {} {} {}\n".format(\ s,a.position[0],a.position[1],a.position[2]) except: print(s) print(a.symbol) print(a.position) raise return str_out
def test____init___(): symbols = configuration['potential']['symbols'] task_name = configuration['task']['task_name'] task_directory = configuration['task']['task_directory'] structure_filename = configuration['structure']['filename'] eam_setfl_filename = configuration['potential']['setfl_filename'] restart=False fullauto=False cleanup(task_directory) assert not os.path.exists(task_directory) assert type(eam_setfl_filename) is str assert type(structure_filename) is str poscar = vasp.Poscar() poscar.read(structure_filename) assert os.path.isfile(eam_setfl_filename) assert os.path.isfile(structure_filename) #<--- code being tested from pypospack.task.lammps import LammpsElasticCalculation lammps_task = LammpsElasticCalculation( task_name = task_name, task_directory = task_directory, structure_filename = structure_filename) #<--- expected behavior lammps_setfl_filename = '{}.eam.alloy'.format("".join(symbols)) task_type = configuration['task']['task_type'] #<--- check directory structure assert os.path.isdir( os.path.abspath(lammps_task.task_directory)) assert os.listdir(lammps_task.task_directory) == [] #<--- check attributes assert lammps_task.task_name == task_name assert os.path.abspath(lammps_task.task_directory)\ == os.path.abspath(task_directory) assert lammps_task.task_type == task_type assert lammps_task.lammps_bin == os.environ['LAMMPS_BIN'] assert lammps_task.lammps_input_filename == 'lammps.in' assert lammps_task.lammps_output_filename == 'lammps.out' assert lammps_task.lammps_structure_filename == 'lammps.structure' assert lammps_task.lammps_setfl_filename == None assert lammps_task.potential is None assert lammps_task.structure_filename == structure_filename assert isinstance(lammps_task.structure,crystal.SimulationCell) assert lammps_task.status == 'INIT'
def write_lammps_structure_filename(self): _supercell = self.supercell _structure_filename_src \ = self.configuration['structure']['structure_filename'] _structure = vasp.Poscar() _structure.read(_structure_filename_src) self.structure = crystal.make_super_cell(structure=_structure, sc=_supercell) self.lammps_structure = lammps.LammpsStructure(\ obj=self.structure) _str_out = self.lammps_input_file_to_string() _lammps_filename = os.path.join(self.task_directory, filename) with open(_lammps_filename, 'w') as f: f.write(_str_out)
def create_simulation(self): # initialize input files self.vs = vasp.VaspSimulation() self.vs.poscar = vasp.Poscar(obj_structure) self.vs.incar = vasp.Incar() self.vs.potcar = vasp.Potcar() self.vs.kpoints = vasp.Kpoints() self.vs.xc = self.xc self.vs.simulation_directory = self.sim_dir self.vs.symbols = self.vs.poscar.symbols # configure potcar file self.vs.potcar.symbols = self.vs.symbols self.vs.potcar.xc = self.vs.xc fn_potcar = os.path.join(self.vs.simulation_directory, 'POTCAR') self.vs.potcar.write(fn_potcar) self.vs.potcar.read(fn_potcar) self.vs.encut = max(self.vs.potcar.encut_max) self.vs.natoms = self.vs.poscar.n_atoms # configure incar file magmom_0 = 1.0 self.vs.incar.ismear = 1 self.vs.incar.sigma = 0.20 self.vs.incar.ispin = 2 self.vs.incar.magmom = "{}*{}".format(self.vs.natoms, magmom_0) self.vs.incar.ibrion = 2 self.vs.incar.isif = 3 self.vs.incar.potim = 0.5 self.vs.incar.ediffg = -0.001 self.vs.poscar.write(os.path.join(\ self.vs.simulation_directory,"POSCAR")) self.vs.incar.write(os.path.join(\ self.vs.simulation_directory,"INCAR")) self.vs.kpoints.write(os.path.join(\ self.vs.simulation_directory,"KPOINTS"))
def postprocess(self): """Some postprocess steps - removes the POTCAR file, for copyright reasons - reads in the OUTCAR file - reads in the OSZICAR file - reads in the CONTCAR file """ try: os.remove(os.path.join(self.task_directory,'POTCAR')) except FileNotFoundError: pass self.outcar = vasp.Outcar() self.outcar.read(os.path.join(self.task_directory,'OUTCAR')) self.contcar = vasp.Poscar() self.contcar.read(os.path.join(self.task_directory,'CONTCAR')) # self.oszicar = vasp.Oszicar() # self.oszicar.read(os.path.join(self.task_directory,'OSZICAR')) self.check_simulation_for_failure() self.get_results()
def get_gulpinputfile_structuresection(self,filename=None): if filename is not None: self.structure_filename = filename # read in the poscar file self.structure = vasp.Poscar() self.structure.read(self.structure_filename) H = self.structure.H * self.structure.a0 str_out = "vectors\n" str_out += "{:.10f} {:.10f} {:.10f}\n".format(H[0,0],H[0,1],H[0,2]) str_out += "{:.10f} {:.10f} {:.10f}\n".format(H[1,0],H[1,1],H[1,2]) str_out += "{:.10f} {:.10f} {:.10f}\n".format(H[2,0],H[2,1],H[2,2]) str_out += "fractional\n" for s in self.structure.symbols: for a in self.structure.atomic_basis: if a.symbol == s: str_out += "{s} core {r1:10.6f} {r2:10.6f} {r3:10.6f}\n".format( s=s, r1=a.position[0], r2=a.position[1], r3=a.position[2]) return str_out
symbol = 'Ni' sg = 'sc' a = 3.508 task_name = 'minimize_init' job_name = "{}_{}_min1".format(symbol, sg) email = "*****@*****.**" qos = "phillpot" ntasks = 16 time = "1:00:00" init_directory = os.getcwd() task_dir = os.path.join(os.getcwd(), task_name) vasp_simulation = vasp.VaspSimulation() vasp_simulation.simulation_directory = task_dir vasp_simulation.poscar = vasp.Poscar(\ ase.build.bulk(symbol,sg,a,cubic=True)) vasp_simulation.incar = vasp.Incar() vasp_simulation.potcar = vasp.Potcar() vasp_simulation.kpoints = vasp.Kpoints() # create directory # create vasp simulation directory and files # delete the directory and the contents if it exists if os.path.exists(vasp_simulation.simulation_directory): shutil.rmtree(vasp_simulation.simulation_directory) os.mkdir(vasp_simulation.simulation_directory) # configure potcar file print('confguring potcar file...') vasp_simulation.xc = 'GGA' vasp_simulation.symbols = vasp_simulation.poscar.symbols
# dir_conv_kpoints, # '{}x{}x{}'.format(k,k,k)) # slurm_job_name = "{}_{}_{}".format( # symbol,sg,"{}x{}x{}".format(k,k,k)) sim_dir = os.path.join(dir_conv_kpoints, '{}x{}x{}'.format(k, k, k)) slurm_job_name = '{}_{}_{}'.format(symbol, sg, '{}x{}x{}'.format(k, k, k)) vasp_simulation = vasp.VaspSimulation(sim_dir=sim_dir) # if os.path.exists(vasp_simulation.simulation_directory): # shutil.rmtree(vasp_simulation.simulation_directory) # os.mkdir(vasp_simulation.simulation_directory) if os.path.exists(sim_dir): shutil.rmtree(sim_dir) os.mkdir(sim_dir) # write poscar vasp_simulation.poscar = vasp.Poscar(vasp.read_poscar_file(\ filename = os.path.join(dir_min_0,'CONTCAR'))) # vasp_simulation.poscar.write( # os.path.join(vasp_simulation.simulation_directory,"POSCAR")) vasp_simulation.poscar.write(os.path.join(sim_dir, "POSCAR")) # write potcar vasp_simulation.xc = xc vasp_simulation.symbols = vasp_simulation.poscar.symbols vasp_simulation.potcar.symbols = vasp_simulation.symbols vasp_simulation.potcar.xc = vasp_simulation.xc # vasp_simulation.potcar.write( # os.path.join(vasp_simulation.simulation_directory,"POTCAR")) vasp_simulation.potcar.write(os.path.join(sim_dir, "POTCAR")) # write incar vasp_simulation.incar = vasp.Incar() vasp_simulation.incar.read(\ os.path.join(dir_min_0,'INCAR'))
def test_write(self): atoms = ase.build.bulk('Cu', 'fcc', cubic=True) sim_cell = vasp.Poscar(atoms) sim_cell.write('POSCAR') assert os.path.exists('POSCAR') os.remove('POSCAR')
import pypospack.io.vasp as vasp import pypospack.io.lammps as lammps if __name__ == '__main__': filename_in = 'rsrc/MgO_NaCl_unit.vasp' filename_out = 'structure.dat' vasp_structure = vasp.Poscar() vasp_structure.read(filename_in) lammps_structure = lammps.LammpsStructure(\ obj=vasp_structure) print("a0", vasp_structure.a0, lammps_structure.a0) print("H", vasp_structure.H, lammps_structure.H) lammps_structure.write(filename=filename_out)
if v['sg'] in ['fcc', 'bcc', 'diamond', 'sc', 'hcp']: if isinstance(v['symbols'], list): if len(v['symbols']) == 1: v['symbols'] = v['symbols'][0] else: raise KeyError( 'cannot have more than one symbol in {}'.format(sg)) else: raise KeyError( 'sg is an unsupported space group, passed in {}'.format(sg)) obj_poscar = None if v['shape'] == 'cubic': obj_poscar = vasp.Poscar(\ ase.build.bulk(\ v['symbols'], v['sg'], a=v['a0'], cubic=True)) elif v['shape'] == 'ortho': obj_poscar= vasp.Poscar(\ ase.build.bulk(\ v['symbols'], v['sg'], a=v['a0'], ortho=True)) elif v['shape'] == 'prim': obj_poscar = vasp.Poscar(\ ase.build.bulk(\ v['symbols']. v['sg'], a0=v['a0']))
full_auto = False incar_dict = {} incar_dict['ismear'] = 0 incar_dict['sigma'] = 0.05 incar_dict['ispin'] = 1 slurm_dict = {} slurm_dict['email'] = '*****@*****.**' slurm_dict['qos'] = 'phillpot' slurm_dict['ntasks'] = 16 slurm_dict['time'] = "1:00:00" # not sure if this works #poscar = vasp.Poscar(structure_filename) poscar = vasp.Poscar() poscar.read(structure_filename) # this approach is taken from the VASP methodology of generating automated kpoint meshes # Ref: https://cms.mpi.univie.ac.at/vasp/vasp/Automatic_k_mesh_generation.html linear_kpoint_density = 3 # kpoints/(units reciprocal lattice) def get_kpoint_mesh(simulation_cell, linear_kpoint_density): """ Args: simulation_cell(pypospack.crystal.SimulationCell): self-explanatory. """ # assert isinstance(simulation_cell,crystal.SimulationCell) b1_length = np.dot(simulation_cell.b1, simulation_cell.b1)**0.5
def read_structure_file(self,filename=None): if filename is not None: self.structure_filename self.structure = vasp.Poscar() self.structure.read(self.structure_filename)