Skip to content

Commit

Permalink
More const in MomentumDistribution member.
Browse files Browse the repository at this point in the history
  • Loading branch information
ye-luo committed Sep 23, 2021
1 parent 267aac9 commit a3ce3f5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 43 deletions.
59 changes: 32 additions & 27 deletions src/Estimators/MomentumDistribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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) 2021 QMCPACK developers.
//
// File developed by: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
//
Expand All @@ -19,17 +19,22 @@

namespace qmcplusplus
{
MomentumDistribution::MomentumDistribution(MomentumDistributionInput&& mdi, size_t np, const PosType& twist_in, const LatticeType& lattice, DataLocality dl)
: OperatorEstBase(dl), input_(std::move(mdi)), twist(twist_in), Lattice(lattice)
MomentumDistribution::MomentumDistribution(MomentumDistributionInput&& mdi,
size_t np,
const PosType& twist_in,
const LatticeType& lattice,
DataLocality dl)
: OperatorEstBase(dl),
input_(std::move(mdi)),
twist(twist_in),
Lattice(lattice),
M(input_.get<int>("samples")),
norm_nofK(1.0 / RealType(M))
{
data_locality_ = dl;
psi_ratios.resize(np);

myName = input_.get<std::string>("name");
M = input_.get<int>("samples");

norm_nofK = 1.0 / RealType(M);

//maximum k-value in the k-grid in cartesian coordinates
auto kmax = input_.get<RealType>("kmax");
//maximum k-values in the k-grid along the reciprocal cell axis
Expand All @@ -45,7 +50,7 @@ MomentumDistribution::MomentumDistribution(MomentumDistributionInput&& mdi, size
//length of reciprocal lattice vector
for (int i = 0; i < OHMMS_DIM; i++)
vec_length[i] = 2.0 * M_PI * std::sqrt(dot(Lattice.Gv[i], Lattice.Gv[i]));
PosType kmaxs = {kmax0,kmax1,kmax2};
PosType kmaxs = {kmax0, kmax1, kmax2};
RealType sum_kmaxs = kmaxs[0] + kmaxs[1] + kmaxs[2];
RealType sphere_kmax;
bool sphere = kmax > 0.0 ? true : false;
Expand Down Expand Up @@ -106,7 +111,7 @@ MomentumDistribution::MomentumDistribution(MomentumDistributionInput&& mdi, size
}
}
}
app_log() <<"\n MomentumDistribution named "<<myName<<"\n";
app_log() << "\n MomentumDistribution named " << myName << "\n";
if (sphere && !directional)
{
app_log() << " Using all k-space points with (kx^2+ky^2+kz^2)^0.5 < " << sphere_kmax
Expand Down Expand Up @@ -167,9 +172,8 @@ MomentumDistribution::MomentumDistribution(MomentumDistributionInput&& mdi, size
psi_ratios_all.resize(M, psi_ratios.size());

// allocate data storage
size_t data_size = nofK.size();
data_ = createLocalData(data_size, data_locality_);

size_t data_size = nofK.size();
data_ = createLocalData(data_size, data_locality_);
}


Expand All @@ -178,21 +182,21 @@ MomentumDistribution* MomentumDistribution::clone()
auto* md = new MomentumDistribution(*this);
if (md->data_locality_ == DataLocality::crowd)
{
app_log()<<"MD::clone dl crowd\n";
app_log() << "MD::clone dl crowd\n";
size_t data_size = data_->size();
md->data_ = createLocalData(data_size, data_locality_);
}
else if (md->data_locality_ == DataLocality::rank)
{
app_log()<<"MD::clone dl rank\n";
app_log() << "MD::clone dl rank\n";
assert(data_locality_ == DataLocality::rank);
size_t data_size = 10; // jtk fix
md->data_locality_ = DataLocality::queue;
md->data_ = createLocalData(data_size, data_locality_);
size_t data_size = 10; // jtk fix
md->data_locality_ = DataLocality::queue;
md->data_ = createLocalData(data_size, data_locality_);
}
else
app_log()<<"MD::clone dl other\n";
app_log() << "MD::clone dl other\n";

return md;
}

Expand Down Expand Up @@ -229,13 +233,15 @@ void MomentumDistribution::startBlock(int steps)
//}
//else
// app_log()<<"MD::startBlock dl other\n";

}

