Skip to content

Commit 37d047b

Browse files
authored
Merge pull request #11 from DCM-UPB/multidim_estimator_unittest
Added unittest 2
2 parents 5446555 + fb064f7 commit 37d047b

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ DerivedData
6262
lib*
6363
config.sh
6464
*exe*
65+
vgcore*
6566

6667
src/*.o
6768

debug/unittests/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
## Unit Test 1
66

77
`ut1/`: check that calling the integrator again without doing the findMRT2step and initialDecorrelation gives the same results.
8+
9+
## Unit Test 2
10+
11+
`ut2/`: Like ut1, but with one one-dimensional and one three-dimensional observable.

debug/unittests/ut1/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int main(){
7676
delete pdf;
7777
delete obs;
7878
delete mci;
79-
delete x;
79+
delete [] x;
8080
delete average;
8181
delete error;
8282

debug/unittests/ut2/main.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "MCIntegrator.hpp"
2+
#include "MCISamplingFunctionInterface.hpp"
3+
4+
#include <iostream>
5+
#include <math.h>
6+
#include <assert.h>
7+
8+
using namespace std;
9+
10+
11+
12+
class ThreeDimGaussianPDF: public MCISamplingFunctionInterface{
13+
public:
14+
ThreeDimGaussianPDF(): MCISamplingFunctionInterface(3, 1){}
15+
~ThreeDimGaussianPDF(){}
16+
17+
void samplingFunction(const double *in, double * protovalues){
18+
protovalues[0] = (in[0]*in[0]) + (in[1]*in[1]) + (in[2]*in[2]);
19+
}
20+
21+
22+
double getAcceptance(const double * protoold, const double * protonew){
23+
return exp(-protonew[0]+protoold[0]);
24+
}
25+
26+
};
27+
28+
class XSquared: public MCIObservableFunctionInterface{
29+
public:
30+
XSquared(): MCIObservableFunctionInterface(3, 1){}
31+
~XSquared(){}
32+
33+
void observableFunction(const double * in, double * out){
34+
out[0] = in[0] * in[0];
35+
}
36+
};
37+
38+
39+
class XYZSquared: public MCIObservableFunctionInterface{
40+
public:
41+
XYZSquared(): MCIObservableFunctionInterface(3, 3){}
42+
~XYZSquared(){}
43+
44+
void observableFunction(const double * in, double * out){
45+
out[0] = in[0] * in[0];
46+
out[1] = in[1] * in[1];
47+
out[2] = in[2] * in[2];
48+
}
49+
};
50+
51+
52+
53+
int main(){
54+
const long NMC = 10000;
55+
const double CORRECT_RESULT = 0.5;
56+
57+
ThreeDimGaussianPDF * pdf = new ThreeDimGaussianPDF();
58+
XSquared * obs1d = new XSquared();
59+
XYZSquared * obs3d = new XYZSquared();
60+
61+
MCI * mci = new MCI(3);
62+
mci->setSeed(5649871);
63+
mci->addSamplingFunction(pdf);
64+
mci->addObservable(obs1d);
65+
mci->addObservable(obs3d);
66+
// the integral should provide 0.5 as answer!
67+
68+
double * x = new double[3];
69+
x[0] = 5.; x[1] = -5.; x[2] = 10.;
70+
71+
double * average = new double[4];
72+
double * error = new double[4];
73+
74+
// this integral will give a wrong answer! This is because the starting point is very bad and initialDecorrelation is skipped (as well as the MRT2step automatic setting)
75+
mci->setX(x);
76+
mci->integrate(NMC, average, error, false, false);
77+
for (int i=0; i<mci->getNObsDim(); ++i) {
78+
assert( abs(average[i]-CORRECT_RESULT) > 2.*error[i] );
79+
}
80+
81+
// this integral, instead, will provide the right answer
82+
mci->setX(x);
83+
mci->integrate(NMC, average, error);
84+
for (int i=0; i<mci->getNObsDim(); ++i) {
85+
assert( abs(average[i]-CORRECT_RESULT) < 2.*error[i] );
86+
}
87+
88+
// now, doing an integral without finding again the MRT2step and doing the initialDecorrelation will also result in a correct result
89+
mci->integrate(NMC, average, error, false, false);
90+
for (int i=0; i<mci->getNObsDim(); ++i) {
91+
assert( abs(average[i]-CORRECT_RESULT) < 2.*error[i] );
92+
}
93+
94+
95+
delete pdf;
96+
delete obs1d;
97+
delete obs3d;
98+
delete mci;
99+
delete [] x;
100+
delete [] average;
101+
delete [] error;
102+
103+
return 0;
104+
}

0 commit comments

Comments
 (0)