DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
interpolate_geometry.h
Go to the documentation of this file.
1// Copyright (C) 2013-2026 Johan Hake, Jan Blechta, Garth N. Wells and Paul T.
2// Kühner
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include "CoordinateElement.h"
11#include "DofMap.h"
12#include "FiniteElement.h"
13#include "FunctionSpace.h"
15#include <basix/finite-element.h>
16#include <cstdint>
17#include <dolfinx/common/IndexMap.h>
18#include <dolfinx/graph/AdjacencyList.h>
19#include <dolfinx/mesh/Geometry.h>
20#include <dolfinx/mesh/Mesh.h>
21#include <dolfinx/mesh/Topology.h>
22#include <dolfinx/mesh/cell_types.h>
23#include <functional>
24#include <memory>
25#include <numeric>
26#include <stdexcept>
27#include <utility>
28#include <vector>
29
32
33namespace dolfinx::fem
34{
57template <std::floating_point T>
59 std::shared_ptr<mesh::Mesh<T>> mesh, const CoordinateElement<T>& new_cmap,
60 const std::function<std::vector<int>(
61 const graph::AdjacencyList<std::int32_t>&)>& reorder_fn = nullptr)
62{
63 assert(mesh);
64 const CoordinateElement<T>& old_cmap = mesh->geometry().cmaps().front();
65 if (new_cmap.cell_shape() != old_cmap.cell_shape())
66 {
67 throw std::invalid_argument(
68 "Cell shape of new coordinate element must match input mesh.");
69 }
70
71 const int gdim = mesh->geometry().dim();
72
73 // Build a vector-valued Lagrange FiniteElement from the coordinate
74 // element. It must have the same dof layout as `new_cmap`, i.e. be
75 // discontinuous if and only if `new_cmap` is.
76 basix::FiniteElement<T> b_element = basix::create_element<T>(
77 basix::element::family::P,
78 mesh::cell_type_to_basix_type(new_cmap.cell_shape()), new_cmap.degree(),
79 new_cmap.variant(), basix::element::dpc_variant::unset,
80 new_cmap.is_discontinuous());
81 auto element = std::make_shared<const FiniteElement<T>>(
82 b_element, static_cast<std::size_t>(gdim),
83 std::vector<std::size_t>{static_cast<std::size_t>(gdim)});
84
85 FunctionSpace<T> V = create_functionspace(mesh, element, reorder_fn);
86
87 // Tabulate physical coordinates of the new geometry dofs.
88 std::vector<T> x_new = V.tabulate_dof_coordinates(false);
89
90 // Pull the geometry dofmap and index map from V.
91 std::shared_ptr<const DofMap> dm = V.dofmap();
92 assert(dm);
93 std::shared_ptr<const common::IndexMap> new_imap = dm->index_map;
94 assert(new_imap);
95
96 auto map_view = dm->map();
97 std::vector<std::int32_t> dofmap_flat(
98 map_view.data_handle(), map_view.data_handle() + map_view.size());
99
100 // Build input_global_indices as the local-to-global of the new geometry
101 // dofs.
102 const std::int32_t num_nodes
103 = new_imap->size_local() + new_imap->num_ghosts();
104 std::vector<std::int32_t> local(num_nodes);
105 std::iota(local.begin(), local.end(), 0);
106 std::vector<std::int64_t> igi(num_nodes);
107 new_imap->local_to_global(local, igi);
108
110 new_imap, std::vector<std::vector<std::int32_t>>{std::move(dofmap_flat)},
111 std::vector<CoordinateElement<T>>{new_cmap}, std::move(x_new), gdim,
112 std::move(igi));
113
114 return mesh::Mesh<T>(mesh->comm(), mesh->topology(), std::move(geometry));
115}
116} // namespace dolfinx::fem
Degree-of-freedom map representations and tools.
Definition CoordinateElement.h:27
Definition CoordinateElement.h:39
basix::element::lagrange_variant variant() const
Variant of the element.
Definition CoordinateElement.cpp:230
mesh::CellType cell_shape() const
Cell shape.
Definition CoordinateElement.cpp:46
bool is_discontinuous() const
Check if the element is the discontinuous version of the coordinate element.
Definition CoordinateElement.cpp:244
int degree() const
The polynomial degree of the element.
Definition CoordinateElement.cpp:216
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:35
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:41
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:39
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:25
Factories for finite element dofmaps and function spaces.
Finite element method functionality.
Definition assemble_expression_impl.h:22
FunctionSpace< T > create_functionspace(std::shared_ptr< mesh::Mesh< T > > mesh, std::shared_ptr< const fem::FiniteElement< T > > e, std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> reorder_fn=nullptr)
NEW Create a function space from a fem::FiniteElement.
Definition functionspace_factory.h:105
mesh::Mesh< T > interpolate_geometry(std::shared_ptr< mesh::Mesh< T > > mesh, const CoordinateElement< T > &new_cmap, const std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> &reorder_fn=nullptr)
Take an existing mesh and create a new mesh with its geometry interpolated into a new coordinate elem...
Definition interpolate_geometry.h:58
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:24
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
basix::cell::type cell_type_to_basix_type(CellType celltype)
Convert a cell type to a Basix cell type.
Definition cell_types.cpp:171