def test_npt(self, patch_print):
        test_name = 'npt'
        benchmark_dir = os.path.join(self.benchmark_dir, test_name)
        test_dir = os.path.join(self.test_dir, test_name)
        os.mkdir(test_dir)
        link_file_list = ['log.lammps', 'equi_settings.json', 'conf.lmp']
        for file in link_file_list:
            src = os.path.join(benchmark_dir, file)
            relative_link_file(file_path=src, target_dir=test_dir)

        post_task(test_dir)
        patch_print.assert_called_once()
        check_file_list = [
            'result',
        ]
        for file in check_file_list:
            f1 = os.path.join(benchmark_dir, file)
            f2 = os.path.join(test_dir, file)
            self.assertEqual(get_file_md5(f1), get_file_md5(f2))
Esempio n. 2
0
 def test_raise_err(self):
     with self.assertRaises(RuntimeError):
         relative_link_file('../dpti/', 
         'relative_link_file_test_dir/')
Esempio n. 3
0
def _make_tasks_onephase(temp, 
                         pres, 
                         task_path, 
                         jdata,
                         ens = 'npt',
                         conf_file = 'conf.lmp', 
                         graph_file = 'graph.pb',
                         if_meam=False,
                         meam_model=None):
    # assume that model and conf.lmp exist in the current dir
    assert os.path.isfile(conf_file), (conf_file, os.getcwd())
    # assert(os.path.isfile(graph_file))
    conf_file = os.path.abspath(conf_file)
    if graph_file:
        graph_abs_file = os.path.abspath(graph_file)
    
    mass_map = jdata['mass_map']
    # MD simulation protocol
    nsteps = jdata['nsteps']
    timestep = jdata['timestep']
    thermo_freq = jdata['thermo_freq']
    tau_t = jdata['tau_t']
    tau_p = jdata['tau_p']

    cwd = os.getcwd()
    if not os.path.isdir(task_path):
        create_path(task_path)

    os.chdir(task_path)
    if not os.path.exists('conf.lmp'):
        os.symlink(os.path.relpath(conf_file), 'conf.lmp')
    if graph_file:
        if not os.path.exists('graph.pb'):
            os.symlink(os.path.relpath(graph_abs_file), 'graph.pb')

    if if_meam:
        relative_link_file(meam_model['library_abs_path'], './')
        relative_link_file(meam_model['potential_abs_path'], './')
        # meam_library_basename = os.path.basename(meam_model['library'])
        # meam_potential_basename = os.path.basename(meam_model['potential'])
        # os.symlink(os.path.join('../../../', meam_library_basename), meam_library_basename)
        # os.symlink(os.path.join('../../../', meam_potential_basename), meam_potential_basename)
    
    # input for NPT MD
    lmp_str \
        = _gen_lammps_input('conf.lmp',
                               mass_map, 
                               graph_file,
                               nsteps, 
                               timestep,
                               ens,
                               temp,
                               pres,
                               tau_t = tau_t,
                               tau_p = tau_p,
                               thermo_freq = thermo_freq,
                               if_meam=if_meam,
                               meam_model=meam_model)
    with open('thermo.out', 'w') as fp :
        fp.write('%.16e %.16e' % (temp, pres))
    with open('in.lammps', 'w') as fp :
        fp.write(lmp_str)

    os.chdir(cwd)
Esempio n. 4
0
 def test_abs_path(self):
     abs_path = os.path.abspath(__file__)
     relative_link_file(abs_path, 'relative_link_file_test_dir/')
Esempio n. 5
0
 def test_abs_path_2(self):
     abs_path = os.path.abspath('../README.md')
     relative_link_file(abs_path, 'relative_link_file_test_dir/')
Esempio n. 6
0
 def test_other_place(self):
     relative_link_file('../setup.py', 'relative_link_file_test_dir/')
Esempio n. 7
0
 def test_normal(self):
     relative_link_file('graph.pb', 'relative_link_file_test_dir/')
