Exemplo n.º 1
0
def cmpt_vasp(jdata, conf_dir, static=False):

    if 'relax_incar' in jdata.keys():
        vasp_str = 'vasp-relax_incar'
    else:
        kspacing = jdata['vasp_params']['kspacing']
        vasp_str = 'vasp-k%.2f' % (kspacing)

    equi_path = re.sub('confs', global_equi_name, conf_dir)
    equi_path = os.path.join(equi_path, vasp_str)
    equi_path = os.path.abspath(equi_path)
    equi_outcar = os.path.join(equi_path, 'OUTCAR')
    task_path = re.sub('confs', global_task_name, conf_dir)
    if static:
        if 'scf_incar' in jdata.keys():
            vasp_static_str = 'vasp-static-scf_incar'
        else:
            kspacing = jdata['vasp_params']['kspacing']
            vasp_static_str = 'vasp-static-k%.2f' % (kspacing)
        task_path = os.path.join(task_path, vasp_static_str)
    else:
        task_path = os.path.join(task_path, vasp_str)
    task_path = os.path.abspath(task_path)

    equi_natoms, equi_epa, equi_vpa = vasp.get_nev(equi_outcar)

    struct_path_widecard = os.path.join(task_path, 'struct-*-m*m')
    struct_path_list = glob.glob(struct_path_widecard)
    struct_path_list.sort()
    if len(struct_path_list) == 0:
        print("# cannot find results for conf %s" % (conf_dir))
    sys.stdout.write("Miller_Indices: \tSurf_E(J/m^2) EpA(eV) equi_EpA(eV)\n")

    result = os.path.join(task_path, 'result')
    with open(result, 'w') as fp:
        fp.write('conf_dir:%s\n' % (conf_dir))
        fp.write("Miller_Indices: \tSurf_E(J/m^2) EpA(eV) equi_EpA(eV)\n")
        for ii in struct_path_list:
            structure_dir = os.path.basename(ii)
            outcar = os.path.join(ii, 'OUTCAR')
            natoms, epa, vpa = vasp.get_nev(outcar)
            if static:
                e0 = np.array(vasp.get_energies(outcar)) / natoms
                epa = e0[0]
            boxes = vasp.get_boxes(outcar)
            AA = np.linalg.norm(np.cross(boxes[0][0], boxes[0][1]))
            Cf = 1.60217657e-16 / (1e-20 * 2) * 0.001
            evac = (epa * natoms - equi_epa * natoms) / AA * Cf
            sys.stdout.write("%s:\t %7.3f   %8.3f %8.3f\n" %
                             (structure_dir, evac, epa, equi_epa))
            fp.write("%s:\t %7.3f   %8.3f %8.3f\n" %
                     (structure_dir, evac, epa, equi_epa))
    fp.close()
    if 'upload_username' in jdata.keys():
        upload_username = jdata['upload_username']
        util.insert_data('surf', 'vasp', upload_username, result)
