def testLAMMPS(): """Test potentials.writePotentials() for LAMMPS""" pota = potentials.Potential("A", "B", lambda x: x) potb = potentials.Potential("C", "D", lambda x: 6.0 - x) expect = [ "A-B", "N 6 R 1.00000000 6.00000000", "", "1 1.00000000 1.00000000 -1.00000000", "2 2.00000000 2.00000000 -1.00000000", "3 3.00000000 3.00000000 -1.00000000", "4 4.00000000 4.00000000 -1.00000000", "5 5.00000000 5.00000000 -1.00000000", "6 6.00000000 6.00000000 -1.00000000", "", "C-D", "N 6 R 1.00000000 6.00000000", "", "1 1.00000000 5.00000000 1.00000000", "2 2.00000000 4.00000000 1.00000000", "3 3.00000000 3.00000000 1.00000000", "4 4.00000000 2.00000000 1.00000000", "5 5.00000000 1.00000000 1.00000000", "6 6.00000000 0.00000000 1.00000000" ] sbuild = StringIO() potentials.writePotentials('LAMMPS', [pota, potb], 6.0, 6, sbuild) sbuild.seek(0) actual = sbuild.readlines() msg = "%s != %s" % (expect, actual) assert len(expect) == len(actual), msg for e, a in zip(expect, actual): a = a.decode() assert os.linesep == a[-1] a = a[:-1] assert e == a
def potential_objects(): f_OO = potentials.potentialforms.buck(1633.00510, 0.327022, 3.948790) f_UU = potentials.potentialforms.buck(294.640000, 0.327022, 0.0) potential_objects = [ potentials.Potential('O', 'O', f_OO), potentials.Potential('U', 'U', f_UU) ] return potential_objects
def testWriteSinglePotential(self): """Test that atsim.potentials._lammps_writeTABLE._writeSinglePotential() works correctly""" expect=["A-B", "N 6 R 0.10000000 5.10000000", "", "1 0.10000000 4.90000000 1.00000000", "2 1.10000000 3.90000000 1.00000000", "3 2.10000000 2.90000000 1.00000000", "4 3.10000000 1.90000000 1.00000000", "5 4.10000000 0.90000000 1.00000000", "6 5.10000000 -0.10000000 1.00000000"] energyfunc = lambda x: 5.0-x pot =potentials.Potential("A", "B", energyfunc) sbuild = StringIO() _lammps_writeTABLE._writeSinglePotential(pot, 0.1, 5.1, 6, sbuild) sbuild.seek(0) actual = sbuild.readlines() msg = "%s != %s" % (expect, actual) self.assertEqual(len(expect), len(actual), msg = msg) for e,a in zip(expect, actual): # a = a.decode() self.assertEqual(os.linesep, a[-1]) a = a[:-1] self.assertEqual(e,a)
def testForce(): """Check that dlpoly.writeTABLE._calculateForce() wraps potentials.Potential.force to return -r dU/dr values needed by DL_POLY""" potfunc = potentials.buck(1388.773, 2.76, 175) pot = potentials.Potential("A", "B", potfunc) expect = pytest.approx(-66990.0984477696) actual = _dlpoly_writeTABLE._calculateForce(pot, 0.5) assert expect == actual
def test_potclass_testForce_numderiv(): """Check potentials.Potential.force() method""" def energyfunc(r): return 1388.773 * math.exp(-r / 0.362318841) - 175.0 / r**6 pot = potentials.Potential("A", "B", energyfunc, h=0.1e-5) expect = 7.151 actual = pot.force(2.0) assert pytest.approx(expect, abs=1e-3) == actual
def testWritePotentials(self): """Test _lammps_writeTABLE.writePotentials() function""" pota =potentials.Potential("A", "B", lambda x: x) potb =potentials.Potential("C", "D", lambda x: 5.0-x) expect=[ "A-B", "N 6 R 0.10000000 5.10000000", "", "1 0.10000000 0.10000000 -1.00000000", "2 1.10000000 1.10000000 -1.00000000", "3 2.10000000 2.10000000 -1.00000000", "4 3.10000000 3.10000000 -1.00000000", "5 4.10000000 4.10000000 -1.00000000", "6 5.10000000 5.10000000 -1.00000000", "", "C-D", "N 6 R 0.10000000 5.10000000", "", "1 0.10000000 4.90000000 1.00000000", "2 1.10000000 3.90000000 1.00000000", "3 2.10000000 2.90000000 1.00000000", "4 3.10000000 1.90000000 1.00000000", "5 4.10000000 0.90000000 1.00000000", "6 5.10000000 -0.10000000 1.00000000"] sbuild = StringIO() _lammps_writeTABLE.writePotentials([pota,potb], 0.1, 5.1, 6, sbuild) sbuild.seek(0) actual = sbuild.readlines() sbuild.seek(0) msg = "%s != %s" % (expect, actual) self.assertEqual(len(expect), len(actual), msg = msg) for e,a in zip(expect, sbuild): # a = a.decode() self.assertEqual(os.linesep, a[-1]) a = a[:-1] self.assertEqual(e,a)
def testWriteEAMFile(self): """Test _writeTABEAM.writeTABEAM() function""" from io import StringIO #Define potentials ppbucku = potentials.Potential('U', 'U', _zerowrap(potentials.buck(668.546808, 0.408333, 0.0))) ppbuckuc = potentials.Potential('U', 'C', _zerowrap(potentials.buck(30.885011, 0.814952, 0.0))) ppswcc = potentials.Potential('C', 'C', _zerowrap(_stillingerWeber(1.1, 2.078, 1.368, 2.5257))) densU = _densityFunction( 1.301894, 2.0, 0.668659, 1.862363, 2.0) densC = _densityFunction(33.446287, 2.0, 1.318078, 1.512686, 2.0) UEAMPot = potentials.EAMPotential('U', 92, 19.05, embeddingFunction, densU ) CEAMPot = potentials.EAMPotential('C', 6, 2.267,embeddingFunction, densC ) maxrho = 410.0 cutoff = 12.0 drho = dr = 0.01 nrho = int(maxrho // drho) nr = int(cutoff // dr) outfile = StringIO() _dlpoly_writeTABEAM.writeTABEAM( nrho, drho, nr, dr, [UEAMPot, CEAMPot], [ppbucku, ppbuckuc, ppswcc], outfile, "Chartier/Van Brutzel Potentials") outfile = StringIO(outfile.getvalue()) #Check structure of the file self.assertEqual(2, self._countSections(outfile, 'embe')) self.assertEqual(2, self._countSections(outfile, 'dens')) self.assertEqual(3, self._countSections(outfile, 'pair'))
def test_potclass_testForceDeriv(): """Check that analytical derivatives are used when available""" # Create a callable that returns an analytical derivative which is very difficult to the (correct) # numerical derivative - then look for this when the force() metho of the potential object is called. deriv_force = 24.0 class AnalyticalDerivative(object): def __call__(self, r): return -3.0 * r + 5.0 def deriv(self, r): return deriv_force potfunc = AnalyticalDerivative() pot = potentials.Potential("A", "B", potfunc) expect = 2.0 actual = pot.energy(1.0) assert pytest.approx(expect) == actual # Now check the force. assert pytest.approx(-deriv_force) == pot.force(1.0)
def testWriteSetFLFinnisSinclair_PairPotentialOrder(self): """Check that pair potentials are written in the correct order""" nrho = 5 drho = 0.1 nr = 5 dr = 0.1 cutoff = 0.5 import collections def defaultdens(): return lambda x: 0.0 defaultdict = collections.defaultdict(defaultdens) eampot1 =potentials.EAMPotential('A', 1, 1.0, lambda x: 0.1, defaultdict) eampot2 =potentials.EAMPotential('B', 2, 2.0, lambda x: 0.2, defaultdict) eampot3 =potentials.EAMPotential('C', 3, 3.0, lambda x: 0.3, defaultdict) pairpot_aa =potentials.Potential('A', 'A', lambda r: 1.0) pairpot_bb =potentials.Potential('B', 'B', lambda r: 2.0) pairpot_cc =potentials.Potential('C', 'C', lambda r: 3.0) pairpot_ba =potentials.Potential('B', 'A', lambda r: 5.0) pairpot_ac =potentials.Potential('A', 'C', lambda r: 6.0) pairpot_bc =potentials.Potential('B', 'C', lambda r: 7.0) # Define two species sio = StringIO() potentials.writeSetFLFinnisSinclair( nrho, drho, nr, dr, [eampot2, eampot1], [pairpot_bb, pairpot_aa, pairpot_ba], comments = ['Comment1', 'Comment2', 'Comment3'], out = sio, cutoff = cutoff) rvals = [0.0, 0.1, 0.2, 0.3, 0.4] expect = [ r * pairpot_bb.energy(r) for r in rvals] expect.extend([ r * pairpot_ba.energy(r) for r in rvals]) expect.extend([ r * pairpot_aa.energy(r) for r in rvals]) sio.seek(0) actual = sio.readlines() actual = actual[37:] actual = [float(v) for v in actual] from . import testutil testutil.compareCollection(self,expect, actual) # Try a ternary system # Define two species sio = StringIO() potentials.writeSetFLFinnisSinclair( nrho, drho, nr, dr, [eampot2, eampot1, eampot3], [pairpot_bb, pairpot_aa, pairpot_ba, pairpot_cc, pairpot_ac, pairpot_bc], sio, ['Comment1', 'Comment2', 'Comment3'], cutoff) rvals = [0.0, 0.1, 0.2, 0.3, 0.4] expect = [ r * pairpot_bb.energy(r) for r in rvals] expect.extend([ r * pairpot_ba.energy(r) for r in rvals]) expect.extend([ r * pairpot_aa.energy(r) for r in rvals]) expect.extend([ r * pairpot_bc.energy(r) for r in rvals]) expect.extend([ r * pairpot_ac.energy(r) for r in rvals]) expect.extend([ r * pairpot_cc.energy(r) for r in rvals]) sio.seek(0) actual = sio.readlines() actual = actual[68:] actual = [float(v) for v in actual] testutil.compareCollection(self,expect, actual)
def testWriteSetFLFromParameters(self): """Test that lammps.potentials.writeSetFL() can generate the Al_zhou.eam.alloy file from LAMMPS distribution""" with open(os.path.join(_getResourceDirectory(), 'Al_zhou.eam.alloy'), 'r') as infile: expectEAMTable = _parseSetFL(infile) comments = [ '#-> LAMMPS Potential File in DYNAMO 86 setfl Format <-#', '# Zhou Al Acta mater(2001)49:4005', '# Implemented by G. Ziegenhain (2007) [email protected]'] #Density function def densityFunction(r): beta = 3.702623 r_e = 2.886166 lamb = 0.790264 f_e = 1.392302 num = f_e * math.exp( -beta * ( (r/r_e) - 1.0) ) den = 1.0 + (( (r/r_e) - lamb )**20) return num/den #Embedding function def subembed(i, F_ni, rho_n, rho): return F_ni * ((rho/rho_n) - 1.0)**float(i) def embeddingFunction(rho): rho_e = 20.226537 rho_n = 0.85 * rho_e rho_o = 1.15 * rho_e nu = 0.779208 if rho < rho_n: return subembed(0, -2.806783, rho_n, rho) + \ subembed(1, -0.276173, rho_n, rho) + \ subembed(2, 0.893409, rho_n, rho) + \ subembed(3, -1.637201, rho_n, rho) elif rho_n <= rho < rho_o: return subembed(0, -2.806783, rho_e, rho) + \ subembed(1, -0.276173, rho_e, rho) + \ subembed(2, 0.893409, rho_e, rho) + \ subembed(3, -1.637201, rho_e, rho) else: return -2.829437 * ( 1.0 - math.log( (rho/rho_e)**nu)) * (rho/rho_e)**nu #Pair potential def ppotfunc(r): A = 0.251519 alpha = 6.94219 r_e = 2.886166 kappa = 0.395132 B = 0.313394 beta = 3.702623 lamb = 0.790264 return ((A * math.exp( -alpha* ( (r/r_e) - 1.0) ))/ (1.0 + ( (r/r_e - kappa)**20))) - \ ((B * math.exp( -beta * ( (r/r_e) - 1.0) ))/ (1.0 + ( (r/r_e - lamb)**20))) alpp = potentials.Potential('Al', 'Al', ppotfunc) nrho = 10001 drho = 0.00559521603477821424 nr = 10001 dr = 0.00101014898510148996 cutoff = 10.10250000000000092371 eampots = [ potentials.EAMPotential('Al', 1, 26.982, embeddingFunction, densityFunction, latticeConstant = 4.05, latticeType = 'FCC')] pairpots = [ alpp ] actualEAMTable = StringIO() potentials.writeSetFL( nrho, drho, nr, dr, eampots, pairpots, comments = comments, out = actualEAMTable, cutoff = cutoff ) actualEAMTable.seek(0) actualEAMTable = _parseSetFL(actualEAMTable) from . import testutil testutil.compareCollection(self,expectEAMTable, actualEAMTable, places = 3, percenttolerance = 7.0)
def testFluorite_NoPair(self): """Test EAM Tabulation using pair potentials for a fluorite structure""" shutil.copyfile( os.path.join(_getResourceDirectory(), 'calc_energy.lmpin'), os.path.join(self.tempdir, 'calc_energy.lmpin')) shutil.copyfile( os.path.join(_getResourceDirectory(), 'CeO2-single_cell.lmpstruct'), os.path.join(self.tempdir, 'structure.lmpstruct')) oldpwd = os.getcwd() os.chdir(self.tempdir) try: with open("potentials.lmpinc", "w") as potfile: potfile.write("pair_style eam/fs\n") potfile.write("pair_coeff * * eam.fs O Ce\n") potfile.write("\n") potfile.write("replicate 4 4 4\n") # Now define potentials def erf(x): # save the sign of x sign = 1 if x >= 0 else -1 x = abs(x) # constants a1 = 0.254829592 a2 = -0.284496736 a3 = 1.421413741 a4 = -1.453152027 a5 = 1.061405429 p = 0.3275911 # A&S formula 7.1.26 t = 1.0/(1.0 + p*x) y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x) return sign*y # erf(-x) = -erf(x) def morse(gamma, r_star, D): def f(r): return D*(math.exp(-2.0*gamma*(r-r_star)) - 2.0*math.exp(-gamma*(r-r_star))) return f def zw(origfunc): def f(r): if r == 0.0: return 0.0 return origfunc(r) return f def zeropot(r): return 0.0 def embedCe(d): return -0.30766574471*(d**0.5) def embedO(d): return -0.690017059438*(d**0.5) def densityCe(r): m=8 a=1556.80263774 #tmpa #55,100//0.00002,0.0001 return (a/(r**m))*0.5*(1+erf(20*(r-1.5))) def densityO(r): m=8 a=106.855913747 #tmpa #55,100//0.00002,0.0001 return (a/(r**m))*0.5*(1+erf(20*(r-1.5))) functionO_Ce = zw(potentials.plus(potentials.buck(351.341192796, 0.380516580733, 0.0), morse(1.86874578949, 2.35603812582, 0.719250701502))) functionO_O = zw(potentials.buck(830.283447557, 0.352856254215, 3.88437209048)) functionCe_Ce = zw(potentials.buck(18600.0, 0.26644, 0.0)) potO_O = potentials.Potential('O', 'O', functionO_O) potO_Ce = potentials.Potential('O', 'Ce', functionO_Ce) potCe_Ce = potentials.Potential('Ce', 'Ce', functionCe_Ce) eampotCe = potentials.EAMPotential('Ce', 92, 238, embedCe, {'Ce': zw(densityCe), 'O' : zw(densityO)}) eampotO = potentials.EAMPotential('O', 8, 16, embedO, {'Ce': zw(densityCe), 'O' : zw(densityO)}) eampots = [eampotCe, eampotO] pairpots = [potO_O, potO_Ce, potCe_Ce] drho = 0.001 nrho = int(100.0 // drho) cutoff = 11.0 dr = 0.01 nr = int(cutoff // dr) with open('eam.fs', 'w') as outfile: potentials.writeSetFLFinnisSinclair( nrho, drho, nr, dr, eampots, [], outfile, ["Yakub potential. Zero embed and density", "",""], cutoff) runLAMMPS() energy = extractLAMMPSEnergy() expect = -1001.27446044 self.assertAlmostEqual(expect, energy, places=5) finally: os.chdir(oldpwd)
def test_potclass_testEnergy(): """Check potentials.Potential.energy""" potfunc = potentials.buck(1388.773, 2.76, 175) pot = potentials.Potential("A", "B", potfunc) assert pytest.approx(-10041.34343169, abs=1e-5) == pot.energy(0.5)
def setUp(self): super(DLPOLYWriteTABEAMTestCase_RunDLPoly, self).setUp() shutil.copyfile(os.path.join(_getResourceDirectory(), 'CONTROL_CeO2'), os.path.join(self.tempdir, 'CONTROL')) shutil.copyfile(os.path.join(_getResourceDirectory(), 'CONFIG_CeO2'), os.path.join(self.tempdir, 'CONFIG')) shutil.copyfile(os.path.join(_getResourceDirectory(), 'FIELD_CeO2'), os.path.join(self.tempdir, 'FIELD')) # Now define potentials def erf(x): # save the sign of x sign = 1 if x >= 0 else -1 x = abs(x) # constants a1 = 0.254829592 a2 = -0.284496736 a3 = 1.421413741 a4 = -1.453152027 a5 = 1.061405429 p = 0.3275911 # A&S formula 7.1.26 t = 1.0/(1.0 + p*x) y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x) return sign*y # erf(-x) = -erf(x) def morse(gamma, r_star, D): def f(r): return D*(math.exp(-2.0*gamma*(r-r_star)) - 2.0*math.exp(-gamma*(r-r_star))) return f def zw(origfunc): def f(r): if r == 0.0: return 0.0 return origfunc(r) return f def zeropot(r): return 0.0 def embedCe(d): return -0.30766574471*(d**0.5) def embedO(d): return -0.690017059438*(d**0.5) def densityCe(r): m=8 a=1556.80263774 #tmpa #55,100//0.00002,0.0001 return (a/(r**m))#*0.5*(1+erf(20*(r-1.5))) def densityO(r): m=8 a=106.855913747 #tmpa #55,100//0.00002,0.0001 return (a/(r**m))#*0.5*(1+erf(20*(r-1.5))) functionO_Ce = zw(potentials.plus(potentials.buck(351.341192796, 0.380516580733, 0.0), morse(1.86874578949, 2.35603812582, 0.719250701502))) functionO_O = zw(potentials.buck(830.283447557, 0.352856254215, 3.88437209048)) functionCe_Ce = zw(potentials.buck(18600.0, 0.26644, 0.0)) potO_O = potentials.Potential('O', 'O', functionO_O) potO_Ce = potentials.Potential('O', 'Ce', functionO_Ce) potCe_Ce = potentials.Potential('Ce', 'Ce', functionCe_Ce) eampotCe = potentials.EAMPotential('Ce', 92, 238, embedCe, zw(densityCe)) eampotO = potentials.EAMPotential('O', 8, 16, embedO, zw(densityO)) self.eampots = [eampotCe, eampotO] self.pairpots = [potO_O, potO_Ce, potCe_Ce]