Skip to content

Commit f739836

Browse files
committed
Adapt MPIMCI interface to changes and add unittest 3
1 parent 9014cad commit f739836

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

debug/unittests/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
## Unit Test 2
1010

1111
`ut2/`: Like ut1, but with one one-dimensional and one three-dimensional observable.
12+
13+
## Unit Test 3
14+
15+
`ut3/`: Like ut1, but checking with fixed number of findMRT2step/decorrelation steps and fixed blocking technique

debug/unittests/ut3/main.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
29+
class XSquared: public MCIObservableFunctionInterface{
30+
public:
31+
XSquared(): MCIObservableFunctionInterface(3, 1){}
32+
~XSquared(){}
33+
34+
void observableFunction(const double * in, double * out){
35+
out[0] = in[0] * in[0];
36+
}
37+
};
38+
39+
40+
41+
int main(){
42+
const long NMC = 10000;
43+
const double CORRECT_RESULT = 0.5;
44+
45+
ThreeDimGaussianPDF * pdf = new ThreeDimGaussianPDF();
46+
XSquared * obs = new XSquared();
47+
48+
MCI * mci = new MCI(3);
49+
mci->setSeed(5649871);
50+
mci->addSamplingFunction(pdf);
51+
mci->addObservable(obs);
52+
// the integral should provide 0.5 as answer!
53+
54+
double * x = new double[3];
55+
x[0] = 5.; x[1] = -5.; x[2] = 10.;
56+
57+
double * average = new double;
58+
double * error = new double;
59+
60+
// 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)
61+
mci->setX(x);
62+
mci->integrate(NMC, average, error, 0, 0);
63+
std::cout << average[0] << " " << error[0] << std::endl;
64+
assert( abs(average[0]-CORRECT_RESULT) > 2.*error[0] );
65+
66+
// this integral, instead, will provide the right answer
67+
mci->setX(x);
68+
mci->integrate(NMC, average, error, 10, 1000);
69+
std::cout << average[0] << " " << error[0] << std::endl;
70+
assert( abs(average[0]-CORRECT_RESULT) < 2.*error[0] );
71+
72+
// now, doing an integral without finding again the MRT2step and doing the initialDecorrelation will also result in a correct result
73+
mci->integrate(NMC, average, error, 0, 0);
74+
std::cout << average[0] << " " << error[0] << std::endl;
75+
assert( abs(average[0]-CORRECT_RESULT) < 2.*error[0] );
76+
77+
// and using fixed blocking also gives the same result
78+
mci->integrate(NMC, average, error, 0, 0, 15);
79+
std::cout << average[0] << " " << error[0] << std::endl;
80+
assert( abs(average[0]-CORRECT_RESULT) < 2.*error[0] );
81+
82+
83+
delete pdf;
84+
delete obs;
85+
delete mci;
86+
delete [] x;
87+
delete average;
88+
delete error;
89+
90+
return 0;
91+
}

src/MPIMCI.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ namespace MPIMCI
6565

6666

6767
// integrate in parallel and accumulate results
68-
void integrate(MCI * const mci, const long &Nmc, double * average, double * error, bool findMRT2step=true, bool initialdecorrelation=true, bool use_mpi=true) // by setting use_mpi to false you can use this without requiring MPI
68+
69+
void integrate(MCI * const mci, const long &Nmc, double * average, double * error, bool findMRT2step=true, bool initialdecorrelation=true, size_t nblocks=0, bool use_mpi=true) // auto-mode-wrapper
70+
{
71+
integrate(mci, Nmc, average, error, findMRT2step ? -1 : 0, initialdecorrelation ? -1 : 0, nblocks, use_mpi);
72+
}
73+
74+
void integrate(MCI * const mci, const long &Nmc, double * average, double * error, int NfindMRT2stepIterations, int NdecorrelationSteps, size_t nblocks=0, bool use_mpi=true) // by setting use_mpi to false you can use this without requiring MPI
6975
{
7076
if (use_mpi) {
7177
// make sure the user has MPI in the correct state
@@ -81,7 +87,7 @@ namespace MPIMCI
8187
double myAverage[mci->getNObsDim()];
8288
double myError[mci->getNObsDim()];
8389

84-
mci->integrate(Nmc, myAverage, myError, findMRT2step, initialdecorrelation);
90+
mci->integrate(Nmc, myAverage, myError, NfindMRT2stepIterations, NdecorrelationSteps, nblocks);
8591

8692
for (int i=0; i<mci->getNObsDim(); ++i) {
8793
MPI_Allreduce(&myAverage[i], &average[i], 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
@@ -94,7 +100,7 @@ namespace MPIMCI
94100
}
95101
}
96102
else {
97-
mci->integrate(Nmc, average, error, findMRT2step, initialdecorrelation); // regular single thread call
103+
mci->integrate(Nmc, average, error, NfindMRT2stepIterations, NdecorrelationSteps, nblocks); // regular single thread call
98104
}
99105
}
100106

0 commit comments

Comments
 (0)