Task: Improve to_hf for pbc dft (pyscf/pyscf#2172)

org: pyscf
repo: pyscf
pr_number: 2172
issue_title: Improve to_hf for pbc dft
issue_body: * Fix to_hf method for k-point dft methods
* FIx old compiler compatibility issue
resolved_issues: number: 2157
title: KeyError in pyscf.pbc.df.df_jk?
body: With
```
from pyscf.pbc import gto, scf

cell = gto.Cell()
cell.atom='''
C 0.000000000000   0.000000000000   0.000000000000
C 1.685068664391   1.685068664391   1.685068664391
'''
cell.basis = 'gth-szv'
cell.pseudo = 'gth-pade'
cell.a = '''
0.000000000, 3.370137329, 3.370137329
3.370137329, 0.000000000, 3.370137329
3.370137329, 3.370137329, 0.000000000'''
cell.unit = 'B'
cell.verbose = 5
cell.build()

#
# KDFT and KGW with 2x2x2 k-points
#
kpts = cell.make_kpts([2,2,2])
kmf = scf.KRKS(cell, xc='pbe').density_fit()
kmf.kpts = kpts
emf = kmf.kernel()

myrhf = kmf.to_hf()
veff = myrhf.get_veff()

```

I get the following error:

```
Traceback (most recent call last):
  File "/home/chillenb/src/gwscripts/kpttest.py", line 28, in <module>
    veff = myrhf.get_veff()
           ^^^^^^^^^^^^^^^^
  File "/home/chillenb/mambaforge/envs/rttddft/lib/python3.11/site-packages/pyscf/pbc/scf/khf.py", line 541, in get_veff
    vj, vk = self.get_jk(cell, dm_kpts, hermi, kpts, kpts_band)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chillenb/mambaforge/envs/rttddft/lib/python3.11/site-packages/pyscf/pbc/scf/khf.py", line 529, in get_jk
    vj, vk = self.with_df.get_jk(dm_kpts, hermi, kpts, kpts_band,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chillenb/mambaforge/envs/rttddft/lib/python3.11/site-packages/pyscf/pbc/df/df.py", line 443, in get_jk
    vk = df_jk.get_k_kpts(self, dm, hermi, kpts, kpts_band, exxdiv)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chillenb/mambaforge/envs/rttddft/lib/python3.11/site-packages/pyscf/pbc/df/df_jk.py", line 613, in get_k_kpts
    make_kpt(ki, kj, True)
  File "/home/chillenb/mambaforge/envs/rttddft/lib/python3.11/site-packages/pyscf/pbc/df/df_jk.py", line 446, in make_kpt
    for LpqR, LpqI, sign in mydf.sr_loop((kpti,kptj), max_memory, False):
  File "/home/chillenb/mambaforge/envs/rttddft/lib/python3.11/site-packages/pyscf/pbc/df/df.py", line 355, in sr_loop
    with _load3c(self._cderi, self._dataname, kpti_kptj) as j3c:
  File "/home/chillenb/mambaforge/envs/rttddft/lib/python3.11/site-packages/pyscf/pbc/df/df.py", line 692, in __enter__
    return self.getitem(*self.kpti_kptj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chillenb/mambaforge/envs/rttddft/lib/python3.11/site-packages/pyscf/pbc/df/df.py", line 746, in getitem
    raise KeyError(f'Key {key} not found')
KeyError: 'Key j3c/8 not found'
```


The fact that the 3c2e integrals should now be computed, and `_j_only` reset, is not getting detected. Is there a recommended way to do this manually?

Would it be bad design to add an option to scf.KRKS.density_fit() so that, if I need to calculate Vk later, I can override the default value of _j_only?


(PySCF 2.5.0 installed from conda-forge)



Reference solution:
fix_patch: diff --git a/pyscf/lib/dft/utils.c b/pyscf/lib/dft/utils.c
index 04ef8e5b2f..fc27b4a48f 100644
--- a/pyscf/lib/dft/utils.c
+++ b/pyscf/lib/dft/utils.c
@@ -52,11 +52,20 @@ void get_gga_vrho_gs(double complex *out, double complex *vrho_gs, double comple
     int i;
     int ngrid2 = 2 * ngrid;
     double complex fac = -2. * _Complex_I;
-    #pragma omp parallel for simd schedule(static)
+#pragma omp parallel
+{
+    double complex v;
+// ensure OpenMP 4.0
+#if defined _OPENMP && _OPENMP >= 201307
+    #pragma omp for simd schedule(static)
+#else
+    #pragma omp for schedule(static)
+#endif
     for (i = 0; i < ngrid; i++) {
-        out[i] = ( Gv[i*3]   * vsigma1_gs[i]
-                  +Gv[i*3+1] * vsigma1_gs[i+ngrid]
-                  +Gv[i*3+2] * vsigma1_gs[i+ngrid2]) * fac + vrho_gs[i];
-        out[i] *= weight;
+        v = ( Gv[i*3]   * vsigma1_gs[i]
+             +Gv[i*3+1] * vsigma1_gs[i+ngrid]
+             +Gv[i*3+2] * vsigma1_gs[i+ngrid2]) * fac + vrho_gs[i];
+        out[i] = v * weight;
     }
 }
+}
diff --git a/pyscf/pbc/dft/kgks.py b/pyscf/pbc/dft/kgks.py
index 7774f2e36e..fb9e1f2f68 100644
--- a/pyscf/pbc/dft/kgks.py
+++ b/pyscf/pbc/dft/kgks.py
@@ -146,7 +146,14 @@ def nuc_grad_method(self):
 
     def to_hf(self):
         '''Convert to KGHF object.'''
-        from pyscf.pbc import scf
-        return self._transfer_attrs_(scf.KGHF(self.cell, self.kpts))
+        from pyscf.pbc import scf, df
+        out = self._transfer_attrs_(scf.KGHF(self.cell, self.kpts))
+
+        # Pure functionals only construct J-type integrals. Enable all integrals for KHF.
+        if (not self._numint.libxc.is_hybrid_xc(self.xc) and
+            len(self.kpts) > 1 and getattr(self.with_df, '_j_only', False)):
+            out.with_df._j_only = False
+            out.with_df.reset()
+        return out
 
     to_gpu = lib.to_gpu
diff --git a/pyscf/pbc/dft/krks.py b/pyscf/pbc/dft/krks.py
index 6278605de9..d690647879 100644
--- a/pyscf/pbc/dft/krks.py
+++ b/pyscf/pbc/dft/krks.py
@@ -181,25 +181,13 @@ def nuc_grad_method(self):
 
     def to_hf(self):
         '''Convert to KRHF object.'''
-        from pyscf.pbc import scf
-        return self._transfer_attrs_(scf.KRHF(self.cell, self.kpts))
+        from pyscf.pbc import scf, df
+        out = self._transfer_attrs_(scf.KRHF(self.cell, self.kpts))
+        # Pure functionals only construct J-type integrals. Enable all integrals for KHF.
+        if (not self._numint.libxc.is_hybrid_xc(self.xc) and
+            len(self.kpts) > 1 and getattr(self.with_df, '_j_only', False)):
+            out.with_df._j_only = False
+            out.with_df.reset()
+        return out
 
     to_gpu = lib.to_gpu
-
-
-if __name__ == '__main__':
-    from pyscf.pbc import gto
-    cell = gto.Cell()
-    cell.unit = 'A'
-    cell.atom = 'C 0.,  0.,  0.; C 0.8917,  0.8917,  0.8917'
-    cell.a = '''0.      1.7834  1.7834
-                1.7834  0.      1.7834
-                1.7834  1.7834  0.    '''
-
-    cell.basis = 'gth-szv'
-    cell.pseudo = 'gth-pade'
-    cell.verbose = 7
-    cell.output = '/dev/null'
-    cell.build()
-    mf = KRKS(cell, cell.make_kpts([2,1,1]))
-    print(mf.kernel())
diff --git a/pyscf/pbc/dft/kroks.py b/pyscf/pbc/dft/kroks.py
index 1d2a1198f8..640bf05e97 100644
--- a/pyscf/pbc/dft/kroks.py
+++ b/pyscf/pbc/dft/kroks.py
@@ -61,25 +61,13 @@ def dump_flags(self, verbose=None):
 
     def to_hf(self):
         '''Convert to KROHF object.'''
-        from pyscf.pbc import scf
-        return self._transfer_attrs_(scf.KROHF(self.cell, self.kpts))
+        from pyscf.pbc import scf, df
+        out = self._transfer_attrs_(scf.KROHF(self.cell, self.kpts))
+        # Pure functionals only construct J-type integrals. Enable all integrals for KHF.
+        if (not self._numint.libxc.is_hybrid_xc(self.xc) and
+            len(self.kpts) > 1 and getattr(self.with_df, '_j_only', False)):
+            out.with_df._j_only = False
+            out.with_df.reset()
+        return out
 
     to_gpu = lib.to_gpu
-
-
-if __name__ == '__main__':
-    from pyscf.pbc import gto
-    cell = gto.Cell()
-    cell.unit = 'A'
-    cell.atom = 'C 0.,  0.,  0.; C 0.8917,  0.8917,  0.8917'
-    cell.a = '''0.      1.7834  1.7834
-                1.7834  0.      1.7834
-                1.7834  1.7834  0.    '''
-
-    cell.basis = 'gth-szv'
-    cell.pseudo = 'gth-pade'
-    cell.verbose = 7
-    cell.output = '/dev/null'
-    cell.build()
-    mf = KROKS(cell, cell.make_kpts([2,1,1]))
-    print(mf.kernel())
diff --git a/pyscf/pbc/dft/kuks.py b/pyscf/pbc/dft/kuks.py
index ac66f973bc..732adcfa32 100644
--- a/pyscf/pbc/dft/kuks.py
+++ b/pyscf/pbc/dft/kuks.py
@@ -157,25 +157,13 @@ def nuc_grad_method(self):
 
     def to_hf(self):
         '''Convert to KUHF object.'''
-        from pyscf.pbc import scf
-        return self._transfer_attrs_(scf.KUHF(self.cell, self.kpts))
+        from pyscf.pbc import scf, df
+        out = self._transfer_attrs_(scf.KUHF(self.cell, self.kpts))
+        # Pure functionals only construct J-type integrals. Enable all integrals for KHF.
+        if (not self._numint.libxc.is_hybrid_xc(self.xc) and
+            len(self.kpts) > 1 and getattr(self.with_df, '_j_only', False)):
+            out.with_df._j_only = False
+            out.with_df.reset()
+        return out
 
     to_gpu = lib.to_gpu
-
-
-if __name__ == '__main__':
-    from pyscf.pbc import gto
-    cell = gto.Cell()
-    cell.unit = 'A'
-    cell.atom = 'C 0.,  0.,  0.; C 0.8917,  0.8917,  0.8917'
-    cell.a = '''0.      1.7834  1.7834
-                1.7834  0.      1.7834
-                1.7834  1.7834  0.    '''
-
-    cell.basis = 'gth-szv'
-    cell.pseudo = 'gth-pade'
-    cell.verbose = 7
-    cell.output = '/dev/null'
-    cell.build()
-    mf = KUKS(cell, cell.make_kpts([2,1,1]))
-    print(mf.kernel())