Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per Particle Logger #4359

Merged
merged 11 commits into from
Jan 10, 2023
4 changes: 3 additions & 1 deletion src/Estimators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ set(QMCEST_SRC
SpinDensityNew.cpp
MomentumDistribution.cpp
OneBodyDensityMatricesInput.cpp
OneBodyDensityMatrices.cpp)
OneBodyDensityMatrices.cpp
PerParticleHamiltonianLoggerInput.cpp
PerParticleHamiltonianLogger.cpp)

####################################
# create libqmcestimators
Expand Down
2 changes: 2 additions & 0 deletions src/Estimators/CSEnergyEstimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct CSEnergyEstimator : public ScalarEstimatorBase

CSEnergyEstimator(CSLocalEnergyInput&& input, const QMCHamiltonian& h);

std::string getName() const override { return "CSEnergyEstimator"; }

inline RealType getUmbrellaWeight(int ipsi)
{
return scalars_saved[ipsi * LE_INDEX + WEIGHT_INDEX].result();
Expand Down
2 changes: 2 additions & 0 deletions src/Estimators/CollectablesEstimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class CollectablesEstimator : public ScalarEstimatorBase
scalars[i](data[i], wgt);
}

std::string getName() const override { return "CollectiblesEstimator"; }

const std::string type_str = "ColletiblesEstimatorNotSupportedInBatchedVersion";
const std::string& getSubTypeStr() const override { return type_str; }
};
Expand Down
1 change: 1 addition & 0 deletions src/Estimators/EstimatorInputDelegates.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
#include "MomentumDistributionInput.h"
#include "OneBodyDensityMatricesInput.h"
#include "SpinDensityInput.h"
#include "PerParticleHamiltonianLoggerInput.h"

#endif
6 changes: 6 additions & 0 deletions src/Estimators/EstimatorManagerCrowd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ void EstimatorManagerCrowd::accumulate(const RefVector<MCPWalker>& walkers,
operator_ests_[i]->accumulate(walkers, psets, wfns, rng);
}

void EstimatorManagerCrowd::registerListeners(const RefVectorWithLeader<QMCHamiltonian>& ham_list)
{
for (auto& estimator : operator_ests_)
if (estimator->isListenerRequired())
estimator->registerListeners(ham_list.getLeader());
}

void EstimatorManagerCrowd::startBlock(int steps)
{
Expand Down
22 changes: 17 additions & 5 deletions src/Estimators/EstimatorManagerCrowd.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ class QMCHamiltonian;
class EstimatorManagerCrowd
{
public:
using MCPWalker = Walker<QMCTraits, PtclOnLatticeTraits>;
using RealType = EstimatorManagerNew::RealType;
using MCPWalker = Walker<QMCTraits, PtclOnLatticeTraits>;
using RealType = EstimatorManagerNew::RealType;

/** EstimatorManagerCrowd are always spawn of an EstimatorManagerNew
*
* This coupling should be removed.
*/
EstimatorManagerCrowd(EstimatorManagerNew& em);

Expand All @@ -66,7 +64,16 @@ class EstimatorManagerCrowd
* \param[in] psets walker particle sets
* \param[in] wfns walker wavefunctions
* \param[inout] rng crowd scope RandomGenerator
*/
*
* walkers is especially questionable since its really just hiding the full sweep hamiltonian values from
* the most recent (maybe) QMCHamiltonian evaluate which are written into it by the QMCHamiltonians
* previous to the accumulate.
* walkers might additionally be useful because they hold another copy of the dynamic (electron) particle sets coords
* that could be inconsistent with psets.
*
* As soon as the legacy Estimators are dropped this API should be reviewed with an eye to disentangling
* ParticleSet, Walker, and QMCHamiltonian.
*/
void accumulate(const RefVector<MCPWalker>& walkers,
const RefVector<ParticleSet>& psets,
const RefVector<TrialWaveFunction>& wfns,
Expand All @@ -79,6 +86,11 @@ class EstimatorManagerCrowd
RealType get_block_num_samples() const { return block_num_samples_; }
RealType get_block_weight() const { return block_weight_; }

/** This registers the crowd lever estimators that require listeners into the QMCHamiltonianMultiWalkerResources
* We really only need a QMCHamiltonian leader but resource acquisition and release works better this way.
*/
void registerListeners(const RefVectorWithLeader<QMCHamiltonian>& ham_list);

private:
///number of samples accumulated in a block
RealType block_num_samples_;
Expand Down
6 changes: 6 additions & 0 deletions src/Estimators/EstimatorManagerInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "MomentumDistributionInput.h"
#include "OneBodyDensityMatricesInput.h"
#include "SpinDensityInput.h"
#include "PerParticleHamiltonianLoggerInput.h"
#include "ModernStringUtils.hpp"

namespace qmcplusplus
Expand Down Expand Up @@ -85,6 +86,8 @@ void EstimatorManagerInput::readXML(xmlNodePtr cur)
appendEstimatorInput<SpinDensityInput>(child);
else if (atype == "momentumdistribution")
appendEstimatorInput<MomentumDistributionInput>(child);
else if (atype == "perparticlehamiltonianlogger")
appendEstimatorInput<PerParticleHamiltonianLoggerInput>(child);
else
throw UniformCommunicateError(error_tag + "unparsable <estimator> node, name: " + aname + " type: " + atype +
" in Estimators input.");
Expand All @@ -107,5 +110,8 @@ void EstimatorManagerInput::readXML(xmlNodePtr cur)
}
}

void EstimatorManagerInput::append(const EstimatorInput& ei) { estimator_inputs_.emplace_back(ei); }
void EstimatorManagerInput::append(const ScalarEstimatorInput& sei) { scalar_estimator_inputs_.emplace_back(sei); }


} // namespace qmcplusplus
16 changes: 13 additions & 3 deletions src/Estimators/EstimatorManagerInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ class EstimatorManagerInputTests;
} // namespace testing

