DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
vtk_utils.h
1// Copyright (C) 2020-2026 Garth N. Wells and Jørgen S. Dokken
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include "cells.h"
10#include <algorithm>
11#include <array>
12#include <basix/mdspan.hpp>
13#include <concepts>
14#include <cstdint>
15#include <dolfinx/common/IndexMap.h>
16#include <dolfinx/fem/DofMap.h>
17#include <dolfinx/fem/FiniteElement.h>
18#include <dolfinx/fem/FunctionSpace.h>
19#include <dolfinx/mesh/Geometry.h>
20#include <dolfinx/mesh/Mesh.h>
21#include <dolfinx/mesh/Topology.h>
22#include <span>
23#include <tuple>
24#include <vector>
25
26namespace dolfinx
27{
28namespace fem
29{
30template <std::floating_point T>
31class FunctionSpace;
32}
33
34namespace mesh
35{
36enum class CellType : std::int8_t;
37template <std::floating_point T>
38class Geometry;
39} // namespace mesh
40
41namespace io
42{
43namespace impl
44{
56template <typename T>
57std::tuple<std::vector<T>, std::array<std::size_t, 2>,
58 std::vector<std::int64_t>, std::vector<std::uint8_t>>
59tabulate_lagrange_dof_coordinates(const fem::FunctionSpace<T>& V)
60{
61 auto mesh = V.mesh();
62 assert(mesh);
63 auto topology = mesh->topology();
64 assert(topology);
65 const std::size_t gdim = mesh->geometry().dim();
66 const int tdim = topology->dim();
67
68 // Get dofmap data
69 auto dofmap = V.dofmap();
70 assert(dofmap);
71 auto map_dofs = dofmap->index_map;
72 assert(map_dofs);
73 const int index_map_bs = dofmap->index_map_bs();
74 const int dofmap_bs = dofmap->bs();
75
76 // Get element data
77 auto element = V.element();
78 assert(element);
79 const int e_bs = element->block_size();
80 const std::size_t scalar_dofs = element->space_dimension() / e_bs;
81 const std::size_t num_nodes
82 = index_map_bs * (map_dofs->size_local() + map_dofs->num_ghosts())
83 / dofmap_bs;
84
85 // Get the dof coordinates on the reference element and the mesh
86 // coordinate map
87 const auto [X, Xshape] = element->interpolation_points();
88 const fem::CoordinateElement<T>& cmap = mesh->geometry().cmaps().front();
89
90 // Prepare cell geometry
91 auto dofmap_x = mesh->geometry().dofmaps().front();
92 std::span<const T> x_g = mesh->geometry().x();
93 const std::size_t num_dofs_g = cmap.dim();
94
95 std::span<const std::uint32_t> cell_info;
96 if (element->needs_dof_transformations())
97 {
98 mesh->topology_mutable()->create_cell_permutations();
99 cell_info = std::span(mesh->topology()->get_cell_permutation_info());
100 }
101
102 using mdspan2_t = md::mdspan<T, md::dextents<std::size_t, 2>>;
103 using cmdspan4_t = md::mdspan<T, md::dextents<std::size_t, 4>>;
104
105 // Tabulate basis functions at node reference coordinates
106 const std::array<std::size_t, 4> phi_shape
107 = cmap.tabulate_shape(0, Xshape[0]);
108 std::vector<T> phi_b(
109 std::reduce(phi_shape.begin(), phi_shape.end(), 1, std::multiplies{}));
110 cmdspan4_t phi_full(phi_b.data(), phi_shape);
111 cmap.tabulate(0, X, Xshape, phi_b);
112 auto phi = md::submdspan(phi_full, 0, md::full_extent, md::full_extent, 0);
113
114 // Loop over cells and tabulate dofs
115 auto map = topology->index_map(tdim);
116 assert(map);
117 const std::int32_t num_cells = map->size_local() + map->num_ghosts();
118 std::vector<T> x_b(scalar_dofs * gdim);
119 mdspan2_t x(x_b.data(), scalar_dofs, gdim);
120 std::vector<T> coordinate_dofs_b(num_dofs_g * gdim);
121 mdspan2_t coordinate_dofs(coordinate_dofs_b.data(), num_dofs_g, gdim);
122
123 std::vector<T> coords(num_nodes * 3, 0.0);
124 std::array<std::size_t, 2> cshape = {num_nodes, 3};
125 // Transformation from reference element basis function data to
126 // conforming element basis function. This function tabulates Lagrange
127 // (point-evaluation) dof coordinates, which never need a DOF
128 // transformation, so this is always a no-op (nullptr) closure.
129 auto apply_dof_transformation
130 = element->template dof_transformation_fn<T>(fem::doftransform::standard);
131 for (std::int32_t c = 0; c < num_cells; ++c)
132 {
133 // Extract cell geometry
134 for (std::size_t i = 0; i < dofmap_x.extent(1); ++i)
135 for (std::size_t j = 0; j < gdim; ++j)
136 coordinate_dofs(i, j) = x_g[3 * dofmap_x(c, i) + j];
137
138 // Tabulate dof coordinates on cell
139 cmap.push_forward(x, coordinate_dofs, phi);
140 if (apply_dof_transformation)
141 {
142 apply_dof_transformation(
143 x_b, std::span(cell_info.data(), cell_info.size()), c, x.extent(1));
144 }
145
146 // Copy dof coordinates into vector
147 auto dofs = dofmap->cell_dofs(c);
148 for (std::size_t i = 0; i < dofs.size(); ++i)
149 {
150 std::int32_t dof = dofs[i];
151 for (std::size_t j = 0; j < gdim; ++j)
152 coords[3 * dof + j] = x(i, j);
153 }
154 }
155
156 // Original point IDs
157 std::vector<std::int64_t> x_id(num_nodes);
158 std::array<std::int64_t, 2> range = map_dofs->local_range();
159 std::int32_t size_local = range[1] - range[0];
160 std::iota(x_id.begin(), std::next(x_id.begin(), size_local), range[0]);
161 std::span ghosts = map_dofs->ghosts();
162 std::ranges::copy(ghosts, std::next(x_id.begin(), size_local));
163
164 // Ghosts
165 std::vector<std::uint8_t> id_ghost(num_nodes, 0);
166 std::fill(std::next(id_ghost.begin(), size_local), id_ghost.end(), 1);
167
168 return {std::move(coords), cshape, std::move(x_id), std::move(id_ghost)};
169}
170
173template <std::floating_point T>
174bool is_cellwise(const fem::FiniteElement<T>& e)
175{
176 return e.space_dimension() / e.block_size() == 1;
177}
178
190template <std::floating_point T>
191bool same_base_element(const fem::FiniteElement<T>& e0,
192 const fem::FiniteElement<T>& e1)
193{
194 return e0.basix_element() == e1.basix_element();
195}
196
197} // namespace impl
198
213template <typename T>
214std::tuple<std::vector<T>, std::array<std::size_t, 2>,
215 std::vector<std::int64_t>, std::vector<std::uint8_t>,
216 std::vector<std::int64_t>, std::array<std::size_t, 2>>
218{
219 auto mesh = V.mesh();
220 assert(mesh);
221 auto topology = mesh->topology();
222 assert(topology);
223 const int tdim = topology->dim();
224
225 assert(V.element());
226 if (V.element()->is_mixed())
227 throw std::runtime_error("Can't create VTK mesh from a mixed element");
228
229 const auto [x, xshape, x_id, x_ghost]
230 = impl::tabulate_lagrange_dof_coordinates(V);
231 auto map = topology->index_map(tdim);
232 const std::size_t num_cells = map->size_local() + map->num_ghosts();
233
234 // Create permutation from DOLFINx dof ordering to VTK
235 auto dofmap = V.dofmap();
236 assert(dofmap);
237 const int element_block_size = V.element()->block_size();
238 const std::uint32_t num_nodes
239 = V.element()->space_dimension() / element_block_size;
240 const std::vector<std::uint16_t> vtkmap = io::cells::transpose(
241 io::cells::perm_vtk(topology->cell_type(), num_nodes));
242
243 // Extract topology for all local cells as
244 // [v0_0, ...., v0_N0, v1_0, ...., v1_N1, ....]
245 std::array<std::size_t, 2> shape = {num_cells, num_nodes};
246 std::vector<std::int64_t> vtk_topology(shape[0] * shape[1]);
247 for (std::size_t c = 0; c < shape[0]; ++c)
248 {
249 auto dofs = dofmap->cell_dofs(c);
250 for (std::size_t i = 0; i < dofs.size(); ++i)
251 vtk_topology[c * shape[1] + i] = dofs[vtkmap[i]];
252 }
253
254 return {std::move(x),
255 xshape,
256 std::move(x_id),
257 std::move(x_ghost),
258 std::move(vtk_topology),
259 shape};
260}
261
276std::pair<std::vector<std::int64_t>, std::array<std::size_t, 2>>
278 md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>> dofmap_x,
279 mesh::CellType cell_type);
280} // namespace io
281} // namespace dolfinx
Degree-of-freedom map representations and tools.
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:36
std::shared_ptr< const DofMap > dofmap() const
The dofmap.
Definition FunctionSpace.h:408
std::shared_ptr< const mesh::Mesh< geometry_type > > mesh() const
The mesh.
Definition FunctionSpace.h:383
std::shared_ptr< const FiniteElement< geometry_type > > element() const
The finite element.
Definition FunctionSpace.h:389
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:39
Finite element method functionality.
Definition assemble_expression_impl.h:28
@ standard
Standard.
Definition FiniteElement.h:33
FunctionSpace(U mesh, V element, W dofmap) -> FunctionSpace< typename std::remove_cvref< typename U::element_type >::type::geometry_type::value_type >
Type deduction.
std::vector< std::uint16_t > perm_vtk(mesh::CellType type, int num_nodes)
Permutation array to map from VTK to DOLFINx node ordering.
Definition cells.cpp:556
std::vector< std::uint16_t > transpose(std::span< const std::uint16_t > map)
Compute the transpose of a re-ordering map.
Definition cells.cpp:720
Support for file IO.
Definition ADIOS2Writers.h:44
std::tuple< std::vector< T >, std::array< std::size_t, 2 >, std::vector< std::int64_t >, std::vector< std::uint8_t >, std::vector< std::int64_t >, std::array< std::size_t, 2 > > vtk_mesh_from_space(const fem::FunctionSpace< T > &V)
Given a FunctionSpace, create a topology and geometry based on the dof coordinates.
Definition vtk_utils.h:217
std::pair< std::vector< std::int64_t >, std::array< std::size_t, 2 > > extract_vtk_connectivity(md::mdspan< const std::int32_t, md::dextents< std::size_t, 2 > > dofmap_x, mesh::CellType cell_type)
Extract the cell topology (connectivity) in VTK ordering for all cells the mesh. The 'topology' inclu...
Definition vtk_utils.cpp:23
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
CellType
Cell type identifier.
Definition cell_types.h:24
Top-level namespace.
Definition defines.h:12