Ejemplo n.º 1
0
    def GenGrid_serial(self, GP):
        '''
        generate grid data of mean prediction and L^{-1}k* for each triplet
         implemented in a parallelized style
        '''
        
        # ------ get 3body kernel info ------
        kernel, efk, cutoffs, hyps, hyps_mask = get_3bkernel(GP)

        # ------ construct grids ------
        nop = self.grid_num[0]
        noa = self.grid_num[2]
        bond_lengths = np.linspace(self.l_bounds[0], self.u_bounds[0], nop)
        cos_angles = np.linspace(self.l_bounds[2], self.u_bounds[2], noa)
        bond_means = np.zeros([nop, nop, noa])
        bond_vars = np.zeros([nop, nop, noa, len(GP.alpha)])
        env12 = AtomicEnvironment(self.bond_struc, 0, self.cutoffs)

        if self.update:
            if 'kv3' in os.listdir():
                os.rmdir('kv3')
            os.mkdir('kv3')

        size = len(GP.training_data)
        ds = [1, 2, 3]
        k_v = np.zeros(3)
        k12_v_all = np.zeros([len(bond_lengths), len(bond_lengths),
                              len(cos_angles), size*3])
        for b1, r1 in enumerate(bond_lengths):
            for b2, r2 in enumerate(bond_lengths):
                for a12, cos_angle12 in enumerate(cos_angles):

                    x2 = r2 * cos_angle12
                    y2 = r2 * np.sqrt(1-cos_angle12**2)
                    r12 = np.linalg.norm(np.array([x2-r1, y2, 0]))

                    env12.bond_array_3 = np.array([[r1, 1, 0, 0],
                                                   [r2, 0, 0, 0]])
                    env12.cross_bond_dists = np.array([[0, r12], [r12, 0]])

                    for isample, sample in enumerate(GP.training_data):
                        for d in ds:
                            k_v[d-1] = kernel(env12, sample, 1, d,
                                              hyps, cutoffs)

                        k12_v_all[b1, b2, a12, isample*3:isample*3+3] = k_v

        for b1, r1 in enumerate(bond_lengths):
            for b2, r2 in enumerate(bond_lengths):
                for a12, cos_angle in enumerate(cos_angles):
                    k12_v = k12_v_all[b1, b2, a12, :]
                    bond_means[b1, b2, a12] = np.matmul(k12_v, GP.alpha)
                    if not self.mean_only:
                        bond_vars[b1, b2, a12, :] = solve_triangular(GP.l_mat, k12_v, lower=True)

        # # ------ save mean and var to file -------
        np.save('grid3_mean', bond_means)
        np.save('grid3_var', bond_vars)

        return bond_means, bond_vars
Ejemplo n.º 2
0
def get_grid_env(species, parameter, kernel_name, same_hyps):
    '''generate a single triplet environment'''

    env1, env2, hm1, hm2 = parameter[kernel_name]
    hm = hm1 if same_hyps else hm2

    big_cell = np.eye(3) * 100
    r1 = 0.5
    r2 = 0.5

    if kernel_name == 'twobody':
        positions = [[0, 0, 0], [r1, 0, 0]]
        grid_struc = Structure(big_cell, species, positions)
        env = AtomicEnvironment(grid_struc, 0, hm['cutoffs'], hm)
        grid = np.array([[r1]])
    elif kernel_name == 'threebody':
        positions = [[0, 0, 0], [r1, 0, 0], [0, r2, 0]]
        grid_struc = Structure(big_cell, species, positions)
        env = AtomicEnvironment(grid_struc, 0, hm['cutoffs'], hm)
        env.bond_array_3 = np.array([[r1, 1, 0, 0], [r2, 0, 0, 0]])
        grid = np.array([[r1, r2, np.sqrt(r1**2 + r2**2)]])
    return env, grid