From 6fece0e44feef43dcb879f3c2e6a2d84a26d518f Mon Sep 17 00:00:00 2001
From: Qiming Sun <osirpt.sun@gmail.com>
Date: Fri, 24 Dec 2021 19:20:42 -0800
Subject: [PATCH] Make libcgto module compatible with both v4 and v5 of libcint

---
 .github/workflows/ci_linux/build_pyscf.sh |  2 +-
 .github/workflows/publish.yml             |  2 +-
 pyscf/lib/CMakeLists.txt                  | 12 ++++++------
 pyscf/lib/gto/ft_ao.c                     |  4 ++--
 pyscf/lib/gto/ft_ao.h                     | 13 ++++++++-----
 pyscf/lib/gto/ft_ao_deriv.c               |  4 ++--
 6 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/.github/workflows/ci_linux/build_pyscf.sh b/.github/workflows/ci_linux/build_pyscf.sh
index 2c091559bb..5cb9d4a56a 100755
--- a/.github/workflows/ci_linux/build_pyscf.sh
+++ b/.github/workflows/ci_linux/build_pyscf.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 cd ./pyscf/lib
-curl -L "https://github.com/pyscf/pyscf-build-deps/blob/master/pyscf-2.0-deps.tar.gz?raw=true" | tar xzf -
+curl -L "https://github.com/pyscf/pyscf-build-deps/blob/master/pyscf-2.1a-deps.tar.gz?raw=true" | tar xzf -
 mkdir build; cd build
 cmake -DBUILD_LIBXC=OFF -DBUILD_XCFUN=OFF -DBUILD_LIBCINT=OFF ..
 make -j4
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index a333cb171c..c843b578c3 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -17,7 +17,7 @@ jobs:
         uses: docker://pyscf/pyscf-pypa-env:latest
         with:
           entrypoint: /build-wheels.sh
-          #args: http://www.sunqm.net/pyscf/files/bin/pyscf-2.0-deps-openblas.tar.gz
+          #args: http://www.sunqm.net/pyscf/files/bin/pyscf-2.1a-deps.tar.gz
       - name: List available wheels
         run: |
           ls ${{ github.workspace }}/linux-wheels
