Skip to content

Commit cec7325

Browse files
committed
Merge branch 'add-custom-valences'.
Support definition of custom valences per atom symbols in BVSCalculator.
2 parents eb5bc37 + 535de35 commit cec7325

5 files changed

+138
-12
lines changed

src/diffpy/srreal/BVParametersTable.cpp

+32-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

@@ -118,6 +140,13 @@ void BVParametersTable::resetAll()
118140
}
119141

120142

143+
const BVParametersTable::SetOfBVParam&
144+
BVParametersTable::getAllCustom() const
145+
{
146+
return mcustomtable;
147+
}
148+
149+
121150
BVParametersTable::SetOfBVParam
122151
BVParametersTable::getAll() const
123152
{
@@ -171,5 +200,6 @@ BVParametersTable::getStandardSetOfBVParam() const
171200
// Serialization -------------------------------------------------------------
172201

173202
DIFFPY_INSTANTIATE_SERIALIZATION(diffpy::srreal::BVParametersTable)
203+
DIFFPY_INSTANTIATE_PTR_SERIALIZATION(diffpy::srreal::BVParametersTable)
174204

175205
// End of file

src/diffpy/srreal/BVParametersTable.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <boost/serialization/export.hpp>
2525
#include <boost/serialization/shared_ptr.hpp>
2626
#include <diffpy/boostextensions/serialize_unordered_set.hpp>
27+
#include <diffpy/boostextensions/serialize_unordered_map.hpp>
2728
#include <diffpy/srreal/BVParam.hpp>
2829

2930
namespace diffpy {
@@ -44,6 +45,9 @@ class BVParametersTable
4445
static const BVParam& none();
4546

4647
// methods
48+
int getAtomValence(const std::string&) const;
49+
void setAtomValence(const std::string&, int value);
50+
void resetAtomValences();
4751
const BVParam& lookup(const BVParam&) const;
4852
const BVParam& lookup(
4953
const std::string& smbl0, const std::string& smbl1) const;
@@ -57,12 +61,17 @@ class BVParametersTable
5761
void resetCustom(const std::string& atom0, int valence0,
5862
const std::string& atom1, int valence1);
5963
void resetAll();
64+
const SetOfBVParam& getAllCustom() const;
6065
SetOfBVParam getAll() const;
6166

6267
private:
6368

69+
// types
70+
typedef boost::unordered_map<std::string, int> AtomTypeValence;
71+
6472
// data
6573
SetOfBVParam mcustomtable;
74+
AtomTypeValence matomvalence;
6675

6776
// methods
6877
const SetOfBVParam& getStandardSetOfBVParam() const;
@@ -73,11 +82,18 @@ class BVParametersTable
7382
void serialize(Archive& ar, const unsigned int version)
7483
{
7584
ar & mcustomtable;
85+
if (version >= 1) {
86+
ar & matomvalence;
87+
}
7688
}
7789

7890
}; // class BVParametersTable
7991

8092
} // namespace srreal
8193
} // namespace diffpy
8294

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

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/TestBVParametersTable.hpp

+48-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
*
1717
*****************************************************************************/
1818

19-
#include <stdexcept>
2019
#include <cxxtest/TestSuite.h>
2120

22-
#include <diffpy/serialization.hpp>
2321
#include <diffpy/srreal/BVParametersTable.hpp>
22+
#include "serialization_helpers.hpp"
2423

2524
using namespace std;
2625
using namespace diffpy::srreal;
@@ -71,6 +70,16 @@ class TestBVParametersTable : public CxxTest::TestSuite
7170
}
7271

7372

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+
7483
void test_lookup()
7584
{
7685
BVParam bp = mbvtb->lookup("Xx", 0, "Yy", 3);
@@ -91,6 +100,11 @@ class TestBVParametersTable : public CxxTest::TestSuite
91100
const BVParam& bnacl0 = mbvtb->lookup("Na", 1, "Cl", -1);
92101
TS_ASSERT_EQUALS(&bnacl0, &mbvtb->lookup("Na+", "Cl-"));
93102
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"));
94108
}
95109

96110

@@ -135,6 +149,25 @@ class TestBVParametersTable : public CxxTest::TestSuite
135149
}
136150

137151

152+
void test_getAllCustom()
153+
{
154+
TS_ASSERT(mbvtb->getAllCustom().empty());
155+
BVParam mymgo("Mg", 2, "O", -2);
156+
mbvtb->setCustom(mymgo);
157+
TS_ASSERT_EQUALS(1u, mbvtb->getAllCustom().size());
158+
const BVParam& bp = mbvtb->lookup("O2-", "Mg2+");
159+
BVParametersTable::SetOfBVParam::const_iterator ii;
160+
ii = mbvtb->getAllCustom().find(mymgo);
161+
TS_ASSERT_DIFFERS(ii, mbvtb->getAllCustom().end());
162+
TS_ASSERT_DIFFERS(&mymgo, &(*ii));
163+
TS_ASSERT_EQUALS(&bp, &(*ii));
164+
TS_ASSERT_EQUALS(mymgo, *ii);
165+
// erase the only custom parameter
166+
mbvtb->resetCustom(*ii);
167+
TS_ASSERT(mbvtb->getAllCustom().empty());
168+
}
169+
170+
138171
void test_getAll()
139172
{
140173
BVParametersTable::SetOfBVParam allpars0, allpars1;
@@ -164,13 +197,8 @@ class TestBVParametersTable : public CxxTest::TestSuite
164197
BVParam mymgo("O", -2, "Mg", 2, 3.456, 0.55, "pj2");
165198
mbvtb->setCustom(mynacl);
166199
mbvtb->setCustom(mymgo);
167-
stringstream storage(ios::in | ios::out | ios::binary);
168-
diffpy::serialization::oarchive oa(storage, ios::binary);
169-
oa << mbvtb;
170-
diffpy::serialization::iarchive ia(storage, ios::binary);
171-
BVParametersTablePtr bvtb1;
172-
TS_ASSERT(!bvtb1.get());
173-
ia >> bvtb1;
200+
// check serialization of shared pointers
201+
BVParametersTablePtr bvtb1 = dumpandload(mbvtb);
174202
TS_ASSERT_DIFFERS(mbvtb.get(), bvtb1.get());
175203
TS_ASSERT_EQUALS(2.345,
176204
bvtb1->lookup("Cl", -1, "Na", 1).mRo);
@@ -184,6 +212,17 @@ class TestBVParametersTable : public CxxTest::TestSuite
184212
bvtb1->lookup("O", -2, "Mg", 2).mB);
185213
TS_ASSERT_EQUALS(string("pj2"),
186214
bvtb1->lookup("O", -2, "Mg", 2).mref_id);
215+
// check serialization of instances
216+
BVParametersTable tb2 = dumpandload(*mbvtb);
217+
TS_ASSERT_EQUALS(2u, tb2.getAllCustom().size());
218+
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"));
187226
}
188227

189228
}; // class TestBVParametersTable

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)