| 39 | namespace nb = nanobind; |
| 40 | |
| 41 | void petsc_la_module(nb::module_& m) |
| 42 | { |
| 43 | import_petsc4py(); |
| 44 | |
| 45 | m.def( |
| 46 | "create_matrix", |
| 47 | [](dolfinx_wrappers::MPICommWrapper comm, |
| 48 | const dolfinx::la::SparsityPattern& p, std::optional<std::string> type) |
| 49 | { |
| 50 | Mat A = dolfinx::la::petsc::create_matrix(comm.get(), p, type); |
| 51 | PyObject* obj = PyPetscMat_New(A); |
| 52 | PetscObjectDereference((PetscObject)A); |
| 53 | return nb::borrow(obj); |
| 54 | }, |
| 55 | nb::arg("comm"), nb::arg("p"), nb::arg("type") = nb::none(), |
| 56 | "Create a PETSc Mat from sparsity pattern."); |
| 57 | |
| 58 | m.def( |
| 59 | "create_index_sets", |
| 60 | [](const std::vector<std::pair<const dolfinx::common::IndexMap*, int>>& |
| 61 | maps) |
| 62 | { |
| 63 | using X = std::vector<std::pair< |
| 64 | std::reference_wrapper<const dolfinx::common::IndexMap>, int>>; |
| 65 | X _maps; |
| 66 | std::ranges::transform(maps, std::back_inserter(_maps), |
| 67 | [](auto m) -> typename X::value_type |
| 68 | { return {*m.first, m.second}; }); |
| 69 | std::vector<IS> index_sets |
| 70 | = dolfinx::la::petsc::create_index_sets(_maps); |
| 71 | |
| 72 | std::vector<nb::object> py_index_sets; |
| 73 | for (auto is : index_sets) |
| 74 | { |
| 75 | PyObject* obj = PyPetscIS_New(is); |
| 76 | PetscObjectDereference((PetscObject)is); |
| 77 | py_index_sets.push_back(nb::steal(obj)); |
| 78 | } |
| 79 | return py_index_sets; |
| 80 | }, |
| 81 | nb::arg("maps")); |
| 82 | |
| 83 | m.def( |
| 84 | "scatter_local_vectors", |
| 85 | [](Vec x, |
| 86 | const std::vector< |
| 87 | nb::ndarray<const PetscScalar, nb::ndim<1>, nb::c_contig>>& x_b, |
| 88 | const std::vector<std::pair< |
| 89 | std::shared_ptr<const dolfinx::common::IndexMap>, int>>& maps) |
| 90 | { |
| 91 | std::vector<std::span<const PetscScalar>> _x_b; |
| 92 | std::ranges::transform(x_b, std::back_inserter(_x_b), [](auto& x) |
| 93 | { return std::span(x.data(), x.size()); }); |
| 94 | |
| 95 | using X = std::vector<std::pair< |
| 96 | std::reference_wrapper<const dolfinx::common::IndexMap>, int>>; |
| 97 | X _maps; |
| 98 | std::ranges::transform(maps, std::back_inserter(_maps), |
no test coverage detected