Merge pull request #3488 from PDoakORNL/OneBodyDensityMatrixInput_1

Adding OneBodyDensityMatricesInput and tests
This commit is contained in:
Ye Luo 2021-09-29 14:07:32 -05:00 committed by GitHub
commit 6873d622aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 342 additions and 15 deletions

View File

@ -24,7 +24,7 @@ set(QMCEST_SRC
OperatorEstBase.cpp
SpinDensityNew.cpp
MomentumDistribution.cpp
)
OneBodyDensityMatricesInput.cpp)
####################################
# create libqmcestimators

View File

@ -63,7 +63,6 @@ void InputSection::readXML(xmlNodePtr cur)
// check input validity
check_valid();
//report();
}
@ -79,7 +78,6 @@ void InputSection::init(const std::unordered_map<std::string, std::any>& init_va
// check input validity
check_valid();
//report();
}
@ -91,7 +89,6 @@ void InputSection::set_defaults()
set_from_value(name, default_value);
}
void InputSection::set_from_stream(const std::string& name, std::istringstream& svalue)
{
if (is_string(name))
@ -158,7 +155,8 @@ void InputSection::check_valid()
<< " has not been assigned\n";
throw UniformCommunicateError(error.str());
}
}
this->checkParticularValidity();
};
void InputSection::report() const

View File

@ -61,7 +61,7 @@ public:
// Enable read-only access to variable values.
// Needs updating to allow copy-less return.
template<typename T>
T get(const std::string& name) const
const T get(const std::string& name) const
{
return std::any_cast<T>(values.at(name));
}
@ -74,6 +74,13 @@ public:
// Initialize from unordered_map/initializer list
void init(const std::unordered_map<std::string, std::any>& init_values);
protected:
/** Do validation for a particular subtype of InputSection
* Called by check_valid.
* Default implementation is noop
*/
virtual void checkParticularValidity() {}
private:
// Query functions
bool is_attribute(const std::string& name) const { return attributes.find(name) != attributes.end(); }

View File

@ -0,0 +1,42 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2021 QMCPACK developers.
//
// File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
//
// Some code refactored from: DensityMatrices1b.cpp
//////////////////////////////////////////////////////////////////////////////////////
#include "string_utils.h"
#include "OneBodyDensityMatricesInput.h"
namespace qmcplusplus
{
OneBodyDensityMatricesInput::OneBodyDensityMatricesInput(){};
OneBodyDensityMatricesInput::OneBodyDensityMatricesInput(xmlNodePtr cur)
{
// This results in checkParticularValidity being called on OneBodyDensityMatrixInputSection
input_section_.readXML(cur);
}
void OneBodyDensityMatricesInput::OneBodyDensityMatrixInputSection::checkParticularValidity()
{
if (has("scale"))
{
Real scale = get<Real>("scale");
std::cout << "SCALE is :" << scale << '\n';
if (scale > 1.0 + 1e-10)
throw UniformCommunicateError("OneBodyDensityMatrices input: scale must be less than one");
else if (scale < 0.0 - 1e-10)
throw UniformCommunicateError("OneBodyDensityMatrices input: scale must be greater than zero");
}
std::string basis_str = get<std::string>("basis");
auto basis_set_names = split(basis_str);
if (basis_set_names.size() == 0 || basis_set_names[0].size() == 0)
throw UniformCommunicateError("OneBodyDensityMatrices input: basis must have at least one sposet");
}
} // namespace qmcplusplus

View File

