OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
PyPolynomialPatch.cpp
Go to the documentation of this file.
1//
2// Python API for PolynomialCoefficient (part of the multidimensional polynomial fitting routines)
3//
4// Copyright (c) 2008-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
27
28#include "PyOpal/ExceptionTranslation.h"
29
30namespace PyOpal {
31
33
34using namespace interpolation;
35namespace py = boost::python;
36
38 NDGrid* points,
39 boost::python::list values,
40 int polyPatchOrder,
41 int smoothingOrder) {
42 int gLength = py::len(values);
43 std::vector<std::vector<double> > valuesVec(gLength);
44 for (int i = 0; i < gLength; ++i) {
45 int lineLength = py::len(values[i]);
46 valuesVec[i] = std::vector<double>(lineLength);
47 for (int j = 0; j < lineLength; ++j) {
48 valuesVec[i][j] = py::extract<double>(values[i][j]);
49 }
50 }
51 // points is owned by Python; clone to get a clean copy...
52 Mesh* pointsClone = points->clone();
53 PolynomialPatch* patch = PPSolveFactory(pointsClone,
54 valuesVec,
55 polyPatchOrder,
56 polyPatchOrder).solve();
57 return patch;
58}
59
60py::list function(PolynomialPatch* patch, py::list point) {
61 int pointDim = patch->getPointDimension();
62 int valueDim = patch->getValueDimension();
63 if (py::len(point) != pointDim) {
64 //error
65 }
66 std::vector<double> pointVec(pointDim);
67 for (int i = 0; i < pointDim; ++i) {
68 pointVec[i] = py::extract<double>(point[i]);
69 }
70 std::vector<double> valueVec(valueDim);
71 patch->function(&pointVec[0], &valueVec[0]);
72 py::list value = py::list();
73 for (int i = 0; i < valueDim; ++i) {
74 value.append(valueVec[i]);
75 }
76 return value;
77}
78
79
80const char* module_docstring = "polynomial_patch module returns the field";
81
82BOOST_PYTHON_MODULE(polynomial_patch) {
84 py::class_<PolynomialPatch, boost::noncopyable>("PolynomialPatch")
85 .def("initialise_from_solve_factory", &initialiseFromSolveFactory,
86 py::return_value_policy<py::manage_new_object>())
87 .staticmethod("initialise_from_solve_factory")
88 .def("function", &function)
89 ;
90}
91
92} // namespace PyPolynomialPatch
93} // namespace PyOpal
BOOST_PYTHON_MODULE(polynomial_patch)
PolynomialPatch * initialiseFromSolveFactory(NDGrid *points, boost::python::list values, int polyPatchOrder, int smoothingOrder)
py::list function(PolynomialPatch *patch, py::list point)
Base class for meshing routines.
NDGrid * clone()
Definition NDGrid.h:350
Patches together many SquarePolynomialVectors to make a multidimensional polynomial spline.
unsigned int getPointDimension() const
virtual void function(const double *point, double *value) const
unsigned int getValueDimension() const
PPSolveFactory solves the system of linear equations to interpolate from a grid of points using highe...