<commit_before>#!/usr/bin/env python
# $Id$
# -*- coding: utf-8

'''
test libcint
'''

__author__ = "<NAME> <<EMAIL>>"

import sys
import os
import ctypes
import numpy

if sys.platform == 'darwin':
    alib = os.environ['buildir'] + '/testsuite/.libs/libtestcint.dylib'
else:
    alib = os.environ['buildir'] + '/testsuite/.libs/libtestcint.so'
_cint = ctypes.cdll.LoadLibrary(alib)

PTR_LIGHT_SPEED    = 0
PTR_COMMON_ORIG    = 1
PTR_SHIELDING_ORIG = 4
PTR_RINV_ORIG      = 4
PTR_ENV_START      = 20

CHARGE_OF  = 0
PTR_COORD  = 1
NUC_MOD_OF = 2
PTR_MASS   = 3
RAD_GRIDS  = 4
ANG_GRIDS  = 5
ATM_SLOTS  = 6

ATOM_OF   = 0
ANG_OF    = 1
NPRIM_OF  = 2
NCTR_OF   = 3
KAPPA_OF  = 4
PTR_EXP   = 5
PTR_COEFF = 6
BAS_SLOTS = 8

atm = (ctypes.c_int * 200)()
bas = (ctypes.c_int * 200)()
env = (ctypes.c_double * 400)()
natm = ctypes.c_int()
nbas = ctypes.c_int()
opt = ctypes.c_ulong(0)
_cint.init_test_env(atm, ctypes.addressof(natm), bas, ctypes.addressof(nbas), env)

_cint.CINTlen_spinor.restype = ctypes.c_int

def test_int1e_sph(name, vref, dim, place):
    intor = getattr(_cint, name)
    op = (ctypes.c_double * (10000 * dim))()
    v1 = 0
    for j in range(nbas.value*2):
        for i in range(j+1):
            di = (bas[ANG_OF+BAS_SLOTS*i] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*i]
            dj = (bas[ANG_OF+BAS_SLOTS*j] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*j]
            shls = (ctypes.c_int * 2)(i, j)
            intor(op, shls, atm, natm, bas, nbas, env);
            v1 += abs(numpy.array(op[:di*dj*dim])).sum()
    if round(abs(v1-vref), place):
        print "* FAIL: ", name, ". err:", '%.16g' % abs(v1-vref), "/", vref
    else:
        print "pass: ", name

def cdouble_to_cmplx(arr):
    return numpy.array(arr)[0::2] + numpy.array(arr)[1::2] * 1j

def test_int1e_spinor(name, vref, dim, place):
    intor = getattr(_cint, name)
    op = (ctypes.c_double * (20000 * dim))()
    v1 = 0
    for j in range(nbas.value*2):
        for i in range(j+1):
            di = _cint.CINTlen_spinor(i, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*i]
            dj = _cint.CINTlen_spinor(j, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*j]
            shls = (ctypes.c_int * 2)(i, j)
            intor(op, shls, atm, natm, bas, nbas, env);
            v1 += abs(cdouble_to_cmplx(op[:di*dj*dim*2])).sum()
    if round(abs(v1-vref), place):
        print "* FAIL: ", name, ". err:", '%.16g' % abs(v1-vref), "/", vref
    else:
        print "pass: ", name

def max_loc(arr):
    loc = []
    maxi = arr.argmax()
    n = maxi
    for i in arr.shape:
        loc.append(n % i)
        n /= i
    loc.reverse()
    return maxi, loc