@ -0,0 +1,120 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2021 QMCPACK developers.
//
// File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
//
// Some code refactored from: DensityMatrices1b.h
//////////////////////////////////////////////////////////////////////////////////////
#ifndef QMCPLUSPLUS_ONE_BODY_DENSITY_MATRICES_INPUT_H
#define QMCPLUSPLUS_ONE_BODY_DENSITY_MATRICES_INPUT_H
#include "Configuration.h"
#include "InputSection.h"
namespace qmcplusplus
{
/** Native representation for DensityMatrices1B Estimator's inputs
*/
class OneBodyDensityMatricesInput
{
public:
enum class Integrators
{
UNIFORM_GRID,
UNIFORM,
DENSITY,
NO_INTEGRATOR
};
enum class Evaluators
{
LOOP,
MATRIX,
NO_EVALUATOR
};
enum class Samplings
{
VOLUME_BASED,
METROPOLIS,
NO_SAMPLING
};
class OneBodyDensityMatrixInputSection : public InputSection
{
public:
/** parse time definition of input parameters */
OneBodyDensityMatrixInputSection()
{
section_name = "OneBodyDensityMatrix";
attributes = {"name", "type"};
parameters = {"basis", "energy_matrix", "integrator", "evaluator", "scale",
"center", "points", "samples", "warmup", "timestep",
"use_drift", "check_overlap", "check_derivatives", "acceptance_ratio", "rstats",
"normalized", "volumed_normed"};
bools = {"energy_matrix", "use_drift", "normalized", "volume_normed",
"check_overlap", "check_derivatives", "rstats", "acceptance_ratio"};
strings = {"name", "type", "basis", "integrator", "evaluator"};
integers = {"points", "samples"};
reals = {"scale", "timestep"};
required = {"name", "basis"};
// I'd much rather see the default defined in simple native c++ as below
}
/** do parse time checks of input */
void checkParticularValidity() override;
};
using Position = QMCTraits::PosType;
using Real = QMCTraits::FullPrecRealType;
OneBodyDensityMatricesInput();
OneBodyDensityMatricesInput(xmlNodePtr cur);
private:
OneBodyDensityMatrixInputSection input_section_;
bool energy_matrix_ = false;
bool use_drift_ = false;
bool normalized_ = true;
bool volume_normalized_ = true;
bool check_overlap_ = false;
bool check_derivatives_ = false;
bool rstats_ = false;
bool acceptance_ratio_ = false;
Integrators integrator_ = Integrators::UNIFORM_GRID;
Samplings sampling_ = Samplings::VOLUME_BASED;
Evaluators evaluator_ = Evaluators::LOOP;
Real scale_ = 1.0;
Position center_ = 0.0;
Real timestep_ = 0.5;
int points_ = 10;
int samples_ = 10;
int warmup_samples_ = 30;
public:
bool get_energy_matrix() const { return energy_matrix_; }
bool get_use_drift() const { return use_drift_; }
bool get_normalized() const { return normalized_; }
bool get_volume_normalized() const { return volume_normalized_; }
bool get_check_overlap() const { return check_overlap_; }
bool get_check_derivatives() const { return check_derivatives_; }
bool get_rstats() const { return rstats_; }
bool get_acceptance_ratio() const { return acceptance_ratio_; }
Integrators get_integrator() const { return integrator_; }
Samplings get_sampling() const { return sampling_; }
Evaluators get_evaluator() const { return evaluator_; }
Real get_scale() const { return scale_; }
Position get_center() const { return center_; }
Real get_timestep() const { return timestep_; }
int get_points() const { return points_; }
int get_samples() const { return samples_; }
int get_warmup_samples() const { return warmup_samples_; }
};
} // namespace qmcplusplus
#endif

View File

@ -23,10 +23,11 @@ set(SRCS
test_manager.cpp
test_EstimatorManagerNew.cpp
test_trace_manager.cpp
SpinDensityTesting.cpp
EstimatorTesting.cpp
test_SpinDensityInput.cpp
test_SpinDensityNew.cpp
test_InputSection.cpp
test_OneBodyDensityMatricesInput.cpp
)
add_executable(${UTEST_EXE} ${SRCS})

View File

@ -2,14 +2,14 @@
// 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: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
//
// File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
//////////////////////////////////////////////////////////////////////////////////////
#include "SpinDensityTesting.h"
#include "EstimatorTesting.h"
namespace qmcplusplus
{

View File

@ -2,15 +2,15 @@
// 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: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
//
// File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
//////////////////////////////////////////////////////////////////////////////////////
#ifndef QMCPLUSPLUS_SPINDENSITYTESTING_H
#define QMCPLUSPLUS_SPINDENSITYTESTING_H
#ifndef QMCPLUSPLUS_ESTIMATOR_TESTING_H
#define QMCPLUSPLUS_ESTIMATOR_TESTING_H
#include "ParticleSet.h"

View File

@ -0,0 +1,53 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2021 QMCPACK developers.
//
// File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
//
// File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
//////////////////////////////////////////////////////////////////////////////////////
#ifndef QMCPLUSPLUS_INVALID_OBDM_INPUT_H
#define QMCPLUSPLUS_INVALID_OBDM_INPUT_H
#include <array>
namespace qmcplusplus
{
namespace testing
{
// clang-format: off
constexpr std::array<const char*, 2> invalid_one_body_density_matrices_input_sections{
R"(
<estimator type="dm1b" name="DensityMatrices">
<parameter name="basis" > spo_u spo_uv </parameter>
<parameter name="evaluator" > matrix </parameter>
<parameter name="integrator" > path </parameter>
<parameter name="scale" > -0.2 </parameter>
<parameter name="samples" > 64 </parameter>
<parameter name="timestep" > 0.5 </parameter>
<parameter name="use_drift" > no </parameter>
</estimator>
)",
R"(
<estimator type="dm1b" name="DensityMatrices">
<parameter name="basis" > dm_basis </parameter>
<parameter name="evaluator" > loop </parameter>
<parameter name="integrator" > uniform </parameter>
<parameter name="samples" > 128 </parameter>
<parameter name="scale" > 1.1 </parameter>
<parameter name="timestep" > 0.5 </parameter>
<parameter name="use_drift" > yes </parameter>
</estimator>
)"
// clang-format: on
};
constexpr int invalid_obdm_input_bad_integrator = 0;
constexpr int invalid_obdm_input_bad_scale = 1;
} // namespace testing
} // namespace qmcplusplus
#endif

