| 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 |
#include <memory> |
| 19 |
#include <pybind11/pybind11.h> |
| 20 |
#include <pybind11/numpy.h> |
| 21 |
#include <pybind11/eigen.h> |
| 22 |
#include <pybind11/stl.h> |
| 23 |
#include <pybind11/operators.h> |
| 24 |
|
| 25 |
#ifdef HAS_PYBIND11_PETSC4PY |
| 26 |
#include <petsc4py/petsc4py.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "casters.h" |
| 30 |
|
| 31 |
#include <dolfin/common/Array.h> |
| 32 |
#include <dolfin/la/solve.h> |
| 33 |
#include <dolfin/la/BlockVector.h> |
| 34 |
#include <dolfin/la/BlockMatrix.h> |
| 35 |
#include <dolfin/la/GenericLinearOperator.h> |
| 36 |
#include <dolfin/la/GenericLinearSolver.h> |
| 37 |
#include <dolfin/la/GenericTensor.h> |
| 38 |
#include <dolfin/la/GenericMatrix.h> |
| 39 |
#include <dolfin/la/GenericVector.h> |
| 40 |
#include <dolfin/la/IndexMap.h> |
| 41 |
#include <dolfin/la/LinearAlgebraObject.h> |
| 42 |
#include <dolfin/la/LinearOperator.h> |
| 43 |
#include <dolfin/la/Matrix.h> |
| 44 |
#include <dolfin/la/Vector.h> |
| 45 |
#include <dolfin/la/Scalar.h> |
| 46 |
#include <dolfin/la/TensorLayout.h> |
| 47 |
#include <dolfin/la/DefaultFactory.h> |
| 48 |
#include <dolfin/la/EigenFactory.h> |
| 49 |
#include <dolfin/la/EigenMatrix.h> |
| 50 |
#include <dolfin/la/EigenVector.h> |
| 51 |
#include <dolfin/la/PETScFactory.h> |
| 52 |
#include <dolfin/la/PETScKrylovSolver.h> |
| 53 |
#include <dolfin/la/PETScLUSolver.h> |
| 54 |
#include <dolfin/la/PETScLinearOperator.h> |
| 55 |
#include <dolfin/la/PETScMatrix.h> |
| 56 |
#include <dolfin/la/PETScNestMatrix.h> |
| 57 |
#include <dolfin/la/PETScOptions.h> |
| 58 |
#include <dolfin/la/PETScPreconditioner.h> |
| 59 |
#include <dolfin/la/PETScVector.h> |
| 60 |
#include <dolfin/la/SUNDIALSNVector.h> |
| 61 |
#include <dolfin/la/TpetraFactory.h> |
| 62 |
#include <dolfin/la/TpetraMatrix.h> |
| 63 |
#include <dolfin/la/TpetraVector.h> |
| 64 |
#include <dolfin/la/TrilinosPreconditioner.h> |
| 65 |
#include <dolfin/la/MueluPreconditioner.h> |
| 66 |
#include <dolfin/la/BelosKrylovSolver.h> |
| 67 |
#include <dolfin/la/LUSolver.h> |
| 68 |
#include <dolfin/la/KrylovSolver.h> |
| 69 |
#include <dolfin/la/SLEPcEigenSolver.h> |
| 70 |
#include <dolfin/la/SparsityPattern.h> |
| 71 |
#include <dolfin/la/solve.h> |
| 72 |
#include <dolfin/la/VectorSpaceBasis.h> |
| 73 |
#include <dolfin/la/test_nullspace.h> |
| 74 |
|
| 75 |
namespace py = pybind11; |
| 76 |
|
| 77 |
namespace |
| 78 |
{ |
| 79 |
template<typename T> |
| 80 |
void check_indices(const py::array_t<T>& x, std::int64_t local_size) |
| 81 |
{ |
| 82 |
for (std::int64_t i = 0; i < (std::int64_t) x.size(); ++i) |
| 83 |
{ |
| 84 |
std::int64_t _x = *(x.data() + i); |
| 85 |
if (_x < 0 or !(_x < local_size)) |
| 86 |
throw py::index_error("Vector index out of range"); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Linear operator trampoline class |
| 91 |
template<typename LinearOperatorBase> |
| 92 |
class PyLinearOperator : public LinearOperatorBase |
| 93 |
{ |
| 94 |
using LinearOperatorBase::LinearOperatorBase; |
| 95 |
|
| 96 |
// pybdind11 has some issues when passing by reference (due to |
| 97 |
// the return value policy), so the below is non-standard. See |
| 98 |
// https://github.com/pybind/pybind11/issues/250. |
| 99 |
|
| 100 |
std::size_t size(std::size_t dim) const |
| 101 |
{ PYBIND11_OVERLOAD_PURE(std::size_t, LinearOperatorBase, size, ); } |
| 102 |
|
| 103 |
void mult(const dolfin::GenericVector& x, dolfin::GenericVector& y) const |
| 104 |
{ PYBIND11_OVERLOAD_INT(void, LinearOperatorBase, "mult", &x, &y); } |
| 105 |
}; |
| 106 |
|
| 107 |
// Linear operator trampoline class (with pure virtual 'mult' |
| 108 |
// function) |
| 109 |
template<typename LinearOperatorBase> |
| 110 |
class PyLinearOperatorPure : public LinearOperatorBase |
| 111 |
{ |
| 112 |
using LinearOperatorBase::LinearOperatorBase; |
| 113 |
|
| 114 |
std::size_t size(std::size_t dim) const |
| 115 |
{ PYBIND11_OVERLOAD_PURE(std::size_t, LinearOperatorBase, size, ); } |
| 116 |
|
| 117 |
void mult(const dolfin::GenericVector& x, dolfin::GenericVector& y) const |
| 118 |
{ |
| 119 |
PYBIND11_OVERLOAD_INT(void, LinearOperatorBase, "mult", &x, &y); |
| 120 |
py::pybind11_fail("Tried to call pure virtual function \'mult\'"); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
namespace dolfin_wrappers |
| 127 |
{ |
| 128 |
using RowMatrixXd = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; |
| 129 |
|
| 130 |
void la(py::module& m) |
| 131 |
{ |
| 132 |
// dolfin::IndexMap |
| 133 |
py::class_<dolfin::IndexMap, std::shared_ptr<dolfin::IndexMap>> index_map(m, "IndexMap"); |
| 134 |
index_map.def("size", &dolfin::IndexMap::size) |
| 135 |
.def("block_size", &dolfin::IndexMap::block_size) |
| 136 |
.def("local_range", &dolfin::IndexMap::local_range) |
| 137 |
.def("local_to_global_unowned", |
| 138 |
[](dolfin::IndexMap& self) { |
| 139 |
return Eigen::Map<const Eigen::Matrix<std::size_t, Eigen::Dynamic, 1>>( |
| 140 |
self.local_to_global_unowned().data(), |
| 141 |
self.local_to_global_unowned().size()); }, |
| 142 |
py::return_value_policy::reference_internal, |
| 143 |
"Return view into unowned part of local-to-global map"); |
| 144 |
|
| 145 |
// dolfin::IndexMap enums |
| 146 |
py::enum_<dolfin::IndexMap::MapSize>(index_map, "MapSize") |
| 147 |
.value("ALL", dolfin::IndexMap::MapSize::ALL) |
| 148 |
.value("OWNED", dolfin::IndexMap::MapSize::OWNED) |
| 149 |
.value("UNOWNED", dolfin::IndexMap::MapSize::UNOWNED) |
| 150 |
.value("GLOBAL", dolfin::IndexMap::MapSize::GLOBAL); |
| 151 |
|
| 152 |
// dolfin::SparsityPattern |
| 153 |
py::class_<dolfin::SparsityPattern, std::shared_ptr<dolfin::SparsityPattern>>(m, "SparsityPattern") |
| 154 |
.def("init", &dolfin::SparsityPattern::init) |
| 155 |
.def("apply", &dolfin::SparsityPattern::apply) |
| 156 |
.def("str", &dolfin::SparsityPattern::str) |
| 157 |
.def("num_nonzeros", &dolfin::SparsityPattern::num_nonzeros) |
| 158 |
.def("num_nonzeros_diagonal", [](const dolfin::SparsityPattern& instance) |
| 159 |
{ |
| 160 |
std::vector<std::size_t> num_nonzeros; |
| 161 |
instance.num_nonzeros_diagonal(num_nonzeros); |
| 162 |
return py::array_t<std::size_t>(num_nonzeros.size(), num_nonzeros.data()); |
| 163 |
}) |
| 164 |
.def("num_nonzeros_off_diagonal", [](const dolfin::SparsityPattern& instance) |
| 165 |
{ |
| 166 |
std::vector<std::size_t> num_nonzeros; |
| 167 |
instance.num_nonzeros_off_diagonal(num_nonzeros); |
| 168 |
return py::array_t<std::size_t>(num_nonzeros.size(), num_nonzeros.data()); |
| 169 |
}) |
| 170 |
.def("num_local_nonzeros", [](const dolfin::SparsityPattern& instance) |
| 171 |
{ |
| 172 |
std::vector<std::size_t> num_nonzeros; |
| 173 |
instance.num_local_nonzeros(num_nonzeros); |
| 174 |
return py::array_t<std::size_t>(num_nonzeros.size(), num_nonzeros.data()); |
| 175 |
}) |
| 176 |
// FIXME: Switch EigenMap in DOLFIN interface when SWIG is dropped |
| 177 |
.def("insert_local", [](dolfin::SparsityPattern& self, |
| 178 |
std::vector<Eigen::Matrix<dolfin::la_index, Eigen::Dynamic, 1>> entries) |
| 179 |
{ |
| 180 |
std::vector<dolfin::ArrayView<const dolfin::la_index>> e(entries.size()); |
| 181 |
for (std::size_t i = 0; i < entries.size(); ++i) |
| 182 |
e[i] = dolfin::ArrayView<const dolfin::la_index>(entries[i].size(), &entries[i][0]); |
| 183 |
|
| 184 |
self.insert_local(e); |
| 185 |
}) |
| 186 |
.def("insert_global", [](dolfin::SparsityPattern& self, |
| 187 |
std::vector<Eigen::Matrix<dolfin::la_index, Eigen::Dynamic, 1>> entries) |
| 188 |
{ |
| 189 |
std::vector<dolfin::ArrayView<const dolfin::la_index>> e(entries.size()); |
| 190 |
for (std::size_t i = 0; i < entries.size(); ++i) |
| 191 |
e[i] = dolfin::ArrayView<const dolfin::la_index>(entries[i].size(), &entries[i][0]); |
| 192 |
|
| 193 |
self.insert_global(e); |
| 194 |
}) |
| 195 |
.def("insert_local_global", [](dolfin::SparsityPattern& self, |
| 196 |
std::vector<Eigen::Matrix<dolfin::la_index, Eigen::Dynamic, 1>> entries) |
| 197 |
{ |
| 198 |
std::vector<dolfin::ArrayView<const dolfin::la_index>> e(entries.size()); |
| 199 |
for (std::size_t i = 0; i < entries.size(); ++i) |
| 200 |
e[i] = dolfin::ArrayView<const dolfin::la_index>(entries[i].size(), &entries[i][0]); |
| 201 |
|
| 202 |
self.insert_local_global(e); |
| 203 |
}); |
| 204 |
|
| 205 |
// dolfin::TensorLayout |
| 206 |
py::class_<dolfin::TensorLayout, std::shared_ptr<dolfin::TensorLayout>, |
| 207 |
dolfin::Variable> tensor_layout(m, "TensorLayout"); |
| 208 |
|
| 209 |
// dolfin::TensorLayout enums |
| 210 |
py::enum_<dolfin::TensorLayout::Sparsity>(tensor_layout, "Sparsity") |
| 211 |
.value("SPARSE", dolfin::TensorLayout::Sparsity::SPARSE) |
| 212 |
.value("DENSE", dolfin::TensorLayout::Sparsity::DENSE); |
| 213 |
py::enum_<dolfin::TensorLayout::Ghosts>(tensor_layout, "Ghosts") |
| 214 |
.value("GHOSTED", dolfin::TensorLayout::Ghosts::GHOSTED) |
| 215 |
.value("UNGHOSTED", dolfin::TensorLayout::Ghosts::UNGHOSTED); |
| 216 |
|
| 217 |
tensor_layout |
| 218 |
.def(py::init([](const MPICommWrapper comm, std::size_t primary_dim, |
| 219 |
dolfin::TensorLayout::Sparsity sparsity_pattern) |
| 220 |
{ return std::unique_ptr<dolfin::TensorLayout>(new dolfin::TensorLayout(comm.get(), primary_dim, sparsity_pattern)); })) |
| 221 |
.def(py::init([](const MPICommWrapper comm, |
| 222 |
std::vector<std::shared_ptr<const dolfin::IndexMap>> index_maps, |
| 223 |
std::size_t primary_dim, dolfin::TensorLayout::Sparsity sparsity_pattern, |
| 224 |
dolfin::TensorLayout::Ghosts ghosted) |
| 225 |
{ return std::unique_ptr<dolfin::TensorLayout>(new dolfin::TensorLayout(comm.get(), index_maps, primary_dim, |
| 226 |
sparsity_pattern, ghosted)); })) |
| 227 |
.def("init", &dolfin::TensorLayout::init) |
| 228 |
.def("sparsity_pattern", (std::shared_ptr<dolfin::SparsityPattern> (dolfin::TensorLayout::*)()) &dolfin::TensorLayout::sparsity_pattern); |
| 229 |
|
| 230 |
// dolfin::LinearAlgebraObject |
| 231 |
py::class_<dolfin::LinearAlgebraObject, std::shared_ptr<dolfin::LinearAlgebraObject>, |
| 232 |
dolfin::Variable>(m, "LinearAlgebraObject") |
| 233 |
.def("mpi_comm", [](dolfin::LinearAlgebraObject& self) |
| 234 |
{ return MPICommWrapper(self.mpi_comm()); }); |
| 235 |
|
| 236 |
// dolfin::GenericLinearOperator |
| 237 |
py::class_<dolfin::GenericLinearOperator, std::shared_ptr<dolfin::GenericLinearOperator>, |
| 238 |
PyLinearOperatorPure<dolfin::GenericLinearOperator>, dolfin::LinearAlgebraObject> |
| 239 |
(m, "GenericLinearOperator", "GenericLinearOperator object"); |
| 240 |
|
| 241 |
// dolfin::GenericTensor |
| 242 |
py::class_<dolfin::GenericTensor, std::shared_ptr<dolfin::GenericTensor>, |
| 243 |
dolfin::LinearAlgebraObject> |
| 244 |
(m, "GenericTensor", "DOLFIN GenericTensor object") |
| 245 |
.def("init", &dolfin::GenericTensor::init) |
| 246 |
.def("empty", &dolfin::GenericTensor::empty) |
| 247 |
.def("factory", &dolfin::GenericTensor::factory) |
| 248 |
.def("local_range", &dolfin::GenericTensor::local_range) |
| 249 |
.def("rank", &dolfin::GenericTensor::rank) |
| 250 |
.def("size", &dolfin::GenericTensor::size) |
| 251 |
.def("str", &dolfin::GenericTensor::str) |
| 252 |
.def("zero", &dolfin::GenericTensor::zero); |
| 253 |
|
| 254 |
// dolfin::GenericMatrix |
| 255 |
py::class_<dolfin::GenericMatrix, std::shared_ptr<dolfin::GenericMatrix>, |
| 256 |
dolfin::GenericTensor, dolfin::GenericLinearOperator> |
| 257 |
(m, "GenericMatrix", py::dynamic_attr(), "DOLFIN GenericMatrix object") |
| 258 |
.def("init_vector", &dolfin::GenericMatrix::init_vector) |
| 259 |
.def("axpy", &dolfin::GenericMatrix::axpy) |
| 260 |
.def("mult", &dolfin::GenericMatrix::mult) |
| 261 |
.def("transpmult", &dolfin::GenericMatrix::transpmult) |
| 262 |
// __ifoo__ |
| 263 |
.def("__imul__", &dolfin::GenericMatrix::operator*=, "Multiply by a scalar") |
| 264 |
.def("__itruediv__", &dolfin::GenericMatrix::operator/=, py::is_operator(), "Divide by a scalar") |
| 265 |
// Below is an examle of a hand-wrapped in-place operator. Note |
| 266 |
// the explicit return type (const reference). This is necessary |
| 267 |
// to avoid segfaults. Need to investigate more how pybind11 |
| 268 |
// handles return types for operators. |
| 269 |
//.def("__itruediv__", [](dolfin::GenericMatrix& self, double a) -> const dolfin::GenericMatrix& |
| 270 |
// { |
| 271 |
// self /= a; |
| 272 |
// return self; |
| 273 |
// }, py::is_operator(), "Divide by a scalar") |
| 274 |
.def("__iadd__", &dolfin::GenericMatrix::operator+=, py::is_operator(), "Add Matrix") |
| 275 |
.def("__isub__", &dolfin::GenericMatrix::operator-=, py::is_operator(), "Subtract Matrix") |
| 276 |
// __add__ |
| 277 |
.def("__add__", [](const dolfin::GenericMatrix& self, const dolfin::GenericMatrix& B) |
| 278 |
{ auto C = self.copy(); (*C) += B; return C; }, py::is_operator()) |
| 279 |
// __sub__ |
| 280 |
.def("__sub__", [](const dolfin::GenericMatrix& self, const dolfin::GenericMatrix& B) |
| 281 |
{ auto C = self.copy(); (*C) -= B; return C; }, py::is_operator()) |
| 282 |
// __mul__ |
| 283 |
.def("__mul__", [](const dolfin::GenericMatrix& self, double a) |
| 284 |
{ auto B = self.copy(); (*B) *= a; return B; }, py::is_operator()) |
| 285 |
.def("__rmul__", [](const dolfin::GenericMatrix& self, double a) |
| 286 |
{ auto B = self.copy(); (*B) *= a; return B; }, py::is_operator()) |
| 287 |
.def("__mul__", [](const dolfin::GenericMatrix& self, const dolfin::GenericVector& x) |
| 288 |
{ |
| 289 |
auto y = x.factory().create_vector(x.mpi_comm()); |
| 290 |
self.init_vector(*y, 0); |
| 291 |
self.mult(x, *y); |
| 292 |
return y; |
| 293 |
}, py::is_operator()) |
| 294 |
.def("__mul__", [](const dolfin::GenericMatrix& self, const py::array_t<double> x) |
| 295 |
{ |
| 296 |
if (x.ndim() != 1) |
| 297 |
throw py::index_error("NumPy must be a 1D array for multiplication by a GenericMatrix"); |
| 298 |
if ((std::size_t) x.size() != self.size(1)) |
| 299 |
throw py::index_error("Length of array must match number of matrix columns"); |
| 300 |
|
| 301 |
auto _x = self.factory().create_vector(self.mpi_comm()); |
| 302 |
self.init_vector(*_x, 1); |
| 303 |
std::vector<double> values(x.data(), x.data() + x.size()); |
| 304 |
_x->set_local(values); |
| 305 |
_x->apply("insert"); |
| 306 |
|
| 307 |
auto y = self.factory().create_vector(self.mpi_comm()); |
| 308 |
self.init_vector(*y, 0); |
| 309 |
|
| 310 |
self.mult(*_x, *y); |
| 311 |
|
| 312 |
y->get_local(values); |
| 313 |
return py::array_t<double>(values.size(), values.data()); |
| 314 |
}, "Multiply a DOLFIN matrix and a NumPy array (non-distributed matricds only)") |
| 315 |
// __div__ |
| 316 |
.def("__truediv__", [](const dolfin::GenericMatrix& self, double a) |
| 317 |
{ auto B = self.copy(); (*B) /= a; return B; }, py::is_operator()) |
| 318 |
// |
| 319 |
.def("copy", &dolfin::GenericMatrix::copy) |
| 320 |
.def("local_range", &dolfin::GenericMatrix::local_range) |
| 321 |
.def("norm", &dolfin::GenericMatrix::norm) |
| 322 |
.def("nnz", &dolfin::GenericMatrix::nnz) |
| 323 |
.def("size", &dolfin::GenericMatrix::size) |
| 324 |
.def("apply", &dolfin::GenericMatrix::apply) |
| 325 |
.def("get_diagonal", &dolfin::GenericMatrix::get_diagonal) |
| 326 |
.def("set_diagonal", &dolfin::GenericMatrix::set_diagonal) |
| 327 |
.def("ident_zeros", &dolfin::GenericMatrix::ident_zeros, py::arg("tol") = DOLFIN_EPS) |
| 328 |
.def("ident", [](dolfin::GenericMatrix& self, std::vector<dolfin::la_index> rows) |
| 329 |
{ self.ident(rows.size(), rows.data()); }, py::arg("rows")) |
| 330 |
.def("get", [](dolfin::GenericMatrix& self, Eigen::Ref<RowMatrixXd> block, |
| 331 |
const std::vector<dolfin::la_index> rows, |
| 332 |
const std::vector<dolfin::la_index> cols) |
| 333 |
{ |
| 334 |
if ((std::size_t) block.rows() != rows.size()) |
| 335 |
throw py::value_error("Block must have the same number of rows as len(rows)"); |
| 336 |
if ((std::size_t) block.cols() != cols.size()) |
| 337 |
throw py::value_error("Block must have the same number of columns as len(cols)"); |
| 338 |
self.get((double *) block.data(), rows.size(), rows.data(), |
| 339 |
cols.size(), cols.data()); |
| 340 |
}, py::arg("block"), py::arg("rows"), py::arg("cols")) |
| 341 |
.def("set", [](dolfin::GenericMatrix& self, const Eigen::Ref<const RowMatrixXd> block, |
| 342 |
const std::vector<dolfin::la_index> rows, |
| 343 |
const std::vector<dolfin::la_index> cols) |
| 344 |
{ |
| 345 |
if ((std::size_t) block.rows() != rows.size()) |
| 346 |
throw py::value_error("Block must have the same number of rows as len(rows)"); |
| 347 |
if ((std::size_t) block.cols() != cols.size()) |
| 348 |
throw py::value_error("Block must have the same number of columns as len(cols)"); |
| 349 |
self.set((const double *) block.data(), rows.size(), rows.data(), |
| 350 |
cols.size(), cols.data()); |
| 351 |
}, py::arg("block"), py::arg("rows"), py::arg("cols")) |
| 352 |
.def("getrow", [](const dolfin::GenericMatrix& instance, std::size_t row) |
| 353 |
{ |
| 354 |
std::vector<double> values; |
| 355 |
std::vector<std::size_t> columns; |
| 356 |
instance.getrow(row, columns, values); |
| 357 |
auto _columns = py::array_t<std::size_t>(columns.size(), columns.data()); |
| 358 |
auto _values = py::array_t<double>(values.size(), values.data()); |
| 359 |
return std::make_pair(_columns, _values); |
| 360 |
}, py::arg("row")) |
| 361 |
.def("array", [](const dolfin::GenericMatrix& instance) |
| 362 |
{ |
| 363 |
// FIXME: This function is highly dubious. It assumes a |
| 364 |
// particular matrix data layout. |
| 365 |
|
| 366 |
auto m_range = instance.local_range(0); |
| 367 |
std::size_t num_rows = m_range.second - m_range.first; |
| 368 |
std::size_t num_cols = instance.size(1); |
| 369 |
|
| 370 |
typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> EigenMatrix; |
| 371 |
EigenMatrix A = EigenMatrix::Zero(num_rows, num_cols); |
| 372 |
std::vector<std::size_t> columns; |
| 373 |
std::vector<double> values; |
| 374 |
for (std::size_t i = 0; i < num_rows; ++i) |
| 375 |
{ |
| 376 |
const std::size_t row = i + m_range.first; |
| 377 |
instance.getrow(row, columns, values); |
| 378 |
for (std::size_t j = 0; j < columns.size(); ++j) |
| 379 |
A(i, columns[j]) = values[j]; |
| 380 |
} |
| 381 |
|
| 382 |
return A; |
| 383 |
}); |
| 384 |
|
| 385 |
// dolfin::GenericVector |
| 386 |
py::class_<dolfin::GenericVector, std::shared_ptr<dolfin::GenericVector>, |
| 387 |
dolfin::GenericTensor> |
| 388 |
(m, "GenericVector", py::dynamic_attr(), "DOLFIN GenericVector object") |
| 389 |
.def("init", (void (dolfin::GenericVector::*)(std::size_t)) &dolfin::GenericVector::init) |
| 390 |
.def("init", (void (dolfin::GenericVector::*)(const dolfin::TensorLayout&)) &dolfin::GenericVector::init) |
| 391 |
.def("init", (void (dolfin::GenericVector::*)(std::pair<std::size_t, std::size_t>)) &dolfin::GenericVector::init) |
| 392 |
.def("copy", &dolfin::GenericVector::copy) |
| 393 |
// sub |
| 394 |
.def("__isub__", (const dolfin::GenericVector& (dolfin::GenericVector::*)(double)) |
| 395 |
&dolfin::GenericVector::operator-=) |
| 396 |
.def("__isub__", (const dolfin::GenericVector& (dolfin::GenericVector::*)(const dolfin::GenericVector&)) |
| 397 |
&dolfin::GenericVector::operator-=) |
| 398 |
.def("__sub__", [](dolfin::GenericVector& self, double a) |
| 399 |
{ auto u = self.copy(); (*u) -= a; return u; }, py::is_operator()) |
| 400 |
.def("__sub__", [](dolfin::GenericVector& self, const dolfin::GenericVector& v) |
| 401 |
{ auto u = self.copy(); (*u) -= v; return u; }, py::is_operator()) |
| 402 |
.def("__rsub__", [](dolfin::GenericVector& self, double a) |
| 403 |
{ auto u = self.copy(); (*u) *= -1 ; (*u) += a; return u; }, py::is_operator()) |
| 404 |
// div |
| 405 |
.def("__itruediv__", (const dolfin::GenericVector& (dolfin::GenericVector::*)(double)) |
| 406 |
&dolfin::GenericVector::operator/=) |
| 407 |
.def("__truediv__", [](const dolfin::GenericVector& instance, double a) |
| 408 |
{ auto x = instance.copy(); *x /= a; return x; }) |
| 409 |
// add |
| 410 |
.def("__iadd__", (const dolfin::GenericVector& (dolfin::GenericVector::*)(double)) |
| 411 |
&dolfin::GenericVector::operator+=) |
| 412 |
.def("__iadd__", (const dolfin::GenericVector& (dolfin::GenericVector::*)(const dolfin::GenericVector&)) |
| 413 |
&dolfin::GenericVector::operator+=) |
| 414 |
.def("__add__", [](const dolfin::GenericVector& self, double a) |
| 415 |
{ auto x = self.copy(); *x += a; return x;} ) |
| 416 |
.def("__add__", [](const dolfin::GenericVector& self, const dolfin::GenericVector& x) |
| 417 |
{ auto y = self.copy(); *y += x; return y;} ) |
| 418 |
.def("__radd__", [](const dolfin::GenericVector& self, double a) |
| 419 |
{ auto x = self.copy(); *x += a; return x;} ) |
| 420 |
// mult |
| 421 |
.def("__imul__", (const dolfin::GenericVector& (dolfin::GenericVector::*)(double)) |
| 422 |
&dolfin::GenericVector::operator*=) |
| 423 |
.def("__imul__", (const dolfin::GenericVector& (dolfin::GenericVector::*)(const dolfin::GenericVector&)) |
| 424 |
&dolfin::GenericVector::operator*=) |
| 425 |
.def("__mul__", [](dolfin::GenericVector& v, double a) |
| 426 |
{ auto u = v.copy(); *u *= a; return u; }) |
| 427 |
.def("__mul__", [](const dolfin::GenericVector& v, const dolfin::GenericVector& u) |
| 428 |
{ auto w = v.copy(); (*w) *= u; return w; }, "Component-wise multiplication of two vectors") |
| 429 |
.def("__rmul__", [](dolfin::GenericVector& v, double a) |
| 430 |
{ auto u = v.copy(); *u *= a; return u; }) |
| 431 |
// __getitem___ |
| 432 |
.def("__getitem__", [](dolfin::GenericVector& self, py::slice slice) |
| 433 |
{ |
| 434 |
std::size_t start, stop, step, slicelength; |
| 435 |
if (!slice.compute(self.local_size(), &start, &stop, &step, &slicelength)) |
| 436 |
throw py::error_already_set(); |
| 437 |
|
| 438 |
std::vector<double> values(slicelength); |
| 439 |
if (start != 0 or stop != self.local_size() or step != 1) |
| 440 |
{ |
| 441 |
std::vector<dolfin::la_index> indices(slicelength, start); |
| 442 |
for (size_t i = 1; i < slicelength; ++i) |
| 443 |
indices[i] = indices[i-1] + step; |
| 444 |
self.get_local(values.data(), values.size(), indices.data()); |
| 445 |
} |
| 446 |
else |
| 447 |
self.get_local(values); |
| 448 |
|
| 449 |
return py::array_t<double>(values.size(), values.data()); |
| 450 |
}) |
| 451 |
.def("__getitem__", [](const dolfin::GenericVector& self, py::array_t<bool> indices) |
| 452 |
{ |
| 453 |
if (indices.ndim() != 1) |
| 454 |
throw py::index_error("Indices must be a 1D array"); |
| 455 |
if ((std::size_t) indices.size() != self.local_size()) |
| 456 |
throw py::index_error("Indices size mismatch"); |
| 457 |
check_indices(indices, self.local_size()); |
| 458 |
|
| 459 |
// Get the values |
| 460 |
std::vector<double> values; |
| 461 |
self.get_local(values); |
| 462 |
|
| 463 |
// Extract filtered values |
| 464 |
std::vector<double> filtered; |
| 465 |
for (std::size_t i = 0; i < (std::size_t) indices.size(); ++i) |
| 466 |
{ |
| 467 |
bool e = *(indices.data() + i); |
| 468 |
if (e) |
| 469 |
filtered.push_back(values[i]); |
| 470 |
} |
| 471 |
|
| 472 |
return py::array_t<double>(filtered.size(), filtered.data()); |
| 473 |
}, py::arg().noconvert()) // Use noconvert to avoid integers being converted to bool |
| 474 |
.def("__getitem__", [](dolfin::GenericVector& self, double index) |
| 475 |
{ throw py::type_error("Cannot use float for GenericVector indexing with floats"); }, py::arg().noconvert()) |
| 476 |
.def("__getitem__", [](dolfin::GenericVector& self, py::array_t<dolfin::la_index> indices) |
| 477 |
{ |
| 478 |
if (indices.ndim() > 1) |
| 479 |
throw py::index_error("Indices must be a 1D array"); |
| 480 |
check_indices(indices, self.local_size()); |
| 481 |
|
| 482 |
py::array_t<double> values(indices.size()); |
| 483 |
self.get_local(values.mutable_data(), values.size(), indices.data()); |
| 484 |
return values; |
| 485 |
}) |
| 486 |
.def("__getitem__", [](dolfin::GenericVector& self, dolfin::la_index index) |
| 487 |
{ |
| 488 |
if (self.local_size() == 0) |
| 489 |
throw py::index_error("GenericVector has zero (local) length. Cannot index into it."); |
| 490 |
else if (index < 0) |
| 491 |
throw py::index_error("Index is negative"); |
| 492 |
else if (!(index < (dolfin::la_index) self.local_size())) |
| 493 |
throw py::index_error("Index exceeds (local) size of GenericVector"); |
| 494 |
return self.getitem(index); |
| 495 |
}) |
| 496 |
// __setitem__ |
| 497 |
.def("__setitem__", [](dolfin::GenericVector& self, py::slice slice, double value) |
| 498 |
{ |
| 499 |
std::size_t start, stop, step, slicelength; |
| 500 |
if (!slice.compute(self.size(), &start, &stop, &step, &slicelength)) |
| 501 |
throw py::error_already_set(); |
| 502 |
if (start != 0 or stop != self.size() or step != 1) |
| 503 |
throw std::range_error("Only setting full slices for GenericVector is supported"); |
| 504 |
|
| 505 |
self = value; |
| 506 |
}) |
| 507 |
.def("__setitem__", [](dolfin::GenericVector& self, py::slice slice, const dolfin::GenericVector& x) |
| 508 |
{ |
| 509 |
std::size_t start, stop, step, slicelength; |
| 510 |
if (!slice.compute(self.size(), &start, &stop, &step, &slicelength)) |
| 511 |
throw py::error_already_set(); |
| 512 |
if (start != 0 or stop != self.size() or step != 1) |
| 513 |
throw std::range_error("Only setting full slices for GenericVector is supported"); |
| 514 |
self = x; |
| 515 |
}) |
| 516 |
.def("__setitem__", [](dolfin::GenericVector& self, py::slice slice, const py::array_t<double> x) |
| 517 |
{ |
| 518 |
if (x.ndim() != 1) |
| 519 |
throw py::index_error("Values to set must be a 1D array"); |
| 520 |
|
| 521 |
std::size_t start, stop, step, slicelength; |
| 522 |
if (!slice.compute(self.size(), &start, &stop, &step, &slicelength)) |
| 523 |
throw py::error_already_set(); |
| 524 |
if (start != 0 or stop != self.size() or step != 1) |
| 525 |
throw std::range_error("Only full slices are supported"); |
| 526 |
|
| 527 |
std::vector<double> values(x.data(), x.data() + x.size()); |
| 528 |
if (!values.empty()) |
| 529 |
{ |
| 530 |
self.set_local(values); |
| 531 |
self.apply("insert"); |
| 532 |
} |
| 533 |
}) |
| 534 |
.def("__setitem__", [](dolfin::GenericVector& self, const py::array_t<dolfin::la_index> indices, double x) |
| 535 |
{ |
| 536 |
if (indices.ndim() > 1) |
| 537 |
throw py::index_error("Indices to set must be a 1D array"); |
| 538 |
check_indices(indices, self.local_size()); |
| 539 |
|
| 540 |
// FIXME: combine with py::array_t<double> x version? |
| 541 |
std::vector<double> _x(indices.size(), x); |
| 542 |
self.set_local(_x.data(), _x.size(), indices.data()); |
| 543 |
self.apply("insert"); |
| 544 |
}) |
| 545 |
.def("__setitem__", [](dolfin::GenericVector& self, const py::array_t<dolfin::la_index> indices, |
| 546 |
const py::array_t<double> x) |
| 547 |
{ |
| 548 |
if (indices.ndim() != 1) |
| 549 |
throw py::index_error("Indices to set must be a 1D array"); |
| 550 |
if (x.ndim() != 1) |
| 551 |
throw py::index_error("Values to set must be a 1D array"); |
| 552 |
if (indices.shape(0) != x.shape(0)) |
| 553 |
throw py::index_error("Index mismatch"); |
| 554 |
check_indices(indices, self.local_size()); |
| 555 |
|
| 556 |
// FIXME: check sizes |
| 557 |
self.set_local(x.data(), x.size(), indices.data()); |
| 558 |
self.apply("insert"); |
| 559 |
}) |
| 560 |
.def("__len__", [](dolfin::GenericVector& self) { return self.local_size(); }) |
| 561 |
.def("size", (std::size_t (dolfin::GenericVector::*)() const) &dolfin::GenericVector::size) |
| 562 |
// |
| 563 |
.def("get_local", [](const dolfin::GenericVector& instance, |
| 564 |
const std::vector<dolfin::la_index>& rows) |
| 565 |
{ |
| 566 |
py::array_t<double> data(rows.size()); |
| 567 |
instance.get_local(data.mutable_data(), rows.size(), rows.data()); |
| 568 |
return data; |
| 569 |
}) |
| 570 |
.def("get_local", [](const dolfin::GenericVector& instance) |
| 571 |
{ |
| 572 |
std::vector<double> values; |
| 573 |
instance.get_local(values); |
| 574 |
return py::array_t<double>(values.size(), values.data()); |
| 575 |
}) |
| 576 |
.def("set_local", [](dolfin::GenericVector& instance, std::vector<double> values) |
| 577 |
{ |
| 578 |
std::vector<dolfin::la_index> indices(values.size()); |
| 579 |
std::iota(indices.begin(), indices.end(), 0); |
| 580 |
instance.set_local(values.data(), values.size(), indices.data()); |
| 581 |
}) |
| 582 |
.def("add_local", [](dolfin::GenericVector& self, py::array_t<double> values) |
| 583 |
{ |
| 584 |
assert(values.ndim() == 1); |
| 585 |
dolfin::Array<double> _values(values.size(), values.mutable_data()); |
| 586 |
self.add_local(_values); |
| 587 |
}) |
| 588 |
.def("gather", [](const dolfin::GenericVector& instance, dolfin::GenericVector& y, |
| 589 |
const std::vector<dolfin::la_index>& rows) |
| 590 |
{ instance.gather(y, rows); }) |
| 591 |
.def("gather", [](const dolfin::GenericVector& instance, py::array_t<dolfin::la_index> rows) |
| 592 |
{ |
| 593 |
std::vector<dolfin::la_index> _rows(rows.data(), rows.data() + rows.size()); |
| 594 |
std::vector<double> values(rows.size()); |
| 595 |
instance.gather(values, _rows); |
| 596 |
return py::array_t<double>(values.size(), values.data()); |
| 597 |
}) |
| 598 |
.def("gather", [](const dolfin::GenericVector& instance, std::vector<dolfin::la_index> rows) |
| 599 |
{ |
| 600 |
std::vector<double> values(rows.size()); |
| 601 |
instance.gather(values, rows); |
| 602 |
return py::array_t<double>(values.size(), values.data()); |
| 603 |
}) |
| 604 |
.def("gather_on_zero", [](const dolfin::GenericVector& instance) |
| 605 |
{ |
| 606 |
std::vector<double> values; |
| 607 |
instance.gather_on_zero(values); |
| 608 |
return py::array_t<double>(values.size(), values.data()); |
| 609 |
}) |
| 610 |
.def("axpy", &dolfin::GenericVector::axpy) |
| 611 |
.def("sum", (double (dolfin::GenericVector::*)() const) &dolfin::GenericVector::sum) |
| 612 |
.def("sum", [](const dolfin::GenericVector& self, py::array_t<std::size_t> rows) |
| 613 |
{ const dolfin::Array<std::size_t> _rows(rows.size(), rows.mutable_data()); return self.sum(_rows); }) |
| 614 |
.def("max", (double (dolfin::GenericVector::*)() const) &dolfin::GenericVector::max) |
| 615 |
.def("min", (double (dolfin::GenericVector::*)() const) &dolfin::GenericVector::min) |
| 616 |
.def("inner", &dolfin::GenericVector::inner) |
| 617 |
.def("norm", &dolfin::GenericVector::norm) |
| 618 |
.def("local_size", &dolfin::GenericVector::local_size) |
| 619 |
.def("local_range", (std::pair<std::int64_t, std::int64_t> (dolfin::GenericVector::*)() const) &dolfin::GenericVector::local_range) |
| 620 |
.def("owns_index", &dolfin::GenericVector::owns_index) |
| 621 |
.def("apply", &dolfin::GenericVector::apply) |
| 622 |
.def_property_readonly("__array_priority__", [](const dolfin::GenericVector& self){ return 0; }); |
| 623 |
|
| 624 |
// dolfin::Matrix |
| 625 |
py::class_<dolfin::Matrix, std::shared_ptr<dolfin::Matrix>, dolfin::GenericMatrix> |
| 626 |
(m, "Matrix", "DOLFIN Matrix object") |
| 627 |
.def(py::init<>()) |
| 628 |
.def(py::init<const dolfin::Matrix&>()) // Remove? (use copy instead) |
| 629 |
.def(py::init<const dolfin::GenericMatrix&>()) // Remove? (use copy instead) |
| 630 |
.def(py::init([](const MPICommWrapper comm) |
| 631 |
{ return std::unique_ptr<dolfin::Matrix>(new dolfin::Matrix(comm.get())); })) |
| 632 |
// Enabling the below messes up the operators because pybind11 |
| 633 |
// then fails to try the GenericMatrix __mul__ operators |
| 634 |
/* |
| 635 |
.def("__mul__", [](const dolfin::Matrix& self, const dolfin::GenericVector& x) |
| 636 |
{ |
| 637 |
// Specialised verion in place of GenericMatrix.__mul__ |
| 638 |
// becuase this guarantees that a dolfin::Vector is |
| 639 |
// return rather than a more concrete vector. Maybe not |
| 640 |
// important, but some tests check the type. |
| 641 |
dolfin::Vector y(x.mpi_comm()); |
| 642 |
self.init_vector(y, 0); |
| 643 |
self.mult(x, y); |
| 644 |
return y; |
| 645 |
}, py::is_operator(),py::arg().noconvert()) |
| 646 |
*/ |
| 647 |
.def("instance", (std::shared_ptr<dolfin::LinearAlgebraObject>(dolfin::Matrix::*)()) |
| 648 |
&dolfin::Matrix::shared_instance); |
| 649 |
|
| 650 |
// dolfin::Vector |
| 651 |
py::class_<dolfin::Vector, std::shared_ptr<dolfin::Vector>, dolfin::GenericVector> |
| 652 |
(m, "Vector", "DOLFIN Vector object") |
| 653 |
.def(py::init<>()) |
| 654 |
.def(py::init<const dolfin::Vector&>()) |
| 655 |
.def(py::init<const dolfin::GenericVector&>()) |
| 656 |
.def(py::init([](const MPICommWrapper comm) |
| 657 |
{ return std::unique_ptr<dolfin::Vector>(new dolfin::Vector(comm.get())); })) |
| 658 |
.def(py::init([](const MPICommWrapper comm, std::size_t N) |
| 659 |
{ return std::unique_ptr<dolfin::Vector>(new dolfin::Vector(comm.get(), N)); })) |
| 660 |
.def("min", &dolfin::Vector::min) |
| 661 |
.def("max", &dolfin::Vector::max) |
| 662 |
.def("abs", &dolfin::Vector::abs) |
| 663 |
.def("norm", &dolfin::Vector::norm) |
| 664 |
.def("inner", &dolfin::Vector::inner) |
| 665 |
.def("axpy", &dolfin::Vector::axpy) |
| 666 |
.def("zero", &dolfin::Vector::zero) |
| 667 |
.def("apply", &dolfin::Vector::apply) |
| 668 |
.def("str", &dolfin::Vector::str) |
| 669 |
.def("instance", (std::shared_ptr<dolfin::LinearAlgebraObject>(dolfin::Vector::*)()) |
| 670 |
&dolfin::Vector::shared_instance); |
| 671 |
|
| 672 |
// dolfin::BlockMatrix |
| 673 |
py::class_<dolfin::BlockMatrix, std::shared_ptr<dolfin::BlockMatrix>> |
| 674 |
(m, "BlockMatrix") |
| 675 |
.def(py::init<std::size_t, std::size_t>(), py::arg("m")=0, py::arg("n")=0) |
| 676 |
.def("__getitem__", [](dolfin::BlockMatrix& self, py::tuple index) |
| 677 |
{ |
| 678 |
if (index.size() != 2) |
| 679 |
throw py::key_error("Wrong number of indices"); |
| 680 |
std::size_t i = index[0].cast<std::size_t>(); |
| 681 |
std::size_t j = index[1].cast<std::size_t>(); |
| 682 |
return self.get_block(i, j); |
| 683 |
}) |
| 684 |
.def("__setitem__", [](dolfin::BlockMatrix& self, py::tuple index, |
| 685 |
std::shared_ptr<dolfin::GenericMatrix> m) |
| 686 |
{ |
| 687 |
if (index.size() != 2) |
| 688 |
throw py::key_error("Wrong number of indices"); |
| 689 |
std::size_t i = index[0].cast<std::size_t>(); |
| 690 |
std::size_t j = index[1].cast<std::size_t>(); |
| 691 |
self.set_block(i, j, m); |
| 692 |
}) |
| 693 |
.def("mult", &dolfin::BlockMatrix::mult, py::arg("x"), py::arg("y"), py::arg("transposed")=false); |
| 694 |
|
| 695 |
// dolfin::BlockVector |
| 696 |
py::class_<dolfin::BlockVector, std::shared_ptr<dolfin::BlockVector>> |
| 697 |
(m, "BlockVector") |
| 698 |
.def(py::init<std::size_t>()) |
| 699 |
.def("__getitem__", [](dolfin::BlockVector& self, std::size_t index) |
| 700 |
{ return self.get_block(index); }) |
| 701 |
.def("__setitem__", [](dolfin::BlockVector& self, std::size_t index, |
| 702 |
std::shared_ptr<dolfin::GenericVector> v) |
| 703 |
{ self.set_block(index, v); }); |
| 704 |
|
| 705 |
// dolfin::Scalar |
| 706 |
py::class_<dolfin::Scalar, std::shared_ptr<dolfin::Scalar>, dolfin::GenericTensor> |
| 707 |
(m, "Scalar") |
| 708 |
.def(py::init<>()) |
| 709 |
.def(py::init([](const MPICommWrapper comm) |
| 710 |
{ return std::unique_ptr<dolfin::Scalar>(new dolfin::Scalar(comm.get())); })) |
| 711 |
.def("add_local_value", &dolfin::Scalar::add_local_value) |
| 712 |
.def("apply", &dolfin::Scalar::apply) |
| 713 |
.def("mpi_comm", [](dolfin::Scalar& self) |
| 714 |
{ return MPICommWrapper(self.mpi_comm()); }) |
| 715 |
.def("get_scalar_value", &dolfin::Scalar::get_scalar_value); |
| 716 |
|
| 717 |
// dolfin::LinearOperator |
| 718 |
py::class_<dolfin::LinearOperator, std::shared_ptr<dolfin::LinearOperator>, |
| 719 |
PyLinearOperatorPure<dolfin::LinearOperator>, dolfin::GenericLinearOperator> |
| 720 |
(m, "LinearOperator") |
| 721 |
.def(py::init<const dolfin::GenericVector&, const dolfin::GenericVector&>()) |
| 722 |
.def("instance", (std::shared_ptr<dolfin::LinearAlgebraObject>(dolfin::LinearOperator::*)()) |
| 723 |
&dolfin::LinearOperator::shared_instance); |
| 724 |
|
| 725 |
// dolfin::GenericLinearAlgebraFactory |
| 726 |
py::class_<dolfin::GenericLinearAlgebraFactory, std::shared_ptr<dolfin::GenericLinearAlgebraFactory>> |
| 727 |
(m, "GenericLinearAlgebraFactory", "DOLFIN GenericLinearAlgebraFactory object"); |
| 728 |
|
| 729 |
// dolfin::DefaultFactory |
| 730 |
py::class_<dolfin::DefaultFactory, std::shared_ptr<dolfin::DefaultFactory>> |
| 731 |
(m, "DefaultFactory", "DOLFIN DefaultFactory object") |
| 732 |
.def(py::init<>()) |
| 733 |
.def_static("factory", &dolfin::DefaultFactory::factory) |
| 734 |
.def("create_matrix", [](const dolfin::DefaultFactory &self, const MPICommWrapper comm) |
| 735 |
{ return self.create_matrix(comm.get()); }) |
| 736 |
.def("create_vector", [](const dolfin::DefaultFactory &self, const MPICommWrapper comm) |
| 737 |
{ return self.create_vector(comm.get()); }); |
| 738 |
|
| 739 |
// dolfin::EigenFactory |
| 740 |
py::class_<dolfin::EigenFactory, std::shared_ptr<dolfin::EigenFactory>, |
| 741 |
dolfin::GenericLinearAlgebraFactory> |
| 742 |
(m, "EigenFactory", "DOLFIN EigenFactory object") |
| 743 |
.def("instance", &dolfin::EigenFactory::instance) |
| 744 |
.def("create_matrix", [](const dolfin::EigenFactory &self, const MPICommWrapper comm) |
| 745 |
{ return self.create_matrix(comm.get()); }) |
| 746 |
.def("create_vector", [](const dolfin::EigenFactory &self, const MPICommWrapper comm) |
| 747 |
{ return self.create_vector(comm.get()); }); |
| 748 |
|
| 749 |
// dolfin::EigenVector |
| 750 |
py::class_<dolfin::EigenVector, std::shared_ptr<dolfin::EigenVector>, |
| 751 |
dolfin::GenericVector> |
| 752 |
(m, "EigenVector", "DOLFIN EigenVector object") |
| 753 |
.def(py::init<>()) |
| 754 |
.def(py::init([](const MPICommWrapper comm) |
| 755 |
{ return std::unique_ptr<dolfin::EigenVector>(new dolfin::EigenVector(comm.get())); })) |
| 756 |
.def(py::init([](const MPICommWrapper comm, std::size_t N) |
| 757 |
{ return std::unique_ptr<dolfin::EigenVector>(new dolfin::EigenVector(comm.get(), N)); })) |
| 758 |
.def("array_view", [](dolfin::EigenVector& self) -> Eigen::Ref<Eigen::VectorXd> { return *self.vec(); }, |
| 759 |
"Return a writable numpy array view of the data in the EigenVector"); |
| 760 |
|
| 761 |
// dolfin::EigenMatrix |
| 762 |
py::class_<dolfin::EigenMatrix, std::shared_ptr<dolfin::EigenMatrix>, |
| 763 |
dolfin::GenericMatrix> |
| 764 |
(m, "EigenMatrix", "DOLFIN EigenMatrix object") |
| 765 |
.def(py::init<>()) |
| 766 |
.def(py::init<std::size_t, std::size_t>()) |
| 767 |
.def("sparray", (dolfin::EigenMatrix::eigen_matrix_type& (dolfin::EigenMatrix::*)()) &dolfin::EigenMatrix::mat, |
| 768 |
py::return_value_policy::reference_internal) |
| 769 |
.def("data_view", [](dolfin::EigenMatrix& instance) |
| 770 |
{ |
| 771 |
auto _data = instance.data(); |
| 772 |
std::size_t nnz = std::get<3>(_data); |
| 773 |
|
| 774 |
Eigen::Map<const Eigen::VectorXi> rows(std::get<0>(_data), instance.size(0) + 1); |
| 775 |
Eigen::Map<const Eigen::VectorXi> cols(std::get<1>(_data), nnz); |
| 776 |
Eigen::Map<const Eigen::VectorXd> values(std::get<2>(_data), nnz); |
| 777 |
|
| 778 |
return py::make_tuple(rows, cols, values); |
| 779 |
}, |
| 780 |
py::return_value_policy::reference_internal, "Return CSR matrix data as NumPy arrays (shared data)") |
| 781 |
.def("data", [](dolfin::EigenMatrix& instance) |
| 782 |
{ |
| 783 |
auto _data = instance.data(); |
| 784 |
std::size_t nnz = std::get<3>(_data); |
| 785 |
|
| 786 |
Eigen::VectorXi rows = Eigen::Map<const Eigen::VectorXi>(std::get<0>(_data), instance.size(0) + 1); |
| 787 |
Eigen::VectorXi cols = Eigen::Map<const Eigen::VectorXi>(std::get<1>(_data), nnz); |
| 788 |
Eigen::VectorXd values = Eigen::Map<const Eigen::VectorXd>(std::get<2>(_data), nnz); |
| 789 |
|
| 790 |
return py::make_tuple(rows, cols, values); |
| 791 |
}, |
| 792 |
py::return_value_policy::copy, "Return copy of CSR matrix data as NumPy arrays"); |
| 793 |
|
| 794 |
// dolfin::GenericLinearSolver |
| 795 |
py::class_<dolfin::GenericLinearSolver, std::shared_ptr<dolfin::GenericLinearSolver>, |
| 796 |
dolfin::Variable> |
| 797 |
(m, "GenericLinearSolver", "DOLFIN GenericLinearSolver object"); |
| 798 |
|
| 799 |
#ifdef HAS_PETSC |
| 800 |
py::class_<dolfin::PETScOptions>(m, "PETScOptions") |
| 801 |
.def_static("set", (void (*)(std::string)) &dolfin::PETScOptions::set) |
| 802 |
.def_static("set", (void (*)(std::string, bool)) &dolfin::PETScOptions::set) |
| 803 |
.def_static("set", (void (*)(std::string, int)) &dolfin::PETScOptions::set) |
| 804 |
.def_static("set", (void (*)(std::string, double)) &dolfin::PETScOptions::set) |
| 805 |
.def_static("set", (void (*)(std::string, std::string)) &dolfin::PETScOptions::set) |
| 806 |
.def_static("clear", (void (*)(std::string)) &dolfin::PETScOptions::clear) |
| 807 |
.def_static("clear", (void (*)()) &dolfin::PETScOptions::clear); |
| 808 |
|
| 809 |
// dolfin::PETScObject |
| 810 |
py::class_<dolfin::PETScObject, std::shared_ptr<dolfin::PETScObject>>(m, "PETScObject"); |
| 811 |
|
| 812 |
// dolfin::PETScFactory |
| 813 |
py::class_<dolfin::PETScFactory, std::shared_ptr<dolfin::PETScFactory>, |
| 814 |
dolfin::GenericLinearAlgebraFactory> |
| 815 |
(m, "PETScFactory", "DOLFIN PETScFactory object") |
| 816 |
.def("instance", &dolfin::PETScFactory::instance) |
| 817 |
.def("create_matrix", [](const dolfin::PETScFactory &self, const MPICommWrapper comm) |
| 818 |
{ return self.create_matrix(comm.get()); }) |
| 819 |
.def("create_vector", [](const dolfin::PETScFactory &self, const MPICommWrapper comm) |
| 820 |
{ return self.create_vector(comm.get()); }); |
| 821 |
|
| 822 |
// dolfin::PETScVector |
| 823 |
py::class_<dolfin::PETScVector, std::shared_ptr<dolfin::PETScVector>, |
| 824 |
dolfin::GenericVector, dolfin::PETScObject> |
| 825 |
(m, "PETScVector", "DOLFIN PETScVector object") |
| 826 |
.def(py::init<>()) |
| 827 |
.def(py::init([](const MPICommWrapper comm) |
| 828 |
{ return std::unique_ptr<dolfin::PETScVector>(new dolfin::PETScVector(comm.get())); })) |
| 829 |
.def(py::init([](const MPICommWrapper comm, std::size_t N) |
| 830 |
{ return std::unique_ptr<dolfin::PETScVector>(new dolfin::PETScVector(comm.get(), N)); })) |
| 831 |
.def(py::init<Vec>()) |
| 832 |
.def("get_options_prefix", &dolfin::PETScVector::get_options_prefix) |
| 833 |
.def("set_options_prefix", &dolfin::PETScVector::set_options_prefix) |
| 834 |
.def("update_ghost_values", &dolfin::PETScVector::update_ghost_values) |
| 835 |
.def("vec", &dolfin::PETScVector::vec, "Return underlying PETSc Vec object"); |
| 836 |
|
| 837 |
// dolfin::PETScBaseMatrix |
| 838 |
py::class_<dolfin::PETScBaseMatrix, std::shared_ptr<dolfin::PETScBaseMatrix>, |
| 839 |
dolfin::PETScObject, dolfin::Variable>(m, "PETScBaseMatrix") |
| 840 |
.def("size", (std::size_t (dolfin::PETScBaseMatrix::*)(std::size_t) const) &dolfin::PETScBaseMatrix::size) |
| 841 |
.def("mat", &dolfin::PETScBaseMatrix::mat, "Return underlying PETSc Mat object"); |
| 842 |
|
| 843 |
// dolfin::PETScLinearOperator |
| 844 |
py::class_<dolfin::PETScLinearOperator, std::shared_ptr<dolfin::PETScLinearOperator>, |
| 845 |
PyLinearOperator<dolfin::PETScLinearOperator>, dolfin::PETScBaseMatrix, |
| 846 |
dolfin::GenericLinearOperator> |
| 847 |
(m, "PETScLinearOperator", "PETScLinearOperator object") |
| 848 |
.def(py::init([](const MPICommWrapper comm) |
| 849 |
{ return std::unique_ptr<dolfin::PETScLinearOperator>(new dolfin::PETScLinearOperator(comm.get())); })) |
| 850 |
.def("size", &dolfin::PETScLinearOperator::size) |
| 851 |
.def("mult", &dolfin::PETScLinearOperator::mult) |
| 852 |
.def("mpi_comm", [](dolfin::PETScLinearOperator& self) |
| 853 |
{ return MPICommWrapper(self.mpi_comm()); }); |
| 854 |
|
| 855 |
// dolfin::PETScMatrix |
| 856 |
py::class_<dolfin::PETScMatrix, std::shared_ptr<dolfin::PETScMatrix>, |
| 857 |
dolfin::GenericMatrix, dolfin::PETScBaseMatrix> |
| 858 |
(m, "PETScMatrix", "DOLFIN PETScMatrix object") |
| 859 |
.def(py::init<>()) |
| 860 |
.def(py::init([](const MPICommWrapper comm) |
| 861 |
{ return std::unique_ptr<dolfin::PETScMatrix>(new dolfin::PETScMatrix(comm.get())); })) |
| 862 |
.def(py::init<Mat>()) |
| 863 |
.def("get_options_prefix", &dolfin::PETScMatrix::get_options_prefix) |
| 864 |
.def("set_options_prefix", &dolfin::PETScMatrix::set_options_prefix) |
| 865 |
.def("set_nullspace", &dolfin::PETScMatrix::set_nullspace) |
| 866 |
.def("set_near_nullspace", &dolfin::PETScMatrix::set_near_nullspace) |
| 867 |
.def("setrow", &dolfin::PETScMatrix::setrow); |
| 868 |
|
| 869 |
// dolfin::PETScNestMatrix |
| 870 |
py::class_<dolfin::PETScNestMatrix, std::shared_ptr<dolfin::PETScNestMatrix>, |
| 871 |
dolfin::PETScMatrix> |
| 872 |
(m, "PETScNestMatrix", "DOLFIN PETScMatrix object") |
| 873 |
.def(py::init<>()) |
| 874 |
//.def(py::init<std::vector<std::shared_ptr<dolfin::GenericMatrix>>>() ) |
| 875 |
// explicit wrapper |
| 876 |
.def(py::init([](py::list mats){ |
| 877 |
std::vector<std::shared_ptr<dolfin::GenericMatrix>> _mats; |
| 878 |
for (auto mat : mats){ |
| 879 |
// python object without cpp attribute. |
| 880 |
auto _mat = mat.cast<std::shared_ptr<dolfin::GenericMatrix>>(); |
| 881 |
if(!mat.is_none()) |
| 882 |
_mats.push_back(_mat); |
| 883 |
else |
| 884 |
_mats.push_back(NULL); |
| 885 |
} |
| 886 |
return dolfin::PETScNestMatrix(_mats); |
| 887 |
})) |
| 888 |
.def("init_vectors",[](const dolfin::PETScNestMatrix self, dolfin::GenericVector& vec_out, py::list vecs){ |
| 889 |
std::vector<std::shared_ptr<dolfin::GenericVector>> _vecs; |
| 890 |
for (auto vec : vecs) |
| 891 |
{ |
| 892 |
auto _vec = vec.cast<std::shared_ptr<dolfin::GenericVector>>(); |
| 893 |
_vecs.push_back(_vec); |
| 894 |
} |
| 895 |
self.init_vectors(vec_out,_vecs); |
| 896 |
}) |
| 897 |
|
| 898 |
.def("mult", &dolfin::PETScNestMatrix::mult) |
| 899 |
//.def("init_vectors",&dolfin::PETScNestMatrix::init_vectors) |
| 900 |
//.def("get_block_dofs",&dolfin::PETScNestMatrix::get_block_dofs) |
| 901 |
.def("get_block_dofs",[](const dolfin::PETScNestMatrix self, std::size_t idx) |
| 902 |
{ |
| 903 |
std::vector<dolfin::la_index> dofs; |
| 904 |
self.get_block_dofs(dofs, idx); |
| 905 |
return py::array_t<dolfin::la_index>(dofs.size(), dofs.data()); |
| 906 |
}, py::arg("idx")) |
| 907 |
|
| 908 |
.def("convert_to_aij", &dolfin::PETScNestMatrix::convert_to_aij) |
| 909 |
.def("size", (std::size_t (dolfin::PETScBaseMatrix::*)(std::size_t) const) &dolfin::PETScNestMatrix::size) |
| 910 |
.def("str", &dolfin::PETScNestMatrix::str); |
| 911 |
|
| 912 |
|
| 913 |
py::class_<dolfin::PETScPreconditioner, std::shared_ptr<dolfin::PETScPreconditioner>, |
| 914 |
dolfin::Variable> |
| 915 |
(m, "PETScPreconditioner", "DOLFIN PETScPreconditioner object") |
| 916 |
.def(py::init<std::string>(), py::arg("type")="default") |
| 917 |
.def("preconditioners", &dolfin::PETScPreconditioner::preconditioners) |
| 918 |
.def("set_fieldsplit", (void (*)(dolfin::PETScKrylovSolver&, const std::vector<std::vector<dolfin::la_index>>&, const std::vector<std::string>&)) |
| 919 |
&dolfin::PETScPreconditioner::set_fieldsplit); |
| 920 |
#endif |
| 921 |
|
| 922 |
#ifdef HAS_TRILINOS |
| 923 |
|
| 924 |
// dolfin::TpetraFactory |
| 925 |
py::class_<dolfin::TpetraFactory, std::shared_ptr<dolfin::TpetraFactory>, |
| 926 |
dolfin::GenericLinearAlgebraFactory> |
| 927 |
(m, "TpetraFactory", "DOLFIN TpetraFactory object") |
| 928 |
.def("instance", &dolfin::TpetraFactory::instance) |
| 929 |
.def("create_matrix", [](const dolfin::TpetraFactory &self, const MPICommWrapper comm) |
| 930 |
{ return self.create_matrix(comm.get()); }) |
| 931 |
.def("create_vector", [](const dolfin::TpetraFactory &self, const MPICommWrapper comm) |
| 932 |
{ return self.create_vector(comm.get()); }); |
| 933 |
|
| 934 |
// dolfin::TpetraVector |
| 935 |
py::class_<dolfin::TpetraVector, std::shared_ptr<dolfin::TpetraVector>, |
| 936 |
dolfin::GenericVector> |
| 937 |
(m, "TpetraVector", "DOLFIN TpetraVector object") |
| 938 |
.def(py::init([](const MPICommWrapper comm=MPICommWrapper(MPI_COMM_WORLD)) |
| 939 |
{ return std::unique_ptr<dolfin::TpetraVector>(new dolfin::TpetraVector(comm.get())); }), |
| 940 |
py::arg("comm")=MPICommWrapper(MPI_COMM_WORLD)) |
| 941 |
.def(py::init([](const MPICommWrapper comm, std::size_t N) |
| 942 |
{ return std::unique_ptr<dolfin::TpetraVector>(new dolfin::TpetraVector(comm.get(), N)); })); |
| 943 |
|
| 944 |
// dolfin::TpetraMatrix |
| 945 |
py::class_<dolfin::TpetraMatrix, std::shared_ptr<dolfin::TpetraMatrix>, |
| 946 |
dolfin::GenericMatrix> |
| 947 |
(m, "TpetraMatrix", "DOLFIN TpetraMatrix object") |
| 948 |
.def(py::init<>()); |
| 949 |
|
| 950 |
// dolfin::TrilinosPreconditioner |
| 951 |
py::class_<dolfin::TrilinosPreconditioner, std::shared_ptr<dolfin::TrilinosPreconditioner>> |
| 952 |
(m, "TrilinosPreconditioner", "DOLFIN TrilinosPreconditioner object"); |
| 953 |
|
| 954 |
// dolfin::MueluPreconditioner |
| 955 |
py::class_<dolfin::MueluPreconditioner, std::shared_ptr<dolfin::MueluPreconditioner>, |
| 956 |
dolfin::TrilinosPreconditioner, dolfin::Variable> |
| 957 |
(m, "MueluPreconditioner", "Muelu Preconditioner") |
| 958 |
.def(py::init<>()); |
| 959 |
|
| 960 |
// dolfin::BelosKrylovSolver |
| 961 |
py::class_<dolfin::BelosKrylovSolver, std::shared_ptr<dolfin::BelosKrylovSolver>, |
| 962 |
dolfin::GenericLinearSolver> |
| 963 |
(m, "BelosKrylovSolver", "Belos KrylovSolver") |
| 964 |
.def(py::init<std::string, std::shared_ptr<dolfin::TrilinosPreconditioner>>()) |
| 965 |
.def("set_operator", &dolfin::BelosKrylovSolver::set_operator) |
| 966 |
.def("set_operators", &dolfin::BelosKrylovSolver::set_operators) |
| 967 |
.def("solve", (std::size_t (dolfin::BelosKrylovSolver::*) |
| 968 |
(dolfin::GenericVector&, const dolfin::GenericVector&)) |
| 969 |
&dolfin::BelosKrylovSolver::solve) |
| 970 |
.def("solve", (std::size_t (dolfin::BelosKrylovSolver::*) |
| 971 |
(const dolfin::GenericLinearOperator&, |
| 972 |
dolfin::GenericVector&, const dolfin::GenericVector&)) |
| 973 |
&dolfin::BelosKrylovSolver::solve); |
| 974 |
#endif |
| 975 |
|
| 976 |
// dolfin::LUSolver |
| 977 |
py::class_<dolfin::LUSolver, std::shared_ptr<dolfin::LUSolver>, |
| 978 |
dolfin::GenericLinearSolver> |
| 979 |
(m, "LUSolver", "DOLFIN LUSolver object") |
| 980 |
.def(py::init<>()) |
| 981 |
.def(py::init<std::string>()) |
| 982 |
.def(py::init<std::shared_ptr<const dolfin::GenericLinearOperator>, std::string>(), |
| 983 |
py::arg("A"), py::arg("method")="default") |
| 984 |
.def(py::init([](const MPICommWrapper comm, |
| 985 |
std::shared_ptr<const dolfin::GenericLinearOperator> A, |
| 986 |
std::string method="default") |
| 987 |
{ return std::unique_ptr<dolfin::LUSolver>(new dolfin::LUSolver(comm.get(), A, method)); }), |
| 988 |
py::arg("comm"), py::arg("A"), py::arg("method") = "default") |
| 989 |
.def(py::init([](const MPICommWrapper comm, |
| 990 |
std::string method="default") |
| 991 |
{ return std::unique_ptr<dolfin::LUSolver>(new dolfin::LUSolver(comm.get(), method)); }), |
| 992 |
py::arg("comm"), py::arg("method") = "default") |
| 993 |
.def("set_operator", &dolfin::LUSolver::set_operator) |
| 994 |
.def("default_parameters", &dolfin::LUSolver::default_parameters) |
| 995 |
.def("solve", (std::size_t (dolfin::LUSolver::*)(dolfin::GenericVector&, |
| 996 |
const dolfin::GenericVector&)) |
| 997 |
&dolfin::LUSolver::solve) |
| 998 |
.def("solve", (std::size_t (dolfin::LUSolver::*)(const dolfin::GenericLinearOperator&, |
| 999 |
dolfin::GenericVector&, |
| 1000 |
const dolfin::GenericVector&)) |
| 1001 |
&dolfin::LUSolver::solve); |
| 1002 |
|
| 1003 |
#ifdef HAS_PETSC |
| 1004 |
// dolfin::PETScLUSolver |
| 1005 |
py::class_<dolfin::PETScLUSolver, std::shared_ptr<dolfin::PETScLUSolver>, |
| 1006 |
dolfin::GenericLinearSolver> |
| 1007 |
(m, "PETScLUSolver", "DOLFIN PETScLUSolver object") |
| 1008 |
.def(py::init<std::string>(), py::arg("method")="default") |
| 1009 |
.def(py::init([](const MPICommWrapper comm, |
| 1010 |
std::string method="default") |
| 1011 |
{ return std::unique_ptr<dolfin::PETScLUSolver>(new dolfin::PETScLUSolver(comm.get(), method)); }), |
| 1012 |
py::arg("comm"), py::arg("method") = "default") |
| 1013 |
.def(py::init([](const MPICommWrapper comm, |
| 1014 |
std::shared_ptr<const dolfin::PETScMatrix> A, |
| 1015 |
std::string method="default") |
| 1016 |
{ return std::unique_ptr<dolfin::PETScLUSolver>(new dolfin::PETScLUSolver(comm.get(), A, method)); }), |
| 1017 |
py::arg("comm"), py::arg("A"), py::arg("method") = "default") |
| 1018 |
.def(py::init<std::shared_ptr<const dolfin::PETScMatrix>, std::string>(), |
| 1019 |
py::arg("A"), py::arg("method")="default") |
| 1020 |
.def("set_operator", (void (dolfin::PETScLUSolver::*)(std::shared_ptr<const dolfin::GenericLinearOperator>)) |
| 1021 |
&dolfin::PETScLUSolver::set_operator) |
| 1022 |
.def("get_options_prefix", &dolfin::PETScLUSolver::get_options_prefix) |
| 1023 |
.def("set_options_prefix", &dolfin::PETScLUSolver::set_options_prefix) |
| 1024 |
.def("set_operator", (void (dolfin::PETScLUSolver::*)(std::shared_ptr<const dolfin::GenericLinearOperator>)) |
| 1025 |
&dolfin::PETScLUSolver::set_operator) |
| 1026 |
.def("set_from_options", &dolfin::PETScLUSolver::set_from_options) |
| 1027 |
.def("solve", (std::size_t (dolfin::PETScLUSolver::*)(dolfin::GenericVector&, const dolfin::GenericVector&)) |
| 1028 |
&dolfin::PETScLUSolver::solve) |
| 1029 |
.def("solve", (std::size_t (dolfin::PETScLUSolver::*)(const dolfin::GenericLinearOperator&, |
| 1030 |
dolfin::GenericVector&, |
| 1031 |
const dolfin::GenericVector&)) |
| 1032 |
&dolfin::PETScLUSolver::solve) |
| 1033 |
.def("ksp", &dolfin::PETScLUSolver::ksp); |
| 1034 |
#endif |
| 1035 |
|
| 1036 |
// dolfin::KrylovSolver |
| 1037 |
py::class_<dolfin::KrylovSolver, std::shared_ptr<dolfin::KrylovSolver>, |
| 1038 |
dolfin::GenericLinearSolver> |
| 1039 |
(m, "KrylovSolver", "DOLFIN KrylovSolver object") |
| 1040 |
.def(py::init<std::string, std::string>(), py::arg("method")="default", |
| 1041 |
py::arg("preconditioner")="default") |
| 1042 |
.def(py::init<std::shared_ptr<const dolfin::GenericLinearOperator>, |
| 1043 |
std::string, std::string>(), py::arg("A"), |
| 1044 |
py::arg("method")="default", py::arg("preconditioner")="default") |
| 1045 |
.def(py::init([](const MPICommWrapper comm, |
| 1046 |
std::shared_ptr<const dolfin::GenericLinearOperator> A, |
| 1047 |
std::string method="default", |
| 1048 |
std::string preconditioner="default") |
| 1049 |
{ return std::unique_ptr<dolfin::KrylovSolver>(new dolfin::KrylovSolver(comm.get(), A, |
| 1050 |
method, preconditioner)); }), |
| 1051 |
py::arg("comm"), py::arg("A"), py::arg("method") = "default", py::arg("preconditioner")="default") |
| 1052 |
.def(py::init([](const MPICommWrapper comm, |
| 1053 |
std::string method="default", |
| 1054 |
std::string preconditioner="default") |
| 1055 |
{ return std::unique_ptr<dolfin::KrylovSolver>(new dolfin::KrylovSolver(comm.get(), |
| 1056 |
method, preconditioner)); }), |
| 1057 |
py::arg("comm"), py::arg("method") = "default", py::arg("preconditioner")="default") |
| 1058 |
.def("set_operator", &dolfin::KrylovSolver::set_operator) |
| 1059 |
.def("set_operators", &dolfin::KrylovSolver::set_operators) |
| 1060 |
.def("default_parameters", &dolfin::KrylovSolver::default_parameters) |
| 1061 |
.def("solve", (std::size_t (dolfin::KrylovSolver::*)(dolfin::GenericVector&, |
| 1062 |
const dolfin::GenericVector&)) |
| 1063 |
&dolfin::KrylovSolver::solve) |
| 1064 |
.def("solve", (std::size_t (dolfin::KrylovSolver::*)(const dolfin::GenericLinearOperator&, |
| 1065 |
dolfin::GenericVector&, const dolfin::GenericVector&)) |
| 1066 |
&dolfin::KrylovSolver::solve); |
| 1067 |
|
| 1068 |
#ifdef HAS_PETSC |
| 1069 |
// dolfin::PETScKrylovSolver |
| 1070 |
py::class_<dolfin::PETScKrylovSolver, std::shared_ptr<dolfin::PETScKrylovSolver>, |
| 1071 |
dolfin::GenericLinearSolver> |
| 1072 |
petsc_ks(m, "PETScKrylovSolver", "DOLFIN PETScKrylovSolver object"); |
| 1073 |
|
| 1074 |
petsc_ks.def(py::init<>()) |
| 1075 |
.def(py::init<std::string>()) |
| 1076 |
.def(py::init<std::string, std::string>()) |
| 1077 |
.def(py::init<std::string, std::shared_ptr<dolfin::PETScPreconditioner>>()) |
| 1078 |
.def(py::init<KSP>()) |
| 1079 |
.def(py::init([](const MPICommWrapper comm, |
| 1080 |
std::string method="default", |
| 1081 |
std::string preconditioner="default") |
| 1082 |
{ return std::unique_ptr<dolfin::PETScKrylovSolver>(new dolfin::PETScKrylovSolver(comm.get(), |
| 1083 |
method, preconditioner)); }), |
| 1084 |
py::arg("comm"), py::arg("method") = "default", py::arg("preconditioner")="default") |
| 1085 |
.def("default_parameters", &dolfin::PETScKrylovSolver::default_parameters) |
| 1086 |
.def("get_options_prefix", &dolfin::PETScKrylovSolver::get_options_prefix) |
| 1087 |
.def("set_options_prefix", &dolfin::PETScKrylovSolver::set_options_prefix) |
| 1088 |
.def("get_norm_type", (dolfin::PETScKrylovSolver::norm_type (dolfin::PETScKrylovSolver::*)() const) |
| 1089 |
&dolfin::PETScKrylovSolver::get_norm_type) |
| 1090 |
.def("set_norm_type", &dolfin::PETScKrylovSolver::set_norm_type) |
| 1091 |
.def("set_operator", (void (dolfin::PETScKrylovSolver::*)(std::shared_ptr<const dolfin::GenericLinearOperator>)) |
| 1092 |
&dolfin::PETScKrylovSolver::set_operator) |
| 1093 |
.def("set_operators", (void (dolfin::PETScKrylovSolver::*)(std::shared_ptr<const dolfin::GenericLinearOperator>, |
| 1094 |
std::shared_ptr<const dolfin::GenericLinearOperator>)) |
| 1095 |
&dolfin::PETScKrylovSolver::set_operators) |
| 1096 |
.def("solve", (std::size_t (dolfin::PETScKrylovSolver::*)(dolfin::GenericVector&, const dolfin::GenericVector&)) |
| 1097 |
&dolfin::PETScKrylovSolver::solve) |
| 1098 |
.def("solve", (std::size_t (dolfin::PETScKrylovSolver::*)(const dolfin::GenericLinearOperator&, |
| 1099 |
dolfin::GenericVector&, const dolfin::GenericVector&)) |
| 1100 |
&dolfin::PETScKrylovSolver::solve) |
| 1101 |
.def("set_from_options", &dolfin::PETScKrylovSolver::set_from_options) |
| 1102 |
.def("set_reuse_preconditioner", &dolfin::PETScKrylovSolver::set_reuse_preconditioner) |
| 1103 |
.def("set_dm", &dolfin::PETScKrylovSolver::set_dm) |
| 1104 |
.def("set_dm_active", &dolfin::PETScKrylovSolver::set_dm_active) |
| 1105 |
.def("ksp", &dolfin::PETScKrylovSolver::ksp); |
| 1106 |
|
| 1107 |
py::enum_<dolfin::PETScKrylovSolver::norm_type>(petsc_ks, "norm_type") |
| 1108 |
.value("none", dolfin::PETScKrylovSolver::norm_type::none) |
| 1109 |
.value("default_norm", dolfin::PETScKrylovSolver::norm_type::default_norm) |
| 1110 |
.value("preconditioned", dolfin::PETScKrylovSolver::norm_type::preconditioned) |
| 1111 |
.value("unpreconditioned", dolfin::PETScKrylovSolver::norm_type::unpreconditioned) |
| 1112 |
.value("natural", dolfin::PETScKrylovSolver::norm_type::natural); |
| 1113 |
#endif |
| 1114 |
|
| 1115 |
#ifdef HAS_SLEPC |
| 1116 |
// dolfin::SLEPcEigenSolver |
| 1117 |
py::class_<dolfin::SLEPcEigenSolver, std::shared_ptr<dolfin::SLEPcEigenSolver>, |
| 1118 |
dolfin::Variable>(m, "SLEPcEigenSolver") |
| 1119 |
.def(py::init<std::shared_ptr<const dolfin::PETScMatrix>>()) |
| 1120 |
.def(py::init<std::shared_ptr<const dolfin::PETScMatrix>, std::shared_ptr<const dolfin::PETScMatrix>>()) |
| 1121 |
// FIXME: The below must come after the other |
| 1122 |
// constructors. Check the MPI_Comm caster raises appropriate |
| 1123 |
// exceptions for pybind11 to move onto next interface. |
| 1124 |
.def(py::init([](const MPICommWrapper comm) |
| 1125 |
{ return std::unique_ptr<dolfin::SLEPcEigenSolver>(new dolfin::SLEPcEigenSolver(comm.get())); })) |
| 1126 |
.def("set_options_prefix", &dolfin::SLEPcEigenSolver::set_options_prefix) |
| 1127 |
.def("set_from_options", &dolfin::SLEPcEigenSolver::set_from_options) |
| 1128 |
.def("set_operators", &dolfin::SLEPcEigenSolver::set_operators) |
| 1129 |
.def("get_options_prefix", &dolfin::SLEPcEigenSolver::get_options_prefix) |
| 1130 |
.def("get_number_converged", &dolfin::SLEPcEigenSolver::get_number_converged) |
| 1131 |
.def("set_deflation_space", &dolfin::SLEPcEigenSolver::set_deflation_space) |
| 1132 |
.def("set_initial_space", &dolfin::SLEPcEigenSolver::set_initial_space) |
| 1133 |
.def("solve", (void (dolfin::SLEPcEigenSolver::*)()) |
| 1134 |
&dolfin::SLEPcEigenSolver::solve) |
| 1135 |
.def("solve", (void (dolfin::SLEPcEigenSolver::*)(std::size_t)) |
| 1136 |
&dolfin::SLEPcEigenSolver::solve) |
| 1137 |
.def("get_eigenvalue", [](dolfin::SLEPcEigenSolver& self, std::size_t i) |
| 1138 |
{ |
| 1139 |
double lr, lc; |
| 1140 |
self.get_eigenvalue(lr, lc, i); |
| 1141 |
return py::make_tuple(lr, lc); |
| 1142 |
}) |
| 1143 |
.def("get_eigenpair", [](dolfin::SLEPcEigenSolver& self, std::size_t i) |
| 1144 |
{ |
| 1145 |
double lr, lc; |
| 1146 |
dolfin::PETScVector r, c; |
| 1147 |
self.get_eigenpair(lr, lc, r, c, i); |
| 1148 |
return py::make_tuple(lr, lc, r, c); |
| 1149 |
}) |
| 1150 |
.def("get_eigenpair", [](dolfin::SLEPcEigenSolver& self, dolfin::PETScVector& r, dolfin::PETScVector& c, std::size_t i) |
| 1151 |
{ |
| 1152 |
double lr, lc; |
| 1153 |
self.get_eigenpair(lr, lc, r, c, i); |
| 1154 |
return py::make_tuple(lr, lc); |
| 1155 |
}); |
| 1156 |
#endif |
| 1157 |
|
| 1158 |
#ifdef HAS_SUNDIALS |
| 1159 |
//dolfin::SUNDIALSNVector |
| 1160 |
py::class_<dolfin::SUNDIALSNVector, std::shared_ptr<dolfin::SUNDIALSNVector>>(m,"SUNDIALSNVector") |
| 1161 |
.def(py::init<const dolfin::GenericVector&>()) |
| 1162 |
.def(py::init<const dolfin::SUNDIALSNVector&>()) |
| 1163 |
.def(py::init<std::shared_ptr<dolfin::GenericVector>>()) |
| 1164 |
.def("nvector", (N_Vector (dolfin::SUNDIALSNVector::*)()) |
| 1165 |
&dolfin::SUNDIALSNVector::nvector) |
| 1166 |
.def("vec", (std::shared_ptr<dolfin::GenericVector> (dolfin::SUNDIALSNVector::*)()) |
| 1167 |
&dolfin::SUNDIALSNVector::vec); |
| 1168 |
#endif |
| 1169 |
|
| 1170 |
// dolfin::VectorSpaceBasis |
| 1171 |
py::class_<dolfin::VectorSpaceBasis, std::shared_ptr<dolfin::VectorSpaceBasis>>(m, "VectorSpaceBasis") |
| 1172 |
.def(py::init<const std::vector<std::shared_ptr<dolfin::GenericVector>>>()) |
| 1173 |
.def("is_orthonormal", &dolfin::VectorSpaceBasis::is_orthonormal, py::arg("tol")=1.0e-10) |
| 1174 |
.def("is_orthogonal", &dolfin::VectorSpaceBasis::is_orthogonal, py::arg("tol")=1.0e-10) |
| 1175 |
.def("orthogonalize", &dolfin::VectorSpaceBasis::orthogonalize) |
| 1176 |
.def("orthonormalize", &dolfin::VectorSpaceBasis::orthonormalize, py::arg("tol")=1.0e-10) |
| 1177 |
.def("dim", &dolfin::VectorSpaceBasis::dim) |
| 1178 |
.def("__getitem__", &dolfin::VectorSpaceBasis::operator[]); |
| 1179 |
|
| 1180 |
// test_nullspace.h |
| 1181 |
m.def("in_nullspace", &dolfin::in_nullspace, py::arg("A"), py::arg("x"), |
| 1182 |
py::arg("type")="right"); |
| 1183 |
|
| 1184 |
// la free functions |
| 1185 |
m.def("list_linear_algebra_backends", &dolfin::list_linear_algebra_backends); |
| 1186 |
m.def("list_linear_solver_methods", &dolfin::list_linear_solver_methods); |
| 1187 |
m.def("list_lu_solver_methods", &dolfin::list_lu_solver_methods); |
| 1188 |
m.def("list_krylov_solver_methods", &dolfin::list_krylov_solver_methods); |
| 1189 |
m.def("list_krylov_solver_preconditioners", &dolfin::list_krylov_solver_preconditioners); |
| 1190 |
m.def("has_linear_algebra_backend", &dolfin::has_linear_algebra_backend); |
| 1191 |
m.def("has_lu_solver_method", &dolfin::has_lu_solver_method); |
| 1192 |
m.def("has_krylov_solver_method", &dolfin::has_krylov_solver_method); |
| 1193 |
m.def("has_krylov_solver_preconditioner", &dolfin::has_krylov_solver_preconditioner); |
| 1194 |
m.def("linear_algebra_backends", &dolfin::linear_algebra_backends); |
| 1195 |
m.def("linear_solver_methods", &dolfin::linear_solver_methods); |
| 1196 |
m.def("lu_solver_methods", &dolfin::lu_solver_methods); |
| 1197 |
m.def("krylov_solver_methods", &dolfin::krylov_solver_methods); |
| 1198 |
m.def("krylov_solver_preconditioners", &dolfin::krylov_solver_preconditioners); |
| 1199 |
m.def("normalize", &dolfin::normalize, py::arg("x"), py::arg("normalization_type")="average"); |
| 1200 |
|
| 1201 |
// la/solve free functions |
| 1202 |
m.def("solve", (std::size_t (*)(const dolfin::GenericLinearOperator&, dolfin::GenericVector&, |
| 1203 |
const dolfin::GenericVector&, std::string, std::string)) &dolfin::solve, |
| 1204 |
py::arg("A"), py::arg("x"), py::arg("b"), py::arg("method")="lu", |
| 1205 |
py::arg("preconditioner")="none"); |
| 1206 |
m.def("residual", &dolfin::residual); |
| 1207 |
} |
| 1208 |
} |