def train_data(images, setup_only=False):
    label = 'nodeplot_test/calc'
    train_images = images

    calc = Amp(descriptor=Gaussian(),
               model=NeuralNetwork(hiddenlayers=(5, 5)),
               label=label,
               cores=1)
    loss = LossFunction(convergence={'energy_rmse': 0.02,
                                     'force_rmse': 0.02})
    calc.model.lossfunction = loss

    if not setup_only:
        calc.train(images=train_images, )
        for image in train_images:
            print "energy =", calc.get_potential_energy(image)
            print "forces =", calc.get_forces(image)
    else:
        images = hash_images(train_images)
        calc.descriptor.calculate_fingerprints(images=images,
                                               log=calc.log,
                                               cores=1,
                                               calculate_derivatives=False)
        calc.model.fit(trainingimages=images,
                       descriptor=calc.descriptor,
                       log=calc.log,
                       cores=1,
                       only_setup=True)
        return calc
def train_test():
    label = 'train_test/calc'
    train_images = generate_data(2)

    calc = Amp(descriptor=Gaussian(),
               model=NeuralNetwork(hiddenlayers=(3, 3)),
               label=label,
               cores=1)
    loss = LossFunction(convergence={'energy_rmse': 0.02,
                                     'force_rmse': 0.02})
    calc.model.lossfunction = loss

    calc.train(images=train_images,)
    for image in train_images:
        print "energy =", calc.get_potential_energy(image)
        print "forces =", calc.get_forces(image)
def test():

    images = make_images()

    for fortran in [False, True]:
        label = 'forcecall/%s' % fortran
        calc = Amp(descriptor=Gaussian(cutoff=cutoff,
                                       Gs=Gs,
                                       fortran=fortran,),
                   model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                       weights=weights,
                                       scalings=scalings,
                                       activation=activation,
                                       mode='atom-centered',
                                       fprange=fingerprints_range,
                                       fortran=fortran,),
                   label=label,)

        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)
Esempio n. 4
0
#!/usr/bin/env python
from amp import Amp
from amp.descriptor import *
from amp.regression import *

calc = Amp(label="./",
           descriptor=Behler(cutoff=6.0),
           regression=NeuralNetwork(hiddenlayers=(2, 3)))

calc.train("../train.db", # The training data
           cores=1,
           global_search=None, # not found the simulated annealing feature useful
           extend_variables=False) # feature does not work properly and will crash
Esempio n. 5
0
# coding=utf-8
'''
author: Yingchun Zhang
organization:Nanjing University
'''

from ase import io
from amp import Amp
from amp.descriptor.gaussian import Gaussian
from amp.model.neuralnetwork import NeuralNetwork
from amp.model import LossFunction

test_images = io.read('last_ten_percent_for_comparasion.traj', index=':')
test_energy = [image.get_total_energy() for image in test_images]
calc = Amp.load('sio2-checkpoint.amp', cores=8)  # 'sio2-checkpoint.amp'
nnp_energy = [calc.get_potential_energy(image) for image in test_images]
x = len(test_images)
testfile = open('test.dat', 'w')
for i in range(x):
    print('%15.8f %15.8f' % (test_energy[i], nnp_energy[i]), file=testfile)
testfile.close()

