def test_dense_forces(self):
     orig_a = io.read('eam_crash2.poscar')
     c = TabulatedAlloyEAM(fn='Cu_mishin1.eam.alloy')
     for fac in [0.2, 0.3, 0.4, 0.5]:
         a = orig_a.copy()
         a.set_cell(fac * a.cell, scale_atoms=True)
         a.set_calculator(c)
         ffd, f0, maxdf = test_forces(a, dx=dx)
         if maxdf > tol:
             nfail += 1
             print("forces .failed.")
             print("max(df)  = %f" % maxdf)
             print("f - from potential")
             for i, f in enumerate(f0):
                 print(i, f)
             print("f - numerically")
             for i, f in enumerate(ffd):
                 print(i, f)
             print("difference between the above")
             for i, f in enumerate(f0 - ffd):
                 print(i, f)
         self.assertTrue(maxdf < tol)
 def test_dense_forces(self):
     orig_a = io.read('eam_crash2.poscar')
     c = TabulatedAlloyEAM(fn='Cu_mishin1.eam.alloy')
     for fac in [0.2, 0.3, 0.4, 0.5]:
         a = orig_a.copy()
         a.set_cell(fac*a.cell, scale_atoms=True)
         a.set_calculator(c)
         ffd, f0, maxdf = test_forces(a, dx=dx)
         if maxdf > tol:
             nfail += 1
             print("forces .failed.")
             print("max(df)  = %f" % maxdf)
             print("f - from potential")
             for i, f in enumerate(f0):
                 print(i, f)
             print("f - numerically")
             for i, f in enumerate(ffd):
                 print(i, f)
             print("difference between the above")
             for i, f in enumerate(f0-ffd):
                 print(i, f)
         self.assertTrue(maxdf < tol)
Exemple #3
0
def run_forces_and_virial_test(test=None):
    nok = 0
    nfail = 0
    for pot, par, mats in tests:
        if len(sys.argv) > 1:
            found = False
            if par is not None:
                for keyword in sys.argv[1:]:
                    if '__ref__' in par:
                        if par['__ref__'].lower().find(keyword.lower()) != -1:
                            found = True
            try:
                potname = pot.__name__
            except:
                potname = pot.__class__.__name__
            for keyword in sys.argv[1:]:
                if potname.lower().find(keyword.lower()) != -1:
                    found = True
            if not found:
                continue

        try:
            potname = pot.__name__
        except:
            potname = pot.__class__.__name__
        if test is None:
            print("--- %s ---" % potname)
        if par is None:
            c = pot()
        else:
            c = pot(**par)
            if test is None and '__ref__' in par:
                print("    %s" % par["__ref__"])

        for imat in mats:
            rattle = 0.5
            mask = False
            if isinstance(imat, tuple):
                name, a = imat
            else:
                name = imat['name']
                a = imat['struct']
                if 'rattle' in imat:
                    rattle = imat['rattle']
                if 'mask' in imat:
                    mask = imat['mask']
            if test is None:
                print("Material:  ", name)
            a.translate([0.1, 0.1, 0.1])
            a.set_calculator(c)

            masks = [None]
            if mask:
                masks += [
                    random_integers(0, len(a) - 1, size=len(a)) < len(a) / 2,
                    random_integers(0, len(a) - 1, size=len(a)) < len(a) / 4
                ]

            for dummy in range(2):
                if dummy == 0:
                    errmsg = 'potential: {0}; material: {1}; equilibrium' \
                        .format(potname, name)
                    if test is None:
                        print('=== equilibrium ===')
                else:
                    errmsg = 'potential: {0}; material: {1}; distorted' \
                        .format(potname, name)
                    if test is None:
                        print('=== distorted ===')

                for mask in masks:
                    if test is None and mask is not None:
                        print('--- using random mask ---')
                    c.set_mask(mask)

                    ffd, f0, maxdf = test_forces(a, dx=dx)

                    if test is None:
                        if abs(maxdf) < tol:
                            nok += 1
                            print("forces .ok.")
                        else:
                            nfail += 1
                            print("forces .failed.")
                            print("max(df)  = %f" % maxdf)

                            print("f - from potential")
                            for i, f in enumerate(f0):
                                print(i, f)

                            print("f - numerically")
                            for i, f in enumerate(ffd):
                                print(i, f)

                            print("difference between the above")
                            for i, f in enumerate(f0 - ffd):
                                print(i, f)
                    else:
                        test.assertTrue(abs(maxdf) < tol,
                                        msg=errmsg + '; forces')

                    sfd, s0, maxds = test_virial(a, de=dx)

                    if test is None:
                        if abs(maxds) < tol:
                            nok += 1
                            print("virial .ok.")
                        else:
                            nfail += 1
                            print("virial .failed.")
                            print("max(ds)  = %f" % maxds)

                            print("s - from potential")
                            print(s0)

                            print("s - numerically")
                            print(sfd)

                            print("difference between the above")
                            print(s0 - sfd)
                    else:
                        test.assertTrue(abs(maxds) < tol,
                                        msg=errmsg + '; virial')

                    pfd, p0, maxdp = test_potential(a, dq=dx)

                    if test is None:
                        if abs(maxdp) < tol:
                            nok += 1
                            print("potential .ok.")
                        else:
                            nfail += 1
                            print("potential .failed.")
                            print("max(dp)  = %f" % maxdp)

                            print("p - from potential")
                            print(p0)

                            print("p - numerically")
                            print(pfd)

                            print("difference between the above")
                            print(p0 - pfd)
                    else:
                        test.assertTrue(abs(maxds) < tol,
                                        msg=errmsg + '; virial')

                a.rattle(rattle)
    if test is None:
        print('{0} tests passed, {1} tests failed.'.format(nok, nfail))
