|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +OCCRI Gamma Point Examples: Getting Started |
| 5 | +
|
| 6 | +This example demonstrates the basic usage of OCCRI (Occupied Orbital Coulomb |
| 7 | +Resolution of Identity) for efficient exact exchange evaluation in periodic |
| 8 | +systems at the Gamma point (single k-point). |
| 9 | +
|
| 10 | +OCCRI provides significant speedup over standard FFTDF while maintaining |
| 11 | +chemical accuracy for hybrid DFT and Hartree-Fock calculations. |
| 12 | +
|
| 13 | +Key concepts covered: |
| 14 | +- Basic OCCRI setup and usage |
| 15 | +- Different SCF methods (RHF, UHF, RKS, UKS) |
| 16 | +- How to set up periodic systems |
| 17 | +- Performance and accuracy considerations |
| 18 | +""" |
| 19 | + |
| 20 | +import numpy |
| 21 | +from pyscf.occri import OCCRI |
| 22 | +from pyscf.pbc import gto, scf |
| 23 | + |
| 24 | +print('=== OCCRI Gamma Point Tutorial ===') |
| 25 | +print('This example shows basic OCCRI usage for single k-point calculations.\n') |
| 26 | + |
| 27 | +# ============================================================================= |
| 28 | +# System Setup |
| 29 | +# ============================================================================= |
| 30 | +print('Setting up diamond structure...') |
| 31 | + |
| 32 | +# Set up diamond structure (2 carbon atoms per unit cell) |
| 33 | +cell = gto.Cell() |
| 34 | +cell.atom = """ |
| 35 | + C 0.000000 0.000000 0.000000 |
| 36 | + C 0.890186 0.890186 0.890186 |
| 37 | +""" |
| 38 | +cell.basis = 'gth-szv' # Compact basis set |
| 39 | +cell.pseudo = 'gth-pbe' # Pseudopotentials |
| 40 | +cell.a = numpy.eye(3) * 3.5607 # Diamond lattice parameter (Å) |
| 41 | +cell.mesh = [20] * 3 # FFT mesh |
| 42 | +cell.verbose = 0 |
| 43 | +cell.build() |
| 44 | + |
| 45 | +print(f'System: {" ".join(cell.atom_symbol(i) for i in range(cell.natm))} ({cell.natm} atoms)') |
| 46 | +print(f'Basis: {cell.basis}') |
| 47 | +print(f'Lattice parameter: {cell.a[0, 0]:.3f} Å') |
| 48 | +print(f'FFT mesh: {cell.mesh} ({numpy.prod(cell.mesh)} total points)') |
| 49 | + |
| 50 | +# ============================================================================= |
| 51 | +# Example 1: Basic OCCRI usage |
| 52 | +# ============================================================================= |
| 53 | +print('\n' + '=' * 50) |
| 54 | +print('Example 1: Basic OCCRI Setup') |
| 55 | +print('=' * 50) |
| 56 | + |
| 57 | +print('\n1a. Restricted Hartree-Fock (RHF)') |
| 58 | + |
| 59 | +# Standard syntax: attach OCCRI to mean-field object |
| 60 | +mf_rhf = scf.RHF(cell) |
| 61 | +mf_rhf.with_df = OCCRI.from_mf(mf_rhf) # This line enables OCCRI |
| 62 | +e_rhf = mf_rhf.kernel() |
| 63 | + |
| 64 | +print(f' Energy: {e_rhf:.6f} Hartree') |
| 65 | + |
| 66 | +print('\n1b. Unrestricted Hartree-Fock (UHF)') |
| 67 | +mf_uhf = scf.UHF(cell) |
| 68 | +mf_uhf.with_df = OCCRI.from_mf(mf_uhf) |
| 69 | +e_uhf = mf_uhf.kernel() |
| 70 | +print(f' Energy: {e_uhf:.6f} Hartree') |
| 71 | + |
| 72 | +# ============================================================================= |
| 73 | +# Example 2: Hybrid DFT calculations |
| 74 | +# ============================================================================= |
| 75 | +print('\n' + '=' * 50) |
| 76 | +print('Example 2: Hybrid DFT with OCCRI') |
| 77 | +print('=' * 50) |
| 78 | + |
| 79 | +print('\n2a. PBE0 (25% exact exchange)') |
| 80 | +mf_pbe0 = scf.RKS(cell) |
| 81 | +mf_pbe0.xc = 'pbe0' |
| 82 | +mf_pbe0.with_df = OCCRI.from_mf(mf_pbe0) # OCCRI handles exact exchange |
| 83 | +e_pbe0 = mf_pbe0.kernel() |
| 84 | +print(f' PBE0 energy: {e_pbe0:.6f} Hartree') |
| 85 | + |
| 86 | +print('\n2b. HSE06 range-separated hybrid') |
| 87 | +mf_hse = scf.RKS(cell) |
| 88 | +mf_hse.xc = 'hse06' # 25% short-range exact exchange |
| 89 | +mf_hse.with_df = OCCRI.from_mf(mf_hse) |
| 90 | +e_hse = mf_hse.kernel() |
| 91 | +print(f' HSE06 energy: {e_hse:.6f} Hartree') |
| 92 | + |
| 93 | +# ============================================================================= |
| 94 | +# Example 3: Configuration options |
| 95 | +# ============================================================================= |
| 96 | +print('\n' + '=' * 50) |
| 97 | +print('Example 3: OCCRI Configuration') |
| 98 | +print('=' * 50) |
| 99 | + |
| 100 | +print('\n3a. Python implementation') |
| 101 | +mf_python = scf.RHF(cell) |
| 102 | +mf_python.with_df = OCCRI.from_mf(mf_python, disable_c=True) |
| 103 | +e_python = mf_python.kernel() |
| 104 | +print(f' Python: {e_python:.6f} Ha') |
| 105 | + |
| 106 | +print('\n3b. C extension') |
| 107 | +mf_c = scf.RHF(cell) |
| 108 | +mf_c.with_df = OCCRI.from_mf(mf_c, disable_c=False) |
| 109 | +e_c = mf_c.kernel() |
| 110 | +print(f' C extension: {e_c:.6f} Ha') |
| 111 | +print(f' Difference: {abs(e_python - e_c):.2e} Ha') |
| 112 | + |
| 113 | +# ============================================================================= |
| 114 | +# Example 4: FFT mesh convergence study |
| 115 | +# ============================================================================= |
| 116 | +print('\n' + '=' * 60) |
| 117 | +print('Example 4: FFT mesh convergence study') |
| 118 | +print('=' * 60) |
| 119 | + |
| 120 | +print('OCCRI accuracy depends on FFT mesh density. This example shows') |
| 121 | +print('how to converge the mesh size for reliable results.\n') |
| 122 | + |
| 123 | +# Test different mesh sizes - keep k-points fixed |
| 124 | +mesh_sizes = [25, 27, 29, 31] # FFT mesh dimensions |
| 125 | +energies = [] |
| 126 | + |
| 127 | +for mesh_size in mesh_sizes: |
| 128 | + print(f'4.{mesh_size}: [{mesh_size}]³ mesh ({mesh_size**3} total points)') |
| 129 | + |
| 130 | + # Create new cell with different mesh |
| 131 | + test_cell = cell.copy() |
| 132 | + test_cell.mesh = [mesh_size] * 3 |
| 133 | + test_cell.build() |
| 134 | + |
| 135 | + mf_test = scf.RHF(test_cell) |
| 136 | + mf_test.with_df = OCCRI.from_mf(mf_test) |
| 137 | + mf_test.verbose = 1 # Reduce output for cleaner display |
| 138 | + |
| 139 | + e_test = mf_test.kernel() |
| 140 | + energies.append(e_test) |
| 141 | + print(f' Energy: {e_test:.8f} Ha') |
| 142 | + |
| 143 | + if len(energies) > 1: |
| 144 | + diff = e_test - energies[-2] |
| 145 | + print(f' Change: {diff:.8f} Ha ({abs(diff) * 1000:.2f} mHa)') |
| 146 | + |
| 147 | + # Check convergence |
| 148 | + if abs(diff) < 1e-6: |
| 149 | + print(' ✓ Converged to μHa accuracy') |
| 150 | + elif abs(diff) < 5e-6: |
| 151 | + print(' ✓ Converged to 5 μHa accuracy') |
| 152 | + else: |
| 153 | + print(' ⚠ Not yet converged') |
| 154 | + print() |
| 155 | + |
| 156 | +print('Mesh convergence guidelines:') |
| 157 | +print('• Energy differences < 1-5 μHa/atom typically sufficient') |
| 158 | +print('• Denser meshes → higher accuracy but slower calculation') |
| 159 | + |
| 160 | +# ============================================================================= |
| 161 | +# Usage guide |
| 162 | +# ============================================================================= |
| 163 | +print('\n' + '=' * 50) |
| 164 | +print('OCCRI Usage Guide') |
| 165 | +print('=' * 50) |
| 166 | + |
| 167 | +print( |
| 168 | + """ |
| 169 | +Quick start: |
| 170 | + mf = scf.RHF(cell) # Create SCF object |
| 171 | + mf.with_df = OCCRI.from_mf(mf) # Enable OCCRI |
| 172 | + energy = mf.kernel() # Run calculation |
| 173 | +
|
| 174 | +When to use OCCRI: |
| 175 | + • Hartree-Fock calculations (exact exchange) |
| 176 | + • Hybrid functionals (PBE0, HSE06, etc.) |
| 177 | + • When standard FFTDF is too slow |
| 178 | + • Large basis sets |
| 179 | +
|
| 180 | +Configuration options: |
| 181 | + OCCRI.from_mf(mf) # Default (use C if available) |
| 182 | + OCCRI.from_mf(mf, disable_c=True) # Force Python implementation |
| 183 | + OCCRI.from_mf(mf, disable_c=False) # Force C implementation |
| 184 | +
|
| 185 | +Compatible methods: |
| 186 | + • scf.RHF, scf.UHF # Hartree-Fock |
| 187 | + • scf.RKS, scf.UKS # DFT (any functional) |
| 188 | + • Gamma point calculations (use 02-kpoint for k-points) |
| 189 | +
|
| 190 | +Performance tips: |
| 191 | + • Converge FFT mesh: start low, increase until energy changes < 1-5 μHa/atom |
| 192 | + • OCCRI scaling: O(N_occ²) vs FFTDF O(N_AO²) |
| 193 | + • Most beneficial when N_AO >> N_occ (large basis, few electrons) |
| 194 | +""" |
| 195 | +) |
| 196 | + |
| 197 | +print('Example completed successfully!') |
0 commit comments