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
30
namespace
Utils
{
31
32
enum class
MemoryOrder
{
COLUMN_MAJOR
,
ROW_MAJOR
};
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
*/
42
inline
int
43
get_linear_index
(
int
a,
int
b,
int
c,
const
Vector3i
&adim,
44
MemoryOrder
memory_order =
MemoryOrder::COLUMN_MAJOR
) {
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
55
inline
int
56
get_linear_index
(
const
Vector3i
&ind,
const
Vector3i
&adim,
57
MemoryOrder
memory_order =
MemoryOrder::COLUMN_MAJOR
) {
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
*/
71
template
<
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.hpp
Vector implementation and trait types for boost qvm interoperability.
Utils::Vector
Definition
Vector.hpp:48
Utils
Definition
Variant.hpp:40
Utils::get_linear_index
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
Utils::lower_triangular
T lower_triangular(T i, T j)
Linear index into a lower triangular matrix.
Definition
index.hpp:71
Utils::MemoryOrder
MemoryOrder
Definition
index.hpp:32
Utils::MemoryOrder::ROW_MAJOR
@ ROW_MAJOR
Utils::MemoryOrder::COLUMN_MAJOR
@ COLUMN_MAJOR
src
utils
include
utils
index.hpp
Generated on Fri Nov 22 2024 02:13:55 for ESPResSo by
1.9.8