Exemplo n.º 2
0
    def _compute_lower(self, output_file, all_tasks, all_res):
        output_file = os.path.abspath(output_file)
        res_data = {}
        ptr_data = os.path.dirname(output_file) + '\n'

        if not self.reprod:
            ptr_data += "Miller_Indices: \tSurf_E(J/m^2) EpA(eV) equi_EpA(eV)\n"
            for ii in all_tasks:
                with open(os.path.join(ii, 'inter.json')) as fp:
                    idata = json.load(fp)
                inter_type = idata['type']
                equi_path = os.path.abspath(
                    os.path.join(os.path.dirname(output_file),
                                 '../relaxation'))
                structure_dir = os.path.basename(ii)
                if inter_type == 'vasp':
                    equi_outcar = os.path.join(equi_path, 'OUTCAR')
                    equi_natoms, equi_epa, equi_vpa = vasp.get_nev(equi_outcar)
                    outcar = os.path.join(ii, 'OUTCAR')
                    natoms, epa, vpa = vasp.get_nev(outcar)
                    if self.static:
                        e0 = np.array(vasp.get_energies(outcar)) / natoms
                        epa = e0[0]
                    boxes = vasp.get_boxes(outcar)
                    AA = np.linalg.norm(np.cross(boxes[0][0], boxes[0][1]))

                elif inter_type in ['deepmd', 'meam', 'eam_fs', 'eam_alloy']:
                    equi_log = os.path.join(equi_path, 'log.lammps')
                    equi_natoms, equi_epa, equi_vpa = lammps.get_nev(equi_log)
                    lmp_log = os.path.join(ii, 'log.lammps')
                    natoms, epa, vpa = lammps.get_nev(lmp_log)
                    AA = lammps.get_base_area(lmp_log)

                else:
                    raise RuntimeError('interaction type not supported')

                Cf = 1.60217657e-16 / (1e-20 * 2) * 0.001
                evac = (epa * natoms - equi_epa * natoms) / AA * Cf
                ptr_data += "%s: \t%7.3f    %8.3f %8.3f\n" % (
                    structure_dir, evac, epa, equi_epa)
                res_data[structure_dir] = [evac, epa, equi_epa]

        else:
            if 'vasp_path' not in self.parameter:
                raise RuntimeError(
                    "please provide the vasp_path for reproduction")
            vasp_path = os.path.abspath(self.parameter['vasp_path'])
            res_data, ptr_data = reproduce.post_repro(vasp_path, all_tasks,
                                                      ptr_data)

        with open(output_file, 'w') as fp:
            json.dump(res_data, fp, indent=4)

        return res_data, ptr_data
Exemplo n.º 3
0
def make_repro(vasp_path, path_to_work):
    if not os.path.exists(vasp_path):
        raise RuntimeError("please do VASP calcualtions first")
    vasp_task = glob.glob(os.path.join(vasp_path, 'task.[0-9]*[0-9]'))
    assert len(vasp_task) > 0, "Please do VASP calcualtions first"
    vasp_task.sort()
    task_num = 0
    task_list = []
    for ii in vasp_task:
        # get vasp energy
        outcar = os.path.join(ii, 'OUTCAR')
        energies = vasp.get_energies(outcar)
        # get xdat
        xdatcar = os.path.join(ii, 'XDATCAR')
        os.chdir(path_to_work)
        if os.path.exists('XDATCAR'):
            os.remove('XDATCAR')
        os.symlink(os.path.relpath(xdatcar), 'XDATCAR')
        xdat_lines = open('XDATCAR', 'r').read().split('\n')
        natoms = vasp.poscar_natoms('XDATCAR')
        xdat_secsize = natoms + 8
        xdat_nframes = len(xdat_lines) // xdat_secsize
        if xdat_nframes > len(energies):
            warnings.warn(
                'nframes %d in xdatcar is larger than energy %d, use the last %d frames'
                % (xdat_nframes, len(energies), len(energies)))
            xdat_nlines = -1 * len(energies) * xdat_secsize  # 06/12 revised
            xdat_lines = xdat_lines[xdat_nlines:]
        xdat_nframes = len(xdat_lines) // xdat_secsize
        print(xdat_nframes, len(energies))

        # loop over frames
        for jj in range(xdat_nframes):
            output_task = os.path.join(path_to_work, 'task.%06d' % task_num)
            task_num += 1
            task_list.append(output_task)
            os.makedirs(output_task, exist_ok=True)
            os.chdir(output_task)
            # clear dir
            for kk in [
                    'INCAR', 'POTCAR', 'POSCAR.orig', 'POSCAR', 'conf.lmp',
                    'in.lammps'
            ]:
                if os.path.exists(kk):
                    os.remove(kk)
            # make conf
            with open('POSCAR', 'w') as fp:
                fp.write('\n'.join(xdat_lines[jj * xdat_secsize:(jj + 1) *
                                              xdat_secsize]))

    return task_list
