Skip to content

Commit e1b4694

Browse files
committedJun 18, 2016
Support predefined valences in BVParametersTable.
Add methods getAtomValence, setAtomValence, resetAtomValences to extract valence from atom or ion symbol. Adjust serialization to store these custom valences.
1 parent 6d6c033 commit e1b4694

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed
 

‎src/diffpy/srreal/BVParametersTable.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ const BVParam& BVParametersTable::none()
4141

4242
// Public Methods ------------------------------------------------------------
4343

44+
int BVParametersTable::getAtomValence(const string& smbl) const
45+
{
46+
AtomTypeValence::const_iterator ii = matomvalence.find(smbl);
47+
int rv = (ii != matomvalence.end()) ? ii->second : atomValence(smbl);
48+
return rv;
49+
}
50+
51+
52+
void BVParametersTable::setAtomValence(const string& smbl, int value)
53+
{
54+
matomvalence[smbl] = value;
55+
}
56+
57+
58+
void BVParametersTable::resetAtomValences()
59+
{
60+
matomvalence.clear();
61+
}
62+
63+
4464
const BVParam& BVParametersTable::lookup(const BVParam& bpk) const
4565
{
4666
SetOfBVParam::const_iterator bpit;
@@ -66,9 +86,11 @@ const BVParam& BVParametersTable::lookup(const BVParam& bpk) const
6686
const BVParam&
6787
BVParametersTable::lookup(const string& smbl0, const string& smbl1) const
6888
{
89+
const int v0 = this->getAtomValence(smbl0);
90+
const int v1 = this->getAtomValence(smbl1);
6991
const BVParam& rv = this->lookup(
70-
atomBareSymbol(smbl0), atomValence(smbl0),
71-
atomBareSymbol(smbl1), atomValence(smbl1));
92+
atomBareSymbol(smbl0), v0,
93+
atomBareSymbol(smbl1), v1);
7294
return rv;
7395
}
7496

‎src/diffpy/srreal/BVParametersTable.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,18 @@ class BVParametersTable
8282
void serialize(Archive& ar, const unsigned int version)
8383
{
8484
ar & mcustomtable;
85+
if (version >= 1) {
86+
ar & matomvalence;
87+
}
8588
}
8689

8790
}; // class BVParametersTable
8891

8992
} // namespace srreal
9093
} // namespace diffpy
9194

95+
// Serialization -------------------------------------------------------------
96+
97+
BOOST_CLASS_VERSION(diffpy::srreal::BVParametersTable, 1)
98+
9299
#endif // BVPARAMETERSTABLE_HPP_INCLUDED

‎src/tests/TestBVParametersTable.hpp

+22
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ class TestBVParametersTable : public CxxTest::TestSuite
7070
}
7171

7272

73+
void test_atomvalence()
74+
{
75+
TS_ASSERT_EQUALS(0, mbvtb->getAtomValence("O"));
76+
mbvtb->setAtomValence("O", -2);
77+
TS_ASSERT_EQUALS(-2, mbvtb->getAtomValence("O"));
78+
mbvtb->resetAtomValences();
79+
TS_ASSERT_EQUALS(0, mbvtb->getAtomValence("O"));
80+
}
81+
82+
7383
void test_lookup()
7484
{
7585
BVParam bp = mbvtb->lookup("Xx", 0, "Yy", 3);
@@ -90,6 +100,11 @@ class TestBVParametersTable : public CxxTest::TestSuite
90100
const BVParam& bnacl0 = mbvtb->lookup("Na", 1, "Cl", -1);
91101
TS_ASSERT_EQUALS(&bnacl0, &mbvtb->lookup("Na+", "Cl-"));
92102
TS_ASSERT_EQUALS(&bnacl0, &mbvtb->lookup("Cl1-", "Na1+"));
103+
const BVParam& bpnone = BVParametersTable::none();
104+
TS_ASSERT_EQUALS(&bpnone, &mbvtb->lookup("Na", "Cl"));
105+
mbvtb->setAtomValence("Na", +1);
106+
mbvtb->setAtomValence("Cl", -1);
107+
TS_ASSERT_EQUALS(&bnacl0, &mbvtb->lookup("Na", "Cl"));
93108
}
94109

95110

@@ -201,6 +216,13 @@ class TestBVParametersTable : public CxxTest::TestSuite
201216
BVParametersTable tb2 = dumpandload(*mbvtb);
202217
TS_ASSERT_EQUALS(2u, tb2.getAllCustom().size());
203218
TS_ASSERT_EQUALS(mynacl, tb2.lookup("Cl-", "Na+"));
219+
// check serialization of customized valences
220+
tb2.setAtomValence("Na", 1);
221+
tb2.setAtomValence("Cl", -1);
222+
BVParametersTable tb3 = dumpandload(tb2);
223+
TS_ASSERT_EQUALS(mynacl, tb3.lookup("Cl", "Na"));
224+
const BVParam& bpnone = BVParametersTable::none();
225+
TS_ASSERT_EQUALS(bpnone, mbvtb->lookup("Cl", "Na"));
204226
}
205227

206228
}; // class TestBVParametersTable

0 commit comments

Comments
 (0)
Please sign in to comment.