(git:d18deda)
Loading...
Searching...
No Matches
grid_common.h
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2025 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7#ifndef GRID_COMMON_H
8#define GRID_COMMON_H
9
10#define GRID_STRINGIFY(SYMBOL) #SYMBOL
11
12// GCC added the simd pragma with version 6.
13#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && \
14 !defined(__INTEL_LLVM_COMPILER) && __GNUC__ < 6
15#define GRID_PRAGMA_SIMD(OBJS, N)
16// Intel added the simd pragma with version 19.00.
17#elif defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1900
18#define GRID_PRAGMA_SIMD(OBJS, N)
19// All compilers support the same syntax defined by the OpenMP standard.
20#else
21#define GRID_PRAGMA_SIMD(OBJS, N) \
22 _Pragma(GRID_STRINGIFY(omp simd linear OBJS simdlen(N)))
23#endif
24
25// GCC added the unroll pragma with version 8 and...
26#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && \
27 !defined(__INTEL_LLVM_COMPILER) && __GNUC__ < 8
28#define GRID_PRAGMA_UNROLL(N)
29#define GRID_PRAGMA_UNROLL_UP_TO(N)
30// ...chose a custom syntax.
31#elif defined(__GNUC__) && !defined(__INTEL_COMPILER) && \
32 !defined(__INTEL_LLVM_COMPILER) && __GNUC__ >= 8
33#define GRID_PRAGMA_UNROLL(N) _Pragma(GRID_STRINGIFY(GCC unroll N))
34#define GRID_PRAGMA_UNROLL_UP_TO(N) _Pragma(GRID_STRINGIFY(GCC unroll N))
35// Most other compilers support a common syntax.
36#else
37#define GRID_PRAGMA_UNROLL(N) _Pragma(GRID_STRINGIFY(unroll(N)))
38#define GRID_PRAGMA_UNROLL_UP_TO(N) _Pragma("unroll")
39#endif
40
41#if defined(__CUDACC__) || defined(__HIPCC__)
42#define GRID_HOST_DEVICE __host__ __device__
43#else
44#define GRID_HOST_DEVICE
45#endif
46
47/*******************************************************************************
48 * \brief Factorial function, e.g. fac(5) = 5! = 120.
49 * \author Ole Schuett
50 ******************************************************************************/
51GRID_HOST_DEVICE static inline double fac(const int i) {
52 static const double table[] = {
53 0.10000000000000000000E+01, 0.10000000000000000000E+01,
54 0.20000000000000000000E+01, 0.60000000000000000000E+01,
55 0.24000000000000000000E+02, 0.12000000000000000000E+03,
56 0.72000000000000000000E+03, 0.50400000000000000000E+04,
57 0.40320000000000000000E+05, 0.36288000000000000000E+06,
58 0.36288000000000000000E+07, 0.39916800000000000000E+08,
59 0.47900160000000000000E+09, 0.62270208000000000000E+10,
60 0.87178291200000000000E+11, 0.13076743680000000000E+13,
61 0.20922789888000000000E+14, 0.35568742809600000000E+15,
62 0.64023737057280000000E+16, 0.12164510040883200000E+18,
63 0.24329020081766400000E+19, 0.51090942171709440000E+20,
64 0.11240007277776076800E+22, 0.25852016738884976640E+23,
65 0.62044840173323943936E+24, 0.15511210043330985984E+26,
66 0.40329146112660563558E+27, 0.10888869450418352161E+29,
67 0.30488834461171386050E+30, 0.88417619937397019545E+31,
68 0.26525285981219105864E+33};
69 return table[i];
70}
71
72/*******************************************************************************
73 * \brief Number of Cartesian orbitals up to given angular momentum quantum.
74 * \author Ole Schuett
75 ******************************************************************************/
76GRID_HOST_DEVICE static inline int ncoset(const int l) {
77 static const int table[] = {0, // l=-1, usefull for computing loop bounds
78 1, // l=0
79 4, // l=1
80 10, // l=2 ...
81 20, 35, 56, 84, 120, 165, 220, 286,
82 364, 455, 560, 680, 816, 969, 1140, 1330};
83 return table[l + 1];
84}
85
86/*******************************************************************************
87 * \brief Maps three angular momentum components to a single zero based index.
88 * \author Ole Schuett
89 ******************************************************************************/
90GRID_HOST_DEVICE static inline int coset(int lx, int ly, int lz) {
91 const int l = lx + ly + lz;
92 if (l == 0) {
93 return 0;
94 } else {
95 return ncoset(l - 1) + ((l - lx) * (l - lx + 1)) / 2 + lz;
96 }
97}
98
99/*******************************************************************************
100 * \brief Returns the smaller of two given integer (missing from the C standard)
101 * \author Ole Schuett
102 ******************************************************************************/
103GRID_HOST_DEVICE static inline int imin(int x, int y) {
104 return (x < y ? x : y);
105}
106
107/*******************************************************************************
108 * \brief Returns the larger of two given integer (missing from the C standard)
109 * \author Ole Schuett
110 ******************************************************************************/
111GRID_HOST_DEVICE static inline int imax(int x, int y) {
112 return (x > y ? x : y);
113}
114
115/*******************************************************************************
116 * \brief Equivalent of Fortran's MODULO, which always return a positive number.
117 * https://gcc.gnu.org/onlinedocs/gfortran/MODULO.html
118 * \author Ole Schuett
119 ******************************************************************************/
120GRID_HOST_DEVICE static inline int modulo(int a, int m) {
121 return ((a % m + m) % m);
122}
123
124/*******************************************************************************
125 * \brief Orbital angular momentum.
126 * \author Ole Schuett
127 ******************************************************************************/
128typedef struct {
129 int l[3];
130} orbital;
131
132/*******************************************************************************
133 * \brief Increase i'th component of given orbital angular momentum.
134 * \author Ole Schuett
135 ******************************************************************************/
136GRID_HOST_DEVICE static inline orbital up(const int i, const orbital a) {
137 orbital b = a;
138 b.l[i] += 1;
139 return b;
140}
141
142/*******************************************************************************
143 * \brief Decrease i'th component of given orbital angular momentum.
144 * \author Ole Schuett
145 ******************************************************************************/
146GRID_HOST_DEVICE static inline orbital down(const int i, const orbital a) {
147 orbital b = a;
148 b.l[i] = imax(0, a.l[i] - 1);
149 return b;
150}
151
152/*******************************************************************************
153 * \brief Return coset index of given orbital angular momentum.
154 * \author Ole Schuett
155 ******************************************************************************/
156GRID_HOST_DEVICE static inline int idx(const orbital a) {
157 return coset(a.l[0], a.l[1], a.l[2]);
158}
159
160#endif // GRID_COMMON_H
161
162// EOF
static GRID_HOST_DEVICE int imin(int x, int y)
Returns the smaller of two given integer (missing from the C standard)
static GRID_HOST_DEVICE int imax(int x, int y)
Returns the larger of two given integer (missing from the C standard)
#define GRID_HOST_DEVICE
Definition grid_common.h:44
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
static GRID_HOST_DEVICE orbital up(const int i, const orbital a)
Increase i'th component of given orbital angular momentum.
static GRID_HOST_DEVICE int idx(const orbital a)
Return coset index of given orbital angular momentum.
static GRID_HOST_DEVICE orbital down(const int i, const orbital a)
Decrease i'th component of given orbital angular momentum.
static void const int const int i
Orbital angular momentum.