Esempio n. 8
0
def make_task(iter_name,
              jdata,
              ens=None,
              temp=None,
              pres=None,
              if_dump_avg_posi=None,
              npt_dir=None):
    # jfile_path = os.path.abspath(jfile)
    # with open(jfile, 'r') as f:
    #     jdata = json.load(f)

    equi_args = [
        Argument("equi_conf", str),
        Argument("mass_map", list, alias=['model_mass_map']),
        Argument("model", str),
        Argument("nsteps", int),
        Argument("timestep", float, alias=['dt']),
        Argument("ens", str),
        Argument("temp", int),
        Argument("pres", int),
        Argument("tau_t", float),
        Argument("tau_p", float),
        Argument("thermo_freq", int, alias=['stat_freq']),
        Argument("dump_freq", int),
        Argument("stat_skip", int),
        Argument("stat_bsize", int),
        Argument("if_dump_avg_posi", bool, optional=True, default=False),
        Argument("is_water",
                 bool,
                 optional=True,
                 default=False,
                 alias=['if_water']),
        Argument("if_meam", bool, optional=True, default=False),
        Argument("meam_model", list, optional=True, default=False),
    ]

    equi_format = Argument("equi", dict, equi_args)

    equi_kwargs_settings = create_dict_not_empty_key(
        ens=ens,
        temp=temp,
        pres=pres,
        if_dump_avg_posi=if_dump_avg_posi,
        npt_dir=npt_dir)

    equi_pre_settings = jdata.copy()
    equi_pre_settings.update(equi_kwargs_settings)

    equi_settings = equi_format.normalize_value(equi_pre_settings)

    task_abs_dir = create_path(iter_name)

    if npt_dir is not None:
        npt_avg_conf_lmp = npt_equi_conf(npt_dir)
        with open(os.path.join(task_abs_dir, 'npt_avg.lmp'), 'w') as f:
            f.write(npt_avg_conf_lmp)
        equi_settings['equi_conf'] = 'npt_avg.lmp'
    else:
        equi_conf = equi_settings['equi_conf']
        relative_link_file(equi_conf, task_abs_dir)
        equi_settings['equi_conf'] = os.path.basename(equi_conf)

    model = equi_settings['model']
    if model:
        relative_link_file(model, task_abs_dir)
        equi_settings['model'] = os.path.basename(model)

    if_meam = equi_settings.get('if_meam', None)
    meam_model = equi_settings.get('meam_model', None)
    if if_meam:
        relative_link_file(meam_model['library'], task_abs_dir)
        relative_link_file(meam_model['potential'], task_abs_dir)

    with open(os.path.join(task_abs_dir, 'jdata.json'), 'w') as f:
        json.dump(jdata, f, indent=4)
    with open(os.path.join(task_abs_dir, 'equi_kwargs_setting.json'),
              'w') as f:
        json.dump(equi_kwargs_settings, f, indent=4)
    with open(os.path.join(task_abs_dir, 'equi_settings.json'), 'w') as f:
        json.dump(equi_settings, f, indent=4)

    lmp_str = gen_equi_lammps_input(
        nsteps=equi_settings['nsteps'],
        thermo_freq=equi_settings['thermo_freq'],
        dump_freq=equi_settings['dump_freq'],
        mass_map=equi_settings['mass_map'],
        temp=equi_settings['temp'],
        tau_t=equi_settings['tau_t'],
        tau_p=equi_settings['tau_p'],
        equi_conf=equi_settings['equi_conf'],
        model=equi_settings['model'],
        timestep=equi_settings['timestep'],
        if_dump_avg_posi=equi_settings['if_dump_avg_posi'],
        ens=equi_settings['ens'],
        pres=equi_settings['pres'],
        if_meam=equi_settings['if_meam'],
        meam_model=equi_settings['meam_model'])

    with open(os.path.join(task_abs_dir, 'in.lammps'), 'w') as fp:
        fp.write(lmp_str)
    return equi_settings
