Skip to content

Commit cfc5ca5

Browse files
authored
Put FEM 'packing' functions into separate header (#3621)
* Simplify coefficient packing for Expressions * Work on packing * Merge pack functions * Simplify * Simplify * Break up fem/utils.h * Add pack.h * Add concept * Fix docstring * Simplifications * Simplifications * Small edit
1 parent e8b710a commit cfc5ca5

5 files changed

Lines changed: 476 additions & 426 deletions

File tree

cpp/dolfinx/fem/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(HEADERS_fem
1818
${CMAKE_CURRENT_SOURCE_DIR}/dolfinx_fem.h
1919
${CMAKE_CURRENT_SOURCE_DIR}/interpolate.h
2020
${CMAKE_CURRENT_SOURCE_DIR}/petsc.h
21+
${CMAKE_CURRENT_SOURCE_DIR}/pack.h
2122
${CMAKE_CURRENT_SOURCE_DIR}/sparsitybuild.h
2223
${CMAKE_CURRENT_SOURCE_DIR}/traits.h
2324
${CMAKE_CURRENT_SOURCE_DIR}/utils.h

cpp/dolfinx/fem/Expression.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "Constant.h"
1010
#include "Function.h"
11+
#include "pack.h"
12+
#include "utils.h"
1113
#include <algorithm>
1214
#include <array>
1315
#include <concepts>
@@ -20,9 +22,6 @@
2022

2123
namespace dolfinx::fem
2224
{
23-
template <dolfinx::scalar T>
24-
class Constant;
25-
2625
/// @brief Represents a mathematical expression evaluated at a
2726
/// pre-defined set of points on the reference cell.
2827
///
@@ -167,8 +166,21 @@ class Expression
167166
throw std::runtime_error("Invalid dimension of evaluation points.");
168167

169168
// Prepare coefficients and constants
170-
auto [coeffs, cstride] = pack_coefficients(*this, entities, estride);
171-
std::vector<scalar_type> constant_data = pack_constants(*this);
169+
std::vector<T> coeffs((entities.size() / estride)
170+
* this->coefficient_offsets().back());
171+
int cstride = this->coefficient_offsets().back();
172+
{
173+
std::vector<
174+
std::reference_wrapper<const Function<scalar_type, geometry_type>>>
175+
c;
176+
std::ranges::transform(this->coefficients(), std::back_inserter(c),
177+
[](auto c) -> const Function<T, U>&
178+
{ return *c; });
179+
fem::pack_coefficients(c, this->coefficient_offsets(), entities, estride,
180+
std::span(coeffs));
181+
}
182+
std::vector<scalar_type> constant_data = fem::pack_constants(*this);
183+
172184
auto fn = this->get_tabulate_expression();
173185

174186
// Prepare cell geometry

cpp/dolfinx/fem/assembler.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "assemble_matrix_impl.h"
1010
#include "assemble_scalar_impl.h"
1111
#include "assemble_vector_impl.h"
12+
#include "pack.h"
1213
#include "traits.h"
1314
#include "utils.h"
1415
#include <algorithm>
@@ -38,10 +39,10 @@ make_coefficients_span(const std::map<std::pair<IntegralType, int>,
3839
{
3940
using Key = typename std::remove_reference_t<decltype(coeffs)>::key_type;
4041
std::map<Key, std::pair<std::span<const T>, int>> c;
41-
std::ranges::transform(coeffs, std::inserter(c, c.end()),
42-
[](auto& e) -> typename decltype(c)::value_type {
43-
return {e.first, {e.second.first, e.second.second}};
44-
});
42+
std::ranges::transform(
43+
coeffs, std::inserter(c, c.end()),
44+
[](auto& e) -> typename decltype(c)::value_type
45+
{ return {e.first, {e.second.first, e.second.second}}; });
4546
return c;
4647
}
4748

0 commit comments

Comments
 (0)