Exemplo n.º 4
0
def _cmpt_deepmd_reprod_traj(jdata, conf_dir, supercell, insert_ele,
                             task_name):
    fp_params = jdata['vasp_params']
    kspacing = fp_params['kspacing']

    if 'relax_incar' in jdata.keys():
        vasp_str = 'vasp-relax_incar'
    else:
        vasp_str = 'vasp-k%.2f' % kspacing

    conf_path = os.path.abspath(conf_dir)
    task_path = re.sub('confs', global_task_name, conf_path)
    vasp_path = os.path.join(task_path, vasp_str)
    lmps_path = os.path.join(task_path,
                             task_name + vasp_str.replace('vasp', ''))
    copy_str = "%sx%sx%s" % (supercell[0], supercell[1], supercell[2])
    struct_widecard = os.path.join(vasp_path,
                                   'struct-%s-%s-*' % (insert_ele, copy_str))
    vasp_struct = glob.glob(struct_widecard)
    vasp_struct.sort()
    cwd = os.getcwd()

    for vs in vasp_struct:
        # compute vasp
        outcar = os.path.join(vs, 'OUTCAR')
        vasp_ener = np.array(vasp.get_energies(outcar))
        vasp_ener_file = os.path.join(vs, 'ener.vasp.out')
        # compute reprod
        struct_basename = os.path.basename(vs)
        ls = os.path.join(lmps_path, struct_basename)
        frame_widecard = os.path.join(ls, 'frame.*')
        frames = glob.glob(frame_widecard)
        frames.sort()
        lmp_ener = []
        for ii in frames:
            log_lmp = os.path.join(ii, 'log.lammps')
            natoms, epa, vpa = lammps.get_nev(log_lmp)
            lmp_ener.append(epa)
        lmp_ener = np.array(lmp_ener)
        lmp_ener = np.reshape(lmp_ener, [-1, 1])
        lmp_ener_file = os.path.join(ls, 'ener.lmp.out')
        vasp_ener = np.reshape(vasp_ener, [-1, 1]) / natoms
        error_start = 1
        lmp_ener -= lmp_ener[-1] - vasp_ener[-1]
        diff = lmp_ener - vasp_ener
        diff = diff[error_start:]
        error = np.linalg.norm(diff) / np.sqrt(np.size(lmp_ener))
        np.savetxt(vasp_ener_file, vasp_ener[error_start:])
        np.savetxt(lmp_ener_file, lmp_ener[error_start:])
        print(os.path.basename(ls), 'EpA_std_err=', error)
Exemplo n.º 5
0
def post_repro(vasp_path, all_tasks, ptr_data):
    ptr_data += "Reproduce: VASP_path DFT_E(eV/atom)  LMP_E(eV/atom)  Difference(eV/atom)\n"
    vasp_task = glob.glob(os.path.join(vasp_path, 'task.[0-9]*[0-9]'))
    assert len(vasp_task) > 0, "Please do VASP calcualtions first"
    vasp_task.sort()
    vasp_ener_tot = []
    lmp_ener_tot = []
    res_data = {}
    for ii in vasp_task:
        # compute vasp
        outcar = os.path.join(ii, 'OUTCAR')
        vasp_ener = np.array(vasp.get_energies(outcar))
        vasp_ener_file = os.path.join(ii, 'ener.vasp.out')
        # compute reprod
        lmp_ener = []

        if len(all_tasks) < (len(vasp_ener_tot) + len(vasp_ener)):
            raise RuntimeError("lammps tasks reproduced not equal to vasp")

        natoms = 1
        for jj in range(
                len(vasp_ener_tot), (len(vasp_ener_tot) + len(vasp_ener))
        ):  #all_tasks[len(vasp_ener_tot):(len(vasp_ener_tot) + len(vasp_ener))]:
            log_lmp = os.path.join(all_tasks[jj], 'log.lammps')
            if not os.path.exists(log_lmp):
                raise RuntimeError("lammps reproduce not finished")
            natoms, epa, vpa = lammps.get_nev(log_lmp)
            lmp_ener.append(epa)
            lmp_ener_tot.append(epa)
            vasp_epa = list(vasp_ener)[jj - len(vasp_ener_tot)] / natoms
            ptr_data += '%s %7.3f  %7.3f  %7.3f\n' % (vasp_task[ii], vasp_epa,
                                                      epa, epa - vasp_epa)
        lmp_ener = np.array(lmp_ener)
        lmp_ener = np.reshape(lmp_ener, [-1, 1])
        vasp_ener_tot += list(vasp_ener)
        vasp_ener = np.reshape(vasp_ener, [-1, 1]) / natoms
        error_start = 1
        lmp_ener -= lmp_ener[-1] - vasp_ener[-1]
        diff = lmp_ener - vasp_ener
        diff = diff[error_start:]
        error = np.linalg.norm(diff) / np.sqrt(np.size(lmp_ener))
        res_data[ii] = {'nframes': len(vasp_ener), 'error': error}
        np.savetxt(vasp_ener_file, vasp_ener[error_start:])
    if not len(vasp_ener_tot) == len(lmp_ener_tot):
        raise RuntimeError("lammps tasks reproduced not equal to vasp")
