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-2026 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
#include "
device_qualifier.hpp
"
30
31
namespace
Utils
{
32
33
enum class
MemoryOrder
{
COLUMN_MAJOR
,
ROW_MAJOR
};
34
35
template
<MemoryOrder memory_order>
36
DEVICE_QUALIFIER
inline
int
get_linear_index
(
int
a,
int
b,
int
c,
37
Vector3i
const
&adim) {
38
DEVICE_ASSERT
((a >= 0) && (a < adim[0]));
39
DEVICE_ASSERT
((b >= 0) && (b < adim[1]));
40
DEVICE_ASSERT
((c >= 0) && (c < adim[2]));
41
if
constexpr
(memory_order ==
MemoryOrder::COLUMN_MAJOR
) {
42
return
a + adim[0] * (b + adim[1] * c);
43
}
44
return
adim[1] * adim[2] * a + adim[2] * b + c;
45
}
46
47
/** Get the linear index from the position (@p a,@p b,@p c) in a 3D grid
48
* of dimensions @p adim.
49
*
50
* @return The linear index
51
* @param a , b , c Position in 3D space
52
* @param adim Dimensions of the underlying grid
53
* @param memory_order Row- or column-major
54
*/
55
DEVICE_QUALIFIER
inline
int
56
get_linear_index
(
int
a,
int
b,
int
c,
Vector3i
const
&adim,
57
MemoryOrder
memory_order =
MemoryOrder::COLUMN_MAJOR
) {
58
if
(memory_order ==
MemoryOrder::COLUMN_MAJOR
) {
59
return
get_linear_index<MemoryOrder::COLUMN_MAJOR>(a, b, c, adim);
60
}
61
return
get_linear_index<MemoryOrder::ROW_MAJOR>(a, b, c, adim);
62
}
63
64
DEVICE_QUALIFIER
inline
int
65
get_linear_index
(
Vector3i
const
&ind,
Vector3i
const
&adim,
66
MemoryOrder
memory_order =
MemoryOrder::COLUMN_MAJOR
) {
67
return
get_linear_index
(ind[0], ind[1], ind[2], adim, memory_order);
68
}
69
70
template
<MemoryOrder memory_order>
71
DEVICE_QUALIFIER
inline
int
get_linear_index
(
Vector3i
const
&ind,
72
Vector3i
const
&adim) {
73
return
get_linear_index<memory_order>(ind[0], ind[1], ind[2], adim);
74
}
75
76
/**
77
* @brief Linear index into a lower triangular matrix.
78
*
79
* This is row-major.
80
*
81
* @tparam T Integral type
82
* @param i row index
83
* @param j column index
84
* @return linear index
85
*/
86
template
<
class
T>
DEVICE_QUALIFIER
T
lower_triangular
(T i, T j) {
87
/* i is a valid row index */
88
DEVICE_ASSERT
(i >= 0);
89
/* j is in the lower triangle */
90
DEVICE_ASSERT
(j >= 0 and j <= i);
91
return
(i * (i + 1)) / 2 + j;
92
}
93
94
}
// namespace Utils
Vector.hpp
Vector implementation and trait types for boost qvm interoperability.
Utils::Vector
Definition
Vector.hpp:51
device_qualifier.hpp
DEVICE_ASSERT
#define DEVICE_ASSERT(A)
Definition
device_qualifier.hpp:32
DEVICE_QUALIFIER
#define DEVICE_QUALIFIER
Definition
device_qualifier.hpp:30
Utils
Definition
Variant.hpp:43
Utils::lower_triangular
DEVICE_QUALIFIER T lower_triangular(T i, T j)
Linear index into a lower triangular matrix.
Definition
index.hpp:86
Utils::get_linear_index
DEVICE_QUALIFIER int get_linear_index(int a, int b, int c, Vector3i const &adim)
Definition
index.hpp:36
Utils::MemoryOrder
MemoryOrder
Definition
index.hpp:33
Utils::MemoryOrder::ROW_MAJOR
@ ROW_MAJOR
Utils::MemoryOrder::COLUMN_MAJOR
@ COLUMN_MAJOR
src
utils
include
utils
index.hpp
Generated on Mon Sep 7 2026 01:24:52 for ESPResSo by
1.9.8