/** These are the estimator input types EstimatorManagerInput delegates to.
* We of course know all the estimator types at compile time and it is useful to have type safety for their usage.
* We know all the estimator types at compile time and it is useful to have type safety for their usage.
* All input clasess must satisfy std::is_trivially_copyable..
*/
class SpinDensityInput;
class MomentumDistributionInput;
class OneBodyDensityMatricesInput;
using EstimatorInput =
std::variant<std::monostate, MomentumDistributionInput, SpinDensityInput, OneBodyDensityMatricesInput>;
class PerParticleHamiltonianLoggerInput;
using EstimatorInput = std::variant<std::monostate,
MomentumDistributionInput,
SpinDensityInput,
OneBodyDensityMatricesInput,
PerParticleHamiltonianLoggerInput>;
using EstimatorInputs = std::vector<EstimatorInput>;

/** The scalar esimtator inputs
Expand Down Expand Up @@ -77,6 +82,11 @@ class EstimatorManagerInput
*/
void readXML(xmlNodePtr cur);

/** typed appending of already parsed inputs.
* only used in testing.
*/
void append(const EstimatorInput& ei);
void append(const ScalarEstimatorInput& sei);
private:
/// this is a vector of variants for typesafe access to the estimator inputs
EstimatorInputs estimator_inputs_;
Expand Down
4 changes: 3 additions & 1 deletion src/Estimators/EstimatorManagerNew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "SpinDensityNew.h"
#include "MomentumDistribution.h"
#include "OneBodyDensityMatrices.h"
#include "PerParticleHamiltonianLogger.h"
#include "QMCHamiltonians/QMCHamiltonian.h"
#include "Message/Communicate.h"
#include "Message/CommOperators.h"
Expand Down Expand Up @@ -98,7 +99,8 @@ EstimatorManagerNew::EstimatorManagerNew(Communicate* c,
createEstimator<MomentumDistributionInput>(est_input, pset.getTotalNum(), pset.getTwist(),
pset.getLattice()) ||
createEstimator<OneBodyDensityMatricesInput>(est_input, pset.getLattice(), pset.getSpeciesSet(),
twf.getSPOMap(), pset)))
twf.getSPOMap(), pset) ||
createEstimator<PerParticleHamiltonianLoggerInput>(est_input, my_comm_->rank())))
throw UniformCommunicateError(std::string(error_tag_) +
"cannot construct an estimator from estimator input object.");

Expand Down
2 changes: 2 additions & 0 deletions src/Estimators/EstimatorManagerNew.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class hdf_archive;
namespace testing
{
class EstimatorManagerNewTest;
class EstimatorManagerNewTestAccess;
} // namespace testing


Expand Down Expand Up @@ -274,6 +275,7 @@ class EstimatorManagerNew

friend class EstimatorManagerCrowd;
friend class qmcplusplus::testing::EstimatorManagerNewTest;
friend class qmcplusplus::testing::EstimatorManagerNewTestAccess;
};
} // namespace qmcplusplus
#endif
10 changes: 8 additions & 2 deletions src/Estimators/LocalEnergyEstimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class LocalEnergyEstimator : public ScalarEstimatorBase
scalars[target](ePtr[source], wwght);
}

