Skip to content

Commit 8830822

Browse files
committed
Validate if optimized evaluation is supported.
PairQuantity.setEvaluatorType(OPTIMIZED) now throws exception if optimized evaluation is not supported. Do not delay until the first calculation call.
1 parent 4699d81 commit 8830822

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/diffpy/srreal/PQEvaluator.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ PQEvaluatorType PQEvaluatorBasic::typeintused() const
8686
}
8787

8888

89+
void PQEvaluatorBasic::validate(PairQuantity& pq) const
90+
{
91+
return;
92+
}
93+
94+
8995
void PQEvaluatorBasic::updateValue(
9096
PairQuantity& pq, StructureAdapterPtr stru)
9197
{
@@ -162,6 +168,14 @@ PQEvaluatorType PQEvaluatorOptimized::typeint() const
162168
}
163169

164170

171+
void PQEvaluatorOptimized::validate(PairQuantity& pq) const
172+
{
173+
// Check if PairQuantity provides stashPartialValue.
174+
pq.stashPartialValue();
175+
pq.restorePartialValue();
176+
}
177+
178+
165179
void PQEvaluatorOptimized::updateValue(
166180
PairQuantity& pq, StructureAdapterPtr stru)
167181
{

src/diffpy/srreal/PQEvaluator.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class PQEvaluatorBasic
6565
virtual PQEvaluatorType typeint() const;
6666
PQEvaluatorType typeintused() const;
6767
virtual void updateValue(PairQuantity&, StructureAdapterPtr);
68+
virtual void validate(PairQuantity&) const;
6869
void setFlag(PQEvaluatorFlag flag, bool value);
6970
bool getFlag(PQEvaluatorFlag flag) const;
7071
void setupParallelRun(int cpuindex, int ncpu);
@@ -103,6 +104,7 @@ class PQEvaluatorOptimized : public PQEvaluatorBasic
103104

104105
// methods
105106
virtual PQEvaluatorType typeint() const;
107+
virtual void validate(PairQuantity&) const;
106108
virtual void updateValue(PairQuantity&, StructureAdapterPtr);
107109

108110
private:

src/diffpy/srreal/PairQuantity.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,20 @@ const double& PairQuantity::getRmax() const
143143
void PairQuantity::setEvaluatorType(PQEvaluatorType evtp)
144144
{
145145
if (mevaluator.get() && mevaluator->typeint() == evtp) return;
146-
mevaluator = createPQEvaluator(evtp, mevaluator);
146+
PQEvaluatorPtr pqev = createPQEvaluator(evtp, mevaluator);
147+
// validate the new evaluator object
148+
try
149+
{
150+
pqev->validate(*this);
151+
}
152+
catch (logic_error e)
153+
{
154+
string emsg("EvaluatorType not supported. ");
155+
emsg += e.what();
156+
throw invalid_argument(emsg);
157+
}
158+
// validator is good here, use it now.
159+
mevaluator = pqev;
147160
this->resetValue();
148161
}
149162

src/tests/TestPQEvaluator.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <diffpy/srreal/PQEvaluator.hpp>
2626
#include <diffpy/srreal/AtomicStructureAdapter.hpp>
2727
#include <diffpy/srreal/PDFCalculator.hpp>
28+
#include <diffpy/srreal/OverlapCalculator.hpp>
2829

2930
namespace diffpy {
3031
namespace srreal {
@@ -70,6 +71,7 @@ class TestPQEvaluator : public CxxTest::TestSuite
7071
mstru9->erase(9);
7172
}
7273

74+
7375
void test_PDF_change_atom()
7476
{
7577
PDFCalculator pdfcb;
@@ -148,6 +150,16 @@ class TestPQEvaluator : public CxxTest::TestSuite
148150
TS_ASSERT(allclose(gb, go));
149151
}
150152

153+
154+
void test_optimized_unsupported()
155+
{
156+
OverlapCalculator olc;
157+
TS_ASSERT_EQUALS(BASIC, olc.getEvaluatorType());
158+
TS_ASSERT_THROWS(
159+
olc.setEvaluatorType(OPTIMIZED), invalid_argument);
160+
TS_ASSERT_EQUALS(BASIC, olc.getEvaluatorType());
161+
}
162+
151163
}; // class TestPQEvaluator
152164

153165
} // namespace srreal

0 commit comments

Comments
 (0)