Exemplo n.º 1
0
    def setUp(self):
        element_profile = {'Ni': {'r': 0.5, 'w': 1}}
        describer1 = BispectrumCoefficients(rcutfac=4.1, twojmax=8,
                                            element_profile=element_profile,
                                            quadratic=False,
                                            pot_fit=True)
        model1 = LinearModel(describer=describer1)
        model1.model.coef_ = coeff
        model1.model.intercept_ = intercept
        snap1 = SNAPotential(model=model1)
        snap1.specie = Element('Ni')
        self.ff_settings1 = snap1

        describer2 = BispectrumCoefficients(rcutfac=4.1, twojmax=8,
                                            element_profile=element_profile,
                                            quadratic=True,
                                            pot_fit=True)
        model2 = LinearModel(describer=describer2)
        model2.model.coef_ = coeff
        model2.model.intercept_ = intercept
        snap2 = SNAPotential(model=model2)
        snap2.specie = Element('Ni')
        self.ff_settings2 = snap2

        self.struct = Structure.from_spacegroup('Fm-3m',
                                                Lattice.cubic(3.506),
                                                ['Ni'], [[0, 0, 0]])
Exemplo n.º 2
0
    def test_subscripts():
        def from_lmp_doc(twojmax, diagonal):
            js = []
            for j1 in range(0, twojmax + 1):
                if diagonal == 2:
                    js.append([j1, j1, j1])
                elif diagonal == 1:
                    for j in range(0, min(twojmax, 2 * j1) + 1, 2):
                        js.append([j1, j1, j])
                elif diagonal == 0:
                    for j2 in range(0, j1 + 1):
                        for j in range(j1 - j2, min(twojmax, j1 + j2) + 1, 2):
                            js.append([j1, j2, j])
                elif diagonal == 3:
                    for j2 in range(0, j1 + 1):
                        for j in range(j1 - j2, min(twojmax, j1 + j2) + 1, 2):
                            if j >= j1:
                                js.append([j1, j2, j])
            return js

        profile = {"Mo": {"r": 0.5, "w": 1}}
        for d in range(4):
            for tjm in range(11):
                bc = BispectrumCoefficients(1.0,
                                            twojmax=tjm,
                                            element_profile=profile,
                                            quadratic=False,
                                            diagonalstyle=d)
                np.testing.assert_equal(bc.subscripts, from_lmp_doc(tjm, d))
Exemplo n.º 3
0
    def from_config(param_file, coeff_file, **kwargs):
        """
        Initialize potentials with parameters file and coefficient file.

        Args:
            param_file (str): The file storing the configuration of potentials.
            coeff_file (str): The file storing the coefficients of potentials.

        Return:
            SNAPotential.
        """
        with open(coeff_file) as f:
            coeff_lines = f.readlines()
        coeff_lines = [
            line for line in coeff_lines if not line.startswith('#')
        ]
        specie, r, w = coeff_lines[1].split()
        r, w = float(r), int(w)
        element_profile = {specie: {'r': r, 'w': w}}

        rcut_pattern = re.compile('rcutfac (.*?)\n', re.S)
        twojmax_pattern = re.compile('twojmax (\d*)\n', re.S)
        rfac_pattern = re.compile('rfac0 (.*?)\n', re.S)
        rmin_pattern = re.compile('rmin0 (.*?)\n', re.S)
        diagonalstyle_pattern = re.compile('diagonalstyle (.*?)\n', re.S)
        quadratic_pattern = re.compile('quadraticflag (.*?)(?=\n|$)', re.S)

        with zopen(param_file, 'rt') as f:
            param_lines = f.read()

        rcut = float(rcut_pattern.findall(param_lines)[-1])
        twojmax = int(twojmax_pattern.findall(param_lines)[-1])
        rfac = float(rfac_pattern.findall(param_lines)[-1])
        rmin = int(rmin_pattern.findall(param_lines)[-1])
        diagonal = int(diagonalstyle_pattern.findall(param_lines)[-1])
        if quadratic_pattern.findall(param_lines):
            quadratic = bool(int(quadratic_pattern.findall(param_lines)[-1]))
        else:
            quadratic = False

        describer = BispectrumCoefficients(rcutfac=rcut,
                                           twojmax=twojmax,
                                           rfac0=rfac,
                                           element_profile=element_profile,
                                           rmin0=rmin,
                                           diagonalstyle=diagonal,
                                           quadratic=quadratic,
                                           pot_fit=True)
        model = LinearModel(describer=describer, **kwargs)
        model.model.coef_ = np.array(coeff_lines[2:], dtype=np.float)
        model.model.intercept_ = 0
        snap = SNAPotential(model=model)
        snap.specie = Element(specie)
        return snap
