Beispiel #1
0
def test_cell_tools():
    # test known values: simple cubic
    cell_a = 5.0
    cell = np.identity(3) * cell_a
    volume = cell_a**3.0
    cryst_const = np.array([cell_a] * 3 + [90.0] * 3)
    np.testing.assert_array_almost_equal(crys.cell2cc(cell), cryst_const)
    np.testing.assert_array_almost_equal(crys.cc2cell(cryst_const), cell)
    np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const))
    np.testing.assert_almost_equal(volume, crys.volume_cell(cell))
    np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)),
                                         cell)
    np.testing.assert_array_almost_equal(
        crys.cell2cc(crys.cc2cell(cryst_const)), cryst_const)

    # random
    #
    # volume : volume_cc() always returns positive values, whereas det() and
    #     volume_cell() may return the volume with negative sign but correct
    #     magnitude.
    # cell : A random cell does also have a random orientation in space. It
    #     does NOT conform the usual convention: a along x, b in x-y plane.
    #     However, the cryst_const and volume must be invariant.
    cell = np.random.rand(3, 3)
    cryst_const = crys.cell2cc(cell)
    volume = abs(np.linalg.det(cell))
    np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const))
    np.testing.assert_almost_equal(volume, abs(crys.volume_cell(cell)))
    # this must always fail for random cells
    try:
        np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)),
                                             cell)
    except AssertionError:
        pass
    # Here, we convert cryst_const to a *different* cell which conforms to the
    # orientation convention, and back to cryst_const.
    np.testing.assert_array_almost_equal(
        crys.cell2cc(crys.cc2cell(cryst_const)), cryst_const)

    # 3d
    cell = rand(100, 3, 3)
    cc = crys.cell2cc3d(cell, axis=0)
    vol_cell = np.abs(crys.volume_cell3d(cell, axis=0))
    vol_cc = crys.volume_cc3d(cc, axis=0)

    assert crys.cell2cc3d(cell, axis=0).shape == (100, 6)
    assert crys.cc2cell3d(cc, axis=0).shape == (100, 3, 3)

    assert vol_cc.shape == (100, )
    assert vol_cell.shape == (100, )
    aaae(vol_cell, vol_cc)
    aaae(crys.cell2cc3d(crys.cc2cell3d(cc)), cc)
def test_cell_tools():
    # test known values: simple cubic
    cell_a = 5.0
    cell = np.identity(3)*cell_a
    volume = cell_a**3.0
    cryst_const = np.array([cell_a]*3 + [90.0]*3)
    np.testing.assert_array_almost_equal(crys.cell2cc(cell), cryst_const)
    np.testing.assert_array_almost_equal(crys.cc2cell(cryst_const), cell)
    np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const))
    np.testing.assert_almost_equal(volume, crys.volume_cell(cell))
    np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)), cell)
    np.testing.assert_array_almost_equal(crys.cell2cc(crys.cc2cell(cryst_const)),
                                         cryst_const)
                                         
    # random
    #
    # volume : volume_cc() always returns positive values, whereas det() and
    #     volume_cell() may return the volume with negative sign but correct
    #     magnitude.
    # cell : A random cell does also have a random orientation in space. It
    #     does NOT conform the usual convention: a along x, b in x-y plane.
    #     However, the cryst_const and volume must be invariant.
    cell = np.random.rand(3,3)
    cryst_const = crys.cell2cc(cell)
    volume = abs(np.linalg.det(cell))
    np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const))
    np.testing.assert_almost_equal(volume, abs(crys.volume_cell(cell)))
    # this must always fail for random cells
    try:
        np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)), cell)
    except AssertionError:
        pass
    # Here, we convert cryst_const to a *different* cell which conforms to the
    # orientation convention, and back to cryst_const.
    np.testing.assert_array_almost_equal(crys.cell2cc(crys.cc2cell(cryst_const)),
                                         cryst_const)

    # 3d
    cell = rand(100,3,3)
    cc = crys.cell2cc3d(cell, axis=0)
    vol_cell = np.abs(crys.volume_cell3d(cell, axis=0))
    vol_cc = crys.volume_cc3d(cc, axis=0)

    assert crys.cell2cc3d(cell, axis=0).shape == (100,6)
    assert crys.cc2cell3d(cc, axis=0).shape == (100,3,3)
    
    assert vol_cc.shape == (100,)
    assert vol_cell.shape == (100,)
    aaae(vol_cell, vol_cc)
    aaae(crys.cell2cc3d(crys.cc2cell3d(cc)), cc)
