-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: addition of potentialSet class (#1991)
Co-authored-by: Adam Washington <[email protected]>
- Loading branch information
Showing
11 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// Copyright (c) 2024 Team Dissolve and contributors | ||
|
||
#include "classes/potentialSet.h" | ||
#include "base/lineParser.h" | ||
#include "classes/atomType.h" | ||
#include "classes/box.h" | ||
#include "classes/configuration.h" | ||
#include "io/export/data1D.h" | ||
#include "items/deserialisers.h" | ||
#include "items/serialisers.h" | ||
#include "templates/algorithms.h" | ||
|
||
PotentialSet::PotentialSet() { fingerprint_ = "NO_FINGERPRINT"; } | ||
|
||
PotentialSet::~PotentialSet() { potentials_.clear(); } | ||
|
||
// Reset Potentials | ||
void PotentialSet::reset() | ||
{ | ||
potentials_.clear(); | ||
fingerprint_ = "NO_FINGERPRINT"; | ||
} | ||
|
||
// Set new fingerprint | ||
void PotentialSet::setFingerprint(std::string_view fingerprint) { fingerprint_ = fingerprint; } | ||
|
||
// Return full map of potentials specified | ||
std::map<std::string, PotentialSet::PotentialData> &PotentialSet::potentialMap() { return potentials_; } | ||
const std::map<std::string, PotentialSet::PotentialData> &PotentialSet::potentialMap() const { return potentials_; } | ||
|
||
/* | ||
* Operators | ||
*/ | ||
|
||
PotentialSet &PotentialSet::operator+=(const double delta) | ||
{ | ||
for (auto &[key, pot] : potentials_) | ||
pot.potential += delta; | ||
return (*this); | ||
} | ||
|
||
PotentialSet &PotentialSet::operator+=(const PotentialSet &source) | ||
{ | ||
for (auto &[key, pot] : source.potentialMap()) | ||
{ | ||
auto it = potentials_.find(key); | ||
if (it != potentials_.end()) | ||
it->second.potential += pot.potential; | ||
else | ||
potentials_[key] = pot; | ||
} | ||
return (*this); | ||
} | ||
|
||
PotentialSet &PotentialSet::operator*=(const double factor) | ||
{ | ||
for (auto &[key, pot] : potentials_) | ||
pot.potential *= factor; | ||
return (*this); | ||
} | ||
|
||
/* | ||
* Serialisation | ||
*/ | ||
|
||
// Read data through specified LineParser | ||
bool PotentialSet::deserialise(LineParser &parser, const CoreData &coreData) | ||
{ | ||
if (parser.readNextLine(LineParser::Defaults, fingerprint_) != LineParser::Success) | ||
return false; | ||
|
||
if (parser.getArgsDelim(LineParser::Defaults) != LineParser::Success) | ||
return false; | ||
auto size = parser.argli(0); | ||
for (auto n = 0; n < size; ++n) | ||
{ | ||
if (parser.getArgsDelim(LineParser::Defaults) != LineParser::Success) | ||
return false; | ||
PotentialData value; | ||
auto key = parser.args(0); | ||
value.count = parser.argi(1); | ||
value.at1 = coreData.findAtomType(parser.args(2)); | ||
value.at2 = coreData.findAtomType(parser.args(3)); | ||
|
||
if (!value.potential.deserialise(parser)) | ||
return false; | ||
|
||
potentials_[key] = value; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// Write data through specified LineParser | ||
bool PotentialSet::serialise(LineParser &parser) const | ||
{ | ||
if (!parser.writeLineF("{}\n", fingerprint_)) | ||
return false; | ||
if (!parser.writeLineF("{}\n", potentials_.size())) | ||
return false; | ||
for (auto &[key, value] : potentials_) | ||
{ | ||
if (!parser.writeLineF("{} {} {} {}\n", key, value.count, value.at1->name(), value.at2->name())) | ||
return false; | ||
if (!value.potential.serialise(parser)) | ||
return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// Copyright (c) 2024 Team Dissolve and contributors | ||
|
||
#pragma once | ||
|
||
#include "classes/atomTypeMix.h" | ||
#include "classes/neutronWeights.h" | ||
#include "math/data1D.h" | ||
#include "math/histogram1D.h" | ||
#include "modules/epsr/epsr.h" | ||
#include "modules/epsrManager/epsrManager.h" | ||
#include "templates/array2D.h" | ||
|
||
// Forward Declarations | ||
class Configuration; | ||
class Interpolator; | ||
|
||
// Set of Potentials | ||
class PotentialSet | ||
{ | ||
public: | ||
PotentialSet(); | ||
~PotentialSet(); | ||
|
||
/* | ||
* Potentials Data | ||
*/ | ||
private: | ||
// Fingerprint for these potentials | ||
std::string fingerprint_; | ||
struct PotentialData | ||
{ | ||
Data1D potential; | ||
double count{0}; | ||
std::shared_ptr<AtomType> at1, at2; | ||
}; | ||
// Map of named potentials to data | ||
std::map<std::string, PotentialData> potentials_; | ||
|
||
public: | ||
// Reset Potentials | ||
void reset(); | ||
// Set new fingerprint | ||
void setFingerprint(std::string_view fingerprint); | ||
// Return fingerprint of potentials | ||
std::string_view fingerprint() const; | ||
// Return full map of potentials specified | ||
std::map<std::string, PotentialData> &potentialMap(); | ||
const std::map<std::string, PotentialData> &potentialMap() const; | ||
|
||
/* | ||
* Operators | ||
*/ | ||
public: | ||
PotentialSet &operator+=(const double delta); | ||
PotentialSet &operator+=(const PotentialSet &source); | ||
PotentialSet &operator*=(const double factor); | ||
|
||
/* | ||
* Serialisation | ||
*/ | ||
public: | ||
// Read data through specified LineParser | ||
bool deserialise(LineParser &parser, const CoreData &coreData); | ||
// Write data through specified LineParser | ||
bool serialise(LineParser &parser) const; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// Copyright (c) 2024 Team Dissolve and contributors | ||
|
||
#include "classes/potentialSet.h" | ||
#include "math/data1D.h" | ||
#include "tests/testData.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace UnitTest | ||
{ | ||
TEST(PotentialSetTest, SimpleAddition) | ||
{ | ||
PotentialSet pots; | ||
Data1D x; | ||
const auto value = 2.0; | ||
x.addPoint(1, value); | ||
pots.potentialMap()["A-A"].potential = x; | ||
pots.potentialMap()["A-B"].potential = x; | ||
pots.potentialMap()["A-C"].potential = x; | ||
|
||
pots += pots; | ||
EXPECT_EQ(4.0, pots.potentialMap()["A-A"].potential.value(0)); | ||
EXPECT_EQ(4.0, pots.potentialMap()["A-B"].potential.value(0)); | ||
EXPECT_EQ(4.0, pots.potentialMap()["A-C"].potential.value(0)); | ||
} | ||
|
||
TEST(PotentialSetTest, Multiplication) | ||
{ | ||
PotentialSet pots; | ||
Data1D x; | ||
const auto value = 3.0; | ||
x.addPoint(1, value); | ||
pots.potentialMap()["A-A"].potential = x; | ||
pots.potentialMap()["A-B"].potential = x; | ||
pots.potentialMap()["A-C"].potential = x; | ||
|
||
pots *= 2; | ||
EXPECT_EQ(6.0, pots.potentialMap()["A-A"].potential.value(0)); | ||
EXPECT_EQ(6.0, pots.potentialMap()["A-B"].potential.value(0)); | ||
EXPECT_EQ(6.0, pots.potentialMap()["A-C"].potential.value(0)); | ||
} | ||
|
||
TEST(PotentialSetTest, ComplexAddition) | ||
{ | ||
PotentialSet pots; | ||
PotentialSet pots2; | ||
Data1D x; | ||
const auto value = 2.0; | ||
x.addPoint(1, value); | ||
pots.potentialMap()["A-A"].potential = x; | ||
pots.potentialMap()["A-B"].potential = x; | ||
pots.potentialMap()["A-C"].potential = x; | ||
|
||
pots2.potentialMap()["A-A"].potential = x; | ||
pots2.potentialMap()["A-B"].potential = x; | ||
pots2.potentialMap()["A-C"].potential = x; | ||
pots2.potentialMap()["A-D"].potential = x; | ||
|
||
pots += pots2; | ||
EXPECT_EQ(4.0, pots.potentialMap()["A-A"].potential.value(0)); | ||
EXPECT_EQ(4.0, pots.potentialMap()["A-B"].potential.value(0)); | ||
EXPECT_EQ(4.0, pots.potentialMap()["A-C"].potential.value(0)); | ||
EXPECT_EQ(2.0, pots.potentialMap()["A-D"].potential.value(0)); | ||
} | ||
|
||
} // namespace UnitTest |