def calculate(self, atoms, properties, system_changes): Calculator.calculate(self, atoms, properties, system_changes) energies = [] forces = [] make_amp_descriptors_simple_nn([atoms], Gs, elements, cores=10, label="test") for calc in self.trained_calcs: energies.append(calc.get_potential_energy(atoms)) forces.append(calc.get_forces(atoms)) energies = np.array(energies) forces = np.array(forces) energy_pred, force_pred, uncertainty = self.calculate_stats( energies, forces) if uncertainty >= self.uncertain_tol: new_data = atoms.copy() new_data.set_calculator(self.parent_calc) self.results["energy"] = new_data.get_potential_energy( apply_constraint=False) self.results["forces"] = new_data.get_forces( apply_constraint=False) self.ensemble_sets, self.parent_dataset = self.bootstrap_ensemble( self.parent_dataset, self.ensemble_sets, new_data=new_data) self.trained_calcs = self.construct_calc(train(self.ensemble_sets)) else: self.results["energy"] = float(energy_pred) self.results["forces"] = force_pred
def test_fp_match(): for i in range(100): # Tests whether the generated fingerprints are consistent with that of AMPs atoms = molecule("H2O") atoms.set_cell([10, 10, 10]) atoms.set_pbc = [True] * 3 atoms.set_calculator( sp(atoms=atoms, energy=-1, forces=np.array([[-1, -1, -1], [-1, -1, -1]])) ) Gs = {} images = [atoms] Gs["G2_etas"] = [0.005] * 2 Gs["G2_rs_s"] = [0] * 2 Gs["G4_etas"] = [0.005] * 2 Gs["G4_zetas"] = [1.0, 4.0] Gs["G4_gammas"] = [1.0, -1.0] Gs["cutoff"] = 6.5 elements = list( sorted(set([atom.symbol for atoms in images for atom in atoms])) ) G = make_symmetry_functions(elements=elements, type="G2", etas=Gs["G2_etas"]) G += make_symmetry_functions( elements=elements, type="G4", etas=Gs["G4_etas"], zetas=Gs["G4_zetas"], gammas=Gs["G4_gammas"], ) G = {"O": G, "H": G} hashes = stock_hash(images) amp_hash = list(hashes.keys())[0] make_amp_descriptors_simple_nn(images, Gs, cores=1, label='test', elements=elements) s_nn_hash = list(new_hash(images, Gs).keys())[0] with open("amp-data-fingerprints.ampdb/loose/" + s_nn_hash, "rb") as f: simple_nn = load(f) os.system("rm amp-data-fingerprints.ampdb/loose/" + s_nn_hash) descriptor = Gaussian(elements=elements, Gs=G, cutoff=Cosine(Gs["cutoff"])) descriptor.calculate_fingerprints(hashes, calculate_derivatives=True) with open("amp-data-fingerprints.ampdb/loose/" + amp_hash, "rb") as f: amp = load(f) os.system("rm amp-data-fingerprints.ampdb/loose/" + amp_hash) for s, am in zip(simple_nn, amp): for i, j in zip(s[1], am[1]): assert abs(i - j) <= 1e-5, "Fingerprints do not match!"
def __init__(self, images, unique_atoms, descriptor, Gs, fprange, label="example", cores=1): self.images = images if type(images) is not list: self.images = [images] self.descriptor = descriptor self.atom_images = self.images if isinstance(images, str): extension = os.path.splitext(images)[1] if extension != (".traj" or ".db"): self.atom_images = ase.io.read(images, ":") self.fprange = fprange self.training_unique_atoms = unique_atoms self.hashed_images = amp_hash(self.atom_images) if descriptor == SNN_Gaussian: self.hashed_images = hash_images(self.atom_images, Gs) self.fps, self.fp_primes = make_amp_descriptors_simple_nn( self.atom_images, Gs, self.training_unique_atoms, cores=cores, label=label, save=False, ) self.unique_atoms = self.unique()
def __init__(self, images, unique_atoms, descriptor, Gs, fprange, label="example", cores=1): self.images = images if type(images) is not list: self.images = [images] self.descriptor = descriptor self.atom_images = self.images if isinstance(images, str): extension = os.path.splitext(images)[1] if extension != (".traj" or ".db"): self.atom_images = ase.io.read(images, ":") self.fprange = fprange self.training_unique_atoms = unique_atoms self.hashed_images = amp_hash(self.atom_images) G2_etas = Gs["G2_etas"] G2_rs_s = Gs["G2_rs_s"] G4_etas = Gs["G4_etas"] G4_zetas = Gs["G4_zetas"] G4_gammas = Gs["G4_gammas"] cutoff = Gs["cutoff"] if str(descriptor)[8:16] == "amptorch": self.hashed_images = hash_images(self.atom_images, Gs) make_amp_descriptors_simple_nn( self.atom_images, Gs, self.training_unique_atoms, cores=cores, label=label ) G = make_symmetry_functions(elements=self.training_unique_atoms, type="G2", etas=G2_etas) G += make_symmetry_functions( elements=self.training_unique_atoms, type="G4", etas=G4_etas, zetas=G4_zetas, gammas=G4_gammas, ) for g in G: g["Rs"] = G2_rs_s self.descriptor = self.descriptor(Gs=G, cutoff=cutoff) self.descriptor.calculate_fingerprints( self.hashed_images, calculate_derivatives=True ) self.unique_atoms = self.unique()
def __init__(self, images, unique_atoms, descriptor, Gs, fprange, label="example", cores=1, specific_atoms=False, save_test_fp=False): self.images = images if type(images) is not list: self.images = [images] self.descriptor = descriptor self.atom_images = self.images if isinstance(images, str): extension = os.path.splitext(images)[1] if extension != (".traj" or ".db"): self.atom_images = ase.io.read(images, ":") self.fprange = fprange self.training_unique_atoms = unique_atoms self.hashed_images = amp_hash(self.atom_images) self.specific_atoms = specific_atoms self.save_fp = save_test_fp G2_etas = Gs["G2_etas"] G2_rs_s = Gs["G2_rs_s"] G4_etas = Gs["G4_etas"] G4_zetas = Gs["G4_zetas"] G4_gammas = Gs["G4_gammas"] cutoff = Gs["cutoff"] if str(descriptor)[8:16] == "amptorch": self.hashed_images = hash_images(self.atom_images, Gs) self.fps, self.fp_primes = make_amp_descriptors_simple_nn( self.atom_images, Gs, self.training_unique_atoms, cores=cores, label=label, save=self.save_fp, specific_atoms=self.specific_atoms) self.unique_atoms = self.unique()
def __init__(self, images, descriptor, Gs, forcetraining, label, cores, delta_data=None, store_primes=False, specific_atoms=False): self.images = images self.base_descriptor = descriptor self.descriptor = descriptor self.Gs = Gs self.atom_images = self.images self.forcetraining = forcetraining self.store_primes = store_primes self.cores = cores self.delta = False self.specific_atoms = specific_atoms if delta_data is not None: self.delta_data = delta_data self.delta_energies = np.array(delta_data[0]) self.delta_forces = delta_data[1] self.num_atoms = np.array(delta_data[2]) self.delta = True if self.store_primes: if not os.path.isdir("./stored-primes/"): os.mkdir("stored-primes") if isinstance(images, str): extension = os.path.splitext(images)[1] if extension != (".traj" or ".db"): self.atom_images = ase.io.read(images, ":") self.elements = self.unique() #TODO Print log - control verbose print("Calculating fingerprints...") G2_etas = Gs["G2_etas"] G2_rs_s = Gs["G2_rs_s"] G4_etas = Gs["G4_etas"] G4_zetas = Gs["G4_zetas"] G4_gammas = Gs["G4_gammas"] cutoff = Gs["cutoff"] # create simple_nn fingerprints if str(descriptor)[8:16] == "amptorch": self.hashed_images = hash_images(self.atom_images, Gs=Gs) make_amp_descriptors_simple_nn(self.atom_images, Gs, self.elements, cores=cores, label=label, specific_atoms=self.specific_atoms) self.isamp_hash = False else: self.hashed_images = amp_hash(self.atom_images) self.isamp_hash = True G = make_symmetry_functions(elements=self.elements, type="G2", etas=G2_etas) G += make_symmetry_functions( elements=self.elements, type="G4", etas=G4_etas, zetas=G4_zetas, gammas=G4_gammas, ) for g in list(G): g["Rs"] = G2_rs_s self.descriptor = self.descriptor(Gs=G, cutoff=cutoff) self.descriptor.calculate_fingerprints( self.hashed_images, calculate_derivatives=forcetraining) print("Fingerprints Calculated!") self.fprange = calculate_fingerprints_range(self.descriptor, self.hashed_images) # perform preprocessing self.fingerprint_dataset, self.energy_dataset, self.num_of_atoms, self.sparse_fprimes, self.forces_dataset, self.index_hashes, self.scalings, self.rearange_forces = ( self.preprocess_data())
if ensemble == "nvtberendsen": dyn = NVTBerendsen(slab, dt * ase.units.fs, temp, taut=300 * ase.units.fs) traj = ase.io.Trajectory(label + ".traj", "w", slab) dyn.attach(traj.write, interval=1) dyn.run(count - 1) if __name__ == "__main__": multiprocessing.set_start_method("spawn") Gs = {} Gs["G2_etas"] = np.logspace(np.log10(0.05), np.log10(5.0), num=4) Gs["G2_rs_s"] = [0] * 4 Gs["G4_etas"] = [0.005] Gs["G4_zetas"] = [1.0, 4.0] Gs["G4_gammas"] = [+1.0, -1] Gs["cutoff"] = 5.876798323827276 # EMT asap_cutoff: False images = ase.io.read("../../datasets/COCu_ber_100ps_300K.traj", ":100") elements = np.array([atom.symbol for atoms in images for atom in atoms]) _, idx = np.unique(elements, return_index=True) elements = list(elements[np.sort(idx)]) make_amp_descriptors_simple_nn(images, Gs, elements, cores=10, label="test") md_run(images, EMT(), images[0], 300, 1, 2000, "test", "nvtberendsen")