#dirName = r'D:\simulationBackup\amp\randomStructures/'
#initialStructure = '64w-equied.xyz'
#initialTrj = '2000-trj.xyz'
#x = io.read(dirName + initialStructure, format='xyz'
Esempio n. 6
0
def periodic_0th_bfgs_step_test():
    """Gaussian/Neural training periodic standard test.

    Compares results to that expected from separate mathematica
    calculations.
    """

    # 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
    if aseversion < 12:  # EMT values have changed from 3.12.0 version
        ref_loss = 8004.292841411172
        ref_energyloss = (43.7360019403031**2.) * 3
        ref_forceloss = (137.40994760947325**2.) * 3
        ref_dloss_dparameters = np.array([
            0.08141668748130322, 0.03231235582925534, 0.04388650395738586,
            0.017417514465922313, 0.028431276597563077, 0.011283700608814465,
            0.0941695726576061, -0.12322258890990219, 0.12679918754154568,
            63.53960075374332, 0.01624770019548904, -86.6263955859162,
            -0.01777752828707744, 86.22415217526024, 0.017745913074496918,
            104.58358033298292, -96.73280209888215, -99.09843648905876,
            -8.302880631972338, -1.2590007162074357, 8.302877346883133,
            1.25875988418134, -8.302866610678247, -1.2563833805675353,
            28.324298392680998, 28.093155094726413, -29.37874455931869,
            -11.247473567044866, 11.119951466664787, -87.08582317481387,
            -20.939485239182346, -125.73267675705365, -35.138524407482116
        ])
    else:
        ref_loss = 8004.287750978173
        ref_energyloss = (43.73598563177581**2.) * 3
        ref_forceloss = (137.409923023214**2.) * 3
        ref_dloss_dparameters = np.array([
            0.08141663280688925, 0.03231233413027478, 0.043886474485922956,
            0.01741750276939638, 0.02843125750487539, 0.011283693031378718,
            0.09416950941914284, -0.12322250616122936, 0.1267991023910503,
            63.53958764057119, 0.016247696749304368, -86.62637753054923,
            -0.01777752451341436, 86.22413420485914, 0.01774590930723711,
            104.58353326982777, -96.73275667196937, -99.09839026204304,
            -8.302877823431269, -1.2590002903842232, 8.302874538343092,
            1.2587594584335775, -8.302863802141216, -1.2563829555383859,
            28.32428881173613, 28.093145591893936, -29.37873462156934,
            -11.24746601393696, 11.11994399919284, -87.08579155328007,
            -20.93947792122797, -125.73262989900473, -35.13850819392253
        ])

    # Testing pure-python and fortran versions of Gaussian-neural on different
    # number of processes

    for fortran in [False, True]:
        for cores in range(1, 4):
            label = 'train-periodic/%s-%i' % (fortran, cores)
            print(label)
            calc = Amp(descriptor=Gaussian(
                cutoff=4.,
                Gs=Gs,
                fortran=fortran,
            ),
                       model=NeuralNetwork(
                           hiddenlayers=hiddenlayers,
                           weights=weights,
                           scalings=scalings,
                           activation='tanh',
                           regressor=regressor,
                           fortran=fortran,
                       ),
                       label=label,
                       dblabel=label,
                       cores=cores)

            lossfunction = LossFunction(convergence=convergence)
            calc.model.lossfunction = lossfunction
            calc.train(images=images, )
            diff = abs(calc.model.lossfunction.loss - ref_loss)
            print("diff at 414 =", diff)
            assert (diff < 10.**(-10.)), \
                'Calculated value of loss function is wrong!'
            diff = abs(calc.model.lossfunction.energy_loss - ref_energyloss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom RMSE is wrong!'
            diff = abs(calc.model.lossfunction.force_loss - ref_forceloss)
            assert (diff < 10 ** (-9.)), \
                'Calculated value of force RMSE is wrong!'

            for _ in range(len(ref_dloss_dparameters)):
                diff = abs(calc.model.lossfunction.dloss_dparameters[_] -
                           ref_dloss_dparameters[_])
                assert(diff < 10 ** (-10.)), \
                    'Calculated value of loss function derivative is wrong!'

            dblabel = label
            secondlabel = '_' + label

            calc = Amp(descriptor=Gaussian(cutoff=4., Gs=Gs, fortran=fortran),
                       model=NeuralNetwork(
                           hiddenlayers=hiddenlayers,
                           weights=weights,
                           scalings=scalings,
                           activation='tanh',
                           regressor=regressor,
                           fortran=fortran,
                       ),
                       label=secondlabel,
                       dblabel=dblabel,
                       cores=cores)

            lossfunction = LossFunction(convergence=convergence)
            calc.model.lossfunction = lossfunction
            calc.train(images=images, )
            diff = abs(calc.model.lossfunction.loss - ref_loss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of loss function is wrong!'
            diff = abs(calc.model.lossfunction.energy_loss - ref_energyloss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom RMSE is wrong!'
            diff = abs(calc.model.lossfunction.force_loss - ref_forceloss)
            assert (diff < 10 ** (-9.)), \
                'Calculated value of force RMSE is wrong!'

            for _ in range(len(ref_dloss_dparameters)):
                diff = abs(calc.model.lossfunction.dloss_dparameters[_] -
                           ref_dloss_dparameters[_])
                assert(diff < 10 ** (-10.)), \
                    'Calculated value of loss function derivative is wrong!'
Esempio n. 7
0
def test():

    pwd = os.getcwd()
    os.mkdir(os.path.join(pwd, 'rotation_test'))

    for descriptor in [Gaussian(), Bispectrum(jmax=2.), Zernike(nmax=5)]:

        pwd = os.getcwd()
        os.mkdir(os.path.join(pwd, 'rotation_test/before_rot'))
        os.mkdir(os.path.join(pwd, 'rotation_test/after_rot'))

        # Non-rotated atomic configuration
        atoms = Atoms([
            Atom('Pt', (0., 0., 0.)),
            Atom('Pt', (0., 0., 1.)),
            Atom('Pt', (0., 2., 1.))
        ])

        atoms.set_constraint(FixAtoms(indices=[0]))
        atoms.set_calculator(EMT())
        atoms.get_potential_energy()
        atoms.get_forces(apply_constraint=False)

        calc = Amp(descriptor=descriptor,
                   fortran=False,
                   label='rotation_test/before_rot/')

        calc.train(images=[atoms],
                   force_goal=None,
                   energy_goal=10.**10.,
                   extend_variables=False,
                   global_search=None,
                   data_format='json')

        ###########################################################################

        # Randomly Rotated (and translated) atomic configuration
        rot = [random.random(), random.random(), random.random()]
        for i in range(1, len(atoms)):
            (atoms[i].x, atoms[i].y,
             atoms[i].z) = rotate_atom(atoms[i].x, atoms[i].y, atoms[i].z,
                                       rot[0] * np.pi, rot[1] * np.pi,
                                       rot[2] * np.pi)
        disp = [random.random(), random.random(), random.random()]
        for atom in atoms:
            atom.x += disp[0]
            atom.y += disp[1]
            atom.z += disp[2]

        calc = Amp(descriptor=descriptor,
                   fortran=False,
                   label='rotation_test/after_rot/')

        calc.train(images=[atoms],
                   force_goal=None,
                   energy_goal=10.**10.,
                   extend_variables=False,
                   global_search=None,
                   data_format='json')

        ###########################################################################

        fp1 = paropen('rotation_test/before_rot/fingerprints.json', 'rb')
        nonrotated_data = json.load(fp1)

        fp2 = paropen('rotation_test/after_rot/fingerprints.json', 'rb')
        rotated_data = json.load(fp2)

        for hash1, hash2 in zip(nonrotated_data.keys(), rotated_data.keys()):
            for index in nonrotated_data[hash1]:
                for _ in range(len(nonrotated_data[hash1][index])):
                    assert abs(nonrotated_data[hash1][index][_] -
                               rotated_data[hash2][index][_]) < 10.**(-7.)

        shutil.rmtree('rotation_test/before_rot')
        shutil.rmtree('rotation_test/after_rot')
Esempio n. 8
0
# coding=utf-8
'''
author: Yingchun Zhang
organization:Nanjing University
'''

from ase import io
from amp import Amp
from amp.descriptor.gaussian import Gaussian
from amp.model.neuralnetwork import NeuralNetwork
from amp.model import LossFunction

test_images = io.read('64water-1.traj', index='100:200')
test_energy = [image.get_total_energy() for image in test_images]
calc = Amp.load('water-checkpoint.amp', cores=8)
nnp_energy = [calc.get_potential_energy(image) for image in test_images]
x = len(test_images)
testfile = open('test.dat', 'w')
for i in range(x):
    print('%15.8f %15.8f' % (test_energy[i], nnp_energy[i]), file=testfile)
testfile.close()

#dirName = r'D:\simulationBackup\amp\randomStructures/'
#initialStructure = '64w-equied.xyz'
#initialTrj = '2000-trj.xyz'
#x = io.read(dirName + initialStructure, format='xyz')
def test():
    images = generate_data(2)
    regressor = Regressor(optimizer='BFGS')

    calc = Amp(descriptor=Gaussian(),
               model=NeuralNetwork(hiddenlayers=(3, 3),
                                   regressor=regressor,),
               cores=1)

    step = 0
    for d in [None, 0.00001]:
        for fortran in [True, False]:
            for cores in [1, 2]:
                step += 1
                label = \
                    'numeric_analytic_test/analytic-%s-%i' % (fortran, cores) \
                    if d is None \
                    else 'numeric_analytic_test/numeric-%s-%i' \
                    % (fortran, cores)
                print(label)

                loss = LossFunction(convergence={'energy_rmse': 10 ** 10,
                                                 'force_rmse': 10 ** 10},
                                    d=d)
                calc.set_label(label)
                calc.dblabel = 'numeric_analytic_test/analytic-True-1'
                calc.model.lossfunction = loss
                calc.descriptor.fortran = fortran
                calc.model.fortran = fortran
                calc.cores = cores

                calc.train(images=images,)

                if step == 1:
                    ref_energies = []
                    ref_forces = []
                    for image in images:
                        ref_energies += [calc.get_potential_energy(image)]
                        ref_forces += [calc.get_forces(image)]
                        ref_dloss_dparameters = \
                            calc.model.lossfunction.dloss_dparameters
                else:
                    energies = []
                    forces = []
                    for image in images:
                        energies += [calc.get_potential_energy(image)]
                        forces += [calc.get_forces(image)]
                        dloss_dparameters = \
                            calc.model.lossfunction.dloss_dparameters

                    for image_no in range(2):

                        diff = abs(energies[image_no] - ref_energies[image_no])
                        assert (diff < 10.**(-13.)), \
                            'The calculated value of energy of image %i is ' \
                            'wrong!' % (image_no + 1)

                        for atom_no in range(6):
                            for i in range(3):
                                diff = abs(forces[image_no][atom_no][i] -
                                           ref_forces[image_no][atom_no][i])
                                assert (diff < 10.**(-10.)), \
                                    'The calculated %i force of atom %i of ' \
                                    'image %i is wrong!' \
                                    % (i, atom_no, image_no + 1)
                        # Checks analytical and numerical dloss_dparameters
                        for _ in range(len(ref_dloss_dparameters)):
                            diff = abs(dloss_dparameters[_] -
                                       ref_dloss_dparameters[_])
                            assert(diff < 10 ** (-10.)), \
                                'The calculated value of loss function ' \
                                'derivative is wrong!'
    # Checks analytical and numerical forces
    forces = []
    for image in images:
        image.set_calculator(calc)
        forces += [calc.calculate_numerical_forces(image, d=d)]
    for atom_no in range(6):
        for i in range(3):
            diff = abs(forces[image_no][atom_no][i] -
                       ref_forces[image_no][atom_no][i])
            assert (diff < 10.**(-9.)), \
                'The calculated %i force of atom %i of ' \
                'image %i is wrong!' % (i, atom_no, image_no + 1)
Esempio n. 10
0
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 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_energies = [3.8560954326995978, 1.6120748520627273,
                        0.19433107801410093]
    correct_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": np.array([[2.8636310860653253,
                                           2.8636310860653253],
                                          [1.5435994865298275,
                                           1.5435994865298275]]),
                          "O": np.array([[2.9409056366723028,
                                          2.972494902604392],
                                         [1.9522542722823606,
                                          4.0720361595017245]]),
                          "Pd": np.array([[2.4629488092411096,
                                           2.6160138774087125],
                                          [0.27127576524253594,
                                           0.5898312261433813]])}

    # Testing pure-python and fortran versions of Gaussian-neural force call
    for fortran in [False, True]:
        for cores in range(1, 4):
            label = 'call-periodic/%s-%i' % (fortran, cores)
            calc = Amp(descriptor=Gaussian(cutoff=4.,
                                           Gs=Gs,
                                           fortran=fortran),
                       model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                           weights=weights,
                                           scalings=scalings,
                                           activation='tanh',
                                           fprange=fingerprints_range,
                                           mode='atom-centered',
                                           fortran=fortran),
                       label=label,
                       dblabel=label,
                       cores=cores)

            predicted_energies = [calc.get_potential_energy(image) for image in
                                  images]

            for image_no in range(len(predicted_energies)):
                diff = abs(predicted_energies[image_no] -
                           correct_energies[image_no])
                assert (diff < 10.**(-14.)), \
                    '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]):
                        diff = abs(predicted_forces[image_no][index][
                            direction] -
                            correct_forces[image_no][index][direction])
                        assert (diff < 10.**(-11.)), \
                            'The predicted %i force of atom %i of image' \
                            ' %i is wrong!' % (direction,
                                               index,
                                               image_no + 1)