Esempio n. 9
0
def _make_tasks(iter_name, jdata, step, if_meam=False, meam_model=None):
    if step == 'soft_on':
        all_lambda = parse_seq(jdata['lambda_soft_on'])
    elif step == 'deep_on':
        all_lambda = parse_seq(jdata['lambda_deep_on'])
    elif step == 'soft_off':
        all_lambda = parse_seq(jdata['lambda_soft_off'])
    else:
        raise RuntimeError('unknow step')
    equi_conf = jdata['equi_conf']
    equi_conf = os.path.abspath(equi_conf)
    # mass_map = jdata['mass_map']
    mass_map = get_first_matched_key_from_dict(jdata,
                                               ['mass_map', 'model_mass_map'])
    model = jdata.get('model', None)
    if model:
        model = os.path.abspath(model)
    soft_param = jdata['soft_param']
    nsteps = jdata['nsteps']
    # timestep = jdata['timestep']
    timestep = get_first_matched_key_from_dict(jdata, ['timestep', 'dt'])
    # thermo_freq = jdata['thermo_freq']
    thermo_freq = get_first_matched_key_from_dict(jdata,
                                                  ['thermo_freq', 'stat_freq'])
    copies = None
    if 'copies' in jdata:
        copies = jdata['copies']
    temp = jdata['temp']

    sparam = jdata.get('soft_param', {})
    if sparam:
        if 'sigma_oo' in sparam:
            sparam['sigma_0_0'] = sparam['sigma_oo']
            sparam['sigma_0_1'] = sparam['sigma_oh']
            sparam['sigma_1_1'] = sparam['sigma_hh']

        # element_num=sparam.get('element_num', 1)
        element_num = len(mass_map)
        sigma_key_index = filter(lambda t: t[0] <= t[1],
                                 ((i, j) for i in range(element_num)
                                  for j in range(element_num)))
        sigma_key_name_list = [
            'sigma_' + str(t[0]) + '_' + str(t[1]) for t in sigma_key_index
        ]
        for sigma_key_name in sigma_key_name_list:
            assert sparam.get(
                sigma_key_name, None
            ), 'there must be key-value for {sigma_key_name} in soft_param'.format(
                sigma_key_name=sigma_key_name)

    job_abs_dir = create_path(iter_name)

    if meam_model:
        relative_link_file(os.path.abspath(meam_model['library']), job_abs_dir)
        relative_link_file(os.path.abspath(meam_model['potential']),
                           job_abs_dir)
        # os.symlink(os.path.join('..', 'conf.lmp'), 'conf.lmp')
        # os.symlink(os.path.join('..', 'graph.pb'), 'graph.pb')

    cwd = os.getcwd()
    os.chdir(iter_name)
    os.symlink(os.path.join('..', 'in.json'), 'in.json')
    os.symlink(os.path.join('..', 'conf.lmp'), 'conf.lmp')
    os.symlink(os.path.join('..', 'graph.pb'), 'graph.pb')

    os.chdir(cwd)
    # print(9898, meam_model)
    for idx, ii in enumerate(all_lambda):
        work_path = os.path.join(iter_name, 'task.%06d' % idx)
        create_path(work_path)
        os.chdir(work_path)
        os.symlink(os.path.join('..', 'conf.lmp'), 'conf.lmp')
        os.symlink(os.path.join('..', 'graph.pb'), 'graph.pb')
        if meam_model:
            meam_library_basename = os.path.basename(meam_model['library'])
            meam_potential_basename = os.path.basename(meam_model['potential'])
            os.symlink(os.path.join('..', meam_library_basename),
                       meam_library_basename)
            os.symlink(os.path.join('..', meam_potential_basename),
                       meam_potential_basename)
        lmp_str \
            = _gen_lammps_input_ideal(step,
                                      'conf.lmp',
                                      mass_map,
                                      ii,
                                      soft_param,
                                      'graph.pb',
                                      nsteps,
                                      timestep,
                                      'nvt',
                                      temp,
                                      thermo_freq = thermo_freq,
                                      copies = copies,
                                      if_meam = if_meam,
                                      meam_model=meam_model)
        with open('in.lammps', 'w') as fp:
            fp.write(lmp_str)
        with open('lambda.out', 'w') as fp:
            fp.write(str(ii))
        os.chdir(cwd)
