Skip to content

Commit 010f36a

Browse files
author
kosm6966
committed
added grids. seem to be working.
1 parent 714b2fc commit 010f36a

File tree

3 files changed

+553
-254
lines changed

3 files changed

+553
-254
lines changed

examples/occri/multigrid/01-simple_multigrid.py

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
C 0.000000 0.000000 0.000000
4040
C 0.890186 0.890186 0.890186
4141
"""
42-
cell.basis = "gth-szv"
42+
cell.basis = "gth-dzvp"
4343
cell.pseudo = "gth-pbe"
4444
cell.a = numpy.eye(3) * 3.5607
4545
cell.mesh = [24] * 3 # Relatively fine mesh for accuracy
46-
cell.verbose = 0
46+
cell.verbose = 4
4747
cell.build()
4848

4949
print(f"System: {cell.natm} carbon atoms")
@@ -75,106 +75,106 @@
7575
print("\n1. Basic multigrid (3 levels, factor 2)")
7676
mf_mg1 = scf.RHF(cell)
7777
mf_mg1.with_df = MultigridOccRI(mf_mg1)
78-
e_mg1 = mf_mg1.kernel()
78+
# e_mg1 = mf_mg1.kernel()
7979

80-
print(f" Energy: {e_mg1:.8f} Ha")
81-
print(f" Difference from reference: {e_mg1 - e_ref:.2e} Ha")
82-
print(f" Converged: {mf_mg1.converged}")
80+
# print(f" Energy: {e_mg1:.8f} Ha")
81+
# print(f" Difference from reference: {e_mg1 - e_ref:.2e} Ha")
82+
# print(f" Converged: {mf_mg1.converged}")
8383

84-
# Example 2: More aggressive coarsening
85-
print("\n2. Aggressive coarsening (4 levels, factor 3)")
86-
mf_mg2 = scf.RHF(cell)
87-
mf_mg2.with_df = MultigridOccRI(
88-
mf_mg2,
89-
mg_levels=4,
90-
coarsening_factor=3,
91-
mg_method='vcycle'
92-
)
93-
e_mg2 = mf_mg2.kernel()
84+
# # Example 2: More aggressive coarsening
85+
# print("\n2. Aggressive coarsening (4 levels, factor 3)")
86+
# mf_mg2 = scf.RHF(cell)
87+
# mf_mg2.with_df = MultigridOccRI(
88+
# mf_mg2,
89+
# mg_levels=4,
90+
# coarsening_factor=3,
91+
# mg_method='vcycle'
92+
# )
93+
# e_mg2 = mf_mg2.kernel()
9494

95-
print(f" Energy: {e_mg2:.8f} Ha")
96-
print(f" Difference from reference: {e_mg2 - e_ref:.2e} Ha")
97-
print(f" Converged: {mf_mg2.converged}")
95+
# print(f" Energy: {e_mg2:.8f} Ha")
96+
# print(f" Difference from reference: {e_mg2 - e_ref:.2e} Ha")
97+
# print(f" Converged: {mf_mg2.converged}")
9898

99-
# Example 3: Full Multigrid method
100-
print("\n3. Full Multigrid (FMG) method")
101-
mf_mg3 = scf.RHF(cell)
102-
mf_mg3.with_df = MultigridOccRI(
103-
mf_mg3,
104-
mg_levels=3,
105-
coarsening_factor=2,
106-
mg_method='fmg'
107-
)
108-
e_mg3 = mf_mg3.kernel()
99+
# # Example 3: Full Multigrid method
100+
# print("\n3. Full Multigrid (FMG) method")
101+
# mf_mg3 = scf.RHF(cell)
102+
# mf_mg3.with_df = MultigridOccRI(
103+
# mf_mg3,
104+
# mg_levels=3,
105+
# coarsening_factor=2,
106+
# mg_method='fmg'
107+
# )
108+
# e_mg3 = mf_mg3.kernel()
109109

110-
print(f" Energy: {e_mg3:.8f} Ha")
111-
print(f" Difference from reference: {e_mg3 - e_ref:.2e} Ha")
112-
print(f" Converged: {mf_mg3.converged}")
110+
# print(f" Energy: {e_mg3:.8f} Ha")
111+
# print(f" Difference from reference: {e_mg3 - e_ref:.2e} Ha")
112+
# print(f" Converged: {mf_mg3.converged}")
113113

114-
# =============================================================================
115-
# Performance Comparison
116-
# =============================================================================
117-
print("\n" + "=" * 50)
118-
print("Performance Summary")
119-
print("=" * 50)
114+
# # =============================================================================
115+
# # Performance Comparison
116+
# # =============================================================================
117+
# print("\n" + "=" * 50)
118+
# print("Performance Summary")
119+
# print("=" * 50)
120120

121-
print(f"Standard OCCRI: {e_ref:.8f} Ha")
122-
if 'e_mg1' in locals():
123-
print(f"Multigrid (basic): {e_mg1:.8f} Ha (Δ = {e_mg1-e_ref:.2e})")
124-
if 'e_mg2' in locals():
125-
print(f"Multigrid (aggr.): {e_mg2:.8f} Ha (Δ = {e_mg2-e_ref:.2e})")
126-
if 'e_mg3' in locals():
127-
print(f"Multigrid (FMG): {e_mg3:.8f} Ha (Δ = {e_mg3-e_ref:.2e})")
121+
# print(f"Standard OCCRI: {e_ref:.8f} Ha")
122+
# if 'e_mg1' in locals():
123+
# print(f"Multigrid (basic): {e_mg1:.8f} Ha (Δ = {e_mg1-e_ref:.2e})")
124+
# if 'e_mg2' in locals():
125+
# print(f"Multigrid (aggr.): {e_mg2:.8f} Ha (Δ = {e_mg2-e_ref:.2e})")
126+
# if 'e_mg3' in locals():
127+
# print(f"Multigrid (FMG): {e_mg3:.8f} Ha (Δ = {e_mg3-e_ref:.2e})")
128128

129-
print("\nMultigrid benefits:")
130-
print("• Faster convergence for large systems")
131-
print("• Better scaling with basis set size")
132-
print("• Reduced memory requirements")
133-
print("• Systematic error control")
134-
135-
else:
136-
print("\n" + "=" * 50)
137-
print("Multigrid OccRI - Placeholder")
138-
print("=" * 50)
129+
# print("\nMultigrid benefits:")
130+
# print("• Faster convergence for large systems")
131+
# print("• Better scaling with basis set size")
132+
# print("• Reduced memory requirements")
133+
# print("• Systematic error control")
134+
135+
# else:
136+
# print("\n" + "=" * 50)
137+
# print("Multigrid OccRI - Placeholder")
138+
# print("=" * 50)
139139

140-
print("Multigrid OccRI is not yet implemented.")
141-
print("When available, it will provide:")
142-
print("• Accelerated convergence through hierarchical grids")
143-
print("• Better performance for large basis sets")
144-
print("• Systematic control of discretization errors")
145-
print("• V-cycle and Full Multigrid solution methods")
146-
147-
# =============================================================================
148-
# Usage Guide
149-
# =============================================================================
150-
print("\n" + "=" * 50)
151-
print("Multigrid OccRI Usage Guide")
152-
print("=" * 50)
153-
154-
print("""
155-
Basic usage:
156-
from pyscf.occri.multigrid import MultigridOccRI
140+
# print("Multigrid OccRI is not yet implemented.")
141+
# print("When available, it will provide:")
142+
# print("• Accelerated convergence through hierarchical grids")
143+
# print("• Better performance for large basis sets")
144+
# print("• Systematic control of discretization errors")
145+
# print("• V-cycle and Full Multigrid solution methods")
146+
147+
# # =============================================================================
148+
# # Usage Guide
149+
# # =============================================================================
150+
# print("\n" + "=" * 50)
151+
# print("Multigrid OccRI Usage Guide")
152+
# print("=" * 50)
153+
154+
# print("""
155+
# Basic usage:
156+
# from pyscf.occri.multigrid import MultigridOccRI
157157

