def write(self, a, folder, sname=None, calc_type="GEOM_OPT"): """Writes input files for an Atoms object with a Castep calculator. | Args: | a (ase.Atoms): Atoms object to write. Can have a Castep | calculator attached to carry cell/param | keywords. | folder (str): Path to save the input files to. | sname (str): Seedname to save the files with. If not | given, use the name of the folder. | calc_type (str): Castep task which will be performed: | "GEOM_OPT" or "MAGRES" """ if calc_type == "GEOM_OPT" or calc_type == "MAGRES": if sname is None: sname = os.path.split(folder)[-1] # Same as folder name self._calc = deepcopy(self._calc) # We only use the calculator attached to the atoms object if a calc # has not been set when initialising the ReadWrite object OR we # have not called write() and made a calculator before. if self._calc is None: if isinstance(a.calc, Castep): self._calc = deepcopy(a.calc) self._create_calculator(calc_type=calc_type) else: self._update_calculator(calc_type) a.calc = self._calc with silence_stdio(): io.write( os.path.join(folder, sname + ".cell"), a, magnetic_moments="initial", ) write_param( os.path.join(folder, sname + ".param"), a.calc.param, force_write=True, ) if self.script is not None: stxt = open(self.script).read() stxt = stxt.format(seedname=sname) with open(os.path.join(folder, "script.sh"), "w") as sf: sf.write(stxt) else: raise (NotImplementedError( "Calculation type {} is not implemented." " Please choose 'GEOM_OPT' or 'MAGRES'".format(calc_type)))
def write(self, a): def set_vals(a, cut, kpn, fgm): a.calc.param.cut_off_energy = cut a.calc.cell.kpoints_mp_grid = kpn if self._gamma: # Compute the necessary offset for the gamma point offset = [0 if k % 2 == 1 else 1.0 / (2 * k) for k in kpn] a.calc.cell.kpoints_mp_offset = offset if fgm is not None: a.calc.param.fine_gmax = fgm set_vals(a, **self._basevals) prevjob = None for name, job in self._worktree.items(): set_vals(a, **job.values) try: os.mkdir(job.dir) except OSError: pass if self._reuse and os.path.isfile(job.cell): # If files exist and we shouldn't overwrite, skip print('Reusing files for {0}'.format(job.seed)) continue if self._sscript: script = open(self._sscript).read() script = script.replace('<seedname>', name) with open(job.seed, 'w') as outf: outf.write(script) if self._sreuse: a.calc.param.write_checkpoint = 'ALL' if prevjob is not None: a.calc.param.reuse = prevjob.name + '.check' print('Writing files for {0}'.format(job.seed)) write_castep_cell(open(job.cell, 'w'), a) write_param(job.param, a.calc.param, force_write=True) prevjob = job
def castep_write_input(a, folder, calc=None, name=None, script=None): """Writes input files for an Atoms object with a Castep calculator. | Args: | a (ase.Atoms): Atoms object to write. Can have a Castep | calculator attached to carry cell/param | keywords. | folder (str): Path to save the input files to. | calc (ase.Calculator): Calculator to attach to Atoms. If | present, the pre-existent one will | be ignored. | name (str): Seedname to save the files with. If not | given, use the name of the folder. | script (str): Path to a file containing a submission script | to copy to the input folder. The script can | contain the argument {seedname} in curly braces, | and it will be appropriately replaced. """ if name is None: name = os.path.split(folder)[-1] # Same as folder name if calc is not None: a.set_calculator(calc) if not isinstance(a.calc, Castep): a = a.copy() calc = Castep(atoms=a) a.set_calculator(calc) io.write(os.path.join(folder, name + '.cell'), a, magnetic_moments='initial') write_param(os.path.join(folder, name + '.param'), a.calc.param, force_write=True) if script is not None: stxt = open(script).read() stxt = stxt.format(seedname=name) with open(os.path.join(folder, 'script.sh'), 'w') as sf: sf.write(stxt)
def write_input_files(self, at, label): global _chdir_lock devel_code = self._orig_devel_code devel_code += ('SOCKET_IP=%s\nSOCKET_PORT=%d\nSOCKET_CLIENT_ID=%d\nSOCKET_LABEL=%d' % \ (self.server.ip, self.server.port, self.client_id, label)) self.castep.param.devel_code = devel_code # chdir not thread safe, so acquire global lock before using it orig_dir = os.getcwd() try: _chdir_lock.acquire() os.chdir(self.subdir) cellf = open('castep.cell', 'w') write_castep_cell(cellf, at, castep_cell=self.castep.cell) cellf.close() write_param('castep.param', self.castep.param, force_write=True) finally: os.chdir(orig_dir) _chdir_lock.release()
def convert_single_structure(gen_file, param_file, directory="."): atoms = io.read(gen_file) params = load_input_file(param_file) # Muon mass and gyromagnetic ratio mass_block = 'AMU\n{0} 0.1138' gamma_block = 'radsectesla\n{0} 851586494.1' ccalc = Castep(castep_command=params['castep_command']) ccalc.cell.kpoint_mp_grid.value = list_to_string(params['k_points_grid']) ccalc.cell.species_mass = mass_block.format( params['mu_symbol']).split('\n') ccalc.cell.species_gamma = gamma_block.format( params['mu_symbol']).split('\n') ccalc.cell.fix_all_cell = True # To make sure for older CASTEP versions atoms.set_calculator(ccalc) symbols = atoms.get_chemical_symbols() symbols[-1] = params['mu_symbol'] atoms.set_array('castep_custom_species', np.array(symbols)) name = "{}.cell".format(params['name']) cell_file = os.path.join(directory, name) io.write(cell_file, atoms) # Param file? if params['castep_param'] is not None: p = read_param(params['castep_param']).param # Set up task and geometry optimization steps p.task.value = 'GeometryOptimization' p.geom_max_iter.value = params['geom_steps'] p.geom_force_tol.value = params['geom_force_tol'] param_file = os.path.join(directory, "{}.param".format(params['name'])) write_param(param_file, p, force_write=True)