Esempio n. 12
0
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
Esempio n. 13
0
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
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_energies = [14.231186811226152, 14.327219917287948,
                        5.5742510565528285, 9.41456771216968,
                        -0.5019297954597407]
    correct_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": np.array([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]),
                          "O": np.array([[0.2139617720858539,
                                          2.258090276328769],
                                         [0.0, 1.085656080548734],
                                         [0.0, 0.0]]),
                          "Pd": np.array([[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]:
        for cores in range(1, 6):
            label = 'call-nonperiodic/%s-%i' % (fortran, cores)
            calc = Amp(descriptor=Gaussian(cutoff=6.5,
                                           Gs=Gs,
                                           fortran=fortran),
                       model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                           weights=weights,
                                           scalings=scalings,
                                           activation='sigmoid',
                                           fprange=fingerprints_range,
                                           mode='atom-centered',
                                           fortran=fortran),
                       label=label,
                       dblabel=label,
                       cores=cores)

            predicted_energies = [calc.get_potential_energy(image) for image in
                                  images]

            for image_no in range(len(predicted_energies)):
                diff = abs(predicted_energies[image_no] -
                           correct_energies[image_no])
                assert (diff < 10.**(-15.)), \
                    '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]):
                        diff = abs(predicted_forces[image_no][index][
                            direction] -
                            correct_forces[image_no][index][direction])
                        assert (diff < 10.**(-15.)), \
                            'The predicted %i force of atom %i of image %i ' \
                            'is wrong!' % (direction, index, image_no + 1)
Esempio n. 15
0
{% if overwrite %}
overwrite = {{ overwrite }}
{% else %}
overwrite = False
{% endif %}
cores = {{ cores }}

# Set working directory
os.chdir(work_dir)

# Create DB update strings
update_str = "_".join([str(f) for f in framework])
update_key = "nn_" + update_str + "_iter{}".format(iteration)

# Load Amp object
calc = Amp.load(load_path, cores=cores)

# Get atoms from the database
images = []
new_db = os.path.join(os.path.dirname(load_path), "DB.db")
shutil.copyfile(db_path, new_db)
db = connect(new_db)
for d in db.select(selection):
    if not overwrite and update_key in d.key_value_pairs.keys():
        continue

    atoms = db.get_atoms(d.id)
    atoms.set_calculator(calc)
    energy = atoms.get_potential_energy()

    update = {update_key: energy}
Esempio n. 16
0
images = [atoms]
g2_etas = [0.005]
g2_rs_s = [0] * 4
g4_etas = [0.005]
g4_zetas = [1., 4.]
g4_gammas = [1., -1.]
cutoff = 4
make_amp_descriptors_simple_nn(images, g2_etas, g2_rs_s, g4_etas, g4_zetas,
                               g4_gammas, cutoff)

G = make_symmetry_functions(elements=elements, type='G2', etas=g2_etas)

#Add Rs parameter (0.0 for default) to comply with my version of AMP
#for g in G:
#        g['Rs'] =  0.0

G += make_symmetry_functions(elements=elements,
                             type='G4',
                             etas=g4_etas,
                             zetas=g4_zetas,
                             gammas=g4_gammas)

calc = Amp(descriptor=Gaussian(Gs=G, cutoff=4.),
           cores=ncores,
           model=NeuralNetwork(hiddenlayers=hiddenlayers))

calc.model.lossfunction = LossFunction(convergence=convergence,
                                       force_coefficient=0.001)

calc.train(images=images)
#import os
#from ase import Atoms, Atom, units
#import ase.io

from amp import Amp
from amp.descriptor.gaussian import Gaussian
from amp.model.neuralnetwork import NeuralNetwork
from amp.model import LossFunction

calc = Amp(descriptor=Gaussian(),
           model=NeuralNetwork(hiddenlayers=(10, 10, 10)))
calc.model.lossfunction = LossFunction(convergence={
    'energy_rmse': 0.02,
    'force_rmse': 0.03
})

calc.train(images='geoopt_LCAO.traj')
from amp import Amp
from amp.descriptor.gaussian import Gaussian
from amp.model.neuralnetwork import NeuralNetwork
from amp.model import LossFunction
from amp import analysis
import ase
from ase import io
import sys

calc = Amp(descriptor=Gaussian(), model=NeuralNetwork(), label="calc")
calc.model.lossfunction = LossFunction(convergence={
    'energy_rmse': 0.02,
    'force_rmse': 0.02
})
images = ase.io.read("water.extxyz", ":")
# images = ase.io.read("../datasets/COCu/COCu_pbc_500.traj", ":")
IMAGES = []
for i in range(100):
    IMAGES.append(images[i])
calc.train(IMAGES)
# analysis.plot_parity("calc.amp", IMAGES, overwrite=True)
Esempio n. 19
0
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():

    ###########################################################################
    # 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": np.array([[0.21396177208585404,
                                          2.258090276328769],
                                         [0.0, 2.1579067008202975],
                                         [0.0, 0.0]]),
                          "Pd": np.array([[0.0, 1.4751761770313006],
                                          [0.0, 0.697686078889583],
                                          [0.0, 0.37848964715610417]])}

    ###########################################################################

    calc = Amp(descriptor=Gaussian(cutoff=6.5,
                                   Gs=Gs,
                                   fortran=False,),
               model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                   weights=weights,
                                   scalings=scalings,
                                   fprange=fingerprints_range,
                                   mode='atom-centered'),
               cores=1)

    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!'