158-
mf = scf.RHF(cell)
159-
mf.with_df = MultigridOccRI(mf, mg_levels=3, coarsening_factor=2)
160-
energy = mf.kernel()
161-
162-
Parameters:
163-
mg_levels : Number of multigrid levels (2-5 recommended)
164-
coarsening_factor : Grid coarsening ratio (2-3 typical)
165-
mg_method : Solution method ('vcycle', 'fmg')
166-
167-
When to use multigrid OCCRI:
168-
• Large basis sets (>100 AOs)
169-
• Dense k-point sampling
170-
• When standard OCCRI is memory-limited
171-
• Systems requiring high accuracy
172-
173-
Performance tips:
174-
• Start with mg_levels=3, coarsening_factor=2
175-
• Use 'fmg' for better initial guess
176-
• Monitor convergence vs. mg_levels
177-
• Balance accuracy vs. computational cost
178-
""")
179-
180-
print("Example completed successfully!")
158+
# mf = scf.RHF(cell)
159+
# mf.with_df = MultigridOccRI(mf, mg_levels=3, coarsening_factor=2)
160+
# energy = mf.kernel()
161+
162+
# Parameters:
163+
# mg_levels : Number of multigrid levels (2-5 recommended)
164+
# coarsening_factor : Grid coarsening ratio (2-3 typical)
165+
# mg_method : Solution method ('vcycle', 'fmg')
166+
167+
# When to use multigrid OCCRI:
168+
# • Large basis sets (>100 AOs)
169+
# • Dense k-point sampling
170+
# • When standard OCCRI is memory-limited
171+
# • Systems requiring high accuracy
172+
173+
# Performance tips:
174+
# • Start with mg_levels=3, coarsening_factor=2
175+
# • Use 'fmg' for better initial guess
176+
# • Monitor convergence vs. mg_levels
177+
# • Balance accuracy vs. computational cost
178+
# """)
179+
180+
# print("Example completed successfully!")