Esempio n. 10
0
def make_tasks(iter_name, jdata, if_meam=None):
    ti_settings = jdata.copy()
    if if_meam is None:
        if_meam = jdata.get('if_meam', None)
    equi_conf = jdata['equi_conf']
    equi_conf = os.path.abspath(equi_conf)
    copies = None
    if 'copies' in jdata :
        copies = jdata['copies']
    model = jdata['model']
    meam_model = jdata.get('meam_model', None)
    # model = os.path.abspath(model)
    # mass_map = jdata['mass_map']
    mass_map = get_first_matched_key_from_dict(jdata, ['model_mass_map', 'mass_map'])
    nsteps = jdata['nsteps']
    # timestep = jdata['timestep']
    timestep = get_first_matched_key_from_dict(jdata, ['timestep', 'dt'])
    # thermo_freq = jdata['thermo_freq']
    thermo_freq = get_first_matched_key_from_dict(jdata, ['thermo_freq', 'stat_freq'])
    # thermos = jdata['thermos']
    ens = jdata['ens']
    path = jdata['path']
    if 'nvt' in ens :
        if path == 't' :
            temp_seq = get_first_matched_key_from_dict(jdata, ['temp_seq', 'temps'])
            temp_list = parse_seq(temp_seq)
            tau_t = jdata['tau_t']
            ntasks = len(temp_list)
        else :
            raise RuntimeError('supported path of nvt ens is \'t\'')
    elif 'npt' in ens :
        if path == 't' :
            temp_seq = get_first_matched_key_from_dict(jdata, ['temp_seq', 'temps'])
            temp_list = parse_seq(temp_seq)
            pres = get_first_matched_key_from_dict(jdata, ['pres', 'press'])
            ntasks = len(temp_list)
        elif path == 't-ginv' :
            temp_seq = get_first_matched_key_from_dict(jdata, ['temp_seq', 'temps'])
            temp_list = parse_seq_ginv(temp_seq)
            pres = get_first_matched_key_from_dict(jdata, ['pres', 'press'])
            ntasks = len(temp_list)
        elif path == 'p' :
            temp = get_first_matched_key_from_dict(jdata, ['temp', 'temps'])
            pres_seq = get_first_matched_key_from_dict(jdata, ['pres_seq', 'press'])
            pres_list = parse_seq(pres_seq)
            ntasks = len(pres_list)
        else :
            raise RuntimeError('supported path of npt ens are \'t\' or \'p\'')
        tau_t = jdata['tau_t']
        tau_p = jdata['tau_p']
    else :
        raise RuntimeError('invalid ens')

    job_abs_dir = create_path(iter_name)




    # dct1 = link_file_in_dict(
    #     dct=jdata,
    #     key_list=["equi_conf", "model"],
    #     target_dir=job_abs_dir
    # )
    # ti_settings.update(dct1)

    # meam_model = jdata.get('meam_model', None)
    # dct2 = link_file_in_dict(
    #     dct=meam_model,
    #     key_list=["library", "potential"],
    #     target_dir=job_abs_dir
    # )
    # if meam_model:
    #     ti_settings['meam_model'].update(dct2)

    # link_file_dict = {}
    # link_file_dict.update(dct1)
    # link_file_dict.update(dct2)


    # copied_conf = os.path.join(os.path.abspath(iter_name), 'conf.lmp')
    # shutil.copyfile(equi_conf, copied_conf)
    # jdata['equi_conf'] = 'conf.lmp'
    # linked_model = os.path.join(os.path.abspath(iter_name), 'graph.pb')
    # shutil.copyfile(model, linked_model)
    # jdata['model'] = 'graph.pb'

    # cwd = os.getcwd()
    # os.chdir(iter_name)
    relative_link_file(equi_conf, job_abs_dir)
    if model:
        relative_link_file(model, job_abs_dir)
    if if_meam:
        relative_link_file(meam_model['library'], job_abs_dir)
        relative_link_file(meam_model['potential'], job_abs_dir)

    with open(os.path.join(job_abs_dir, 
        'ti_settings.json'), 'w') as fp:
        json.dump(ti_settings, fp, indent=4)

    with open(os.path.join(job_abs_dir, 'ti_settings.json'),'w') as f:
            json.dump(ti_settings, f, indent=4)

    for ii in range(ntasks) :
        task_dir = os.path.join(job_abs_dir, 'task.%06d' % ii)
        task_abs_dir = create_path(task_dir)
        # os.chdir(work_path)

        relative_link_file(equi_conf, task_abs_dir)
        if model:
            relative_link_file(model, task_abs_dir)
        if if_meam:
            relative_link_file(meam_model['library'], task_abs_dir)
            relative_link_file(meam_model['potential'], task_abs_dir)

        # for file in list(link_file_dict.values()):
        #     file_path = os.path.join(job_abs_dir, file)
        #     relative_link_file(file_path, task_abs_dir)
            # os.symlink()
        # os.symlink(os.path.relpath(copied_conf), 'conf.lmp')
        # os.symlink(os.path.relpath(linked_model), 'graph.pb')
        if 'nvt' in ens and path == 't' :
            lmp_str \
                = _gen_lammps_input(os.path.basename(equi_conf),
                                    mass_map, 
                                    model,
                                    nsteps, 
                                    timestep,
                                    ens,
                                    temp_list[ii],
                                    pres=pres,
                                    tau_t = tau_t,
                                    thermo_freq = thermo_freq, 
                                    copies = copies,
                                    if_meam=if_meam,
                                    meam_model=meam_model
                                    )
            thermo_out = temp_list[ii]
            # with open('thermo.out', 'w') as fp :
            #     fp.write('%f' % temps[ii])
        elif 'npt' in ens and (path == 't' or path == 't-ginv'):
            lmp_str \
                = _gen_lammps_input(os.path.basename(equi_conf),
                                    mass_map, 
                                    model,
                                    nsteps, 
                                    timestep,
                                    ens,
                                    temp_list[ii],
                                    pres,
                                    tau_t = tau_t,
                                    tau_p = tau_p,
                                    thermo_freq = thermo_freq, 
                                    copies = copies,
                                    if_meam=if_meam,
                                    meam_model=meam_model
                                    )
            thermo_out = temp_list[ii]
            # with open('thermo.out', 'w') as fp :
            #     fp.write('%f' % (temps[ii]))
        elif 'npt' in ens and path == 'p' :
            lmp_str \
                = _gen_lammps_input(os.path.basename(equi_conf),
                                    mass_map, 
                                    model,
                                    nsteps, 
                                    timestep,
                                    ens,
                                    temp,
                                    pres_list[ii],
                                    tau_t = tau_t,
                                    tau_p = tau_p,
                                    thermo_freq = thermo_freq, 
                                    copies = copies,
                                    if_meam=if_meam,
                                    meam_model=meam_model
                                )
            thermo_out = pres_list[ii]
        else:
            raise RuntimeError('invalid ens or path setting' )
        
        with open(os.path.join(task_abs_dir, 'thermo.out'), 'w') as fp:
            fp.write('%f' % (thermo_out))
        with open(os.path.join(task_abs_dir, 'in.lammps'), 'w') as fp:
            fp.write(lmp_str)