CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
CUBAwrapper.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  * Copyright (C) 2010 by Federico Marulli *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful,*
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  *******************************************************************/
20 
37 #include "CUBAwrapper.h"
38 
39 using namespace std;
40 
41 
42 // ============================================================================
43 
44 
45 int cbl::wrapper::cuba::CUBAIntegrand (const int *ndim, const cubareal xx[], const int *ncomp, cubareal ff[], void *userdata)
46 {
48 
49  vector<double> var;
50  double fact = 1.;
51  for (int i=0; i<*ndim; i++) {
52  var.push_back(xx[i]*(pp->integration_limits[i][1]-pp->integration_limits[i][0])+pp->integration_limits[i][0]);
53  fact *= (pp->integration_limits[i][1]-pp->integration_limits[i][0]);
54  }
55 
56  ff[*ncomp-1] = pp->func(var)*fact;
57 
58  return 0;
59 }
60 
61 
62 // ============================================================================
63 
64 
65 cbl::wrapper::cuba::CUBAwrapper::CUBAwrapper (FunctionDoubleVectorPtrVectorRef func, const std::shared_ptr<void> function_parameters, std::vector<double> &parameters, const int ndim)
66 {
67  set_integrand(func, function_parameters, parameters, ndim);
68 }
69 
70 
71 // ============================================================================
72 
73 
75 {
76  set_integrand(func, ndim);
77 }
78 
79 
80 // ============================================================================
81 
82 
83 void cbl::wrapper::cuba::CUBAwrapper::set_integrand (FunctionDoubleVectorPtrVectorRef func, const std::shared_ptr<void> function_parameters, std::vector<double> &parameters, const int ndim)
84 {
85  m_integrand = std::bind(func, std::placeholders::_1, function_parameters, parameters);
86  m_ndim = ndim;
87 }
88 
89 
90 // ============================================================================
91 
92 
94 {
95  m_integrand = func;
96  m_ndim = ndim;
97 }
98 
99 
100 // ============================================================================
101 
102 
103 double cbl::wrapper::cuba::CUBAwrapper::IntegrateVegas (vector<vector<double>> integration_limits, const bool parallelize)
104 {
105  int comp, neval, fail;
106  vector<double> integral(m_inputs.NCOMP, 0), error(m_inputs.NCOMP, 0), prob(m_inputs.NCOMP, 0);
107 
109  userdata->func = m_integrand;
110  userdata->integration_limits = integration_limits;
111 
112  if (!parallelize) {
113  cubacores(0, 0);
114  }
115 
116  Vegas(m_ndim, m_inputs.NCOMP, cbl::wrapper::cuba::CUBAIntegrand, userdata, m_inputs.NVEC,
117  m_inputs.EPSREL, m_inputs.EPSABS, m_inputs.VERBOSE, m_inputs.SEED,
118  m_inputs.MINEVAL, m_inputs.MAXEVAL, m_inputs.NSTART, m_inputs.NINCREASE, m_inputs.NBATCH,
119  m_inputs.GRIDNO, m_inputs.STATEFILE, m_inputs.SPIN.get(),
120  &neval, &fail, integral.data(), error.data(), prob.data());
121 
122  if(m_inputs.VERBOSE>0){
123  printf("VEGAS RESULT:\tneval %d\tfail %d\n", neval, fail);
124  for( comp = 0; comp < m_inputs.NCOMP; ++comp )
125  printf("VEGAS RESULT:\t%.8f +- %.8f\tp = %.3f\n", (double)integral[comp], (double)error[comp], (double)prob[comp]);
126  }
127 
128  return integral[0];
129 }
130 
131 
132 // ============================================================================
133 
134 
135 double cbl::wrapper::cuba::CUBAwrapper::IntegrateSuave (vector<vector<double>> integration_limits, const bool parallelize)
136 {
137  int comp, neval, fail, nregions;
138  vector<double> integral(m_inputs.NCOMP, 0), error(m_inputs.NCOMP, 0), prob(m_inputs.NCOMP, 0);
139 
141  userdata->func = m_integrand;
142  userdata->integration_limits = integration_limits;
143 
144  if (!parallelize) {
145  cubacores(0, 0);
146  }
147 
148  Suave(m_ndim, m_inputs.NCOMP, cbl::wrapper::cuba::CUBAIntegrand, userdata, m_inputs.NVEC,
149  m_inputs.EPSREL, m_inputs.EPSABS, m_inputs.VERBOSE | m_inputs.LAST, m_inputs.SEED,
150  m_inputs.MINEVAL, m_inputs.MAXEVAL, m_inputs.NNEW, m_inputs.NMIN, m_inputs.FLATNESS,
151  m_inputs.STATEFILE, m_inputs.SPIN.get(),
152  &nregions, &neval, &fail, integral.data(), error.data(), prob.data());
153 
154  if(m_inputs.VERBOSE>0){
155  printf("SUAVE RESULT:\tnregions %d\tneval %d\tfail %d\n", nregions, neval, fail);
156  for( comp = 0; comp < m_inputs.NCOMP; ++comp )
157  printf("SUAVE RESULT:\t%.8f +- %.8f\tp = %.3f\n", (double)integral[comp], (double)error[comp], (double)prob[comp]);
158  }
159 
160  return integral[0];
161 }
162 
163 
164 // ============================================================================
165 
166 
167 double cbl::wrapper::cuba::CUBAwrapper::IntegrateDivonne (vector<vector<double>> integration_limits, const bool parallelize)
168 {
169  int comp, neval, fail, nregions;
170  vector<double> integral(m_inputs.NCOMP, 0), error(m_inputs.NCOMP, 0), prob(m_inputs.NCOMP, 0);
171 
173  userdata->func = m_integrand;
174  userdata->integration_limits = integration_limits;
175 
176  if (!parallelize) {
177  cubacores(0, 0);
178  }
179 
180  Divonne(m_ndim, m_inputs.NCOMP, cbl::wrapper::cuba::CUBAIntegrand, userdata, m_inputs.NVEC,
181  m_inputs.EPSREL, m_inputs.EPSABS, m_inputs.VERBOSE, m_inputs.SEED,
182  m_inputs.MINEVAL, m_inputs.MAXEVAL, m_inputs.KEY1, m_inputs.KEY2, m_inputs.KEY3, m_inputs.MAXPASS,
183  m_inputs.BORDER, m_inputs.MAXCHISQ, m_inputs.MINDEVIATION,
184  m_inputs.NGIVEN, m_inputs.LDXGIVEN, NULL, m_inputs.NEXTRA, NULL,
185  m_inputs.STATEFILE, m_inputs.SPIN.get(),
186  &nregions, &neval, &fail, integral.data(), error.data(), prob.data());
187 
188  if(m_inputs.VERBOSE>0){
189  printf("DIVONNE RESULT:\tnregions %d\tneval %d\tfail %d\n", nregions, neval, fail);
190  for( comp = 0; comp < m_inputs.NCOMP; ++comp )
191  printf("DIVONNE RESULT:\t%.8f +- %.8f\tp = %.3f\n", (double)integral[comp], (double)error[comp], (double)prob[comp]);
192  }
193 
194  return integral[0];
195 }
196 
197 
198 // ============================================================================
199 
200 
201 double cbl::wrapper::cuba::CUBAwrapper::IntegrateCuhre (vector<vector<double>> integration_limits, const bool parallelize)
202 {
203  int comp, neval, fail, nregions;
204  vector<double> integral(m_inputs.NCOMP, 0), error(m_inputs.NCOMP, 0), prob(m_inputs.NCOMP, 0);
205 
207  userdata->func = m_integrand;
208  userdata->integration_limits = integration_limits;
209 
210  if (!parallelize) {
211  cubacores(0, 0);
212  }
213 
214  Cuhre(m_ndim, m_inputs.NCOMP, cbl::wrapper::cuba::CUBAIntegrand, userdata, m_inputs.NVEC,
215  m_inputs.EPSREL, m_inputs.EPSABS, m_inputs.VERBOSE | m_inputs.LAST,
216  m_inputs.MINEVAL, m_inputs.MAXEVAL, m_inputs.KEY,
217  m_inputs.STATEFILE, m_inputs.SPIN.get(),
218  &nregions, &neval, &fail, integral.data(), error.data(), prob.data());
219 
220  if(m_inputs.VERBOSE>0){
221  printf("CUHRE RESULT:\tnregions %d\tneval %d\tfail %d\n", nregions, neval, fail);
222  for( comp = 0; comp < m_inputs.NCOMP; ++comp )
223  printf("CUHRE RESULT:\t%.8f +- %.8f\tp = %.3f\n", (double)integral[comp], (double)error[comp], (double)prob[comp]);
224  }
225 
226  return integral[0];
227 }
class CUBAwrapper that wrap CUBA routines for multidimensional integration
double IntegrateSuave(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Suave routine
CUBAwrapper()=default
default constructor
double IntegrateDivonne(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Divonne routine
double IntegrateCuhre(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Cuhre routine
void set_integrand(FunctionDoubleVectorPtrVectorRef func, const std::shared_ptr< void > function_parameters, std::vector< double > &parameters, const int ndim)
set the integrand
Definition: CUBAwrapper.cpp:83
double IntegrateVegas(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Vegas routine
int CUBAIntegrand(const int *ndim, const cubareal xx[], const int *ncomp, cubareal ff[], void *userdata)
generic CUBA integrand
Definition: CUBAwrapper.cpp:45
std::function< double(std::vector< double >)> FunctionDoubleVector
typedef of a function returning a double with a vector in input
Definition: Kernel.h:690
std::function< double(std::vector< double >, std::shared_ptr< void >, std::vector< double > &)> FunctionDoubleVectorPtrVectorRef
typedef of a function returning a double with a vector, a pointer and a vector reference in input
Definition: Kernel.h:702
support object for cuba integrand
Definition: CUBAwrapper.h:157
FunctionDoubleVector func
function to be integrated
Definition: CUBAwrapper.h:159
std::vector< std::vector< double > > integration_limits
limits of the integration
Definition: CUBAwrapper.h:162