Esempio n. 21
0
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 non_periodic_0th_bfgs_step_test():

    # 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

    ref_loss = 7144.8107853579895
    ref_energyloss = (24.318837496016506 ** 2.) * 5
    ref_forceloss = (144.70282477494519 ** 2.) * 5
    ref_dloss_dparameters = np.array([0, 0, 0, 0, 0, 0,
                                      0.01374139170953901,
                                      0.36318423812749656,
                                      0.028312691567496464,
                                      0.6012336354445753,
                                      0.9659002689921986,
                                      -1.289777005924742,
                                      -0.5718960934643078,
                                      -2.642566722179569,
                                      -1.196039924610482, 0, 0,
                                      -2.72563797131018,
                                      -0.9080181024866707,
                                      -0.7739948323226851,
                                      -0.29157894253717415,
                                      -2.0599829042717404,
                                      -0.6156374289895887,
                                      -0.006086517460749253,
                                      -0.829678548408266,
                                      0.0008092646745710161,
                                      0.04161302703491613,
                                      0.0034264690790135606,
                                      -0.957800456897051,
                                      -0.006281929606579444,
                                      -0.2883588477371198,
                                      -4.245777410962108,
                                      -4.3174120941045535,
                                      -8.02385959091948,
                                      -3.240512651984099,
                                      -27.289862194988853,
                                      -26.8177742762544,
                                      -82.45107056051073,
                                      -80.68167683508715])
    ref_energy_maxresid = 54.21915548269209
    ref_force_maxresid = 791.6736436232306

    # Testing pure-python and fortran versions of Gaussian-neural on different
    # number of processes

    for fortran in [False, True]:
        for cores in range(1, 6):
            label = 'train-nonperiodic/%s-%i' % (fortran, cores)
            print label
            calc = Amp(descriptor=Gaussian(cutoff=6.5,
                                           Gs=Gs,
                                           fortran=fortran,),
                       model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                           weights=weights,
                                           scalings=scalings,
                                           activation='sigmoid',
                                           regressor=regressor,
                                           fortran=fortran,),
                       label=label,
                       dblabel=label,
                       cores=cores)

            lossfunction = LossFunction(convergence=convergence)
            calc.model.lossfunction = lossfunction
            calc.train(images=images,)
            diff = abs(calc.model.lossfunction.loss - ref_loss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of loss function is wrong!'
            diff = abs(calc.model.lossfunction.energy_loss - ref_energyloss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom RMSE is wrong!'
            diff = abs(calc.model.lossfunction.force_loss - ref_forceloss)
            assert (diff < 10 ** (-10.)), \
                'Calculated value of force RMSE is wrong!'
            diff = abs(calc.model.lossfunction.energy_maxresid -
                       ref_energy_maxresid)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom max residual is wrong!'
            diff = abs(calc.model.lossfunction.force_maxresid -
                       ref_force_maxresid)
            assert (diff < 10 ** (-10.)), \
                'Calculated value of force max residual is wrong!'

            for _ in range(len(ref_dloss_dparameters)):
                diff = abs(calc.model.lossfunction.dloss_dparameters[_] -
                           ref_dloss_dparameters[_])
                assert(diff < 10 ** (-12.)), \
                    "Calculated value of loss function derivative is wrong!"

            dblabel = label
            secondlabel = '_' + label

            calc = Amp(descriptor=Gaussian(cutoff=6.5,
                                           Gs=Gs,
                                           fortran=fortran,),
                       model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                           weights=weights,
                                           scalings=scalings,
                                           activation='sigmoid',
                                           regressor=regressor,
                                           fortran=fortran,),
                       label=secondlabel,
                       dblabel=dblabel,
                       cores=cores)

            lossfunction = LossFunction(convergence=convergence)
            calc.model.lossfunction = lossfunction
            calc.train(images=images,)
            diff = abs(calc.model.lossfunction.loss - ref_loss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of loss function is wrong!'
            diff = abs(calc.model.lossfunction.energy_loss - ref_energyloss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom RMSE is wrong!'
            diff = abs(calc.model.lossfunction.force_loss - ref_forceloss)
            assert (diff < 10 ** (-10.)), \
                'Calculated value of force RMSE is wrong!'
            diff = abs(calc.model.lossfunction.energy_maxresid -
                       ref_energy_maxresid)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom max residual is wrong!'
            diff = abs(calc.model.lossfunction.force_maxresid -
                       ref_force_maxresid)
            assert (diff < 10 ** (-10.)), \
                'Calculated value of force max residual is wrong!'

            for _ in range(len(ref_dloss_dparameters)):
                diff = abs(calc.model.lossfunction.dloss_dparameters[_] -
                           ref_dloss_dparameters[_])
                assert(diff < 10 ** (-12.)), \
                    'Calculated value of loss function derivative is wrong!'
Esempio n. 23
0
def amp_jobs(fdata, job, ndata, HL, E_conv):
    total_images = ase.io.read(fdata, index=':')
    images_c = Images(total_images)
    ### job == training
    if re.search("tr", job):
        if not ndata:
            images = images_c.total_images
            print("Start training using all the data %d" % len(images))
        else:
            images = images_c.get_training_images(ndata)
            print("data training:total sets %d/%d" %
                  (len(images), len(total_images)))
        exe_train_images(images, HL, E_conv)
    ### job == test
    elif re.search("te", job):
        amp_pes = "amp.amp"
        images = images_c.get_test_images(ndata)
        title, suptitle = get_title(job, fdata, HL, E_conv, len(total_images),
                                    len(images))
        print("data test:total sets %d/%d" % (len(images), len(total_images)))
        exe_test_images(job, images, amp_pes, title, suptitle)
    ### job == validation
    elif re.search("va", job):
        print("validation test")
        if not ndata:
            ndata = 5
            print("data images are diveded into %d sets" % ndata)
        ### training set scan for valicaiotn
        for i in range(ndata - 1):  # last one is kept for test
            ### training
            images, img_valid = images_c.get_val_train_images(ndata, i)
            print("num images: training {} validation {}".format(
                len(images), len(img_valid)))
            exe_train_images(images, HL, E_conv)
            ### validating
            amp_pes = "amp.amp"
            title, suptitle = get_title(job, fdata, HL, E_conv,
                                        len(total_images), len(images))
            #exe_test_images(job, img_valid, amp_pes, title, suptitle, val_id=i)
            ### Alternative Ways
            calc = Amp.load(amp_pes)
            y = []
            y_bar = []
            for mol in img_valid:
                y.append(mol.get_potential_energy())
                mol.set_calculator(calc)
                y_bar.append(mol.get_potential_energy())
            '''
            err = rmse(y, y_bar)
            print("in job {}-{}: validation error is {}".format(job,i,err)) 
            '''
            # check divided image sets: plot 2d here
            if False:
                x_draw = []
                y_draw = []
                for n, atoms in enumerate(images):
                    pot = atoms.get_potential_energy()
                    x_draw.append(n)
                    y_draw.append(pot)
                mplot_vector_two(x_draw,
                                 y_draw,
                                 Title="Extracted Training Set %d" % i,
                                 Xtitle="serial number",
                                 Ytitle="Epot")

    elif re.search('md', job):
        print("ndata is used for start geometry")
        if not ndata:
            atoms = ase.io.read(fdata, index='0')
        else:
            atoms = ase.io.read(fdata, index=ndata)
        run_md(atoms)
    return
Esempio n. 24
0
             {"type":"G2", "element":"Pd", "eta":400., "Rs":4.0},
             {"type":"G2", "element":"Pd", "eta":400., "Rs":5.0},
             {"type":"G2", "element":"Pd", "eta":400., "Rs":6.0},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":-1.0, "zeta":100.0, "theta_s":-1.0},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":-1.0, "zeta":100.0, "theta_s":-1.5},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":-1.0, "zeta":100.0, "theta_s":-1.75},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":-1.0, "zeta":100.0, "theta_s":-2.0},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":-1.0, "zeta":300.0, "theta_s":-2.15},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":-1.0, "zeta":300.0, "theta_s":-2.25},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":1.0, "zeta":160.0, "theta_s":0.0},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":1.0, "zeta":320.0, "theta_s":0.25},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":1.0, "zeta":640.0, "theta_s":0.35},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":1.0, "zeta":160.0, "theta_s":0.5},
            {"type":"G4", "elements":["Pd", "Pd"],"eta":0.005, "gamma":1.0, "zeta":160.0, "theta_s":0.75},
             ]}


calc = Amp(descriptor=Gaussian(Gs=Gs,
                               cutoff=Cosine(6.0)
                               ),
           cores=24,
           model=NeuralNetwork(hiddenlayers=(50,), activation='sigmoid',
                               maxTrainingEpochs=20000,
                               energy_coefficient=1.0,
                               force_coefficient=0.1,
                               optimizationMethod='l-BFGS-b',
                               convergenceCriteria={'energy_rmse': 0.05,
                                                    'force_rmse': 0.05}))
calc.train(images='train.traj',overwrite=True)

Esempio n. 25
0

elements = ['O', 'Ru']
Gs = make_symmetry_functions(elements)

filehandle = open('nodes', 'r')
linelist = filehandle.readlines()
filehandle.close()
cores = {}
for i in range(len(linelist)):
    node = linelist[i][0:5]
    cores[node] = 32

train_images = io.read(
    '/work/common/hxin_lab/jiamin/non_adiabatic/Langevin/Training_3nd/trajs/fingerprints/identified.traj',
    index=':')

