/home/runner/work/kynema/kynema/kynema/src/math/vector_operations.hpp Source File

Kynema API: /home/runner/work/kynema/kynema/kynema/src/math/vector_operations.hpp Source File
Kynema API
A flexible multibody structural dynamics code for wind turbines
Loading...
Searching...
No Matches
vector_operations.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4
5#include <Kokkos_Core.hpp>
6
7namespace kynema::math {
8
10template <typename VectorType, typename MatrixType>
11KOKKOS_INLINE_FUNCTION void VecTilde(const VectorType& vector, const MatrixType& matrix) {
12 matrix(0, 0) = 0.;
13 matrix(0, 1) = -vector(2);
14 matrix(0, 2) = vector(1);
15 matrix(1, 0) = vector(2);
16 matrix(1, 1) = 0.;
17 matrix(1, 2) = -vector(0);
18 matrix(2, 0) = -vector(1);
19 matrix(2, 1) = vector(0);
20 matrix(2, 2) = 0.;
21}
22
24template <typename AVectorType, typename BVectorType>
25KOKKOS_INLINE_FUNCTION double DotProduct(const AVectorType& a, const BVectorType& b) {
26 double sum = 0.;
27 for (auto i = 0; i < a.extent_int(0); ++i) {
28 sum += a(i) * b(i);
29 }
30 return sum;
31}
32
34constexpr double DotProduct(const std::array<double, 3>& a, const std::array<double, 3>& b) {
35 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
36}
37
39template <typename VectorType>
40KOKKOS_INLINE_FUNCTION void CrossProduct(
41 const VectorType& a, const VectorType& b, const VectorType& c
42) {
43 c(0) = a(1) * b(2) - a(2) * b(1);
44 c(1) = a(2) * b(0) - a(0) * b(2);
45 c(2) = a(0) * b(1) - a(1) * b(0);
46}
47
49constexpr std::array<double, 3> CrossProduct(
50 std::span<const double, 3> a, std::span<const double, 3> b
51) {
52 return std::array<double, 3>{
53 a[1] * b[2] - a[2] * b[1],
54 a[2] * b[0] - a[0] * b[2],
55 a[0] * b[1] - a[1] * b[0],
56 };
57}
58
60constexpr double Norm(const std::array<double, 3>& v) {
61 return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
62}
63
65constexpr std::array<double, 3> UnitVector(const std::array<double, 3>& v) {
66 const double norm = Norm(v);
67 if (norm == 0.) {
68 throw std::invalid_argument("Cannot normalize a zero vector");
69 }
70 return std::array<double, 3>{
71 v[0] / norm,
72 v[1] / norm,
73 v[2] / norm,
74 };
75}
76} // namespace kynema::math
Definition gll_quadrature.hpp:7
KOKKOS_INLINE_FUNCTION void CrossProduct(const VectorType &a, const VectorType &b, const VectorType &c)
Calculate the cross product between two vector views.
Definition vector_operations.hpp:40
KOKKOS_INLINE_FUNCTION void VecTilde(const VectorType &vector, const MatrixType &matrix)
Converts a 3x1 vector to a 3x3 skew-symmetric matrix and returns the result.
Definition vector_operations.hpp:11
KOKKOS_INLINE_FUNCTION double DotProduct(const AVectorType &a, const BVectorType &b)
Calculate the dot product between two vector views.
Definition vector_operations.hpp:25
constexpr double Norm(const std::array< double, 3 > &v)
Calculate the norm of a given vector.
Definition vector_operations.hpp:60
constexpr std::array< double, 3 > UnitVector(const std::array< double, 3 > &v)
UnitVector returns the unit vector of the given vector.
Definition vector_operations.hpp:65