|
| 1 | +////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// This file is distributed under the University of Illinois/NCSA Open Source License. |
| 3 | +// See LICENSE file in top directory for details. |
| 4 | +// |
| 5 | +// Copyright (c) 2024 QMCPACK developers. |
| 6 | +// |
| 7 | +// File developed by: Peter W. Doak, doakpw@ornl.gov, Oak Ridge National Laboratory |
| 8 | +// |
| 9 | +// Some code refactored from: QMCHamiltonian/{SkEstimator.cpp, SkAllEstimator.cpp} |
| 10 | +////////////////////////////////////////////////////////////////////////////////////// |
| 11 | + |
| 12 | +#include "StructureFactorEstimator.h" |
| 13 | +#include "StructureFactorInput.h" |
| 14 | +#include "ParticleSet.h" |
| 15 | +#include <LongRange/StructFact.h> |
| 16 | + |
| 17 | +namespace qmcplusplus |
| 18 | +{ |
| 19 | + |
| 20 | +StructureFactorEstimator::StructureFactorEstimator(const StructureFactorInput& sfi, |
| 21 | + const ParticleSet& pset_ions, |
| 22 | + const ParticleSet& pset_elec, |
| 23 | + DataLocality data_locality) |
| 24 | + : OperatorEstBase(data_locality), |
| 25 | + input_(sfi), |
| 26 | + elns_(pset_elec), |
| 27 | + elec_num_species_(elns_.getSpeciesSet().getTotalNum()), |
| 28 | + ions_(pset_ions), |
| 29 | + ion_num_species_(ions_.getSpeciesSet().getTotalNum()) |
| 30 | +{ |
| 31 | + my_name_ = "StructureFactorEstimator"; |
| 32 | + |
| 33 | + num_kpoints_ = pset_ions.getSimulationCell().getKLists().getNumK(); |
| 34 | + kshell_offsets_ = pset_ions.getSimulationCell().getKLists().getKShell(); |
| 35 | + int max_kshell = kshell_offsets_.size() - 1; |
| 36 | + |
| 37 | + rhok_tot_r_.resize(num_kpoints_); |
| 38 | + rhok_tot_i_.resize(num_kpoints_); |
| 39 | + sfk_e_e_.resize(num_kpoints_); |
| 40 | + rhok_e_.resize(num_kpoints_); |
| 41 | + // Legacy comment, but I don't trust it. |
| 42 | + //for values, we are including e-e structure factor, and e-Ion. So a total of NumIonSpecies+1 structure factors. |
| 43 | + //+2 for the real and imaginary parts of rho_k^e |
| 44 | + // |
| 45 | + // skAll seems to be written for e-e sf + complex rho_k^e |
| 46 | + data_.resize(3 * num_kpoints_); |
| 47 | + kmags_.resize(max_kshell); |
| 48 | + one_over_degeneracy_kshell_.resize(max_kshell); |
| 49 | + for (int ks = 0; ks < max_kshell; ks++) |
| 50 | + { |
| 51 | + kmags_[ks] = std::sqrt(pset_elec.getSimulationCell().getKLists().getKSQWorking()[kshell_offsets_[ks]]); |
| 52 | + one_over_degeneracy_kshell_[ks] = 1.0 / static_cast<Real>(kshell_offsets_[ks + 1] - kshell_offsets_[ks]); |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +StructureFactorEstimator::StructureFactorEstimator(const StructureFactorEstimator& sfe, DataLocality dl) |
| 57 | + : qmcplusplus::StructureFactorEstimator(sfe) |
| 58 | +{ |
| 59 | + data_locality_ = dl; |
| 60 | +} |
| 61 | + |
| 62 | +void StructureFactorEstimator::accumulate(const RefVector<MCPWalker>& walkers, |
| 63 | + const RefVector<ParticleSet>& psets, |
| 64 | + const RefVector<TrialWaveFunction>& wfns, |
| 65 | + const RefVector<QMCHamiltonian>& hams, |
| 66 | + RandomBase<FullPrecReal>& rng) |
| 67 | +{ |
| 68 | + for (int iw = 0; iw < walkers.size(); ++iw) |
| 69 | + { |
| 70 | + Real weight = walkers[iw].get().Weight; |
| 71 | + |
| 72 | + //sum over species |
| 73 | + std::copy(psets[iw].get().getSK().rhok_r[0], psets[iw].get().getSK().rhok_r[0] + num_kpoints_, rhok_tot_r_.begin()); |
| 74 | + std::copy(psets[iw].get().getSK().rhok_i[0], psets[iw].get().getSK().rhok_i[0] + num_kpoints_, rhok_tot_i_.begin()); |
| 75 | + for (int i = 1; i < elec_num_species_; ++i) |
| 76 | + accumulate_elements(psets[iw].get().getSK().rhok_r[i], psets[iw].get().getSK().rhok_r[i] + num_kpoints_, |
| 77 | + rhok_tot_r_.begin()); |
| 78 | + for (int i = 1; i < elec_num_species_; ++i) |
| 79 | + accumulate_elements(psets[iw].get().getSK().rhok_i[i], psets[iw].get().getSK().rhok_i[i] + num_kpoints_, |
| 80 | + rhok_tot_i_.begin()); |
| 81 | + |
| 82 | + for (int k = 0; k < num_kpoints_; k++) |
| 83 | + { |
| 84 | + sfk_e_e_[k] += weight * (rhok_tot_r_[k] * rhok_tot_r_[k] + rhok_tot_i_[k] * rhok_tot_i_[k]); |
| 85 | + rhok_e_[k] += weight * std::complex<Real>{rhok_tot_r_[k], rhok_tot_i_[k]}; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +void StructureFactorEstimator::registerOperatorEstimator(hdf_archive& file) |
| 91 | +{ |
| 92 | + hdf_path hdf_name{my_name_}; |
| 93 | + hdf_path path_variables = hdf_name / std::string_view("kpoints"); |
| 94 | + file.push(path_variables, true); |
| 95 | + // hdf_archive wants non const references, if that code was better |
| 96 | + // this would be unecessary |
| 97 | + file.write(const_cast<std::vector<KPt>&>(ions_.getSimulationCell().getKLists().getKptsCartWorking()), "value"); |
| 98 | + file.pop(); |
| 99 | +} |
| 100 | + |
| 101 | +void StructureFactorEstimator::write(hdf_archive& file) |
| 102 | +{ |
| 103 | + hdf_path hdf_name{my_name_}; |
| 104 | + file.push(hdf_name); |
| 105 | + // this is call rhok_e_e in the output of the legacy, but that is just wrong it is |rhok_e_e_|^2 |
| 106 | + file.write(sfk_e_e_, "sfk_e_e"); |
| 107 | + file.write(rhok_e_, "rhok_e_"); |
| 108 | +} |
| 109 | + |
| 110 | +void StructureFactorEstimator::collect(const RefVector<OperatorEstBase>& type_erased_operator_estimators) |
| 111 | +{ |
| 112 | + int num_crowds = type_erased_operator_estimators.size(); |
| 113 | + for (OperatorEstBase& crowd_oeb : type_erased_operator_estimators) |
| 114 | + { |
| 115 | + StructureFactorEstimator& crowd_sfe = dynamic_cast<StructureFactorEstimator&>(crowd_oeb); |
| 116 | + this->sfk_e_e_ += crowd_sfe.sfk_e_e_; |
| 117 | + this->rhok_e_ += crowd_sfe.rhok_e_; |
| 118 | + } |
| 119 | + OperatorEstBase::collect(type_erased_operator_estimators); |
| 120 | +} |
| 121 | + |
| 122 | +void StructureFactorEstimator::startBlock(int steps) {} |
| 123 | + |
| 124 | +UPtr<OperatorEstBase> StructureFactorEstimator::spawnCrowdClone() const |
| 125 | +{ |
| 126 | + UPtr<StructureFactorEstimator> spawn(std::make_unique<StructureFactorEstimator>(*this, data_locality_)); |
| 127 | + return spawn; |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace qmcplusplus |
0 commit comments