Exemplo n.º 1
0
    def test_simple_input(self):
        # Reference structure
        s = ("acell 1 1 1 rprim 1 0 0 0 1 0 0 0 1 natom 1 "
             "ntypat 1 typat 1 znucl 14 xred 0 0 0 ")
        inp = AbinitInputFile.from_string(s)
        repr(inp); str(inp)
        assert inp.to_string(verbose=1)
        assert inp._repr_html_()
        si1_structure = inp.structure
        assert inp.ndtset == 1 and len(si1_structure) == 1 and si1_structure.formula == "Si1"

        # Default values for acell (3*1) and rprim (np.eye(3))
        s = "natom 1 ntypat 1 typat 1 znucl 14 xred 0 0 0"
        inp = AbinitInputFile.from_string(s)
        assert inp.structure == si1_structure

        # Same structure but with * syntax.
        s = "acell 3*1 natom 1 ntypat 1 typat 1 znucl 14 xred 3*0e0"
        inp = AbinitInputFile.from_string(s)
        assert inp.structure == si1_structure
        assert not inp.has_multi_structures

        # xcart instead of xred and more variables using * syntax.
        s = "acell 1 2*1 natom 1*1 ntypat 1*1 typat 1*1 znucl *14 xcart 0d0 0d0 0d0"
        inp = AbinitInputFile.from_string(s)
        assert inp.structure == si1_structure

        # acell and rprimd with unit.
        s = ("acell 1 2*1 Bohr rprim 1 0 0 0 1 0 0 0 1 Bohr natom 1 "
             "ntypat 1 typat *1 znucl 1*14 xangst 0 0 0")
        with AbinitInputFile.from_string(s) as inp:
            assert inp.structure == si1_structure
            if self.has_nbformat():
                inp.write_notebook(nbpath=self.get_tmpname(text=True))
Exemplo n.º 2
0
    def test_input_with_serie(self):
        # Test arithmetic and geometric series with ecut.
        s = """
          ndtset 3
          natom 2 ntypat 1 znucl 1 typat 2*1
          acell 10 10 10 xcart -0.7 0.0 0.0 0.7 0.0 0.0
          ecut: 10 ecut+ 5
          pawecutdg: 2 pawecutdg* 3
        """
        inp = AbinitInputFile.from_string(s)
        assert inp.ndtset == 3
        self.assertArrayEqual([dt["ecut"] for dt in inp.datasets], [10, 15, 20])
        self.assertArrayEqual([dt["pawecutdg"] for dt in inp.datasets], [2, 6, 18])
        repr(inp); str(inp)

        # Test arithmetic series with xcart.
        s = """
          ndtset 2
          natom 2 ntypat 1 znucl 1 typat 2*1
          acell 1 1 1
          xcart: -0.7 0.0 0.0 0.7 0.0 0.0
          xcart+ -0.1 0.0 0.0 0.1 0.0 0.0
        """
        inp = AbinitInputFile.from_string(s)
        assert inp.ndtset == 2 and inp.structure is None

        s0, s1 = inp.datasets[0].structure, inp.datasets[1].structure
        self.assert_almost_equal(s0.cart_coords.ravel() / bohr_to_ang, [-0.7, 0, 0, 0.7, 0, 0])
        self.assert_almost_equal(s1.cart_coords.ravel() / bohr_to_ang, [-0.8, 0, 0, 0.8, 0, 0])
        str(inp)
Exemplo n.º 3
0
    def test_simple_input(self):
        # Reference structure
        s = ("acell 1 1 1 rprim 1 0 0 0 1 0 0 0 1 natom 1"
             "ntypat 1 typat 1 znucl 14 xred 0 0 0")
        inp = AbinitInputFile.from_string(s)

        print(inp)
        si1_structure = inp.structure
        assert inp.ndtset == 1 and len(si1_structure) == 1 and si1_structure.formula == "Si1"

        # Default values for acell (3*1) and rprim (np.eye(3))
        s = "natom 1" "ntypat 1 typat 1 znucl 14 xred 0 0 0"
        inp = AbinitInputFile.from_string(s)
        assert inp.structure == si1_structure

        # Same structure but with * syntax.
        s = "acell 3*1 natom 1 ntypat 1 typat 1 znucl 14 xred 0 0 0"
        inp = AbinitInputFile.from_string(s)
        assert inp.structure == si1_structure

        # xcart instead of xred and more variables using * syntax. 
        s = "acell 1 2*1 natom 1*1 ntypat 1*1 typat 1*1 znucl 1*14 xcart 0 0 0"
        inp = AbinitInputFile.from_string(s)
        assert inp.structure == si1_structure

        # acell and rprimd with unit.
        s = "acell 1 2*1 Bohr rprim 1 0 0 0 1 0 0 0 1 Bohr natom 1*1 ntypat 1*1 typat 1*1 znucl 1*14 xangst 0 0 0"
        inp = AbinitInputFile.from_string(s)
        assert inp.structure == si1_structure