print len(train_images)
#calc = Amp(descriptor=Gaussian(Gs=Gs, elements=['O','Ru']), model=NeuralNetwork(hiddenlayers=(70,70)), cores = 32)
convergence = {
    'energy_rmse': 0.001,
    'energy_maxresid': None,
    'force_rmse': 0.005,
    'force_maxresid': None
}
lossfunction = LossFunction(convergence=convergence)
calc = Amp.load('./amp-checkpoint.amp', cores=cores)
#calc=Amp.load('/work/common/hxin_lab/jiamin/non_adiabatic/Langevin/Training_2nd/sym70_2L70/amp-checkpoint.amp', cores=cores)
calc.model.lossfunction = lossfunction
calc.train(images=train_images, )
Esempio n. 26
0
def train_rnn(baseframe=100,
              tframe=8,
              total_step=10,
              traj='ethane.traj',
              convergence={
                  'energy_rmse': 0.25,
                  'force_rmse': 0.5
              },
              elements=['C', 'H', 'O'],
              hiddenlayers=(64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64),
              optim='ADAM',
              cores=4):
    """Gaussian/tflow train test."""
    p = ple()
    label = 'amp'
    all_images = Trajectory(traj)
    nimg, mean_e = get_mean_energy(all_images)

    G = make_symmetry_functions(elements=elements,
                                type='G2',
                                etas=np.logspace(np.log10(0.05),
                                                 np.log10(5.),
                                                 num=4))
    G += make_symmetry_functions(elements=elements,
                                 type='G5',
                                 etas=[0.005],
                                 zetas=[1., 4.],
                                 gammas=[+1., -1.])
    G = {element: G for element in elements}  # Gs=G

    if not isfile('amp.amp'):
        print('\nset up calculator ...\n')
        calc = Amp(descriptor=Gaussian(mode='atom-centered', Gs=G),
                   model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                       convergenceCriteria=convergence,
                                       activation='tanh',
                                       energy_coefficient=1.0,
                                       force_coefficient=None,
                                       optimizationMethod=optim,
                                       parameters={'energyMeanScale': mean_e},
                                       maxTrainingEpochs=100000),
                   label=label,
                   cores=cores)  # 'l-BFGS-b' or 'ADAM'
        trained_images = [all_images[j] for j in range(0, baseframe)]
        calc.train(overwrite=True, images=trained_images)
        del calc
    else:
        calc = Amp.load('amp.amp')
        calc.model.parameters['convergence'] = convergence
        calc.model.lossfunction = LossFunction(convergence=convergence)
        trained_images = [all_images[j] for j in range(0, baseframe)]
        calc.train(overwrite=True, images=trained_images)
        del calc

    tstep = int((nimg - baseframe) / tframe)
    if total_step > tstep:
        total_step = tstep
    print('Max train cycle of %d is allowed.' % total_step)

    edfts, eamps, eamps_ = [], [], []
    dolabel = True
    basestep = int(baseframe / tframe)

    for step in range(basestep, total_step + basestep):
        new_images = [
            all_images[j]
            for j in range(0 + step * tframe, tframe + step * tframe)
        ]
        trained_images.extend(new_images)

        x, edft, eamp, eamp_ = [], [], [], []
        ii = step * tframe

        # -----    test     -----
        calc1 = Amp.load('amp.amp')
        for i, image in enumerate(new_images):
            x.append(ii)
            eamp_.append(calc1.get_potential_energy(image))
            eamps_.append(eamp_[-1])
            edft.append(image.get_potential_energy())
            edfts.append(edft[-1])
            ii += 1
        del calc1

        # -----    train     -----
        calc = Amp.load('amp.amp')
        calc.model.lossfunction = LossFunction(convergence=convergence)
        # calc.model.convergenceCriteria=convergence
        calc.train(overwrite=True, images=trained_images)
        del calc

        # -----    test     -----
        calc2 = Amp.load('amp.amp')
        print('\n----   current training result   ---- \n')
        for i, image in enumerate(new_images):
            eamp.append(calc2.get_potential_energy(image))
            eamps.append(eamp[-1])
            print("energy(AMP) = %f energy(DFT) = %f" % (eamp[-1], edft[i]))
            # print("forces = %s" % str(calc2.get_forces(image)))
        del calc2

        plot_energies(edfts, eamps, eamp_=None)
        system('epstopdf energies.eps')

        p.scatter(x, edft, eamp, eamp_, dolabel=dolabel)
        if dolabel:
            dolabel = False

    p.plot()
    system('epstopdf energies_scatter.eps')
Esempio n. 27
0
RMSE_tr = np.sqrt(np.mean((y_train.ravel() - model.predict(x_train))**2))
RMSE_te = np.sqrt(np.mean((Y_test.ravel() - model.predict(X_test))**2))
np.savetxt('para_rawdata.txt', np.column_stack((RMSE_tr, RMSE_te)))

Temp_profile = np.loadtxt('/work/common/hxin_lab/jiamin/non_adiabatic/Langevin/2TempModel/F_100/tempfinal.txt')
MaxwellBoltzmannDistribution(atoms, 300 * units.kB) # initialize         

for i in range(1000):
    log = open('log.txt', 'a')    
    energy = []
    T_el = Temp_profile[i][2]

    start = time.time()
    for j in range(len(path)):
        calc_NN = Amp.load(path[j],cores=1)
        energy.append(calc_NN.get_potential_energy(atoms))    
    rmse = np.average(np.std(energy, axis=0))

    if rmse <0.03:
        atoms.set_calculator(Amp.load(path[0],cores=1))
        fp_Oxy = scaler_fp.transform(get_fingerprints(atoms))
        O_elec_den = scaler_chg.inverse_transform(model.predict(fp_Oxy))
        frict = get_frictions(atoms, O_elec_den)
        log.write("step %d, time %11.4f fs, rmse %11.4f eV, T_el %f, friction %f and %f, using NNs. " % (i, Temp_profile[i][0], rmse, T_el, frict[-2], frict[-1]))

    else:
        atoms.set_calculator(calc_espresso)
        atoms.get_potential_energy()

        bare_surf = atoms.copy()
Esempio n. 28
0
#!/usr/bin/env python
from amp import Amp
from amp.descriptor import Behler
from amp.regression import NeuralNetwork

calc = Amp(label="./",
           descriptor=Behler(cutoff=6.5),
           regression=NeuralNetwork(hiddenlayers=(2, 7)))

calc.train("../train.db",
           cores=16,
           energy_goal=0.0005,
           force_goal=None,
           extend_variables=False)