#    for ii in range(len(lmp_ener_tot)):
#        ptr_data += '%7.3f  %7.3f  %7.3f\n' % (vasp_ener_tot[ii], lmp_ener_tot[ii],
#                                               lmp_ener_tot[ii] - vasp_ener_tot[ii])
    return res_data, ptr_data
Exemplo n.º 6
0
def cmpt_vasp(jdata, conf_dir, static=False):
    fp_params = jdata['vasp_params']
    kspacing = fp_params['kspacing']

    equi_path = re.sub('confs', global_equi_name, conf_dir)
    equi_path = os.path.join(equi_path, 'vasp-k%.2f' % kspacing)
    equi_path = os.path.abspath(equi_path)
    equi_outcar = os.path.join(equi_path, 'OUTCAR')
    task_path = re.sub('confs', global_task_name, conf_dir)
    if static:
        task_path = os.path.join(task_path, 'vasp-static-k%.2f' % kspacing)
    else:
        task_path = os.path.join(task_path, 'vasp-k%.2f' % kspacing)
    task_path = os.path.abspath(task_path)

    equi_natoms, equi_epa, equi_vpa = vasp.get_nev(equi_outcar)

    struct_path_widecard = os.path.join(task_path, 'struct-*-m*m')
    struct_path_list = glob.glob(struct_path_widecard)
    struct_path_list.sort()
    if len(struct_path_list) == 0:
        print("# cannot find results for conf %s supercell %s" %
              (conf_dir, supercell))
    sys.stdout.write("Miller_Indices: \tSurf_E(J/m^2) EpA(eV) equi_EpA(eV)\n")
    for ii in struct_path_list:
        structure_dir = os.path.basename(ii)
        outcar = os.path.join(ii, 'OUTCAR')
        natoms, epa, vpa = vasp.get_nev(outcar)
        if static:
            e0 = np.array(vasp.get_energies(outcar)) / natoms
            epa = e0[0]
        boxes = vasp.get_boxes(outcar)
        AA = np.linalg.norm(np.cross(boxes[0][0], boxes[0][1]))
        Cf = 1.60217657e-16 / (1e-20 * 2) * 0.001
        evac = (epa * natoms - equi_epa * natoms) / AA * Cf
        sys.stdout.write("%s:\t %7.3f   %8.3f %8.3f\n" %
                         (structure_dir, evac, epa, equi_epa))