def test_comp1e_spinor(name1, name_ref, shift, dim, place):
    intor     = getattr(_cint, name1)
    intor_ref = getattr(_cint, name_ref)
    op =     (ctypes.c_double * (20000 * dim))()
    op_ref = (ctypes.c_double * (20000 * dim))()

    pfac = 1
    if shift[0] > 0:
        pfac *= -1j
    if shift[1] > 0:
        pfac *= 1j

    for j in range(nbas.value*2 - shift[1]):
        for i in range(min(nbas.value*2-shift[0],j+1)):
            di = _cint.CINTlen_spinor(i, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*i]
            dj = _cint.CINTlen_spinor(j, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*j]
            shls = (ctypes.c_int * 2)(i+shift[0], j+shift[1])
            intor(op, shls, atm, natm, bas, nbas, env);
            shls_ref = (ctypes.c_int * 2)(i, j)
            intor_ref(op_ref, shls_ref, atm, natm, bas, nbas, env);
            dd = abs(pfac * cdouble_to_cmplx(op[:di*dj*dim*2]).reshape(di,dj,dim)
                     - cdouble_to_cmplx(op_ref[:di*dj*dim*2]).reshape(di,dj,dim))
            if numpy.round(dd, place).sum():
                maxi = dd.argmax()
                print "* FAIL: ", name1, "/", name_ref, ". shell:", i, j, \
                        "err:", dd.flatten()[maxi], \
                        "/", op_ref[maxi*2]+op_ref[maxi*2+1]*1j
                return
    print "pass: ", name1, "/", name_ref

####################
def test_int2e_sph(name, vref, dim, place):
    intor = getattr(_cint, name)
    op = (ctypes.c_double * (1000000 * dim))()
    v1 = 0
    for l in range(nbas.value*2):
        for k in range(l+1):
            for j in range(nbas.value*2):
                for i in range(j+1):
                    di = (bas[ANG_OF+BAS_SLOTS*i] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*i]
                    dj = (bas[ANG_OF+BAS_SLOTS*j] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*j]
                    dk = (bas[ANG_OF+BAS_SLOTS*k] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*k]
                    dl = (bas[ANG_OF+BAS_SLOTS*l] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*l]
                    shls = (ctypes.c_int * 4)(i, j, k, l)
                    intor(op, shls, atm, natm, bas, nbas, env, opt);
                    v1 += abs(numpy.array(op[:di*dj*dk*dl*dim])).sum()
    if round(abs(v1-vref), place):
        print "* FAIL: ", name, ". err:", '%.16g' % abs(v1-vref), "/", vref
    else:
        print "pass: ", name

def test_int2e_spinor(name, vref, dim, place):
    intor = getattr(_cint, name)
    op = (ctypes.c_double * (2000000 * dim))()
    v1 = 0
    for l in range(nbas.value*2):
        for k in range(l+1):
            for j in range(nbas.value*2):
                for i in range(j+1):
                    di = _cint.CINTlen_spinor(i, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*i]
                    dj = _cint.CINTlen_spinor(j, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*j]
                    dk = _cint.CINTlen_spinor(k, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*k]
                    dl = _cint.CINTlen_spinor(l, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*l]
                    shls = (ctypes.c_int * 4)(i, j, k, l)
                    intor(op, shls, atm, natm, bas, nbas, env, opt);
                    v1 += abs(cdouble_to_cmplx(op[:di*dj*dk*dl*dim*2])).sum()
    if round(abs(v1-vref), place):
        print "* FAIL: ", name, ". err:", '%.16g' % abs(v1-vref), "/", vref
    else:
        print "pass: ", name

def test_comp2e_spinor(name1, name_ref, shift, dim, place):
    intor     = getattr(_cint, name1)
    intor_ref = getattr(_cint, name_ref)
    op =     (ctypes.c_double * (2000000 * dim))()
    op_ref = (ctypes.c_double * (2000000 * dim))()

    pfac = 1
    if shift[0] > 0:
        pfac *= -1j
    if shift[1] > 0:
        pfac *= 1j
    if shift[2] > 0:
        pfac *= -1j
    if shift[3] > 0:<commit_msg>fix segfault problems because of ctypes restype<commit_after>#!/usr/bin/env python
# $Id$
# -*- coding: utf-8

'''
test libcint
'''

__author__ = "<NAME> <<EMAIL>>"

import sys
import os
import ctypes
import numpy

alib = os.environ['buildir'] + '/testsuite/.libs/libtestcint.so'
_cint = ctypes.CDLL(alib)

