def test_lmp_predict(gp_model, mgp_model, ase_calculator, lmp_calculator): """ test the lammps implementation """ currdir = os.getcwd() label = 'tmp_lmp' for f in os.listdir("./"): if label in f: os.remove(f) if f in ['log.lammps']: os.remove(f) clean() lammps_location = mgp_model.lmp_file_name # lmp file is automatically written now every time MGP is constructed mgp_model.write_lmp_file(lammps_location) # create test structure np.random.seed(1) cell = np.diag(np.array([1, 1, 1])) * 4 nenv = 10 unique_species = gp_model.training_statistics['species'] cutoffs = gp_model.cutoffs struc_test, f = get_random_structure(cell, unique_species, nenv) # build ase atom from struc ase_atoms_flare = struc_test.to_ase_atoms() ase_atoms_flare = FLARE_Atoms.from_ase_atoms(ase_atoms_flare) ase_atoms_flare.set_calculator(ase_calculator) ase_atoms_lmp = struc_test.to_ase_atoms() ase_atoms_lmp.set_calculator(lmp_calculator) try: lmp_en = ase_atoms_lmp.get_potential_energy() flare_en = ase_atoms_flare.get_potential_energy() lmp_stress = ase_atoms_lmp.get_stress() flare_stress = ase_atoms_flare.get_stress() lmp_forces = ase_atoms_lmp.get_forces() flare_forces = ase_atoms_flare.get_forces() except Exception as e: os.chdir(currdir) print(e) raise e os.chdir(currdir) # check that lammps agrees with mgp to within 1 meV/A print("energy", lmp_en, flare_en) assert np.isclose(lmp_en, flare_en, atol=1e-3).all() print("force", lmp_forces, flare_forces) assert np.isclose(lmp_forces, flare_forces, atol=1e-3).all() print("stress", lmp_stress, flare_stress) assert np.isclose(lmp_stress, flare_stress, atol=1e-3).all()
def __init__( self, atoms, timestep, number_of_steps, dft_calc, md_engine, md_kwargs, calculator=None, trajectory=None, **otf_kwargs ): self.atoms = FLARE_Atoms.from_ase_atoms(atoms) if calculator is not None: self.atoms.set_calculator(calculator) self.timestep = timestep self.md_engine = md_engine self.md_kwargs = md_kwargs if md_engine == "VelocityVerlet": MD = VelocityVerlet elif md_engine == "NVTBerendsen": MD = NVTBerendsen elif md_engine == "NPTBerendsen": MD = NPTBerendsen elif md_engine == "NPT": MD = NPT_mod elif md_engine == "Langevin": MD = Langevin elif md_engine == "NoseHoover": MD = NoseHoover else: raise NotImplementedError(md_engine + " is not implemented in ASE") self.md = MD( atoms=self.atoms, timestep=timestep, trajectory=trajectory, **md_kwargs ) force_source = dft_source self.flare_calc = self.atoms.calc # Convert ASE timestep to ps for the output file. flare_dt = timestep / (units.fs * 1e3) super().__init__( dt=flare_dt, number_of_steps=number_of_steps, gp=self.flare_calc.gp_model, force_source=force_source, dft_loc=dft_calc, dft_input=self.atoms, **otf_kwargs ) self.flare_name = self.output_name + "_flare.json" self.dft_name = self.output_name + "_dft.pickle" self.atoms_name = self.output_name + "_atoms.json"
def test_lmp_predict(all_lmp, all_gp, all_mgp, bodies, multihyps): """ test the lammps implementation """ # pytest.skip() prefix = f"{bodies}{multihyps}" mgp_model = all_mgp[prefix] gp_model = all_gp[prefix] lmp_calculator = all_lmp[prefix] ase_calculator = FLARE_Calculator(gp_model, mgp_model, par=False, use_mapping=True) # create test structure np.random.seed(1) cell = np.diag(np.array([1, 1, 1])) * 4 nenv = 10 unique_species = gp_model.training_statistics["species"] cutoffs = gp_model.cutoffs struc_test, f = get_random_structure(cell, unique_species, nenv) # build ase atom from struc ase_atoms_flare = struc_test.to_ase_atoms() ase_atoms_flare = FLARE_Atoms.from_ase_atoms(ase_atoms_flare) ase_atoms_flare.calc = ase_calculator ase_atoms_lmp = deepcopy(struc_test).to_ase_atoms() ase_atoms_lmp.calc = lmp_calculator try: lmp_en = ase_atoms_lmp.get_potential_energy() flare_en = ase_atoms_flare.get_potential_energy() lmp_stress = ase_atoms_lmp.get_stress() flare_stress = ase_atoms_flare.get_stress() lmp_forces = ase_atoms_lmp.get_forces() flare_forces = ase_atoms_flare.get_forces() except Exception as e: os.chdir(curr_path) print(e) raise e os.chdir(curr_path) # check that lammps agrees with mgp to within 1 meV/A print("energy", lmp_en - flare_en, flare_en) assert np.isclose(lmp_en, flare_en, atol=1e-3) print("force", lmp_forces - flare_forces, flare_forces) assert np.isclose(lmp_forces, flare_forces, atol=1e-3).all() print("stress", lmp_stress - flare_stress, flare_stress) assert np.isclose(lmp_stress, flare_stress, atol=1e-3).all() # check the lmp var # mgp_std = np.sqrt(mgp_pred[1]) # print("isclose? diff:", lammps_stds[atom_num]-mgp_std, "mgp value", mgp_std) # assert np.isclose(lammps_stds[atom_num], mgp_std, rtol=1e-2) clean(prefix=prefix)