def all_gps() -> GaussianProcess: """Returns a GP instance with a two-body numba-based kernel""" gp_dict = {True: None, False: None} for multihyps in multihyps_list: cutoffs = np.ones(2)*0.8 hyps, hm, _ = generate_hm(1, 1, multihyps=multihyps) hl = hm['hyps_label'] if (multihyps is False): hm = None # test update_db gpname = '2+3+mb_mc' hyps = np.hstack([hyps, [1, 1]]) hl = np.hstack([hl[:-1], ['sigm', 'lsm'], hl[-1]]) cutoffs = np.ones(3)*0.8 gp_dict[multihyps] = \ GaussianProcess(kernel_name=gpname, hyps=hyps, hyp_labels=hl, cutoffs=cutoffs, multihyps=multihyps, hyps_mask=hm, parallel=False, n_cpus=1) test_structure, forces = get_random_structure(np.eye(3), [1, 2], 3) gp_dict[multihyps].update_db(test_structure, forces) yield gp_dict del gp_dict
def test_permuted_initalization(self): """ Run through some common permutations of input sequences to ensure that the GP correctly initializes. """ for kernel_list in [["2"], ["3"], ["2", "3"]]: GaussianProcess(kernels=kernel_list) with raises(ValueError): GaussianProcess(kernels=["2", "3", "mb"]) full_kernel_list = ["2", "3"] for component in ["sc", "mc"]: GaussianProcess(kernels=full_kernel_list, component=component) for parallel in [True, False]: GaussianProcess(parallel=parallel) for per_atom_par in [True, False]: GaussianProcess(per_atom_par=per_atom_par)
def two_plus_three_gp() -> GaussianProcess: """Returns a GP instance with a 2+3-body kernel.""" cutoffs = np.array([0.8, 0.8]) hyps = np.array([1.0, 1.0, 1.0, 1.0, 1.0]) # test update_db gpname = "2+3_mc" cutoffs = np.ones(2) * 0.8 gp_model = GaussianProcess( kernel_name=gpname, hyps=hyps, cutoffs=cutoffs, multihyps=False, parallel=False, n_cpus=1, ) test_structure, forces = get_random_structure(np.eye(3), [1, 2], 3) energy = 3.14 gp_model.update_db(test_structure, forces, energy=energy) yield gp_model del gp_model
def three_body_gp() -> GaussianProcess: """Returns a GP instance with a two-body numba-based kernel""" print("\nSetting up...\n") # params cell = np.eye(3) unique_species = [2, 1] cutoffs = np.array([0.8, 0.8]) noa = 5 nbond = 0 ntriplet = 1 hyps, hm = generate_hm(nbond, ntriplet) # create test structure test_structure, forces = get_random_structure(cell, unique_species, noa) # test update_db gaussian = \ GaussianProcess(kernel=en.three_body_mc, kernel_grad=en.three_body_mc_grad, hyps=hyps, hyp_labels=hm['hyps_label'], cutoffs=cutoffs, multihyps=True, hyps_mask=hm) gaussian.update_db(test_structure, forces) # return gaussian yield gaussian # code after yield will be executed once all tests are run # this will not be run if an exception is raised in the setup print("\n\nTearing down\n") del gaussian
def methanol_gp(): the_gp = GaussianProcess( kernel_name="2+3_mc", hyps=np.array([ 3.75996759e-06, 1.53990678e-02, 2.50624782e-05, 5.07884426e-01, 1.70172923e-03, ]), cutoffs=np.array([5, 3]), hyp_labels=["l2", "s2", "l3", "s3", "n0"], maxiter=1, opt_algorithm="L-BFGS-B", ) with open(path.join(TEST_FILE_DIR, "methanol_envs.json"), "r") as f: dicts = [loads(s) for s in f.readlines()] for cur_dict in dicts: force = cur_dict["forces"] env = AtomicEnvironment.from_dict(cur_dict) the_gp.add_one_env(env, force) the_gp.set_L_alpha() return the_gp
def test_load_one_frame_and_run(): the_gp = GaussianProcess( kernel_name="2+3_mc", hyps=np.array([ 3.75996759e-06, 1.53990678e-02, 2.50624782e-05, 5.07884426e-01, 1.70172923e-03, ]), cutoffs=np.array([5, 3]), hyp_labels=["l2", "s2", "l3", "s3", "n0"], maxiter=1, opt_algorithm="L-BFGS-B", ) with open(path.join(TEST_FILE_DIR, "methanol_frames.json"), "r") as f: frames = [Structure.from_dict(loads(s)) for s in f.readlines()] tt = TrajectoryTrainer( frames, gp=the_gp, shuffle_frames=True, print_as_xyz=True, rel_std_tolerance=0, abs_std_tolerance=0, skip=15, ) tt.run() for f in glob(f"gp_from_aimd*"): remove(f)
def test_load_one_frame_and_run(): the_gp = GaussianProcess(kernel=two_plus_three_body_mc, kernel_grad=two_plus_three_body_mc_grad, hyps=np.array([ 3.75996759e-06, 1.53990678e-02, 2.50624782e-05, 5.07884426e-01, 1.70172923e-03 ]), cutoffs=np.array([7, 7]), hyp_labels=['l2', 's2', 'l3', 's3', 'n0'], maxiter=1, opt_algorithm='L-BFGS-B') with open('./test_files/methanol_frames.json', 'r') as f: frames = [Structure.from_dict(loads(s)) for s in f.readlines()] tt = TrajectoryTrainer(frames, gp=the_gp, shuffle_frames=True, rel_std_tolerance=0, abs_std_tolerance=0, skip=15) tt.run() os.system('rm ./gp_from_aimd.gp') os.system('rm ./gp_from_aimd.out') os.system('rm ./gp_from_aimd.xyz') os.system('rm ./gp_from_aimd-f.xyz')
def test_training_statistics(): """ Ensure training statistics are being recorded correctly :return: """ test_structure, forces = get_random_structure(np.eye(3), ["H", "Be"], 10) energy = 3.14 gp = GaussianProcess(kernel_name="2", cutoffs=[10]) data = gp.training_statistics assert data["N"] == 0 assert len(data["species"]) == 0 assert len(data["envs_by_species"]) == 0 gp.update_db(test_structure, forces, energy=energy) data = gp.training_statistics assert data["N"] == 10 assert len(data["species"]) == len(set(test_structure.coded_species)) assert len(data["envs_by_species"]) == len( set(test_structure.coded_species))
def test_set_L_alpha(two_body_gp, params): # params cell = np.eye(3) unique_species = [2, 1] noa = 2 # create test structure test_structure, forces = get_random_structure(cell, unique_species, noa) # set gp model kernel = en.two_plus_three_body kernel_grad = en.two_plus_three_body_grad hyps = np.array([ 2.23751151e-01, 8.19990316e-01, 1.28421842e-04, 1.07467158e+00, 5.50677932e-02 ]) cutoffs = np.array([5.4, 5.4]) hyp_labels = ['sig2', 'ls2', 'sig3', 'ls3', 'noise'] energy_force_kernel = en.two_plus_three_force_en energy_kernel = en.two_plus_three_en opt_algorithm = 'BFGS' # test update_db gaussian = \ GaussianProcess(kernel, kernel_grad, hyps, cutoffs, hyp_labels, energy_force_kernel, energy_kernel, opt_algorithm, par=True, no_cpus=2) gaussian.update_db(test_structure, forces) gaussian.set_L_alpha()
def two_body_gp() -> GaussianProcess: """Returns a GP instance with a two-body numba-based kernel""" print("\nSetting up...\n") # params cell = np.eye(3) unique_species = [2, 1] cutoffs = np.array([0.8, 0.8]) noa = 5 # create test structure test_structure, forces = get_random_structure(cell, unique_species, noa) # test update_db gaussian = \ GaussianProcess(kernel=en.three_body, kernel_grad=en.three_body_grad, hyps=np.array([1, 1, 1]), hyp_labels=['Length', 'Signal Var.', 'Noise Var.'], par=True, no_cpus=2, cutoffs=cutoffs) gaussian.update_db(test_structure, forces) # return gaussian yield gaussian # code after yield will be executed once all tests are run # this will not be run if an exception is raised in the setup print("\n\nTearing down\n") del gaussian
def test_training_statistics(): """ Ensure training statistics are being recorded correctly :return: """ test_structure, forces = get_random_structure(np.eye(3), ['H', 'Be'], 10) gp = GaussianProcess(kernel_name='2', cutoffs=[10]) data = gp.training_statistics assert data['N'] == 0 assert len(data['species']) == 0 assert len(data['envs_by_species']) == 0 gp.update_db(test_structure, forces) data = gp.training_statistics assert data['N'] == 10 assert len(data['species']) == len(set(test_structure.coded_species)) assert len(data['envs_by_species']) == len(set( test_structure.coded_species))
def all_gps() -> GaussianProcess: """Returns a GP instance with a two-body numba-based kernel""" gp_dict = {True: None, False: None} for multihyps in multihyps_list: hyps, hm, cutoffs = generate_hm(1, 1, multihyps=multihyps) hl = hm["hyp_labels"] # test update_db gp_dict[multihyps] = GaussianProcess( kernels=hm["kernels"], hyps=hyps, hyp_labels=hl, cutoffs=cutoffs, hyps_mask=hm, parallel=False, n_cpus=1, ) test_structure, forces = get_random_structure(np.eye(3), [1, 2], 3) energy = 3.14 gp_dict[multihyps].update_db(test_structure, forces, energy=energy) yield gp_dict del gp_dict
def test_pred_on_elements(): the_gp = GaussianProcess(kernel_name="2+3_mc", hyps=np.array([ 3.75996759e-06, 1.53990678e-02, 2.50624782e-05, 5.07884426e-01, 1.70172923e-03 ]), cutoffs=np.array([7, 3]), hyp_labels=['l2', 's2', 'l3', 's3', 'n0'], maxiter=1, opt_algorithm='L-BFGS-B') with open('./test_files/methanol_frames.json', 'r') as f: frames = [Structure.from_dict(loads(s)) for s in f.readlines()] with open('./test_files/methanol_envs.json', 'r') as f: data_dicts = [loads(s) for s in f.readlines()[:6]] envs = [AtomicEnvironment.from_dict(d) for d in data_dicts] forces = [np.array(d['forces']) for d in data_dicts] seeds = list(zip(envs, forces)) all_frames = deepcopy(frames) tt = TrajectoryTrainer(frames, gp=the_gp, shuffle_frames=False, rel_std_tolerance=0, abs_std_tolerance=0, abs_force_tolerance=.001, skip=5, min_atoms_per_train=100, pre_train_seed_envs=seeds, pre_train_seed_frames=[frames[-1]], max_atoms_from_frame=4, output_name='meth_test', model_format='json', atom_checkpoint_interval=50, pre_train_atoms_per_element={'H': 1}, predict_atoms_per_element={ 'H': 0, 'C': 1, 'O': 0 }) # Set to predict only on Carbon after training on H to ensure errors are # high and that they get added to the gp tt.run() # Ensure forces weren't written directly to structure for i in range(len(all_frames)): assert np.array_equal(all_frames[i].forces, frames[i].forces) # Assert that Carbon atoms were correctly added assert the_gp.training_statistics['envs_by_species']['C'] > 2 for f in glob(f"meth_test*"): remove(f) for f in glob(f"gp_from_aimd*"): remove(f)
def test_constrained_optimization_simple(): """ Test constrained optimization with a standard number of hyperparameters (3 for a 3-body) :return: """ # params cell = np.eye(3) species = [1, 1, 2, 2, 2] positions = np.random.uniform(0, 1, (5, 3)) forces = np.random.uniform(0, 1, (5, 3)) two_species_structure = Structure(cell=cell, species=species, positions=positions, forces=forces) hyp_labels = [ '2-Body_sig2,', '2-Body_l2', '3-Body_sig2', '3-Body_l2', 'noise' ] hyps = np.array([1.2, 2.2, 3.2, 4.2, 12.]) cutoffs = np.array((.8, .8)) # Define hyp masks spec_mask = np.zeros(118, dtype=int) spec_mask[1] = 1 hyps_mask = { 'nspec': 2, 'spec_mask': spec_mask, 'nbond': 2, 'bond_mask': [0, 1, 1, 1], 'ntriplet': 2, 'triplet_mask': [0, 1, 1, 1, 1, 1, 1, 1], 'original': np.array([1.1, 1.2, 2.1, 2.2, 3.1, 3.2, 4.1, 4.2, 12.]), 'train_noise': True, 'map': [1, 3, 5, 7, 8] } gp = GaussianProcess(kernel=en.two_plus_three_body_mc, kernel_grad=en.two_plus_three_body_mc_grad, hyps=hyps, hyp_labels=hyp_labels, cutoffs=cutoffs, par=False, n_cpus=1, hyps_mask=hyps_mask, maxiter=1, multihyps=True) gp.update_db(two_species_structure, two_species_structure.forces) # Check that the hyperparameters were updated results = gp.train() assert not np.equal(results.x, hyps).all()
def get_gp( bodies, kernel_type="mc", multihyps=True, cellabc=[1, 1, 1.5], force_only=False, noa=5, ) -> GaussianProcess: """Returns a GP instance with a two-body numba-based kernel""" print("\nSetting up...\n") # params cell = np.diag(cellabc) unique_species = [1, 2] ntwobody = 0 nthreebody = 0 prefix = bodies if "2" in bodies or "two" in bodies: ntwobody = 1 if "3" in bodies or "three" in bodies: nthreebody = 1 hyps, hm, _ = generate_hm(ntwobody, nthreebody, nmanybody=0, multihyps=multihyps) cutoffs = hm["cutoffs"] kernels = hm["kernels"] hl = hm["hyp_labels"] # create test structure test_structure, forces = get_random_structure(cell, unique_species, noa) energy = 3.14 # test update_db gaussian = GaussianProcess( kernels=kernels, component=kernel_type, hyps=hyps, hyp_labels=hl, cutoffs=cutoffs, hyps_mask=hm, parallel=False, n_cpus=1, ) if force_only: gaussian.update_db(test_structure, forces) else: gaussian.update_db(test_structure, forces, energy=energy) gaussian.check_L_alpha() # print(gaussian.alpha) return gaussian
def test_seed_and_run(): the_gp = GaussianProcess( kernel_name="2+3_mc", hyps=np.array([ 3.75996759e-06, 1.53990678e-02, 2.50624782e-05, 5.07884426e-01, 1.70172923e-03, ]), cutoffs=np.array([5, 3]), hyp_labels=["l2", "s2", "l3", "s3", "n0"], maxiter=1, opt_algorithm="L-BFGS-B", ) with open(path.join(TEST_FILE_DIR, "methanol_frames.json"), "r") as f: frames = [Structure.from_dict(loads(s)) for s in f.readlines()] with open(path.join(TEST_FILE_DIR, "methanol_envs.json"), "r") as f: data_dicts = [loads(s) for s in f.readlines()[:6]] envs = [AtomicEnvironment.from_dict(d) for d in data_dicts] forces = [np.array(d["forces"]) for d in data_dicts] seeds = list(zip(envs, forces)) tt = TrajectoryTrainer( frames, gp=the_gp, shuffle_frames=True, rel_std_tolerance=0, abs_std_tolerance=0, skip=10, pre_train_seed_envs=seeds, pre_train_seed_frames=[frames[-1]], max_atoms_from_frame=4, output_name="meth_test", model_format="pickle", train_checkpoint_interval=1, pre_train_atoms_per_element={"H": 1}, ) tt.run() with open("meth_test_model.pickle", "rb") as f: new_gp = pickle.load(f) test_env = envs[0] for d in [1, 2, 3]: assert np.all( the_gp.predict(x_t=test_env, d=d) == new_gp.predict(x_t=test_env, d=d)) for f in glob(f"meth_test*"): remove(f)
def test_otf_al(): """ Test that an otf run can survive going for more steps :return: """ os.system('cp ./test_files/qe_input_2.in ./pwscf.in') # make gp model kernel = en.three_body kernel_grad = en.three_body_grad hyps = np.array([0.1, 1, 0.01]) hyp_labels = ['Signal Std', 'Length Scale', 'Noise Std'] cutoffs = np.array([3.9, 3.9]) energy_force_kernel = en.three_body_force_en gp = \ GaussianProcess(kernel=kernel, kernel_grad=kernel_grad, hyps=hyps, cutoffs=cutoffs, hyp_labels=hyp_labels, energy_force_kernel=energy_force_kernel, maxiter=50) # set up DFT calculator qe_input = './pwscf.in' # quantum espresso input file dft_loc = os.environ.get('PWSCF_COMMAND') # set up OTF parameters dt = 0.001 # timestep (ps) number_of_steps = 100 # number of steps std_tolerance_factor = 1 max_atoms_added = 2 freeze_hyps = 3 otf = OTF(qe_input, dt, number_of_steps, gp, dft_loc, std_tolerance_factor, init_atoms=[0], calculate_energy=True, output_name='al_otf_qe', freeze_hyps=freeze_hyps, skip=5, max_atoms_added=max_atoms_added) # run OTF MD otf.run() os.system('mkdir test_outputs') os.system('mv al_otf_qe* test_outputs') cleanup_espresso_run()
def get_gp(par=False, per_atom_par=False, n_cpus=1): hyps = np.array([1, 1, 1, 1, 1]) hyp_labels = [ 'Signal Std 2b', 'Length Scale 2b', 'Signal Std 3b', 'Length Scale 3b', 'Noise Std' ] cutoffs = {'twobody': 4, 'threebody': 4} return GaussianProcess(\ kernel_name='23mc', hyps=hyps, cutoffs=cutoffs, hyp_labels=hyp_labels, maxiter=50, par=par, per_atom_par=per_atom_par, n_cpus=n_cpus)
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 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_seed_and_run(): the_gp = GaussianProcess(kernel=two_plus_three_body_mc, kernel_grad=two_plus_three_body_mc_grad, hyps=np.array([ 3.75996759e-06, 1.53990678e-02, 2.50624782e-05, 5.07884426e-01, 1.70172923e-03 ]), cutoffs=np.array([7, 7]), hyp_labels=['l2', 's2', 'l3', 's3', 'n0'], maxiter=1, opt_algorithm='L-BFGS-B') with open('./test_files/methanol_frames.json', 'r') as f: frames = [Structure.from_dict(loads(s)) for s in f.readlines()] with open('./test_files/methanol_envs.json', 'r') as f: data_dicts = [loads(s) for s in f.readlines()[:6]] envs = [AtomicEnvironment.from_dict(d) for d in data_dicts] forces = [np.array(d['forces']) for d in data_dicts] seeds = list(zip(envs, forces)) tt = TrajectoryTrainer(frames, gp=the_gp, shuffle_frames=True, rel_std_tolerance=0, abs_std_tolerance=0, skip=15, pre_train_seed_envs=seeds, pre_train_seed_frames=[frames[-1]], max_atoms_from_frame=4, model_write='meth_test.pickle', model_format='pickle', checkpoint_interval=1, pre_train_atoms_per_element={'H': 1}) tt.run() with open('meth_test.pickle', 'rb') as f: new_gp = pickle.load(f) test_env = envs[0] for d in [0, 1, 2]: assert np.all( the_gp.predict(x_t=test_env, d=d) == new_gp.predict(x_t=test_env, d=d)) os.system('rm ./gp_from_aimd.out') os.system('rm ./gp_from_aimd.xyz') os.system('rm ./gp_from_aimd-f.xyz') os.system('rm ./meth_test.pickle')
def test_otf_al(): """ Test that an otf run can survive going for more steps :return: """ call('cp ./test_files/cp2k_input_2.in ./cp2k.in'.split()) cp2k_input = './cp2k.in' dt = 0.001 number_of_steps = 5 cutoffs = np.array([3.9, 3.9]) dft_loc = os.environ.get('CP2K_COMMAND') std_tolerance_factor = 1 max_atoms_added = 2 freeze_hyps = 3 # make gp model kernel = en.three_body kernel_grad = en.three_body_grad hyps = np.array([0.1, 1, 0.01]) hyp_labels = ['Signal Std', 'Length Scale', 'Noise Std'] energy_force_kernel = en.three_body_force_en gp = \ GaussianProcess(kernel=kernel, kernel_grad=kernel_grad, hyps=hyps, cutoffs=cutoffs, hyp_labels=hyp_labels, energy_force_kernel=energy_force_kernel, maxiter=50) otf = OTF(cp2k_input, dt, number_of_steps, gp, dft_loc, std_tolerance_factor, init_atoms=[0], calculate_energy=True, output_name='al_otf_cp2k', freeze_hyps=freeze_hyps, skip=5, force_source="cp2k", max_atoms_added=max_atoms_added) otf.run() call('mkdir test_outputs'.split()) call('mv al_otf_cp2k*.* test_outputs'.split()) cleanup("al_otf_cp2k")
def test_otf_h2_par(): """ Test that an otf run can survive going for more steps :return: """ call('cp ./test_files/cp2k_input_1.in ./cp2k.in'.split()) cp2k_input = './cp2k.in' dt = 0.0001 number_of_steps = 5 cutoffs = np.array([5]) dft_loc = os.environ.get('CP2K_COMMAND') std_tolerance_factor = -0.1 # make gp model kernel = en.two_body kernel_grad = en.two_body_grad hyps = np.array([1, 1, 1]) hyp_labels = ['Signal Std', 'Length Scale', 'Noise Std'] energy_force_kernel = en.two_body_force_en gp = \ GaussianProcess(kernel=kernel, kernel_grad=kernel_grad, hyps=hyps, cutoffs=cutoffs, hyp_labels=hyp_labels, energy_force_kernel=energy_force_kernel, par=True, maxiter=50) otf = OTF(cp2k_input, dt, number_of_steps, gp, dft_loc, std_tolerance_factor, init_atoms=[0], calculate_energy=True, max_atoms_added=1, dft_softwarename="cp2k", no_cpus=2, par=True, mpi="mpi", output_name='h2_otf_cp2k_par') otf.run() call('mkdir test_outputs'.split()) call('mv h2_otf_cp2k_par* test_outputs'.split()) cleanup()
def test_otf_Al_npool(): """ Test that an otf run can survive going for more steps :return: """ os.system('cp ./test_files/qe_input_2.in ./pwscf.in') qe_input = './pwscf.in' dt = 0.0001 number_of_steps = 4 cutoffs = np.array([5]) dft_loc = os.environ.get('PWSCF_COMMAND') std_tolerance_factor = -0.1 # make gp model kernel = en.two_body kernel_grad = en.two_body_grad hyps = np.array([1, 1, 1]) hyp_labels = ['Signal Std', 'Length Scale', 'Noise Std'] energy_force_kernel = en.two_body_force_en gp = \ GaussianProcess(kernel=kernel, kernel_grad=kernel_grad, hyps=hyps, cutoffs=cutoffs, hyp_labels=hyp_labels, par=True, energy_force_kernel=energy_force_kernel, maxiter=50) otf = OTF(qe_input, dt, number_of_steps, gp, dft_loc, std_tolerance_factor, init_atoms=[0], calculate_energy=True, max_atoms_added=1, no_cpus=2, par=True, npool=2, mpi="mpi", output_name='h2_otf_qe_par') otf.run() os.system('mkdir test_outputs') os.system('mv h2_otf_qe_par* test_outputs') cleanup_espresso_run()
def two_plus_three_gp() -> GaussianProcess: """Returns a GP instance with a 2+3-body kernel.""" cutoffs = {'twobody': 0.8, 'threebody': 0.8} hyps = np.array([1., 1., 1., 1., 1.]) gp_model = \ GaussianProcess(kernels=['twobody', 'threebody'], hyps=hyps, cutoffs=cutoffs, multihyps=False, parallel=False, n_cpus=1) test_structure, forces = \ get_random_structure(np.eye(3), [1, 2], 3) energy = 3.14 gp_model.update_db(test_structure, forces, energy=energy) yield gp_model del gp_model
def test_otf_h2(): """ :return: """ os.system('cp ./test_files/test_POSCAR_2 POSCAR') vasp_input = './POSCAR' dt = 0.0001 number_of_steps = 5 cutoffs = np.array([5]) dft_loc = 'cp ./test_files/test_vasprun_h2.xml vasprun.xml' std_tolerance_factor = -0.1 # make gp model kernel = en.two_body kernel_grad = en.two_body_grad hyps = np.array([1, 1, 1]) hyp_labels = ['Signal Std', 'Length Scale', 'Noise Std'] energy_force_kernel = en.two_body_force_en gp = GaussianProcess(kernel=kernel, kernel_grad=kernel_grad, hyps=hyps, cutoffs=cutoffs, hyp_labels=hyp_labels, energy_force_kernel=energy_force_kernel, maxiter=50) otf = OTF(vasp_input, dt, number_of_steps, gp, dft_loc, std_tolerance_factor, init_atoms=[0], calculate_energy=True, max_atoms_added=1, no_cpus=1, dft_softwarename='vasp', output_name='h2_otf_vasp') otf.run() os.system('mv h2_otf_vasp* test_outputs')
def get_gp(par=False, per_atom_par=False, n_cpus=1): hyps = np.array([1, 1, 1, 1, 1]) hyp_labels = [ "Signal Std 2b", "Length Scale 2b", "Signal Std 3b", "Length Scale 3b", "Noise Std", ] cutoffs = {"twobody": 4, "threebody": 4} return GaussianProcess( kernel_name="23mc", hyps=hyps, cutoffs=cutoffs, hyp_labels=hyp_labels, maxiter=50, par=par, per_atom_par=per_atom_par, n_cpus=n_cpus, )
def methanol_gp(): the_gp = GaussianProcess(kernel_name="2+3_mc", hyps=np.array([ 3.75996759e-06, 1.53990678e-02, 2.50624782e-05, 5.07884426e-01, 1.70172923e-03 ]), cutoffs=np.array([7, 7]), hyp_labels=['l2', 's2', 'l3', 's3', 'n0'], maxiter=1, opt_algorithm='L-BFGS-B') with open('./test_files/methanol_envs.json') as f: dicts = [loads(s) for s in f.readlines()] for cur_dict in dicts: force = cur_dict['forces'] env = AtomicEnvironment.from_dict(cur_dict) the_gp.add_one_env(env, force) the_gp.set_L_alpha() return the_gp
def get_full_gp(self) -> GaussianProcess: """ "Flatten" an RBCM into a standard Gaussian Process. "I think the people of this country have had enough of experts." - Michael Gove :return: """ gp_model = GaussianProcess(**self.__dict__) gp_model.training_data = [] gp_model.training_labels = [] gp_model.name = "rbcm_derived_gp" for i in range(self.n_experts): gp_model.training_data += self.training_data[i] gp_model.training_labels += self.training_labels[i] gp_model.training_labels_np = np.hstack(gp_model.training_labels) gp_model.all_labels = np.hstack( (gp_model.training_labels_np, gp_model.energy_labels_np)) gp_model.sync_data() return gp_model
def test_to_from_gp(): """ To/from methods for creating new RBCMs and turning them back into GPs :return: """ gp = GaussianProcess() for frame in methanol_frames: gp.update_db(frame, forces=frame.forces) rbcm = RobustBayesianCommitteeMachine.from_gp(gp) new_gp = rbcm.get_full_gp() test_env = methanol_envs[0] for d in range(1, 4): assert np.array_equal(gp.predict(test_env, d), new_gp.predict(test_env, d))