Skip to content

Commit

Permalink
Use std::optional in bc setting
Browse files Browse the repository at this point in the history
  • Loading branch information
garth-wells committed Sep 24, 2024
1 parent 4ae2d4b commit e789938
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
45 changes: 21 additions & 24 deletions cpp/dolfinx/fem/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cstdint>
#include <dolfinx/common/types.h>
#include <memory>
#include <optional>
#include <span>
#include <vector>

Expand All @@ -37,10 +38,10 @@ make_coefficients_span(const std::map<std::pair<IntegralType, int>,
{
using Key = typename std::remove_reference_t<decltype(coeffs)>::key_type;
std::map<Key, std::pair<std::span<const T>, 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;
}

Expand Down Expand Up @@ -418,30 +419,26 @@ void set_diagonal(
template <dolfinx::scalar T, std::floating_point U>
void set_bc(std::span<T> b,
const std::vector<std::shared_ptr<const DirichletBC<T, U>>>& bcs,
std::span<const T> x0, T scale = 1)
std::optional<std::span<const T>> 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 <dolfinx::scalar T, std::floating_point U>
void set_bc(std::span<T> b,
const std::vector<std::shared_ptr<const DirichletBC<T, U>>>& 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
6 changes: 1 addition & 5 deletions python/dolfinx/fem/assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
27 changes: 15 additions & 12 deletions python/dolfinx/wrappers/assemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, U>& form) {
[](const dolfinx::fem::Form<T, U>& form)
{
return dolfinx_wrappers::as_nbarray(dolfinx::fem::pack_constants(form));
},
nb::arg("form"), "Pack constants for a Form.");
Expand Down Expand Up @@ -417,21 +418,23 @@ void declare_assembly_functions(nb::module_& m)
[](nb::ndarray<T, nb::ndim<1>, nb::c_contig> b,
const std::vector<
std::shared_ptr<const dolfinx::fem::DirichletBC<T, U>>>& bcs,
nb::ndarray<const T, nb::ndim<1>, nb::c_contig> x0, T scale)
std::optional<nb::ndarray<const T, nb::ndim<1>, nb::c_contig>> x0,
T scale)
{
dolfinx::fem::set_bc<T>(std::span(b.data(), b.size()), bcs,
std::span(x0.data(), x0.shape(0)), scale);
if (x0.has_value())
{
dolfinx::fem::set_bc<T>(
std::span(b.data(), b.size()), bcs,
std::span(x0.value().data(), x0.value().shape(0)), scale);
}
else
{
dolfinx::fem::set_bc<T>(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<T, nb::ndim<1>, nb::c_contig> b,
const std::vector<
std::shared_ptr<const dolfinx::fem::DirichletBC<T, U>>>& bcs,
T scale)
{ dolfinx::fem::set_bc<T>(std::span(b.data(), b.size()), bcs, scale); },
nb::arg("b").noconvert(), nb::arg("bcs"), nb::arg("scale"));
}

} // namespace
Expand Down

0 comments on commit e789938

Please sign in to comment.