PTR_LIGHT_SPEED    = 0
PTR_COMMON_ORIG    = 1
PTR_SHIELDING_ORIG = 4
PTR_RINV_ORIG      = 4
PTR_ENV_START      = 20

CHARGE_OF  = 0
PTR_COORD  = 1
NUC_MOD_OF = 2
PTR_MASS   = 3
RAD_GRIDS  = 4
ANG_GRIDS  = 5
ATM_SLOTS  = 6

ATOM_OF   = 0
ANG_OF    = 1
NPRIM_OF  = 2
NCTR_OF   = 3
KAPPA_OF  = 4
PTR_EXP   = 5
PTR_COEFF = 6
BAS_SLOTS = 8

atm = (ctypes.c_int * 200)()
bas = (ctypes.c_int * 200)()
env = (ctypes.c_double * 400)()
natm = ctypes.c_int()
nbas = ctypes.c_int()
opt = ctypes.c_ulong(0)
_cint.init_test_env.restype = ctypes.c_void_p
_cint.init_test_env(atm, ctypes.addressof(natm), bas, ctypes.addressof(nbas), env)

_cint.CINTlen_spinor.restype = ctypes.c_int

def test_int1e_sph(name, vref, dim, place):
    intor = getattr(_cint, name)
    intor.restype = ctypes.c_void_p
    op = (ctypes.c_double * (10000 * dim))()
    v1 = 0
    for j in range(nbas.value*2):
        for i in range(j+1):
            di = (bas[ANG_OF+BAS_SLOTS*i] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*i]
            dj = (bas[ANG_OF+BAS_SLOTS*j] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*j]
            shls = (ctypes.c_int * 2)(i, j)
            intor(op, shls, atm, natm, bas, nbas, env);
            v1 += abs(numpy.array(op[:di*dj*dim])).sum()
    if round(abs(v1-vref), place):
        print "* FAIL: ", name, ". err:", '%.16g' % abs(v1-vref), "/", vref
    else:
        print "pass: ", name

def cdouble_to_cmplx(arr):
    return numpy.array(arr)[0::2] + numpy.array(arr)[1::2] * 1j

def test_int1e_spinor(name, vref, dim, place):
    intor = getattr(_cint, name)
    intor.restype = ctypes.c_void_p
    op = (ctypes.c_double * (20000 * dim))()
    v1 = 0
    for j in range(nbas.value*2):
        for i in range(j+1):
            di = _cint.CINTlen_spinor(i, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*i]
            dj = _cint.CINTlen_spinor(j, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*j]
            shls = (ctypes.c_int * 2)(i, j)
            intor(op, shls, atm, natm, bas, nbas, env);
            v1 += abs(cdouble_to_cmplx(op[:di*dj*dim*2])).sum()
    if round(abs(v1-vref), place):
        print "* FAIL: ", name, ". err:", '%.16g' % abs(v1-vref), "/", vref
    else:
        print "pass: ", name

def max_loc(arr):
    loc = []
    maxi = arr.argmax()
    n = maxi
    for i in arr.shape:
        loc.append(n % i)
        n /= i
    loc.reverse()
    return maxi, loc

def test_comp1e_spinor(name1, name_ref, shift, dim, place):
    intor     = getattr(_cint, name1)
    intor.restype = ctypes.c_void_p
    intor_ref = getattr(_cint, name_ref)
    intor_ref.restype = ctypes.c_void_p
    op =     (ctypes.c_double * (20000 * dim))()
    op_ref = (ctypes.c_double * (20000 * dim))()

    pfac = 1
    if shift[0] > 0:
        pfac *= -1j
    if shift[1] > 0:
        pfac *= 1j

    for j in range(nbas.value*2 - shift[1]):
        for i in range(min(nbas.value*2-shift[0],j+1)):
            di = _cint.CINTlen_spinor(i, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*i]
            dj = _cint.CINTlen_spinor(j, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*j]
            shls = (ctypes.c_int * 2)(i+shift[0], j+shift[1])
            intor(op, shls, atm, natm, bas, nbas, env);
            shls_ref = (ctypes.c_int * 2)(i, j)
            intor_ref(op_ref, shls_ref, atm, natm, bas, nbas, env);
            dd = abs(pfac * cdouble_to_cmplx(op[:di*dj*dim*2]).reshape(di,dj,dim)
                     - cdouble_to_cmplx(op_ref[:di*dj*dim*2]).reshape(di,dj,dim))
            if numpy.round(dd, place).sum():
                maxi = dd.argmax()
                print "* FAIL: ", name1, "/", name_ref, ". shell:", i, j, \
                        "err:", dd.flatten()[maxi], \
                        "/", op_ref[maxi*2]+op_ref[maxi*2+1]*1j
                return
    print "pass: ", name1, "/", name_ref

