Example #1
0
    def base_multipole_fields(self, options):
        mol = cache.molecule["pna"]
        cppe_state = CppeState(options, mol, lambda s: None)
        cppe_state.calculate_static_energies_and_fields()

        multipole_fields = cppe_state.multipole_fields

        potentials = cppe_state.potentials
        polsites = get_polarizable_sites(potentials)

        ref = np.zeros((len(potentials), 3))
        for i, p1 in enumerate(polsites):
            for j, p2 in enumerate(polsites):
                if not p1.is_polarizable:
                    continue
                if p1.index == p2.index:
                    continue
                if p1.excludes_site(p2.index):
                    continue
                diff = p1.position - p2.position
                for m in p2.multipoles:
                    M_full = symmetry.unfold_tensor(m.values, m.k)
                    if options.get("damp_multipole", False):
                        a_i = p1.polarizability.isotropic_value
                        a_j = p2.polarizability.isotropic_value
                        damp = cppe_state.options["damping_factor_multipole"]
                        a = 1/(a_i*a_j)**(1/6) * damp
                        ref[p1.index, :] += polfields.thole_exp_field(
                            diff, m.k, M_full, 1, a)
                    else:
                        ref[p1.index, :] += polfields.field(
                            diff, m.k, M_full, 1)

        np.testing.assert_allclose(ref.flatten(), multipole_fields, atol=1e-14)
Example #2
0
    def test_solver_by_inversion(self):
        mol = cache.molecule["pna"]
        options = {"potfile": self.potfile_path,
                   "induced_thresh": 1e-12}
        cppe_state = CppeState(options, mol, print_callback)
        cppe_state.calculate_static_energies_and_fields()

        static_fields = cppe_state.static_fields
        zeros = np.zeros_like(static_fields)
        cppe_state.update_induced_moments(zeros, False)

        potentials = cppe_state.potentials
        polsites = get_polarizable_sites(potentials)
        npolsites = cppe_state.get_polarizable_site_number()
        bmat = np.zeros((3 * npolsites, 3 * npolsites))

        for s1, pot1 in enumerate(polsites):
            pol = pot1.polarizability
            inv_alpha = triangle_to_mat(pol.values)
            bmat[block(s1, s1)] = np.linalg.inv(inv_alpha)
            for s2, pot2 in enumerate(polsites):
                if pot1.excludes_site(pot2.index) or s1 == s2:
                    continue
                diff = pot2.position - pot1.position
                T12 = T_recursive(2, diff)
                if s1 > s2:
                    bmat[block(s1, s2)] = -triangle_to_mat(T12)
                    bmat[block(s2, s1)] = -triangle_to_mat(T12)

        # Test the matrix apply
        bmatrix_cpp = BMatrix(polsites, options)
        ret = bmatrix_cpp.compute_apply(static_fields)
        ret_ref = bmat @ static_fields
        np.testing.assert_allclose(ret, ret_ref, atol=1e-12)

        ret1 = bmatrix_cpp.compute_apply_slice(static_fields, 0, 8)
        ret2 = bmatrix_cpp.compute_apply_slice(static_fields, 8, npolsites)
        ret_all = ret1 + ret2
        np.testing.assert_allclose(ret_all, ret_ref, atol=1e-12)

        # build the Bmatrix from C++
        A = LinearOperator(2 * (static_fields.size,),
                           matvec=bmatrix_cpp.compute_apply)
        Afull = A @ np.eye(static_fields.size)
        np.testing.assert_allclose(Afull, bmat, atol=1e-20)

        induced_moments_direct = np.linalg.inv(bmat) @ static_fields
        induced_moments_solver = cppe_state.get_induced_moments()
        np.testing.assert_almost_equal(induced_moments_solver,
                                       induced_moments_direct, decimal=9)

        ind_mom = InducedMoments(potentials, options)
        res_cg = ind_mom.compute_cg(static_fields)
        np.testing.assert_allclose(res_cg, induced_moments_direct, atol=1e-10)

        binv = bmatrix_cpp.direct_inverse()
        np.testing.assert_allclose(binv @ static_fields,
                                   induced_moments_direct, atol=1e-15)
