Skip to content

COSX Reformulation for Negative Grid Weights - #2931

Merged
davpoolechem merged 14 commits into
psi4:masterfrom
davpoolechem:dpoole34/cosx-2xmat
Aug 21, 2023
Merged

COSX Reformulation for Negative Grid Weights#2931
davpoolechem merged 14 commits into
psi4:masterfrom
davpoolechem:dpoole34/cosx-2xmat

Conversation

@davpoolechem

@davpoolechem davpoolechem commented Apr 24, 2023

Copy link
Copy Markdown
Contributor

Description

This PR is a follow-up to #2906, and what can be considered an official solution to the issue discussed in #2890. The current issue is that COSX does not work with certain grid configurations - specifically, it does not work with grids that have negative grid weights, due to the use of an intermediate matrix in COSX that uses the square root of negative grid weights. #2906 "fixed" this issue by having COSX throw an exception when grids with negative weights were encountered. This PR provides a COSX reformulation that allows COSX to work with negative grid weights.

The reformulation does two primary things:

  • The $X$ matrix (Eq. 4 in Neese 2009) is redefined as $X_{\kappa g} = \sqrt{|w_{g}|} \kappa(r_{g})$. In words, $X_{\kappa g}$ now uses the square root of the magnitude of the weights, instead of the square root of the raw weights.
  • To correct for the above, the computation of the $G$ matrix (Eq. 7 in Neese 2009) is performed as $G_{\nu g} = \sum_{\tau} \text{sign}(w_{g}) A_{\nu \tau} (r_{g}) F_{\tau g}$ . In words, the sign of the corresponding grid weights are included in the formation of $G_{\nu g}$, when $A_{\nu \tau}$ and $F_{\tau g}$ are contracted.

One other thing comes out as a consequence of this:

  • For overlap fitting, the numerical overlap matrix $S_{N}$ (Eq. 13 in Izsák 2011) is computed as $S_{N} = X * X_{\text{alt}}^{T}$, where $X_{\kappa g} = \sqrt{|w_{g}|} \kappa(r_{g})$ (the same as the first bullet point in the reformulation above), and $X_{\text{alt},\kappa g} = \text{sign}(w_{g}) \sqrt{|w_{g}|} \kappa(r_{g})$. Essentially, $S_{N}$ now uses two variants of the $X$ matrix in its formulation, one of which folds the grid weight sign into itself.

User API & Changelog headlines

  • COSX can now be used with a wider variety of grids, as a bug preventing COSX to be used with specific grid configurations has been fixed.

Dev notes & details

  • The COSX implementation has been reformulated to enable calculations with grids containing negative grid weights.

Questions

  • Is the current way of handling computation of the numerical overlap matrix acceptable? Currently, two X matrices are used to form the numerical overlap matrix. One goal of folding the grid weight sign into the formation of G was to prevent the use of multiple X matrices. However, I don't think that can be done here without removing the call to linalg::doublet in the compute_numeric_overlap function. One could probably do a slightly-modified, manually-implemented matrix multiply with the grid weight folded in, and construct $S_{N}$ with a single $X$ matrix that way. But that comes at the cost of not utilizing BLAS. What does everyone consider preferable?
  • What, exactly, is the best way to test this? I would like to add a test to test_dfjcosk.py for a new grid weight option with negative weights. Unfortunately, it seems that other codes that have COSX just don't have grid options that lead to negative grid weights. For now, I've been comparing against Psi4 COSX calculations using similar grid sizes and ensuring the calculations have similar energies, but I certainly wouldn't consider that robust.

Checklist

Status

  • Ready for review
  • Ready for merge

