|
| 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 | +} |
0 commit comments