Skip to content

Commit

Permalink
Merge pull request #36 from PhilipFackler/refactor-test_RotatedSPOs
Browse files Browse the repository at this point in the history
Refactor test_RotatedSPOs
  • Loading branch information
williamfgc authored Sep 27, 2023
2 parents 43adc45 + a0deb00 commit 12cfc1f
Show file tree
Hide file tree
Showing 59 changed files with 13,914 additions and 3,774 deletions.
4 changes: 4 additions & 0 deletions src/Particle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
####################################
set(PARTICLE
InitMolecularSystem.cpp
InitMolecularSystemT.cpp
SimulationCell.cpp
SimulationCellT.cpp
ParticleSetPool.cpp
ParticleSetPoolT.cpp
ParticleSet.cpp
ParticleSetT.cpp
PSdispatcher.cpp
Expand All @@ -28,9 +30,11 @@ set(PARTICLE
MCCoords.cpp
MCCoordsT.cpp
MCWalkerConfiguration.cpp
MCWalkerConfigurationT.cpp
WalkerConfigurations.cpp
SpeciesSet.cpp
SampleStack.cpp
SampleStackT.cpp
createDistanceTableAA.cpp
createDistanceTableAB.cpp
createDistanceTableT.cpp
Expand Down
314 changes: 314 additions & 0 deletions src/Particle/InitMolecularSystemT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source
// License. See LICENSE file in top directory for details.
//
// Copyright (c) 2020 QMCPACK developers.
//
// File developed by: Jordan E. Vincent, University of Illinois at
// Urbana-Champaign
// Luke Shulenburger, [email protected], Sandia National
// Laboratories Jeremy McMinnis, [email protected],
// University of Illinois at Urbana-Champaign Jeongnim Kim,
// [email protected], University of Illinois at
// Urbana-Champaign Miguel Morales, [email protected],
// Lawrence Livermore National Laboratory Mark Dewing,
// [email protected], University of Illinois at
// Urbana-Champaign Mark A. Berrill, [email protected], Oak
// Ridge National Laboratory
//
// File created by: Jeongnim Kim, [email protected], University of Illinois
// at Urbana-Champaign
//////////////////////////////////////////////////////////////////////////////////////

#include "InitMolecularSystemT.h"

#include "OhmmsData/AttributeSet.h"
#include "Particle/DistanceTableT.h"
#include "Particle/ParticleSetPoolT.h"
#include "ParticleBase/RandomSeqGeneratorGlobal.h"

namespace qmcplusplus
{
template <typename T>
InitMolecularSystemT<T>::InitMolecularSystemT(

Check warning on line 33 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L33

Added line #L33 was not covered by tests
ParticleSetPoolT<T>& pset, const char* aname) :
OhmmsElementBase(aname),
ptclPool(pset)

Check warning on line 36 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L36

Added line #L36 was not covered by tests
{
}

template <typename T>
bool
InitMolecularSystemT<T>::put(xmlNodePtr cur)

Check warning on line 42 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L42

Added line #L42 was not covered by tests
{
std::string target("e"), source("i"), volume("no");
OhmmsAttributeSet hAttrib;
hAttrib.add(target, "target");
hAttrib.add(source, "source");
hAttrib.add(volume, "use_volume");
hAttrib.put(cur);
ParticleSetT<T>* els = ptclPool.getParticleSet(target);
if (els == 0) {
ERRORMSG("No target particle " << target << " exists.")
return false;
}
ParticleSetT<T>* ions = ptclPool.getParticleSet(source);
if (ions == 0) {
ERRORMSG("No source particle " << source << " exists.")
return false;
}

app_log() << "<init source=\"" << source << "\" target=\"" << target
<< "\">" << std::endl;

if (volume == "yes")
initWithVolume(ions, els);
else
initMolecule(ions, els);

makeUniformRandom(els->spins);
els->spins *= 2 * M_PI;

Check warning on line 70 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L70

Added line #L70 was not covered by tests

app_log() << "</init>" << std::endl;
app_log().flush();

return true;
}

template <typename T>
void
InitMolecularSystemT<T>::initAtom(ParticleSetT<T>* ions, ParticleSetT<T>* els)

Check warning on line 80 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L80

Added line #L80 was not covered by tests
{
// 3N-dimensional Gaussian
typename ParticleSetT<T>::ParticlePos chi(els->getTotalNum());
makeGaussRandom(chi);
RealType q = std::sqrt(static_cast<RealType>(els->getTotalNum())) * 0.5;
int nel(els->getTotalNum()), items(0);

Check warning on line 86 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L83-L86

Added lines #L83 - L86 were not covered by tests
while (nel) {
els->R[items] = ions->R[0] + q * chi[items];
--nel;
++items;

Check warning on line 90 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L88-L90

Added lines #L88 - L90 were not covered by tests
}
}

template <typename TReal>
struct LoneElectronT
{
using RealType = TReal;
int ID;
RealType BondLength;
inline LoneElectronT(int id, RealType bl) : ID(id), BondLength(bl)

Check warning on line 100 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L100

Added line #L100 was not covered by tests
{
}
};

template <typename T>
void
InitMolecularSystemT<T>::initMolecule(

Check warning on line 107 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L107

Added line #L107 was not covered by tests
ParticleSetT<T>* ions, ParticleSetT<T>* els)
{
if (ions->getTotalNum() == 1)
return initAtom(ions, els);

Check warning on line 111 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L111

Added line #L111 was not covered by tests

const int d_ii_ID = ions->addTable(*ions);
ions->update();
const typename ParticleSetT<T>::ParticleIndex& grID(ions->GroupID);
SpeciesSet& Species(ions->getSpeciesSet());
int Centers = ions->getTotalNum();

Check warning on line 117 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L113-L117

Added lines #L113 - L117 were not covered by tests
std::vector<int> Qtot(Centers), Qcore(Centers), Qval(Centers, 0);
// use charge as the core electrons first
int icharge = Species.addAttribute("charge");
// Assign default core charge
for (int iat = 0; iat < Centers; iat++)
Qtot[iat] = static_cast<int>(Species(icharge, grID[iat]));

Check warning on line 123 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L123

Added line #L123 was not covered by tests
// cutoff radius (Bohr) this a random choice
RealType cutoff = 4.0;

Check warning on line 125 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L125

Added line #L125 was not covered by tests
typename ParticleSetT<T>::ParticlePos chi(els->getTotalNum());
// makeGaussRandom(chi);
makeSphereRandom(chi);
// the upper limit of the electron index with spin up
const int numUp = els->last(0);

Check warning on line 130 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L130

Added line #L130 was not covered by tests
// the upper limit of the electron index with spin down. Pay attention to
// the no spin down electron case.
const int numDown = els->last(els->groups() > 1 ? 1 : 0) - els->first(0);
// consumer counter of random numbers chi
int random_number_counter = 0;
int nup_tot = 0, ndown_tot = numUp;
std::vector<LoneElectronT<RealType>> loneQ;
RealType rmin = cutoff;
typename ParticleSetT<T>::SingleParticlePos cm;

Check warning on line 139 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L135-L139

Added lines #L135 - L139 were not covered by tests

const auto& dist = ions->getDistTableAA(d_ii_ID).getDistances();
// Step 1. Distribute even Q[iat] of atomic center iat. If Q[iat] is odd,
// put Q[iat]-1 and save the lone electron.
for (size_t iat = 0; iat < Centers; iat++) {
cm += ions->R[iat];

Check warning on line 145 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L145

Added line #L145 was not covered by tests
for (size_t jat = iat + 1; jat < Centers; ++jat) {
rmin = std::min(rmin, dist[jat][iat]);
}
// use 40% of the minimum bond
RealType sep = rmin * 0.4;

Check warning on line 150 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L150

Added line #L150 was not covered by tests
int v2 = Qtot[iat] / 2;
if (Qtot[iat] > v2 * 2) {
loneQ.push_back(LoneElectronT<RealType>(iat, sep));
}
for (int k = 0; k < v2; k++) {
// initialize electron positions in pairs
if (nup_tot < numUp)
els->R[nup_tot++] =
ions->R[iat] + sep * chi[random_number_counter++];

Check warning on line 159 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L158-L159

Added lines #L158 - L159 were not covered by tests
if (ndown_tot < numDown)
els->R[ndown_tot++] =
ions->R[iat] + sep * chi[random_number_counter++];

Check warning on line 162 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L161-L162

Added lines #L161 - L162 were not covered by tests
}
}

// Step 2. Distribute the electrons left alone
// mmorales: changed order of spin assignment to help with spin
// imbalances in molecules at large distances.
// Not guaranteed to work, but should help in most cases
// as long as atoms in molecules are defined sequencially
typename std::vector<LoneElectronT<RealType>>::iterator it(loneQ.begin());
typename std::vector<LoneElectronT<RealType>>::iterator it_end(loneQ.end());

Check warning on line 172 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L171-L172

Added lines #L171 - L172 were not covered by tests
while (it != it_end && nup_tot != numUp && ndown_tot != numDown) {
if (nup_tot < numUp) {
els->R[nup_tot++] = ions->R[(*it).ID] +
(*it).BondLength * chi[random_number_counter++];
++it;

Check warning on line 177 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L175-L177

Added lines #L175 - L177 were not covered by tests
}
if (ndown_tot < numDown && it != it_end) {
els->R[ndown_tot++] = ions->R[(*it).ID] +
(*it).BondLength * chi[random_number_counter++];

Check warning on line 181 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L180-L181

Added lines #L180 - L181 were not covered by tests
++it;
}
}

// Step 3. Handle more than neutral electrons
// extra electrons around the geometric center
RealType cnorm = 1.0 / static_cast<RealType>(Centers);
RealType sep = rmin * 2;

Check warning on line 189 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L188-L189

Added lines #L188 - L189 were not covered by tests
cm = cnorm * cm;
if (nup_tot < numUp)
while (nup_tot < numUp)
els->R[nup_tot++] = cm + sep * chi[random_number_counter++];

Check warning on line 193 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L193

Added line #L193 was not covered by tests
if (ndown_tot < numDown)
while (ndown_tot < numDown)
els->R[ndown_tot++] = cm + sep * chi[random_number_counter++];

Check warning on line 196 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L196

Added line #L196 was not covered by tests

// safety check. all the random numbers should have been consumed once and
// only once.
if (random_number_counter != chi.size())
throw std::runtime_error("initMolecule unexpected random number "
"consumption. Please report a bug!");

// put all the electrons in a unit box
if (els->getLattice().SuperCellEnum != SUPERCELL_OPEN) {
els->R.setUnit(PosUnit::Cartesian);
els->applyBC(els->R);
els->update(false);
}
}

/// helper function to determine the lower bound of a domain (need to move up)
template <typename T>
inline TinyVector<T, 3>
lower_bound(const TinyVector<T, 3>& a, const TinyVector<T, 3>& b)
{
return TinyVector<T, 3>(
std::min(a[0], b[0]), std::min(a[1], b[1]), std::min(a[2], b[2]));
}

/// helper function to determine the upper bound of a domain (need to move up)
template <typename T>
inline TinyVector<T, 3>
upper_bound(const TinyVector<T, 3>& a, const TinyVector<T, 3>& b)
{
return TinyVector<T, 3>(
std::max(a[0], b[0]), std::max(a[1], b[1]), std::max(a[2], b[2]));
}

template <typename T>
void
InitMolecularSystemT<T>::initWithVolume(

Check warning on line 232 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L232

Added line #L232 was not covered by tests
ParticleSetT<T>* ions, ParticleSetT<T>* els)
{
TinyVector<RealType, OHMMS_DIM> start(1.0);
TinyVector<RealType, OHMMS_DIM> end(0.0);

Check warning on line 236 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L235-L236

Added lines #L235 - L236 were not covered by tests

typename ParticleSetT<T>::ParticlePos Ru(ions->getTotalNum());

Check warning on line 238 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L238

Added line #L238 was not covered by tests
Ru.setUnit(PosUnit::Lattice);
ions->applyBC(ions->R, Ru);

for (int iat = 0; iat < Ru.size(); iat++) {
start = lower_bound(Ru[iat], start);
end = upper_bound(Ru[iat], end);
}

TinyVector<RealType, OHMMS_DIM> shift;
Tensor<RealType, OHMMS_DIM> newbox(ions->getLattice().R);

Check warning on line 248 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L247-L248

Added lines #L247 - L248 were not covered by tests

RealType buffer = 2.0; // buffer 2 bohr

Check warning on line 250 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L250

Added line #L250 was not covered by tests
for (int idim = 0; idim < OHMMS_DIM; ++idim) {
// if(ions->getLattice().BoxBConds[idim])
//{
// start[idim]=0.0;
// end[idim]=1.0;
// shift[idim]=0.0;
// }
// else
{
RealType buffer_r = buffer * ions->getLattice().OneOverLength[idim];
start[idim] = std::max((RealType)0.0, (start[idim] - buffer_r));
end[idim] = std::min((RealType)1.0, (end[idim] + buffer_r));
shift[idim] = start[idim] * ions->getLattice().Length[idim];
if (std::abs(end[idim] = start[idim]) <
buffer) { // handle singular case
start[idim] = std::max(0.0, start[idim] - buffer_r / 2.0);
end[idim] = std::min(1.0, end[idim] + buffer_r / 2.0);
}

newbox(idim, idim) =
(end[idim] - start[idim]) * ions->getLattice().Length[idim];

Check warning on line 271 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L270-L271

Added lines #L270 - L271 were not covered by tests
}
}

typename ParticleSetT<T>::ParticleLayout slattice(ions->getLattice());
slattice.set(newbox);

Check warning on line 276 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L275-L276

Added lines #L275 - L276 were not covered by tests

app_log() << " InitMolecularSystem::initWithVolume " << std::endl;
app_log() << " Effective Lattice shifted by " << shift << std::endl;
app_log() << newbox << std::endl;

Ru.resize(els->getTotalNum());
makeUniformRandom(Ru);
for (int iat = 0; iat < Ru.size(); ++iat)
els->R[iat] = slattice.toCart(Ru[iat]) + shift;
els->R.setUnit(PosUnit::Cartesian);

Check warning on line 286 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L285-L286

Added lines #L285 - L286 were not covered by tests
}

template <typename T>
bool
InitMolecularSystemT<T>::put(std::istream& is)

Check warning on line 291 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L291

Added line #L291 was not covered by tests
{
return true;

Check warning on line 293 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L293

Added line #L293 was not covered by tests
}

template <typename T>
bool
InitMolecularSystemT<T>::get(std::ostream& os) const

Check warning on line 298 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L298

Added line #L298 was not covered by tests
{
return true;

Check warning on line 300 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L300

Added line #L300 was not covered by tests
}

template <typename T>
void
InitMolecularSystemT<T>::reset()

Check warning on line 305 in src/Particle/InitMolecularSystemT.cpp

View check run for this annotation

Codecov / codecov/patch

src/Particle/InitMolecularSystemT.cpp#L305

Added line #L305 was not covered by tests
{
}

template class InitMolecularSystemT<double>;
template class InitMolecularSystemT<float>;
template class InitMolecularSystemT<std::complex<double>>;
template class InitMolecularSystemT<std::complex<float>>;

} // namespace qmcplusplus
Loading

0 comments on commit 12cfc1f

Please sign in to comment.