def check_scatter_fq(value): """ Smoke test for FQ Parameters ---------- value: list or tuple The values to use in the tests """ 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_fq(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_nrg(value): """ Check for PDF energy against known value :param value: :return: """ # setup atoms1, atoms2 = value[0] exp_dict = value[1] p, thresh = value[2] proc1, alg1 = value[3] scat = ElasticScatter(verbose=True) scat.update_experiment(exp_dict) scat.set_processor(proc1, alg1) if value[4] == 'FQ': exp_func = scat.get_fq exp_grad = scat.get_grad_fq elif value[4] == 'PDF': exp_func = scat.get_pdf exp_grad = scat.get_grad_pdf else: exp_func = None exp_grad = None target_data = exp_func(atoms1) calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential=p) atoms2.set_calculator(calc) ans = atoms2.get_potential_energy() assert ans >= thresh del atoms1, atoms2, proc1, alg1, p, thresh, scat, target_data, calc, ans
def check_del_atom(value): atoms, exp = value[0:2] proc, alg = value[-1] scat = ElasticScatter(exp_dict=exp, verbose=True) scat.set_processor(proc, alg) assert scat._check_wrap_atoms_state(atoms) == False # Test a set of different sized ensembles ans1 = scat.get_fq(atoms) assert scat._check_wrap_atoms_state(atoms) == True # Check that Scatter gave back something assert ans1 is not None assert np.any(ans1) atoms2 = dc(atoms) del atoms2[np.random.choice(len(atoms2))] assert scat._check_wrap_atoms_state(atoms2) == False ans2 = scat.get_fq(atoms2) assert scat._check_wrap_atoms_state(atoms2) == True # Check that Scatter gave back something assert ans2 is not None assert np.any(ans2) assert not np.allclose(ans1, ans2) # make certain we did not give back the same pointer assert ans1 is not ans2 # Check that all the values are not zero del atoms, exp, proc, alg, scat, ans1 return
def test_consistency(): outs = [[] for i in range(len(test_atoms))] s = ElasticScatter() for i, atoms in enumerate(test_data): fq = s.get_fq(atoms) outs[i % len(test_atoms)].append(fq) for j in range(len(test_atoms)): for a, b in permutations(outs[j], 2): stats_check(a, b, rtol=2e-7, atol=1e-7)
def finite_difference_grad(atoms, exp_dict): s = ElasticScatter(exp_dict, verbose=True) start_fq = s.get_fq(atoms) finite_difference_grad_fq = np.zeros((len(atoms), 3, len(start_fq))) for i in range(len(atoms)): for w in range(3): atoms2 = dc(atoms) atoms2[i].position[w] += dq fq2 = s.get_fq(atoms2) finite_difference_grad_fq[i, w, :] = (fq2 - start_fq) / dq return finite_difference_grad_fq
def start(value): atoms, exp = value[:2] e = ElasticScatter(exp) e._wrap_atoms(atoms) q = atoms.get_positions().astype(np.float32) if value[2] == 'fq': scatter_array = atoms.get_array('F(Q) scatter') else: scatter_array = atoms.get_array('PDF scatter') n, qmax_bin = scatter_array.shape k_max = n * (n - 1) / 2. return q, scatter_array, n, qmax_bin, k_max, 0
def start(value): atoms, exp = value[:2] e = ElasticScatter(exp) e._wrap_atoms(atoms) q = atoms.get_positions().astype(np.float32) if value[2] == 'fq': scatter_array = atoms.get_array('F(Q) scatter') else: scatter_array = atoms.get_array('PDF scatter') n, qmax_bin = scatter_array.shape k_max = int(n * (n - 1) / 2.) return q, scatter_array, n, qmax_bin, k_max, 0
def test_consistency2(): outs = [[] for i in range(len(test_atoms))] s = ElasticScatter(verbose=True) for i, atoms in enumerate(test_data): stru = convert_atoms_to_stru(atoms) srfit_calc = DebyePDFCalculator() srfit_calc.qmin = s.exp['qmin'] srfit_calc.qmax = s.exp['qmax'] srfit_calc.qstep = s.exp['qbin'] srfit_calc(stru) stats_check(s.get_scatter_vector(), srfit_calc.qgrid) fq = srfit_calc.fq outs[i % len(test_atoms)].append(fq) for j in range(len(test_atoms)): for a, b in permutations(outs[j], 2): stats_check(a, b)
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_scatter_sq(value): """ Check two processor, algorithm pairs against each other for SQ 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_sq(atoms) # run algorithm 2 scat.set_processor(proc2, alg2) ans2 = scat.get_sq(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_forces(value): """ Check for PDF forces against known value :param value: :return: """ # setup atoms1, atoms2 = value[0] exp_dict = value[1] p, thresh = value[2] proc1, alg1 = value[3] scat = ElasticScatter(verbose=True) scat.update_experiment(exp_dict) scat.set_processor(proc1, alg1) if value[4] == 'FQ': exp_func = scat.get_fq exp_grad = scat.get_grad_fq elif value[4] == 'PDF': exp_func = scat.get_pdf exp_grad = scat.get_grad_pdf else: exp_func = None exp_grad = None target_data = exp_func(atoms1) calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential=p) atoms2.set_calculator(calc) # print atoms2.get_potential_energy() forces = atoms2.get_forces() print(forces) com = atoms2.get_center_of_mass() for i in range(len(atoms2)): dist = atoms2[i].position - com stats_check(np.cross(dist, forces[i]), np.zeros(3), atol=1e-7) del dist del atoms1, atoms2, proc1, alg1, p, thresh, scat, target_data, calc, \ forces, com
def check_scatter_grad_pdf(value): """ Smoke test for grad 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_grad_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 )
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 test_fq_against_srfit(): # unpack the atoms and experiment atoms = local_test_atoms exp = None # get the pyIID F(Q) s = ElasticScatter(exp) # s.set_processor('CPU', 'nxn') # get the SrFit F(Q) stru = convert_atoms_to_stru(atoms) srfit_calc = DebyePDFCalculator() srfit_calc.qmin = s.exp['qmin'] srfit_calc.qmax = s.exp['qmax'] srfit_calc.qstep = s.exp['qbin'] srfit_calc(stru) stats_check(s.get_scatter_vector(), srfit_calc.qgrid) ans1 = s.get_fq(atoms) ans2 = srfit_calc.fq stats_check(ans1, ans2, rtol=1e-4, atol=5e-6) del srfit_calc stats_check(ans1, ans2, rtol=1e-4, atol=5e-6)
def check_nrg(value): """ Check two processor, algorithm pairs against each other for PDF energy Parameters ---------- value: list or tuple The values to use in the tests """ rtol = 4e-6 atol = 9e-6 # setup atoms1, atoms2 = value[0] exp_dict = value[1] p, thresh = value[2] proc1, alg1 = value[3][0] proc2, alg2 = value[3][1] scat = ElasticScatter(verbose=True) scat.update_experiment(exp_dict) scat.set_processor(proc1, alg1) if value[4] == 'FQ': exp_func = scat.get_fq exp_grad = scat.get_grad_fq elif value[4] == 'PDF': exp_func = scat.get_pdf exp_grad = scat.get_grad_pdf else: exp_func = None exp_grad = None target_data = exp_func(atoms1) calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential=p) atoms2.set_calculator(calc) ans1 = atoms2.get_potential_energy() scat.set_processor(proc2, alg2) calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential=p) atoms2.set_calculator(calc) ans2 = atoms2.get_potential_energy() stats_check(ans2, ans1, rtol, atol)
def check_forces(value): """ Check two processor, algorithm pairs against each other for PDF forces :param value: :return: """ # setup rtol = 1e-4 atol = 6e-5 atoms1, atoms2 = value[0] exp_dict = value[1] p, thresh = value[2] proc1, alg1 = value[3][0] proc2, alg2 = value[3][1] scat = ElasticScatter(verbose=True) scat.update_experiment(exp_dict) scat.set_processor(proc1, alg1) if value[4] == 'FQ': exp_func = scat.get_fq exp_grad = scat.get_grad_fq elif value[4] == 'PDF': exp_func = scat.get_pdf exp_grad = scat.get_grad_pdf else: exp_func = None exp_grad = None target_data = exp_func(atoms1) calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential=p) atoms2.set_calculator(calc) ans1 = atoms2.get_forces() scat.set_processor(proc2, alg2) calc = Calc1D(target_data=target_data, exp_function=exp_func, exp_grad_function=exp_grad, potential=p) atoms2.set_calculator(calc) ans2 = atoms2.get_forces() stats_check(ans2, ans1, rtol=rtol, atol=atol)
def omega_comparison(value): s = ElasticScatter(value[1]) if value[-1] == 'fq': qbin = s.exp['qbin'] else: qbin = s.pdf_qbin r1, r2, task = r_comparison(value) norm1, norm2, task = norm_comparison(value) q, scatter_array, n, qmax_bin, k_max, k_cov = task omega1 = np.zeros((n, n, qmax_bin), np.float32) nxn_omega(omega1, r1, qbin) omega2 = np.zeros((k_max, qmax_bin), np.float32) k_omega(omega2, r2, qbin) stats_check(omega1, symmetric_reshape(omega2)) assert_allclose(omega1, symmetric_reshape(omega2)) print(np.max(np.abs(omega1 - symmetric_reshape(omega2)))) return omega1, omega2, task
import numpy as np from ase.atoms import Atoms from pyiid.experiments.elasticscatter import ElasticScatter import matplotlib.pyplot as plt from matplotlib.colors import LogNorm import ase.io __author__ = 'christopher' # atoms = Atoms('Au4', [[0, 0, 0], [3, 0, 0], [0, 3, 0], [3, 3, 0]]) atoms = ase.io.read('/mnt/work-data/dev/IID_data/db_test/PDF_LAMMPS_587.traj') scat = ElasticScatter() scat.update_experiment({'qmax': 8, 'qmin': .5}) s = scat.get_scatter_vector() k = 100 i_max, j_max = k, k pixel_array = np.zeros((i_max, j_max)) for i in range(i_max): for j in range(j_max): pixel_array[i, j] = np.sqrt(i ** 2 + j ** 2) pixel_array /= np.max(pixel_array) pixel_array *= np.max(s) img = scat.get_2d_scatter(atoms, pixel_array) print img.shape # plt.imshow(pixel_array) print np.max(img), np.min(img)
from copy import deepcopy as dc from ase.visualize import view from itertools import product from scipy.signal import argrelmax from workflow_analysis.analysis.plot import plot_pdf, colors from pyiid.sim.dynamics import classical_dynamics from pyiid.calc.calc_1d import Calc1D from ase import Atoms def null_func(atoms): return np.zeros((len(atoms), 3)) atoms = Atoms(FaceCenteredCubic('Au', [[1, 0, 0], [1, 1, 0], [1, 1, 1]], (4, 6, 4))) atoms.center() 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)
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
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.wrappers.elasticscatter import wrap_atoms 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 = []
from ase import Atoms 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
from copy import deepcopy as dc from ase.visualize import view from itertools import product from scipy.signal import argrelmax from workflow_analysis.analysis.plot import plot_pdf, colors from pyiid.sim.dynamics import classical_dynamics from pyiid.calc.calc_1d import Calc1D from ase import Atoms def null_func(atoms): return np.zeros((len(atoms), 3)) atoms = FaceCenteredCubic('Au', [[1, 0, 0], [1, 1, 0], [1, 1, 1]], (4, 6, 4)) atoms.center() 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() adp_tensor_target[:, :1] = 0.0 target_atoms = atoms.copy() target_adps = ADP(atoms, adps=adp_tensor_target) target_atoms.info['adps'] = target_adps
import numpy as np from ase.atoms import Atoms from pyiid.experiments.elasticscatter import ElasticScatter import matplotlib.pyplot as plt from matplotlib.colors import LogNorm import ase.io __author__ = 'christopher' # atoms = Atoms('Au4', [[0, 0, 0], [3, 0, 0], [0, 3, 0], [3, 3, 0]]) atoms = ase.io.read('/mnt/work-data/dev/IID_data/db_test/PDF_LAMMPS_587.traj') scat = ElasticScatter() scat.update_experiment({'qmax': 8, 'qmin': .5}) s = scat.get_scatter_vector() k = 100 i_max, j_max = k, k pixel_array = np.zeros((i_max, j_max)) for i in range(i_max): for j in range(j_max): pixel_array[i, j] = np.sqrt(i**2 + j**2) pixel_array /= np.max(pixel_array) pixel_array *= np.max(s) img = scat.get_2d_scatter(atoms, pixel_array) print img.shape # plt.imshow(pixel_array) print np.max(img), np.min(img) plt.imshow(img, aspect='auto')
finite_difference_grad_fq = np.zeros((len(atoms), 3, len(start_fq))) for i in range(len(atoms)): for w in range(3): atoms2 = dc(atoms) atoms2[i].position[w] += dq fq2 = s.get_fq(atoms2) finite_difference_grad_fq[i, w, :] = (fq2 - start_fq) / dq return finite_difference_grad_fq if __name__ == '__main__': rt = 1e-5 at = 2e-2 import matplotlib.pyplot as plt # atoms = setup_atomic_square()[0] # atoms.adps = ADP(atoms, adps=np.random.random((len(atoms), 3))) atoms = Atoms('Au2', [[0, 0, 0], [3, 0, 0]]) exp = None s = ElasticScatter(exp) # s.set_processor('CPU', 'nxn') a = finite_difference_grad(atoms, exp) b = s.get_grad_fq(atoms) print(a[0, 0] / b[0, 0]) plt.plot(a[0, 0, :], label='fd') plt.plot(b[0, 0, :], label='analytical') plt.legend(loc='best') plt.show() # stats_check(a, b, rt, at) # assert_allclose(a, b, rtol=rt, atol=at)