Esempio n. 29
0
    n_test = int(5e3)
    save_interval = 10
    legend = ["SW", "AMP"]

    energy_log = "energy-trained-log.txt"
    force_log = "force-trained-log.txt"
    energy_plot = system + "_" + "energy_log.png"
    force_plot = system + "_" + "force_log.png"
    plter = Plotter()
    plter.plot_trainlog(energy_log, energy_plot)
    plter.plot_trainlog(force_log, force_plot)

    trjbd = TrajectoryBuilder()
    calc = OpenKIMcalculator("SW_StillingerWeber_1985_Si__MO_405512056662_005")
    test_atoms = trjbd.build_atoms(system, size, temp, calc, seed=0)
    calc = Amp.load("calcs/force-trained.amp")
    amp_test_atoms = trjbd.build_atoms(system, size, temp, calc, seed=0)

    test_traj = "test.traj"
    steps, test_traj = trjbd.integrate_atoms(
        test_atoms, test_traj, n_test, save_interval, timestep=timestep, convert=True
    )

    amp_test_traj = "amp_test.traj"
    steps, amp_test_traj = trjbd.integrate_atoms(
        amp_test_atoms,
        amp_test_traj,
        n_test,
        save_interval,
        timestep=timestep,
        convert=True,
Esempio n. 30
0
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!'
Esempio n. 31
0
from mlutils.neb import accelerate_neb
from amp import Amp
from amp.descriptor.gaussian import Gaussian
from amp.model.neuralnetwork import NeuralNetwork
from amp.model import LossFunction

from ase.calculators.emt import EMT

initial = 'initial.traj'
final = 'final.traj'

Gs = None
n = 5
cutoff = 6.5
amp_calc = Amp(descriptor=Gaussian(cutoff=cutoff, fortran=True, Gs=Gs),
               model=NeuralNetwork(hiddenlayers=(n, n),
                                   fortran=True,
                                   checkpoints=None))

convergence = {'energy_rmse': 0.0001, 'force_rmse': 0.01}
amp_calc.model.lossfunction = LossFunction(convergence=convergence)

dft_calc = EMT()

neb = accelerate_neb(initial=initial,
                     final=final,
                     tolerance=0.05,
                     maxiter=200,
                     fmax=0.05,
                     ifmax=1.,
                     step=2.,
                     metric='fmax')
Esempio n. 32
0
def non_periodic_0th_bfgs_step_test():
    """Gaussian/Neural training non-periodic standard test.

    Compares results to that expected from separate mathematica
    calculations.
    """

    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
    if aseversion < 12:  # EMT values have changed from 3.12.0 version
        ref_loss = 7144.8107853579895
        ref_energyloss = (24.318837496016506**2.) * 5
        ref_forceloss = (144.70282477494519**2.) * 5
        ref_dloss_dparameters = np.array([
            0, 0, 0, 0, 0, 0, 0.01374139170953901, 0.36318423812749656,
            0.028312691567496464, 0.6012336354445753, 0.9659002689921986,
            -1.289777005924742, -0.5718960934643078, -2.642566722179569,
            -1.196039924610482, 0, 0, -2.72563797131018, -0.9080181024866707,
            -0.7739948323226851, -0.29157894253717415, -2.0599829042717404,
            -0.6156374289895887, -0.006086517460749253, -0.829678548408266,
            0.0008092646745710161, 0.04161302703491613, 0.0034264690790135606,
            -0.957800456897051, -0.006281929606579444, -0.2883588477371198,
            -4.245777410962108, -4.3174120941045535, -8.02385959091948,
            -3.240512651984099, -27.289862194988853, -26.8177742762544,
            -82.45107056051073, -80.68167683508715
        ])
        ref_energy_maxresid = 54.21915548269209
        ref_force_maxresid = 791.6736436232306
    else:
        ref_loss = 7144.807220773296
        ref_energyloss = (24.318829702548342**2.) * 5
        ref_forceloss = (144.70279593472887**2.) * 5
        ref_dloss_dparameters = np.array([
            0, 0, 0, 0, 0, 0, 0.01374139170953901, 0.36318423812749656,
            0.028312691567496464, 0.6012336354445753, 0.9659002689921986,
            -1.2897765357544038, -0.5718958286530584, -2.642565840915077,
            -1.1960394346870424, 0, 0, -2.7256370964673238,
            -0.9080177898160631, -0.7739945904033205, -0.29157882294526083,
            -2.0599825024556027, -0.6156371996742152, -0.006086514109432934,
            -0.8296782839032163, 0.0008092653341775424, 0.04161306816722683,
            0.0034264692325982156, -0.9578001030483714, -0.006281927374160914,
            -0.28835874344086, -4.245775886469167, -4.317410633818672,
            -8.02385959091948, -3.240512651984099, -27.289853042932705,
            -26.81776520493048, -82.45104200076496, -80.68164887277251
        ])
        ref_energy_maxresid = 54.21913802238612
        ref_force_maxresid = 791.6734866205463

    # Testing pure-python and fortran versions of Gaussian-neural on different
    # number of processes

    for fortran in [False, True]:
        for cores in range(1, 6):
            label = 'train-nonperiodic/%s-%i' % (fortran, cores)
            print(label)
            calc = Amp(descriptor=Gaussian(
                cutoff=6.5,
                Gs=Gs,
                fortran=fortran,
            ),
                       model=NeuralNetwork(
                           hiddenlayers=hiddenlayers,
                           weights=weights,
                           scalings=scalings,
                           activation='sigmoid',
                           regressor=regressor,
                           fortran=fortran,
                       ),
                       label=label,
                       dblabel=label,
                       cores=cores)

            lossfunction = LossFunction(convergence=convergence)
            calc.model.lossfunction = lossfunction
            calc.train(images=images, )
            diff = abs(calc.model.lossfunction.loss - ref_loss)
            print("diff at 204 =", diff)
            assert (diff < 10.**(-10.)), \
                'Calculated value of loss function is wrong!'
            diff = abs(calc.model.lossfunction.energy_loss - ref_energyloss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom RMSE is wrong!'
            diff = abs(calc.model.lossfunction.force_loss - ref_forceloss)
            assert (diff < 10 ** (-10.)), \
                'Calculated value of force RMSE is wrong!'
            diff = abs(calc.model.lossfunction.energy_maxresid -
                       ref_energy_maxresid)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom max residual is wrong!'
            diff = abs(calc.model.lossfunction.force_maxresid -
                       ref_force_maxresid)
            assert (diff < 10 ** (-10.)), \
                'Calculated value of force max residual is wrong!'

            for _ in range(len(ref_dloss_dparameters)):
                diff = abs(calc.model.lossfunction.dloss_dparameters[_] -
                           ref_dloss_dparameters[_])
                assert(diff < 10 ** (-12.)), \
                    "Calculated value of loss function derivative is wrong!"

            dblabel = label
            secondlabel = '_' + label

            calc = Amp(descriptor=Gaussian(
                cutoff=6.5,
                Gs=Gs,
                fortran=fortran,
            ),
                       model=NeuralNetwork(
                           hiddenlayers=hiddenlayers,
                           weights=weights,
                           scalings=scalings,
                           activation='sigmoid',
                           regressor=regressor,
                           fortran=fortran,
                       ),
                       label=secondlabel,
                       dblabel=dblabel,
                       cores=cores)

            lossfunction = LossFunction(convergence=convergence)
            calc.model.lossfunction = lossfunction
            calc.train(images=images, )
            diff = abs(calc.model.lossfunction.loss - ref_loss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of loss function is wrong!'
            diff = abs(calc.model.lossfunction.energy_loss - ref_energyloss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom RMSE is wrong!'
            diff = abs(calc.model.lossfunction.force_loss - ref_forceloss)
            assert (diff < 10 ** (-10.)), \
                'Calculated value of force RMSE is wrong!'
            diff = abs(calc.model.lossfunction.energy_maxresid -
                       ref_energy_maxresid)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom max residual is wrong!'
            diff = abs(calc.model.lossfunction.force_maxresid -
                       ref_force_maxresid)
            assert (diff < 10 ** (-10.)), \
                'Calculated value of force max residual is wrong!'

            for _ in range(len(ref_dloss_dparameters)):
                diff = abs(calc.model.lossfunction.dloss_dparameters[_] -
                           ref_dloss_dparameters[_])
                assert(diff < 10 ** (-12.)), \
                    'Calculated value of loss function derivative is wrong!'
Esempio n. 33
0
#conf = Trajectory('t4x2.traj', 'r')
#atoms = conf[0]
atoms = read('POSCAR-' + str(i), format='vasp')

#Set initial positions and velocities
#positions = np.loadtxt('pos1')
velocities_fs = np.loadtxt('POSCAR-' + str(i),
                           skiprows=54,
                           usecols=(0, 1, 2),
                           max_rows=44)
velocities = velocities_fs / units.fs
#atoms.set_positions(positions)
atoms.set_velocities(velocities)

#To use AMP calculator remove comment and change EMT() to calc
calc = Amp.load('735.amp', label='label' + str(i))
atoms.set_calculator(calc)

# We want to run MD with the Langevin algorithm
#time step of 1 fs, the temperatures T, electron densities file, constant lattice friction.
dyn = LangevinLaser(atoms, units.fs, T, lattice_friction, str(i))


def printenergy(a=atoms):  # store a reference to atoms in the definition.
    """Function to print the potential, kinetic and total energy."""
    epot = a.get_potential_energy() / len(a)
    ekin = a.get_kinetic_energy() / len(a)
    print('Energy per atom: Epot = %.3feV  Ekin = %.3feV (T=%3.0fK)  '
          'Etot = %.3feV' % (epot, ekin, ekin / (1.5 * units.kB), epot + ekin))

Esempio n. 34
0
def train_images(images, HL, E_conv):
    Hidden_Layer=tuple(HL)
    calc = Amp(descriptor=Gaussian(), model=NeuralNetwork(hiddenlayers=Hidden_Layer))
    calc.model.lossfunction = LossFunction(convergence={'energy_rmse': E_conv})
    #calc.model.lossfunction = LossFunction(force_coefficient=-0.1)
    calc.train(images=images, overwrite=True)
Esempio n. 35
0
def train_amp(baseframe=200,
              traj='ethane.traj',
              convergence={
                  'energy_rmse': 0.25,
                  'force_rmse': 0.5
              },
              elements=['C', 'H', 'O'],
              cores=4):
    """Gaussian/tflow train test."""
    p = ple()
    label = 'amp'
    all_images = Trajectory(traj)
    nimg, mean_e = get_mean_energy(all_images)

    G = make_symmetry_functions(elements=elements,
                                type='G2',
                                etas=np.logspace(np.log10(0.05),
                                                 np.log10(5.),
                                                 num=4))
    G += make_symmetry_functions(elements=elements,
                                 type='G5',
                                 etas=[0.005],
                                 zetas=[1., 4.],
                                 gammas=[+1., -1.])
    G = {element: G for element in elements}  # Gs=G

    if not isfile('amp.amp'):
        # print('\nset up calculator ...\n')
        calc = Amp(descriptor=Gaussian(mode='atom-centered', Gs=G),
                   model=NeuralNetwork(hiddenlayers=(1024, 1024, 1024, 512,
                                                     512, 256, 256, 256, 256,
                                                     128, 128),
                                       convergenceCriteria=convergence,
                                       activation='tanh',
                                       energy_coefficient=1.0,
                                       force_coefficient=None,
                                       optimizationMethod='ADAM',
                                       parameters={'energyMeanScale': mean_e},
                                       maxTrainingEpochs=100000),
                   label=label,
                   cores=cores)  # 'l-BFGS-b' or 'ADAM'
        trained_images = [all_images[j] for j in range(0, baseframe)]
        calc.train(overwrite=True, images=trained_images)
        del calc
    else:
        calc = Amp.load('amp.amp')
        calc.model.parameters['convergence'] = convergence
        calc.model.lossfunction = LossFunction(convergence=convergence)
        trained_images = [all_images[j] for j in range(0, baseframe)]
        calc.train(overwrite=True, images=trained_images)
        del calc

    edfts, eamps, eamps_ = [], [], []
    dolabel = True
    basestep = int(baseframe / tframe)

    system('epstopdf energies.eps')
    p.scatter(x, edft, eamp, eamp_, dolabel=dolabel)
    p.plot()

    plot_energies(edfts, eamps, eamp_=eamps_)
    system('epstopdf energies_scatter.eps')
Esempio n. 36
0
from ase import io
#from ase.calculators.emt import EMT
#from ase.build import fcc110
#from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
#from ase.md import VelocityVerlet

from amp import Amp
from amlt import super_cell_if_needed

import time

cut_off_radius = rcut = 6.5

#MLIP = Amp.load('Zr_O.amp')
MLIP = Amp.load('amp-checkpoint.amp')

struct_types = ['known', 'polymorphD3', 'random']

dyn_types = ['md', 'relax', 'sp']

from os import path, getcwd
basedir = getcwd()
from glob import glob

############
#from amlt import super_cell_if_needed

image_list = []
total = 0
time1 = time.time()
for struct_type in struct_types:
def test_calcs():
    """Gaussian/Neural non-periodic standard.

    Checks that the answer matches that expected from previous Mathematica
    calculations.
    """

    #: Making the list of non-periodic images
    images = [
        Atoms(
            symbols="PdOPd2",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[0.0, 0.0, 0.0], [0.0, 2.0, 0.0],
                                [0.0, 0.0, 3.0], [1.0, 0.0, 0.0]]),
        ),
        Atoms(
            symbols="PdOPd2",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[0.0, 1.0, 0.0], [1.0, 2.0, 1.0],
                                [-1.0, 1.0, 2.0], [1.0, 3.0, 2.0]]),
        ),
        Atoms(
            symbols="PdO",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[2.0, 1.0, -1.0], [1.0, 2.0, 1.0]]),
        ),
        Atoms(
            symbols="Pd2O",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[-2.0, -1.0, -1.0], [1.0, 2.0, 1.0],
                                [3.0, 4.0, 4.0]]),
        ),
        Atoms(
            symbols="Cu",
            pbc=np.array([False, False, False], dtype=bool),
            calculator=EMT(),
            cell=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
            positions=np.array([[0.0, 0.0, 0.0]]),
        ),
    ]

    # Parameters
    hiddenlayers = {"O": (2, ), "Pd": (2, ), "Cu": (2, )}

    Gs = {}
    Gs["G2_etas"] = [0.2]
    Gs["G2_rs_s"] = [0]
    Gs["G4_etas"] = [0.4]
    Gs["G4_zetas"] = [1]
    Gs["G4_gammas"] = [1]
    Gs["cutoff"] = 6.5

    elements = ["O", "Pd", "Cu"]

    G = make_symmetry_functions(elements=elements,
                                type="G2",
                                etas=Gs["G2_etas"])
    G += make_symmetry_functions(
        elements=elements,
        type="G4",
        etas=Gs["G4_etas"],
        zetas=Gs["G4_zetas"],
        gammas=Gs["G4_gammas"],
    )
    amp_images = amp_hash(images)
    descriptor = Gaussian(Gs=G, cutoff=Gs["cutoff"])
    descriptor.calculate_fingerprints(amp_images, calculate_derivatives=True)
    fingerprints_range = calculate_fingerprints_range(descriptor, amp_images)
    np.random.seed(1)
    O_weights_1 = np.random.rand(10, 2)
    O_weights_2 = np.random.rand(1, 3).reshape(-1, 1)
    np.random.seed(2)
    Pd_weights_1 = np.random.rand(10, 2)
    Pd_weights_2 = np.random.rand(1, 3).reshape(-1, 1)
    np.random.seed(3)
    Cu_weights_1 = np.random.rand(10, 2)
    Cu_weights_2 = np.random.rand(1, 3).reshape(-1, 1)

    weights = OrderedDict([
        ("O", OrderedDict([(1, O_weights_1), (2, O_weights_2)])),
        ("Pd", OrderedDict([(1, Pd_weights_1), (2, Pd_weights_2)])),
        ("Cu", OrderedDict([(1, Cu_weights_1), (2, Cu_weights_2)])),
    ])

    scalings = OrderedDict([
        ("O", OrderedDict([("intercept", 0), ("slope", 1)])),
        ("Pd", OrderedDict([("intercept", 0), ("slope", 1)])),
        ("Cu", OrderedDict([("intercept", 0), ("slope", 1)])),
    ])

    calc = Amp(
        descriptor,
        model=NeuralNetwork(
            hiddenlayers=hiddenlayers,
            weights=weights,
            scalings=scalings,
            activation="tanh",
            fprange=fingerprints_range,
            mode="atom-centered",
            fortran=False,
        ),
        logging=False,
    )

    amp_energies = [calc.get_potential_energy(image) for image in images]
    amp_forces = [calc.get_forces(image) for image in images]
    amp_forces = np.concatenate(amp_forces)

    torch_O_weights_1 = torch.FloatTensor(O_weights_1[:-1, :]).t()
    torch_O_bias_1 = torch.FloatTensor(O_weights_1[-1, :])
    torch_O_weights_2 = torch.FloatTensor(O_weights_2[:-1, :]).t()
    torch_O_bias_2 = torch.FloatTensor(O_weights_2[-1, :])
    torch_Pd_weights_1 = torch.FloatTensor(Pd_weights_1[:-1, :]).t()
    torch_Pd_bias_1 = torch.FloatTensor(Pd_weights_1[-1, :])
    torch_Pd_weights_2 = torch.FloatTensor(Pd_weights_2[:-1, :]).t()
    torch_Pd_bias_2 = torch.FloatTensor(Pd_weights_2[-1, :])
    torch_Cu_weights_1 = torch.FloatTensor(Cu_weights_1[:-1, :]).t()
    torch_Cu_bias_1 = torch.FloatTensor(Cu_weights_1[-1, :])
    torch_Cu_weights_2 = torch.FloatTensor(Cu_weights_2[:-1, :]).t()
    torch_Cu_bias_2 = torch.FloatTensor(Cu_weights_2[-1, :])

    device = "cpu"
    dataset = AtomsDataset(
        images,
        descriptor=Gaussian,
        cores=1,
        label="consistency",
        Gs=Gs,
        forcetraining=True,
    )

    fp_length = dataset.fp_length
    batch_size = len(dataset)
    dataloader = DataLoader(dataset,
                            batch_size,
                            collate_fn=collate_amp,
                            shuffle=False)
    model = FullNN(elements, [fp_length, 2, 2], device, forcetraining=True)
    model.state_dict()["elementwise_models.O.model_net.0.weight"].copy_(
        torch_O_weights_1)
    model.state_dict()["elementwise_models.O.model_net.0.bias"].copy_(
        torch_O_bias_1)
    model.state_dict()["elementwise_models.O.model_net.2.weight"].copy_(
        torch_O_weights_2)
    model.state_dict()["elementwise_models.O.model_net.2.bias"].copy_(
        torch_O_bias_2)
    model.state_dict()["elementwise_models.Pd.model_net.0.weight"].copy_(
        torch_Pd_weights_1)
    model.state_dict()["elementwise_models.Pd.model_net.0.bias"].copy_(
        torch_Pd_bias_1)
    model.state_dict()["elementwise_models.Pd.model_net.2.weight"].copy_(
        torch_Pd_weights_2)
    model.state_dict()["elementwise_models.Pd.model_net.2.bias"].copy_(
        torch_Pd_bias_2)
    model.state_dict()["elementwise_models.Cu.model_net.0.weight"].copy_(
        torch_Cu_weights_1)
    model.state_dict()["elementwise_models.Cu.model_net.0.bias"].copy_(
        torch_Cu_bias_1)
    model.state_dict()["elementwise_models.Cu.model_net.2.weight"].copy_(
        torch_Cu_weights_2)
    model.state_dict()["elementwise_models.Cu.model_net.2.bias"].copy_(
        torch_Cu_bias_2)
    import torch.nn as nn
    for name, layer in model.named_modules():
        if isinstance(layer, MLP):
            layer.model_net = nn.Sequential(layer.model_net, Tanh())

    for batch in dataloader:
        x = to_tensor(batch[0], device)
        y = to_tensor(batch[1], device)
        energy_pred, force_pred = model(x)
    for idx, i in enumerate(amp_energies):
        assert round(i, 4) == round(
            energy_pred.tolist()[idx][0],
            4), "The predicted energy of image %i is wrong!" % (idx + 1)
    print("Energy predictions are correct!")
    for idx, sample in enumerate(amp_forces):
        for idx_d, value in enumerate(sample):
            predict = force_pred.tolist()[idx][idx_d]
            assert abs(value - predict) < 0.0001, (
                "The predicted force of image % i, direction % i is wrong! Values: %s vs %s"
                % (idx + 1, idx_d, value, force_pred.tolist()[idx][idx_d]))
    print("Force predictions are correct!")