pyscf/occri/multigrid/__init__.py

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from pyscf.gto.mole import decontract_basis
2929
from pyscf.gto import uncontract
30+
from pyscf.occri.multigrid.mg_grids import build_grids
3031

3132

3233

@@ -73,16 +74,19 @@ def __init__(self,
7374
self.rcut_epsilon=rcut_epsilon
7475
self.ke_epsilon=ke_epsilon
7576
self.incore=incore
77+
self.k_grid_mesh=None
7678

7779
# Print all attributes
78-
print()
79-
print("******** <class 'ISDFX'> ********", flush=True)
80-
for key, value in vars(self).items():
81-
if key not in ['cell']:
82-
print(f"{key}: {value}", flush=True)
80+
if self.cell.verbose > 4:
81+
print()
82+
print("******** <class 'ISDFX'> ********", flush=True)
83+
for key, value in vars(self).items():
84+
if key in ['method', 'kmesh', 'get_k', 'alpha_cutoff', 'rcut_epsilon', 'ke_epsilon', 'incore']:
85+
print(f"{key}: {value}", flush=True)
86+
print()
8387

8488
self.to_uncontracted_basis()
85-
self.build_grids()
89+
build_grids(self)
8690

8791

8892

@@ -100,33 +104,24 @@ def to_uncontracted_basis(myisdf):
100104
c = decontract_basis(cell, aggregate=True)[1]
101105
myisdf.c = c.astype(numpy.complex128) if nk > 1 else c.astype(numpy.float64)
102106

103-
104-
def build_grids(self):
105-
106-
"""Initialize multigrid components"""
107-
"""
108-
Construct multi-grid system for ISDF calculations.
109-
"""
110-
# Exchange grids
111-
from pyscf.occri.multigrid.mg_grids import make_exchange_lists
112-
mg_k, atomgrids_k = make_exchange_lists(self)
113-
self.full_grids_k = mg_k
114-
self.atom_grids_k = atomgrids_k
115-
116-
# The atom-centered grid points are assigned to each atom-grid.
117-
get_atomgrid_coords(mydf, atomgrids, mg[-1])
118-
# Non-zero functions are assigned to each atom-grid.
119-
place_functions_on_atomgrids(mydf, atomgrids, mg[-1])
120-
121-
def primitive_gto_cutoff(cell, rcut_epsilon, shell_idx):
107+
def primitive_gto_cutoff(self, shell_idx):
122108
"""Cutoff raidus, above which each shell decays to a value less than the
123109
required precsion"""
124110
rcut = []
111+
rcut_epsilon = self.rcut_epsilon
112+
cell = self.cell_unc
125113
for ib in shell_idx:
126114
es = cell.bas_exp(ib)
127115
r = (-numpy.log(rcut_epsilon) / es) ** 0.5
128-
rcut.append(r)
129-
return rcut
116+
rcut.extend(r)
117+
return numpy.asarray(rcut, numpy.float64)
118+
119+
def primitive_gto_exponent(self, rmin):
120+
"""
121+
Calculate primitive Gaussian-type orbital exponent based on minimum radius and cutoff precision.
122+
"""
123+
rcut_epsilon = self.rcut_epsilon
124+
return -numpy.log(rcut_epsilon) / max(rmin**2, 1e-12)
130125

131126
def get_jk_kpts(self, dms, exxdiv=None):
132127
"""
@@ -147,7 +142,3 @@ def get_jk_kpts(self, dms, exxdiv=None):
147142
# TODO: Implement multigrid exchange evaluation
148143
# For now, fallback to standard OCCRI
149144
return self.get_k(dms, exxdiv)
150-
151-
152-
# Export main classes
153-
__all__ = ['MultigridOccRI', 'GridHierarchy', 'MGInterpolation']

0 commit comments

Comments
 (0)