def find_swaps(): all_atoms = [] e_ref = [] db = connect(db_name) for row in db.select(converged=True): all_atoms.append(row.toatoms()) final_struct_id = row.final_struct_id final = db.get(id=final_struct_id) e_ref.append(final.energy) extractor = MCTrajectoryExtractor() swaps = extractor.find_swaps(all_atoms) with open(eci_file, 'r') as infile: eci = json.load(infile) e_pred = [] setting = settingFromJSON("almgsixSettings.json") e_pred = {} for swap in swaps: print(swap) for s in swap: if s in e_pred.keys(): continue atoms = attach_calculator(setting, atoms=all_atoms[s], eci=eci) atoms.numbers = all_atoms[s].numbers e_pred[s] = atoms.get_potential_energy() for k, v in e_pred.items(): print(k, v, e_ref[k]) dev = extractor.swap_energy_deviations(swaps, e_pred, e_ref) extractor.plot_swap_deviation(dev) plt.show()
def show_fit(): data = np.loadtxt('data/almgsiX_data.csv', delimiter=',') energy = data[:, -1] energy -= np.mean(energy) X = data[:, :-1] singlets = data[:, 1:4] settings = settingFromJSON('almgsixSettings.json') concs = singlets2conc(settings.basis_functions, singlets) coeff_all = np.loadtxt("data/coeff_all.txt") coeff_alrich = np.loadtxt("data/coeff_alrich.txt") coeff_al_low = np.loadtxt("data/coeff_al_low.txt") c_al = np.array([v['Al'] for v in concs]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) pred_all = X.dot(coeff_all) pred_alrich = X.dot(coeff_alrich) pred_al_low = X.dot(coeff_al_low) weights = interpolator(c_al) pred_sum = pred_alrich*weights + pred_al_low*(1.0 - weights) print(energy - pred_all) ax.plot(c_al, pred_all - energy, 'o', mfc='none') ax.plot(c_al, pred_alrich - energy, 'x') ax.plot(c_al, pred_al_low - energy, 'v', mfc='none') ax.plot(c_al, pred_sum - energy, '^', mfc='none') plt.show()
def get_gs(): atoms = bulk('Al', cubic=True) * (5, 1, 1) setting = settingFromJSON(FNAME) with open(ECI_FILE, 'r') as infile: eci = json.load(infile) atoms = attach_calculator(setting=setting, atoms=atoms, eci=eci) setting.set_active_template(atoms=atoms) cluster = setting.cluster_list.get_by_name("c2_d0000_0")[0] cnst = PairConstraint(['X', 'X'], cluster, setting.trans_matrix, atoms) symbols = ['Al', 'Mg', 'Si', 'X'] numX = 0 removed = False for atom in atoms: new_symb = choice(symbols) atom.symbol = new_symb if new_symb == 'X': numX += 1 if numX == 1 and not removed: symbols.remove('X') removed = True T = [1000, 800, 600, 400, 200, 100, 50] for temp in T: mc = Montecarlo(atoms, temp) mc.add_constraint(cnst) mc.run(steps=10 * len(atoms)) return atoms
def fit_eci(): data = np.loadtxt('data/almgsiX_data.csv', delimiter=',') energy = data[:, -1] X = data[:, :-1] #energy -= np.mean(energy) indices = list(range(X.shape[0])) shuffle(indices) N = int(0.8*len(indices)) train = indices[:N] test = indices[N:] X_test = X[test, :] energy_test = energy[test] # Remove test data X = X[train, :] energy = energy[train] singlets = data[:, 1:4] settings = settingFromJSON('almgsixSettings.json') concs = singlets2conc(settings.basis_functions, singlets) conc_array = np.array([[v for v in x.values()] for x in concs]) indicators = conc_array[:, :-1] coeffs = [] weights = [] for types in product(range(0, 2), repeat=3): multiplicator = np.ones(data.shape[0]) for i, t in enumerate(types): if t == 0: multiplicator *= interpolator(indicators[:, i]) else: multiplicator *= (1.0-interpolator(indicators[:, i])) mdata = np.zeros_like(X) for i in range(X.shape[1]): mdata[:, i] = X[:, i]*multiplicator[train] model = LassoCV(eps=1e-6, cv=5) #model = RidgeCV(alphas=np.logspace(-12, -5, 50)) energy_fit = energy*multiplicator[train] model.fit(mdata, energy_fit) coeffs.append(model.coef_) weights.append(multiplicator) pred = np.zeros_like(energy) pred_test = np.zeros_like(energy_test) print(X_test.shape) for i in range(len(coeffs)): pred += X.dot(coeffs[i])*weights[i][train] pred_test += X_test.dot(coeffs[i])*weights[i][test] rmse = np.sqrt(np.mean((energy - pred)**2)) rmse_test = np.sqrt(np.mean((energy_test - pred_test)**2)) print("MSE: {} meV/atom".format(rmse*1000.0)) print("MSE test: {} meV/atom".format(rmse_test*1000.0))
def structure_impact(): eci_file = 'data/almgsiX_eci.json' with open(eci_file, 'r') as infile: eci = json.load(infile) print(eci) settings = settingFromJSON('almgsixSettings.json') print(settings.basis_functions) dE = 0.011 # 11 meV/atom ecis = np.array([[v for k, v in eci.items() if k.startswith(('c1', 'c2', 'c3', 'c4'))]]) x, _, _, _ = np.linalg.lstsq(ecis, [dE]) print(np.sum(np.abs(x))) print(dE/np.max(np.abs(ecis))) print(dE/np.median(np.abs(ecis)))
def explore(): eci_file = 'data/almgsiX_eci.json' with open(eci_file, 'r') as infile: eci = json.load(infile) settings = settingFromJSON('almgsixSettings.json') atoms = settings.atoms*(5, 5, 5) atoms = attach_calculator(setting=settings, atoms=atoms, eci=eci) num_structs = 10*len(atoms) energies = [] symbs = ['Al', 'Mg', 'Si', 'X'] now = time.time() for i in range(num_structs): if time.time() - now > 30: print("{} of {}".format(i, num_structs)) now = time.time() idx = np.random.randint(0, high=len(atoms)) symb = choice(symbs) atoms[idx].symbol = symb energies.append(atoms.get_potential_energy()/len(atoms)) np.savetxt("data/energies.txt", energies)
from clease import settingFromJSON, CorrFunction from ase.db import connect from clease.basis_function import BinaryLinear setting = settingFromJSON("almgsixSettings3.json") db = connect(setting.db_name) db.delete_external_table('polynomial_cf') db.delete_external_table('binary_linear_cf') bf = BinaryLinear(setting.unique_elements, reduntant_element='X') print(setting.unique_elements) print(bf.unique_elements) setting.basis_func_type = bf cf = CorrFunction(setting) cf.reconfigure_db_entries()
def main(): setting = settingFromJSON("almgsixSettings.json") setting.db_name = 'almgsiX_dft.db' new_struct = NewStructures(setting) new_struct.insert_structures(traj_init="data/restricted_gs_5x1x1.traj")