cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Integrator.cpp
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2023-2024 Laurent Forthomme
4
*
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* 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 License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include "
CepGen/Core/Exception.h
"
20
#include "
CepGen/Integration/Integrand.h
"
21
#include "
CepGen/Integration/Integrator.h
"
22
#include "
CepGen/Modules/IntegratorFactory.h
"
23
#include "
CepGen/Utils/String.h
"
24
#include "
CepGenAddOns/PythonWrapper/Environment.h
"
25
#include "
CepGenAddOns/PythonWrapper/Error.h
"
26
#include "
CepGenAddOns/PythonWrapper/Utils.h
"
27
28
namespace
cepgen
{
29
namespace
python
{
30
class
Integrator
final :
public
cepgen::Integrator
{
31
public
:
32
explicit
Integrator
(
const
ParametersList
& params)
33
:
cepgen
::
Integrator
(params), env_(
ParametersList
().setName(
"python_integrator"
)) {
34
auto
cfg =
ObjectPtr::importModule
(steer<std::string>(
"module"
));
35
if
(!cfg)
36
throw
PY_ERROR
<<
"Failed to import the Python module '"
<< steer<std::string>(
"module"
) <<
"'."
;
37
if
(func_ = cfg.
attribute
(
"integrate"
); !func_ || !PyCallable_Check(func_.get()))
38
throw
PY_ERROR
<<
"Failed to retrieve/cast the object to a Python functional."
;
39
}
40
41
void
setLimits
(
const
std::vector<Limits>& lims)
override
{ lims_ =
ObjectPtr::make
(lims); }
42
43
Value
integrate
(
Integrand
& integrand)
override
{
44
gIntegrand
= &integrand;
45
const
auto
iterations = steer<int>(
"iterations"
);
46
const
auto
evals = steer<int>(
"evals"
);
47
PyMethodDef py_integr = {
"integrand"
, py_integrand, METH_VARARGS,
"A python-wrapped integrand"
};
48
ObjectPtr
function(PyCFunction_NewEx(&py_integr,
nullptr
, ObjectPtr::make<std::string>(
"integrand"
).get()));
49
const
auto
value = lims_ ? func_(function.get(), (
int
)integrand.
size
(), iterations, 1000, evals, lims_.get())
50
: func_(function.get(), (
int
)integrand.
size
(), iterations, 1000, evals);
51
if
(!value)
52
throw
PY_ERROR
;
53
const
auto
vals = value.vector<
double
>();
54
if
(vals.size() < 2)
55
throw
CG_FATAL
(
"python:Integrator"
)
56
<<
"Wrong multiplicity of result returned from Python's integration algorithm: "
<< vals <<
"."
;
57
58
return
Value
{vals[0], vals[1]};
59
}
60
61
static
ParametersDescription
description
() {
62
auto
desc =
cepgen::Integrator::description
();
63
desc.setDescription(
"Python integration algorithm"
);
64
desc.add<std::string>(
"module"
,
"IntegrationAlgos.Vegas"
)
65
.setDescription(
"name of the Python module embedding the integrate() function"
);
66
desc.add<
int
>(
"iterations"
, 10);
67
desc.add<
int
>(
"evals"
, 1000);
68
return
desc;
69
}
70
static
Integrand
*
gIntegrand
;
71
72
private
:
73
Environment
env_;
74
ObjectPtr
func_{
nullptr
}, lims_{
nullptr
};
75
static
PyObject* py_integrand(PyObject*
/*self*/
, PyObject* args) {
76
if
(!
gIntegrand
)
77
throw
CG_FATAL
(
"python:Integrator"
) <<
"Integrand was not initialised."
;
78
const
auto
c_args =
ObjectPtr::wrap
(PyTuple_GetItem(args, 0)).
vector
<
double
>();
79
return
ObjectPtr::make<double>(
gIntegrand
->
eval
(c_args)).release();
80
}
81
};
82
Integrand*
Integrator::gIntegrand
=
nullptr
;
83
}
// namespace python
84
}
// namespace cepgen
85
using
PythonIntegrator
=
cepgen::python::Integrator
;
86
REGISTER_INTEGRATOR
(
"python"
,
PythonIntegrator
);
Error.h
PY_ERROR
#define PY_ERROR
Definition
Error.h:25
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
Integrand.h
Integrator.h
IntegratorFactory.h
REGISTER_INTEGRATOR
#define REGISTER_INTEGRATOR(name, obj)
Add a generic process definition to the list of handled processes.
Definition
IntegratorFactory.h:25
String.h
cepgen::Integrand
An integrand wrapper placeholder.
Definition
Integrand.h:27
cepgen::Integrand::eval
virtual double eval(const std::vector< double > &)=0
Compute the integrand for a given coordinates set.
cepgen::Integrand::size
virtual size_t size() const =0
Phase space dimension.
cepgen::Integrator
Monte-Carlo integration algorithm.
Definition
Integrator.h:28
cepgen::Integrator::description
static ParametersDescription description()
Definition
Integrator.cpp:71
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::Value
A scalar value with its uncertainty.
Definition
Value.h:26
cepgen::python::Environment
Definition
Environment.h:28
cepgen::python::Integrator
Definition
Integrator.cpp:30
cepgen::python::Integrator::Integrator
Integrator(const ParametersList ¶ms)
Definition
Integrator.cpp:32
cepgen::python::Integrator::integrate
Value integrate(Integrand &integrand) override
Perform the multidimensional Monte Carlo integration.
Definition
Integrator.cpp:43
cepgen::python::Integrator::description
static ParametersDescription description()
Definition
Integrator.cpp:61
cepgen::python::Integrator::setLimits
void setLimits(const std::vector< Limits > &lims) override
Specify the variables limits on integration.
Definition
Integrator.cpp:41
cepgen::python::Integrator::gIntegrand
static Integrand * gIntegrand
Definition
Integrator.cpp:70
cepgen::python::ObjectPtr
Smart pointer to a Python object and its dereferencing operator.
Definition
ObjectPtr.h:36
cepgen::python::ObjectPtr::attribute
ObjectPtr attribute(const std::string &) const
Retrieve the attribute from a python object.
Definition
ObjectPtr.cpp:368
cepgen::python::ObjectPtr::vector
std::vector< T > vector() const
Retrieve a vector of objects, either from a Python list or tuple.
Definition
ObjectPtr.cpp:130
cepgen::python::ObjectPtr::importModule
static ObjectPtr importModule(const std::string &)
Import a Python module in a new reference-counted Python object.
Definition
ObjectPtr.cpp:374
cepgen::python::ObjectPtr::wrap
static ObjectPtr wrap(PyObject *)
Wrap a PyObject without cleaning at the destructor.
Definition
ObjectPtr.cpp:45
cepgen::python::ObjectPtr::make
static ObjectPtr make(const T &)
Build a new Python object from a C++ one.
Environment.h
Utils.h
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
python
Definition
__init__.py:1
CepGenAddOns
PythonWrapper
Integrator.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7