/*@{*/
/** legacy accumulation function
*/
inline void accumulate(const MCWalkerConfiguration& W,
WalkerIterator first,
WalkerIterator last,
Expand All @@ -81,11 +82,16 @@ class LocalEnergyEstimator : public ScalarEstimatorBase
for (; first != last; ++first)
accumulate(**first, wgt);
}

std::string getName() const override { return "LocalEnergyEstimator"; }

void add2Record(RecordListType& record) override;
void registerObservables(std::vector<ObservableHelper>& h5dec, hdf_archive& file) override;
LocalEnergyEstimator* clone() override;
/*@}*/

/** Accumulate the hamiltonian operator values for system
* This is the batched version
*/
inline void accumulate(const RefVector<MCPWalker>& walkers) override
{
for (MCPWalker& walker : walkers)
Expand Down
2 changes: 2 additions & 0 deletions src/Estimators/LocalEnergyOnlyEstimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct LocalEnergyOnlyEstimator : public ScalarEstimatorBase
scalars_saved.resize(2);
}

std::string getName() const override { return "LocalEnergyOnlyEstimator"; }

inline void accumulate(const MCWalkerConfiguration& W,
WalkerIterator first,
WalkerIterator last,
Expand Down
4 changes: 4 additions & 0 deletions src/Estimators/MomentumDistributionInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class MomentumDistributionInput
};

MomentumDistributionInput(xmlNodePtr cur);
/** default copy constructor
* This is required due to MDI being part of a variant used as a vector element.
*/
MomentumDistributionInput(const MomentumDistributionInput&) = default;
private:
MomentumDistributionInputSection input_section_;

Expand Down
2 changes: 1 addition & 1 deletion src/Estimators/OneBodyDensityMatricesInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ OneBodyDensityMatricesInput::OneBodyDensityMatricesInput(xmlNodePtr cur)
{
// This results in checkParticularValidity being called on OneBodyDensityMatrixInputSection
input_section_.readXML(cur);
auto setIfInInput = [&](auto& var, const std::string& tag) -> bool { return input_section_.setIfInInput(var, tag); };
auto setIfInInput = LAMBDA_setIfInInput;
setIfInInput(energy_matrix_, "energy_matrix");
setIfInInput(use_drift_, "use_drift");
setIfInInput(normalized_, "normalized");
Expand Down
3 changes: 3 additions & 0 deletions src/Estimators/OneBodyDensityMatricesInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class OneBodyDensityMatricesInput
using Position = QMCTraits::PosType;
using Real = QMCTraits::RealType;

/** default copy constructor
* This is required due to OBDMI being part of a variant used as a vector element.
*/
OneBodyDensityMatricesInput(const OneBodyDensityMatricesInput&) = default;
OneBodyDensityMatricesInput(xmlNodePtr cur);

Expand Down
4 changes: 2 additions & 2 deletions src/Estimators/OperatorEstBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// 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.
// Copyright (c) 2022 QMCPACK developers.
//
// File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
//
// File refactored from: OperatorEstBase.cpp
// File refactored from: OperatorBase.cpp
//////////////////////////////////////////////////////////////////////////////////////

/**@file
Expand Down
11 changes: 11 additions & 0 deletions src/Estimators/OperatorEstBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "OhmmsData/RecordProperty.h"
#include "Utilities/RandomGenerator.h"
#include "QMCHamiltonians/ObservableHelper.h"
#include "QMCHamiltonians/QMCHamiltonian.h"
#include "QMCWaveFunctions/OrbitalSetTraits.h"
#include "type_traits/DataLocality.h"
#include "hdf/hdf_archive.h"
Expand Down Expand Up @@ -118,9 +119,19 @@ class OperatorEstBase

const std::string& get_my_name() const { return my_name_; }

/** Register 0-many listeners with a leading QMCHamiltonian instance i.e. a QMCHamiltonian
* that has acquired the crowd scope QMCHamiltonianMultiWalkerResource.
* This must be called for each crowd scope estimator that listens to register listeners into
* the crowd scope QMCHamiltonianMultiWalkerResource.
*
* Many estimators don't need per particle values so the default implementation is no op.
*/
virtual void registerListeners(QMCHamiltonian& ham_leader){};

bool isListenerRequired() { return requires_listener_; }

DataLocality get_data_locality() const { return data_locality_; }

protected:
/** locality for accumulation of estimator data.
* This designates the memory scheme used for the estimator
Expand Down
Loading