diff --git a/cpp/dolfinx/fem/assembler.h b/cpp/dolfinx/fem/assembler.h index c53aef84b4..f1c6f6271a 100644 --- a/cpp/dolfinx/fem/assembler.h +++ b/cpp/dolfinx/fem/assembler.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -37,10 +38,10 @@ make_coefficients_span(const std::map, { using Key = typename std::remove_reference_t::key_type; std::map, int>> c; - std::ranges::transform(coeffs, std::inserter(c, c.end()), - [](auto& e) -> typename decltype(c)::value_type { - return {e.first, {e.second.first, e.second.second}}; - }); + std::ranges::transform( + coeffs, std::inserter(c, c.end()), + [](auto& e) -> typename decltype(c)::value_type + { return {e.first, {e.second.first, e.second.second}}; }); return c; } @@ -418,30 +419,26 @@ void set_diagonal( template void set_bc(std::span b, const std::vector>>& bcs, - std::span x0, T scale = 1) + std::optional> x0, T scale = 1) { - if (b.size() > x0.size()) - throw std::runtime_error("Size mismatch between b and x0 vectors."); - for (auto& bc : bcs) + if (x0.has_value()) { - assert(bc); - bc->set(b, x0, scale); - } -} -/// Set bc values in owned (local) part of the vector, multiplied by -/// 'scale'. The bcs should be on (sub-)spaces of the form L that b -/// represents. -template -void set_bc(std::span b, - const std::vector>>& bcs, - T scale = 1) -{ - for (auto& bc : bcs) + if (b.size() > x0.value().size()) + throw std::runtime_error("Size mismatch between b and x0 vectors."); + for (auto& bc : bcs) + { + assert(bc); + bc->set(b, x0.value(), scale); + } + } + else { - assert(bc); - bc->set(b, scale); + for (auto& bc : bcs) + { + assert(bc); + bc->set(b, scale); + } } } - } // namespace dolfinx::fem diff --git a/python/dolfinx/fem/assemble.py b/python/dolfinx/fem/assemble.py index d3bb918e80..3ba3915c74 100644 --- a/python/dolfinx/fem/assemble.py +++ b/python/dolfinx/fem/assemble.py @@ -359,8 +359,4 @@ def set_bc( calling this function is not required unless ghost entries need to be updated to the boundary condition value. """ - _bcs = [bc._cpp_object for bc in bcs] - if x0 is None: - _cpp.fem.set_bc(b, _bcs, scale) - else: - _cpp.fem.set_bc(b, _bcs, x0, scale) + _cpp.fem.set_bc(b, [bc._cpp_object for bc in bcs], x0, scale) diff --git a/python/dolfinx/wrappers/assemble.cpp b/python/dolfinx/wrappers/assemble.cpp index d216039d08..4f71eb5c67 100644 --- a/python/dolfinx/wrappers/assemble.cpp +++ b/python/dolfinx/wrappers/assemble.cpp @@ -193,7 +193,8 @@ void declare_assembly_functions(nb::module_& m) nb::arg("form"), "Pack coefficients for a Form."); m.def( "pack_constants", - [](const dolfinx::fem::Form& form) { + [](const dolfinx::fem::Form& form) + { return dolfinx_wrappers::as_nbarray(dolfinx::fem::pack_constants(form)); }, nb::arg("form"), "Pack constants for a Form."); @@ -417,21 +418,23 @@ void declare_assembly_functions(nb::module_& m) [](nb::ndarray, nb::c_contig> b, const std::vector< std::shared_ptr>>& bcs, - nb::ndarray, nb::c_contig> x0, T scale) + std::optional, nb::c_contig>> x0, + T scale) { - dolfinx::fem::set_bc(std::span(b.data(), b.size()), bcs, - std::span(x0.data(), x0.shape(0)), scale); + if (x0.has_value()) + { + dolfinx::fem::set_bc( + std::span(b.data(), b.size()), bcs, + std::span(x0.value().data(), x0.value().shape(0)), scale); + } + else + { + dolfinx::fem::set_bc(std::span(b.data(), b.size()), bcs, + std::nullopt, scale); + } }, nb::arg("b").noconvert(), nb::arg("bcs"), nb::arg("x0").noconvert(), nb::arg("scale")); - m.def( - "set_bc", - [](nb::ndarray, nb::c_contig> b, - const std::vector< - std::shared_ptr>>& bcs, - T scale) - { dolfinx::fem::set_bc(std::span(b.data(), b.size()), bcs, scale); }, - nb::arg("b").noconvert(), nb::arg("bcs"), nb::arg("scale")); } } // namespace