OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
PyNDGrid.cpp
Go to the documentation of this file.
1 //
2 // Python API for NDGrid
3 //
4 // Copyright (c) 2023, Chris Rogers, STFC Rutherford Appleton Laboratory, Didcot, UK
5 //
6 // This file is part of OPAL.
7 //
8 // OPAL is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
15 //
16 
17 #include <Python.h>
18 #include <structmember.h>
19 
20 #include <exception>
21 #include <iostream>
22 #include <boost/python.hpp>
23 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
24 
26 
27 #include "PyOpal/ExceptionTranslation.h"
29 
30 
31 namespace PyOpal {
32 
33 namespace PyNDGrid {
34 
35 using namespace interpolation;
36 namespace py = boost::python;
37 
38 class Inform;
39 extern Inform *gmsg;
40 
41 NDGrid initialiseVariableSpacing(boost::python::list gridCoordinates) {
42  int gLength = boost::python::len(gridCoordinates);
43  std::vector<std::vector<double> > coords(gLength);
44  for (int i = 0; i < gLength; ++i) {
45  int lineLength = boost::python::len(gridCoordinates[i]);
46  coords[i] = std::vector<double>(lineLength);
47  for (int j = 0; j < lineLength; ++j) {
48  coords[i][j] = boost::python::extract<double>(gridCoordinates[i][j]);
49  }
50  }
51  return NDGrid(coords);
52 }
53 
54 
55 NDGrid initialiseFixedSpacing(boost::python::list size,
56  boost::python::list spacing,
57  boost::python::list min) {
58  int dim = boost::python::len(size);
59  if (dim != boost::python::len(spacing)) {
60  // error
61  } else if (dim != boost::python::len(min)) {
62  // error
63  }
64  std::vector<int> sizeVec(dim);
65  std::vector<double> spacingVec(dim);
66  std::vector<double> minVec(dim);
67  for (int i = 0; i < dim; ++i) {
68  sizeVec[i] = boost::python::extract<int>(size[i]);
69  spacingVec[i] = boost::python::extract<double>(spacing[i]);
70  minVec[i] = boost::python::extract<double>(min[i]);
71  }
72 
73  return NDGrid(dim, &sizeVec[0], &spacingVec[0], &minVec[0]);
74 }
75 
76 py::list coordVector(NDGrid& grid, int dimension) {
77  if (dimension >= grid.getPositionDimension()) {
78  throw OpalException("PyNDGrid::coordVector",
79  "Dimension out of bounds");
80  }
81  std::vector<double> vec = grid.coordVector(dimension);
82  py::list coord = py::list();
83  for (size_t i = 0; i < vec.size(); ++i) {
84  coord.append(vec[i]);
85  }
86  return coord;
87 }
88 
89 
90 
91 
92 const char* module_docstring = "ndgrid module for generating grids";
93 
96  boost::python::class_<NDGrid>("NDGrid")
97  .def("initialise_variable_spacing", &initialiseVariableSpacing)
98  .staticmethod("initialise_variable_spacing")
99  .def("initialise_fixed_spacing", &initialiseFixedSpacing)
100  .staticmethod("initialise_fixed_spacing")
101  .def("size", &NDGrid::size)
102  .def("get_position_dimension", &NDGrid::getPositionDimension)
103  .def("coord_vector", &coordVector)
104  ;
105 }
106 } // namespace PyNDGrid
107 } // namespace PyOpal
BOOST_PYTHON_MODULE(ndgrid)
Definition: PyNDGrid.cpp:94
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:76
Inform * gmsg
Definition: Main.cpp:70
The base class for all OPAL exceptions.
Definition: OpalException.h:28
std::vector< double > coordVector(const int &dimension) const
Definition: NDGrid.h:314
Definition: Vec.h:21
Definition: Inform.h:42
const char * module_docstring
Definition: PyNDGrid.cpp:92
py::list coordVector(NDGrid &grid, int dimension)
Definition: PyNDGrid.cpp:76
int size(const int &dimension) const
Definition: NDGrid.h:310
NDGrid initialiseFixedSpacing(boost::python::list size, boost::python::list spacing, boost::python::list min)
Definition: PyNDGrid.cpp:55
NDGrid initialiseVariableSpacing(boost::python::list gridCoordinates)
Definition: PyNDGrid.cpp:41
int getPositionDimension() const
Definition: NDGrid.h:376