Skip to content

Commit 535de35

Browse files
committed
Apply custom valences in BVSCalculator.
Use custom valences in BVParametersTable when evaluating bond valence sums. Add relevant unit tests.
1 parent e1b4694 commit 535de35

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/diffpy/srreal/BVSCalculator.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,12 @@ void BVSCalculator::cacheStructureData()
186186
int cntsites = this->countSites();
187187
mstructure_cache.baresymbols.resize(cntsites);
188188
mstructure_cache.valences.resize(cntsites);
189+
const BVParametersTable& bvtb = *(this->getBVParamTable());
189190
for (int i = 0; i < cntsites; ++i)
190191
{
191192
const string& smbl = mstructure->siteAtomType(i);
192193
mstructure_cache.baresymbols[i] = atomBareSymbol(smbl);
193-
mstructure_cache.valences[i] = atomValence(smbl);
194+
mstructure_cache.valences[i] = bvtb.getAtomValence(smbl);
194195
}
195196
}
196197

src/tests/TestBVSCalculator.hpp

+40
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
using namespace std;
2727
using diffpy::srreal::BVSCalculator;
28+
using diffpy::srreal::BVParametersTablePtr;
2829
using diffpy::srreal::StructureAdapterPtr;
30+
using diffpy::srreal::PeriodicStructureAdapter;
31+
using diffpy::srreal::PeriodicStructureAdapterPtr;
2932

3033
//////////////////////////////////////////////////////////////////////////////
3134
// class TestBVSCalculator
@@ -76,6 +79,39 @@ class TestBVSCalculator : public CxxTest::TestSuite
7679
}
7780

7881

82+
void test_customAtomValences()
83+
{
84+
using diffpy::srreal::Atom;
85+
const double eps = 1e-4;
86+
mbvc->eval(mnacl);
87+
TS_ASSERT_DELTA(0.01352, mbvc->bvrmsdiff(), eps);
88+
BVParametersTablePtr bvtb = mbvc->getBVParamTable();
89+
bvtb->setAtomValence("Cl1-", 0);
90+
mbvc->eval(mnacl);
91+
TS_ASSERT_EQUALS(0.0, mbvc->value()[0]);
92+
TS_ASSERT_EQUALS(0.0, mbvc->value()[4]);
93+
// create structure with bare atom symbols "Na", "Cl".
94+
PeriodicStructureAdapterPtr naclbare =
95+
boost::dynamic_pointer_cast<
96+
PeriodicStructureAdapter>(mnacl->clone());
97+
for (int i = 0; i < naclbare->countSites(); ++i)
98+
{
99+
Atom& a = naclbare->at(i);
100+
a.atomtype = a.atomtype.substr(0, 2);
101+
}
102+
TS_ASSERT_EQUALS(string("Na"), naclbare->siteAtomType(0));
103+
// verify valence sums are zero for a standard setup.
104+
mbvc->eval(naclbare);
105+
TS_ASSERT_EQUALS(0.0, mbvc->value()[0]);
106+
TS_ASSERT_EQUALS(0.0, mbvc->value()[4]);
107+
// verify valence sums with custom atom valences.
108+
bvtb->setAtomValence("Na", +1);
109+
bvtb->setAtomValence("Cl", -1);
110+
mbvc->eval(naclbare);
111+
TS_ASSERT_DELTA(0.01352, mbvc->bvrmsdiff(), eps);
112+
}
113+
114+
79115
void test_setValencePrecision()
80116
{
81117
TS_ASSERT_THROWS(mbvc->setValencePrecision(0), invalid_argument);
@@ -97,6 +133,10 @@ class TestBVSCalculator : public CxxTest::TestSuite
97133
mbvc->setValencePrecision(1e-7);
98134
mbvc->setDoubleAttr("rmax", 5.0);
99135
TS_ASSERT_EQUALS(5.0, mbvc->getDoubleAttr("rmaxused"));
136+
// check if value updates with changes in the table.
137+
BVParametersTablePtr bvtb = mbvc->getBVParamTable();
138+
bvtb->setCustom("Na", 1, "Cl", -1, 0.0, 0);
139+
TS_ASSERT_EQUALS(0.0, mbvc->getDoubleAttr("rmaxused"));
100140
}
101141

102142

0 commit comments

Comments
 (0)