25namespace dolfinx::fem::impl
28using mdspan2_t = MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
30 MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 2>>;
60template <dolfinx::scalar T>
63 std::span<
const scalar_value_type_t<T>> x,
64 std::span<const std::int32_t> cells,
65 std::tuple<mdspan2_t,
int, std::span<const std::int32_t>> dofmap0,
67 std::tuple<mdspan2_t,
int, std::span<const std::int32_t>> dofmap1,
69 std::span<const std::int8_t> bc1,
FEkernel<T> auto kernel,
70 std::span<const T> coeffs,
int cstride, std::span<const T> constants,
71 std::span<const std::uint32_t> cell_info0,
72 std::span<const std::uint32_t> cell_info1)
77 const auto [dmap0, bs0, cells0] = dofmap0;
78 const auto [dmap1, bs1, cells1] = dofmap1;
81 const int num_dofs0 = dmap0.extent(1);
82 const int num_dofs1 = dmap1.extent(1);
83 const int ndim0 = bs0 * num_dofs0;
84 const int ndim1 = bs1 * num_dofs1;
85 std::vector<T> Ae(ndim0 * ndim1);
87 std::vector<scalar_value_type_t<T>> coordinate_dofs(3 * x_dofmap.extent(1));
90 assert(cells0.size() == cells.size());
91 assert(cells1.size() == cells.size());
92 for (std::size_t index = 0; index < cells.size(); ++index)
96 std::int32_t c = cells[index];
97 std::int32_t c0 = cells0[index];
98 std::int32_t c1 = cells1[index];
101 auto x_dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
102 x_dofmap, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
103 for (std::size_t i = 0; i < x_dofs.size(); ++i)
105 std::copy_n(std::next(x.begin(), 3 * x_dofs[i]), 3,
106 std::next(coordinate_dofs.begin(), 3 * i));
110 std::fill(Ae.begin(), Ae.end(), 0);
111 kernel(Ae.data(), coeffs.data() + index * cstride, constants.data(),
112 coordinate_dofs.data(),
nullptr,
nullptr);
115 P0(_Ae, cell_info0, c0, ndim1);
116 P1T(_Ae, cell_info1, c1, ndim0);
119 auto dofs0 = std::span(dmap0.data_handle() + c0 * num_dofs0, num_dofs0);
120 auto dofs1 = std::span(dmap1.data_handle() + c1 * num_dofs1, num_dofs1);
124 for (
int i = 0; i < num_dofs0; ++i)
126 for (
int k = 0; k < bs0; ++k)
128 if (bc0[bs0 * dofs0[i] + k])
131 const int row = bs0 * i + k;
132 std::fill_n(std::next(Ae.begin(), ndim1 * row), ndim1, 0);
140 for (
int j = 0; j < num_dofs1; ++j)
142 for (
int k = 0; k < bs1; ++k)
144 if (bc1[bs1 * dofs1[j] + k])
147 const int col = bs1 * j + k;
148 for (
int row = 0; row < ndim0; ++row)
149 Ae[row * ndim1 + col] = 0;
155 mat_set(dofs0, dofs1, Ae);
188template <dolfinx::scalar T>
189void assemble_exterior_facets(
191 std::span<
const scalar_value_type_t<T>> x,
192 std::span<const std::int32_t> facets,
193 std::tuple<mdspan2_t,
int, std::span<const std::int32_t>> dofmap0,
195 std::tuple<mdspan2_t,
int, std::span<const std::int32_t>> dofmap1,
197 std::span<const std::int8_t> bc1,
FEkernel<T> auto kernel,
198 std::span<const T> coeffs,
int cstride, std::span<const T> constants,
199 std::span<const std::uint32_t> cell_info0,
200 std::span<const std::uint32_t> cell_info1)
205 const auto [dmap0, bs0, facets0] = dofmap0;
206 const auto [dmap1, bs1, facets1] = dofmap1;
209 std::vector<scalar_value_type_t<T>> coordinate_dofs(3 * x_dofmap.extent(1));
210 const int num_dofs0 = dmap0.extent(1);
211 const int num_dofs1 = dmap1.extent(1);
212 const int ndim0 = bs0 * num_dofs0;
213 const int ndim1 = bs1 * num_dofs1;
214 std::vector<T> Ae(ndim0 * ndim1);
215 std::span<T> _Ae(Ae);
216 assert(facets.size() % 2 == 0);
217 assert(facets0.size() == facets.size());
218 assert(facets1.size() == facets.size());
219 for (std::size_t index = 0; index < facets.size(); index += 2)
224 std::int32_t
cell = facets[index];
225 std::int32_t local_facet = facets[index + 1];
226 std::int32_t cell0 = facets0[index];
227 std::int32_t cell1 = facets1[index];
230 auto x_dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
231 x_dofmap,
cell, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
232 for (std::size_t i = 0; i < x_dofs.size(); ++i)
234 std::copy_n(std::next(x.begin(), 3 * x_dofs[i]), 3,
235 std::next(coordinate_dofs.begin(), 3 * i));
239 std::fill(Ae.begin(), Ae.end(), 0);
240 kernel(Ae.data(), coeffs.data() + index / 2 * cstride, constants.data(),
241 coordinate_dofs.data(), &local_facet,
nullptr);
243 P0(_Ae, cell_info0, cell0, ndim1);
244 P1T(_Ae, cell_info1, cell1, ndim0);
247 auto dofs0 = std::span(dmap0.data_handle() + cell0 * num_dofs0, num_dofs0);
248 auto dofs1 = std::span(dmap1.data_handle() + cell1 * num_dofs1, num_dofs1);
251 for (
int i = 0; i < num_dofs0; ++i)
253 for (
int k = 0; k < bs0; ++k)
255 if (bc0[bs0 * dofs0[i] + k])
258 const int row = bs0 * i + k;
259 std::fill_n(std::next(Ae.begin(), ndim1 * row), ndim1, 0);
266 for (
int j = 0; j < num_dofs1; ++j)
268 for (
int k = 0; k < bs1; ++k)
270 if (bc1[bs1 * dofs1[j] + k])
273 const int col = bs1 * j + k;
274 for (
int row = 0; row < ndim0; ++row)
275 Ae[row * ndim1 + col] = 0;
281 mat_set(dofs0, dofs1, Ae);
317template <dolfinx::scalar T>
318void assemble_interior_facets(
320 std::span<
const scalar_value_type_t<T>> x,
int num_cell_facets,
321 std::span<const std::int32_t> facets,
322 std::tuple<
const DofMap&,
int, std::span<const std::int32_t>> dofmap0,
324 std::tuple<
const DofMap&,
int, std::span<const std::int32_t>> dofmap1,
326 std::span<const std::int8_t> bc1,
FEkernel<T> auto kernel,
327 std::span<const T> coeffs,
int cstride, std::span<const int> offsets,
328 std::span<const T> constants, std::span<const std::uint32_t> cell_info0,
329 std::span<const std::uint32_t> cell_info1,
330 const std::function<std::uint8_t(std::size_t)>& get_perm)
335 const auto [dmap0, bs0, facets0] = dofmap0;
336 const auto [dmap1, bs1, facets1] = dofmap1;
339 using X = scalar_value_type_t<T>;
340 std::vector<X> coordinate_dofs(2 * x_dofmap.extent(1) * 3);
341 std::span<X> cdofs0(coordinate_dofs.data(), x_dofmap.extent(1) * 3);
342 std::span<X> cdofs1(coordinate_dofs.data() + x_dofmap.extent(1) * 3,
343 x_dofmap.extent(1) * 3);
345 std::vector<T> Ae, be;
346 std::vector<T> coeff_array(2 * offsets.back());
347 assert(offsets.back() == cstride);
350 std::vector<std::int32_t> dmapjoint0, dmapjoint1;
351 assert(facets.size() % 4 == 0);
352 assert(facets0.size() == facets.size());
353 assert(facets1.size() == facets.size());
354 for (std::size_t index = 0; index < facets.size(); index += 4)
358 std::array cells{facets[index], facets[index + 2]};
359 std::array cells0{facets0[index], facets0[index + 2]};
360 std::array cells1{facets1[index], facets1[index + 2]};
363 std::array local_facet{facets[index + 1], facets[index + 3]};
366 auto x_dofs0 = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
367 x_dofmap, cells[0], MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
368 for (std::size_t i = 0; i < x_dofs0.size(); ++i)
370 std::copy_n(std::next(x.begin(), 3 * x_dofs0[i]), 3,
371 std::next(cdofs0.begin(), 3 * i));
373 auto x_dofs1 = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
374 x_dofmap, cells[1], MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
375 for (std::size_t i = 0; i < x_dofs1.size(); ++i)
377 std::copy_n(std::next(x.begin(), 3 * x_dofs1[i]), 3,
378 std::next(cdofs1.begin(), 3 * i));
382 std::span<const std::int32_t> dmap0_cell0 = dmap0.cell_dofs(cells0[0]);
383 std::span<const std::int32_t> dmap0_cell1 = dmap0.cell_dofs(cells0[1]);
384 dmapjoint0.resize(dmap0_cell0.size() + dmap0_cell1.size());
385 std::copy(dmap0_cell0.begin(), dmap0_cell0.end(), dmapjoint0.begin());
386 std::copy(dmap0_cell1.begin(), dmap0_cell1.end(),
387 std::next(dmapjoint0.begin(), dmap0_cell0.size()));
389 std::span<const std::int32_t> dmap1_cell0 = dmap1.cell_dofs(cells1[0]);
390 std::span<const std::int32_t> dmap1_cell1 = dmap1.cell_dofs(cells1[1]);
391 dmapjoint1.resize(dmap1_cell0.size() + dmap1_cell1.size());
392 std::copy(dmap1_cell0.begin(), dmap1_cell0.end(), dmapjoint1.begin());
393 std::copy(dmap1_cell1.begin(), dmap1_cell1.end(),
394 std::next(dmapjoint1.begin(), dmap1_cell0.size()));
396 const int num_rows = bs0 * dmapjoint0.size();
397 const int num_cols = bs1 * dmapjoint1.size();
400 Ae.resize(num_rows * num_cols);
401 std::fill(Ae.begin(), Ae.end(), 0);
403 const std::array perm{
404 get_perm(cells[0] * num_cell_facets + local_facet[0]),
405 get_perm(cells[1] * num_cell_facets + local_facet[1])};
406 kernel(Ae.data(), coeffs.data() + index / 2 * cstride, constants.data(),
407 coordinate_dofs.data(), local_facet.data(), perm.data());
416 std::span<T> _Ae(Ae);
417 std::span<T> sub_Ae0 = _Ae.subspan(bs0 * dmap0_cell0.size() * num_cols,
418 bs0 * dmap0_cell1.size() * num_cols);
420 P0(_Ae, cell_info0, cells0[0], num_cols);
421 P0(sub_Ae0, cell_info0, cells0[1], num_cols);
422 P1T(_Ae, cell_info1, cells1[0], num_rows);
424 for (
int row = 0; row < num_rows; ++row)
428 std::span<T> sub_Ae1 = _Ae.subspan(
429 row * num_cols + bs1 * dmap1_cell0.size(), bs1 * dmap1_cell1.size());
430 P1T(sub_Ae1, cell_info1, cells1[1], 1);
436 for (std::size_t i = 0; i < dmapjoint0.size(); ++i)
438 for (
int k = 0; k < bs0; ++k)
440 if (bc0[bs0 * dmapjoint0[i] + k])
443 std::fill_n(std::next(Ae.begin(), num_cols * (bs0 * i + k)),
451 for (std::size_t j = 0; j < dmapjoint1.size(); ++j)
453 for (
int k = 0; k < bs1; ++k)
455 if (bc1[bs1 * dmapjoint1[j] + k])
458 for (
int m = 0; m < num_rows; ++m)
459 Ae[m * num_cols + bs1 * j + k] = 0;
465 mat_set(dmapjoint0, dmapjoint1, Ae);
474template <dolfinx::scalar T, std::
floating_po
int U>
477 std::span<
const scalar_value_type_t<T>> x, std::span<const T> constants,
478 const std::map<std::pair<IntegralType, int>,
479 std::pair<std::span<const T>,
int>>& coefficients,
480 std::span<const std::int8_t> bc0, std::span<const std::int8_t> bc1)
483 std::shared_ptr<const mesh::Mesh<U>> mesh = a.mesh();
486 auto mesh0 = a.function_spaces().at(0)->mesh();
489 auto mesh1 = a.function_spaces().at(1)->mesh();
493 std::shared_ptr<const fem::DofMap> dofmap0
494 = a.function_spaces().at(0)->dofmap();
495 std::shared_ptr<const fem::DofMap> dofmap1
496 = a.function_spaces().at(1)->dofmap();
499 auto dofs0 = dofmap0->map();
500 const int bs0 = dofmap0->bs();
501 auto dofs1 = dofmap1->map();
502 const int bs1 = dofmap1->bs();
504 auto element0 = a.function_spaces().at(0)->element();
506 auto element1 = a.function_spaces().at(1)->element();
511 = element1->template dof_transformation_right_fn<T>(
514 std::span<const std::uint32_t> cell_info0;
515 std::span<const std::uint32_t> cell_info1;
516 if (element0->needs_dof_transformations()
517 or element1->needs_dof_transformations() or a.needs_facet_permutations())
519 mesh0->topology_mutable()->create_entity_permutations();
520 mesh1->topology_mutable()->create_entity_permutations();
521 cell_info0 = std::span(mesh0->topology()->get_cell_permutation_info());
522 cell_info1 = std::span(mesh1->topology()->get_cell_permutation_info());
530 impl::assemble_cells(
532 {dofs0, bs0, a.domain(IntegralType::cell, i, *mesh0)}, P0,
533 {dofs1, bs1, a.domain(IntegralType::cell, i, *mesh1)}, P1T, bc0, bc1,
534 fn, coeffs, cstride, constants, cell_info0, cell_info1);
541 auto& [coeffs, cstride]
543 impl::assemble_exterior_facets(
545 {dofs0, bs0, a.domain(IntegralType::exterior_facet, i, *mesh0)}, P0,
546 {dofs1, bs1, a.domain(IntegralType::exterior_facet, i, *mesh1)}, P1T,
547 bc0, bc1, fn, coeffs, cstride, constants, cell_info0, cell_info1);
552 std::function<std::uint8_t(std::size_t)> get_perm;
553 if (a.needs_facet_permutations())
555 mesh->topology_mutable()->create_entity_permutations();
556 const std::vector<std::uint8_t>& perms
557 = mesh->topology()->get_facet_permutations();
558 get_perm = [&perms](std::size_t i) {
return perms[i]; };
561 get_perm = [](std::size_t) {
return 0; };
566 const std::vector<int> c_offsets = a.coefficient_offsets();
571 auto& [coeffs, cstride]
573 impl::assemble_interior_facets(
574 mat_set, x_dofmap, x, num_cell_facets,
576 {*dofmap0, bs0, a.domain(IntegralType::interior_facet, i, *mesh0)},
578 {*dofmap1, bs1, a.domain(IntegralType::interior_facet, i, *mesh1)},
579 P1T, bc0, bc1, fn, coeffs, cstride, c_offsets, constants, cell_info0,
580 cell_info1, get_perm);