/** Gets called every step and writes to thread local data.
*
*/
void MomentumDistribution::accumulate(const RefVector<MCPWalker>& walkers, const RefVector<ParticleSet>& psets, const RefVector<TrialWaveFunction>& wfns, RandomGenerator_t& rng)
void MomentumDistribution::accumulate(const RefVector<MCPWalker>& walkers,
const RefVector<ParticleSet>& psets,
const RefVector<TrialWaveFunction>& wfns,
RandomGenerator_t& rng)
{
for (int iw = 0; iw < walkers.size(); ++iw)
{
Expand All @@ -247,7 +253,7 @@ void MomentumDistribution::accumulate(const RefVector<MCPWalker>& walkers, const
const int np = pset.getTotalNum();
const int nk = kPoints.size();

// accumulate weight
// accumulate weight
// (required by all estimators, otherwise inf results)
walkers_weight_ += weight;

Expand All @@ -263,7 +269,7 @@ void MomentumDistribution::accumulate(const RefVector<MCPWalker>& walkers, const
psi.evaluateRatiosAlltoOne(pset, psi_ratios);
for (int i = 0; i < np; ++i)
psi_ratios_all[s][i] = psi_ratios[i];

for (int ik = 0; ik < nk; ++ik)
kdotp[ik] = -dot(kPoints[ik], vPos[s]);
eval_e2iphi(nk, kdotp.data(), phases_vPos[s].data(0), phases_vPos[s].data(1));
Expand All @@ -289,16 +295,15 @@ void MomentumDistribution::accumulate(const RefVector<MCPWalker>& walkers, const
#pragma omp simd aligned(nofK_here, phases_c, phases_s, phases_vPos_c, phases_vPos_s : QMC_SIMD_ALIGNMENT)
for (int ik = 0; ik < nk; ++ik)
nofK_here[ik] += (phases_c[ik] * phases_vPos_c[ik] - phases_s[ik] * phases_vPos_s[ik]) * ratio_c -
(phases_s[ik] * phases_vPos_c[ik] + phases_c[ik] * phases_vPos_s[ik]) * ratio_s;
(phases_s[ik] * phases_vPos_c[ik] + phases_c[ik] * phases_vPos_s[ik]) * ratio_s;
}
}

// accumulate data
for (int ik = 0; ik < nofK.size(); ++ik)
(*data_)[ik] += weight * nofK[ik] * norm_nofK;

}
};
}


void MomentumDistribution::collect(const RefVector<OperatorEstBase>& type_erased_operator_estimators)
Expand Down Expand Up @@ -326,7 +331,7 @@ void MomentumDistribution::registerOperatorEstimator(hid_t gid)
h5o->set_dimensions(ng, 0); // JTK: doesn't seem right
h5o->open(gid);
h5o->addProperty(const_cast<std::vector<PosType>&>(kPoints), "kpoints");
h5o->addProperty(const_cast<std::vector<int>&>(kWeights), "kweights");
h5o->addProperty(const_cast<std::vector<int>&>(kWeights), "kweights");
}


Expand Down
20 changes: 13 additions & 7 deletions src/Estimators/MomentumDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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) 2021 QMCPACK developers.
//
// File developed by: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
//
Expand Down Expand Up @@ -36,15 +36,15 @@ class MomentumDistribution : public OperatorEstBase

//data members set only during construction
///input values
MomentumDistributionInput input_;
const MomentumDistributionInput input_;
///twist angle
const PosType twist;
///lattice vector
const LatticeType Lattice;
///number of samples
int M;
const int M;
///normalization factor for n(k)
RealType norm_nofK;
const RealType norm_nofK;
///list of k-points in Cartesian Coordinates
std::vector<PosType> kPoints;
///weight of k-points (make use of symmetry)
Expand All @@ -69,7 +69,11 @@ class MomentumDistribution : public OperatorEstBase

/** Constructor for MomentumDistributionInput
*/
MomentumDistribution(MomentumDistributionInput&& mdi, size_t np, const PosType& twist, const LatticeType& lattice, DataLocality dl = DataLocality::crowd);
MomentumDistribution(MomentumDistributionInput&& mdi,
size_t np,
const PosType& twist,
const LatticeType& lattice,
DataLocality dl = DataLocality::crowd);

//MomentumDistribution(const MomentumDistribution& md);

Expand All @@ -83,7 +87,10 @@ class MomentumDistribution : public OperatorEstBase

/** accumulate 1 or more walkers of MomentumDistribution samples
*/
void accumulate(const RefVector<MCPWalker>& walkers, const RefVector<ParticleSet>& psets, const RefVector<TrialWaveFunction>& wfns, RandomGenerator_t& rng) override;
void accumulate(const RefVector<MCPWalker>& walkers,
const RefVector<ParticleSet>& psets,
const RefVector<TrialWaveFunction>& wfns,
RandomGenerator_t& rng) override;