####################
def test_int2e_sph(name, vref, dim, place):
    intor = getattr(_cint, name)
    intor.restype = ctypes.c_void_p
    op = (ctypes.c_double * (1000000 * dim))()
    v1 = 0
    for l in range(nbas.value*2):
        for k in range(l+1):
            for j in range(nbas.value*2):
                for i in range(j+1):
                    di = (bas[ANG_OF+BAS_SLOTS*i] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*i]
                    dj = (bas[ANG_OF+BAS_SLOTS*j] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*j]
                    dk = (bas[ANG_OF+BAS_SLOTS*k] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*k]
                    dl = (bas[ANG_OF+BAS_SLOTS*l] * 2 + 1) * bas[NCTR_OF+BAS_SLOTS*l]
                    shls = (ctypes.c_int * 4)(i, j, k, l)
                    intor(op, shls, atm, natm, bas, nbas, env, opt);
                    v1 += abs(numpy.array(op[:di*dj*dk*dl*dim])).sum()
    if round(abs(v1-vref), place):
        print "* FAIL: ", name, ". err:", '%.16g' % abs(v1-vref), "/", vref
    else:
        print "pass: ", name

def test_int2e_spinor(name, vref, dim, place):
    intor = getattr(_cint, name)
    intor.restype = ctypes.c_void_p
    op = (ctypes.c_double * (2000000 * dim))()
    v1 = 0
    for l in range(nbas.value*2):
        for k in range(l+1):
            for j in range(nbas.value*2):
                for i in range(j+1):
                    di = _cint.CINTlen_spinor(i, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*i]
                    dj = _cint.CINTlen_spinor(j, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*j]
                    dk = _cint.CINTlen_spinor(k, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*k]
                    dl = _cint.CINTlen_spinor(l, bas, nbas) * bas[NCTR_OF+BAS_SLOTS*l]
                    shls = (ctypes.c_int * 4)(i, j, k, l)
                    intor(op, shls, atm, natm, bas, nbas, env, opt);
                    v1 += abs(cdouble_to_cmplx(op[:di*dj*dk*dl*dim*2])).sum()
    if round(abs(v1-vref), place):
        print "* FAIL: ", name, ". err:", '%.16g' % abs(v1-vref), "/", vref
    else:
        print "pass: ", name

def test_comp2e_spinor(name1, name_ref, shift, dim, place):
    intor     = getattr(_cint, name1)
    intor.restype = ctypes.c_void_p
    intor_ref = getattr(_cint, name_ref)
    intor_ref.restype = ctypes.c_void_p
    op =     (ctypes.c_double * (2000000 * dim))()
    op_ref = (ctypes.c_double * (2000000 * dim))()

    pfac = 1
    if shift[0] > 0:
        pfac *= -1j
    if shift[1] > 0:
        pfac *= 1j
    if shift[2] > 0:
        pfac *= -1j
    if shift[3] > 0:
{'fraction_non_alphanumeric': 0.1649724528594708, 'fraction_numerical': 0.02933188484519283, 'mean_word_length': 2.2685772254628453, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '624496', 'n_tokens_mistral': 5791, 'n_tokens_neox': 5491, 'n_words': 1080}