View File

@ -0,0 +1,53 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2021 QMCPACK developers.
//
// File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
//
// File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
//////////////////////////////////////////////////////////////////////////////////////
#ifndef QMCPLUSPLUS_VALID_OBDM_INPUT_H
#define QMCPLUSPLUS_VALID_OBDM_INPUT_H
#include <array>
namespace qmcplusplus
{
namespace testing
{
// clang-format: off
constexpr std::array<const char*, 2> valid_one_body_density_matrices_input_sections{
R"(
<estimator type="dm1b" name="DensityMatrices">
<parameter name="basis" > spo_u spo_uv </parameter>
<parameter name="evaluator" > matrix </parameter>
<parameter name="integrator" > density </parameter>
<parameter name="samples" > 64 </parameter>
<parameter name="timestep" > 0.5 </parameter>
<parameter name="use_drift" > no </parameter>
</estimator>
)",
R"(
<estimator type="dm1b" name="DensityMatrices">
<parameter name="basis" > dm_basis </parameter>
<parameter name="evaluator" > loop </parameter>
<parameter name="integrator" > uniform </parameter>
<parameter name="samples" > 128 </parameter>
<parameter name="scale" > 0.8 </parameter>
<parameter name="timestep" > 0.5 </parameter>
<parameter name="use_drift" > yes </parameter>
</estimator>
)"
// clang-format: on
};
constexpr int valid_obdm_input = 0;
constexpr int vlaid_obdm_input_scale = 1;
} // namespace testing
} // namespace qmcplusplus
#endif

View File

@ -0,0 +1,53 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2021 QMCPACK developers.
//
// File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
//
// File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
//////////////////////////////////////////////////////////////////////////////////////
#include "catch.hpp"
#include "OneBodyDensityMatricesInput.h"
#include "ValidOneBodyDensityMatricesInput.h"
#include "InvalidOneBodyDensityMatricesInput.h"
#include "EstimatorTesting.h"
#include "ParticleSet.h"
#include "OhmmsData/Libxml2Doc.h"
#include "Message/UniformCommunicateError.h"
#include <iostream>
namespace qmcplusplus
{
TEST_CASE("OneBodyDensityMatricesInput::from_xml", "[estimators]")
{
using POLT = PtclOnLatticeTraits;
using Lattice = POLT::ParticleLayout_t;
for (auto input_xml : testing::valid_one_body_density_matrices_input_sections)
{
Libxml2Document doc;
bool okay = doc.parseFromString(input_xml);
REQUIRE(okay);
xmlNodePtr node = doc.getRoot();
OneBodyDensityMatricesInput obdmi(node);
}
for (auto input_xml : testing::invalid_one_body_density_matrices_input_sections)
{
Libxml2Document doc;
bool okay = doc.parseFromString(input_xml);
REQUIRE(okay);
xmlNodePtr node = doc.getRoot();
CHECK_THROWS_AS(OneBodyDensityMatricesInput(node), UniformCommunicateError);
}
}
} // namespace qmcplusplus

View File

@ -14,7 +14,7 @@
#include "SpinDensityInput.h"
#include "ValidSpinDensityInput.h"
#include "SpinDensityTesting.h"
#include "EstimatorTesting.h"
#include "ParticleSet.h"
#include "OhmmsData/Libxml2Doc.h"

View File

@ -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: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
//
@ -18,7 +18,7 @@
#include "RandomForTest.h"
#include "ParticleSet.h"
#include "TrialWaveFunction.h"
#include "SpinDensityTesting.h"
#include "EstimatorTesting.h"
#include "OhmmsData/Libxml2Doc.h"