Пример #1
0
def test_efs_kern_vec(params, ihyps):
    name, cutoffs, hyps_mask_list, _ = params

    np.random.seed(10)
    test_point = get_tstp()

    size1 = len(flare.gp_algebra._global_training_data[name])
    size2 = len(flare.gp_algebra._global_training_structures[name])

    hyps_mask = hyps_mask_list[ihyps]
    hyps = hyps_mask["hyps"]
    kernel = str_to_kernel_set(hyps_mask["kernels"], "mc", hyps_mask)

    test_point = get_tstp()

    energy_vector, force_array, stress_array = efs_kern_vec(
        name, kernel[5], kernel[4], test_point, hyps, cutoffs, hyps_mask)

    energy_vector_par, force_array_par, stress_array_par = efs_kern_vec(
        name,
        kernel[5],
        kernel[4],
        test_point,
        hyps,
        cutoffs,
        hyps_mask,
        n_cpus=2,
        n_sample=100,
    )

    assert np.equal(energy_vector, energy_vector_par).all()
    assert np.equal(force_array, force_array_par).all()
    assert np.equal(stress_array, stress_array_par).all()
Пример #2
0
    def predict_efs(self, x_t: AtomicEnvironment):
        """Predict the local energy, forces, and partial stresses of an
        atomic environment and their predictive variances."""

        # Kernel vector allows for evaluation of atomic environments.
        if self.parallel and not self.per_atom_par:
            n_cpus = self.n_cpus
        else:
            n_cpus = 1

        _global_training_data[self.name] = self.training_data
        _global_training_labels[self.name] = self.training_labels_np

        energy_vector, force_array, stress_array = efs_kern_vec(
            self.name,
            self.efs_force_kernel,
            self.efs_energy_kernel,
            x_t,
            self.hyps,
            cutoffs=self.cutoffs,
            hyps_mask=self.hyps_mask,
            n_cpus=n_cpus,
            n_sample=self.n_sample,
        )

        # Check that alpha is up to date with training set.
        self.check_L_alpha()

        # Compute mean predictions.
        en_pred = np.matmul(energy_vector, self.alpha)
        force_pred = np.matmul(force_array, self.alpha)
        stress_pred = np.matmul(stress_array, self.alpha)

        # Compute uncertainties.
        args = from_mask_to_args(self.hyps, self.cutoffs, self.hyps_mask)
        self_en, self_force, self_stress = self.efs_self_kernel(x_t, *args)

        en_var = self_en - np.matmul(np.matmul(energy_vector, self.ky_mat_inv),
                                     energy_vector)
        force_var = self_force - np.diag(
            np.matmul(np.matmul(force_array, self.ky_mat_inv),
                      force_array.transpose()))
        stress_var = self_stress - np.diag(
            np.matmul(np.matmul(stress_array, self.ky_mat_inv),
                      stress_array.transpose()))

        return en_pred, force_pred, stress_pred, en_var, force_var, stress_var