|
| 1 | +import numpy as np |
| 2 | +from fitsnap3lib.scrapers.ase_funcs import get_apre, create_shared_arrays |
| 3 | + |
| 4 | + |
| 5 | +def calc_bispectrum_names(twojmax): |
| 6 | + lst = [] |
| 7 | + for j1 in range(0, twojmax + 1): |
| 8 | + for j2 in range(0, j1 + 1): |
| 9 | + for j in range(j1 - j2, min(twojmax, j1 + j2) + 1, 2): |
| 10 | + if j >= j1: |
| 11 | + lst.append([j1 / 2.0, j2 / 2.0, j / 2.0]) |
| 12 | + return lst |
| 13 | + |
| 14 | + |
| 15 | +def ase_scraper( |
| 16 | + s, frames, energies, forces, stresses=[[0, 0, 0], [0, 0, 0], [0, 0, 0]] |
| 17 | +): |
| 18 | + """ |
| 19 | + Custom function to allocate shared arrays used in Calculator and build the internal list of |
| 20 | + dictionaries `data` of configuration info. Customized version of `fitsnap3lib.scrapers.ase_funcs`. |
| 21 | +
|
| 22 | + Args: |
| 23 | + s: fitsnap instance. |
| 24 | + frames: list or array of ASE atoms objects. |
| 25 | + energies: array of energies. |
| 26 | + forces: array of forces for all configurations. |
| 27 | + stresses: array of stresses for all configurations. |
| 28 | +
|
| 29 | + Creates a list of data dictionaries `s.data` suitable for fitsnap descriptor calculation. |
| 30 | + If running in parallel, this list will be distributed over procs, so that each proc will have a |
| 31 | + portion of the list. |
| 32 | + """ |
| 33 | + |
| 34 | + create_shared_arrays(s, frames) |
| 35 | + s.data = [ |
| 36 | + collate_data(a, e, f, s) |
| 37 | + for (a, e, f, s) in zip(frames, energies, forces, stresses) |
| 38 | + ] |
| 39 | + |
| 40 | + |
| 41 | +def collate_data(atoms, energy, forces, stresses): |
| 42 | + """ |
| 43 | + Function to organize fitting data for FitSNAP from ASE atoms objects. |
| 44 | +
|
| 45 | + Args: |
| 46 | + atoms: ASE atoms object for a single configuration of atoms. |
| 47 | + energy: energy of a configuration. |
| 48 | + forces: numpy array of forces for a configuration. |
| 49 | + stresses: numpy array of stresses for a configuration. |
| 50 | +
|
| 51 | + Returns a fitsnap data dictionary for a single configuration. |
| 52 | + """ |
| 53 | + |
| 54 | + # make a data dictionary for this config |
| 55 | + |
| 56 | + apre = get_apre(cell=atoms.cell) |
| 57 | + R = np.dot(np.linalg.inv(atoms.cell), apre) |
| 58 | + |
| 59 | + positions = np.matmul(atoms.get_positions(), R) |
| 60 | + cell = apre.T |
| 61 | + |
| 62 | + data = {} |
| 63 | + data["PositionsStyle"] = "angstrom" |
| 64 | + data["AtomTypeStyle"] = "chemicalsymbol" |
| 65 | + data["StressStyle"] = "bar" |
| 66 | + data["LatticeStyle"] = "angstrom" |
| 67 | + data["EnergyStyle"] = "electronvolt" |
| 68 | + data["ForcesStyle"] = "electronvoltperangstrom" |
| 69 | + data["Group"] = "Displaced_BCC" |
| 70 | + data["File"] = None |
| 71 | + data["Stress"] = stresses |
| 72 | + data["Positions"] = positions |
| 73 | + data["Energy"] = energy |
| 74 | + data["AtomTypes"] = atoms.get_chemical_symbols() |
| 75 | + data["NumAtoms"] = len(atoms) |
| 76 | + data["Forces"] = forces |
| 77 | + data["QMLattice"] = cell |
| 78 | + data["test_bool"] = 0 |
| 79 | + data["Lattice"] = cell |
| 80 | + data["Rotation"] = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) |
| 81 | + data["Translation"] = np.zeros((len(atoms), 3)) |
| 82 | + data["eweight"] = 1.0 |
| 83 | + data["fweight"] = 1.0 / 150.0 |
| 84 | + data["vweight"] = 0.0 |
| 85 | + |
| 86 | + return data |
| 87 | + |
| 88 | + |
| 89 | +def subsample_twojmax(total_bispect, twojmax_lst): |
| 90 | + bi_spect_names_str_lst = [str(lst) for lst in total_bispect] |
| 91 | + twojmax_master_str_lst = [ |
| 92 | + [str(lst) for lst in calc_bispectrum_names(twojmax=tjm)] for tjm in twojmax_lst |
| 93 | + ] |
| 94 | + ind_lst = [ |
| 95 | + [desc in desc_lst for desc in bi_spect_names_str_lst] |
| 96 | + for desc_lst in twojmax_master_str_lst |
| 97 | + ] |
| 98 | + return ind_lst |
0 commit comments