from pymatgen import MPRester, Structure from pymatgen.command_line.gulp_caller import GulpCaller, GulpIO from matplotlib import pyplot mpr = MPRester() mono: Structure = mpr.get_structure_by_material_id('mp-352') ortho: Structure = mpr.get_structure_by_material_id('mp-685097') tetra: Structure = mpr.get_structure_by_material_id('mp-1018721') gio = GulpIO() gc = GulpCaller() for i in [mono, ortho, tetra]: arr = [] arr2 = [] i.apply_strain(-.05) for j in range(15): gin = gio.buckingham_input(i, ['conv'], library='morse.lib', alib=True) gout = gc.run(gin) arr.append(i.density) arr2.append(gio.get_energy(gout)) i.apply_strain(-.01) i.to('POSCAR', i.get_space_group_info()[0].replace('/', '_') + '.vasp') if i.get_space_group_info()[0] == 'P4_2/nmc': pyplot.plot(arr, [r * 2 for r in arr2], label=i.get_space_group_info()[0]) else: pyplot.plot(arr, arr2, label=i.get_space_group_info()[0]) pyplot.legend() pyplot.show()
class GulpIOTest(unittest.TestCase): _multiprocess_shared_ = True def setUp(self): p = Poscar.from_file( os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR.Al12O18"), check_for_POTCAR=False ) self.structure = p.structure self.gio = GulpIO() def test_keyword_line_with_correct_keywords(self): kw = ("defect", "property") inp_str = self.gio.keyword_line(*kw) for word in kw: self.assertIn(word, inp_str) def test_structure_lines_default_options(self): inp_str = self.gio.structure_lines(self.structure) self.assertIn("cell", inp_str) self.assertIn("frac", inp_str) self.assertIn("space", inp_str) def test_structure_lines_no_unitcell(self): inp_str = self.gio.structure_lines(self.structure, cell_flg=False) self.assertNotIn("cell", inp_str) def test_structure_lines_no_frac_coords(self): inp_str = self.gio.structure_lines( self.structure, cell_flg=False, frac_flg=False ) self.assertNotIn("cell", inp_str) self.assertIn("cart", inp_str) @unittest.skip("Not Implemented yet") def test_specie_potential(self): pass @unittest.expectedFailure def test_library_line_explicit_path(self): gin = self.gio.library_line( "/Users/mbkumar/Research/Defects/GulpExe/Libraries/catlow.lib" ) self.assertIn("lib", gin) def test_library_line_wrong_file(self): with self.assertRaises(GulpError): gin = self.gio.library_line("temp_to_fail.lib") def test_buckingham_potential(self): mgo_latt = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]] mgo_specie = ["Mg", "O"] * 4 mgo_frac_cord = [ [0, 0, 0], [0.5, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0], [0.5, 0, 0.5], [0, 0, 0.5], [0, 0.5, 0.5], [0.5, 0.5, 0.5], ] mgo_uc = Structure(mgo_latt, mgo_specie, mgo_frac_cord, True, True) gin = self.gio.buckingham_potential(mgo_uc) self.assertIn("specie", gin) self.assertIn("buck", gin) self.assertIn("spring", gin) self.assertIn("Mg core", gin) self.assertIn("O core", gin) self.assertIn("O shel", gin) gin = self.gio.buckingham_potential(self.structure) self.assertIn("specie", gin) self.assertIn("buck", gin) self.assertIn("spring", gin) def test_buckingham_input(self): mgo_latt = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]] mgo_specie = ["Mg", "O"] * 4 mgo_frac_cord = [ [0, 0, 0], [0.5, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0], [0.5, 0, 0.5], [0, 0, 0.5], [0, 0.5, 0.5], [0.5, 0.5, 0.5], ] mgo_uc = Structure(mgo_latt, mgo_specie, mgo_frac_cord, True, True) gin = self.gio.buckingham_input(mgo_uc, keywords=("optimise", "conp")) self.assertIn("optimise", gin) self.assertIn("cell", gin) self.assertIn("specie", gin) self.assertIn("buck", gin) self.assertIn("spring", gin) self.assertIn("Mg core", gin) self.assertIn("O core", gin) self.assertIn("O shel", gin) # Improve the test def test_tersoff_potential(self): mgo_latt = [[4.212, 0, 0], [0, 4.212, 0], [0, 0, 4.212]] mgo_specie = ["Mg", "O"] * 4 mgo_frac_cord = [ [0, 0, 0], [0.5, 0, 0], [0.5, 0.5, 0], [0, 0.5, 0], [0.5, 0, 0.5], [0, 0, 0.5], [0, 0.5, 0.5], [0.5, 0.5, 0.5], ] mgo_uc = Structure(mgo_latt, mgo_specie, mgo_frac_cord, True, True) gin = self.gio.tersoff_potential(mgo_uc) self.assertIn("specie", gin) self.assertIn("Mg core", gin) def test_get_energy(self): # Output string obtained from running GULP on a terminal out_str = """ Components of energy : -------------------------------------------------------------------------------- Interatomic potentials = 5.61135426 eV Monopole - monopole (real) = -4.34238722 eV Monopole - monopole (recip)= -43.45344934 eV Monopole - monopole (total)= -47.79583656 eV -------------------------------------------------------------------------------- Total lattice energy : Primitive unit cell = -42.18448230 eV Non-primitive unit cell = -168.73792920 eV -------------------------------------------------------------------------------- Total lattice energy (in kJmol-1): Primitive unit cell = -4070.1577 kJ/(mole unit cells) Non-primitive unit cell = -16280.6308 kJ/(mole unit cells) -------------------------------------------------------------------------------- Components of energy : -------------------------------------------------------------------------------- Interatomic potentials = 6.79846039 eV Monopole - monopole (real) = -4.45761741 eV Monopole - monopole (recip)= -44.60653603 eV Monopole - monopole (total)= -49.06415344 eV -------------------------------------------------------------------------------- Total lattice energy : Primitive unit cell = -42.26569304 eV Non-primitive unit cell = -169.06277218 eV -------------------------------------------------------------------------------- Total lattice energy (in kJmol-1): Primitive unit cell = -4077.9933 kJ/(mole unit cells) Non-primitive unit cell = -16311.9732 kJ/(mole unit cells) --------------------------------------------------------------------------------""" energy = self.gio.get_energy(out_str) self.assertEqual(energy, -169.06277218) def test_get_relaxed_structure(self): # Output string obtained from running GULP on a terminal with open(os.path.join(PymatgenTest.TEST_FILES_DIR, "example21.gout"), "r") as fp: out_str = fp.read() struct = self.gio.get_relaxed_structure(out_str) self.assertIsInstance(struct, Structure) self.assertEqual(8, len(struct.sites)) self.assertEqual(4.212, struct.lattice.a) self.assertEqual(90, struct.lattice.alpha) @unittest.skip("Test later") def test_tersoff_inpt(self): gin = self.gio.tersoff_input(self.structure)
from pymatgen.command_line.gulp_caller import GulpCaller, GulpIO from matplotlib import pyplot import numpy as np gio = GulpIO() gc = GulpCaller() arr = [] for i in np.linspace(0.5, 1, 100): body = f'''conv #cell #1 1 10 90 90 90 cart O core 0 0 -{i} O core 0 0 {i} Hf core 0 0 0 species Hf core 2.4 O core -1.2 general 0 12 Hf core O core 0 0 1.0 1. ''' gout = gc.run(body) e = gio.get_energy(gout) arr.append(e) pyplot.plot(arr) pyplot.show()
for j, i in enumerate(ortho.sites): if str(i.specie) == 'Hf': ortho.replace(j, species=Specie('Zr')) break for j, i in enumerate(ortho.sites): if str(i.specie) == 'Hf': ortho.replace(j, species=Specie('Zr')) break gio = GulpIO() gc = GulpCaller() for i in [mono, ortho, tetra]: arr = [] arr2 = [] i.apply_strain(.05) for j in range(10): gin = gio.buckingham_input(i, ['conv'], library='morse.lib', alib=True) gout = gc.run(gin) print(gout) arr.append(i.density) arr2.append(gio.get_energy(gout) / i.volume) i.apply_strain(-.01) i.to('POSCAR', i.get_space_group_info()[0].replace('/', '_') + '.vasp') if i.get_space_group_info()[0] == 'P4_2/nmc': pyplot.plot(arr, [r * 2 for r in arr2], label=i.get_space_group_info()[0]) else: pyplot.plot(arr, arr2, label=i.get_space_group_info()[0]) pyplot.legend() pyplot.show()
from pymatgen.command_line.gulp_caller import GulpIO except: print 'You should install pymatgen first.' exit(0) import sys from monty.io import zopen if __name__ == '__main__': Gulp = GulpIO() #change the Structure #Cry_Str = Structure.from_file("POSCAR") #gulpinput = Gulp.structure_lines(structure = Cry_Str) #with open('gulpinput','w') as f: #f.write(gulpinput) #exit(0) #output the energy with zopen('log', "rt") as f: contents = f.read() energy = Gulp.get_energy(contents) print energy #output the relaxed structure with zopen('log', "rt") as f: contents = f.read() Opt_Str = Gulp.get_relaxed_structure(contents) Vasp_Str = Poscar(Opt_Str) Vasp_Str.write_file('out.vasp')