Nektar++
Python/LinearAlgebra/NekMatrix.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekMatrix.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Python wrapper for NekMatrix.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_LIBUTILITIES_PYTHON_LINEARALGERBA_NEKMATRIX_HPP
36#define NEKTAR_LIBUTILITIES_PYTHON_LINEARALGERBA_NEKMATRIX_HPP
37
40
42
43namespace pybind11::detail
44{
45
46template <typename Type, typename T> struct standard_nekmatrix_caster
47{
48public:
49 PYBIND11_TYPE_CASTER(Type, const_name("NekMatrix<T>"));
50
51 bool load(handle src, bool)
52 {
53 // Perform some checks: the source should be an ndarray.
54 if (!array_t<T>::check_(src))
55 {
56 return false;
57 }
58
59 // The array should be a c-style, contiguous array of type T.
60 auto buf = array_t<T, array::c_style | array::forcecast>::ensure(src);
61 if (!buf)
62 {
63 return false;
64 }
65
66 // There should be two dimensions.
67 auto dims = buf.ndim();
68 if (dims != 2)
69 {
70 return false;
71 }
72
73 // Copy data across from the Python array to C++.
74 std::vector<size_t> shape = {buf.shape()[0], buf.shape()[1]};
76 shape[0], shape[1], buf.data(), Nektar::eFULL, buf.data());
77
78 return true;
79 }
80
81 // Conversion part 2 (C++ -> Python)
82 static handle cast(
84 return_value_policy, handle)
85 {
87
88 // Construct a new wrapper matrix to hold onto the data. Assign a
89 // destructor so that the wrapper is cleaned up when the Python object
90 // is deallocated.
91 capsule c(new NMat(src.GetRows(), src.GetColumns(), src.GetPtr(),
93 [](void *ptr) {
94 NMat *mat = (NMat *)ptr;
95 delete mat;
96 });
97
98 // Create the NumPy array, passing the capsule. When we go out of scope,
99 // c's reference count will have been reduced by 1, but array increases
100 // the reference count when it assigns the base to the array.
101 array a({src.GetRows(), src.GetColumns()},
102 {sizeof(T), src.GetRows() * sizeof(T)}, src.GetRawPtr(), c);
103
104 // This causes the array to be released without decreasing its reference
105 // count, which we do since if we just returned a, then the array would
106 // be deallocated immediately when this function returns.
107 return a.release();
108 }
109};
110
111template <typename Type>
112struct type_caster<Nektar::NekMatrix<Type, Nektar::StandardMatrixTag>>
114 Nektar::NekMatrix<Type, Nektar::StandardMatrixTag>, Type>
115{
116};
117
118} // namespace pybind11::detail
119
120#endif
PYBIND11_TYPE_CASTER(Type, const_name("NekMatrix<T>"))
static handle cast(const Nektar::NekMatrix< T, Nektar::StandardMatrixTag > &src, return_value_policy, handle)