ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
orthonormal_vec.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "utils/Vector.hpp"
23
24#include <cstddef>
25#include <numbers>
26
27namespace Utils {
28/**
29 * @brief Return a vector that is orthonormal to vec
30 */
31template <typename T, std::size_t N>
33 /* Calculate orthonormal vector using Gram-Schmidt orthogonalization of a
34 trial vector. Only works if the trial vector is not parallel, so we have to
35 try a second one in that case
36 */
37 Vector<Vector<T, N>, 2> try_vectors = {Vector<T, N>::broadcast(T(0)),
39 try_vectors[0][0] = T(1);
40 try_vectors[1][1] = T(1);
41
42 Vector<T, N> ret;
43 for (auto v : try_vectors) {
44 auto orth_component = v - (v * vec) / vec.norm2() * vec;
45 auto norm = orth_component.norm();
46 if (norm >= 1. / std::numbers::sqrt2) {
47 ret = orth_component / norm;
48 break;
49 }
50 }
51 return ret;
52}
53
54} // namespace Utils
Vector implementation and trait types for boost qvm interoperability.
T norm2() const
Definition Vector.hpp:137
T norm() const
Definition Vector.hpp:138
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value)
Create a vector that has all entries set to the same value.
Definition Vector.hpp:110
Vector< T, N > calc_orthonormal_vector(Vector< T, N > const &vec)
Return a vector that is orthonormal to vec.