Beispiel #3
0
    def _get_random_struct(self):
        """Generate random cryst_const and atom coords.

        Returns
        -------
        Structure

        Raises
        ------
        RandomStructureFail
        """
        self.cell = cc2cell(self.get_random_cryst_const())
        self.coords_frac = np.empty((self.natoms, 3))
        self.counters['coords'] = []
        for iatom in range(self.natoms):
            if iatom == 0:
                cnt = 1
                self._add_random_atom(iatom)
            else:
                cnt = 1
                while cnt <= self.atom_maxtry:
                    self._add_random_atom(iatom)
                    if self._atoms_too_close(iatom):
                        self._add_random_atom(iatom)
                        cnt += 1
                    else:
                        break
            self.counters['coords'].append(cnt)
            if cnt > self.atom_maxtry:
                raise RandomStructureFail("failed to create random coords for "
                                          "iatom=%i of %i" %
                                          (iatom, self.natoms - 1))
        st = Structure(symbols=self.symbols,
                       coords_frac=self.coords_frac,
                       cell=self.cell)
        return st
Beispiel #4
0
    def _get_random_struct(self):
        """Generate random cryst_const and atom coords.

        Returns
        -------
        Structure

        Raises
        ------
        RandomStructureFail
        """
        self.cell = cc2cell(self.get_random_cryst_const())
        self.coords_frac = np.empty((self.natoms, 3))
        self.counters['coords'] = []
        for iatom in range(self.natoms):
            if iatom == 0:
                cnt = 1
                self._add_random_atom(iatom)
            else:                
                cnt = 1
                while cnt <= self.atom_maxtry:
                    self._add_random_atom(iatom)
                    if self._atoms_too_close(iatom):
                        self._add_random_atom(iatom)
                        cnt += 1
                    else:
                        break
            self.counters['coords'].append(cnt)
            if cnt > self.atom_maxtry:
                raise RandomStructureFail("failed to create random coords for "
                                          "iatom=%i of %i" %(iatom,
                                                             self.natoms-1))
        st = Structure(symbols=self.symbols,
                       coords_frac=self.coords_frac,
                       cell=self.cell)        
        return st
Beispiel #5
0
def test_ibrav():
    # bogus
    aa = 3.0
    bb = 4.0
    cc = 5.0
    alpha = 66*pi/180
    beta = 77*pi/180
    gamma = 88*pi/180

    ibrav = 1
    celldm = [aa] + [None]*5
    ibrav2cell(ibrav, celldm)
    
    ibrav = 2
    celldm = [aa] + [None]*5
    ibrav2cell(ibrav, celldm)
    
    ibrav = 3
    celldm = [aa] + [None]*5
    ibrav2cell(ibrav, celldm)
    
    ibrav = 4
    celldm = [aa, None, cc/aa, None, None, None]
    ibrav2cell(ibrav, celldm)
    
    ibrav = 5
    celldm = [aa, None, None, cos(alpha), None, None]
    ibrav2cell(ibrav, celldm)
    
    ibrav = 6
    celldm = [aa, None, cc/aa, None, None, None]
    ibrav2cell(ibrav, celldm)
    
    ibrav = 7
    celldm = [aa, None, cc/aa, None, None, None]
    ibrav2cell(ibrav, celldm)
    
    ibrav = 8
    celldm = [aa, bb/aa, cc/aa, None, None, None]
    ibrav2cell(ibrav, celldm)
    
    ibrav = 9
    celldm = [aa, bb/aa, cc/aa, None, None, None]
    ibrav2cell(ibrav, celldm)
    
    ibrav = 10
    celldm = [aa, bb/aa, cc/aa, None, None, None]
    ibrav2cell(ibrav, celldm)
    
    ibrav = 11
    celldm = [aa, bb/aa, cc/aa, None, None, None]
    ibrav2cell(ibrav, celldm)
    
    # celldm(4)=cos(ab) in doc!?
    ibrav = 12 