Exemplo n.º 7
0
def _make_reprod_traj(jdata, conf_dir, supercell, insert_ele, task_type):
    kspacing = jdata['vasp_params']['kspacing']
    fp_params = jdata['lammps_params']
    model_dir = fp_params['model_dir']
    type_map = fp_params['type_map']
    model_dir = os.path.abspath(model_dir)
    model_name = fp_params['model_name']
    if not model_name and task_type == 'deepmd':
        models = glob.glob(os.path.join(model_dir, '*pb'))
        model_name = [os.path.basename(ii) for ii in models]
        assert len(model_name) > 0, "No deepmd model in the model_dir"
    else:
        models = [os.path.join(model_dir, ii) for ii in model_name]

    model_param = {
        'model_name': fp_params['model_name'],
        'param_type': fp_params['model_param_type']
    }

    ntypes = len(type_map)

    conf_path = os.path.abspath(conf_dir)
    task_path = re.sub('confs', global_task_name, conf_path)
    vasp_path = os.path.join(task_path, 'vasp-k%.2f' % kspacing)
    lmps_path = os.path.join(task_path, task_type + '-reprod-k%.2f' % kspacing)
    os.makedirs(lmps_path, exist_ok=True)
    copy_str = "%sx%sx%s" % (supercell[0], supercell[1], supercell[2])
    struct_widecard = os.path.join(vasp_path,
                                   'struct-%s-%s-*' % (insert_ele, copy_str))
    vasp_struct = glob.glob(struct_widecard)
    vasp_struct.sort()
    cwd = os.getcwd()

    # make lammps.in
    if task_type == 'deepmd':
        fc = lammps.make_lammps_eval('conf.lmp', ntypes, lammps.inter_deepmd,
                                     model_name)
    elif task_type == 'meam':
        fc = lammps.make_lammps_eval('conf.lmp', ntypes, lammps.inter_meam,
                                     model_param)
    f_lammps_in = os.path.join(lmps_path, 'lammps.in')
    with open(f_lammps_in, 'w') as fp:
        fp.write(fc)

    for vs in vasp_struct:
        # get vasp energy
        outcar = os.path.join(vs, 'OUTCAR')
        energies = vasp.get_energies(outcar)
        # get xdat
        xdatcar = os.path.join(vs, 'XDATCAR')
        struct_basename = os.path.basename(vs)
        ls = os.path.join(lmps_path, struct_basename)
        print(ls)
        os.makedirs(ls, exist_ok=True)
        os.chdir(ls)
        if os.path.exists('XDATCAR'):
            os.remove('XDATCAR')
        os.symlink(os.path.relpath(xdatcar), 'XDATCAR')
        xdat_lines = open('XDATCAR', 'r').read().split('\n')
        natoms = vasp.poscar_natoms('XDATCAR')
        xdat_secsize = natoms + 8
        xdat_nframes = len(xdat_lines) // xdat_secsize
        if xdat_nframes > len(energies):
            warnings.warn(
                'nframes %d in xdat is larger than energy %d, use the last %d frames'
                % (xdat_nframes, len(energies), len(energies)))
            xdat_nlines = len(energies) * xdat_secsize
            xdat_lines = xdat_lines[xdat_nlines:]
        xdat_nframes = len(xdat_lines) // xdat_secsize
        print(xdat_nframes, len(energies))
        #link lammps.in and model
        for jj in ['lammps.in'] + model_name:
            if os.path.islink(jj):
                os.unlink(jj)
        os.symlink(os.path.relpath(f_lammps_in), 'lammps.in')
        if task_type == 'deepmd':
            for ii in model_name:
                if os.path.exists(ii):
                    os.remove(ii)
            for (ii, jj) in zip(models, model_name):
                os.symlink(os.path.relpath(ii), jj)
            share_models = glob.glob(os.path.join(ls, '*pb'))
        else:
            share_models = models

        # loop over frames
        for ii in range(xdat_nframes):
            frame_path = 'frame.%06d' % ii
            os.makedirs(frame_path, exist_ok=True)
            os.chdir(frame_path)
            # clear dir
            for jj in ['conf.lmp']:
                if os.path.isfile(jj):
                    os.remove(jj)
            for jj in ['lammps.in'] + model_name:
                if os.path.islink(jj):
                    os.unlink(jj)
            # link lammps in
            os.symlink(os.path.relpath('../lammps.in'), 'lammps.in')
            # make conf
            with open('POSCAR', 'w') as fp:
                fp.write('\n'.join(xdat_lines[ii * xdat_secsize:(ii + 1) *
                                              xdat_secsize]))
            lammps.cvt_lammps_conf('POSCAR', 'conf.lmp')
            ptypes = vasp.get_poscar_types('POSCAR')
            lammps.apply_type_map('conf.lmp', type_map, ptypes)
            # link models
            for (kk, ll) in zip(share_models, model_name):
                os.symlink(os.path.relpath(kk), ll)
            os.chdir(ls)
        os.chdir(cwd)