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
34/** get the linear index from the position (@p a,@p b,@p c) in a 3D grid
35 * of dimensions @p adim.
36 *
37 * @return The linear index
38 * @param a , b , c Position in 3D space
39 * @param adim Dimensions of the underlying grid
40 * @param memory_order Row- or column-major
41 */
42inline int
43get_linear_index(int a, int b, int c, const Vector3i &adim,
45 assert((a >= 0) && (a < adim[0]));
46 assert((b >= 0) && (b < adim[1]));
47 assert((c >= 0) && (c < adim[2]));
48
49 if (memory_order == MemoryOrder::COLUMN_MAJOR) {
50 return a + adim[0] * (b + adim[1] * c);
51 }
52 return adim[1] * adim[2] * a + adim[2] * b + c;
53}
54
55inline int
56get_linear_index(const Vector3i &ind, const Vector3i &adim,
58 return get_linear_index(ind[0], ind[1], ind[2], adim, memory_order);
59}
60
61/**
62 * @brief Linear index into a lower triangular matrix.
63 *
64 * This is row-major.
65 *
66 * @tparam T Integral type
67 * @param i row index
68 * @param j column index
69 * @return linear index
70 */
71template <class T> T lower_triangular(T i, T j) {
72 /* i is a valid row index */
73 assert(i >= 0);
74 /* j is in the lower triangle */
75 assert(j >= 0 and j <= i);
76 return (i * (i + 1)) / 2 + j;
77}
78
79} // namespace Utils
Vector implementation and trait types for boost qvm interoperability.
int get_linear_index(int a, int b, int c, const Vector3i &adim, MemoryOrder memory_order=MemoryOrder::COLUMN_MAJOR)
get the linear index from the position (a,b,c) in a 3D grid of dimensions adim.
Definition index.hpp:43
T lower_triangular(T i, T j)
Linear index into a lower triangular matrix.
Definition index.hpp:71
MemoryOrder
Definition index.hpp:32