def test_none(): label = 'noforce_test' if not os.path.exists(label): os.mkdir(label) print('Generating data.') images = generate_data(10) print('Training none-neural network.') calc = Amp(descriptor=None, label=os.path.join(label, 'none'), regression=NeuralNetwork(hiddenlayers=(5, 5))) calc.train(images, force_goal=None, global_search=None)
def test(): images = make_images() for fortran in [False, True]: calc = Amp( descriptor=Gaussian( cutoff=cutoff, Gs=Gs, ), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation=activation, ), fingerprints_range=fingerprints_range, fortran=fortran, ) if fortran is False: reference_energies = [ calc.get_potential_energy(image) for image in images ] else: predicted_energies = [ calc.get_potential_energy(image) for image in images ] for image_no in range(len(predicted_energies)): assert (abs(predicted_energies[image_no] - reference_energies[image_no]) < 10.**(-5.)), \ 'Calculated energy value of image %i by \ fortran version is not consistent with the \ value of python version.' % (image_no + 1) if fortran is False: reference_forces = [calc.get_forces(image) for image in images] else: predicted_forces = [calc.get_forces(image) for image in images] for image_no in range(len(predicted_forces)): for index in range(np.shape(predicted_forces[image_no])[0]): for k in range(np.shape(predicted_forces[image_no])[1]): assert (abs(predicted_forces[image_no][index][k] - reference_forces[image_no][index][k]) < 10.**(-5.)), \ 'Calculated %i force of atom %i of \ image %i by fortran version is not \ consistent with the value of python \ version.' % (k, index, image_no + 1)
def test_none(): label = 'force_test' if not os.path.exists(label): os.mkdir(label) print('Generating data.') all_images = generate_data(4) train_images, test_images = randomize_images(all_images) print('Training none-neural network.') calc1 = Amp(descriptor=None, label=os.path.join(label, 'none'), regression=NeuralNetwork(hiddenlayers=(5, 5))) calc1.train(train_images, energy_goal=0.01, force_goal=0.05, global_search=None) print('Testing none-neural network.') energies1 = [] for image in all_images: energies1.append(calc1.get_potential_energy(atoms=image)) print('Verify making new calc works.') params = calc1.todict() calc2 = Amp(**params) energies2 = [] for image in all_images: energies2.append(calc2.get_potential_energy(atoms=image)) assert energies1 == energies2 print('Verifying can move an atom and get new energy.') image = all_images[0] image.set_calculator(calc2) e1 = image.get_potential_energy(apply_constraint=False) f1 = image.get_forces(apply_constraint=False) image[0].x += 0.5 # perturb e2 = image.get_potential_energy(apply_constraint=False) f2 = image.get_forces(apply_constraint=False) assert e1 != e2 assert not (f1 == f2).all()
def periodic_0th_bfgs_step_test(): ########################################################################### # Making the list of images images = [ Atoms(symbols='PdOPd', pbc=np.array([True, False, False], dtype=bool), cell=np.array([[2., 0., 0.], [0., 2., 0.], [0., 0., 2.]]), positions=np.array([[0.5, 1., 0.5], [1., 0.5, 1.], [1.5, 1.5, 1.5]])), Atoms(symbols='PdO', pbc=np.array([True, True, False], dtype=bool), cell=np.array([[2., 0., 0.], [0., 2., 0.], [0., 0., 2.]]), positions=np.array([[0.5, 1., 0.5], [1., 0.5, 1.]])), Atoms(symbols='Cu', pbc=np.array([True, True, False], dtype=bool), cell=np.array([[1.8, 0., 0.], [0., 1.8, 0.], [0., 0., 1.8]]), positions=np.array([[0., 0., 0.]])) ] for image in images: image.set_calculator(EMT()) image.get_potential_energy(apply_constraint=False) image.get_forces(apply_constraint=False) ########################################################################### # Parameters Gs = { 'O': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['O', 'Pd'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }], 'Pd': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.2 }, { 'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta': 0.9, 'gamma': 0.75, 'zeta': 1.5 }], 'Cu': [{ 'type': 'G2', 'element': 'Cu', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['Cu', 'Cu'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }] } hiddenlayers = {'O': (2), 'Pd': (2), 'Cu': (2)} weights = OrderedDict([ ('O', OrderedDict([(1, np.matrix([[-2.0, 6.0], [3.0, -3.0], [1.5, -0.9]])), (2, np.matrix([[5.5], [3.6], [1.4]]))])), ('Pd', OrderedDict([(1, np.matrix([[-1.0, 3.0], [2.0, 4.2], [1.0, -0.7]])), (2, np.matrix([[4.0], [0.5], [3.0]]))])), ('Cu', OrderedDict([(1, np.matrix([[0.0, 1.0], [-1.0, -2.0], [2.5, -1.9]])), (2, np.matrix([[0.5], [1.6], [-1.4]]))])) ]) scalings = OrderedDict([ ('O', OrderedDict([('intercept', -2.3), ('slope', 4.5)])), ('Pd', OrderedDict([('intercept', 1.6), ('slope', 2.5)])), ('Cu', OrderedDict([('intercept', -0.3), ('slope', -0.5)])) ]) ########################################################################### # Correct values correct_cost = 8004.292841472513 correct_energy_rmse = 43.736001940333836 correct_force_rmse = 137.4099476110887 correct_der_cost_fxn = [ 0.0814166874813534, 0.03231235582927526, 0.04388650395741291, 0.017417514465933048, 0.0284312765975806, 0.011283700608821421, 0.09416957265766414, -0.12322258890997816, 0.12679918754162384, 63.5396007548815, 0.016247700195771732, -86.62639558745185, -0.017777528287386473, 86.22415217678898, 0.017745913074805372, 104.58358033260711, -96.7328020983672, -99.09843648854351, -8.302880631971407, -1.2590007162073242, 8.3028773468822, 1.258759884181224, -8.302866610677315, -1.2563833805673688, 28.324298392677846, 28.09315509472324, -29.378744559315365, -11.247473567051799, 11.119951466671642, -87.08582317485761, -20.93948523898559, -125.73267675714658, -35.13852440758523 ] ########################################################################### # Testing pure-python and fortran versions of Gaussian-neural on different # number of processes for global_search in [None, 'SA']: for fortran in [False, True]: for extend_variables in [False, True]: for data_format in ['db', 'json']: for save_memory in [False]: for cores in range(1, 5): string = 'CuOPdbp/2/%s-%s-%s-%s-%s-%i' label = string % (global_search, fortran, extend_variables, data_format, save_memory, cores) if global_search is 'SA': gs = \ SimulatedAnnealing(temperature=10, steps=5) elif global_search is None: gs = None print label calc = Amp(descriptor=Gaussian(cutoff=4., Gs=Gs), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='tanh', ), fortran=fortran, label=label) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10, force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) assert (abs(calc.cost_function - correct_cost) < 10.**(-7.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - correct_energy_rmse) < 10.**(-10.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - correct_force_rmse) < 10 ** (-8.)), \ 'The calculated value of force RMSE is wrong!' for _ in range(len(correct_der_cost_fxn)): assert(abs(calc.der_variables_cost_function[ _] - correct_der_cost_fxn[_]) < 10 ** (-8)), \ 'The calculated value of cost function \ derivative is wrong!' dblabel = label secondlabel = '_' + label calc = Amp(descriptor=Gaussian(cutoff=4., Gs=Gs), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='tanh', ), fortran=fortran, label=secondlabel, dblabel=dblabel) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10, force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) assert (abs(calc.cost_function - correct_cost) < 10.**(-7.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - correct_energy_rmse) < 10.**(-10.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - correct_force_rmse) < 10 ** (-8.)), \ 'The calculated value of force RMSE is wrong!' for _ in range(len(correct_der_cost_fxn)): assert(abs(calc.der_variables_cost_function[ _] - correct_der_cost_fxn[_] < 10 ** (-8))), \ 'The calculated value of cost function \
def non_periodic_0th_bfgs_step_test(): pwd = os.getcwd() os.mkdir(os.path.join(pwd, 'CuOPdbp')) os.mkdir(os.path.join(pwd, '_CuOPdbp')) os.mkdir(os.path.join(pwd, 'CuOPdbp/0')) os.mkdir(os.path.join(pwd, '_CuOPdbp/0')) os.mkdir(os.path.join(pwd, 'CuOPdbp/1')) os.mkdir(os.path.join(pwd, '_CuOPdbp/1')) os.mkdir(os.path.join(pwd, 'CuOPdbp/2')) os.mkdir(os.path.join(pwd, '_CuOPdbp/2')) ########################################################################### # Making the list of periodic image images = [ Atoms(symbols='PdOPd2', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[0., 0., 0.], [0., 2., 0.], [0., 0., 3.], [1., 0., 0.]])), Atoms(symbols='PdOPd2', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[0., 1., 0.], [1., 2., 1.], [-1., 1., 2.], [1., 3., 2.]])), Atoms(symbols='PdO', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[2., 1., -1.], [1., 2., 1.]])), Atoms(symbols='Pd2O', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[-2., -1., -1.], [1., 2., 1.], [3., 4., 4.]])), Atoms(symbols='Cu', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[0., 0., 0.]])) ] for image in images: image.set_calculator(EMT()) image.get_potential_energy(apply_constraint=False) image.get_forces(apply_constraint=False) ########################################################################### # Parameters Gs = { 'O': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta': 0.2, 'gamma': 0.3, 'zeta': 1 }, { 'type': 'G4', 'elements': ['O', 'Pd'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }], 'Pd': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.2 }, { 'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta': 0.9, 'gamma': 0.75, 'zeta': 1.5 }, { 'type': 'G4', 'elements': ['O', 'Pd'], 'eta': 0.4, 'gamma': 0.3, 'zeta': 4 }], 'Cu': [{ 'type': 'G2', 'element': 'Cu', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['Cu', 'O'], 'eta': 0.2, 'gamma': 0.3, 'zeta': 1 }, { 'type': 'G4', 'elements': ['Cu', 'Cu'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }] } hiddenlayers = {'O': (2), 'Pd': (2), 'Cu': (2)} weights = OrderedDict([ ('O', OrderedDict([(1, np.matrix([[-2.0, 6.0], [3.0, -3.0], [1.5, -0.9], [-2.5, -1.5]])), (2, np.matrix([[5.5], [3.6], [1.4]]))])), ('Pd', OrderedDict([(1, np.matrix([[-1.0, 3.0], [2.0, 4.2], [1.0, -0.7], [-3.0, 2.0]])), (2, np.matrix([[4.0], [0.5], [3.0]]))])), ('Cu', OrderedDict([(1, np.matrix([[0.0, 1.0], [-1.0, -2.0], [2.5, -1.9], [-3.5, 0.5]])), (2, np.matrix([[0.5], [1.6], [-1.4]]))])) ]) scalings = OrderedDict([ ('O', OrderedDict([('intercept', -2.3), ('slope', 4.5)])), ('Pd', OrderedDict([('intercept', 1.6), ('slope', 2.5)])), ('Cu', OrderedDict([('intercept', -0.3), ('slope', -0.5)])) ]) ########################################################################### # Correct values correct_cost = 7144.810783950215 correct_energy_rmse = 24.318837496017185 correct_force_rmse = 144.70282475062052 correct_der_cost_fxn = [ 0, 0, 0, 0, 0, 0, 0.01374139170953901, 0.36318423812749656, 0.028312691567496464, 0.6012336354445753, 0.9659002689921986, -1.2897770059416218, -0.5718960935176884, -2.6425667221503035, -1.1960399246712894, 0, 0, -2.7256379713943852, -0.9080181026559658, -0.7739948323247023, -0.2915789426043727, -2.05998290443513, -0.6156374289747903, -0.0060865174621348985, -0.8296785483640939, 0.0008092646748983969, 0.041613027034688874, 0.003426469079592851, -0.9578004568876517, -0.006281929608090211, -0.28835884773094056, -4.2457774110285245, -4.317412094174614, -8.02385959091948, -3.240512651984099, -27.289862194996896, -26.8177742762254, -82.45107056053345, -80.6816768350809 ] ########################################################################### # Testing pure-python and fortran versions of Gaussian-neural on different # number of processes for global_search in [None, 'SA']: for fortran in [False, True]: for extend_variables in [False, True]: for data_format in ['db', 'json']: for save_memory in [False]: for cores in range(1, 7): string = 'CuOPdbp/0/%s-%s-%s-%s-%s-%i' label = string % (global_search, fortran, extend_variables, data_format, save_memory, cores) if global_search is 'SA': gs = \ SimulatedAnnealing(temperature=10, steps=5) elif global_search is None: gs = None print label calc = Amp(descriptor=Gaussian(cutoff=6.5, Gs=Gs), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='sigmoid', ), fortran=fortran, label=label) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) assert (abs(calc.cost_function - correct_cost) < 10.**(-5.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - correct_energy_rmse) < 10.**(-10.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - correct_force_rmse) < 10 ** (-7)), \ 'The calculated value of force RMSE is wrong!' for _ in range(len(correct_der_cost_fxn)): assert(abs(calc.der_variables_cost_function[ _] - correct_der_cost_fxn[_]) < 10 ** (-9)), \ 'The calculated value of cost function \ derivative is wrong!' dblabel = label secondlabel = '_' + label calc = Amp(descriptor=Gaussian(cutoff=6.5, Gs=Gs), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='sigmoid', ), fortran=fortran, label=secondlabel, dblabel=dblabel) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) assert (abs(calc.cost_function - correct_cost) < 10.**(-5.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - correct_energy_rmse) < 10.**(-10.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - correct_force_rmse) < 10 ** (-7)), \ 'The calculated value of force RMSE is wrong!' for _ in range(len(correct_der_cost_fxn)): assert(abs(calc.der_variables_cost_function[ _] - correct_der_cost_fxn[_] < 10 ** (-9))), \ 'The calculated value of cost function \
def test(): pwd = os.getcwd() os.mkdir(os.path.join(pwd, 'CuOPdnone')) os.mkdir(os.path.join(pwd, '_CuOPdnone')) ########################################################################### # Parameters weights = OrderedDict([(1, np.array([[1., 2.5], [0., 1.5], [0., -1.5], [3., 9.], [1., -2.5], [2., 3.], [2., 2.5], [3., 0.], [-3.5, 1.], [5., 3.], [-2., 2.5], [-4., 4.], [0., 0.]])), (2, np.array([[1.], [2.], [0.]])), (3, np.array([[3.5], [0.]]))]) scalings = OrderedDict([('intercept', 3.), ('slope', 2.)]) images = generate_images() ########################################################################### # Testing pure-python and fortran versions of Gaussian-neural on different # number of processes for global_search in [None, 'SA']: for fortran in [False, True]: for extend_variables in [False, True]: for data_format in ['db', 'json']: for save_memory in [False]: for cores in range(1, 7): string = 'CuOPdnone/%s-%s-%s-%s-%s-%i' label = string % (global_search, fortran, extend_variables, data_format, save_memory, cores) if global_search is 'SA': gs = \ SimulatedAnnealing(temperature=10, steps=5) elif global_search is None: gs = None print label calc = Amp( descriptor=None, regression=NeuralNetwork( hiddenlayers=(2, 1), activation='tanh', weights=weights, scalings=scalings, ), fortran=fortran, label=label, ) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) # Check for consistency between the two models assert (abs(calc.cost_function - cost_function) < 10.**(-5.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - energy_rmse) < 10.**(-5.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - force_rmse) < 10 ** (-5)), \ 'The calculated value of force RMSE is wrong!' dblabel = label secondlabel = '_' + label calc = Amp(descriptor=None, regression=NeuralNetwork( hiddenlayers=(2, 1), activation='tanh', weights=weights, scalings=scalings, ), fortran=fortran, label=secondlabel, dblabel=dblabel) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) # Check for consistency between the two models assert (abs(calc.cost_function - cost_function) < 10.**(-5.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - energy_rmse) < 10.**(-5.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - force_rmse) < 10 ** (-5)), \ 'The calculated value of force RMSE is wrong!'
#!/usr/bin/env python from amp import Amp from amp.descriptor import Gaussian from amp.regression import NeuralNetwork from ase.db import connect from amp import SimulatedAnnealing db = connect('../../../database/master.db') images = [] for d in db.select('train_set=True'): atoms = db.get_atoms(d.id) del atoms.constraints images += [atoms] for n in [2, 3]: calc = Amp(label='./', dblabel='../../', descriptor=Gaussian(cutoff=6.5), regression=NeuralNetwork(hiddenlayers=(2, n))) calc.train(images=images, data_format='db', cores=4, energy_goal=1e-2, force_goal=1e-1, global_search=SimulatedAnnealing(temperature=70, steps=50), extend_variables=False)
def test(): ########################################################################### # Parameters atoms = Atoms(symbols='PdOPd2', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[0., 1., 0.], [1., 2., 1.], [-1., 1., 2.], [1., 3., 2.]])) ########################################################################### # Parameters Gs = { 'O': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta': 0.2, 'gamma': 0.3, 'zeta': 1 }, { 'type': 'G4', 'elements': ['O', 'Pd'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }], 'Pd': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.2 }, { 'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta': 0.9, 'gamma': 0.75, 'zeta': 1.5 }, { 'type': 'G4', 'elements': ['O', 'Pd'], 'eta': 0.4, 'gamma': 0.3, 'zeta': 4 }] } hiddenlayers = {'O': (2), 'Pd': (2)} weights = OrderedDict([ ('O', OrderedDict([(1, np.matrix([[-2.0, 6.0], [3.0, -3.0], [1.5, -0.9], [-2.5, -1.5]])), (2, np.matrix([[5.5], [3.6], [1.4]]))])), ('Pd', OrderedDict([(1, np.matrix([[-1.0, 3.0], [2.0, 4.2], [1.0, -0.7], [-3.0, 2.0]])), (2, np.matrix([[4.0], [0.5], [3.0]]))])) ]) scalings = OrderedDict([ ('O', OrderedDict([('intercept', -2.3), ('slope', 4.5)])), ('Pd', OrderedDict([('intercept', 1.6), ('slope', 2.5)])) ]) fingerprints_range = { "O": [[0.21396177208585404, 2.258090276328769], [0.0, 2.1579067008202975], [0.0, 0.0]], "Pd": [[0.0, 1.4751761770313006], [0.0, 0.697686078889583], [0.0, 0.37848964715610417]] } ########################################################################### calc = Amp( descriptor=Gaussian( cutoff=6.5, Gs=Gs, ), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, ), fingerprints_range=fingerprints_range, ) atoms.set_calculator(calc) e1 = atoms.get_potential_energy(apply_constraint=False) e2 = calc.get_potential_energy(atoms) f1 = atoms.get_forces(apply_constraint=False) atoms[0].x += 0.5 boolean = atoms.calc.calculation_required(atoms, properties=['energy']) e3 = atoms.get_potential_energy(apply_constraint=False) e4 = calc.get_potential_energy(atoms) f2 = atoms.get_forces(apply_constraint=False) assert (e1 == e2 and e3 == e4 and abs(e1 - e3) > 10.**(-3.) and (boolean is True) and (not (f1 == f2).all())), 'Displaced-atom test broken!'
def test(): ########################################################################### # Parameters weights = \ OrderedDict([(1, np.array([[0.14563579, 0.19176385], [-0.01991609, 0.35873379], [-0.27988951, 0.03490866], [0.19195185, 0.43116313], [0.41035737, 0.02617128], [-0.13235187, -0.23112657], [-0.29065111, 0.23865951], [0.05854897, 0.24249052], [0.13660673, 0.19288898], [0.31894165, -0.41831075], [-0.23522261, -0.24009372], [-0.14450575, -0.15275409], [0., 0.]])), (2, np.array([[-0.27415999], [0.28538579], [0.]])), (3, np.array([[0.32147131], [0.]]))]) scalings = OrderedDict([('intercept', 3.), ('slope', 2.)]) images = generate_images() ########################################################################### # Testing pure-python and fortran versions of CartesianNeural on different # number of processes for fortran in [False, True]: calc = Amp( descriptor=None, regression=NeuralNetwork(hiddenlayers=(2, 1), weights=weights, scalings=scalings, activation='tanh'), fortran=fortran, ) predicted_energies = [ calc.get_potential_energy(image) for image in images ] for image_no in range(len(predicted_energies)): assert (abs(predicted_energies[image_no] - correct_predicted_energies[image_no]) < 5 * 10.**(-10.)), \ 'The calculated energy of image %i is wrong!' % (image_no + 1) predicted_forces = [calc.get_forces(image) for image in images] for image_no in range(len(predicted_forces)): for index in range(np.shape(predicted_forces[image_no])[0]): for direction in range(3): assert (abs(predicted_forces[image_no][index][direction] - correct_predicted_forces[image_no][index] [direction]) < 5 * 10.**(-10.)), \ 'The calculated %i force of atom %i of image %i is' \ 'wrong!' % (direction, index, image_no + 1)
def non_periodic_test(): ########################################################################### # Making the list of non-periodic images images = [Atoms(symbols='PdOPd2', pbc=np.array([False, False, False], dtype=bool), cell=np.array( [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array( [[0., 0., 0.], [0., 2., 0.], [0., 0., 3.], [1., 0., 0.]])), Atoms(symbols='PdOPd2', pbc=np.array([False, False, False], dtype=bool), cell=np.array( [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array( [[0., 1., 0.], [1., 2., 1.], [-1., 1., 2.], [1., 3., 2.]])), Atoms(symbols='PdO', pbc=np.array([False, False, False], dtype=bool), cell=np.array( [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array( [[2., 1., -1.], [1., 2., 1.]])), Atoms(symbols='Pd2O', pbc=np.array([False, False, False], dtype=bool), cell=np.array( [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array( [[-2., -1., -1.], [1., 2., 1.], [3., 4., 4.]])), Atoms(symbols='Cu', pbc=np.array([False, False, False], dtype=bool), cell=np.array( [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array( [[0., 0., 0.]]))] ########################################################################### # Correct energies and forces correct_predicted_energies = [14.231186811226152, 14.327219917287948, 5.5742510565528285, 9.41456771216968, -0.5019297954597407] correct_predicted_forces = \ [[[-0.05095024246182649, -0.10709193432146558, -0.09734321482638622], [-0.044550772904033635, 0.2469763195486647, -0.07617425912869778], [-0.02352490951707703, -0.050782839419131864, 0.24409220250631508], [0.11902592488293715, -0.08910154580806727, -0.07057472855123109]], [[-0.024868720575099375, -0.07417891957113862, -0.12121240797223251], [0.060156158438252574, 0.017517013378773042, -0.020047135079325505], [-0.10901144291312388, -0.06671262448352767, 0.06581556263014315], [0.07372400504997068, 0.12337453067589325, 0.07544398042141486]], [[0.10151747265164626, -0.10151747265164626, -0.20303494530329252], [-0.10151747265164626, 0.10151747265164626, 0.20303494530329252]], [[-0.00031177673224312745, -0.00031177673224312745, -0.0002078511548287517], [0.004823209772264884, 0.004823209772264884, 0.006975000714861393], [-0.004511433040021756, -0.004511433040021756, -0.006767149560032641]], [[0.0, 0.0, 0.0]]] ########################################################################### # Parameters Gs = {'O': [{'type': 'G2', 'element': 'Pd', 'eta': 0.8}, {'type': 'G4', 'elements': [ 'Pd', 'Pd'], 'eta':0.2, 'gamma':0.3, 'zeta':1}, {'type': 'G4', 'elements': ['O', 'Pd'], 'eta':0.3, 'gamma':0.6, 'zeta':0.5}], 'Pd': [{'type': 'G2', 'element': 'Pd', 'eta': 0.2}, {'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta':0.9, 'gamma':0.75, 'zeta':1.5}, {'type': 'G4', 'elements': ['O', 'Pd'], 'eta':0.4, 'gamma':0.3, 'zeta':4}], 'Cu': [{'type': 'G2', 'element': 'Cu', 'eta': 0.8}, {'type': 'G4', 'elements': ['Cu', 'O'], 'eta':0.2, 'gamma':0.3, 'zeta':1}, {'type': 'G4', 'elements': ['Cu', 'Cu'], 'eta':0.3, 'gamma':0.6, 'zeta':0.5}]} hiddenlayers = {'O': (2), 'Pd': (2), 'Cu': (2)} weights = OrderedDict([('O', OrderedDict([(1, np.matrix([[-2.0, 6.0], [3.0, -3.0], [1.5, -0.9], [-2.5, -1.5]])), (2, np.matrix([[5.5], [3.6], [1.4]]))])), ('Pd', OrderedDict([(1, np.matrix([[-1.0, 3.0], [2.0, 4.2], [1.0, -0.7], [-3.0, 2.0]])), (2, np.matrix([[4.0], [0.5], [3.0]]))])), ('Cu', OrderedDict([(1, np.matrix([[0.0, 1.0], [-1.0, -2.0], [2.5, -1.9], [-3.5, 0.5]])), (2, np.matrix([[0.5], [1.6], [-1.4]]))]))]) scalings = OrderedDict([('O', OrderedDict([('intercept', -2.3), ('slope', 4.5)])), ('Pd', OrderedDict([('intercept', 1.6), ('slope', 2.5)])), ('Cu', OrderedDict([('intercept', -0.3), ('slope', -0.5)]))]) fingerprints_range = {"Cu": [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], "O": [[0.2139617720858539, 2.258090276328769], [0.0, 1.085656080548734], [0.0, 0.0]], "Pd": [[0.0, 1.4751761770313006], [0.0, 0.28464992134267897], [0.0, 0.20167521020630502]]} ########################################################################### # Testing pure-python and fortran versions of Gaussian-neural force call for fortran in [False, True]: calc = Amp(descriptor=Gaussian(cutoff=6.5, Gs=Gs), regression=NeuralNetwork(hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='sigmoid',), fingerprints_range=fingerprints_range, fortran=fortran) predicted_energies = [calc.get_potential_energy(image) for image in images] for image_no in range(len(predicted_energies)): assert (abs(predicted_energies[image_no] - correct_predicted_energies[image_no]) < 10.**(-10.)), \ 'The predicted energy of image %i is wrong!' % (image_no + 1) predicted_forces = [calc.get_forces(image) for image in images] for image_no in range(len(predicted_forces)): for index in range(np.shape(predicted_forces[image_no])[0]): for direction in range( np.shape(predicted_forces[image_no])[1]): assert (abs(predicted_forces[image_no][index][direction] - correct_predicted_forces[image_no][index] [direction]) < 10.**(-10.)), \ 'The predicted %i force of atom %i of image %i is' \ 'wrong!' % (direction, index, image_no + 1)
def periodic_test(): ########################################################################### # Making the list of periodic images images = [Atoms(symbols='PdOPd', pbc=np.array([True, False, False], dtype=bool), cell=np.array( [[2., 0., 0.], [0., 2., 0.], [0., 0., 2.]]), positions=np.array( [[0.5, 1., 0.5], [1., 0.5, 1.], [1.5, 1.5, 1.5]])), Atoms(symbols='PdO', pbc=np.array([True, True, False], dtype=bool), cell=np.array( [[2., 0., 0.], [0., 2., 0.], [0., 0., 2.]]), positions=np.array( [[0.5, 1., 0.5], [1., 0.5, 1.]])), Atoms(symbols='Cu', pbc=np.array([True, True, False], dtype=bool), cell=np.array( [[1.8, 0., 0.], [0., 1.8, 0.], [0., 0., 1.8]]), positions=np.array( [[0., 0., 0.]]))] ########################################################################### # Correct energies and forces correct_predicted_energies = [3.8560954326995978, 1.6120748520627273, 0.19433107801410093] correct_predicted_forces = \ [[[0.14747720528015523, -3.3010645563584973, 3.3008168318984463], [0.03333579762326405, 9.050780376599887, -0.42608278400777605], [-0.1808130029034193, -5.7497158202413905, -2.8747340478906698]], [[6.5035267996045045 * (10.**(-6.)), -6.503526799604495 * (10.**(-6.)), 0.00010834689201069249], [-6.5035267996045045 * (10.**(-6.)), 6.503526799604495 * (10.**(-6.)), -0.00010834689201069249]], [[0.0, 0.0, 0.0]]] ########################################################################### # Parameters Gs = {'O': [{'type': 'G2', 'element': 'Pd', 'eta': 0.8}, {'type': 'G4', 'elements': ['O', 'Pd'], 'eta':0.3, 'gamma':0.6, 'zeta':0.5}], 'Pd': [{'type': 'G2', 'element': 'Pd', 'eta': 0.2}, {'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta':0.9, 'gamma':0.75, 'zeta':1.5}], 'Cu': [{'type': 'G2', 'element': 'Cu', 'eta': 0.8}, {'type': 'G4', 'elements': ['Cu', 'Cu'], 'eta':0.3, 'gamma':0.6, 'zeta':0.5}]} hiddenlayers = {'O': (2), 'Pd': (2), 'Cu': (2)} weights = OrderedDict([('O', OrderedDict([(1, np.matrix([[-2.0, 6.0], [3.0, -3.0], [1.5, -0.9]])), (2, np.matrix([[5.5], [3.6], [1.4]]))])), ('Pd', OrderedDict([(1, np.matrix([[-1.0, 3.0], [2.0, 4.2], [1.0, -0.7]])), (2, np.matrix([[4.0], [0.5], [3.0]]))])), ('Cu', OrderedDict([(1, np.matrix([[0.0, 1.0], [-1.0, -2.0], [2.5, -1.9]])), (2, np.matrix([[0.5], [1.6], [-1.4]]))]))]) scalings = OrderedDict([('O', OrderedDict([('intercept', -2.3), ('slope', 4.5)])), ('Pd', OrderedDict([('intercept', 1.6), ('slope', 2.5)])), ('Cu', OrderedDict([('intercept', -0.3), ('slope', -0.5)]))]) fingerprints_range = {"Cu": [[2.8636310860653253, 2.8636310860653253], [1.5435994865298275, 1.5435994865298275]], "O": [[2.9409056366723028, 2.972494902604392], [1.9522542722823606, 4.0720361595017245]], "Pd": [[2.4629488092411096, 2.6160138774087125], [0.27127576524253594, 0.5898312261433813]]} ########################################################################### # Testing pure-python and fortran versions of Gaussian-neural force call for fortran in [False, True]: calc = Amp(descriptor=Gaussian(cutoff=4., Gs=Gs), regression=NeuralNetwork(hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='tanh',), fingerprints_range=fingerprints_range, fortran=fortran) predicted_energies = [calc.get_potential_energy(image) for image in images] for image_no in range(len(predicted_energies)): assert (abs(predicted_energies[image_no] - correct_predicted_energies[image_no]) < 10.**(-10.)), \ 'The predicted energy of image %i is wrong!' % (image_no + 1) predicted_forces = [calc.get_forces(image) for image in images] for image_no in range(len(predicted_forces)): for index in range(np.shape(predicted_forces[image_no])[0]): for direction in range( np.shape(predicted_forces[image_no])[1]): assert (abs(predicted_forces[image_no][index][direction] - correct_predicted_forces[image_no][index] [direction]) < 10.**(-10.)), \ 'The predicted %i force of atom %i of image %i is' \ 'wrong!' % (direction, index, image_no + 1)
def test(): images = make_training_images() for descriptor in [None, Gaussian()]: for global_search in [ None, SimulatedAnnealing(temperature=10, steps=5) ]: for data_format in ['json', 'db']: for save_memory in [ False, ]: for fortran in [False, True]: for cores in range(1, 4): print(descriptor, global_search, data_format, save_memory, fortran, cores) pwd = os.getcwd() testdir = 'read_write_test' os.mkdir(testdir) os.chdir(testdir) regression = NeuralNetwork(hiddenlayers=( 5, 5, )) calc = Amp( label='calc', descriptor=descriptor, regression=regression, fortran=fortran, ) # Should start with new variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) # Test that we cannot overwrite. (Strange code # here because we *want* it to raise an # exception...) try: calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) except IOError: pass else: raise RuntimeError( 'Code allowed to overwrite!') # Test that we can manually overwrite. # Should start with existing variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, overwrite=True, cores=cores, ) label = 'testdir' if not os.path.exists(label): os.mkdir(label) # New directory calculator. calc = Amp( label='testdir/calc', descriptor=descriptor, regression=regression, fortran=fortran, ) # Should start with new variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) # Open existing, save under new name. calc = Amp( load='calc', label='calc2', descriptor=descriptor, regression=regression, fortran=fortran, ) # Should start with existing variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) label = 'calc_new' if not os.path.exists(label): os.mkdir(label) # Change label and re-train calc.set_label('calc_new/calc') # Should start with existing variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) # Open existing without specifying new name. calc = Amp( load='calc', descriptor=descriptor, regression=regression, fortran=fortran, ) # Should start with existing variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) os.chdir(pwd) shutil.rmtree(testdir, ignore_errors=True) del calc, regression
def test(): pwd = os.getcwd() os.mkdir(os.path.join(pwd, 'consistnone')) images = generate_images() count = 0 for global_search in [None, 'SA']: for fortran in [False, True]: for extend_variables in [False, True]: for data_format in ['db', 'json']: for save_memory in [False]: for cores in range(1, 7): string = 'consistnone/%s-%s-%s-%s-%s-%i' label = string % (global_search, fortran, extend_variables, data_format, save_memory, cores) if global_search is 'SA': global_search = \ SimulatedAnnealing(temperature=10, steps=5) calc = Amp(descriptor=None, regression=NeuralNetwork( hiddenlayers=(2, 1), activation='tanh', weights=weights, scalings=scalings, ), fortran=fortran, label=label) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., cores=cores, data_format=data_format, save_memory=save_memory, global_search=global_search, extend_variables=extend_variables) if count == 0: reference_cost_function = calc.cost_function reference_energy_rmse = \ calc.energy_per_atom_rmse reference_force_rmse = calc.force_rmse ref_cost_fxn_variable_derivatives = \ calc.der_variables_cost_function else: assert (abs(calc.cost_function - reference_cost_function) < 10.**(-10.)), \ '''Cost function value for %r fortran, %r data format, %r save_memory, and %i cores is not consistent with the value of python version on single core.''' % (fortran, data_format, save_memory, cores) assert (abs(calc.energy_per_atom_rmse - reference_energy_rmse) < 10.**(-10.)), \ '''Energy rmse value for %r fortran, %r data format, %r save_memory, and %i cores is not consistent with the value of python version on single core.''' % (fortran, data_format, save_memory, cores) assert (abs(calc.force_rmse - reference_force_rmse) < 10.**(-10.)), \ '''Force rmse value for %r fortran, %r data format, %r save_memory, and %i cores is not consistent with the value of python version on single core.''' % (fortran, data_format, save_memory, cores) for _ in range( len(ref_cost_fxn_variable_derivatives)): assert (calc.der_variables_cost_function[_] - ref_cost_fxn_variable_derivatives[_] < 10.**(-10.)) '''Derivative of the cost function for %r fortran, %r data format, %r save_memory, and %i cores is not consistent with the value of python version on single core. ''' % (fortran, data_format, save_memory, cores) count = count + 1