def check_scatter_pdf(value): """ Check two processor, algorithm pairs against each other for PDF calculation :param value: :return: """ # set everything up atoms, exp = value[:2] scat = ElasticScatter(exp_dict=exp, verbose=True) proc1, alg1 = value[-1][0] proc2, alg2 = value[-1][1] # run algorithm 1 scat.set_processor(proc1, alg1) ans1 = scat.get_pdf(atoms) # run algorithm 2 scat.set_processor(proc2, alg2) ans2 = scat.get_pdf(atoms) # test stats_check(ans1, ans2, rtol, atol) assert_allclose(ans1, ans2, rtol=rtol, atol=atol) # make certain we did not give back the same pointer assert ans1 is not ans2
def check_scatter_consistancy(value): atoms, exp = value[0:2] proc, alg = value[-1] scat = ElasticScatter(exp_dict=exp, verbose=True) scat.set_processor(proc, alg) ans = scat.get_pdf(atoms) ans1 = scat.get_fq(atoms) print(len(ans1)) print(scat.get_scatter_vector().shape) ans2 = scat.get_sq(atoms) print(len(ans2)) ans3 = scat.get_iq(atoms)
def check_dynamics(value): """ Test classical dynamics simulation, symplectic dynamics are look the same forward as reversed Parameters ---------- value: list or tuple The values to use in the tests """ ideal_atoms, _ = value[0] ideal_atoms.set_velocities(np.zeros((len(ideal_atoms), 3))) if isinstance(value[1], str): s = ElasticScatter(verbose=True) target_data = None exp_func = None exp_grad = None if value[1] == 'PDF': target_data = s.get_pdf(ideal_atoms) exp_func = s.get_pdf exp_grad = s.get_grad_pdf elif value[1] == 'FQ': target_data = s.get_fq(ideal_atoms) exp_func = s.get_fq exp_grad = s.get_grad_fq calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential='rw', conv=30) else: calc = value[1] ideal_atoms.positions *= 1.02 ideal_atoms.set_calculator(calc) start_pe = ideal_atoms.get_potential_energy() e = value[2] traj = classical_dynamics(ideal_atoms, e, 5) pe_list = [] for atoms in traj: pe_list.append(atoms.get_potential_energy()) min_pe = np.min(pe_list) print(min_pe, start_pe, len(traj)) print(pe_list) if start_pe != 0.0: assert min_pe < start_pe
def check_n_forces(value): """ Test numerical vs analytical forces Parameters ---------- value: list or tuple The values to use in the tests """ rtol = 1e-6 atol = 6e-5 ideal_atoms = value[0] ideal_atoms.set_velocities(np.zeros((len(ideal_atoms), 3))) if isinstance(value[1], str): s = ElasticScatter(verbose=True) target_data = None exp_func = None exp_grad = None if value[1] == 'PDF': target_data = s.get_pdf(ideal_atoms) exp_func = s.get_pdf exp_grad = s.get_grad_pdf elif value[1] == 'FQ': target_data = s.get_fq(ideal_atoms) exp_func = s.get_fq exp_grad = s.get_grad_fq calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential='rw', conv=1) else: calc = value[1] ideal_atoms.positions *= 1.02 ideal_atoms.set_calculator(calc) ans1 = ideal_atoms.get_forces() ans2 = calc.calculate_numerical_forces(ideal_atoms, d=5e-5) stats_check(ans2, ans1, rtol=rtol, atol=atol )
def check_scatter_pdf(value): """ Smoke test for PDF :param value: :return: """ atoms, exp = value[0:2] proc, alg = value[-1] scat = ElasticScatter(exp_dict=exp, verbose=True) scat.set_processor(proc, alg) # Test a set of different sized ensembles ans = scat.get_pdf(atoms) # Check that Scatter gave back something assert ans is not None # Check that all the values are not zero assert np.any(ans) del atoms, exp, proc, alg, scat, ans return
def check_n_forces(value): """ Test numerical vs analytical forces Parameters ---------- value: list or tuple The values to use in the tests """ rtol = 1e-6 atol = 6e-5 ideal_atoms = value[0] ideal_atoms.set_velocities(np.zeros((len(ideal_atoms), 3))) if isinstance(value[1], str): s = ElasticScatter(verbose=True) target_data = None exp_func = None exp_grad = None if value[1] == 'PDF': target_data = s.get_pdf(ideal_atoms) exp_func = s.get_pdf exp_grad = s.get_grad_pdf elif value[1] == 'FQ': target_data = s.get_fq(ideal_atoms) exp_func = s.get_fq exp_grad = s.get_grad_fq calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential='rw', conv=1) else: calc = value[1] ideal_atoms.positions *= 1.02 ideal_atoms.set_calculator(calc) ans1 = ideal_atoms.get_forces() ans2 = calc.calculate_numerical_forces(ideal_atoms, d=5e-5) stats_check(ans2, ans1, rtol=rtol, atol=atol)
s = ElasticScatter({'qmin': 3., 'rmax': 20.}) displacement = atoms.get_positions() - atoms.get_center_of_mass() distance = np.sqrt(np.sum(displacement**2, axis=1)) print(np.max(distance)) norm_displacement = (displacement.T / distance).T adp_tensor = norm_displacement * .01 print(np.max(adp_tensor)) adp_tensor_target = adp_tensor.copy() target_atoms = atoms.copy() target_adps = ADP(atoms, adps=adp_tensor_target) target_atoms.info['adps'] = target_adps target_pdf = s.get_pdf(target_atoms) adp_calc = Calc1D(target_data=target_pdf, exp_grad_function=s.get_grad_adp_pdf, exp_function=s.get_pdf, conv=100) calc = Calc1D(target_data=target_pdf, exp_grad_function=null_func, exp_function=s.get_pdf, conv=100) starting_atoms = atoms.copy() starting_adps = ADP(atoms, adps=adp_tensor * np.random.random(adp_tensor.shape)) starting_adps.set_calculator(adp_calc)
def check_nuts(self, value): """ Test NUTS simulation Parameters ---------- value: list or tuple The values to use in the tests """ print(self.traj_file) ideal_atoms, _ = value[0] ideal_atoms.set_velocities(np.zeros((len(ideal_atoms), 3))) s = ElasticScatter(verbose=True) if value[1] == 'PDF': target_data = s.get_pdf(ideal_atoms) exp_func = s.get_pdf exp_grad = s.get_grad_pdf calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential='rw', conv=30) elif value[1] == 'FQ': target_data = s.get_pdf(ideal_atoms) exp_func = s.get_pdf exp_grad = s.get_grad_pdf calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential='rw', conv=30) else: calc = value[1] ideal_atoms.positions *= 1.02 ideal_atoms.set_calculator(calc) start_pe = ideal_atoms.get_potential_energy() if value[2]: traj_name = self.traj_file.name else: traj_name = None nuts = NUTSCanonicalEnsemble(ideal_atoms, escape_level=4, verbose=True, seed=seed, trajectory=traj_name) traj, metadata = nuts.run(5) print(traj[0].get_momenta()) pe_list = [] for atoms in traj: pe_list.append(atoms.get_potential_energy()) min_pe = np.min(pe_list) print(len(traj)) print(min_pe, start_pe) if start_pe != 0.0: if not min_pe < start_pe: view(traj) assert min_pe < start_pe self.traj_file.close() if value[2]: assert os.path.exists(self.traj_file.name) read_traj = TrajectoryReader(self.traj_file.name) print(len(traj), len(read_traj)) assert len(traj) == len(read_traj) for i, (atoms1, atoms2) in enumerate(zip(read_traj, traj)): for att in ['get_positions', 'get_potential_energy', 'get_forces', 'get_momenta']: print(i, att) assert_allclose(*[getattr(a, att)() for a in [atoms1, atoms2]]) del traj
from pyiid.calc.pdfcalc import PDFCalc from pyiid.utils import build_sphere_np import matplotlib.pyplot as plt from pprint import pprint import time from copy import deepcopy as dc from collections import OrderedDict import pickle import traceback from pyiid.experiments.elasticscatter import ElasticScatter exp = None scat = ElasticScatter() atoms = Atoms('Au4', [[0,0,0], [3,0,0], [0,3,0], [3,3,0]]) pdf = scat.get_pdf(atoms) type_list = [] time_list = [] benchmarks = [ ('CPU', 'flat'), ('Multi-GPU', 'flat') ] colors=['b', 'r'] sizes = range(10, 80, 5) print sizes for proc, alg in benchmarks: print proc, alg number_of_atoms = [] scat.set_processor(proc, alg) type_list.append((proc, alg))
from pyiid.experiments.elasticscatter import ElasticScatter from pyiid.calc.calc_1d import Calc1D from pyiid.sim.nuts_hmc import NUTSCanonicalEnsemble from ase.cluster import Octahedron import matplotlib.pyplot as plt from ase.visualize import view # Lets set up the atoms # We use the Atomic Simulation Environment to take care of our atoms # atoms = Atoms('Au4', [[0, 0, 0], [3, 0, 0], [0, 3, 0], [3, 3, 0]]) atoms = Octahedron('Au', 2) view(atoms) # we can view the atoms by importing ASE's gui scat = ElasticScatter() pdf = scat.get_pdf(atoms) r = scat.get_r() # Now lets dilate the atoms so that they don't match the pdf atoms2 = dc(atoms) atoms2.positions *= 1.05 pdf2 = scat.get_pdf(atoms2) r = scat.get_r() # Now we need to define the potential energy surface calc = Calc1D( target_data=pdf, # The target or experimental data exp_function=scat.get_pdf, # The function which takes in atoms and produces # data like the experiment exp_grad_function=scat.get_grad_pdf, # the function which produces the # gradient of the calculated data
def check_nuts(self, value): """ Test NUTS simulation Parameters ---------- value: list or tuple The values to use in the tests """ print(self.traj_file) ideal_atoms, _ = value[0] ideal_atoms.set_velocities(np.zeros((len(ideal_atoms), 3))) s = ElasticScatter(verbose=True) if value[1] == 'PDF': target_data = s.get_pdf(ideal_atoms) exp_func = s.get_pdf exp_grad = s.get_grad_pdf calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential='rw', conv=30) elif value[1] == 'FQ': target_data = s.get_pdf(ideal_atoms) exp_func = s.get_pdf exp_grad = s.get_grad_pdf calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential='rw', conv=30) else: calc = value[1] ideal_atoms.positions *= 1.02 ideal_atoms.set_calculator(calc) start_pe = ideal_atoms.get_potential_energy() if value[2]: traj_name = self.traj_file.name else: traj_name = None nuts = NUTSCanonicalEnsemble(ideal_atoms, escape_level=4, verbose=True, seed=seed, trajectory=traj_name) traj, metadata = nuts.run(5) print(traj[0].get_momenta()) pe_list = [] for atoms in traj: pe_list.append(atoms.get_potential_energy()) min_pe = np.min(pe_list) print(len(traj)) print(min_pe, start_pe) if start_pe != 0.0: if not min_pe < start_pe: view(traj) assert min_pe < start_pe self.traj_file.close() if value[2]: assert os.path.exists(self.traj_file.name) read_traj = TrajectoryReader(self.traj_file.name) print(len(traj), len(read_traj)) assert len(traj) == len(read_traj) for i, (atoms1, atoms2) in enumerate(zip(read_traj, traj)): for att in [ 'get_positions', 'get_potential_energy', 'get_forces', 'get_momenta' ]: print(i, att) assert_allclose( *[getattr(a, att)() for a in [atoms1, atoms2]]) del traj
displacement = atoms.get_positions() - atoms.get_center_of_mass() distance = np.sqrt(np.sum(displacement**2, axis=1)) print(np.max(distance)) norm_displacement = (displacement.T / distance).T adp_tensor = norm_displacement * .01 print(np.max(adp_tensor)) adp_tensor_target = adp_tensor.copy() adp_tensor_target[:, :1] = 0.0 target_atoms = atoms.copy() target_adps = ADP(atoms, adps=adp_tensor_target) target_atoms.info['adps'] = target_adps target_pdf = s.get_pdf(target_atoms) adp_calc = Calc1D(target_data=target_pdf, exp_grad_function=s.get_grad_adp_pdf, exp_function=s.get_pdf, conv=100) calc = Calc1D(target_data=target_pdf, exp_grad_function=null_func, exp_function=s.get_pdf, conv=100) starting_atoms = atoms.copy() starting_adps = ADP(atoms, adps=adp_tensor * adp_tensor_target * 1.5) starting_adps.set_calculator(adp_calc)