def test_parameter(self): modelname = "SW_StillingerWeber_1985_Si__MO_405512056662_005" model = KIMModel(modelname) # parameters params = model.get_model_params() sigma = params["sigma"][0] A = params["A"][0] # optimizing parameters # B will not be optimized, only providing initial guess model.set_opt_params(sigma=[["default"]], B=[["default", "fix"]], A=[["default"]]) calc = Calculator(model) x0 = calc.get_opt_params() assert x0[0] == sigma assert x0[1] == A assert len(x0) == 2 assert model.get_num_opt_params() == 2 x1 = [i + 0.1 for i in x0] calc.update_model_params(x1) assert params["sigma"][0] == sigma + 0.1 assert params["A"][0] == A + 0.1
def test_lj(): model = LennardJones() # set params directly model.set_fitting_params(sigma=[[1.1, "fix"]], epsilon=[[2.1, None, 3.0]]) model.update_model_params() # model.echo_model_params() # model.echo_fitting_params() # set params by reading from file (the same as set params directly) # fname = 'tmp_lj.params' # write_tmp_params(fname) # model.read_fitting_params(fname) # delete_tmp_params(fname) calc = Calculator(model) dset = Dataset(order_by_species=False) fname = "./configs_extxyz/MoS2/MoS2_energy_forces_stress.xyz" dset.read(fname) configs = dset.get_configs() energy_forces_stress(calc, configs, True, False, False) energy_forces_stress(calc, configs, True, True, False) energy_forces_stress(calc, configs, True, True, True) # params relation callback model.set_params_relation_callback(params_relation) x0 = calc.get_opt_params() calc.update_opt_params(x0) sigma = model.get_model_params("sigma") epsilon = model.get_model_params("epsilon") assert np.allclose(sigma * 2, epsilon)
def init(): model = KIMModel(model_name="SW_StillingerWeber_1985_Si__MO_405512056662_006") # Cannot set them all by calling this function only once, because the assertion # depends on order model.set_opt_params(A=[[5.0]]) model.set_opt_params(B=[["default"]]) model.set_opt_params(sigma=[[2.0951, "fix"]]) model.set_opt_params(gamma=[[1.5]]) path = Path(__file__).parent.joinpath("configs_extxyz/Si_4") tset = Dataset(path) configs = tset.get_configs() calc = Calculator(model) calc.create(configs, use_energy=True, use_forces=True) loss = Loss(calc, residual_fn=residual_fn, nprocs=1) return loss
def test_compute(self): test_file_path = Path(__file__).parents[1].joinpath("configs_extxyz") tset = Dataset(test_file_path.joinpath("Si_4")) configs = tset.get_configs() modelname = "SW_StillingerWeber_1985_Si__MO_405512056662_005" model = KIMModel(modelname) # calculator calc = Calculator(model) compute_arguments = calc.create(configs) for i, ca in enumerate(compute_arguments): calc.compute(ca) energy = calc.get_energy(ca) forces = calc.get_forces(ca)[:3] assert energy == pytest.approx(ref_energies[i], 1e-6) assert np.allclose(forces, ref_forces[i])
""" Compute the root-mean-square error (RMSE) of a model prediction and reference values in the dataset. """ from kliff.analyzers import EnergyForcesRMSE from kliff.calculators import Calculator from kliff.dataset import Dataset from kliff.models import KIMModel from kliff.utils import download_dataset model = KIMModel(model_name="SW_StillingerWeber_1985_Si__MO_405512056662_005") # load the trained model back # model.load("kliff_model.yaml") dataset_path = download_dataset(dataset_name="Si_training_set_4_configs") tset = Dataset(dataset_path) configs = tset.get_configs() calc = Calculator(model) calc.create(configs) analyzer = EnergyForcesRMSE(calc) analyzer.run(verbose=2, sort="energy")
def test_main(): # training set tset = Dataset() tset.read("./configs_extxyz/Si_4") configs = tset.get_configs() # model modelname = "SW_StillingerWeber_1985_Si__MO_405512056662_005" model = KIM(modelname) # calculator calc = Calculator(model) compute_arguments = calc.create(configs) for i, ca in enumerate(compute_arguments): calc.compute(ca) energy = calc.get_energy(ca) forces = calc.get_forces(ca)[:3] assert energy == pytest.approx(ref_energies[i], 1e-6) assert np.allclose(forces, ref_forces[i]) # Cannot set them all by calling this function only once, because the assertion # depends on order model.set_fitting_params(sigma=[["default"]]) model.set_fitting_params(A=[["default", "fix"]]) model.set_fitting_params(B=[["default"]]) # update params x0 = calc.get_opt_params() x1 = [i + 0.1 for i in x0] calc.update_opt_params(x1) params = model.inquire_params() assert np.allclose(params["sigma"].get_value(), [x1[0]]) assert np.allclose(params["B"].get_value(), [x1[1]]) # restore params calc.update_opt_params(x0)