ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
index.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 <cassert>
23#include <cstddef>
24#include <iterator>
25#include <numeric>
26#include <stdexcept>
27
28#include "Vector.hpp"
29
30namespace Utils {
31
33
34template <MemoryOrder memory_order>
35inline int get_linear_index(int a, int b, int c, const Vector3i &adim) {
36 assert((a >= 0) && (a < adim[0]));
37 assert((b >= 0) && (b < adim[1]));
38 assert((c >= 0) && (c < adim[2]));
39 if constexpr (memory_order == MemoryOrder::COLUMN_MAJOR) {
40 return a + adim[0] * (b + adim[1] * c);
41 }
42 return adim[1] * adim[2] * a + adim[2] * b + c;
43}
44
45/** get the linear index from the position (@p a,@p b,@p c) in a 3D grid
46 * of dimensions @p adim.
47 *
48 * @return The linear index
49 * @param a , b , c Position in 3D space
50 * @param adim Dimensions of the underlying grid
51 * @param memory_order Row- or column-major
52 */
53inline int
54get_linear_index(int a, int b, int c, const Vector3i &adim,
56 if (memory_order == MemoryOrder::COLUMN_MAJOR) {
57 return get_linear_index<MemoryOrder::COLUMN_MAJOR>(a, b, c, adim);
58 }
59 return get_linear_index<MemoryOrder::ROW_MAJOR>(a, b, c, adim);
60}
61
62inline int
63get_linear_index(const Vector3i &ind, const Vector3i &adim,
65 return get_linear_index(ind[0], ind[1], ind[2], adim, memory_order);
66}
67
68template <MemoryOrder memory_order>
69inline int get_linear_index(const Vector3i &ind, const Vector3i &adim) {
70 return get_linear_index<memory_order>(ind[0], ind[1], ind[2], adim);
71}
72
73/**
74 * @brief Linear index into a lower triangular matrix.
75 *
76 * This is row-major.
77 *
78 * @tparam T Integral type
79 * @param i row index
80 * @param j column index
81 * @return linear index
82 */
83template <class T> T lower_triangular(T i, T j) {
84 /* i is a valid row index */
85 assert(i >= 0);
86 /* j is in the lower triangle */
87 assert(j >= 0 and j <= i);
88 return (i * (i + 1)) / 2 + j;
89}
90
91} // namespace Utils
Vector implementation and trait types for boost qvm interoperability.
T lower_triangular(T i, T j)
Linear index into a lower triangular matrix.
Definition index.hpp:83
int get_linear_index(int a, int b, int c, const Vector3i &adim)
Definition index.hpp:35
MemoryOrder
Definition index.hpp:32