|
| 1 | +#!/usr/bin/env python |
| 2 | +# $Id$ |
| 3 | +# -*- coding: utf-8 |
| 4 | + |
| 5 | +''' |
| 6 | +test libcint |
| 7 | +''' |
| 8 | + |
| 9 | +__author__ = "Qiming Sun <[email protected]>" |
| 10 | + |
| 11 | +import sys |
| 12 | +import os |
| 13 | +import ctypes |
| 14 | +import numpy |
| 15 | + |
| 16 | +_cint = ctypes.CDLL(os.path.abspath(os.path.join(__file__, '../../build/libcint.so')), |
| 17 | + mode=os.RTLD_GLOBAL|os.RTLD_NOW) |
| 18 | + |
| 19 | +PTR_EXPCUTOFF = 0 |
| 20 | +PTR_COMMON_ORIG = 1 |
| 21 | +PTR_SHIELDING_ORIG = 4 |
| 22 | +PTR_RINV_ORIG = 4 |
| 23 | +PTR_RINV_ZETA = 7 |
| 24 | +PTR_RANGE_OMEGA = 8 |
| 25 | +PTR_ENV_START = 20 |
| 26 | +NGRIDS = 11 |
| 27 | +PTR_GRIDS = 12 |
| 28 | + |
| 29 | +CHARGE_OF = 0 |
| 30 | +PTR_COORD = 1 |
| 31 | +NUC_MOD_OF = 2 |
| 32 | +PTR_ZETA = 3 |
| 33 | +RAD_GRIDS = 4 |
| 34 | +ANG_GRIDS = 5 |
| 35 | +ATM_SLOTS = 6 |
| 36 | + |
| 37 | +ATOM_OF = 0 |
| 38 | +ANG_OF = 1 |
| 39 | +NPRIM_OF = 2 |
| 40 | +NCTR_OF = 3 |
| 41 | +KAPPA_OF = 4 |
| 42 | +PTR_EXP = 5 |
| 43 | +PTR_COEFF = 6 |
| 44 | +BAS_SLOTS = 8 |
| 45 | + |
| 46 | +natm = 4 |
| 47 | +nbas = 0 |
| 48 | +atm = numpy.zeros((natm,ATM_SLOTS), dtype=numpy.int32) |
| 49 | +bas = numpy.zeros((1000,BAS_SLOTS), dtype=numpy.int32) |
| 50 | +env = numpy.zeros(10000) |
| 51 | +off = PTR_ENV_START |
| 52 | +for i in range(natm): |
| 53 | + atm[i, CHARGE_OF] = (i+1)*2 |
| 54 | + atm[i, PTR_COORD] = off |
| 55 | + env[off+0] = .2 * (i+1) |
| 56 | + env[off+1] = .3 + (i+1) * .5 |
| 57 | + env[off+2] = .1 - (i+1) * .5 |
| 58 | + off += 3 |
| 59 | +off0 = off |
| 60 | + |
| 61 | +# basis with kappa > 0 |
| 62 | +nh = 0 |
| 63 | + |
| 64 | +bas[nh,ATOM_OF ] = 0 |
| 65 | +bas[nh,ANG_OF ] = 1 |
| 66 | +bas[nh,KAPPA_OF] = 1 |
| 67 | +bas[nh,NPRIM_OF] = 1 |
| 68 | +bas[nh,NCTR_OF ] = 1 |
| 69 | +bas[nh,PTR_EXP] = off |
| 70 | +env[off+0] = 1 |
| 71 | +bas[nh,PTR_COEFF] = off + 1 |
| 72 | +env[off+1] = 1 |
| 73 | +off += 2 |
| 74 | +nh += 1 |
| 75 | + |
| 76 | +bas[nh,ATOM_OF ] = 1 |
| 77 | +bas[nh,ANG_OF ] = 2 |
| 78 | +bas[nh,KAPPA_OF] = 2 |
| 79 | +bas[nh,NPRIM_OF] = 2 |
| 80 | +bas[nh,NCTR_OF ] = 2 |
| 81 | +bas[nh,PTR_EXP] = off |
| 82 | +env[off+0] = 5 |
| 83 | +env[off+1] = 3 |
| 84 | +bas[nh,PTR_COEFF] = off + 2 |
| 85 | +env[off+2] = 1 |
| 86 | +env[off+3] = 2 |
| 87 | +env[off+4] = 4 |
| 88 | +env[off+5] = 1 |
| 89 | +off += 6 |
| 90 | +nh += 1 |
| 91 | + |
| 92 | +bas[nh,ATOM_OF ] = 2 |
| 93 | +bas[nh,ANG_OF ] = 3 |
| 94 | +bas[nh,KAPPA_OF] = 3 |
| 95 | +bas[nh,NPRIM_OF] = 1 |
| 96 | +bas[nh,NCTR_OF ] = 1 |
| 97 | +bas[nh,PTR_EXP ] = off |
| 98 | +env[off+0] = 1 |
| 99 | +bas[nh,PTR_COEFF] = off + 1 |
| 100 | +env[off+1] = 1 |
| 101 | +off += 2 |
| 102 | +nh += 1 |
| 103 | + |
| 104 | +bas[nh,ATOM_OF ] = 3 |
| 105 | +bas[nh,ANG_OF ] = 4 |
| 106 | +bas[nh,KAPPA_OF] = 4 |
| 107 | +bas[nh,NPRIM_OF] = 1 |
| 108 | +bas[nh,NCTR_OF ] = 1 |
| 109 | +bas[nh,PTR_EXP ] = off |
| 110 | +env[off+0] = .5 |
| 111 | +bas[nh,PTR_COEFF] = off + 1 |
| 112 | +env[off+1] = 1. |
| 113 | +off = off + 2 |
| 114 | +nh += 1 |
| 115 | + |
| 116 | +nbas = nh |
| 117 | + |
| 118 | +# basis with kappa < 0 |
| 119 | +n = off - off0 |
| 120 | +for i in range(n): |
| 121 | + env[off+i] = env[off0+i] |
| 122 | + |
| 123 | +for i in range(nh): |
| 124 | + bas[i+nh,ATOM_OF ] = bas[i,ATOM_OF ] |
| 125 | + bas[i+nh,ANG_OF ] = bas[i,ANG_OF ] - 1 |
| 126 | + bas[i+nh,KAPPA_OF] =-bas[i,KAPPA_OF] |
| 127 | + bas[i+nh,NPRIM_OF] = bas[i,NPRIM_OF] |
| 128 | + bas[i+nh,NCTR_OF ] = bas[i,NCTR_OF ] |
| 129 | + bas[i+nh,PTR_EXP ] = bas[i,PTR_EXP ] + n |
| 130 | + bas[i+nh,PTR_COEFF]= bas[i,PTR_COEFF] + n |
| 131 | + env[bas[i+nh,PTR_COEFF]] /= 2 * env[bas[i,PTR_EXP]] |
| 132 | + |
| 133 | +env[bas[5,PTR_COEFF]+0] = env[bas[1,PTR_COEFF]+0] / (2 * env[bas[1,PTR_EXP]+0]) |
| 134 | +env[bas[5,PTR_COEFF]+1] = env[bas[1,PTR_COEFF]+1] / (2 * env[bas[1,PTR_EXP]+1]) |
| 135 | +env[bas[5,PTR_COEFF]+2] = env[bas[1,PTR_COEFF]+2] / (2 * env[bas[1,PTR_EXP]+0]) |
| 136 | +env[bas[5,PTR_COEFF]+3] = env[bas[1,PTR_COEFF]+3] / (2 * env[bas[1,PTR_EXP]+1]) |
| 137 | + |
| 138 | +natm = ctypes.c_int(natm) |
| 139 | +nbas = ctypes.c_int(nbas) |
| 140 | +c_atm = atm.ctypes.data_as(ctypes.c_void_p) |
| 141 | +c_bas = bas.ctypes.data_as(ctypes.c_void_p) |
| 142 | +c_env = env.ctypes.data_as(ctypes.c_void_p) |
| 143 | + |
| 144 | +opt = ctypes.POINTER(ctypes.c_void_p)() |
| 145 | +_cint.CINTlen_spinor.restype = ctypes.c_int |
| 146 | + |
| 147 | + |
| 148 | +def test_int1e_grids_sph1(name, vref, dim, place): |
| 149 | + intor = getattr(_cint, name) |
| 150 | + intor.restype = ctypes.c_void_p |
| 151 | + ngrids = 1 |
| 152 | + numpy.random.seed(12) |
| 153 | + grids = numpy.random.random((ngrids, 3)) - 5.2 |
| 154 | + env_g = numpy.append(env, grids.ravel()) |
| 155 | + env_g[NGRIDS] = ngrids |
| 156 | + env_g[PTR_GRIDS] = env.size |
| 157 | + op = numpy.empty(1000000 * dim) |
| 158 | + v1 = 0 |
| 159 | + cnt = 0 |
| 160 | + for j in range(0,nbas.value*2): |
| 161 | + for i in range(j+1): |
| 162 | + print(i, j) |
| 163 | + di = (bas[i,ANG_OF] * 2 + 1) * bas[i,NCTR_OF] |
| 164 | + dj = (bas[j,ANG_OF] * 2 + 1) * bas[j,NCTR_OF] |
| 165 | + shls = (ctypes.c_int * 4)(i, j, 0, ngrids) |
| 166 | + intor(op.ctypes.data_as(ctypes.c_void_p), |
| 167 | + shls, c_atm, natm, c_bas, nbas, |
| 168 | + env_g.ctypes.data_as(ctypes.c_void_p), opt) |
| 169 | + v1 += abs(op[:ngrids*di*dj*dim]).sum() |
| 170 | + cnt += ngrids*di*dj*dim |
| 171 | + ref = numpy.empty((di, dj), order='F') |
| 172 | + env_g[PTR_RINV_ORIG:PTR_RINV_ORIG+3] = grids[0] |
| 173 | + _cint.cint1e_rinv_sph(ref.ctypes.data_as(ctypes.c_void_p), |
| 174 | + shls, c_atm, natm, c_bas, nbas, |
| 175 | + env_g.ctypes.data_as(ctypes.c_void_p), opt) |
| 176 | + dat = op[:ref.size].reshape(dj, di).T |
| 177 | + if abs(dat - ref).max() > 1e-12: |
| 178 | + print(i, j, abs(dat - ref).max()) |
| 179 | + print('sum', v1, 'diff', v1 - vref) |
| 180 | + if abs(v1 - vref) < 1e-10: |
| 181 | + print('pass') |
| 182 | + else: |
| 183 | + print('failed') |
| 184 | + |
| 185 | +def test_int1e_grids_sph(name, vref, dim, place): |
| 186 | + intor = getattr(_cint, name) |
| 187 | + intor.restype = ctypes.c_void_p |
| 188 | + ngrids = 148 |
| 189 | + numpy.random.seed(12) |
| 190 | + grids = numpy.random.random((ngrids, 3)) - 5.2 |
| 191 | + env_g = numpy.append(env, grids.ravel()) |
| 192 | + env_g[NGRIDS] = ngrids |
| 193 | + env_g[PTR_GRIDS] = env.size |
| 194 | + op = numpy.empty(1000000 * dim) |
| 195 | + v1 = 0 |
| 196 | + cnt = 0 |
| 197 | + for j in range(nbas.value*2): |
| 198 | + for i in range(j+1): |
| 199 | + print(i, j) |
| 200 | + di = (bas[i,ANG_OF] * 2 + 1) * bas[i,NCTR_OF] |
| 201 | + dj = (bas[j,ANG_OF] * 2 + 1) * bas[j,NCTR_OF] |
| 202 | + shls = (ctypes.c_int * 4)(i, j, 0, ngrids) |
| 203 | + intor(op.ctypes.data_as(ctypes.c_void_p), |
| 204 | + shls, c_atm, natm, c_bas, nbas, |
| 205 | + env_g.ctypes.data_as(ctypes.c_void_p), opt) |
| 206 | + v1 += abs(op[:ngrids*di*dj*dim]).sum() |
| 207 | + cnt += ngrids*di*dj*dim |
| 208 | + print('sum', v1, 'diff', v1 - vref) |
| 209 | + if abs(v1 - vref) < 1e-10: |
| 210 | + print('pass') |
| 211 | + else: |
| 212 | + print('failed') |
| 213 | + |
| 214 | +def test_mol1(): |
| 215 | + import time |
| 216 | + import pyscf |
| 217 | + from pyscf import df |
| 218 | + mol0 = pyscf.M(atom=''' |
| 219 | +C 0.16146 -4.47867 0.00000 |
| 220 | +H -0.89492 5.29077 0.00000 |
| 221 | +H 0.47278 4.59602 0.88488 |
| 222 | +H 0.47278 4.59602 -0.88488 |
| 223 | +H -1.49009 3.01810 -0.87995 |
| 224 | + ''', basis='ccpvtz') |
| 225 | + numpy.random.seed(12) |
| 226 | + ngrids = 201 |
| 227 | + grids = numpy.random.random((ngrids, 3)) * 12 - 5 |
| 228 | + |
| 229 | + def check(intor, ref): |
| 230 | + j3c = mol.intor(intor, grids=grids) |
| 231 | + diff = abs(j3c - ref).max() |
| 232 | + print(diff) |
| 233 | + return diff > 1e-12 |
| 234 | + |
| 235 | + failed = False |
| 236 | + for omega in (0, 0.1, -0.1): |
| 237 | + for zeta in (0, 10, 1e16): |
| 238 | + print('omega, zeta', omega, zeta) |
| 239 | + if zeta == 0: |
| 240 | + expnt = 1e16 |
| 241 | + else: |
| 242 | + expnt = zeta |
| 243 | + mol = mol0.copy() |
| 244 | + mol.omega = omega |
| 245 | + mol.set_rinv_zeta(zeta) |
| 246 | + fmol = pyscf.gto.fakemol_for_charges(grids, expnt) |
| 247 | + ref = df.incore.aux_e2(mol, fmol, intor='int3c2e').transpose(2,0,1) |
| 248 | + failed = check('int1e_grids', ref) or failed |
| 249 | + |
| 250 | + ref = df.incore.aux_e2(mol, fmol, intor='int3c2e_ip1').transpose(0,3,1,2) |
| 251 | + failed = check('int1e_grids_ip', ref) or failed |
| 252 | + |
| 253 | + ref = df.incore.aux_e2(mol, fmol, intor='int3c2e_ip1_cart').transpose(0,3,1,2) |
| 254 | + failed = check('int1e_grids_ip_cart', ref) or failed |
| 255 | + |
| 256 | + ref = df.incore.aux_e2(mol, fmol, intor='int3c2e_ip1_spinor').transpose(0,3,1,2) |
| 257 | + failed = check('int1e_grids_ip_spinor', ref) or failed |
| 258 | + |
| 259 | + ref = df.r_incore.aux_e2(mol, fmol, intor='int3c2e_spsp1_spinor').transpose(2,0,1) |
| 260 | + failed = check('int1e_grids_spvsp_spinor', ref) or failed |
| 261 | + if failed: |
| 262 | + print('failed') |
| 263 | + else: |
| 264 | + print('pass') |
| 265 | + |
| 266 | +test_int1e_grids_sph1('cint1e_grids_sph', 36.81452996003706, 1, 9) |
| 267 | +test_int1e_grids_sph('cint1e_grids_ip_sph', 3279.92861109671, 1, 9) |
| 268 | +try: |
| 269 | + test_mol1() |
| 270 | +except ImportError: |
| 271 | + pass |
0 commit comments