diff --git a/pyscf/lib/CMakeLists.txt b/pyscf/lib/CMakeLists.txt
index 21b36b7859..68259e6bb5 100644
--- a/pyscf/lib/CMakeLists.txt
+++ b/pyscf/lib/CMakeLists.txt
@@ -145,18 +145,18 @@ if(NOT EXISTS "${PROJECT_SOURCE_DIR}/deps/include/cint.h")
   ExternalProject_Add(libcint
     GIT_REPOSITORY https://github.com/sunqm/libcint.git # libcint is a portable, cross-platform implementation
     # GIT_REPOSITORY https://github.com/sunqm/qcint.git # qcint is an optimized implementation for x86-64 architecture
-    GIT_TAG v4.4.6
+    GIT_TAG v5.1.0
     PREFIX ${PROJECT_BINARY_DIR}/deps
     INSTALL_DIR ${PROJECT_SOURCE_DIR}/deps
-    CMAKE_CACHE_ARGS 
-        -DWITH_F12:STRING=${WITH_F12} 
+    CMAKE_CACHE_ARGS
+        -DWITH_F12:STRING=${WITH_F12}
         -DWITH_RANGE_COULOMB:STRING=1
-        -DWITH_FORTRAN:STRING=0 
-        -DWITH_CINT2_INTERFACE:STRING=0 
+        -DWITH_FORTRAN:STRING=0
+        -DWITH_CINT2_INTERFACE:STRING=0
         -DMIN_EXPCUTOFF:STRING=20
         -DKEEP_GOING:STRING=1
         -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-        -DCMAKE_INSTALL_LIBDIR:PATH=lib 
+        -DCMAKE_INSTALL_LIBDIR:PATH=lib
         -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
         -DBLAS_LIBRARIES:STRING=${BLAS_LIBRARIES}
         -DCMAKE_C_CREATE_SHARED_LIBRARY:STRING=${C_LINK_TEMPLATE}
diff --git a/pyscf/lib/gto/ft_ao.c b/pyscf/lib/gto/ft_ao.c
index 3d3bc4783e..9dd543c173 100644
--- a/pyscf/lib/gto/ft_ao.c
+++ b/pyscf/lib/gto/ft_ao.c
@@ -1017,7 +1017,7 @@ int GTO_aopair_lazy_contract(double complex *gctr, CINTEnvVars *envs,
 
         *jempty = 1;
         for (jp = 0; jp < j_prim; jp++) {
-                envs->aj = aj[jp];
+                envs->aj[0] = aj[jp];
                 if (j_ctr == 1) {
                         fac1j = fac1 * cj[jp];
                 } else {
@@ -1025,7 +1025,7 @@ int GTO_aopair_lazy_contract(double complex *gctr, CINTEnvVars *envs,
                         *iempty = 1;
                 }
                 for (ip = 0; ip < i_prim; ip++) {
-                        envs->ai = ai[ip];
+                        envs->ai[0] = ai[ip];
                         aij = ai[ip] + aj[jp];
                         eij = (ai[ip] * aj[jp] / aij) * rrij;
                         if (eij > EXP_CUTOFF) {
diff --git a/pyscf/lib/gto/ft_ao.h b/pyscf/lib/gto/ft_ao.h
index 10de55eed3..f7f1d113c1 100644
--- a/pyscf/lib/gto/ft_ao.h
+++ b/pyscf/lib/gto/ft_ao.h
@@ -20,7 +20,10 @@
 #include <complex.h>
 #include "cint.h"
 
-#if !defined HAVE_DEFINED_CINTENVVARS_H
+// HAVE_DEFINED_CINTOPT_H is defined in cint.h libcint v5.0. The CINTEnvVars
+// struct definition below is not the same to the one in cint.h . However, it
+// is compatible with both libcint v4 and libcint v5.
+#ifndef HAVE_DEFINED_CINTENVVARS_H
 #define HAVE_DEFINED_CINTENVVARS_H
 typedef struct {
         int *atm;
@@ -66,10 +69,10 @@ typedef struct {
         // Replace them with four words (ai, aj, ak, al)
         //double _padding1;
         //double rirj[3];
-        double ai;
-        double aj;
-        double ak;
-        double al;
+        double ai[1];
+        double aj[1];
+        double ak[1];
+        double al[1];
         double rkrl[3];
         double *rx_in_rijrx;
         double *rx_in_rklrx;
diff --git a/pyscf/lib/gto/ft_ao_deriv.c b/pyscf/lib/gto/ft_ao_deriv.c
index 220c64cc0a..b52dca0d54 100644
--- a/pyscf/lib/gto/ft_ao_deriv.c
+++ b/pyscf/lib/gto/ft_ao_deriv.c
@@ -28,7 +28,7 @@ void GTO_ft_nabla1i(double complex *f, double complex *g, int li, int lj,
                     double *Gv, size_t NGv, CINTEnvVars *envs)
 {
         const int dj = envs->g_stride_j;
-        const double ai2 = -2 * envs->ai;
+        const double ai2 = -2 * envs->ai[0];
         const size_t g_size = envs->g_size * NGv;
         double complex *gx = g;
         double complex *gy = g + g_size;
@@ -64,7 +64,7 @@ void GTO_ft_nabla1j(double complex *f, double complex *g, int li, int lj,
                     double *Gv, size_t NGv, CINTEnvVars *envs)
 {
         const int dj = envs->g_stride_j;
-        const double aj2 = -2 * envs->aj;
+        const double aj2 = -2 * envs->aj[0];
         const size_t g_size = envs->g_size * NGv;
         double complex *gx = g;
         double complex *gy = g + g_size;