Exemplo n.º 4
0
    def test_input_with_datasets(self):
        # H2 molecule in a big box
        s = """
          ndtset 2
          natom 2 ntypat 1 znucl 1 typat 2*1
          acell1 10 10 10  # this is a comment
          acell2 20 20 20
          xcart -0.7 0.0 0.0 0.7 0.0 0.0
        """
        inp = AbinitInputFile.from_string(s)
        assert inp.ndtset == 2
        dt0 = inp.datasets[0]
        repr(dt0); str(dt0)
        assert dt0.to_string(verbose=2)
        assert dt0._repr_html_()
        s0, s1 = inp.datasets[0].structure, inp.datasets[1].structure
        assert s0 != s1
        assert s1.volume == 8 * s0.volume
        repr(inp); str(inp)

        # same input but with global acell
        s = """
          ndtset 2
          natom 2 ntypat 1 znucl 1 typat 2*1
          acell  10 10 10 ! this is a comment
          acell2 20 20 20 ! bohrs
          xcart -0.7 0.0 0.0 0.7 0.0 0.0
        """
        inp = AbinitInputFile.from_string(s)
        assert s0 == inp.datasets[0].structure
        assert s1 == inp.datasets[1].structure
        repr(inp); str(inp)

        d = inp.datasets[1].get_vars()
        assert "natom" not in d and len(d) == 0

        # same input in terms of an arithmetic series in acell
        s = """
          ndtset 2
          natom 2 ntypat 1 znucl 1 typat 2*1
          acell:  10 10 10
          acell+ 10 10 10 # bohrs
          xcart -0.7 0.0 0.0 0.7 0.0 0.0
        """
        inp = AbinitInputFile.from_string(s)
        assert s0 == inp.datasets[0].structure
        assert s1 == inp.datasets[1].structure
        str(inp)
Exemplo n.º 5
0
    def test_input_with_datasets(self):
        # H2 molecule in a big box
        s = """
          ndtset 2
          natom 2 ntypat 1 znucl 1 typat 2*1
          acell1 10 10 10  # this is a comment
          acell2 20 20 20
          xcart -0.7 0.0 0.0 0.7 0.0 0.0
        """
        inp = AbinitInputFile.from_string(s)
        assert inp.ndtset == 2
        s0, s1 = inp.datasets[0].structure, inp.datasets[1].structure
        assert s0 != s1
        assert s1.volume == 8 * s0.volume
        print(inp)

        # same input but with global acell
        s = """
          ndtset 2
          natom 2 ntypat 1 znucl 1 typat 2*1
          acell  10 10 10 ! this is a comment
          acell2 20 20 20 ! bohrs
          xcart -0.7 0.0 0.0 0.7 0.0 0.0
        """
        inp = AbinitInputFile.from_string(s)
        assert s0 == inp.datasets[0].structure
        assert s1 == inp.datasets[1].structure
        print(inp)

        # same input in terms of an arithmetic series in acell
        s = """
          ndtset 2
          natom 2 ntypat 1 znucl 1 typat 2*1
          acell:  10 10 10
          acell+ 10 10 10 # bohrs
          xcart -0.7 0.0 0.0 0.7 0.0 0.0
        """
        inp = AbinitInputFile.from_string(s)
        assert s0 == inp.datasets[0].structure
        assert s1 == inp.datasets[1].structure
        print(inp)
Exemplo n.º 6
0
    def test_tricky_inputs(self):
        """Testing tricky inputs"""
        s = """\
# define kpt mesh
kptrlatt 2 2 -2
        -2 2 -2
        -2 2  2

#definition of the elementary cell
natom 2
ntypat 2
znucl 31 1*33
typat 1*1 2

acell 3*5.6533 angstrom # expt value

rprim 0   1/2 1/2
      1/2 0   1/2
      1/2 1/2 0

include "foo"
xred 3*0 3*1/4
"""
        inp = AbinitInputFile.from_string(s)
        assert inp.ndtset == 1 and inp.structure is not None and len(inp.structure) == 2
        self.assertArrayEqual(inp.structure[0].frac_coords, [0, 0, 0])
        self.assertArrayEqual(inp.structure[0].specie.symbol, "Ga")
        self.assertArrayEqual(inp.structure[1].frac_coords, [1/4, 1/4, 1/4])
        self.assertArrayEqual(inp.structure[1].specie.symbol, "As")
        mat = 5.6533 * np.array([0, 1/2, 1/2, 1/2, 0, 1/2, 1/2, 1/2, 0])
        mat.shape = (3, 3)
        self.assertArrayEqual(inp.structure[1].lattice.matrix, mat)

        # tutorial/input/tbase2_1
        # 2 datasets with different natom (should use typat[:1] in 2nd dataset)
        s = """\
ndtset 2

#Definition of the unit cell and ecut,
#for which one will have to make a convergence study
acell 10 10 10
ecut 10

   natom1  2             # There are two atoms
   xcart1  -0.7  0.0 0.0 # The starting values of the
            0.7  0.0 0.0 # atomic coordinates

#Second dataset : get the total energy of the isolated atom
   natom2  1             # There is one atom
   xcart2  0.0 0.0 0.0   # The atom is located at the origin

#Definition of the atom types
ntypat 1          # There is only one type of atom
znucl 1           # The keyword "znucl" refers to the atomic number of the

#Definition of the atoms
typat 1 1         # For the first dataset, both numbers will be read,
                  # while for the second dataset, only one number will be read
"""
        inp = AbinitInputFile.from_string(s)
        assert inp.ndtset == 2 and inp.structure is None
        assert len(inp.datasets[0].structure) == 2
        assert len(inp.datasets[1].structure) == 1
        str(inp)