Example #3
0
def grad_nuclear_field_fdiff(atoms, coords, potentials, step_au=1e-3):
    """
    Computes the finite difference gradient
    with a 5-point stencil
    """
    natoms = len(atoms)
    polsites = cppe.get_polarizable_sites(potentials)
    grad = np.zeros((natoms, 3, len(polsites), 3))
    for i in range(natoms):
        for c in range(3):
            print("Computing dE/d{} for atom {}.".format(coords_label[c], i))
            for f, p in zip(*stencils["5p"]):
                coords_p = coords.copy()
                coords_p[i, c] += f * step_au
                mol_p = cppe.Molecule()
                for z, coord in zip(atoms, coords_p):
                    mol_p.append(cppe.Atom(z, *coord))

                nf = NuclearFields(mol_p, potentials)
                en_pert = nf.compute().reshape(len(polsites), 3)
                grad[i, c] += p * en_pert / step_au
    return grad
Example #4
0
    def test_compute_nuclear_interaction_energy_gradient(self):
        mol = cache.molecule["pna"]
        options = {"potfile": self.potfile_path}

        p = PotfileReader(self.potfile_path)
        potentials = p.read()

        atoms = [a.atomic_number for a in mol]
        coords = np.array([a.position for a in mol])

        grad_nuc = MultipoleExpansion(mol, potentials).nuclear_gradient()

        grad_nuc_fd = grad_nuclear_interaction_energy_fdiff(
            atoms, coords, potentials)
        np.testing.assert_allclose(grad_nuc_fd, grad_nuc, atol=1e-10)

        grad_field_fd = grad_nuclear_field_fdiff(atoms, coords, potentials)

        natoms = len(atoms)
        polsites = cppe.get_polarizable_sites(potentials)
        grad_field = NuclearFields(mol, potentials).nuclear_gradient().reshape(
            (natoms, 3, len(polsites), 3))
        np.testing.assert_allclose(grad_field_fd, grad_field, atol=1e-10)
Example #5
0
    def test_solver_by_inversion(self):
        f = h5py.File(self.pna_path, 'r')
        mol = Molecule()
        options = PeOptions()
        options.potfile = self.potfile_path
        options.induced_thresh = 12
        for z, coord in zip(f['atom_charges'], f['atom_coords']):
            mol.append(Atom(z, *coord))
        cppe_state = CppeState(options, mol)
        cppe_state.calculate_static_energies_and_fields()

        static_fields = np.array(cppe_state.get_static_fields())
        zeros = np.zeros_like(static_fields)
        cppe_state.update_induced_moments(zeros, False)

        potentials = cppe_state.get_potentials()
        polsites = get_polarizable_sites(potentials)
        npolsites = cppe_state.get_polarizable_site_number()
        bmat = np.zeros((3 * npolsites, 3 * npolsites))

        coeffs = Tk_coefficients(5)
        for s1, pot1 in enumerate(polsites):
            for pol in pot1.get_polarizabilities():
                inv_alpha = triangle_to_mat(pol.get_values())
                bmat[block(s1, s1)] = np.linalg.inv(inv_alpha)
            for s2, pot2 in enumerate(polsites):
                if pot1.excludes_site(pot2.index) or s1 == s2:
                    continue
                diff = pot2.get_site_position() - pot1.get_site_position()
                T12 = Tk_tensor(2, diff, coeffs)
                if s1 > s2:
                    bmat[block(s1, s2)] = -triangle_to_mat(T12)
                    bmat[block(s2, s1)] = -triangle_to_mat(T12)
        induced_moments_direct = np.linalg.inv(bmat) @ static_fields
        induced_moments_solver = cppe_state.get_induced_moments()
        np.testing.assert_almost_equal(induced_moments_solver,
                                       induced_moments_direct, decimal=10)