Skip to content

Commit

Permalink
Merge pull request #461 from gitpeterwind/master
Browse files Browse the repository at this point in the history
bug in disjoin and adjoin when spin is not paired
  • Loading branch information
stigrj authored Aug 25, 2023
2 parents 95aad36 + e717462 commit d8f2b39
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 340 deletions.
4 changes: 2 additions & 2 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,16 @@ bool driver::scf::guess_energy(const json &json_guess, Molecule &mol, FockBuilde
auto &Phi = mol.getOrbitals();
auto &nucs = mol.getNuclei();
auto &F_mat = mol.getFockMatrix();
Phi.distribute();

F_mat = ComplexMatrix::Zero(Phi.size(), Phi.size());
if (localize) orbital::localize(prec, Phi, F_mat);
else orbital::diagonalize(prec, Phi, F_mat);

F.setup(prec);
F_mat = F(Phi, Phi);
mol.getSCFEnergy() = F.trace(Phi, nucs);
F.clear();

if (not localize) orbital::diagonalize(prec, Phi, F_mat);
if (plevel == 1) mrcpp::print::footer(1, t_scf, 2);

Timer t_eps;
Expand Down
5 changes: 3 additions & 2 deletions src/initial_guess/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ bool initial_guess::core::setup(OrbitalVector &Phi, double prec, const Nuclei &n
initial_guess::core::rotate_orbitals(Phi, prec, U, Psi);
initial_guess::core::rotate_orbitals(Phi_a, prec, U, Psi);
initial_guess::core::rotate_orbitals(Phi_b, prec, U, Psi);
for (auto &phi_a : Phi_a) Phi.push_back(phi_a);
for (auto &phi_b : Phi_b) Phi.push_back(phi_b);
Phi = orbital::adjoin(Phi, Phi_a);
Phi = orbital::adjoin(Phi, Phi_b);

V.clear();
p.clear();

Expand Down
4 changes: 2 additions & 2 deletions src/initial_guess/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ bool initial_guess::cube::setup(OrbitalVector &Phi, double prec, const std::stri
success &= initial_guess::cube::project_mo(Phi_b, prec, file_b);

// Collect orbitals into one vector
for (auto &phi_a : Phi_a) Phi.push_back(phi_a);
for (auto &phi_b : Phi_b) Phi.push_back(phi_b);
Phi = orbital::adjoin(Phi, Phi_a);
Phi = orbital::adjoin(Phi, Phi_b);

return success;
}
Expand Down
6 changes: 3 additions & 3 deletions src/initial_guess/gto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/

#include <MRCPP/Gaussians>
#include <MRCPP/Parallel>
#include <MRCPP/Printer>
#include <MRCPP/Timer>
#include <MRCPP/Parallel>

#include "gto.h"

Expand Down Expand Up @@ -94,8 +94,8 @@ bool initial_guess::gto::setup(OrbitalVector &Phi, double prec, double screen, c
initial_guess::gto::project_mo(Phi_b, prec, bas_file, mob_file, screen);

// Collect orbitals into one vector
for (auto &phi_a : Phi_a) Phi.push_back(phi_a);
for (auto &phi_b : Phi_b) Phi.push_back(phi_b);
Phi = orbital::adjoin(Phi, Phi_a);
Phi = orbital::adjoin(Phi, Phi_b);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/initial_guess/mw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ bool initial_guess::mw::setup(OrbitalVector &Phi, double prec, const std::string
success &= initial_guess::mw::project_mo(Phi_b, prec, file_b);

// Collect orbitals into one vector
for (auto &phi_a : Phi_a) Phi.push_back(phi_a);
for (auto &phi_b : Phi_b) Phi.push_back(phi_b);
Phi = orbital::adjoin(Phi, Phi_a);
Phi = orbital::adjoin(Phi, Phi_b);

return success;
}
Expand Down
10 changes: 4 additions & 6 deletions src/initial_guess/sad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ bool initial_guess::sad::setup(OrbitalVector &Phi, double prec, double screen, c
print_utils::text(0, "Zeta quality", std::to_string(zeta));
mrcpp::print::separator(0, '~', 2);


// Make Fock operator contributions
Timer t_tot, t_lap;
auto P_p = std::make_shared<mrcpp::PoissonOperator>(*MRA, prec);
Expand Down Expand Up @@ -132,8 +131,8 @@ bool initial_guess::sad::setup(OrbitalVector &Phi, double prec, double screen, c
initial_guess::core::rotate_orbitals(Phi, prec, U, Psi);
initial_guess::core::rotate_orbitals(Phi_a, prec, U, Psi);
initial_guess::core::rotate_orbitals(Phi_b, prec, U, Psi);
for (auto &phi_a : Phi_a) Phi.push_back(phi_a);
for (auto &phi_b : Phi_b) Phi.push_back(phi_b);
Phi = orbital::adjoin(Phi, Phi_a);
Phi = orbital::adjoin(Phi, Phi_b);
p.clear();
V.clear();

Expand All @@ -156,7 +155,6 @@ bool initial_guess::sad::setup(OrbitalVector &Phi, double prec, double screen, c
print_utils::text(0, "AO basis ", "3-21G");
mrcpp::print::separator(0, '~', 2);


// Make Fock operator contributions
Timer t_tot, t_lap;
auto P_p = std::make_shared<mrcpp::PoissonOperator>(*MRA, prec);
Expand Down Expand Up @@ -210,8 +208,8 @@ bool initial_guess::sad::setup(OrbitalVector &Phi, double prec, double screen, c
initial_guess::core::rotate_orbitals(Phi, prec, U, Psi);
initial_guess::core::rotate_orbitals(Phi_a, prec, U, Psi);
initial_guess::core::rotate_orbitals(Phi_b, prec, U, Psi);
for (auto &phi_a : Phi_a) Phi.push_back(phi_a);
for (auto &phi_b : Phi_b) Phi.push_back(phi_b);
Phi = orbital::adjoin(Phi, Phi_a);
Phi = orbital::adjoin(Phi, Phi_b);
p.clear();
V.clear();

Expand Down
30 changes: 28 additions & 2 deletions src/qmfunctions/orbital_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,24 @@ OrbitalVector orbital::param_copy(const OrbitalVector &Phi) {
*/
OrbitalVector orbital::adjoin(OrbitalVector &Phi_a, OrbitalVector &Phi_b) {
OrbitalVector out;
for (auto &phi : Phi_a) out.push_back(phi);
for (auto &phi : Phi_b) out.push_back(phi);
for (auto &phi : Phi_a) {
if (phi.getRank() % mrcpp::mpi::wrk_size != out.size() % mrcpp::mpi::wrk_size) {
// need to send orbital from owner to new owner
if (mrcpp::mpi::my_orb(phi)) { mrcpp::mpi::send_function(phi, out.size() % mrcpp::mpi::wrk_size, phi.getRank(), mrcpp::mpi::comm_wrk); }
if (mrcpp::mpi::my_orb(out.size())) { mrcpp::mpi::recv_function(phi, phi.getRank() % mrcpp::mpi::wrk_size, phi.getRank(), mrcpp::mpi::comm_wrk); }
}
phi.setRank(out.size());
out.push_back(phi);
}
for (auto &phi : Phi_b) {
if (phi.getRank() % mrcpp::mpi::wrk_size != out.size() % mrcpp::mpi::wrk_size) {
// need to send orbital from owner to new owner
if (mrcpp::mpi::my_orb(phi)) { mrcpp::mpi::send_function(phi, out.size() % mrcpp::mpi::wrk_size, phi.getRank(), mrcpp::mpi::comm_wrk); }
if (mrcpp::mpi::my_orb(out.size())) { mrcpp::mpi::recv_function(phi, phi.getRank() % mrcpp::mpi::wrk_size, phi.getRank(), mrcpp::mpi::comm_wrk); }
}
phi.setRank(out.size());
out.push_back(phi);
}
Phi_a.clear();
Phi_b.clear();
return out;
Expand All @@ -350,9 +366,19 @@ OrbitalVector orbital::disjoin(OrbitalVector &Phi, int spin) {
OrbitalVector tmp;
for (auto &i : Phi) {
if (i.spin() == spin) {
if (i.getRank() % mrcpp::mpi::wrk_size != out.size() % mrcpp::mpi::wrk_size) {
// need to send orbital from owner to new owner
if (mrcpp::mpi::my_orb(i)) { mrcpp::mpi::send_function(i, out.size() % mrcpp::mpi::wrk_size, i.getRank(), mrcpp::mpi::comm_wrk); }
if (mrcpp::mpi::my_orb(out.size())) { mrcpp::mpi::recv_function(i, i.getRank() % mrcpp::mpi::wrk_size, i.getRank(), mrcpp::mpi::comm_wrk); }
}
i.setRank(out.size());
out.push_back(i);
} else {
if (i.getRank() % mrcpp::mpi::wrk_size != tmp.size() % mrcpp::mpi::wrk_size) {
// need to send orbital from owner to new owner
if (mrcpp::mpi::my_orb(i)) { mrcpp::mpi::send_function(i, tmp.size() % mrcpp::mpi::wrk_size, i.getRank(), mrcpp::mpi::comm_wrk); }
if (mrcpp::mpi::my_orb(tmp.size())) { mrcpp::mpi::recv_function(i, i.getRank() % mrcpp::mpi::wrk_size, i.getRank(), mrcpp::mpi::comm_wrk); }
}
i.setRank(tmp.size());
tmp.push_back(i);
}
Expand Down
90 changes: 45 additions & 45 deletions tests/h2_scf_cube/reference/h2.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,26 @@
"center_of_mass": [
0.0,
0.0,
-1.0790011801500128e-17
0.0
],
"charge": 0,
"dipole_moment": {
"dip-1": {
"magnitude": 0.00012113100321661997,
"magnitude": 0.00012102753578936309,
"r_O": [
0.0,
0.0,
0.0
],
"vector": [
8.562070411278825e-05,
8.562070411586567e-05,
-3.303028034780805e-06
8.55475075913613e-05,
8.554750760505124e-05,
-3.303378076340241e-06
],
"vector_el": [
8.562070345640525e-05,
8.562070345336806e-05,
-3.3030280327824035e-06
8.554750693863842e-05,
8.55475069398344e-05,
-3.303378077894553e-06
],
"vector_nuc": [
0.0,
Expand Down Expand Up @@ -194,27 +194,27 @@
"multiplicity": 1,
"orbital_energies": {
"energy": [
-0.6041912188474012
-0.6041439916486521
],
"occupation": [
2.0
],
"spin": [
"p"
],
"sum_occupied": -1.2083824376948025
"sum_occupied": -1.2082879832973041
},
"scf_energy": {
"E_ee": 1.3364093910495531,
"E_ee": 1.336599781488902,
"E_eext": 0.0,
"E_el": -1.8765896196296343,
"E_en": -3.69917850248415,
"E_kin": 1.1543817009196822,
"E_el": -1.8765903594447302,
"E_en": -3.6996424251152353,
"E_kin": 1.1547496895230769,
"E_next": 0.0,
"E_nn": 0.7431319742935788,
"E_nuc": 0.7431319742935788,
"E_tot": -1.1334576453360556,
"E_x": -0.6682022091147195,
"E_tot": -1.1334583851511515,
"E_x": -0.6682974053414734,
"E_xc": 0.0,
"Er_el": 0.0,
"Er_nuc": 0.0,
Expand All @@ -224,24 +224,24 @@
"provenance": {
"creator": "MRChem",
"mpi_processes": 1,
"nthreads": 16,
"nthreads": 8,
"routine": "mrchem.x",
"total_cores": 16,
"total_cores": 8,
"version": "1.2.0-alpha"
},
"rsp_calculations": null,
"scf_calculation": {
"initial_energy": {
"E_ee": 1.358939683386662,
"E_ee": 1.3538226445895374,
"E_eext": 0.0,
"E_el": -1.829749569512661,
"E_en": -3.744860398103421,
"E_kin": 1.235638204215726,
"E_el": -1.8275794505669825,
"E_en": -3.737803179434459,
"E_kin": 1.2333096343690937,
"E_next": 0.0,
"E_nn": 0.7431319742935788,
"E_nuc": 0.7431319742935788,
"E_tot": -1.0866175952190824,
"E_x": -0.679467059011628,
"E_tot": -1.0844474762734038,
"E_x": -0.6769085500911546,
"E_xc": 0.0,
"Er_el": 0.0,
"Er_nuc": 0.0,
Expand All @@ -252,50 +252,50 @@
"cycles": [
{
"energy_terms": {
"E_ee": 1.3328267231722024,
"E_ee": 1.3334650996810284,
"E_eext": 0.0,
"E_el": -1.8760020038041993,
"E_en": -3.6894429729944385,
"E_kin": 1.1470250154088903,
"E_el": -1.8760091767528069,
"E_en": -3.6909547785327312,
"E_kin": 1.1482104628294079,
"E_next": 0.0,
"E_nn": 0.7431319742935788,
"E_nuc": 0.7431319742935788,
"E_tot": -1.1328700295106207,
"E_x": -0.6664107693908536,
"E_tot": -1.1328772024592282,
"E_x": -0.6667299607305119,
"E_xc": 0.0,
"Er_el": 0.0,
"Er_nuc": 0.0,
"Er_tot": 0.0
},
"energy_total": -1.1328700295106207,
"energy_update": 0.04625243429153825,
"mo_residual": 0.048665495920872257,
"wall_time": 1.495084261
"energy_total": -1.1328772024592282,
"energy_update": 0.04842972618582442,
"mo_residual": 0.048367387481250995,
"wall_time": 0.705733583
},
{
"energy_terms": {
"E_ee": 1.3364093910495531,
"E_ee": 1.336599781488902,
"E_eext": 0.0,
"E_el": -1.8765896196296343,
"E_en": -3.69917850248415,
"E_kin": 1.1543817009196822,
"E_el": -1.8765903594447302,
"E_en": -3.6996424251152353,
"E_kin": 1.1547496895230769,
"E_next": 0.0,
"E_nn": 0.7431319742935788,
"E_nuc": 0.7431319742935788,
"E_tot": -1.1334576453360556,
"E_x": -0.6682022091147195,
"E_tot": -1.1334583851511515,
"E_x": -0.6682974053414734,
"E_xc": 0.0,
"Er_el": 0.0,
"Er_nuc": 0.0,
"Er_tot": 0.0
},
"energy_total": -1.1334576453360556,
"energy_update": 0.0005876158254349839,
"mo_residual": 0.009310882808245198,
"wall_time": 1.138047308
"energy_total": -1.1334583851511515,
"energy_update": 0.0005811826919233098,
"mo_residual": 0.00932449268671754,
"wall_time": 0.74150425
}
],
"wall_time": 2.633229463
"wall_time": 1.447400583
},
"success": true
},
Expand Down
Loading

0 comments on commit d8f2b39

Please sign in to comment.