Esempio n. 1
0
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
Esempio n. 2
0
def VASP_POSCAR_Reader(outcar, species=None, format=None):
    """Read a configuration from a VASP OUTCAR file."""

    if (outcar == 'stdin' or outcar == '-'):
        p = sys.stdin
    else:
        p = open(outcar, 'r')

    re_comment = re.compile("\s*POSCAR:\s*(.+)")
    re_potcar = re.compile("\s*POTCAR:\s*\S+\s+(\S+)")
    re_n_atoms = re.compile("\s*ions per type =\s*((?:\d+\s*)*)")

    energy_i = -1
    at_i = -1
    lat_i = -1
    elements = []
    n_at = -1
    at_cur = None
    for lr in p:
        l = lr.rstrip()
        if (n_at <= 0):
            # parse header type things
            m = re_comment.match(l)
            if (m is not None):
                VASP_Comment = m.group(1)
                # print "got VASP_Comment '%s'" % VASP_Comment
            m = re_potcar.match(l)
            if (m is not None):
                elements.append(m.group(1))
            m = re_n_atoms.match(l)
            if (m is not None):
                # print "got ions per type, groups are:"
                # print m.groups()
                lat = fzeros((3, 3))
                n_types = [int(f) for f in m.group(1).split()]
                n_at = sum(n_types)
                at = Atoms(n=n_at, latttice=lat)
                i_at = 0
                for type_i in range(len(n_types)):
                    for j in range(n_types[type_i]):
                        i_at += 1
                        # print "set species of atom %d to '%s'" % (i_at, elements[type_i])
                        at.species[i_at] = elements[type_i]
                at.set_zs()
        else:
            # parse per-config lattice/pos/force
            if (l.find("direct lattice vectors") >=
                    0):  # get ready to read lattice vectors
                at_cur = at.copy()
                lat_cur = fzeros((3, 3))
                lat_i = 1
            elif (lat_i >= 1 and lat_i <= 3):  # read lattice vectors
                lat_cur[:, lat_i] = [
                    float(r) for r in l.replace("-", " -").split()[0:3]
                ]
                lat_i += 1
            elif (l.find("TOTAL-FORCE (eV/Angst)") >=
                  0):  # get ready to read atomic positions and forces
                if (not hasattr(at_cur, "force")):
                    at_cur.add_property("force", 0.0, n_cols=3)
                at_i = 1
                p.next()
            elif (at_i >= 1
                  and at_i <= at_cur.n):  # read atomic positions and forces
                pos_force = [
                    float(r) for r in l.replace("-", " -").split()[0:6]
                ]
                at_cur.pos[:, at_i] = pos_force[0:3]
                at_cur.force[:, at_i] = pos_force[3:6]
                at_i += 1
            elif (l.find("free  energy") >= 0):  # get ready to read energy
                at_cur.params['Energy'] = float(l.split()[4])
                energy_i = 1
                p.next()
            elif (energy_i == 1):  # read energy
                # print "energy(sigma->0) line"
                # print l.split()
                at_cur.params['Energy_sigma_to_zero'] = float(l.split()[6])
                energy_i += 1
                yield at_cur
            if (at_cur is not None and at_i
                    == at_cur.n):  # at end of configuration, set lattice
                at_cur.set_lattice(lat_cur, False)

    __all__ = ['VASP_POSCAR_Reader', 'VASP_OUTCAR_Reader', 'VASPWriter']