def read_cp2k_qm_kind(fh): """ Read a file in cp2k_input.qmmm_qm_kind This is the format produced by the QUIP cp2k_driver. Returns a list of tuples (qm_kind, atom_indices). """ filename, lines = read_text_file(fh) qm_kinds = [] current_qm_kind = None current_indices = [] for line in lines: line = line.strip() if line.startswith('&QM_KIND'): current_qm_kind = line.split()[1] current_indices = [] elif line.startswith('&END QM_KIND'): qm_kinds.append((current_qm_kind, current_indices)) elif line.startswith('MM_INDEX'): index = int(line.split()[1]) current_indices.append(index) else: raise ValueError('Unexpected line %r' % line) return qm_kinds
def read(self, filename): filename, lines = read_text_file(filename) for line in lines: dummy, key, value = line.split() self[key] = value
def CP2KDirectoryReader(run_dir, at_ref=None, proj='quip', calc_qm_charges=None, calc_virial=False, out_i=None, qm_vacuum=6.0, run_suffix='_extended', format=None): if at_ref is None: filepot_xyz = os.path.join(run_dir, 'filepot.xyz') if not os.path.exists(filepot_xyz): # try looking up one level filepot_xyz = os.path.join(run_dir, '../filepot.xyz') if os.path.exists(filepot_xyz): at_ref = Atoms(filepot_xyz) else: at_ref = Atoms(os.path.join(run_dir, 'cp2k_output.out'), format='cp2k_output') at = at_ref.copy() cp2k_output_filename, cp2k_output = read_text_file( os.path.join(run_dir, 'cp2k_output.out')) cp2k_params = CP2KInputHeader( os.path.join(run_dir, 'cp2k_input.inp.header')) at.params.update(cp2k_params) run_type = cp2k_run_type(cp2k_output=cp2k_output, cp2k_input_header=cp2k_params) try: cluster_mark = getattr(at, 'cluster_mark' + run_suffix) qm_list_a = ((cluster_mark != HYBRID_NO_MARK).nonzero()[0]).astype( np.int32) except AttributeError: qm_list_a = fzeros(0, dtype=np.int32) if calc_qm_charges is None: calc_qm_charges = '' try: cur_qmmm_qm_abc = [ float(cp2k_params['QMMM_ABC_X']), float(cp2k_params['QMMM_ABC_Y']), float(cp2k_params['QMMM_ABC_Z']) ] except KeyError: if 'QM_cell' + run_suffix in at.params: cur_qmmm_qm_abc = at.params['QM_cell' + run_suffix] else: cur_qmmm_qm_abc = qmmm_qm_abc(at, qm_list_a, qm_vacuum) quip_cp2k_at = Atoms(os.path.join(run_dir, 'quip_cp2k.xyz')) rev_sort_index_file = os.path.join(run_dir, '../quip_rev_sort_index') fields = [int(x) for x in open(rev_sort_index_file).read().split()] rev_sort_index = farray(fields, dtype=np.int32) #verbosity_push(PRINT_SILENT) cp2k_energy, cp2k_force = read_output(quip_cp2k_at, qm_list_a, cur_qmmm_qm_abc, run_dir, proj, calc_qm_charges, calc_virial, True, 3, at.n, out_i) #verbosity_pop() qm_list = None if os.path.exists(os.path.join(run_dir, 'cp2k_input.qmmm_qm_kind')): qm_kind_grep_cmd = "grep MM_INDEX %s/cp2k_input.qmmm_qm_kind | awk '{print $2}'" % run_dir qm_list = [int(i) for i in os.popen(qm_kind_grep_cmd).read().split()] if qm_list is not None: if run_type == 'QMMM': reordering_index = getattr(at, 'reordering_index', None) at.add_property('qm', False, overwrite=True) if reordering_index is not None: qm_list = reordering_index[qm_list] at.qm[qm_list] = True elif run_type == 'QS': at.add_property('qm_orig_index', 0, overwrite=True) for i, qm_at in fenumerate(qm_list): at.qm_orig_index[i] = sort_index[qm_at] at.add_property('force', cp2k_force, overwrite=True) at.params['energy'] = cp2k_energy yield at
def CP2KOutputReader(fh, module=None, type_map=None, kind_map=None, format=None): # mapping from run type to (default module index, list of available module) run_types = { 'QS': ['QUICKSTEP'], 'QMMM': ['FIST', 'QM/MM', 'QUICKSTEP'], 'MM': ['FIST'] } filename, lines = read_text_file(fh) run_type = cp2k_run_type(cp2k_output=lines) if type_map is None: type_map = {} if kind_map is None: kind_map = {} try: available_modules = run_types[run_type] except KeyError: raise ValueError('Unknown CP2K run type %s' % run_type) if module is None: module = available_modules[0] try: cell_index = available_modules.index(module) except ValueError: raise ValueError("Don't know how to read module %s from file %s" % (module, filename)) cell_lines = [ i for i, line in enumerate(lines) if line.startswith(" CELL| Vector a") ] if cell_lines == []: raise ValueError("Cannot find cell in file %s" % filename) try: cell_line = cell_lines[cell_index] except IndexError: raise ValueError( "Cannot find cell with index %d in file %s for module %s" % (cell_index, filename, module)) lattice = fzeros((3, 3)) for i in [0, 1, 2]: lattice[:, i + 1] = [float(c) for c in lines[cell_line + i].split()[4:7]] try: start_line = lines.index( " MODULE %s: ATOMIC COORDINATES IN angstrom\n" % module) except ValueError: raise ValueError( "Cannot find atomic positions for module %s in file %s" % (module, filename)) kinds = [] species = [] Zs = [] pos = [] masses = [] Zeffs = [] types = [] qeffs = [] for line in lines[start_line + 4:]: if line.strip() == '': break if module == 'FIST': atom, kind, typ, x, y, z, qeff, mass = line.split() types.append(typ) Z = type_map.get(typ, 0) kind = int(kind) if Z == 0: Z = kind_map.get(kind, 0) Zs.append(Z) qeffs.append(float(qeff)) else: atom, kind, sp, Z, x, y, z, Zeff, mass = line.split() species.append(sp) Zs.append(int(Z)) Zeffs.append(float(Zeff)) kinds.append(int(kind)) pos.append([float(x), float(y), float(z)]) masses.append(float(mass)) at = Atoms(n=len(kinds), lattice=lattice) at.pos[...] = farray(pos).T at.set_atoms(Zs) at.add_property('mass', farray(masses) * MASSCONVERT) at.add_property('kind', kinds) if module == 'FIST': at.add_property('type', ' ' * TABLE_STRING_LENGTH) at.add_property('qm', False) at.qm[:] = (at.type.stripstrings() == '_QM_') | (at.type.stripstrings() == '_LNK') at.type[...] = s2a(types, TABLE_STRING_LENGTH) at.add_property('qeff', qeffs) else: at.species[...] = s2a(species, TABLE_STRING_LENGTH) at.add_property('zeff', Zeffs) yield at