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/fem/DofMap.h>
15 #include <dolfinx/fem/FiniteElement.h>
16 #include <dolfinx/la/PETScVector.h>
17 #include <dolfinx/la/Vector.h>
18 #include <dolfinx/mesh/Geometry.h>
19 #include <dolfinx/mesh/Mesh.h>
20 #include <dolfinx/mesh/Topology.h>
29 #include <xtensor/xadapt.hpp>
30 #include <xtensor/xtensor.hpp>
31 #include <xtl/xspan.hpp>
51 explicit Function(std::shared_ptr<const FunctionSpace> V)
52 : _id(common::UniqueIdGenerator::
id()), _function_space(V),
53 _x(std::make_shared<la::Vector<T>>(V->dofmap()->index_map,
54 V->dofmap()->index_map_bs()))
56 if (!V->component().empty())
58 throw std::runtime_error(
"Cannot create Function from subspace. Consider "
59 "collapsing the function space");
69 Function(std::shared_ptr<const FunctionSpace> V,
71 : _id(common::UniqueIdGenerator::
id()), _function_space(V), _x(
x)
78 assert(V->dofmap()->index_map->size_global() * V->dofmap()->index_map_bs()
79 <= _x->bs() * _x->map()->size_global());
87 :
name(std::move(v.
name)), _id(std::move(v._id)),
88 _function_space(std::move(v._function_space)), _x(std::move(v._x)),
89 _petsc_vector(std::exchange(v._petsc_vector, nullptr))
97 VecDestroy(&_petsc_vector);
103 name = std::move(v.name);
104 _id = std::move(v._id);
105 _function_space = std::move(v._function_space);
106 _x = std::move(v._x);
107 std::swap(_petsc_vector, v._petsc_vector);
120 auto sub_space = _function_space->
sub({i});
131 const auto [function_space_new, collapsed_map]
135 assert(function_space_new);
136 auto vector_new = std::make_shared<la::Vector<T>>(
137 function_space_new->dofmap()->index_map,
138 function_space_new->dofmap()->index_map_bs());
141 const std::vector<T>& x_old = _x->array();
142 std::vector<T>& x_new = vector_new->mutable_array();
143 for (std::size_t i = 0; i < collapsed_map.size(); ++i)
145 assert((
int)i < x_new.size());
146 assert(collapsed_map[i] < x_old.size());
147 x_new[i] = x_old[collapsed_map[i]];
150 return Function(function_space_new, vector_new);
157 return _function_space;
166 assert(_function_space->dofmap());
167 assert(_function_space->dofmap()->index_map);
168 if (_x->bs() * _x->map()->size_global()
169 != _function_space->dofmap()->index_map->size_global()
170 * _function_space->dofmap()->index_map_bs())
172 throw std::runtime_error(
173 "Cannot access a non-const vector from a subfunction");
177 if constexpr (std::is_same<T, PetscScalar>::value)
182 *_function_space->dofmap()->index_map,
183 _function_space->dofmap()->index_map_bs(), _x->mutable_array());
186 return _petsc_vector;
190 throw std::runtime_error(
191 "Cannot return PETSc vector wrapper. Type mismatch");
196 std::shared_ptr<const la::Vector<T>>
x()
const {
return _x; }
199 std::shared_ptr<la::Vector<T>>
x() {
return _x; }
208 const std::function<xt::xarray<T>(
const xt::xtensor<double, 2>&)>& f)
210 assert(_function_space);
211 assert(_function_space->element());
212 assert(_function_space->mesh());
213 const int tdim = _function_space->mesh()->topology().dim();
214 auto cell_map = _function_space->mesh()->topology().index_map(tdim);
216 const int num_cells = cell_map->size_local() + cell_map->num_ghosts();
217 std::vector<std::int32_t> cells(num_cells, 0);
218 std::iota(cells.begin(), cells.end(), 0);
222 *_function_space->element(), *_function_space->mesh(), cells);
236 void eval(
const xt::xtensor<double, 2>&
x,
237 const xtl::span<const std::int32_t>& cells,
238 xt::xtensor<T, 2>& u)
const
243 if (
x.shape(0) != cells.size())
245 throw std::runtime_error(
246 "Number of points and number of cells must be equal.");
248 if (
x.shape(0) != u.shape(0))
250 throw std::runtime_error(
251 "Length of array for Function values must be the "
252 "same as the number of points.");
256 assert(_function_space);
257 std::shared_ptr<const mesh::Mesh> mesh = _function_space->mesh();
259 const std::size_t gdim = mesh->geometry().dim();
260 const std::size_t tdim = mesh->topology().dim();
261 auto map = mesh->topology().index_map(tdim);
265 = mesh->geometry().dofmap();
267 const std::size_t num_dofs_g = x_dofmap.
num_links(0);
268 const xt::xtensor<double, 2>& x_g = mesh->geometry().x();
274 assert(_function_space->element());
275 std::shared_ptr<const fem::FiniteElement> element
276 = _function_space->element();
278 const int bs_element = element->block_size();
279 const std::size_t reference_value_size
280 = element->reference_value_size() / bs_element;
281 const std::size_t value_size = element->value_size() / bs_element;
282 const std::size_t space_dimension = element->space_dimension() / bs_element;
286 const int num_sub_elements = element->num_sub_elements();
287 if (num_sub_elements > 1 and num_sub_elements != bs_element)
289 throw std::runtime_error(
"Function::eval is not supported for mixed "
290 "elements. Extract subspaces.");
294 xt::xtensor<double, 2> X({1, tdim});
295 xt::xtensor<double, 3> J
296 = xt::zeros<double>({
static_cast<std::size_t
>(1), gdim, tdim});
297 xt::xtensor<double, 3> K
298 = xt::zeros<double>({
static_cast<std::size_t
>(1), tdim, gdim});
299 xt::xtensor<double, 1> detJ = xt::zeros<double>({1});
302 xt::xtensor<double, 4> basis_derivatives_reference_values(
303 {1, 1, space_dimension, reference_value_size});
304 auto basis_reference_values = xt::view(basis_derivatives_reference_values,
305 0, xt::all(), xt::all(), xt::all());
306 xt::xtensor<double, 3> basis_values(
307 {
static_cast<std::size_t
>(1), space_dimension, value_size});
310 std::vector<T> coefficients(space_dimension * bs_element);
313 std::shared_ptr<const fem::DofMap> dofmap = _function_space->dofmap();
315 const int bs_dof = dofmap->bs();
317 mesh->topology_mutable().create_entity_permutations();
318 const std::vector<std::uint32_t>& cell_info
319 = mesh->topology().get_cell_permutation_info();
320 xt::xtensor<double, 2> coordinate_dofs
321 = xt::zeros<double>({num_dofs_g, gdim});
322 xt::xtensor<double, 2> xp
323 = xt::zeros<double>({
static_cast<std::size_t
>(1), gdim});
326 std::fill(u.data(), u.data() + u.size(), 0.0);
327 const std::vector<T>& _v = _x->mutable_array();
329 const std::function<void(
const xtl::span<double>&,
330 const xtl::span<const std::uint32_t>&,
332 apply_dof_transformation
333 = element->get_dof_transformation_function<
double>();
335 for (std::size_t p = 0; p < cells.size(); ++p)
337 const int cell_index = cells[p];
344 auto x_dofs = x_dofmap.
links(cell_index);
345 for (std::size_t i = 0; i < num_dofs_g; ++i)
346 for (std::size_t j = 0; j < gdim; ++j)
347 coordinate_dofs(i, j) = x_g(x_dofs[i], j);
349 for (std::size_t j = 0; j < gdim; ++j)
353 cmap.
pull_back(X, J, detJ, K, xp, coordinate_dofs);
356 element->tabulate(basis_derivatives_reference_values, X, 0);
359 apply_dof_transformation(xtl::span(basis_reference_values.data(),
360 basis_reference_values.size()),
361 cell_info, cell_index, reference_value_size);
364 element->transform_reference_basis(basis_values, basis_reference_values,
368 xtl::span<const std::int32_t> dofs = dofmap->cell_dofs(cell_index);
369 for (std::size_t i = 0; i < dofs.size(); ++i)
370 for (
int k = 0; k < bs_dof; ++k)
371 coefficients[bs_dof * i + k] = _v[bs_dof * dofs[i] + k];
374 auto u_row = xt::row(u, p);
375 for (
int k = 0; k < bs_element; ++k)
377 for (std::size_t i = 0; i < space_dimension; ++i)
379 for (std::size_t j = 0; j < value_size; ++j)
381 u_row[j * bs_element + k]
382 += coefficients[bs_element * i + k] * basis_values(0, i, j);
393 assert(_function_space);
394 std::shared_ptr<const mesh::Mesh> mesh = _function_space->mesh();
396 const int tdim = mesh->topology().dim();
399 const std::size_t value_size_loc = _function_space->element()->value_size();
402 xt::xtensor<T, 2> point_values(
403 {mesh->geometry().
x().shape(0), value_size_loc});
407 = mesh->geometry().dofmap();
410 const int num_dofs_g = x_dofmap.
num_links(0);
411 const xt::xtensor<double, 2>& x_g = mesh->geometry().x();
415 auto map = mesh->topology().index_map(tdim);
417 const std::int32_t num_cells = map->size_local() + map->num_ghosts();
419 std::vector<std::int32_t> cells(x_g.shape(0));
420 for (std::int32_t c = 0; c < num_cells; ++c)
423 xtl::span<const std::int32_t> dofs = x_dofmap.links(c);
424 for (
int i = 0; i < num_dofs_g; ++i)
428 eval(x_g, cells, point_values);
437 std::size_t
id()
const {
return _id; }
444 std::shared_ptr<const FunctionSpace> _function_space;
447 std::shared_ptr<la::Vector<T>> _x;
450 mutable Vec _petsc_vector =
nullptr;