Skip to content

Commit

Permalink
Merge pull request QMCPACK#4859 from PDoakORNL/consistent_precision_i…
Browse files Browse the repository at this point in the history
…nputsection

Make InputSection full precision only.
  • Loading branch information
ye-luo authored Dec 6, 2023
2 parents b0faa12 + 2b1f1dc commit 3fe4a91
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 40 deletions.
15 changes: 10 additions & 5 deletions src/Estimators/InputSection.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) 2022 QMCPACK developers.
// Copyright (c) 2023 QMCPACK developers.
//
// File developed by: Jaron T. Krogel, [email protected], Oak Ridge National Laboratory
// Peter W. Doak, [email protected], Oak Ridge National Laboratory
Expand Down Expand Up @@ -49,7 +49,6 @@ void InputSection::readAttributes(xmlNodePtr cur,
setFromStreamCustom(element_name, qualified_name, stream);
else
setFromStream(qualified_name, stream);

att = att->next;
}
}
Expand All @@ -65,6 +64,7 @@ void InputSection::handleDelegate(const std::string& ename, const xmlNodePtr ele

void InputSection::readXML(xmlNodePtr cur)
{
assert(cur != nullptr);
// For historical reasons that actual "type" of the element/input section is expressed in a very inconsistent way.
// It could be coded via the element name i.e. the tag, or at minimum a method, type, or name attribute.
std::string section_ename{lowerCase(castXMLCharToChar(cur->name))};
Expand Down Expand Up @@ -200,7 +200,7 @@ void InputSection::setFromStream(const std::string& name, std::istringstream& sv
else if (isMultiReal(name))
{
std::vector<Real> real_values;
for (FullPrecReal value; svalue >> value;)
for (Real value; svalue >> value;)
real_values.push_back(static_cast<Real>(value));
assignValue(name, real_values);
}
Expand All @@ -219,7 +219,7 @@ void InputSection::setFromStream(const std::string& name, std::istringstream& sv
}
else if (isReal(name))
{
FullPrecReal value;
Real value;
svalue >> value;
assignValue(name, Real(value));
}
Expand Down Expand Up @@ -249,7 +249,7 @@ void InputSection::assignValue(const std::string& name, const T& value)
else
{
if (has(name))
std::any_cast<std::vector<T>>(values_[name]).push_back(value);
std::any_cast<std::vector<T>&>(values_[name]).push_back(value);
else
values_[name] = std::vector<T>{value};
}
Expand Down Expand Up @@ -318,6 +318,11 @@ void InputSection::report(std::ostream& out) const
out << "\n\n";
}

void InputSection::report() const
{
report(app_log());
}

std::any InputSection::lookupAnyEnum(const std::string& enum_name,
const std::string& enum_value,
const std::unordered_map<std::string, std::any>& enum_map)
Expand Down
16 changes: 11 additions & 5 deletions src/Estimators/InputSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@
namespace qmcplusplus
{

/** Input section provides basic parsing and a uniform method of access to the raw parsed input.
* It is still expected to be a composed part of the actual input class for a simulation class.
* It does not operate at reduced precision, i.e. numerical input is always parsed and retrieved
* at full precision. Gettting values from input section is strongly typed so you will get errors
* if you try to get numeric types at reduced precision.
*/
class InputSection
{
public:
using FullPrecReal = QMCTraits::FullPrecRealType;
using Real = QMCTraits::RealType;
using Position = QMCTraits::PosType;
using Real = QMCTraits::FullPrecRealType;
using Position = typename QMCTypes<Real,OHMMS_DIM>::PosType;

InputSection() = default;
InputSection(const InputSection& other) = default;

InputSection& operator=(const InputSection& other) = default;

protected:
// Internal data below comprise the input specification.
// Most apply attributes to input variables.
Expand Down Expand Up @@ -233,11 +239,11 @@ class InputSection
static std::any lookupAnyEnum(const std::string& enum_name,
const std::string& enum_value,
const std::unordered_map<std::string, std::any>& enum_map);

protected:
// Simple dump of contents. Useful for developing and as
// debugging function useful when input sections local error reports
// may be insufficient.
void report() const;
void report(std::ostream& out) const;

private:
Expand Down
4 changes: 4 additions & 0 deletions src/Estimators/MagnetizationDensityInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include "EstimatorInput.h"
namespace qmcplusplus
{
template bool InputSection::setIfInInput<qmcplusplus::MagnetizationDensityInput::Integrator>(
qmcplusplus::MagnetizationDensityInput::Integrator& var,
const std::string& tag);

MagnetizationDensityInput::MagnetizationDensityInput(xmlNodePtr cur)
{
input_section_.readXML(cur);
Expand Down
9 changes: 7 additions & 2 deletions src/Estimators/MagnetizationDensityInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class MagnetizationDensityInput
lookup_input_enum_value{{"integrator-simpsons", Integrator::SIMPSONS},
{"integrator-montecarlo", Integrator::MONTECARLO}};
// clang-format on
using Real = QMCTraits::RealType;
using Real = QMCTraits::FullPrecRealType;
using POLT = PtclOnLatticeTraits;
using Lattice = POLT::ParticleLayout;
using PosType = QMCTraits::PosType;
using PosType = TinyVector<Real, OHMMS_DIM>;
using Consumer = MagnetizationDensity;
static constexpr int DIM = QMCTraits::DIM;

Expand Down Expand Up @@ -127,5 +127,10 @@ class MagnetizationDensityInput
bool have_center_ = false;
bool have_corner_ = false;
};

extern template bool InputSection::setIfInInput<qmcplusplus::MagnetizationDensityInput::Integrator>(
qmcplusplus::MagnetizationDensityInput::Integrator& var,
const std::string& tag);

} // namespace qmcplusplus
#endif /* QMCPLUSPLUS_MAGNETIZATION_DENSITY_INPUT_H */
3 changes: 2 additions & 1 deletion src/Estimators/MomentumDistribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ MomentumDistribution::MomentumDistribution(MomentumDistributionInput&& mdi,
for (int i = 0; i < OHMMS_DIM; i++)
vec_length[i] = 2.0 * M_PI * std::sqrt(dot(Lattice.Gv[i], Lattice.Gv[i]));
RealType kmax = input_.get_kmax();
PosType kmaxs = {input_.get_kmax0(), input_.get_kmax1(), input_.get_kmax2()};
auto realCast = [](auto& real) { return static_cast<RealType>(real); };
PosType kmaxs = {realCast(input_.get_kmax0()), realCast(input_.get_kmax1()), realCast(input_.get_kmax2())};
RealType sum_kmaxs = kmaxs[0] + kmaxs[1] + kmaxs[2];
RealType sphere_kmax;
bool sphere = input_.get_kmax() > 0.0 ? true : false;
Expand Down
4 changes: 2 additions & 2 deletions src/Estimators/MomentumDistributionInput.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) 2021 QMCPACK developers.
// Copyright (c) 2023 QMCPACK developers.
//
// File developed by: Jaron T. Krogel, [email protected], Oak Ridge National Laboratory
//
Expand All @@ -24,7 +24,7 @@ class MomentumDistributionInput
{
public:
using Consumer = MomentumDistribution;
using Real = QMCTraits::RealType;
using Real = QMCTraits::FullPrecRealType;

class MomentumDistributionInputSection : public InputSection
{
Expand Down
5 changes: 4 additions & 1 deletion src/Estimators/OneBodyDensityMatricesInput.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) 2021 QMCPACK developers.
// Copyright (c) 2023 QMCPACK developers.
//
// File developed by: Peter Doak, [email protected], Oak Ridge National Laboratory
//
Expand All @@ -16,6 +16,9 @@
namespace qmcplusplus
{

template bool InputSection::setIfInInput<qmcplusplus::OneBodyDensityMatricesInput::Integrator>(qmcplusplus::OneBodyDensityMatricesInput::Integrator& var, const std::string& tag);


OneBodyDensityMatricesInput::OneBodyDensityMatricesInput(xmlNodePtr cur)
{
// This results in checkParticularValidity being called on OneBodyDensityMatrixInputSection
Expand Down
8 changes: 6 additions & 2 deletions src/Estimators/OneBodyDensityMatricesInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class OneBodyDensityMatricesInput
std::any assignAnyEnum(const std::string& name) const override;
};

using Position = QMCTraits::PosType;
using Real = QMCTraits::RealType;
using Real = QMCTraits::FullPrecRealType;
using Position = TinyVector<Real, OHMMS_DIM>;

/** default copy constructor
* This is required due to OBDMI being part of a variant used as a vector element.
Expand Down Expand Up @@ -155,6 +155,10 @@ class OneBodyDensityMatricesInput
friend class testing::OneBodyDensityMatricesTests;
};

extern template bool InputSection::setIfInInput<qmcplusplus::OneBodyDensityMatricesInput::Integrator>(
qmcplusplus::OneBodyDensityMatricesInput::Integrator& var,
const std::string& tag);

} // namespace qmcplusplus

#endif
4 changes: 3 additions & 1 deletion src/Estimators/ReferencePointsInput.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) 2022 QMCPACK developers.
// Copyright (c) 2023 QMCPACK developers.
//
// File developed by: Jaron T. Krogel, [email protected], Oak Ridge National Laboratory
// Mark A. Berrill, [email protected], Oak Ridge National Laboratory
Expand All @@ -20,6 +20,8 @@

namespace qmcplusplus
{
template bool InputSection::setIfInInput<ReferencePointsInput::Coord>(ReferencePointsInput::Coord& var,
const std::string& tag);

ReferencePointsInput::ReferencePointsInput(xmlNodePtr cur)
{
Expand Down
10 changes: 6 additions & 4 deletions src/Estimators/ReferencePointsInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ class ReferencePointsInput
* the same CoordType.
*/
ReferencePointsInput() = default;

ReferencePointsInput(xmlNodePtr cur);
ReferencePointsInput(const ReferencePointsInput& rpi) = default;

Coord get_coord_form() const { return coord_form_; }
const Points& get_points() const { return points_; }

private:
void readRefPointsXML(xmlNodePtr cur);
// As far as I can tell if not specified in input this is what is happening in legacy.
Expand All @@ -94,11 +95,12 @@ class ReferencePointsInput
static constexpr std::string_view error_tag{"ReferencePointsInput input: "};
};

/** factory function used by InputSection to make reference points Input
/** factory function used by InputSection to make reference points Input
* \param[out] value_label key value in delegating InputSection for storing the constructed Input from processed node.
*/
std::any makeReferencePointsInput(xmlNodePtr, std::string& value_label);
std::any makeReferencePointsInput(xmlNodePtr, std::string& value_label);

extern template bool InputSection::setIfInInput<ReferencePointsInput::Coord>(ReferencePointsInput::Coord& var,
const std::string& tag);
} // namespace qmcplusplus

#endif
30 changes: 13 additions & 17 deletions src/Estimators/tests/test_InputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

namespace qmcplusplus
{
using Real = QMCTraits::RealType;
// Take note that all input is done at full precision.
using Real = QMCTraits::FullPrecRealType;

enum class TestEnum1
{
Expand All @@ -51,11 +52,11 @@ class TestInputSection : public InputSection
TestInputSection()
{
section_name = "Test";
attributes = {"name", "samples", "kmax", "full","width::type"};
attributes = {"name", "samples", "kmax", "full", "width::type"};
parameters = {"label", "count", "width", "rational", "testenum1",
"testenum2", "sposets", "center", "density", "target"};
required = {"count", "full"};
strings = {"name", "label","width::type"};
strings = {"name", "label", "width::type"};
multi_strings = {"sposets"};
multi_reals = {"density"};
multiple = {"target"};
Expand Down Expand Up @@ -227,28 +228,27 @@ TEST_CASE("InputSection::readXML", "[estimators]")
// required attribute/parameter is missing
// unrecognized attribute/parameter encountered

std::unordered_map<std::string, const char*> invalid_inputs = {
{"missing_attribute", R"(
std::unordered_map<std::string, const char*> invalid_inputs =
{{"missing_attribute", R"(
<test>
<parameter name="count"> 15 </parameter>
</test>
)"},
{"missing_parameter", R"(
{"missing_parameter", R"(
<test full="no"/>
)"},
{"foreign_attribute", R"(
{"foreign_attribute", R"(
<test full="no" area="51">
<parameter name="count"> 15 </parameter>
</test>
)"},
{"foreign_parameter", R"(
{"foreign_parameter", R"(
<test full="no">
<parameter name="count"> 15 </parameter>
<parameter name="area" > 51 </parameter>
</test>
)"},
{"invalid_section_name", R"(<not_test><parameter name="nothing"></parameter></not_test>)"}
};
{"invalid_section_name", R"(<not_test><parameter name="nothing"></parameter></not_test>)"}};

for (auto& [label, xml] : invalid_inputs)
{
Expand Down Expand Up @@ -430,9 +430,6 @@ class CustomTestInput : public InputSection
{
std::string cus_at;
std::getline(svalue, cus_at);
// if (ename != section_name)
// values_[ename + " " + name] = cus_at;
// else
values_[name] = cus_at;
}
else
Expand Down Expand Up @@ -490,7 +487,6 @@ TEST_CASE("InputSection::custom", "[estimators]")
CHECK(ws.numbers == exp_numbers);

cti.report(std::cout);

std::string custom_attribute = cti.get<std::string>("with_custom::custom_attribute");
CHECK(custom_attribute == "This is a custom attribute.");
custom_attribute = cti.get<std::string>("custom_attribute");
Expand Down Expand Up @@ -520,10 +516,10 @@ class AnotherInput
public:
AnotherInputSection()
{
section_name = "AnotherInput";
section_name = "AnotherInput";
section_name_alternates = {"ainput"};
attributes = {"name", "optional"};
strings = {"name", "optional"};
attributes = {"name", "optional"};
strings = {"name", "optional"};
}
};

Expand Down

0 comments on commit 3fe4a91

Please sign in to comment.