Esempio n. 38
0
#!/usr/bin/python
from amp import Amp

amp = Amp()

# unmute
amp.pwm.unmute()
Esempio n. 39
0
os.chdir(work_dir)

# Get atoms from the database
images = []
db = connect(db_path)
for d in db.select(['iteration<={}'.format(iteration), 'train_set=True']):
    atoms = db.get_atoms(d.id)
    del atoms.constraints
    images.append(atoms)

# Build Amp object
framework_str = "-".join([str(f) for f in framework])
label = label
dblabel = dblabel
desc = Gaussian(cutoff=cutoff)
model = NeuralNetwork(hiddenlayers=framework)
calc = Amp(label=label,
           dblabel=dblabel,
           descriptor=desc,
           model=model,
	   cores=cores)
loss = LossFunction(convergence={'energy_rmse': energy_rmse,
                                 'force_rmse': force_rmse})
calc.model.lossfunction = loss
           
# Perform simulated annealing for global search
Annealer(calc=calc, images=images)

# Train the network
calc.train(images=images)
Esempio n. 40
0
from amp import Amp
from LithiumVacancyOrdering import VacancyOrdering


ocv = VacancyOrdering()
calc=Amp.load('calc.amp')

ocv.load_atoms('LCO_3x3.traj',calc) 


