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