Exemplo n.º 4
0
 def setUp(self):
     element_profile = {'Ni': {'r': 0.5, 'w': 1}}
     describer = BispectrumCoefficients(rcutfac=4.1, twojmax=8,
                                        element_profile=element_profile,
                                        pot_fit=True)
     model = LinearModel(describer=describer)
     model.model.coef_ = coeff
     model.model.intercept_ = intercept
     snap = SNAPotential(model=model)
     snap.specie = Element('Ni')
     self.ff_settings = snap
Exemplo n.º 5
0
 def setUp(self):
     element_profile = {'Ni': {'r': 0.5, 'w': 1}}
     describer = BispectrumCoefficients(rcutfac=4.1, twojmax=8,
                                        element_profile=element_profile,
                                        pot_fit=True)
     model = LinearModel(describer=describer)
     model.model.coef_ = coeff
     model.model.intercept_ = intercept
     snap = SNAPotential(model=model)
     snap.specie = Element('Ni')
     self.struct = Structure.from_spacegroup('Fm-3m',
                                             Lattice.cubic(3.506),
                                             ['Ni'], [[0, 0, 0]])
     self.ff_settings = snap
Exemplo n.º 6
0
    def test_describe(self):
        s = Structure.from_spacegroup(225, Lattice.cubic(5.69169),
                                      ['Na', 'Cl'], [[0, 0, 0], [0, 0, 0.5]])
        profile = dict(Na=dict(r=0.3, w=0.9), Cl=dict(r=0.7, w=3.0))
        s *= [2, 2, 2]
        structures = [s] * 10
        for s in structures:
            n = np.random.randint(4)
            inds = np.random.randint(16, size=n)
            s.remove_sites(inds)

        bc_atom = BispectrumCoefficients(5,
                                         3,
                                         profile,
                                         diagonalstyle=2,
                                         quadratic=False,
                                         pot_fit=False)
        df_atom = bc_atom.describe_all(structures)
        for i, s in enumerate(structures):
            df_s = df_atom.xs(i, level='input_index')
            self.assertEqual(df_s.shape, (len(s), 4))
            self.assertTrue(df_s.equals(bc_atom.describe(s)))

        bc_pot = BispectrumCoefficients(5,
                                        3,
                                        profile,
                                        diagonalstyle=2,
                                        quadratic=False,
                                        pot_fit=True)
        df_pot = bc_pot.describe_all(structures, include_stress=True)
        for i, s in enumerate(structures):
            df_s = df_pot.xs(i, level='input_index')
            self.assertEqual(df_s.shape, ((1 + len(s) * 3 + 6, 10)))
            self.assertTrue(
                df_s.equals(bc_pot.describe(s, include_stress=True)))
            sna = df_s.iloc[0]
            for specie in ['Na', 'Cl']:
                self.assertAlmostEqual(
                    sna[specie, 'n'],
                    s.composition.fractional_composition[specie])
                np.testing.assert_array_equal(df_s[specie, 'n'][1:],
                                              np.zeros(len(s) * 3 + 6))