/** this allows the EstimatorManagerNew to reduce without needing to know the details
* of MomentumDistribution's data.
Expand All @@ -107,7 +114,6 @@ class MomentumDistribution : public OperatorEstBase
* big state big coupling design.
*/
void registerOperatorEstimator(hid_t gid) override;

};

} // namespace qmcplusplus
Expand Down
12 changes: 8 additions & 4 deletions src/Estimators/OperatorEstBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OperatorEstBase
*/
using Data = UPtr<std::vector<QMCT::RealType>>;

/// locality for accumulation data
/// locality for accumulation data. FIXME full documentation of this state machine.
DataLocality data_locality_;

///name of this object
Expand All @@ -63,7 +63,10 @@ class OperatorEstBase
* Depending on data locality the accumlation of the result may be different from
* the single thread write directly into the OperatorEstimator data.
*/
virtual void accumulate(const RefVector<MCPWalker>& walkers, const RefVector<ParticleSet>& psets, const RefVector<TrialWaveFunction>& wfns, RandomGenerator_t& rng) = 0;
virtual void accumulate(const RefVector<MCPWalker>& walkers,
const RefVector<ParticleSet>& psets,
const RefVector<TrialWaveFunction>& wfns,
RandomGenerator_t& rng) = 0;

/** Reduce estimator result data from crowds to rank
*
Expand All @@ -78,7 +81,7 @@ class OperatorEstBase
virtual void normalize(QMCT::RealType invToWgt);

virtual void startBlock(int steps) = 0;

std::vector<QMCT::RealType>& get_data_ref() { return *data_; }

Data& get_data() { return data_; };
Expand Down Expand Up @@ -107,10 +110,11 @@ class OperatorEstBase
/** Return the total walker weight for this block
*/
QMCT::FullPrecRealType get_walkers_weight() { return walkers_weight_; }

protected:
QMCT::FullPrecRealType walkers_weight_;

// convenient Descriptors hdf5 for Operator Estimators only populated for rank scope OperatorEstimator
// convenient Descriptors hdf5 for Operator Estimators only populated for rank scope OperatorEstimator
UPtrVector<ObservableHelper> h5desc_;

/** create the typed data block for the Operator.
Expand Down
10 changes: 6 additions & 4 deletions src/Estimators/SpinDensityNew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ namespace qmcplusplus
SpinDensityNew::SpinDensityNew(SpinDensityInput&& input, const SpeciesSet& species, DataLocality dl)
: OperatorEstBase(dl), input_(std::move(input)), species_(species), species_size_(getSpeciesSize(species))
{
myName = "SpinDensity";
data_locality_ = dl;
myName = "SpinDensity";

if (input_.get_cell().explicitly_defined == true)
lattice_ = input_.get_cell();
Expand Down Expand Up @@ -123,7 +122,10 @@ void SpinDensityNew::startBlock(int steps)
* I tried for readable and not doing the optimizers job.
* The offsets into bare data are already bad enough.
*/
void SpinDensityNew::accumulate(const RefVector<MCPWalker>& walkers, const RefVector<ParticleSet>& psets, const RefVector<TrialWaveFunction>& wfns, RandomGenerator_t& rng)
void SpinDensityNew::accumulate(const RefVector<MCPWalker>& walkers,
const RefVector<ParticleSet>& psets,
const RefVector<TrialWaveFunction>& wfns,
RandomGenerator_t& rng)
{
auto& dp_ = derived_parameters_;
for (int iw = 0; iw < walkers.size(); ++iw)
Expand All @@ -147,7 +149,7 @@ void SpinDensityNew::accumulate(const RefVector<MCPWalker>& walkers, const RefVe
accumulateToData(point, weight);
}
}
};
}

void SpinDensityNew::accumulateToData(size_t point, QMCT::RealType weight)
{
Expand Down
5 changes: 4 additions & 1 deletion src/Estimators/SpinDensityNew.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ class SpinDensityNew : public OperatorEstBase

/** accumulate 1 or more walkers of SpinDensity samples
*/
void accumulate(const RefVector<MCPWalker>& walkers, const RefVector<ParticleSet>& psets, const RefVector<TrialWaveFunction>& wfns, RandomGenerator_t& rng) override;
void accumulate(const RefVector<MCPWalker>& walkers,
const RefVector<ParticleSet>& psets,
const RefVector<TrialWaveFunction>& wfns,
RandomGenerator_t& rng) override;

/** this allows the EstimatorManagerNew to reduce without needing to know the details
* of SpinDensityNew's data.
Expand Down

0 comments on commit a3ce3f5

Please sign in to comment.