MADNESS 0.10.1
exchange_commutator.h
Go to the documentation of this file.
1//
2// exchange_commutator.h — library-style API for evaluating [K̂, f₁₂] |ij⟩
3//
4// Phase A: thin wrappers around existing CCPotentials::apply_Kfxy /
5// apply_KffK_low_rank / apply_KffK_low_rank_direct, plus the scalar
6// split-α diagnostic extracted from benchmark_exchange_commutator.cc.
7// All entry points return a KffKResult with uniform wall-time / memory /
8// rank instrumentation; all diagnostics run through a single diagnose()
9// function operating on a harmonic-basis projection of the 6D result.
10//
11
12#ifndef MADNESS_CHEM_EXCHANGE_COMMUTATOR_H
13#define MADNESS_CHEM_EXCHANGE_COMMUTATOR_H
14
20
21namespace madness {
22
24
25 ExchangeCommutator() = default;
26
27 /// Uniform result envelope for every algorithm path. Each "piece" may
28 /// be empty when the algorithm only produces the combined commutator
29 /// (e.g. lrf-split-alpha constructs KffK directly).
30 struct KffKResult {
31 std::vector<CCPairFunction<double, 6>> Kf; ///< K̂ f₁₂ | i j ⟩
32 std::vector<CCPairFunction<double, 6>> fK; ///< f₁₂ K̂ | i j ⟩
33 std::vector<CCPairFunction<double, 6>> KffK; ///< [K̂, f₁₂] | i j ⟩ = Kf − fK (signs baked in)
34 double t_wall = 0.0; ///< wall time (s)
35 double mem_gb = 0.0; ///< combined size of pair factors (GByte)
36 long rank = 0; ///< combined LRF rank (0 for 6D)
37 std::string algo; ///< "6d" / "lrf" / "lrf-direct" / "lrf-split-alpha"
38 };
39
40 /// Options for the split-α LRF k-commutator variant
41 /// (see lrf_k_commutator_analysis.md and lrf_k_commutator_results.md).
43 double alpha_star = 1.0e4; ///< GFit partition threshold; α_μ > α* is discarded
44 double lo = 1.0e-6;
45 double hi = 10.0;
46 double eps_gfit = 1.0e-6;
47 };
48
49 /// Options for the three-range LRF k-commutator variant.
50 /// Partition the Coulomb GFit into:
51 /// * diffuse (α_μ < alpha_lo): wide Gaussians, large-ve LRF, low rank
52 /// * medium (alpha_lo ≤ α_μ ≤ alpha_hi): tighter LRF
53 /// * tight (α_μ > alpha_hi): discard (δ-cancellation in commutator)
54 /// See lrf_three_range_gfit.md for the derivation.
56 double alpha_lo = 1.0; ///< diffuse / medium boundary
57 double alpha_hi = 1.0e4; ///< medium / tight boundary (≡ alpha_star)
58 double lo = 1.0e-6;
59 double hi = 10.0;
60 double eps_gfit = 1.0e-6;
61 /// Per-range LRF construction parameters; both default-constructed.
62 /// Caller is expected to set radius / volume_element / tol / lmax /
63 /// f12type per range before invocation.
66 /// If true, build the medium-range LRF via project_from_operator
67 /// (Taylor / direct construction) instead of random-Y project().
68 /// Mandatory for the medium slab in isolation: random-Y stalls at
69 /// large error without diffuse anchor functions.
70 bool medium_use_taylor = true;
71 /// Max Taylor order for project_from_operator (passed straight
72 /// through; default matches lowrankfunction.h's own default).
74 /// If true, the medium slab is treated by a *6D* apply with a
75 /// partial-Coulomb operator (only the medium Gaussians) instead
76 /// of any LRF. The diffuse slab still uses the LRF path.
77 /// Mutually exclusive with `medium_use_taylor`; takes precedence.
78 bool medium_use_6d = false;
79 };
80
81
82 /// compute the LRF of the exchange operator: K(r,r') = \sum_k k(r)g(r,r') R2(r') k(r')
83 ///
84 /// apply K to a function f via: Kf(r) = inner(K,f,p2,p1);
85 /// note K is not self-adjoint due to the ncf factor
87 World& world,
88 const Info& info,
90 const LowRankFunctionParameters& lrfparam);
91
92 // ---------------------------------------------------------------------
93 // Top-level entry points — each produces a KffKResult wrapping one of
94 // the four existing algorithm paths.
95 // ---------------------------------------------------------------------
96
97 /// 6D algorithm: Kf via apply_Kfxy, fK via make_f_xy_macrotask(Kx,...).
98 /// Mirrors the body of test_kcomm_6d in the original benchmark file.
100 World& world,
101 const CCFunction<double, 3>& phi_i,
102 const CCFunction<double, 3>& phi_j,
103 const Info& info,
104 const real_convolution_6d* Gscreen = nullptr);
105
106 // ---------------------------------------------------------------------
107 // Split-α k-commutator. Scalar-only variant for Phase A — 6D pair
108 // assembly is deferred.
109 // ---------------------------------------------------------------------
110
111 /// Assemble the 6D exchange commutator [K̂, f] |ij⟩ using the split-α
112 /// LRF k-commutator algorithm. Sums over occupied orbitals in
113 /// info.mo_bra/info.mo_ket. Currently assumes phi_i == phi_j
114 /// (symmetric case) and exploits swap_particles for the K̂₂ piece.
115 ///
116 /// Output pair layout:
117 /// result.pair[0] = Σ_k Σ_ρ g_kρ(1) · (phi_j · A_kρ)(2) (separable)
118 /// result.pair[1] = Σ_k Σ_ρ B_kρ · (g_kρ·phi_i)(1) · f(1,2) · phi_j(2)
119 /// with coefficient −1
120 /// plus their particle-2 mirrors when i == j.
122 World& world,
123 const CCFunction<double, 3>& phi_i,
124 const CCFunction<double, 3>& phi_j,
125 const Info& info,
126 const LowRankFunction<double,6>& exchange_op,
127 const LowRankFunctionParameters& lrfparam,
128 const SplitAlphaOptions& opt);
129
130 /// Three-range LRF k-commutator: builds two LRFs (diffuse + medium) with
131 /// independent LowRankFunctionParameters, concatenates their g/h vectors
132 /// and assembles Kf / fK / KffK with the same algebra as
133 /// apply_KffK_lowrank_split_alpha. Tight terms (α_μ > opt.alpha_hi) are
134 /// discarded. Returns KffKResult with algo = "lrf-three-range" and rank
135 /// = rank_diffuse + rank_medium (printed individually in t_wall summary).
137 World& world,
138 const CCFunction<double, 3>& phi_i,
139 const CCFunction<double, 3>& phi_j,
140 const Info& info,
141 const ThreeRangeOptions& opt);
142
143 // ---------------------------------------------------------------------
144 // [K̂,f] diagnostic — plain ⟨ab| projection vs analytic 3D reference
145 // ---------------------------------------------------------------------
146
147 /// Diagnose ⟨ab | [K̂,f₁₂] | ij⟩ (no G, no Q₁₂): the 6D pieces are
148 /// projected onto the observer basis and compared against the analytic
149 /// Kf/fK matrix elements from kf_provider/fk_provider evaluated over the
150 /// plain ⟨ab| bra. Unassigned CCPairFunctions are skipped.
151 ///
152 /// NOTE: diagnose_KffK, diagnose_GKffK and diagnose_GQKffK share the same
153 /// signature; parameters not needed by a particular diagnostic are
154 /// ignored (here: energy — pass any value).
155 /// Returns a DiagnosticMatrix with entries "Kf", "fK", "KffK" (= Kf − fK).
156 /// ref=3D Exchange+f12 analytic formula, result=6D projection, error=||ref-result||.
158 World& world,
159 const std::vector<CCPairFunction<double,6>>& Kf_cc,
160 const std::vector<CCPairFunction<double,6>>& fK_cc,
161 const real_function_3d& phi_i,
162 const real_function_3d& phi_j,
163 const real_function_3d& Kphi_i,
164 const real_function_3d& Kphi_j,
165 const Info& info,
166 double energy,
167 const std::vector<real_function_3d>& aobasis,
168 bool orthonormalize_basis = true) const;
169
170 // ---------------------------------------------------------------------
171 // G·[K̂,f] diagnostics — Schwinger-quadrature 3D reference vs 6D projection
172 // ---------------------------------------------------------------------
173
174 /// Diagnose ⟨ab | G [K̂,f₁₂] | ij⟩ by comparing a 6D projection against
175 /// a 3D Schwinger eigentime quadrature.
176 /// Returns a DiagnosticMatrix with entries "GKf", "GfK", "GKffK".
177 /// ref=3D Schwinger quadrature, result=6D projection <ab|G·piece|ij>, error=||ref-result||.
178 ///
179 /// The 6D references GKf_cc and GfK_cc are the G-applied commutator pieces
180 /// as CCPairFunction vectors. Accepting CCPairFunction (instead of a bare
181 /// real_function_6d) allows any algorithm variant to be tested:
182 ///
183 /// * 6D path (apply_KffK_6d): Kf and fK are pure-6D; caller applies G6d
184 /// via apply(G6d, kffk.Kf[0].get_function()) and wraps in a vector.
185 /// * LRF path (apply_KffK_lowrank_split_alpha, three_range): Kf may be
186 /// decomposed. Caller converts to pure (sum outer products), applies G6d,
187 /// and passes the result wrapped in a CCPairFunction vector.
188 /// * Mixed/multi-piece results: sum all pieces (each entry is summed inside).
189 ///
190 /// The 6D reference is projected using the same partial_inner / matrix_inner
191 /// machinery as diagnose(), handling both pure and decomposed CCPairFunctions.
192 ///
193 /// The 3D Schwinger formulas use only K̂, K̂†, and the Slater f₁₂:
194 ///
195 /// G Kf: ⟨ã b̃ | K̂₁ f₁₂ | φᵢ φⱼ⟩ = inner((K̂†ã)·φᵢ, f₁₂(b̃·φⱼ)) + K̂₂ swap
196 /// G fK: ⟨ã b̃ | f₁₂ K̂₁ | φᵢ φⱼ⟩ = inner(ã·K̂φᵢ, f₁₂(b̃·φⱼ)) + K̂₂ swap
197 ///
198 /// where ã = g_{αₙ}*a and b̃ = g_{αₙ}*b are Gaussian-smoothed AO basis
199 /// functions and the sum over quadrature nodes n (with 6D weight
200 /// w_n = c_n^{bsh}·(αₙ/π)^{3/2}) approximates G.
201 ///
202 /// NOTE: diagnose_GKffK and diagnose_GQKffK share the same signature.
203 /// @param GKf_cc G·K̂f₁₂|ij⟩ pieces (caller applies G6d externally)
204 /// @param GfK_cc G·f₁₂K̂|ij⟩ pieces
205 /// @param Kphi_i K̂φᵢ (precomputed once by caller)
206 /// @param Kphi_j K̂φⱼ (precomputed once by caller)
207 /// @param info molecular/orbital info carrying mo_ket, mo_bra, parameters
208 /// @param energy ε_i + ε_j (must be negative)
209 /// @param aobasis observer basis
210 /// @param orthonormalize_basis Löwdin-orthonormalize aobasis (false: keep raw)
212 World& world,
213 const std::vector<CCPairFunction<double,6>>& GKf_cc,
214 const std::vector<CCPairFunction<double,6>>& GfK_cc,
215 const real_function_3d& phi_i,
216 const real_function_3d& phi_j,
217 const real_function_3d& Kphi_i,
218 const real_function_3d& Kphi_j,
219 const Info& info,
220 double energy,
221 const std::vector<real_function_3d>& aobasis,
222 bool orthonormalize_basis = true) const;
223
224 // ---------------------------------------------------------------------
225 // G·Q₁₂·[K̂,f] diagnostic — 3D-only reference, Q₁₂ expanded on the ket
226 // ---------------------------------------------------------------------
227
228 /// Diagnose ⟨ab | G Q₁₂ [K̂,f₁₂] | ij⟩, Q₁₂ = (1-O₁)(1-O₂), O = Σ|k_ket⟩⟨k_bra|.
229 ///
230 /// Caller supplies G·Q₁₂·K̂f₁₂|ij⟩ and G·Q₁₂·f₁₂K̂|ij⟩ (neither G nor Q is
231 /// applied internally). The reference is computed via 3D functions only:
232 /// G is moved to the bra via the Schwinger/BSH fit and Q₁₂ is expanded on
233 /// the ket side as scalar contractions of analytic Kf/fK matrix elements —
234 /// see operator_diagnostics.md for the full derivation. Entries: "GQKf",
235 /// "GQfK", "GQKffK" (= GQKf − GQfK).
236 ///
237 /// The observer basis is an explicit parameter (unlike diagnose_GKffK):
238 /// pass info.ao for the accuracy check, or the raw pair-bra functions
239 /// {mo_bra[i], mo_bra[j]} with orthonormalize_basis=false so that the
240 /// (0,1) element ⟨ī j̄|G Q₁₂ [K̂,f]|ij⟩ is the exchange-commutator
241 /// contribution to the MP2 pair energy.
242 ///
243 /// @param GQKf_cc G Q₁₂ K̂f₁₂|ij⟩ pieces (caller applies Q12 and G externally)
244 /// @param GQfK_cc G Q₁₂ f₁₂K̂|ij⟩ pieces
245 /// @param Kphi_i K̂φᵢ (precomputed once by caller)
246 /// @param Kphi_j K̂φⱼ (precomputed once by caller)
247 /// @param info carries mo_ket, mo_bra (Q₁₂ and K̂ spaces) and parameters
248 /// @param energy ε_i + ε_j (must be negative)
249 /// @param aobasis observer basis
250 /// @param orthonormalize_basis Löwdin-orthonormalize aobasis (false: keep raw)
252 World& world,
253 const std::vector<CCPairFunction<double,6>>& GQKf_cc,
254 const std::vector<CCPairFunction<double,6>>& GQfK_cc,
255 const real_function_3d& phi_i,
256 const real_function_3d& phi_j,
257 const real_function_3d& Kphi_i,
258 const real_function_3d& Kphi_j,
259 const Info& info,
260 double energy,
261 const std::vector<real_function_3d>& aobasis,
262 bool orthonormalize_basis = true) const;
263
264 /// element provider: M(x,y) = Σ_p ⟨p1[x](1) p2[y](2)|K̂_p f₁₂|φᵢφⱼ⟩
265 /// K̂₁: ⟨(K̂†x)·φᵢ | f₁₂⋆(y·φⱼ)⟩ K̂₂: ⟨f₁₂⋆(x·φᵢ) | (K̂†y)·φⱼ⟩
266 /// K̂† (Exchange with bra=mo_ket, ket=mo_bra) and the f₁₂ convolution are
267 /// built once from `info` and captured in the closure.
269 World& world,
270 const real_function_3d& phi_i,
271 const real_function_3d& phi_j,
272 const Info& info,
273 double eps = -1.0);
274
275 /// element provider: M(x,y) = Σ_p ⟨p1[x](1) p2[y](2)|f₁₂ K̂_p|φᵢφⱼ⟩
276 /// K̂₁: ⟨x·(K̂φᵢ) | f₁₂⋆(y·φⱼ)⟩ K̂₂: ⟨x·φᵢ | f₁₂⋆(y·(K̂φⱼ))⟩
277 /// The f₁₂ convolution is built once from `info` and captured in the closure.
279 World& world,
280 const real_function_3d& phi_i,
281 const real_function_3d& phi_j,
282 const real_function_3d& Kphi_i,
283 const real_function_3d& Kphi_j,
284 const Info& info,
285 double eps = -1.0);
286};
287
288} // namespace madness
289
290#endif // MADNESS_CHEM_EXCHANGE_COMMUTATOR_H
structure for a CC Function 3D which holds an index and a type
Definition ccpairfunction.h:45
a 6D function, either in full or low rank form, possibly including an 2-particle function
Definition ccpairfunction.h:373
Definition operator_diagnostics.h:58
std::function< Tensor< T >( const std::vector< Function< T, LDIM > > &, const std::vector< Function< T, LDIM > > &)> ElementProvider
Definition operator_diagnostics.h:67
LowRankFunction represents a hi-dimensional (NDIM) function as a sum of products of low-dimensional (...
Definition lowrankfunction.h:752
A parallel world class.
Definition world.h:134
double(* energy)()
Definition derivatives.cc:58
Runtime details on the configuration and version of MADNESS.
Definition madness.h:69
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
Function< double, 3 > real_function_3d
Definition functypedefs.h:80
SeparatedConvolution< double, 6 > real_convolution_6d
Definition functypedefs.h:139
Definition exchange_commutator.h:30
double t_wall
wall time (s)
Definition exchange_commutator.h:34
std::vector< CCPairFunction< double, 6 > > Kf
K̂ f₁₂ | i j ⟩
Definition exchange_commutator.h:31
long rank
combined LRF rank (0 for 6D)
Definition exchange_commutator.h:36
std::string algo
"6d" / "lrf" / "lrf-direct" / "lrf-split-alpha"
Definition exchange_commutator.h:37
std::vector< CCPairFunction< double, 6 > > KffK
[K̂, f₁₂] | i j ⟩ = Kf − fK (signs baked in)
Definition exchange_commutator.h:33
double mem_gb
combined size of pair factors (GByte)
Definition exchange_commutator.h:35
std::vector< CCPairFunction< double, 6 > > fK
f₁₂ K̂ | i j ⟩
Definition exchange_commutator.h:32
Definition exchange_commutator.h:42
double eps_gfit
Definition exchange_commutator.h:46
double alpha_star
GFit partition threshold; α_μ > α* is discarded.
Definition exchange_commutator.h:43
double hi
Definition exchange_commutator.h:45
double lo
Definition exchange_commutator.h:44
Definition exchange_commutator.h:55
LowRankFunctionParameters lrfparam_medium
Definition exchange_commutator.h:65
double alpha_hi
medium / tight boundary (≡ alpha_star)
Definition exchange_commutator.h:57
double hi
Definition exchange_commutator.h:59
int max_taylor_order
Definition exchange_commutator.h:73
double lo
Definition exchange_commutator.h:58
bool medium_use_taylor
Definition exchange_commutator.h:70
LowRankFunctionParameters lrfparam_diffuse
Definition exchange_commutator.h:64
double eps_gfit
Definition exchange_commutator.h:60
double alpha_lo
diffuse / medium boundary
Definition exchange_commutator.h:56
bool medium_use_6d
Definition exchange_commutator.h:78
static KffKResult apply_KffK_6d(World &world, const CCFunction< double, 3 > &phi_i, const CCFunction< double, 3 > &phi_j, const Info &info, const real_convolution_6d *Gscreen=nullptr)
Definition exchange_commutator.cc:142
DiagnosticMatrix diagnose_GKffK(World &world, const std::vector< CCPairFunction< double, 6 > > &GKf_cc, const std::vector< CCPairFunction< double, 6 > > &GfK_cc, const real_function_3d &phi_i, const real_function_3d &phi_j, const real_function_3d &Kphi_i, const real_function_3d &Kphi_j, const Info &info, double energy, const std::vector< real_function_3d > &aobasis, bool orthonormalize_basis=true) const
Definition exchange_commutator.cc:608
static DiagnosticMatrix ::ElementProvider kf_provider(World &world, const real_function_3d &phi_i, const real_function_3d &phi_j, const Info &info, double eps=-1.0)
Definition exchange_commutator.cc:659
DiagnosticMatrix diagnose_GQKffK(World &world, const std::vector< CCPairFunction< double, 6 > > &GQKf_cc, const std::vector< CCPairFunction< double, 6 > > &GQfK_cc, const real_function_3d &phi_i, const real_function_3d &phi_j, const real_function_3d &Kphi_i, const real_function_3d &Kphi_j, const Info &info, double energy, const std::vector< real_function_3d > &aobasis, bool orthonormalize_basis=true) const
Definition exchange_commutator.cc:728
static LowRankFunction< double, 6 > compute_lrf_exchange_operator(World &world, const Info &info, const SplitAlphaOptions &opt, const LowRankFunctionParameters &lrfparam)
Definition exchange_commutator.cc:196
DiagnosticMatrix diagnose_KffK(World &world, const std::vector< CCPairFunction< double, 6 > > &Kf_cc, const std::vector< CCPairFunction< double, 6 > > &fK_cc, const real_function_3d &phi_i, const real_function_3d &phi_j, const real_function_3d &Kphi_i, const real_function_3d &Kphi_j, const Info &info, double energy, const std::vector< real_function_3d > &aobasis, bool orthonormalize_basis=true) const
Definition exchange_commutator.cc:567
static KffKResult apply_KffK_lowrank_split_alpha(World &world, const CCFunction< double, 3 > &phi_i, const CCFunction< double, 3 > &phi_j, const Info &info, const LowRankFunction< double, 6 > &exchange_op, const LowRankFunctionParameters &lrfparam, const SplitAlphaOptions &opt)
Definition exchange_commutator.cc:258
static DiagnosticMatrix ::ElementProvider fk_provider(World &world, const real_function_3d &phi_i, const real_function_3d &phi_j, const real_function_3d &Kphi_i, const real_function_3d &Kphi_j, const Info &info, double eps=-1.0)
Definition exchange_commutator.cc:691
static KffKResult apply_KffK_lowrank_three_range(World &world, const CCFunction< double, 3 > &phi_i, const CCFunction< double, 3 > &phi_j, const Info &info, const ThreeRangeOptions &opt)
Definition exchange_commutator.cc:354
POD holding some basic functions and some intermediates for the CC2 calculation.
Definition CCStructures.h:919
Definition lowrankfunction.h:22
static AtomicBasisSet aobasis
Definition testperiodicdft.cc:40
vector< FLOAT > opt(const vector< FLOAT > &x, const vector< FLOAT > &f, const vector< FLOAT > &w, const vector< FLOAT > &guess, int maxiter)
Definition y.cc:486