def run_forces_and_virial_test(test=None):
    nok = 0
    nfail = 0
    for pot, par, mats in tests:
        if len(sys.argv) > 1:
            found = False
            if par is not None:
                for keyword in sys.argv[1:]:
                    if '__ref__' in par:
                        if par['__ref__'].lower().find(keyword.lower()) != -1:
                            found = True
            try:
                potname = pot.__name__
            except:
                potname = pot.__class__.__name__
            for keyword in sys.argv[1:]:
                if potname.lower().find(keyword.lower()) != -1:
                    found = True
            if not found:
                continue

        try:
            potname = pot.__name__
        except:
            potname = pot.__class__.__name__
        if test is None:
            print("--- %s ---" % potname)
        if par is None:
            c  = pot()
        else:
            c  = pot(**par)
            if test is None and '__ref__' in par:
                print("    %s" % par["__ref__"])

        for imat in mats:
            rattle = 0.5
            mask = False
            if isinstance(imat, tuple):
                name, a = imat
            else:
                name = imat['name']
                a = imat['struct']
                if 'rattle' in imat:
                    rattle = imat['rattle']
                if 'mask' in imat:
                    mask = imat['mask']
            if test is None:
                print("Material:  ", name)
            a.translate([0.1,0.1,0.1])
            a.set_calculator(c)

            masks = [None]
            if mask:
                masks += [random_integers(0, len(a)-1, size=len(a)) < len(a)/2,
                          random_integers(0, len(a)-1, size=len(a)) < len(a)/4]

            for dummy in range(2):
                if dummy == 0:
                    errmsg = 'potential: {0}; material: {1}; equilibrium' \
                        .format(potname, name)
                    if test is None:
                        print('=== equilibrium ===')
                else:
                    errmsg = 'potential: {0}; material: {1}; distorted' \
                        .format(potname, name)
                    if test is None:
                        print('=== distorted ===')

                for mask in masks:
                    if test is None and mask is not None:
                        print('--- using random mask ---')
                    c.set_mask(mask)

                    ffd, f0, maxdf = test_forces(a, dx=dx)
        
                    if test is None:
                        if abs(maxdf) < tol:
                            nok += 1
                            print("forces .ok.")
                        else:
                            nfail += 1
                            print("forces .failed.")
                            print("max(df)  = %f" % maxdf)

                            print("f - from potential")
                            for i, f in enumerate(f0):
                                print(i, f)

                            print("f - numerically")
                            for i, f in enumerate(ffd):
                                print(i, f)

                            print("difference between the above")
                            for i, f in enumerate(f0-ffd):
                                print(i, f)
                    else:
                      test.assertTrue(abs(maxdf) < tol,
                                        msg=errmsg+'; forces')

                    sfd, s0, maxds = test_virial(a, de=dx)

                    if test is None:
                        if abs(maxds) < tol:
                            nok += 1
                            print("virial .ok.")
                        else:
                            nfail += 1
                            print("virial .failed.")
                            print("max(ds)  = %f" % maxds)
                    
                            print("s - from potential")
                            print(s0)
                
                            print("s - numerically")
                            print(sfd)

                            print("difference between the above")
                            print(s0-sfd)
                    else:
                        test.assertTrue(abs(maxds) < tol,
                                        msg=errmsg+'; virial')

                    pfd, p0, maxdp = test_potential(a, dq=dx)

                    if test is None:
                        if abs(maxdp) < tol:
                            nok += 1
                            print("potential .ok.")
                        else:
                            nfail += 1
                            print("potential .failed.")
                            print("max(dp)  = %f" % maxdp)
                    
                            print("p - from potential")
                            print(p0)
                
                            print("p - numerically")
                            print(pfd)

                            print("difference between the above")
                            print(p0-pfd)
                    else:
                        test.assertTrue(abs(maxds) < tol,
                                        msg=errmsg+'; virial')
            
                a.rattle(rattle)
    if test is None:
        print('{0} tests passed, {1} tests failed.'.format(nok, nfail))
