Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
tensors_and_points_dimension_manipulation.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2022, 2024 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_tensors_and_points_dimension_manipulation_h
5#define lethe_tensors_and_points_dimension_manipulation_h
6
7#include <deal.II/base/point.h>
8#include <deal.II/base/tensor.h>
9
10using namespace dealii;
11
12/**
13 * Copies a two-dimensional tensor in a three-dimensional tensor. The third
14 * element of the three-dimensional tensor (in z direction) is set to zero.
15 * If the tensor given is already 3d, do nothing and return the tensor.
16 *
17 * @param tensor the tensor that should be transformed in 3d
18 * @return tensor_3d Three-dimensional output tensor
19 *
20 */
21
22template <int dim>
23inline Tensor<1, 3>
24tensor_nd_to_3d(const Tensor<1, dim> &tensor)
25{
26 Tensor<1, 3> tensor_3d;
27 tensor_3d[0] = tensor[0];
28 tensor_3d[1] = tensor[1];
29 if constexpr (dim > 2)
30 tensor_3d[2] = tensor[2];
31
32 if constexpr (dim == 2)
33 tensor_3d[2] = 0;
34
35 return tensor_3d;
36}
37
38/**
39 * Copies a three-dimensional tensor in a two-dimensional tensor. The third
40 * element of the three-dimensional point (in z direction) is neglected.
41 * If the vector is tensor given is two-dimensional, do nothing and return the
42 * tensor.
43 *
44 * @param tensor Three-dimensional input tensor
45 * @return tensor_2d Three-dimensional output tensor
46 *
47 */
48template <int dim>
49inline Tensor<1, 2>
50tensor_nd_to_2d(const Tensor<1, dim> &tensor)
51{
52 Tensor<1, 2> tensor_2d;
53 tensor_2d[0] = tensor[0];
54 tensor_2d[1] = tensor[1];
55 return tensor_2d;
56}
57
58/**
59 * Copies a two-dimensional point in a three-dimensional point. The third
60 * element of the three-dimensional point (in z direction) is set to zero.
61 * If the point given is already 3d, do nothing and return the point.
62 *
63 * @param point Two-dimensional input point
64 * @return point_3d Three-dimensional output point
65 *
66 */
67template <int dim>
68inline Point<3>
69point_nd_to_3d(const Point<dim> &point)
70{
71 Point<3> point_3d;
72 point_3d[0] = point[0];
73 point_3d[1] = point[1];
74 if constexpr (dim > 2)
75 point_3d[2] = point[2];
76 if constexpr (dim == 2)
77 point_3d[2] = 0;
78
79 return point_3d;
80}
81
82/**
83 * Copies a three-dimensional point in a two-dimensional point. The third
84 * element of the three-dimensional point (in z direction) is neglected.
85 * If the point given is already two dimensional, do nothing and return the
86 * point.
87 *
88 * @param point Three-dimensional input point
89 * @return point_2d Two-dimensional output point
90 *
91 */
92template <int dim>
93inline Point<2>
94point_nd_to_2d(const Point<dim> &point)
95{
96 Point<2> point_2d;
97 point_2d[0] = point[0];
98 point_2d[1] = point[1];
99 return point_2d;
100}
101
102/**
103 * @brief Compute the tangential component of a vector on a surface defined by
104 * its unit normal vector, which removes the normal component of the vector.
105 * In 3D, it computes:
106 * \f[
107 * \mathbf{t} = \mathbf{n} \times (\mathbf{v} \times \mathbf{n})
108 * \f]
109 * In 2D, the unit tangent \f$\boldsymbol{\tau}\f$ is the unit normal rotated by
110 * 90 degrees (cross_product_2d), and it computes:
111 * \f[
112 * \mathbf{t} = (\mathbf{v} \cdot \boldsymbol{\tau}) \boldsymbol{\tau}
113 * \f]
114 * Both expressions are equal to
115 * \f$\mathbf{v} - (\mathbf{v} \cdot \mathbf{n}) \mathbf{n}\f$.
116 *
117 * @note This operation gives the tangential trace of a field, for instance the
118 * traces in \f$H^{-1/2}(\mathrm{curl})\f$, where only the tangential
119 * components are continuous across interfaces.
120 *
121 * @tparam dim Spatial dimension
122 * @tparam Number Type of the vector
123 *
124 * @param[in] tensor Vector whose tangential component is computed.
125 * @param[in] normal Unit normal vector of the surface.
126 *
127 * @return The tangential component of the vector.
128 */
129template <int dim, typename Number>
130DEAL_II_ALWAYS_INLINE inline Tensor<1, dim, Number>
131tangential_component(const Tensor<1, dim, Number> &tensor,
132 const Tensor<1, dim> &normal)
133{
134 static_assert(dim == 2 || dim == 3,
135 "The tangential component is only defined in 2D and 3D.");
136 if constexpr (dim == 2)
137 {
138 // In 2D, the unit tangent is the unit normal rotated by 90 degrees
139 const Tensor<1, dim> tangent = cross_product_2d(normal);
140 return (tensor * tangent) * tangent;
141 }
142 else
143 return cross_product_3d(normal, cross_product_3d(tensor, normal));
144}
145
146#endif
DEAL_II_ALWAYS_INLINE Tensor< 1, dim, Number > tangential_component(const Tensor< 1, dim, Number > &tensor, const Tensor< 1, dim > &normal)
Compute the tangential component of a vector on a surface defined by its unit normal vector,...
Tensor< 1, 3 > tensor_nd_to_3d(const Tensor< 1, dim > &tensor)
Point< 2 > point_nd_to_2d(const Point< dim > &point)
Tensor< 1, 2 > tensor_nd_to_2d(const Tensor< 1, dim > &tensor)
Point< 3 > point_nd_to_3d(const Point< dim > &point)