9 #include "FunctionSpace.h"
10 #include "interpolate.h"
11 #include <dolfinx/common/IndexMap.h>
12 #include <dolfinx/common/UniqueIdGenerator.h>
13 #include <dolfinx/common/array2d.h>
14 #include <dolfinx/common/span.hpp>
15 #include <dolfinx/fem/DofMap.h>
16 #include <dolfinx/fem/FiniteElement.h>
17 #include <dolfinx/la/PETScVector.h>
18 #include <dolfinx/la/Vector.h>
19 #include <dolfinx/mesh/Geometry.h>
20 #include <dolfinx/mesh/Mesh.h>
21 #include <dolfinx/mesh/Topology.h>
30 #include <xtensor/xtensor.hpp>
50 explicit Function(std::shared_ptr<const FunctionSpace> V)
51 : _id(common::UniqueIdGenerator::
id()), _function_space(V),
52 _x(std::make_shared<la::Vector<T>>(V->dofmap()->index_map,
53 V->dofmap()->index_map_bs()))
55 if (!V->component().empty())
57 throw std::runtime_error(
"Cannot create Function from subspace. Consider "
58 "collapsing the function space");
68 Function(std::shared_ptr<const FunctionSpace> V,
70 : _id(common::UniqueIdGenerator::
id()), _function_space(V), _x(
x)
77 assert(V->dofmap()->index_map->size_global() * V->dofmap()->index_map_bs()
78 <= _x->bs() * _x->map()->size_global());
86 :
name(std::move(v.
name)), _id(std::move(v._id)),
87 _function_space(std::move(v._function_space)), _x(std::move(v._x)),
88 _petsc_vector(std::exchange(v._petsc_vector, nullptr))
96 VecDestroy(&_petsc_vector);
102 name = std::move(v.name);
103 _id = std::move(v._id);
104 _function_space = std::move(v._function_space);
105 _x = std::move(v._x);
106 std::swap(_petsc_vector, v._petsc_vector);
119 auto sub_space = _function_space->
sub({i});
130 const auto [function_space_new, collapsed_map]
134 assert(function_space_new);
135 auto vector_new = std::make_shared<la::Vector<T>>(
136 function_space_new->dofmap()->index_map,
137 function_space_new->dofmap()->index_map_bs());
140 const std::vector<T>& x_old = _x->array();
141 std::vector<T>& x_new = vector_new->mutable_array();
142 for (std::size_t i = 0; i < collapsed_map.size(); ++i)
144 assert((
int)i < x_new.size());
145 assert(collapsed_map[i] < x_old.size());
146 x_new[i] = x_old[collapsed_map[i]];
149 return Function(function_space_new, vector_new);
156 return _function_space;
165 assert(_function_space->dofmap());
166 assert(_function_space->dofmap()->index_map);
167 if (_x->bs() * _x->map()->size_global()
168 != _function_space->dofmap()->index_map->size_global()
169 * _function_space->dofmap()->index_map_bs())
171 throw std::runtime_error(
172 "Cannot access a non-const vector from a subfunction");
176 if constexpr (std::is_same<T, PetscScalar>::value)
181 *_function_space->dofmap()->index_map,
182 _function_space->dofmap()->index_map_bs(), _x->mutable_array());
185 return _petsc_vector;
189 throw std::runtime_error(
190 "Cannot return PETSc vector wrapper. Type mismatch");
195 std::shared_ptr<const la::Vector<T>>
x()
const {
return _x; }
198 std::shared_ptr<la::Vector<T>>
x() {
return _x; }
207 const std::function<xt::xarray<T>(
const xt::xtensor<double, 2>&)>& f)
209 assert(_function_space);
210 assert(_function_space->element());
211 assert(_function_space->mesh());
212 const int tdim = _function_space->mesh()->topology().dim();
213 auto cell_map = _function_space->mesh()->topology().index_map(tdim);
215 const int num_cells = cell_map->size_local() + cell_map->num_ghosts();
216 std::vector<std::int32_t> cells(num_cells, 0);
217 std::iota(cells.begin(), cells.end(), 0);
221 *_function_space->element(), *_function_space->mesh(), cells);
236 const tcb::span<const std::int32_t>& cells,
array2d<T>& u)
const
241 if (
x.shape[0] != cells.size())
243 throw std::runtime_error(
244 "Number of points and number of cells must be equal.");
246 if (
x.shape[0] != u.
shape[0])
248 throw std::runtime_error(
249 "Length of array for Function values must be the "
250 "same as the number of points.");
254 assert(_function_space);
255 std::shared_ptr<const mesh::Mesh> mesh = _function_space->mesh();
257 const int gdim = mesh->geometry().dim();
258 const int tdim = mesh->topology().dim();
259 auto map = mesh->topology().index_map(tdim);
263 = mesh->geometry().dofmap();
265 const int num_dofs_g = x_dofmap.
num_links(0);
272 assert(_function_space->element());
273 std::shared_ptr<const fem::FiniteElement> element
274 = _function_space->element();
276 const int bs_element = element->block_size();
277 const int reference_value_size
278 = element->reference_value_size() / bs_element;
279 const int value_size = element->value_size() / bs_element;
280 const int space_dimension = element->space_dimension() / bs_element;
284 const int num_sub_elements = element->num_sub_elements();
285 if (num_sub_elements > 1 and num_sub_elements != bs_element)
287 throw std::runtime_error(
"Function::eval is not supported for mixed "
288 "elements. Extract subspaces.");
292 std::vector<double> J(gdim * tdim);
293 std::array<double, 1> detJ;
294 std::vector<double> K(tdim * gdim);
298 std::vector<double> basis_reference_values(space_dimension
299 * reference_value_size);
300 std::vector<double> basis_values(space_dimension * value_size);
303 std::vector<T> coefficients(space_dimension * bs_element);
306 std::shared_ptr<const fem::DofMap> dofmap = _function_space->dofmap();
308 const int bs_dof = dofmap->bs();
310 mesh->topology_mutable().create_entity_permutations();
311 const std::vector<std::uint32_t>& cell_info
312 = mesh->topology().get_cell_permutation_info();
319 const std::vector<T>& _v = _x->mutable_array();
320 for (std::size_t p = 0; p < cells.size(); ++p)
322 const int cell_index = cells[p];
329 auto x_dofs = x_dofmap.
links(cell_index);
330 for (
int i = 0; i < num_dofs_g; ++i)
331 for (
int j = 0; j < gdim; ++j)
332 coordinate_dofs(i, j) = x_g(x_dofs[i], j);
334 for (
int j = 0; j < gdim; ++j)
341 element->evaluate_reference_basis(basis_reference_values, X);
344 element->apply_dof_transformation(basis_reference_values.data(),
345 cell_info[cell_index],
346 reference_value_size);
349 element->transform_reference_basis(basis_values, basis_reference_values,
353 tcb::span<const std::int32_t> dofs = dofmap->cell_dofs(cell_index);
354 for (std::size_t i = 0; i < dofs.size(); ++i)
355 for (
int k = 0; k < bs_dof; ++k)
356 coefficients[bs_dof * i + k] = _v[bs_dof * dofs[i] + k];
359 auto u_row = u.
row(p);
360 for (
int k = 0; k < bs_element; ++k)
362 for (
int i = 0; i < space_dimension; ++i)
364 for (
int j = 0; j < value_size; ++j)
366 u_row[j * bs_element + k] += coefficients[bs_element * i + k]
367 * basis_values[i * value_size + j];
378 assert(_function_space);
379 std::shared_ptr<const mesh::Mesh> mesh = _function_space->mesh();
381 const int tdim = mesh->topology().dim();
384 const int value_size_loc = _function_space->element()->value_size();
387 array2d<T> point_values(mesh->geometry().x().shape[0], value_size_loc);
391 = mesh->geometry().dofmap();
394 const int num_dofs_g = x_dofmap.
num_links(0);
399 auto map = mesh->topology().index_map(tdim);
401 const std::int32_t num_cells = map->size_local() + map->num_ghosts();
403 std::vector<std::int32_t> cells(x_g.
shape[0]);
404 for (std::int32_t c = 0; c < num_cells; ++c)
407 tcb::span<const std::int32_t> dofs = x_dofmap.
links(c);
408 for (
int i = 0; i < num_dofs_g; ++i)
412 eval(x_g, cells, point_values);
421 std::size_t
id()
const {
return _id; }
428 std::shared_ptr<const FunctionSpace> _function_space;
431 std::shared_ptr<la::Vector<T>> _x;
434 mutable Vec _petsc_vector =
nullptr;
This class provides a dynamic 2-dimensional row-wise array data structure.
Definition: array2d.h:21
std::array< size_type, 2 > shape
The shape of the array.
Definition: array2d.h:157
constexpr tcb::span< value_type > row(size_type i)
Access a row in the array.
Definition: array2d.h:116
constexpr value_type * data() noexcept
Get pointer to the first element of the underlying storage.
Definition: array2d.h:133
constexpr size_type size() const noexcept
Returns the number of elements in the array.
Definition: array2d.h:144
This class manages coordinate mappings for isoparametric cells.
Definition: CoordinateElement.h:31
void compute_reference_geometry(array2d< double > &X, std::vector< double > &J, tcb::span< double > detJ, std::vector< double > &K, const array2d< double > &x, const array2d< double > &cell_geometry) const
Compute reference coordinates X, and J, detJ and K for physical coordinates x.
Definition: CoordinateElement.cpp:83
This class represents a function in a finite element function space , given by.
Definition: Function.h:46
Vec vector() const
Return vector of expansion coefficients as a PETSc Vec. Throws an exception a PETSc Vec cannot be cre...
Definition: Function.h:162
Function(std::shared_ptr< const FunctionSpace > V, std::shared_ptr< la::Vector< T >> x)
Create function on given function space with a given vector.
Definition: Function.h:68
void interpolate(const Function< T > &v)
Interpolate a Function (on possibly non-matching meshes)
Definition: Function.h:202
Function & operator=(Function &&v) noexcept
Move assignment.
Definition: Function.h:100
Function(Function &&v)
Move constructor.
Definition: Function.h:85
void eval(const array2d< double > &x, const tcb::span< const std::int32_t > &cells, array2d< T > &u) const
Evaluate the Function at points.
Definition: Function.h:235
std::shared_ptr< la::Vector< T > > x()
Underlying vector.
Definition: Function.h:198
std::shared_ptr< const FunctionSpace > function_space() const
Return shared pointer to function space.
Definition: Function.h:154
virtual ~Function()
Destructor.
Definition: Function.h:93
std::size_t id() const
ID.
Definition: Function.h:421
array2d< T > compute_point_values() const
Compute values at all mesh 'nodes'.
Definition: Function.h:376
std::string name
Name.
Definition: Function.h:418
std::shared_ptr< const la::Vector< T > > x() const
Underlying vector.
Definition: Function.h:195
Function collapse() const
Collapse a subfunction (view into the Function) to a stand-alone Function.
Definition: Function.h:127
Function(std::shared_ptr< const FunctionSpace > V)
Create function on given function space.
Definition: Function.h:50
void interpolate(const std::function< xt::xarray< T >(const xt::xtensor< double, 2 > &)> &f)
Interpolate an expression.
Definition: Function.h:206
Function sub(int i) const
Extract subfunction (view into the Function)
Definition: Function.h:117
This class provides a static adjacency list data structure. It is commonly used to store directed gra...
Definition: AdjacencyList.h:68
tcb::span< T > links(int node)
Get the links (edges) for given node.
Definition: AdjacencyList.h:151
int num_links(int node) const
Number of connections for given node.
Definition: AdjacencyList.h:141
Distributed vector.
Definition: Vector.h:19
Finite element method functionality.
Definition: assemble_matrix_impl.h:23
xt::xtensor< double, 2 > interpolation_coords(const fem::FiniteElement &element, const mesh::Mesh &mesh, const tcb::span< const std::int32_t > &cells)
Compute the evaluation points in the physical space at which an expression should be computed to inte...
Definition: interpolate.cpp:17
void interpolate(Function< T > &u, const Function< T > &v)
Interpolate a finite element Function (on possibly non-matching meshes) in another finite element spa...
Definition: interpolate.h:143
Vec create_ghosted_vector(const common::IndexMap &map, int bs, tcb::span< PetscScalar > x)
Create a PETSc Vec that wraps the data in an array.
Definition: PETScVector.cpp:64