Skip to content

Commit cb5f60f

Browse files
authored
Merge pull request #6 from francesco086/continuous_integration
Continuous integration
2 parents e8310ba + 2ba6c3f commit cb5f60f

File tree

10 files changed

+174
-9
lines changed

10 files changed

+174
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ src/*.o
6868
debug/*.o
6969
debug/*.txt
7070

71+
debug/unittests/ut*/*.o
72+
debug/unittests/ut*/*.sh
73+
7174
examples/*/a.out
7275
examples/*/*.o
7376

config_template.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ FLAGS="-std=c++11 -Wall -Werror"
1212
# Optimization flags
1313
OPTFLAGS="-O3"
1414

15+
# Debuggin flags
16+
DEBUGFLAGS="-g -O0"
17+
1518
#FFNN Library (used in ex3)
1619
FFNN_FOLDER="/...../FeedForwardNeuralNetwork"
1720
IFFNN="-I${FFNN_FOLDER}/src/"

debug/unittests/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# LEGEND OF THE UNIT TESTS
2+
3+
4+
5+
## Unit Test 1
6+
7+
`ut1/`: check that calling the integrator again without doing the findMRT2step and initialDecorrelation gives the same results.

debug/unittests/run.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
for folder in ut*
4+
do
5+
cp run_single_unittest.sh ${folder}/
6+
cd ${folder}
7+
echo "-> running unittest ${folder}"
8+
echo ""
9+
./run_single_unittest.sh
10+
cd ..
11+
done
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# After using this script it is necessary to run again the build.sh script
4+
# for generating again the library with the optimization flags
5+
6+
OS_NAME=$(uname)
7+
source ../../../config.sh
8+
9+
\rm -f exe
10+
\rm -f *.o
11+
12+
ROOT_FOLDER=$(dirname $(dirname $(dirname $(pwd))))
13+
14+
15+
## Build the debugging main executable
16+
$CC $FLAGS $DEBUGFLAGS -Wall -I${ROOT_FOLDER}/src/ -c *.cpp
17+
18+
case ${OS_NAME} in
19+
"Linux")
20+
$CC $FLAGS $DEBUGFLAGS -L${ROOT_FOLDER} -Wl,-rpath=${RPATH} -o exe *.o -l${LIBNAME}
21+
;;
22+
"Darwin")
23+
$CC $FLAGS $DEBUGFLAGS -L${ROOT_FOLDER} -o exe *.o -l${LIBNAME}
24+
;;
25+
*)
26+
echo "The detected operating system is not between the known ones (Linux and Darwin)"
27+
;;
28+
esac
29+
30+
# Run the debugging executable
31+
./exe

debug/unittests/ut1/main.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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, false, false);
63+
assert( abs(average[0]-CORRECT_RESULT) > 2.*error[0] );
64+
65+
// this integral, instead, will provide the right answer
66+
mci->setX(x);
67+
mci->integrate(NMC, average, error);
68+
assert( abs(average[0]-CORRECT_RESULT) < 2.*error[0] );
69+
70+
// now, doing an integral without finding again the MRT2step and doing the initialDecorrelation will also result in a correct result
71+
mci->integrate(NMC, average, error, false, false);
72+
assert( abs(average[0]-CORRECT_RESULT) < 2.*error[0] );
73+
74+
75+
76+
delete pdf;
77+
delete obs;
78+
delete mci;
79+
delete x;
80+
delete average;
81+
delete error;
82+
83+
return 0;
84+
}

doc/user_manual.pdf

947 Bytes
Binary file not shown.

doc/user_manual.tex

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ \section{Settings}
7979
It is not strictly necessary to set all the following settings because there are already default values, however the user should be aware of their existence.
8080

8181

82+
83+
\subsection{Random generator's seed} % (fold)
84+
\label{sub:random_generator_seed}
85+
86+
\begin{verbatim}
87+
> void setSeed(const long seed)
88+
\end{verbatim}
89+
90+
One can set the random generator's seed \verb+seed+ simply by invoking:
91+
\begin{verbatim}
92+
mci.setSeed(seed);
93+
\end{verbatim}
94+
% subsection random_generator_seed (end)
95+
96+
97+
8298
\subsection{Integral domain} % (fold)
8399
\label{sub:integral_domain}
84100

@@ -474,7 +490,7 @@ \section{Integration} % (fold)
474490
\label{sec:integration}
475491

476492
\begin{verbatim}
477-
> void integrate(const long &Nmc, double * average, double * error);
493+
> void integrate(const long &Nmc, double * average, double * error, bool findMRT2step=true, bool initialdecorrelation=true);
478494
\end{verbatim}
479495

480496
Once that all the settings are done, one can obtain the result of the integral by invoking
@@ -485,6 +501,8 @@ \section{Integration} % (fold)
485501
\verb+Nmc+ is the number of sampled points: The larger this value, the more accurate will be the result, according to the well known $1/\sqrt{\verb+NMC+}$ rule.
486502
\verb+average+ will contain the resulting numeric estimation of the integral with an estimated standard deviation stored in \verb+error+.
487503

504+
The two optional parameters \verb+findMRT2step+ and \verb+initialdecorrelation+ can be set to false in case one wants to skip the automatic setting of the MRT2 step and the initial random MC steps used to "warm up" the walker (i.e. perform initial decorrelation from the starting walker position).
505+
488506
The Monte Carlo integral is composed by several substep, completely hidden to the user.
489507
We report them for general knowledge.
490508
\begin{enumerate}

src/MCIntegrator.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010

1111
// --- Integrate
1212

13-
void MCI::integrate(const long &Nmc, double * average, double * error)
13+
void MCI::integrate(const long &Nmc, double * average, double * error, bool findMRT2step, bool initialdecorrelation)
1414
{
1515
long i;
1616

1717
if ( _flagpdf )
18-
{
19-
//find the optimal mrt2 step
20-
this->findMRT2Step();
21-
// take care to do the initial decorrelation of the walker
22-
this->initialDecorrelation();
23-
}
18+
{
19+
//find the optimal mrt2 step
20+
if (findMRT2step) this->findMRT2Step();
21+
// take care to do the initial decorrelation of the walker
22+
if (initialdecorrelation) this->initialDecorrelation();
23+
}
2424

2525
//allocation of the array where the data will be stored
2626
_datax = new double*[Nmc];
@@ -526,6 +526,12 @@ void MCI::setIRange(const double * const * irange)
526526
}
527527

528528

529+
void MCI::setSeed(const long seed)
530+
{
531+
_rgen.seed(seed);
532+
}
533+
534+
529535
// --- Constructor and Destructor
530536

531537
MCI::MCI(const int & ndim)

src/MCIntegrator.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class MCI
8888
~MCI(); //Destructor
8989

9090
// --- Setters
91+
void setSeed(const long seed);
92+
9193
void setIRange(const double * const * irange);
9294

9395
void setX(const double * x);
@@ -127,7 +129,7 @@ class MCI
127129
double getAcceptanceRate(){return (double(_acc)/(double(_acc+_rej)));}
128130

129131
// --- Integrate
130-
void integrate(const long &Nmc, double * average, double * error);
132+
void integrate(const long &Nmc, double * average, double * error, bool findMRT2step=true, bool initialdecorrelation=true);
131133

132134

133135
};

0 commit comments

Comments
 (0)