BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ArrayUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Intensity/ArrayUtils.cpp
6 //! @brief Implements various functions to interact from numpy on Python side
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #ifdef BORNAGAIN_PYTHON
16 
18 #include "Base/Utils/PythonCore.h"
19 
20 PyObject* ArrayUtils::createNumpyArray(const std::vector<double>& data)
21 {
22  const size_t ndim(1);
23  npy_int ndim_numpy = ndim;
24  npy_intp* ndimsizes_numpy = new npy_intp[ndim];
25  ndimsizes_numpy[0] = data.size();
26 
27  // creating standalone numpy array
28  PyObject* pyarray = PyArray_SimpleNew(ndim_numpy, ndimsizes_numpy, NPY_DOUBLE);
29  delete[] ndimsizes_numpy;
30  if (pyarray == nullptr)
31  throw Exceptions::RuntimeErrorException("ExportOutputData() -> Panic in PyArray_SimpleNew");
32 
33  // getting pointer to data buffer of numpy array
34  double* array_buffer = (double*)PyArray_DATA((PyArrayObject*)pyarray);
35 
36  for (size_t index = 0; index < data.size(); ++index)
37  *array_buffer++ = data[index];
38 
39  return pyarray;
40 }
41 
42 #endif // BORNAGAIN_PYTHON
Defines various functions to interact from numpy on Python side.
_object PyObject
Definition: PyObject.h:20
Includes python header and takes care of warnings.
PyObject * createNumpyArray(const std::vector< double > &data)
Definition: ArrayUtils.cpp:20