##    celldm = [aa, bb/aa, cc/aa, cos(alpha), None, None]
    celldm = [aa, bb/aa, cc/aa, None, None, cos(gamma)]
    ibrav2cell(ibrav, celldm)
    
    # celldm(4)=cos(ab) in doc!?
    ibrav = 13
##    celldm = [aa, bb/aa, cc/aa, cos(alpha), None, None]
    celldm = [aa, bb/aa, cc/aa, None, None, cos(gamma)]
    ibrav2cell(ibrav, celldm)
    
    # WOHOO!
    ibrav = 14
    celldm = [aa, bb/aa, cc/aa, cos(alpha), cos(beta), cos(gamma)]
    cell=ibrav2cell(ibrav, celldm)
    np.testing.assert_array_almost_equal(cell, 
                                         crys.cc2cell(crys.celldm2cc(celldm)))
Beispiel #6
0
def test_ibrav():
    # bogus
    aa = 3.0
    bb = 4.0
    cc = 5.0
    alpha = 66 * pi / 180
    beta = 77 * pi / 180
    gamma = 88 * pi / 180

    ibrav = 1
    celldm = [aa] + [None] * 5
    ibrav2cell(ibrav, celldm)

    ibrav = 2
    celldm = [aa] + [None] * 5
    ibrav2cell(ibrav, celldm)

    ibrav = 3
    celldm = [aa] + [None] * 5
    ibrav2cell(ibrav, celldm)

    ibrav = 4
    celldm = [aa, None, cc / aa, None, None, None]
    ibrav2cell(ibrav, celldm)

    ibrav = 5
    celldm = [aa, None, None, cos(alpha), None, None]
    ibrav2cell(ibrav, celldm)

    ibrav = 6
    celldm = [aa, None, cc / aa, None, None, None]
    ibrav2cell(ibrav, celldm)

    ibrav = 7
    celldm = [aa, None, cc / aa, None, None, None]
    ibrav2cell(ibrav, celldm)

    ibrav = 8
    celldm = [aa, bb / aa, cc / aa, None, None, None]
    ibrav2cell(ibrav, celldm)

    ibrav = 9
    celldm = [aa, bb / aa, cc / aa, None, None, None]
    ibrav2cell(ibrav, celldm)

    ibrav = 10
    celldm = [aa, bb / aa, cc / aa, None, None, None]
    ibrav2cell(ibrav, celldm)

    ibrav = 11
    celldm = [aa, bb / aa, cc / aa, None, None, None]
    ibrav2cell(ibrav, celldm)

    # celldm(4)=cos(ab) in doc!?
    ibrav = 12
    ##    celldm = [aa, bb/aa, cc/aa, cos(alpha), None, None]
    celldm = [aa, bb / aa, cc / aa, None, None, cos(gamma)]
    ibrav2cell(ibrav, celldm)

    # celldm(4)=cos(ab) in doc!?
    ibrav = 13
    ##    celldm = [aa, bb/aa, cc/aa, cos(alpha), None, None]
    celldm = [aa, bb / aa, cc / aa, None, None, cos(gamma)]
    ibrav2cell(ibrav, celldm)

    # WOHOO!
    ibrav = 14
    celldm = [aa, bb / aa, cc / aa, cos(alpha), cos(beta), cos(gamma)]
    cell = ibrav2cell(ibrav, celldm)
    np.testing.assert_array_almost_equal(cell,
                                         crys.cc2cell(crys.celldm2cc(celldm)))