ocv.run_GCMC(T=300, num_sweeps=270, monoclinic=False)


ocv.get_hull(filename='energy_out.txt')

Esempio n. 41
0
Compare DFT and ml forcefield 
"""
from ase.io import read
from ase.io import Trajectory
from amp import Amp
import ase.io
import numpy as np
import sys

arg = sys.argv
configs = Trajectory(arg[1], 'r')
#configs = read(arg[1], ":")
#except:
#  configs = ase.io.trajectory.convert(arg[1])

calc = Amp.load('amp.amp')
e_off_log = open('e_off_log.dat', 'w')
f_off_log = open('f_off_log.dat', 'w')
e_off_log.write("{:12s} {:12s}\n".format('DFT_E', 'AMP_E'))
f_off_log.write("{:12s} {:12s}\n".format('DFT_f', 'AMP_f'))
off_xyz = open('f_off_traj.xyz', 'w')
e_off_xyz = open('e_off_traj.xyz', 'w')

e_log = open('e_log.dat', 'w')
f_log = open('f_log.dat', 'w')
e_log.write("{:12s} {:12s}\n".format('DFT_E', 'AMP_E'))
f_log.write("{:12s} {:12s}\n".format('DFT_f', 'AMP_f'))

traj_amp = Trajectory('amp_' + arg[1], 'w')

f_threshold = 2.0
Esempio n. 42
0
def train_images(images):
    calc = Amp(descriptor=Gaussian(), model=NeuralNetwork(hiddenlayers=(10, 10, 10)))
    calc.model.lossfunction = LossFunction(convergence={'energy_rmse': 0.001})
    calc.model.lossfunction = LossFunction(force_coefficient=-0.1)
    calc.train(images=images, overwrite=True)
Esempio n. 43
0
images = []
db = connect(db_path)
for d in db.select(['iteration<={}'.format(iteration), 'train_set=True']):
    atoms = db.get_atoms(d.id)
    del atoms.constraints
    images.append(atoms)

# Build Amp object
framework_str = "-".join([str(f) for f in framework])

init_params = "initial-parameters.json"
checkpoints = "checkpoint-parameters.json"
nn_files = os.listdir(label)

if checkpoints in nn_files:
    calc = Amp(load=os.path.join(label, checkpoints))
elif init_params in nn_files:
    calc = Amp(load=os.path.join(label, init_params))
else:
    label = label
    dblabel = dblabel
    desc = Gaussian(cutoff=cutoff)
    model = NeuralNetwork(hiddenlayers=framework)
    calc = Amp(label=label,
            dblabel=dblabel,
            descriptor=desc,
            regression=model)


{% if optimizer %}
# Change optimization algorithm
Esempio n. 44
0
#!/usr/bin/env python
from amp import Amp
from amp.descriptor import Behler
from amp.regression import NeuralNetwork

calc = Amp(label="./",
           descriptor=Behler(cutoff=6.0),
           regression=NeuralNetwork(hiddenlayers=(2, 16)))

calc.train("../train.db",
           cores=12,
           force_goal=None,
           extend_variables=False)
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

    ref_loss = 8004.292841411172
    ref_energyloss = (43.7360019403031 ** 2.) * 3
    ref_forceloss = (137.40994760947325 ** 2.) * 3
    ref_dloss_dparameters = np.array([0.08141668748130322,
                                      0.03231235582925534,
                                      0.04388650395738586,
                                      0.017417514465922313,
                                      0.028431276597563077,
                                      0.011283700608814465,
                                      0.0941695726576061,
                                      -0.12322258890990219,
                                      0.12679918754154568,
                                      63.53960075374332,
                                      0.01624770019548904,
                                      -86.6263955859162,
                                      -0.01777752828707744,
                                      86.22415217526024,
                                      0.017745913074496918,
                                      104.58358033298292,
                                      -96.73280209888215,
                                      -99.09843648905876,
                                      -8.302880631972338,
                                      -1.2590007162074357,
                                      8.302877346883133,
                                      1.25875988418134,
                                      -8.302866610678247,
                                      -1.2563833805675353,
                                      28.324298392680998,
                                      28.093155094726413,
                                      -29.37874455931869,
                                      -11.247473567044866,
                                      11.119951466664787,
                                      -87.08582317481387,
                                      -20.939485239182346,
                                      -125.73267675705365,
                                      -35.138524407482116])

    # Testing pure-python and fortran versions of Gaussian-neural on different
    # number of processes

    for fortran in [False, True]:
        for cores in range(1, 4):
            label = 'train-periodic/%s-%i' % (fortran, cores)
            print label
            calc = Amp(descriptor=Gaussian(cutoff=4.,
                                           Gs=Gs,
                                           fortran=fortran,),
                       model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                           weights=weights,
                                           scalings=scalings,
                                           activation='tanh',
                                           regressor=regressor,
                                           fortran=fortran,),
                       label=label,
                       dblabel=label,
                       cores=cores)

            lossfunction = LossFunction(convergence=convergence)
            calc.model.lossfunction = lossfunction
            calc.train(images=images,)
            diff = abs(calc.model.lossfunction.loss - ref_loss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of loss function is wrong!'
            diff = abs(calc.model.lossfunction.energy_loss - ref_energyloss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom RMSE is wrong!'
            diff = abs(calc.model.lossfunction.force_loss - ref_forceloss)
            assert (diff < 10 ** (-9.)), \
                'Calculated value of force RMSE is wrong!'

            for _ in range(len(ref_dloss_dparameters)):
                diff = abs(calc.model.lossfunction.dloss_dparameters[_] -
                           ref_dloss_dparameters[_])
                assert(diff < 10 ** (-10.)), \
                    'Calculated value of loss function derivative is wrong!'

            dblabel = label
            secondlabel = '_' + label

            calc = Amp(descriptor=Gaussian(cutoff=4.,
                                           Gs=Gs,
                                           fortran=fortran),
                       model=NeuralNetwork(hiddenlayers=hiddenlayers,
                                           weights=weights,
                                           scalings=scalings,
                                           activation='tanh',
                                           regressor=regressor,
                                           fortran=fortran,),
                       label=secondlabel,
                       dblabel=dblabel,
                       cores=cores)

            lossfunction = LossFunction(convergence=convergence)
            calc.model.lossfunction = lossfunction
            calc.train(images=images,)
            diff = abs(calc.model.lossfunction.loss - ref_loss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of loss function is wrong!'
            diff = abs(calc.model.lossfunction.energy_loss - ref_energyloss)
            assert (diff < 10.**(-10.)), \
                'Calculated value of energy per atom RMSE is wrong!'
            diff = abs(calc.model.lossfunction.force_loss - ref_forceloss)
            assert (diff < 10 ** (-9.)), \
                'Calculated value of force RMSE is wrong!'

            for _ in range(len(ref_dloss_dparameters)):
                diff = abs(calc.model.lossfunction.dloss_dparameters[_] -
                           ref_dloss_dparameters[_])
                assert(diff < 10 ** (-10.)), \
                    'Calculated value of loss function derivative is wrong!'