def mgp_model(gp_model): """ test the init function """ grid_params = {} if 'twobody' in gp_model.kernels: grid_params['twobody'] = {'grid_num': [64], 'lower_bound': [0.1]} if 'threebody' in gp_model.kernels: grid_params['threebody'] = { 'grid_num': [16] * 3, 'lower_bound': [0.1] * 3 } species_list = [1, 2, 3] lammps_location = f'tmp_lmp' mapped_model = MappedGaussianProcess(grid_params=grid_params, unique_species=species_list, n_cpus=1, lmp_file_name=lammps_location, var_map='simple') # import flare.mgp.mapxb # flare.mgp.mapxb.global_use_grid_kern = False mapped_model.build_map(gp_model) yield mapped_model del mapped_model
def test_mgp_gpfa(all_mgp, all_gp): """ Ensure that passing in an MGP also works for the trajectory trainer :param all_mgp: :param all_gp: :return: """ np.random.seed(10) gp_model = get_gp("3", "mc", False) gp_model.set_L_alpha() grid_num_3 = 3 lower_cut = 0.01 grid_params_3b = { "lower_bound": [lower_cut] * 3, "grid_num": [grid_num_3] * 3, "svd_rank": "auto", } grid_params = {"load_grid": None, "update": False} grid_params["threebody"] = grid_params_3b unique_species = gp_model.training_statistics["species"] mgp_model = MappedGaussianProcess(grid_params=grid_params, unique_species=unique_species, n_cpus=1) mgp_model.build_map(gp_model) nenv = 10 cell = np.eye(3) struc, f = get_random_structure(cell, unique_species, nenv) struc.forces = np.array(f) frames = [struc] tt = TrajectoryTrainer( frames, mgp_model, rel_std_tolerance=0, abs_std_tolerance=0, abs_force_tolerance=1e-8, print_training_plan=True, ) assert tt.gp_is_mapped is True tt.run() # Test that training plan is properly written with open("gp_from_aimd_training_plan.json", "r") as f: plan = json.loads(f.readline()) assert isinstance(plan["0"], list) assert len(plan["0"]) == len(struc) assert [p[0] for p in plan["0"]] == list(range(len(struc))) for f in glob(f"gp_from_aimd*"): remove(f)
def test_load_model(all_mgp, bodies, multihyps): """ test the mapping for mc_simple kernel """ name = f"my_mgp_{bodies}_{multihyps}.json" all_mgp[f"{bodies}{multihyps}"] = MappedGaussianProcess.from_file(name) os.remove(name) name = f"my_mgp_{bodies}_{multihyps}.pickle" all_mgp[f"{bodies}{multihyps}"] = MappedGaussianProcess.from_file(name) os.remove(name)
def flare_calc(): flare_calc_dict = {} for md_engine in md_list: # ---------- create gaussian process model ------------------- # set up GP hyperparameters kernels = ["twobody", "threebody"] # use 2+3 body kernel parameters = {"cutoff_twobody": 5.0, "cutoff_threebody": 3.5} pm = ParameterHelper(kernels=kernels, random=True, parameters=parameters) hm = pm.as_dict() hyps = hm["hyps"] cut = hm["cutoffs"] print("hyps", hyps) gp_model = GaussianProcess( kernels=kernels, component="sc", # single-component. For multi-comp, use 'mc' hyps=hyps, cutoffs=cut, hyp_labels=["sig2", "ls2", "sig3", "ls3", "noise"], opt_algorithm="L-BFGS-B", n_cpus=1, ) # ----------- create mapped gaussian process ------------------ grid_params = { "twobody": { "grid_num": [64] }, "threebody": { "grid_num": [16, 16, 16] }, } mgp_model = MappedGaussianProcess(grid_params=grid_params, unique_species=[1, 2], n_cpus=1, var_map="pca") # ------------ create ASE's flare calculator ----------------------- flare_calculator = FLARE_Calculator(gp_model, mgp_model=mgp_model, par=True, use_mapping=True) flare_calc_dict[md_engine] = flare_calculator print(md_engine) yield flare_calc_dict del flare_calc_dict
def test_init(bodies, multihyps, all_mgp, all_gp): """ test the init function """ clean() gp_model = all_gp[f"{bodies}{multihyps}"] # grid parameters grid_params = {} if "2" in bodies: grid_params["twobody"] = {"grid_num": [160], "lower_bound": [0.02]} if "3" in bodies: grid_params["threebody"] = { "grid_num": [31, 32, 33], "lower_bound": [0.02] * 3 } lammps_location = f"{bodies}{multihyps}" data = gp_model.training_statistics try: mgp_model = MappedGaussianProcess( grid_params=grid_params, unique_species=data["species"], n_cpus=1, lmp_file_name=lammps_location, var_map="simple", ) except: mgp_model = MappedGaussianProcess( grid_params=grid_params, unique_species=data["species"], n_cpus=1, lmp_file_name=lammps_location, var_map=None, ) all_mgp[f"{bodies}{multihyps}"] = mgp_model
def flare_calc(): flare_calc_dict = {} for md_engine in md_list: # ---------- create gaussian process model ------------------- # set up GP hyperparameters kernels = ['twobody', 'threebody'] # use 2+3 body kernel parameters = {'cutoff_twobody': 5.0, 'cutoff_threebody': 3.5} pm = ParameterHelper(kernels=kernels, random=True, parameters=parameters) hm = pm.as_dict() hyps = hm['hyps'] cut = hm['cutoffs'] print('hyps', hyps) gp_model = GaussianProcess( kernels=kernels, component='sc', # single-component. For multi-comp, use 'mc' hyps=hyps, cutoffs=cut, hyp_labels=['sig2', 'ls2', 'sig3', 'ls3', 'noise'], opt_algorithm='L-BFGS-B', n_cpus=1) # ----------- create mapped gaussian process ------------------ grid_params = { 'twobody': { 'grid_num': [64] }, 'threebody': { 'grid_num': [16, 16, 16] } } mgp_model = MappedGaussianProcess(grid_params=grid_params, unique_species=[1, 2], n_cpus=1, var_map='pca') # ------------ create ASE's flare calculator ----------------------- flare_calculator = FLARE_Calculator(gp_model, mgp_model=mgp_model, par=True, use_mapping=True) flare_calc_dict[md_engine] = flare_calculator print(md_engine) yield flare_calc_dict del flare_calc_dict
def test_init(bodies, multihyps, all_mgp, all_gp): """ test the init function """ clean() gp_model = all_gp[f'{bodies}{multihyps}'] # grid parameters grid_params = {} if ('2' in bodies): grid_params['twobody'] = {'grid_num': [128], 'lower_bound': [0.02]} if ('3' in bodies): grid_params['threebody'] = { 'grid_num': [31, 32, 33], 'lower_bound': [0.02] * 3 } lammps_location = f'{bodies}{multihyps}' data = gp_model.training_statistics try: mgp_model = MappedGaussianProcess(grid_params=grid_params, unique_species=data['species'], n_cpus=1, lmp_file_name=lammps_location, var_map='simple') except: mgp_model = MappedGaussianProcess(grid_params=grid_params, unique_species=data['species'], n_cpus=1, lmp_file_name=lammps_location, var_map=None) all_mgp[f'{bodies}{multihyps}'] = mgp_model
def from_dict(dct): dct["gp_model"] = GaussianProcess.from_dict(dct["gp_model"]) if dct["use_mapping"]: dct["mgp_model"] = MappedGaussianProcess.from_dict(dct["mgp_model"]) calc = FLARE_Calculator(**dct) res = dct["results"] for key in res: if isinstance(res[key], float): calc.results[key] = res[key] if isinstance(res[key], list): calc.results[key] = np.array(res[key]) if dct["use_mapping"]: for xb in calc.mgp_model.maps: xb_map = calc.mgp_model.maps[xb] xb_map.hyps_mask = calc.gp_model.hyps_mask return calc