Skip to content

Commit

Permalink
JK Grad: Patches up UKS K and wK builds, now with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgasmith committed Jun 14, 2018
1 parent d87af4e commit 5c0be33
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3,026 deletions.
39 changes: 24 additions & 15 deletions psi4/src/psi4/scfgrad/jk_grad.cc
Expand Up @@ -696,6 +696,7 @@ void DFJKGrad::build_AB_inv_terms()
size_t unit_name = us.first;
size_t nmo_size = us.second;
size_t nmo_size2 = nmo_size * nmo_size;
// printf("%s | %zu %zu\n", buff_name.c_str(), unit_name, nmo_size);

psio_address next_Aija = PSIO_ZERO;

Expand Down Expand Up @@ -993,9 +994,9 @@ void DFJKGrad::build_AB_x_terms()

// => Temporary Gradient Reduction <= //

//gradients_["Coulomb"]->zero();
//gradients_["Exchange"]->zero();
//gradients_["Exchange,LR"]->zero();
// gradients_["Coulomb"]->zero();
// gradients_["Exchange"]->zero();
// gradients_["Exchange,LR"]->zero();

if (do_J_) {
for (int t = 0; t < df_ints_num_threads_; t++) {
Expand All @@ -1014,8 +1015,8 @@ void DFJKGrad::build_AB_x_terms()
}

//gradients_["Coulomb"]->print();
//gradients_["Exchange"]->print();
//gradients_["Exchange,LR"]->print();
// gradients_["Exchange"]->print();
// gradients_["Exchange,LR"]->print();
}
void DFJKGrad::build_Amn_x_terms()
{
Expand Down Expand Up @@ -1151,13 +1152,13 @@ void DFJKGrad::build_Amn_x_terms()
std::vector<std::tuple<size_t, std::string, double**, size_t, psio_address, double**>> transforms;
if (do_K_ || do_wK_) {
transforms.push_back(std::make_tuple(unit_a_, "(A|ij)", Cap, na, next_Aija, Kmnp));
if (!restricted){
if (!restricted) {
transforms.push_back(std::make_tuple(unit_b_, "(A|ij)", Cbp, nb, next_Aijb, Kmnp));
}
}
if (do_wK_) {
transforms.push_back(std::make_tuple(unit_a_, "(A|w|ij)", Cap, na, next_Awija, wKmnp));
if (!restricted){
if (!restricted) {
transforms.push_back(std::make_tuple(unit_b_, "(A|w|ij)", Cbp, nb, next_Awijb, wKmnp));
}
}
Expand All @@ -1178,6 +1179,8 @@ void DFJKGrad::build_Amn_x_terms()

// => J_mn^A <= //

if (do_K_ || do_wK_) Kmn->zero();
if (do_wK_) wKmn->zero();
for (const auto& trans : transforms) {

// > Unpack transform < //
Expand All @@ -1187,6 +1190,7 @@ void DFJKGrad::build_Amn_x_terms()
size_t nmo = std::get<3>(trans);
psio_address address = std::get<4>(trans);
double** retp = std::get<5>(trans);
// printf("%4.2lf : %zu %s %zu\n", factor, unit, buffer.c_str(), nmo);

size_t nmo2 = nmo * nmo;

Expand All @@ -1196,13 +1200,15 @@ void DFJKGrad::build_Amn_x_terms()
// > (A|ij) C_mi -> (A|mj) < //
#pragma omp parallel for
for (int P = 0; P < np; P++) {
C_DGEMM('N', 'N', nso, nmo, nmo, 1.0, Cp[0], nmo, &Aijp[0][P * nmo2], nmo, 0.0, Amip[P], nmo);
C_DGEMM('N', 'N', nso, nmo, nmo, 1.0, Cp[0], nmo, &Aijp[0][P * nmo2], nmo, 0.0, Amip[P], na);
}

// > (A|mj) C_nj -> (A|mn) < //
C_DGEMM('N', 'T', np * (size_t)nso, nso, nmo, factor, Amip[0], nmo, Cp[0], nmo, 0.0, retp[0], nso);
C_DGEMM('N', 'T', np * (size_t)nso, nso, nmo, factor, Amip[0], na, Cp[0], nmo, 1.0, retp[0], nso);
}



// > Integrals < //
int nthread_df = df_ints_num_threads_;
#pragma omp parallel for schedule(dynamic) num_threads(nthread_df)
Expand Down Expand Up @@ -1374,9 +1380,9 @@ void DFJKGrad::build_Amn_x_terms()

// => Temporary Gradient Reduction <= //

//gradients_["Coulomb"]->zero();
//gradients_["Exchange"]->zero();
//gradients_["Exchange,LR"]->zero();
// gradients_["Coulomb"]->zero();
// gradients_["Exchange"]->zero();
// gradients_["Exchange,LR"]->zero();

if (do_J_) {
for (int t = 0; t < df_ints_num_threads_; t++) {
Expand All @@ -1394,9 +1400,9 @@ void DFJKGrad::build_Amn_x_terms()
}
}

//gradients_["Coulomb"]->print();
//gradients_["Exchange"]->print();
//gradients_["Exchange,LR"]->print();
// gradients_["Coulomb"]->print();
// gradients_["Exchange"]->print();
// gradients_["Exchange,LR"]->print();
}

void DFJKGrad::compute_hessian()
Expand Down Expand Up @@ -2205,9 +2211,11 @@ void DirectJKGrad::compute_gradient()
std::map<std::string, std::shared_ptr<Matrix> > vals = compute1(ints);
if (do_J_) {
gradients_["Coulomb"]->copy(vals["J"]);
gradients_["Coulomb"]->print();
}
if (do_K_) {
gradients_["Exchange"]->copy(vals["K"]);
gradients_["Exchange"]->print();
}
}
if (do_wK_) {
Expand All @@ -2217,6 +2225,7 @@ void DirectJKGrad::compute_gradient()
}
std::map<std::string, std::shared_ptr<Matrix> > vals = compute1(ints);
gradients_["Exchange,LR"]->copy(vals["K"]);
gradients_["Exchange,LR"]->print();
}
}
std::map<std::string, std::shared_ptr<Matrix> > DirectJKGrad::compute1(std::vector<std::shared_ptr<TwoBodyAOInt> >& ints)
Expand Down
23 changes: 23 additions & 0 deletions tests/dft-grad-lr2/input.dat
Expand Up @@ -6,6 +6,12 @@ func_reference = psi4.Matrix.from_list([ #TEST
cam_b3lyp_reference = psi4.Matrix.from_list([ #TEST
[ 0.000000000000, -0.000000000000, -0.109138077779], #TEST
[ 0.000000000000, -0.000000000000, 0.109137316634]]) #TEST
func_uks_reference = psi4.Matrix.from_list([ #TEST
[ 0.000000000000, -0.000000000000, 0.001777777286], #TEST
[ -0.000000000000, 0.000000000000, -0.001777777287]]) #TEST
cam_b3lyp_uks_reference = psi4.Matrix.from_list([ #TEST
[ 0.000000000000, 0.000000000000, -0.034683256669], #TEST
[ 0.000000000000, 0.000000000000, 0.034649906885]]) #TEST

molecule {
H 0.000 0.000 0.000
Expand All @@ -30,6 +36,7 @@ func = {
"beta": 0.9,
"omega": 0.2
},
"c_functionals": {}
}

anl_grad = gradient('scf', dft_functional=func, dertype=1)
Expand All @@ -42,3 +49,19 @@ fd_grad = gradient('scf', dft_functional="CAM-B3LYP", dertype=0)
compare_matrices(cam_b3lyp_reference, anl_grad, 6, "Analytic vs Reference CAM Gradients") #TEST
compare_matrices(anl_grad, fd_grad, 5, "Analytic vs FD CAM Gradients") #TEST

molecule {
1 2
H 0.000 0.000 0.000
F 0.000 0.000 1.100
}

set reference uks
fd_grad = gradient('scf', dft_functional=func, dertype=0)
anl_grad = gradient('scf', dft_functional=func, dertype=1)
compare_matrices(func_uks_reference, anl_grad, 6, "Analytic vs Reference CAM-like UKS Gradients") #TEST
compare_matrices(anl_grad, fd_grad, 6, "Analytic vs FD CAM UKS Gradients") #TEST

anl_grad = gradient('scf', dft_functional="CAM-B3LYP", dertype=1)
fd_grad = gradient('scf', dft_functional="CAM-B3LYP", dertype=0)
compare_matrices(cam_b3lyp_uks_reference, anl_grad, 6, "Analytic vs Reference CAM Gradients") #TEST
compare_matrices(anl_grad, fd_grad, 4, "Analytic vs FD CAM Gradients") #TEST

0 comments on commit 5c0be33

Please sign in to comment.