def run_forces_and_virial_test(test=None):
    for pot, par, mats in tests:
        if len(sys.argv) > 1:
            found = False
            if par is not None:
                for keyword in sys.argv[1:]:
                    if '__ref__' in par:
                        if par['__ref__'].lower().find(keyword.lower()) != -1:
                            found = True
            try:
                potname = pot.__name__
            except:
                potname = pot.__class__.__name__
            for keyword in sys.argv[1:]:
                if potname.lower().find(keyword.lower()) != -1:
                    found = True
            if not found:
                continue

        try:
            potname = pot.__name__
        except:
            potname = pot.__class__.__name__
        if test is None:
            print "--- %s ---" % potname
        if par is None:
            c  = pot()
        else:
            c  = pot(**par)
            if test is None and '__ref__' in par:
                print "    %s" % par["__ref__"]

        for imat in mats:
            if isinstance(imat, tuple):
                name, a = imat
            else:
                name = imat['name']
                a = imat['struct']
            if test is None:
                print "Material:  ", name
            a.translate([0.1,0.1,0.1])
            a.set_calculator(c)

            for dummy in range(2):
                if dummy == 0:
                    errmsg = 'potential: {0}; material: {1}; equilibrium' \
                        .format(potname, name)
                    if test is None:
                        print "...equilibrium..."
                else:
                    errmsg = 'potential: {0}; material: {1}; distorted' \
                        .format(potname, name)
                    if test is None:
                        print "...distorted..."

                ffd, f0, maxdf = test_forces(a, dx=dx)
        
                if test is None:
                    if abs(maxdf) < tol:
                        print "forces .ok."
                    else:
                        print "forces .failed."
                        print "max(df)  = %f" % maxdf

                        print "f - from potential"
                        print f0

                        print "f - numerically"
                        print ffd
                else:
                    test.assertTrue(abs(maxdf) < tol,
                                    msg=errmsg+'; forces')

                sfd, s0, maxds = test_virial(a, de=dx)

                if test is None:
                    if abs(maxds) < tol:
                        print "virial .ok."
                    else:
                        print "virial .failed."
                        print "max(ds)  = %f" % maxds
                    
                        print "s - from potential"
                        print s0
                
                        print "s - numerically"
                        print sfd
                else:
                    test.assertTrue(abs(maxds) < tol,
                                    msg=errmsg+'; virial')

                pfd, p0, maxdp = test_potential(a, dq=dx)

                if test is None:
                    if abs(maxdp) < tol:
                        print "potential .ok."
                    else:
                        print "potential .failed."
                        print "max(dp)  = %f" % maxdp
                    
                        print "p - from potential"
                        print p0
                
                        print "p - numerically"
                        print pfd
                else:
                    test.assertTrue(abs(maxds) < tol,
                                    msg=errmsg+'; virial')
            
                a.rattle(0.5)