Debian Codegraph @

python/src/petsc_casters.h

raw
1 // Copyright (C) 2017 Chris Richardson and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN 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 Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18 #ifndef _DOLFIN_PYBIND11_PETSC
19 #define _DOLFIN_PYBIND11_PETSC
20
21 #include <pybind11/pybind11.h>
22 #include <pybind11/stl.h>
23
24 #ifdef HAS_PETSC
25 #include <petscdm.h>
26 #include <petscksp.h>
27 #include <petscmat.h>
28 #include <petscsnes.h>
29 #include <petscvec.h>
30
31 // pybind11 casters for PETSc/petsc4py objects
32 #ifdef HAS_PYBIND11_PETSC4PY
33 #include <petsc4py/petsc4py.h>
34
35 // Import petsc4py on demand
36 #define VERIFY_PETSC4PY(func) \
37 if (!func) \
38 { \
39 if (import_petsc4py() != 0) \
40 { \
41 std::cout << "ERROR: could not import petsc4py!" << std::endl; \
42 throw std::runtime_error("Error when importing petsc4py"); \
43 } \
44 }
45
46 // Macro for casting between dolfin and petsc4py objects
47 #define PETSC_CASTER_MACRO(TYPE, NAME) \
48 template <> class type_caster<_p_##TYPE> \
49 { \
50 public: \
51 PYBIND11_TYPE_CASTER(TYPE, _(#NAME)); \
52 bool load(handle src, bool) \
53 { \
54 VERIFY_PETSC4PY(PyPetsc##TYPE##_Get); \
55 if (PyObject_TypeCheck(src.ptr(), &PyPetsc##TYPE##_Type) == 0) \
56 return false; \
57 value = PyPetsc##TYPE##_Get(src.ptr()); \
58 return true; \
59 } \
60 \
61 static handle cast(TYPE src, pybind11::return_value_policy policy, handle parent) \
62 { \
63 VERIFY_PETSC4PY(PyPetsc##TYPE##_New); \
64 return pybind11::handle(PyPetsc##TYPE##_New(src)); \
65 } \
66 \
67 operator TYPE() \
68 { return value; } \
69 }
70 #else
71 #define PETSC_CASTER_MACRO(TYPE, NAME) \
72 template <> class type_caster<_p_##TYPE> \
73 { \
74 public: \
75 PYBIND11_TYPE_CASTER(TYPE, _(#NAME)); \
76 bool load(handle src, bool) \
77 { \
78 throw std::runtime_error("DOLFIN has not been configured with petsc4py. Accessing underlying PETSc object requires petsc4py"); \
79 return false; \
80 } \
81 \
82 static handle cast(TYPE src, pybind11::return_value_policy policy, handle parent) \
83 { \
84 throw std::runtime_error("DOLFIN has not been configured with petsc4py. Accessing underlying PETSc object requires petsc4py"); \
85 return handle(); \
86 } \
87 \
88 operator TYPE() \
89 { return value; } \
90 }
91 #endif
92
93
94 namespace pybind11
95 {
96 namespace detail
97 {
98 PETSC_CASTER_MACRO(DM, dm);
99 PETSC_CASTER_MACRO(KSP, ksp);
100 PETSC_CASTER_MACRO(Mat, mat);
101 PETSC_CASTER_MACRO(SNES, snes);
102 PETSC_CASTER_MACRO(Vec, vec);
103 }
104 }
105
106 #undef PETSC_CASTER_MACRO
107
108 #endif
109 #endif