def test_add_zeroed_interactions(): with _get_resource_dir().join("setfl.aspot").open() as infile: cfg = Configuration() tab = cfg.read(infile) # Check that eam potential exist expect_embed = set(['Ga', 'In', 'O']) actual_embed = set([ep.species for ep in tab.eam_potentials]) assert expect_embed == actual_embed # Check that the defaulted embedding functions are instances of as.zero zero_type = type(zero().func) as_zero_func_species = set([ ep.species for ep in tab.eam_potentials if hasattr(ep.embeddingFunction, "func") and type(ep.embeddingFunction.func) == zero_type ]) # assert set(['O', 'Mg', 'Al']) == as_zero_func_species assert set(['O']) == as_zero_func_species # Check that null-density functions exist. expect_density = expect_embed actual_density = set() for ep in tab.eam_potentials: a = ep.species actual_density.add(a) assert expect_density == actual_density # Check that tabulation can complete. import io outfile = io.StringIO() tab.write(outfile)
def test_pair_configuration(): cfg_string = u"""[Pair] O-O = as.buck 1000.0 0.3 32.0 U-O = as.buck 2000.0 0.2 0.0 """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) assert tabulation.nr == 1001 assert tabulation.dr == 0.01 assert tabulation.cutoff == 10.0 assert tabulation.type == "Pair" assert tabulation.target == "LAMMPS" assert 2 == len(tabulation.potentials) potlist = tabulation.potentials expect = [("O", "O"), ("U", "O")] actual = [(p.speciesA, p.speciesB) for p in potlist] assert sorted(expect) == sorted(actual) r = 1.3 buck_oo = pf.buck(r, 1000.0, 0.3, 32.0) buck_uo = pf.buck(r, 2000.0, 0.2, 0.0) expect = [(("O", "O"), pytest.approx(buck_oo)), (("U", "O"), pytest.approx(buck_uo))] actual = [((p.speciesA, p.speciesB), p.energy(r)) for p in potlist] assert sorted(expect) == sorted(actual)
def test_lammps_setfl_crg_tabulate_UO2(lammps_run_fluorite_fixture): tmpdir = lammps_run_fluorite_fixture cfgobj = Configuration() config_file = io.open( _get_lammps_resource_dir().join("CRG_U_Th.aspot").strpath, encoding="utf8") tabulation = cfgobj.read(config_file) with tmpdir.join("table.eam.alloy").open("w") as outfile: tabulation.write(outfile) with lammps_run_fluorite_fixture.join("potentials.lmpinc").open( 'w') as potfile: potfile.write(u"""variable O equal 1 set type 1 charge -1.1104 set type 2 charge 2.2208 kspace_style pppm 1.0e-6 pair_style hybrid/overlay coul/long 10.0 eam/alloy pair_coeff * * coul/long pair_coeff * * eam/alloy table.eam.alloy O U """) runLAMMPS(cwd=tmpdir.strpath) energy = extractLAMMPSEnergy(cwd=tmpdir.strpath) expect = -163.072240194504 assert pytest.approx(expect) == energy
def main(): # Make a file like object from the potable input string given above. potable_input_file = io.StringIO(potable_input) # Create a Configuration() object and read input from the input file. configuration = Configuration() # ... Configuration is a factory class for PairTabulation and EAMTabulation # objects. In the current case it will return a GULP_PairTabulation object. tabulation = configuration.read(potable_input_file) # The potable input defines a single pair potential. # Potential objects are accessible from the tabulation object through # its .potentials property. potential_Si_O = tabulation.potentials[0] # The potential-form for this interaction is now accessed. multirange_potentialform = potential_Si_O.potentialFunction # The potential-forms created from potable input are Multi_Range_Potential_Form # objects. This is true even if only one range is defined, as is the case here. # # Let's get hold of the spline potential form through the Multi_Range_Potential_Form # .range_defns property (which returns a list of MultiRangeDefinitionTuple) # spline_potentialform = multirange_potentialform.range_defns[ 0].potential_form # Now let's get hold of the spline coefficients spline_coefficients = spline_potentialform.splineCoefficients print("Spline coefficients are: {}".format(spline_coefficients))
def test_dlpoly_EAM_FS_tabulate_AlFe(tmpdir): cfg_file_path = _get_lammps_resource_dir().join("AlFe_setfl_fs.aspot") from atsim.potentials.config._config_parser import _RawConfigParser inifile = _RawConfigParser() inifile.read(u"{}".format(cfg_file_path.strpath)) inifile[u"Tabulation"][u'target'] = u"DL_POLY_EAM_fs" modified = io.StringIO() inifile.write(modified) modified.seek(0) cfgobj = Configuration() tabulation = cfgobj.read(modified) with tmpdir.join("TABEAM").open("w") as outfile: tabulation.write(outfile) _get_dlpoly_resource_dir().join("CONTROL_random_Al_Fe").copy( tmpdir.join("CONTROL")) _get_dlpoly_resource_dir().join("CONFIG_random_Al_Fe").copy( tmpdir.join("CONFIG")) _get_dlpoly_resource_dir().join("FIELD_random_Al_Fe").copy( tmpdir.join("FIELD")) runDLPoly(cwd=tmpdir.strpath) actual = extractDLPOLYEnergy(cwd=tmpdir.strpath) expect = -31.769632 assert pytest.approx(expect) == actual
def test_custom_species_data(): cfg_file_path = _get_lammps_resource_dir().join("CRG_U_Th.aspot") from atsim.potentials.config._config_parser import _RawConfigParser inifile = _RawConfigParser() inifile.read(u"{}".format(cfg_file_path.strpath)) inifile.add_section(u'Species') inifile[u"Species"][u"U.atomic_mass"] = u"235" inifile[u"Species"][u"U.lattice_constant"] = u"5.678" inifile[u"Species"][u"Th.lattice_type"] = u"bcc" modified = io.StringIO() inifile.write(modified) modified.seek(0) cfgobj = Configuration() tabulation = cfgobj.read(modified) plist = tabulation.eam_potentials upot = [p for p in plist if p.species == u"U"][0] assert pytest.approx(235.0) == upot.mass assert pytest.approx(5.678) == upot.latticeConstant cepot = [p for p in plist if p.species == u"Th"][0] assert cepot.latticeType == u'bcc'
def test_lammps_pair_configuration_tabulate(lammps_run_fixture): tmpdir = lammps_run_fixture cfg_string = u"""[Potential-Form] buck(r, A, rho, C) : A*exp(-r/rho) - C/r^6 morse(r, gamma, r_star, D) : D*(exp(-2.0*gamma*(r-r_star)) - 2.0*exp(-gamma*(r-r_star))) buck_morse(r, A, rho, C, gamma, r_star, D) : buck(r,A,rho,C) + morse(r, gamma, r_star, D) [Pair] O-U = buck_morse 1000.0 0.1 32.0 0.3 2.0 10.0 """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) with tmpdir.join("table.lmptab").open("w") as outfile: tabulation.write(outfile) with lammps_run_fixture.join("potentials.lmpinc").open('w') as potfile: potfile.write( "pair_style hybrid/overlay zero {0} table linear {1}\n".format( tabulation.cutoff, tabulation.nr)) potfile.write("pair_coeff * * zero\n") potfile.write("pair_coeff 1 2 table table.lmptab O-U\n") potfile.write("\n") runLAMMPS(cwd=tmpdir.strpath) energy = extractLAMMPSEnergy(cwd=tmpdir.strpath) expect = pf.buck(2.0, 1000.0, 0.1, 32.0) + pf.morse(2.0, 0.3, 2.0, 10.0) assert pytest.approx(expect) == energy
def test_lammps_EAM_FS_tabulate_AlFe(lammps_run_fixture): tmpdir = lammps_run_fixture cfgobj = Configuration() config_file = io.open( _get_lammps_resource_dir().join("AlFe_setfl_fs.aspot").strpath, encoding="utf8") tabulation = cfgobj.read(config_file) _get_lammps_resource_dir().join("random_Al_Fe.lmpstruct").copy( tmpdir.join("structure.lmpstruct")) _get_lammps_resource_dir().join("AlFe_mm.eam.fs").copy( tmpdir.join("table.eam.fs")) with tmpdir.join("potentials.lmpinc").open("w") as potfile: potfile.write(u"pair_style eam/fs\n") potfile.write(u"pair_coeff * * table.eam.fs Al Fe\n") runLAMMPS(cwd=tmpdir.strpath) expect = extractLAMMPSEnergy(cwd=tmpdir.strpath) with tmpdir.join("table.eam.fs").open("w") as outfile: tabulation.write(outfile) runLAMMPS(cwd=tmpdir.strpath) actual = extractLAMMPSEnergy(cwd=tmpdir.strpath) assert pytest.approx(expect) == actual
def main(): # Make a file like object from the potable input string given above. potable_input_file = io.StringIO(potable_input) # Create a Configuration() object and read input from the input file. configuration = Configuration() # This example shows how to override and add items to potable input before it # is passed to the Configuration object. # The tabulation target will be change to 'LAMMPS' # The cutoff will be reduced to 6.5 # An additional pair-interaction will be given for O-O cp = ConfigParser( potable_input_file, overrides=[ ConfigParserOverrideTuple("Tabulation", "target", "LAMMPS"), ConfigParserOverrideTuple("Tabulation", "cutoff", "6.5") ], additional=[ ConfigParserOverrideTuple("Pair", "O-O", "as.buck 444.7686 0.402 0.0") ]) # Create the tabulation by passing the Config_Parser object to the Configuration.read_from_parser method. tabulation = configuration.read_from_parser(cp) # Now write tabulation to console tabulation.write(sys.stderr)
def test_unknown_modifier_exception(): cfg_string = u"""[Pair] U-O = unknown_modifier(as.buck 1000.0 0.3 32.0, as.constant 1.0) """ cfgobj = Configuration() with pytest.raises(Unknown_Modifier_Exception): tabulation = cfgobj.read(io.StringIO(cfg_string))
def test_trans_modifier(): cfg_string = u"""[Pair] A-B = trans(pow(as.polynomial 3.0 2.0, as.constant 2), as.constant -2.0) A-A = sum(as.buck 1000.0 0.1 0, trans(as.buck 1000.0 0.1 0, as.constant 1.0)) """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) pots = tabulation.potentials potdict = dict([((p.speciesA, p.speciesB), p) for p in pots]) ab = potdict[("A", "B")] import sympy r, A, B, C = sympy.symbols("r A B C") ab_sympy = (A + B * (r - 2.0))**C var_dict = dict(r=2.5, A=3.0, B=2.0, C=2.0) expect = float(ab_sympy.subs(var_dict)) actual = ab.energy(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ab.potentialFunction, "deriv") ab_deriv = sympy.diff(ab_sympy, r) expect = float(ab_deriv.subs(var_dict)) actual = ab.potentialFunction.deriv(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ab.potentialFunction, "deriv2") ab_deriv2 = sympy.diff(ab_sympy, r, 2) expect = float(ab_deriv2.subs(var_dict)) actual = ab.potentialFunction.deriv2(var_dict["r"]) assert pytest.approx(expect) == actual aa = potdict[("A", "A")] r, A, B, C, D, E, F = sympy.symbols("r A B C D E F") aa_sympy = (A * sympy.exp(-r / B) - C / r**6) + (D * sympy.exp(-(r + 1) / E) - F / (r + 1)**6) var_dict = dict(r=2.5, A=1000.0, B=0.1, C=0.0, D=1000.0, E=0.1, F=0.0) expect = float(aa_sympy.subs(var_dict)) actual = aa.energy(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(aa.potentialFunction, "deriv") aa_deriv = sympy.diff(aa_sympy, r) expect = float(aa_deriv.subs(var_dict)) actual = aa.potentialFunction.deriv(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(aa.potentialFunction, "deriv2") aa_deriv2 = sympy.diff(aa_sympy, r, 2) expect = float(aa_deriv2.subs(var_dict)) actual = aa.potentialFunction.deriv2(var_dict["r"]) assert pytest.approx(expect) == actual
def test_dlpoly_pair_configuration_tabulate(tmpdir): cfg_string = u"""[Tabulation] target : DLPOLY nr : 1000 cutoff : 6.5 [Potential-Form] buck(r, A, rho, C) : A*exp(-r/rho) - C/r^6 morse(r, gamma, r_star, D) : D*(exp(-2.0*gamma*(r-r_star)) - 2.0*exp(-gamma*(r-r_star))) buck_morse(r, A, rho, C, gamma, r_star, D) : buck(r,A,rho,C) + morse(r, gamma, r_star, D) [Pair] O-U = buck_morse 1000.0 0.1 32.0 0.3 2.0 10.0 """ import py.path this_dir = py.path.local(py.path.local(__file__).dirname) dlpoly_resource_dir = py.path.local( this_dir.dirname).join("dl_poly_resources") templatevars = dict(speciesA="O", speciesB="U", Ax=0.0, Ay=0.0, Az=0.0, Bx=2.0, By=0.0, Bz=0.0, potDef="vdw 1\nO U tab\n") # Write the config file with dlpoly_resource_dir.join("CONFIG_pair.in").open() as config_template: with tmpdir.join("CONFIG").open("w") as outfile: outfile.write(config_template.read() % templatevars) # Write the FIELD file with dlpoly_resource_dir.join("FIELD_pair.in").open() as field_template: with tmpdir.join("FIELD").open("w") as outfile: outfile.write(field_template.read() % templatevars) # Write the CONTROL file dlpoly_resource_dir.join("CONTROL_pair").copy(tmpdir.join("CONTROL")) # Finally write the TABLE file cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) with tmpdir.join("TABLE").open("w") as outfile: tabulation.write(outfile) runDLPoly(cwd=tmpdir.strpath) energy = extractDLPOLYEnergy(cwd=tmpdir.strpath) expect = pf.buck(2.0, 1000.0, 0.1, 32.0) + pf.morse(2.0, 0.3, 2.0, 10.0) assert pytest.approx(expect) == energy
def test_pymath(potential_form, expected_output): cfg_string = base_template.format(potential_form) cfgfile = io.StringIO(cfg_string) cfgobj = Configuration() tabulation = cfgobj.read(cfgfile) pot = tabulation.potentials[0] actual = pot.energy(1.0) assert pytest.approx(expected_output) == actual
def test_pair_model(tmpdir): cfg = u"""[Tabulation] target : excel dr : 0.01 cutoff : 5 [Pair] O-O : as.polynomial 0 1 Al-O : as.polynomial 0 2 """ infile = io.StringIO(cfg) configuration = Configuration() tabulation = configuration.read(infile) assert type(tabulation) is Excel_PairTabulation out_path = tmpdir.join("pair.xlsx") with tabulation.open_fp(out_path.strpath) as outfile: tabulation.write(outfile) wb = load_workbook(out_path.strpath) assert ["Pair"] == wb.sheetnames ws = wb["Pair"] # Check column headings assert ["r", "Al-O", "O-O"] == [c.value for c in next(ws.rows)] # Check number of table rows assert len(next(ws.columns)) == 502 # Check some values assert ws["A2"].value == 0.0 assert ws["B2"].value == 0.0 assert ws["C2"].value == 0.0 assert ws["A3"].value == 0.01 assert ws["B3"].value == pytest.approx(2 * 0.01) assert ws["C3"].value == pytest.approx(0.01) assert ws["A502"].value == 5.0 assert ws["B502"].value == pytest.approx(2.0 * 5.0) assert ws["C502"].value == pytest.approx(5.0)
def test_adp_in_lammps(tmpdir): expect_dir = tmpdir.join("expect") expect_dir.ensure(dir=True) lmpin = _get_lammps_resource_dir().join("AlCu3.lmpstruct") lmpin.copy(expect_dir.join("structure.lmpstruct")) lmpin = _get_lammps_resource_dir().join("calc_energy.lmpin") lmpin.copy(expect_dir.join("calc_energy.lmpin")) # Copy existing table file _get_lammps_resource_dir().join("AlCu.adp").copy(expect_dir.join("AlCu.adp")) with expect_dir.join("potentials.lmpinc").open("w") as potentials: potentials.write("pair_style adp\n") potentials.write("pair_coeff * * AlCu.adp Al Cu\n") runLAMMPS(cwd=expect_dir.strpath) expect_energy = extractLAMMPSEnergy(cwd=expect_dir.strpath) actual_dir = tmpdir.join("actual") actual_dir.ensure(dir=True) lmpin = _get_lammps_resource_dir().join("AlCu3.lmpstruct") lmpin.copy(actual_dir.join("structure.lmpstruct")) lmpin = _get_lammps_resource_dir().join("calc_energy.lmpin") lmpin.copy(actual_dir.join("calc_energy.lmpin")) with actual_dir.join("potentials.lmpinc").open("w") as potentials: potentials.write("pair_style adp\n") potentials.write("pair_coeff * * AlCu.adp Al Cu\n") # Tabulate potential cfg_file = _get_lammps_resource_dir().join("Al_Cu_adp.aspot") configuration = Configuration() tabulation = configuration.read(cfg_file.open('r')) with actual_dir.join("AlCu.adp").open("w") as outfile: tabulation.write(outfile) runLAMMPS(cwd=actual_dir.strpath) actual_energy = extractLAMMPSEnergy(cwd=actual_dir.strpath) assert expect_energy == approx(actual_energy)
def test_vararg_potentialform(): """Check that a potential form that uses varargs works correctly""" cfg_string = u"""[Pair] O-O = as.polynomial 10.0 20.0 30.0 """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) potfunc = tabulation.potentials[0].potentialFunction assert pf.polynomial(1.6, 10.0, 20.0, 30.0) == potfunc(1.6) # Now check its use in a custom potentialform cfg_string = u"""[Pair] O-O = mypoly 10.0 20.0 30.0 [Potential-Form] mypoly(r, A, B, C) = as.polynomial(r, A,B,C) + 10.0""" expect = pf.polynomial(1.6, 10.0, 20.0, 30.0) + 10.0 cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) potfunc = tabulation.potentials[0].potentialFunction assert pytest.approx(expect) == potfunc(1.6)
def test_sum_modifier_deriv(): """Make sure the sum() modifier uses .deriv() methods correctly if they are available""" cfg_string = u"""[Pair] #no deriv A-B = sum(born_mayer 1000.0 0.1, dispersion 32.0) # both have deriv B-C = sum(as.bornmayer 1000.0 0.1, as.buck 0 1.0 32.0) #one_deriv C-D = sum(as.bornmayer 1000.0 0.1, dispersion 32.0) [Potential-Form] born_mayer(r, A, rho) = A * exp(-r/rho) dispersion(r, C) = - C/r^6 """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) pots = tabulation.potentials expect = [("A", "B"), ("B", "C"), ("C", "D")] actual = [(p.speciesA, p.speciesB) for p in pots] assert sorted(expect) == sorted(actual) potdict = dict([((p.speciesA, p.speciesB), p) for p in pots]) assert not hasattr(potdict[('A', 'B')].potentialFunction, 'deriv') assert hasattr(potdict[('B', 'C')].potentialFunction, 'deriv') assert hasattr(potdict[('C', 'D')].potentialFunction, 'deriv') expect_energy = pytest.approx(pf.buck(1.6, 1000, 0.1, 32.0)) expect_deriv = pytest.approx(pf.buck.deriv(1.6, 1000, 0.1, 32.0)) assert expect_energy == potdict[('A', 'B')].potentialFunction(1.6) assert expect_energy == potdict[('B', 'C')].potentialFunction(1.6) assert expect_deriv == potdict[('B', 'C')].potentialFunction.deriv(1.6) assert expect_energy == potdict[('C', 'D')].potentialFunction(1.6) assert expect_deriv == potdict[('C', 'D')].potentialFunction.deriv(1.6)
def test_sum_modifier(): cfg_string = u"""[Pair] O-O = as.buck 1000.0 0.3 32.0 U-O = sum(as.buck 1000.0 0.3 32.0, as.constant 1.0) """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) potlist = tabulation.potentials expect = [("O", "O"), ("U", "O")] actual = [(p.speciesA, p.speciesB) for p in potlist] assert sorted(expect) == sorted(actual) r = 1.3 buck_oo = pf.buck(r, 1000.0, 0.3, 32.0) expect = [(("O", "O"), pytest.approx(buck_oo)), (("U", "O"), pytest.approx(buck_oo + 1.0))] actual = [((p.speciesA, p.speciesB), p.energy(r)) for p in potlist] assert sorted(expect) == sorted(actual)
def test_adp_tabulation(tmpdir): cfg_file = _get_lammps_resource_dir().join("Al_Cu_adp.aspot") configuration = Configuration() tabulation = configuration.read(cfg_file.open('r')) assert type(tabulation) is ADP_EAMTabulation # assert "eam_adp" == tabulation.tabulation_type assert 10000 == tabulation.nrho assert tabulation.drho == approx(2.2770502180000001e-03) assert 10000 == tabulation.nr assert tabulation.dr == approx(6.2872099999999995e-04) assert 2 == len(tabulation.eam_potentials) eam_dict = dict([(ep.species, ep) for ep in tabulation.eam_potentials]) assert ['Al', 'Cu'] == sorted(eam_dict.keys()) assert eam_dict['Al'].embeddingValue(0.1001902096E+01) == approx(-0.2418739157E+01) assert eam_dict['Cu'].embeddingValue(0.1009492263E+01) == approx(-0.9390266026E+00) assert eam_dict['Al'].electronDensity(0.1609791553E+01) == approx(0.7617792157E-01) assert eam_dict['Cu'].electronDensity(0.1512859370E+01) == approx(0.1824604072E+00) assert 3 == len(tabulation.potentials) assert [('Al', 'Al'), ('Al','Cu'), ('Cu', 'Cu')] == sorted([tuple(sorted((p.speciesA, p.speciesB))) for p in tabulation.potentials]) pair_dict = dict([(tuple(sorted((p.speciesA, p.speciesB))), p) for p in tabulation.potentials]) assert pair_dict[('Al', 'Al')].energy(0.1123368233E+01) == approx(0.5178467078E+01) assert pair_dict[('Al', 'Cu')].energy(0.1500522547E+01) == approx(0.2913519771E+01) assert pair_dict[('Cu', 'Cu')].energy(0.1817755147E+01) == approx(0.2103976024E+01) assert 1 == len(tabulation.dipole_potentials) pair_dict = dict([(tuple(sorted((p.speciesA, p.speciesB))), p) for p in tabulation.dipole_potentials]) assert pair_dict[('Al', 'Cu')].energy(0.1634465200E+01) == approx(-0.5117209620E-01) assert 1 == len(tabulation.quadrupole_potentials) pair_dict = dict([(tuple(sorted((p.speciesA, p.speciesB))), p) for p in tabulation.quadrupole_potentials]) assert pair_dict[('Al', 'Cu')].energy(0.5044715650E+01) == approx(-0.4386367758E-02)
def test_multirange_deriv(): """Make sure that .deriv() methods are used where possible in MultiRangePotential""" # Neither potentialform has deriv() - resultant doesn't have deriv() cfg_string = u"""[Pair] #no deriv A-B = born_mayer 1000.0 0.1 >=3.0 dispersion 32.0 # both have deriv B-C = as.bornmayer 1000.0 0.1 >=3.0 as.buck 0 1.0 32.0 #one_deriv C-D = as.bornmayer 1000.0 0.1 >=3.0 dispersion 32.0 [Potential-Form] born_mayer(r, A, rho) = A * exp(-r/rho) dispersion(r, C) = - C/r^6 """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) pots = tabulation.potentials potdict = dict([((p.speciesA, p.speciesB), p) for p in pots]) assert not hasattr(potdict[('A', 'B')].potentialFunction, 'deriv') assert hasattr(potdict[('B', 'C')].potentialFunction, 'deriv') assert hasattr(potdict[('C', 'D')].potentialFunction, 'deriv') assert pytest.approx(pf.bornmayer.deriv( 1.6, 1000.0, 0.1)) == potdict[('B', 'C')].potentialFunction.deriv(1.6) assert pytest.approx(pf.buck.deriv( 3.2, 0, 1.0, 32.0)) == potdict[('B', 'C')].potentialFunction.deriv(3.2) assert pytest.approx(pf.bornmayer.deriv( 1.6, 1000.0, 0.1)) == potdict[('C', 'D')].potentialFunction.deriv(1.6) assert pytest.approx(pf.buck.deriv( 3.2, 0, 1.0, 32.0)) == potdict[('C', 'D')].potentialFunction.deriv(3.2)
def test_dlpoly_TABEAM_tabulate_CeO2(tmpdir): # Copy files into the tmpdir. rd = _get_dlpoly_resource_dir() files = [("CONFIG_CeO2", "CONFIG"), ("CONTROL_CeO2", "CONTROL"), ("FIELD_CeO2", "FIELD")] for src, dest in files: src = rd.join(src) dest = tmpdir.join(dest) src.copy(dest) # Tabulate the TABEAM potential cfgobj = Configuration() config_file = io.open(rd.join("CRG_Ce.aspot").strpath, encoding="utf8") tabulation = cfgobj.read(config_file) with tmpdir.join("TABEAM").open("w") as outfile: tabulation.write(outfile) runDLPoly(cwd=tmpdir.strpath) actual = extractDLPOLYEnergy(cwd=tmpdir.strpath) expect = -532.6778 assert pytest.approx(expect) == actual
def test_pair_deriv(): """Test that pair potentials defined in potentialfunctions expose their .deriv() functions""" cfg_string = u"""[Pair] O-O = as.buck 1000.0 0.3 32.0 """ cfgobj = Configuration() # import pdb; pdb.set_trace() tabulation = cfgobj.read(io.StringIO(cfg_string)) potfunc = tabulation.potentials[0].potentialFunction assert hasattr(potfunc, "deriv") assert hasattr(potfunc, "deriv2") assert pf.buck.deriv(1.6, 1000.0, 0.3, 32.0) == potfunc.deriv(1.6) assert pf.buck.deriv2(1.6, 1000.0, 0.3, 32.0) == potfunc.deriv2(1.6) # Test that cexprtk potentialform doesn't provide a .deriv function. cfg_string = u"""[Pair] O-O = my_buck 1000.0 0.3 32.0 [Potential-Form] my_buck(r, A, rho, C) = A*exp(-r/rho) - C/r^6 """ cfgobj = Configuration() # import pdb; pdb.set_trace() tabulation = cfgobj.read(io.StringIO(cfg_string)) potfunc = tabulation.potentials[0].potentialFunction assert not hasattr(potfunc, "deriv") assert not hasattr(potfunc, "deriv2")
def test_product_modifier(): cfg_string = u"""[Pair] A-B = product(as.buck 1000.0 0.2 32.0, as.polynomial 0.0 2.0, as.polynomial 1.0 -3.0 0.5) A-C = sum( product( as.constant 2.0, as.polynomial 1.0 2.0 1.5, product( as.polynomial 1.0 2.0 3.0, as.polynomial 4.0 5.0 6.0 ) ), as.polynomial 0.0 -1.0) """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) pots = tabulation.potentials potdict = dict([((p.speciesA, p.speciesB), p) for p in pots]) ab = potdict[("A", "B")] import sympy r, A, rho, C, p1_0, p1_1, p2_0, p2_1, p2_2 = sympy.symbols( "r A rho C p1_0 p1_1 p2_0 p2_1 p2_2") ab_sympy = (A * sympy.exp(-r / rho) - C / r**6) * (p1_0 + p1_1 * r) * (p2_0 + p2_1 * r + p2_2 * r**2) var_dict = dict(r=2.5, A=1000.0, rho=0.2, C=32.0, p1_0=0.0, p1_1=2.0, p2_0=1.0, p2_1=-3.0, p2_2=0.5) expect = float(ab_sympy.subs(var_dict)) actual = ab.energy(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ab.potentialFunction, "deriv") ab_deriv = sympy.diff(ab_sympy, r) expect = float(ab_deriv.subs(var_dict)) actual = ab.potentialFunction.deriv(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ab.potentialFunction, "deriv2") ab_deriv2 = sympy.diff(ab_sympy, r, 2) expect = float(ab_deriv2.subs(var_dict)) actual = ab.potentialFunction.deriv2(var_dict["r"]) assert pytest.approx(expect) == actual ac = potdict[("A", "C")] r = sympy.symbols("r") A = sympy.symbols("A") p1_0, p1_1, p1_2 = sympy.symbols("p1_0 p1_1 p1_2") p2_0, p2_1, p2_2 = sympy.symbols("p2_0 p2_1 p2_2") p3_0, p3_1, p3_2 = sympy.symbols("p3_0 p3_1 p3_2") p4_0, p4_1 = sympy.symbols("p4_0 p4_1") ac_sympy = (A * (p1_0 + p1_1 * r + p1_2 * r**2) * ((p2_0 + p2_1 * r + p2_2 * r**2) * (p3_0 + p3_1 * r + p3_2 * r**2))) + (p4_0 + p4_1 * r) var_dict = dict(r=2.5, A=2.0, p1_0=1.0, p1_1=2.0, p1_2=1.5, p2_0=1.0, p2_1=2.0, p2_2=3.0, p3_0=4.0, p3_1=5.0, p3_2=6.0, p4_0=0.0, p4_1=-1.0) expect = float(ac_sympy.subs(var_dict)) actual = ac.energy(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ac.potentialFunction, "deriv") ac_deriv = sympy.diff(ac_sympy, r) expect = float(ac_deriv.subs(var_dict)) actual = ac.potentialFunction.deriv(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ac.potentialFunction, "deriv2") ac_deriv2 = sympy.diff(ac_sympy, r, 2) expect = float(ac_deriv2.subs(var_dict)) actual = ac.potentialFunction.deriv2(var_dict["r"]) assert pytest.approx(expect) == actual
def test_pow_modifier(): cfg_string = u"""[Pair] A-B = pow(as.polynomial 3.0 2.0, as.constant 2) A-A = pow(as.polynomial 3.0 2.0, pow(as.polynomial 0 2, as.constant 0.5)) A-C = pow(sum(as.buck 1000 0.3 0, as.polynomial 0 1 2), pow(sum(as.polynomial 0 0.1, as.polynomial 0 -0.05), as.constant 0.5)) A-D = pow(as.polynomial 1.0 2.0 3.0, as.polynomial 0 5 0.1, as.constant 0.01) """ cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) pots = tabulation.potentials potdict = dict([((p.speciesA, p.speciesB), p) for p in pots]) ab = potdict[("A", "B")] import sympy r, A, B, C = sympy.symbols("r A B C") ab_sympy = (A + B * r)**C var_dict = dict(r=2.5, A=3.0, B=2.0, C=2.0) expect = float(ab_sympy.subs(var_dict)) actual = ab.energy(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ab.potentialFunction, "deriv") ab_deriv = sympy.diff(ab_sympy, r) expect = float(ab_deriv.subs(var_dict)) actual = ab.potentialFunction.deriv(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ab.potentialFunction, "deriv2") ab_deriv2 = sympy.diff(ab_sympy, r, 2) expect = float(ab_deriv2.subs(var_dict)) actual = ab.potentialFunction.deriv2(var_dict["r"]) assert pytest.approx(expect) == actual ac = potdict[("A", "A")] r = sympy.symbols("r") # A-C = pow(sum(as.buck 100 0.2 0, as.polynomial 0 1 2), pow(sum(as.polynomial 0 0.1, as.polynomial 0 -0.05), as.constant 0.5)) # pow(as.polynomial 3.0 2.0, pow(as.constant 3, as.constant 2)) ac_sympy = (3.0 + 2.0 * r)**((2 * r)**0.5) var_dict = dict(r=2.5) actual = ac.energy(var_dict["r"]) expect = float(ac_sympy.subs(var_dict)) assert pytest.approx(expect) == actual assert hasattr(ac.potentialFunction, "deriv") ac_deriv = sympy.diff(ac_sympy, r) expect = float(ac_deriv.subs(var_dict)) actual = ac.potentialFunction.deriv(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ac.potentialFunction, "deriv2") ac_deriv2 = sympy.diff(ac_sympy, r, 2) expect = float(ac_deriv2.subs(var_dict)) actual = ac.potentialFunction.deriv2(var_dict["r"]) assert pytest.approx(expect) == actual ac = potdict[("A", "C")] r = sympy.symbols("r") A_buck, rho_buck, C_buck = sympy.symbols("A_buck rho_buck C_buck") # A-C = pow(sum(as.buck 100 0.2 0, as.polynomial 0 1 2), pow(sum(as.polynomial 0 0.1, as.polynomial 0 -0.05), as.constant 0.5)) ac_sympy = ((A_buck * sympy.exp(-r / rho_buck)) + (r + 2 * r**2))**((0.1 * r - 0.05 * r)**(0.5)) var_dict = dict(r=2.5, A_buck=1000, rho_buck=0.3) actual = ac.energy(var_dict["r"]) expect = float(ac_sympy.subs(var_dict)) assert pytest.approx(expect) == actual assert hasattr(ac.potentialFunction, "deriv") ac_deriv = sympy.diff(ac_sympy, r) expect = float(ac_deriv.subs(var_dict)) actual = ac.potentialFunction.deriv(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ac.potentialFunction, "deriv2") ac_deriv2 = sympy.diff(ac_sympy, r, 2) expect = float(ac_deriv2.subs(var_dict)) actual = ac.potentialFunction.deriv2(var_dict["r"]) assert pytest.approx(expect) == actual ac = potdict[("A", "D")] # A-D = pow(as.polynomial 1.0 2.0 3.0, as.polynomial 4 5 6, as.constant 3) r = sympy.symbols("r") p1, p2, p3, p4, p5, p6, p7 = sympy.symbols("p1 p2 p3 p4 p5 p6 p7") ac_sympy = ((p1 + p2 * r + p3 * r**2)**(p4 + p5 * r + p6 * r**2))**p7 var_dict = dict(r=2.5, p1=1, p2=2, p3=3, p4=0, p5=5, p6=0.1, p7=1.0 / 100.0) expect = float(ac_sympy.subs(var_dict)) actual = ac.energy(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ac.potentialFunction, "deriv") ac_deriv = sympy.diff(ac_sympy, r) expect = float(ac_deriv.subs(var_dict)) actual = ac.potentialFunction.deriv(var_dict["r"]) assert pytest.approx(expect) == actual assert hasattr(ac.potentialFunction, "deriv2") ac_deriv2 = sympy.diff(ac_sympy, r, 2) expect = float(ac_deriv2.subs(var_dict)) actual = ac.potentialFunction.deriv2(var_dict["r"]) assert pytest.approx(expect) == actual
def test_gulp_pair_configuration_tabulate(tmpdir): cfg_string = u"""[Tabulation] target : GULP dr: 0.01 cutoff : 15.0 [Pair] O-U = as.bornmayer 1761.775 0.35642 O-O = as.buck 9547.96 0.2192 32.0 """ gulp_input = u"""single cell 5.468 5.468 5.468 90.0 90.0 90.0 frac U 0 0 0 U 1/2 1/2 0 U 1/2 0 1/2 U 0 1/2 1/2 O 1/4 1/4 1/4 O 1/4 3/4 1/4 O 3/4 3/4 1/4 O 3/4 1/4 1/4 O 1/4 1/4 3/4 O 1/4 3/4 3/4 O 3/4 3/4 3/4 O 3/4 1/4 3/4 species U 4.0 O -2.0 include potentials.lib """ # First calculate the expected energy using GULP's built-in analytical potentials with tmpdir.join("potentials.lib").open("w") as potfile: potfile.write("buck\n") potfile.write("O O 9547.96 0.2192 32.0 15.0\n") potfile.write("O U 1761.775 0.35642 0.0 15.0\n") gulp_infile = io.StringIO(gulp_input) gulp_infile.seek(0) gulp_outfile = io.StringIO() runGULP(gulp_infile, gulp_outfile, cwd=tmpdir.strpath) gulp_outfile.seek(0) expect = extractGULPEnergy(gulp_outfile) tmpdir.join("potentials.lib").remove() assert not tmpdir.join("potentials.lib").exists() # Now build a potential model and tabulate it - then re-run the calculation and check the energies match. cfgobj = Configuration() tabulation = cfgobj.read(io.StringIO(cfg_string)) with tmpdir.join("potentials.lib").open("w") as potfile: tabulation.write(potfile) gulp_infile.seek(0) gulp_outfile = io.StringIO() runGULP(gulp_infile, gulp_outfile, cwd=tmpdir.strpath) gulp_outfile.seek(0) actual = extractGULPEnergy(gulp_outfile) assert pytest.approx(expect, abs=1e-3) == actual
def test_throw_configuration_exception(): cfg = u"""[Tabulation] target : eam_adp drho : 2.2770502180000001e-03 nrho : 10000 nr : 10000 dr : 6.2872099999999995e-04 [EAM-Embed] Al : as.zero Cu : as.zero [EAM-Density] Al : as.zero Cu : as.zero [Pair] Al-Al : as.zero Cu-Al : as.zero Cu-Cu : as.zero """ configuration = Configuration() with raises(ConfigurationException): configuration.read(io.StringIO(cfg)) cfg = u"""[Tabulation] target : eam_adp drho : 2.2770502180000001e-03 nrho : 10000 nr : 10000 dr : 6.2872099999999995e-04 [EAM-Embed] Al : as.zero Cu : as.zero [EAM-Density] Al : as.zero Cu : as.zero [Pair] Al-Al : as.zero Cu-Al : as.zero Cu-Cu : as.zero [EAM-ADP-Dipole] Al-Al : as.zero Al-Cu : as.zero Cu-Cu : as.zero """ with raises(ConfigurationException): configuration.read(io.StringIO(cfg)) cfg = u"""[Tabulation] target : eam_adp drho : 2.2770502180000001e-03 nrho : 10000 nr : 10000 dr : 6.2872099999999995e-04 [EAM-Embed] Al : as.zero Cu : as.zero [EAM-Density] Al : as.zero Cu : as.zero [Pair] Al-Al : as.zero Cu-Al : as.zero Cu-Cu : as.zero [EAM-ADP-Quadrupole] Al-Al : as.zero Al-Cu : as.zero Cu-Cu : as.zero """ with raises(ConfigurationException): configuration.read(io.StringIO(cfg))
def test_fs_AlFe(): cfg_file_path = _get_lammps_resource_dir().join("AlFe_setfl_fs.aspot") with cfg_file_path.open() as config_file: config_parser = ConfigParser(config_file) assert config_parser.tabulation.target == "setfl_fs" with cfg_file_path.open() as config_file: cfgobj = Configuration() tabulation = cfgobj.read(config_file) # Now check that the EAM potentials are as they should be. assert tabulation.target == "setfl_fs" assert 2 == len(tabulation.eam_potentials) epots = tabulation.eam_potentials epot_dict = dict(zip([p.species for p in epots], epots)) assert ["Al", "Fe"] == sorted([p.species for p in epots]) # Check the embedding functions alEmbedFunction_actual = epot_dict["Al"].embeddingFunction for rho in range(1, 300): expect = alEmbedFunction(rho) actual = alEmbedFunction_actual(rho) assert pytest.approx(expect) == actual feEmbedFunction_actual = epot_dict["Fe"].embeddingFunction for rho in range(1, 300): expect = feEmbedFunction(rho) actual = feEmbedFunction_actual(rho) assert pytest.approx(expect) == actual # print("{},{}".format(expect, actual)) # pytest.fail() # Check the density functions al_pot = epot_dict["Al"] dens_dict = al_pot.electronDensityFunction assert ["Al", "Fe"] == sorted(dens_dict.keys()) alAlDensFunction_actual = dens_dict["Al"] alFeDensFunction_actual = dens_dict["Fe"] for r in range(65): r = r * 0.1 expect = alAlDensFunction(r) actual = alAlDensFunction_actual(r) assert pytest.approx(expect) == actual expect = feAlDensFunction(r) actual = alFeDensFunction_actual(r) assert pytest.approx(expect) == actual fe_pot = epot_dict["Fe"] dens_dict = fe_pot.electronDensityFunction assert ["Al", "Fe"] == sorted(dens_dict.keys()) feAlDensFunction_actual = dens_dict["Al"] feFeDensFunction_actual = dens_dict["Fe"] for r in range(65): r = r * 0.1 expect = feAlDensFunction(r) actual = feAlDensFunction_actual(r) assert pytest.approx(expect) == actual expect = feFeDensFunction(r) actual = feFeDensFunction_actual(r) assert pytest.approx(expect) == actual # Check pair-potentials ppots = tabulation.potentials pairs = [(ppot.speciesA, ppot.speciesB) for ppot in ppots] assert [("Al", "Al"), ("Al", "Fe"), ("Fe", "Fe")] == pairs expect_dict = { ("Al", "Al"): ppfuncAlAl, ("Al", "Fe"): ppfuncAlFe, ("Fe", "Fe"): ppfuncFeFe } for actual_pp in ppots: expect_pp = expect_dict[(actual_pp.speciesA, actual_pp.speciesB)] for r in range(100): r = r / 10.0 expect = expect_pp(r) actual = actual_pp.energy(r) assert pytest.approx(expect) == actual
def test_pair_eam(tmpdir): cfg = u"""[Tabulation] target : excel_eam dr : 0.01 cutoff : 5 drho : 0.01 cutoff_rho : 5 [Pair] O-O : as.polynomial 0 1 Al-O : as.polynomial 0 2 [EAM-Density] O : as.polynomial 0 3 Al : as.polynomial 0 4 [EAM-Embed] O : as.polynomial 0 5 Al : as.polynomial 0 6 """ infile = io.StringIO(cfg) configuration = Configuration() tabulation = configuration.read(infile) assert type(tabulation) is Excel_EAMTabulation out_path = tmpdir.join("pair.xlsx") with tabulation.open_fp(out_path.strpath) as outfile: tabulation.write(outfile) wb = load_workbook(out_path.strpath) assert ["Pair", "EAM-Density", "EAM-Embed"] == wb.sheetnames # Check [Pair] table ws = wb["Pair"] # Check column headings assert ["r", "Al-O", "O-O"] == [c.value for c in next(ws.rows)] # Check number of table rows assert len(next(ws.columns)) == 502 # Check some values assert ws["A2"].value == 0.0 assert ws["B2"].value == 0.0 assert ws["C2"].value == 0.0 assert ws["A3"].value == 0.01 assert ws["B3"].value == pytest.approx(2 * 0.01) assert ws["C3"].value == pytest.approx(0.01) assert ws["A502"].value == 5.0 assert ws["B502"].value == pytest.approx(2.0 * 5.0) assert ws["C502"].value == pytest.approx(5.0) # Check [EAM-Density] table ws = wb["EAM-Density"] # Check column headings assert ["r", "Al", "O"] == [c.value for c in next(ws.rows)] # Check number of table rows assert len(next(ws.columns)) == 502 # Check some values assert ws["A2"].value == 0.0 assert ws["B2"].value == 0.0 assert ws["C2"].value == 0.0 assert ws["A3"].value == 0.01 assert ws["B3"].value == pytest.approx(4 * 0.01) assert ws["C3"].value == pytest.approx(3 * 0.01) assert ws["A502"].value == 5.0 assert ws["B502"].value == pytest.approx(4.0 * 5.0) assert ws["C502"].value == pytest.approx(3.0 * 5.0) # Check [EAM-Embed] table ws = wb["EAM-Embed"] # Check column headings assert ["rho", "Al", "O"] == [c.value for c in next(ws.rows)] # Check number of table rows assert len(next(ws.columns)) == 502 # Check some values assert ws["A2"].value == 0.0 assert ws["B2"].value == 0.0 assert ws["C2"].value == 0.0 assert ws["A3"].value == 0.01 assert ws["B3"].value == pytest.approx(6 * 0.01) assert ws["C3"].value == pytest.approx(5 * 0.01) assert ws["A502"].value == 5.0 assert ws["B502"].value == pytest.approx(6.0 * 5.0) assert ws["C502"].value == pytest.approx(5.0 * 5.0)
def test_badly_conditioned_spline(tmpdir): """Test for GULP 'badly_conditioned_spline' error encountered when writing Basak potential""" gulp_input = u"""single cell 5.468 5.468 5.468 90.0 90.0 90.0 frac U 0 0 0 U 1/2 1/2 0 U 1/2 0 1/2 U 0 1/2 1/2 O 1/4 1/4 1/4 O 1/4 3/4 1/4 O 3/4 3/4 1/4 O 3/4 1/4 1/4 O 1/4 1/4 3/4 O 1/4 3/4 3/4 O 3/4 3/4 3/4 O 3/4 1/4 3/4 species U 2.4 O -1.2 include potentials.lib""" # First calculate the expected energy using GULP's built-in analytical potentials with tmpdir.join("potentials.lib").open("w") as potfile: potfile.write("buck\n") potfile.write("O O 1633.01 0.3270196735 3.94879 10.0\n") potfile.write("U U 294.640906285709 0.327022 0.0 10.0\n") potfile.write("O U 693.650933805978 0.327022 0.0 10.0\n") potfile.write("\n") potfile.write("morse\n") potfile.write("O U 0.577189831995 1.65 2.369 10.0\n") gulp_infile = io.StringIO(gulp_input) gulp_infile.seek(0) gulp_outfile = io.StringIO() runGULP(gulp_infile, gulp_outfile, cwd=tmpdir.strpath) gulp_outfile.seek(0) expect = extractGULPEnergy(gulp_outfile) tmpdir.join("potentials.lib").remove() assert not tmpdir.join("potentials.lib").exists() # Now build a potential model and tabulate it - then re-run the calculation and check the energies match. aspot = io.StringIO(u""" [Tabulation] target : GULP cutoff : 10.0 nr : 1000 [Pair] O-O = as.buck 1633.010242995040 0.327022 3.948787 U-U = as.buck 294.640906285709 0.327022 0.0 O-U = sum(as.buck 693.650933805978 0.327022 0.0, as.morse 1.65 2.369 0.577189831995) """) aspot.seek(0) from atsim.potentials.config import Configuration tabulation = Configuration().read(aspot) with tmpdir.join("potentials.lib").open("w") as potfile: tabulation.write(potfile) gulp_infile.seek(0) gulp_outfile = io.StringIO() runGULP(gulp_infile, gulp_outfile, cwd=tmpdir.strpath) gulp_outfile.seek(0) actual = extractGULPEnergy(gulp_outfile) assert pytest.approx(expect, rel=1e-4) == actual tmpdir.join("potentials.lib").remove() assert not tmpdir.join("potentials.lib").exists()