Exemplo n.º 7
0
    def test_enforce_znucl_and_typat(self):
        """Test the order of typat and znucl in the Abinit input when  enforce_typat, enforce_znucl are used."""

        # Ga  Ga1  1  0.33333333333333  0.666666666666667  0.500880  1.0
        # Ga  Ga2  1  0.66666666666667  0.333333333333333  0.000880  1.0
        # N  N3  1  0.333333333333333  0.666666666666667  0.124120  1.0
        # N  N4  1  0.666666666666667  0.333333333333333  0.624120  1.0
        gan2 = Structure.from_file(abidata.cif_file("gan2.cif"))
        pseudos = abidata.pseudos("31ga.pspnc", "7n.pspnc")

        def_znucl = [31, 7]
        def_typat = [1, 1, 2, 2]

        # Build AbinitInput with default sorting algorithm for znucl and typat
        def_inp = AbinitInput(gan2, pseudos)
        def_dict = def_inp.as_dict()
        assert def_dict["enforce_znucl"] is None
        assert def_dict["enforce_typat"] is None

        # Build AbinitInput with specific znucl and typat.
        enforce_znucl = [7 ,31]
        enforce_typat = [2, 2, 1, 1]
        enf_inp = AbinitInput(gan2, pseudos, enforce_znucl=enforce_znucl, enforce_typat=enforce_typat)

        enf_dict = enf_inp.as_dict()
        self.assert_equal(enf_dict["enforce_znucl"], enforce_znucl)
        self.assert_equal(enf_dict["enforce_typat"], enforce_typat)

        # Parser the input string and make sure znucl and typat are what we expect.
        from abipy.abio.abivars import AbinitInputFile
        def_string = def_inp.to_string()
        enf_string = enf_inp.to_string()
        #print("\ndef_string\n", def_string, "\nenf_string:\n", enf_string)
        assert def_string != enf_string
        def_inpfile = AbinitInputFile.from_string(def_string)
        enf_inpfile = AbinitInputFile.from_string(enf_string)
        assert def_inpfile.structure == enf_inpfile.structure

        self.assert_equal(def_inpfile.datasets[0]["znucl"], " ".join(str(e) for e in def_znucl))
        self.assert_equal(def_inpfile.datasets[0]["typat"], " ".join(str(t) for t in def_typat))
        self.assert_equal(enf_inpfile.datasets[0]["znucl"], " ".join(str(e) for e in enforce_znucl))
        self.assert_equal(enf_inpfile.datasets[0]["typat"], " ".join(str(t) for t in enforce_typat))

        other_enf_dict = AbinitInput.from_dict(enf_dict).as_dict()
        self.assert_equal(other_enf_dict["enforce_znucl"], enforce_znucl)
        self.assert_equal(other_enf_dict["enforce_typat"], enforce_typat)

        # Make sure we detect wrong calls.
        with self.assertRaises(ValueError):
            AbinitInput(gan2, pseudos, enforce_znucl=[1,], enforce_typat=enforce_typat)
        with self.assertRaises(ValueError):
            AbinitInput(gan2, pseudos, enforce_znucl=enforce_znucl, enforce_typat=[1, 2])

        # Make sure enforce flags are propagated in MultiDataset and other methods.
        inputs = enf_inp.product("ngkpt", "tsmear", [[2, 2, 2], [4, 4, 4]], [0.1, 0.2, 0.3])
        for inp in inputs:
            self.assert_equal(inp.enforce_znucl, enforce_znucl)
            self.assert_equal(inp.enforce_typat, enforce_typat)

        multi = MultiDataset.from_inputs(inputs)
        for znucl in multi.enforce_znucl:
            self.assert_equal(znucl, enforce_znucl)
        for typat in multi.enforce_typat:
            self.assert_equal(typat, enforce_typat)