Comment thread psi4/src/psi4/libfock/DFJCOSK.cc Outdated
Comment thread psi4/src/psi4/libfock/DFJCOSK.cc Outdated
Comment thread psi4/src/psi4/libfock/DFJCOSK.cc Outdated
Comment thread psi4/src/psi4/libfock/DFJCOSK.cc Outdated
Comment thread psi4/src/psi4/libfock/DFJCOSK.cc Outdated
for (int ipoint = 0; ipoint < init_block->npoints(); ++ipoint) {
if (w[ipoint] < 0.0) {
throw PSIEXCEPTION("The definition of the current initial grid includes negative weights. As these are not suitable for the COSX implementation, please choose another initial grid through adjusting either COSX_PRUNING_SCHEME or COSX_SPHERICAL_POINTS_INITIAL.");
outfile->Printf(" WARNING: The definition of the current initial grid includes negative weights, which the original COSX formulation does not support!\n If this is of concern, please choose another initial grid through adjusting either COSX_PRUNING_SCHEME or COSX_SPHERICAL_POINTS_INITIAL.\n\n");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not be a WARNING but INFO (does this exist in Psi4?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to switch to INFO, but Psi4 seems to trend towards using WARNING instead of INFO for printout like this, at least from my observation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added! I'll leave the conversation open, though, in case others want to comment on INFO vs. WARNING.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not be a WARNING but INFO (does this exist in Psi4?)

There's no proper logging from c-side. :-( Only PSIEXCEPTION that stops execution and Printf("WARNING/INFO/ETC that goes to the output file. Admittedly not great. I don't think the situation has been seriously examined for improvement since pybind11 (and having python on the outside of psi4) was young.

in case others want to comment on INFO vs. WARNING.

I agree it's probably INFO at this point (rounding the grid up to down to a pure-positive one would be WARNING), but there's not standardization.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not be a WARNING but INFO (does this exist in Psi4?)

There's no proper logging from c-side. :-( Only PSIEXCEPTION that stops execution and Printf("WARNING/INFO/ETC that goes to the output file. Admittedly not great. I don't think the situation has been seriously examined for improvement since pybind11 (and having python on the outside of psi4) was young.

in case others want to comment on INFO vs. WARNING.

I agree it's probably INFO at this point (rounding the grid up to down to a pure-positive one would be WARNING), but there's not standardization.

All good to know! Maybe such logging is something that could be more seriously looked at in the future. It wouldn't be a bad functionality to have.

Comment thread psi4/src/psi4/libfock/DFJCOSK.cc Outdated

@susilehtola susilehtola left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good, please fix the issues above

@davpoolechem

davpoolechem commented Apr 24, 2023

Copy link
Copy Markdown
Contributor Author

Looks pretty good, please fix the issues above

Thanks for the review! I think everything you suggested should be covered now.

@loriab loriab added the scf Involves general SCF: convergence algorithms, RHF/UHF/ROHF/CUHF... label Apr 24, 2023
@davpoolechem
davpoolechem force-pushed the dpoole34/cosx-2xmat branch from 375ea4d to e7104df Compare May 2, 2023 13:24
Comment thread psi4/src/psi4/libfock/DFJCOSK.cc Outdated
Comment thread psi4/src/psi4/libfock/DFJCOSK.cc Outdated
for (size_t p = 0; p < npoints_block; p++) {
for (size_t k = 0; k < nbf_block; k++) {
X_blockp[p][k] = point_values->get(p, k) * std::sqrt(w[p]);
X_block_nosignp[p][k] = point_values->get(p, k) * std::sqrt(std::fabs(w[p]));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
X_block_nosignp[p][k] = point_values->get(p, k) * std::sqrt(std::fabs(w[p]));
X_block_nosignp[p][k] = point_values->get(p, k) * std::sqrt(std::abs(w[p]));

@susilehtola

Copy link
Copy Markdown
Member

I'm good with the way of computing the overlap matrix. One does want to use BLAS and this just means that you need an extra copy of the basis function value matrix.

@davpoolechem
davpoolechem force-pushed the dpoole34/cosx-2xmat branch from e0b332d to eafaba2 Compare May 4, 2023 13:19
@davpoolechem

Copy link
Copy Markdown
Contributor Author

I'm good with the way of computing the overlap matrix. One does want to use BLAS and this just means that you need an extra copy of the basis function value matrix.

Yeah, I was thinking it would be preferable to use BLAS, as well.

@davpoolechem davpoolechem mentioned this pull request May 13, 2023
24 tasks
@davpoolechem

Copy link
Copy Markdown
Contributor Author

So with #2955 (SplitJK) also being a thing, I think it would be preferable to get that one in first, and then rebase this one to adjust for the changes.

@davpoolechem
davpoolechem force-pushed the dpoole34/cosx-2xmat branch 3 times, most recently from 3c0f816 to 7df2330 Compare June 12, 2023 12:15
@davpoolechem
davpoolechem force-pushed the dpoole34/cosx-2xmat branch 2 times, most recently from eb0635c to 0d2add3 Compare June 26, 2023 13:37
@davpoolechem
davpoolechem force-pushed the dpoole34/cosx-2xmat branch 2 times, most recently from 1b6c258 to 9420930 Compare July 3, 2023 17:18
@davpoolechem
davpoolechem force-pushed the dpoole34/cosx-2xmat branch from 9420930 to 1b5b26c Compare July 10, 2023 15:25
@davpoolechem
davpoolechem force-pushed the dpoole34/cosx-2xmat branch from 1b5b26c to e82f036 Compare July 20, 2023 13:31
@davpoolechem
davpoolechem force-pushed the dpoole34/cosx-2xmat branch 2 times, most recently from ef8609d to 649b7c7 Compare August 10, 2023 18:05
@davpoolechem
davpoolechem added this pull request to the merge queue Aug 21, 2023
Merged via the queue into psi4:master with commit 5b91eab Aug 21, 2023
@loriab loriab added this to the Psi4 1.9 milestone Sep 25, 2023
@davpoolechem davpoolechem mentioned this pull request Oct 23, 2023
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scf Involves general SCF: convergence algorithms, RHF/UHF/ROHF/CUHF...

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants