Example #1
0
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)
Example #2
0
if __name__ == '__main__':
    Gulp = GulpIO()

    num = int(sys.argv[1])
    if num == 1:
        #write input file of gulp
        Cry_Str = Structure.from_file("POSCAR")
        with open('gulpinput', 'w') as f:
            f.write(Gulp.keyword_line('opti conj conp nosymmetry qok'))
            f.write(Gulp.keyword_line('switch_min bfgs gnorm 0.5\n'))

            gulpinput = Gulp.structure_lines(structure=Cry_Str, symm_flg=False)
            f.write(gulpinput)

            f.write(Gulp.keyword_line('maxcyc 500'))
            f.write(Gulp.keyword_line('library self_build.lib'))
            f.write(Gulp.keyword_line('dump every gulpopt'))

    if num == 2:
        #output the energy
        energy = gulp_average_energy()
        print energy

    if num == 3:
        #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')