Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
CartesianNumbering.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* CartesianNumbering.h (C) 2000-2021 */
9/* */
10/* Handling of Cartesian numbering on a grid of entities. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CARTESIANMESH_CARTESIANNUMBERING_H
13#define ARCANE_CARTESIANMESH_CARTESIANNUMBERING_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/cartesianmesh/CartesianMeshGlobal.h"
18#include "arcane/cartesianmesh/v2/CartesianTypes.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane::CartesianMesh::V2
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
44template <typename IdType>
45class CartesianNumbering
46{
47 public:
48
50 using IdType3 = IdType[3];
51
52 public:
53
54 CartesianNumbering() {}
55
56 ARCCORE_HOST_DEVICE CartesianNumbering(const CartesianNumbering<IdType>& rhs)
57 {
58 m_dimension = rhs.m_dimension;
59 for (Integer d(0); d < 3; ++d) {
60 m_nitems_dir[d] = rhs.m_nitems_dir[d];
61 m_coef[d] = rhs.m_coef[d];
62 }
63 m_nitems = rhs.m_nitems;
64 m_first_item_id = rhs.m_first_item_id;
65 }
66
67 void initNumbering(const IdType3& nitems_dir, Integer dimension, IdType first_item_id = 0)
68 {
69 m_dimension = dimension;
70 m_nitems = 1;
71 m_first_item_id = first_item_id;
72
73 for (Integer d(0); d < m_dimension; ++d) {
74 m_nitems_dir[d] = nitems_dir[d];
75 m_nitems *= m_nitems_dir[d];
76 }
77
78 for (Integer d(m_dimension); d < 3; ++d) {
79 m_nitems_dir[d] = 1;
80 }
81
82 m_coef[0] = m_coef[1] = m_coef[2] = 1;
83
84 // Numbering according to i, then j, then k
85 m_coef[0] = 1;
86 for (Integer d(1); d < m_dimension; ++d) {
87 m_coef[d] = m_coef[d - 1] * m_nitems_dir[d - 1];
88 }
89 }
90
93 {
94 return m_dimension;
95 }
96
98 const IdType3& nbItem3() const
99 {
100 return m_nitems_dir;
101 }
102
104 // Equal to nbItem3()[dir]
105 IdType nbItemDir(Integer dir) const
106 {
107 return m_nitems_dir[dir];
108 }
109
111 IdType nbItem() const
112 {
113 return m_nitems;
114 }
115
117 IdType firstId() const
118 {
119 return m_first_item_id;
120 }
121
123 // Equal to delta3()[dir]
124 // id(item_ijk) + deltaDir(1) = id({item_ijk[0],item_ijk[1]+1,item_ijk[0]})
125 IdType deltaDir(Integer dir) const
126 {
127 return m_coef[dir];
128 }
129
131 const IdType3& delta3() const
132 {
133 return m_coef;
134 }
135
137 ARCCORE_HOST_DEVICE inline IdType id(IdType i, IdType j, IdType k) const
138 {
139 // m_coef[0] is 1
140 // TODO : specialization via template
141 IdType item_id;
142 if (m_dimension < 3) {
143 item_id = m_first_item_id + i /*m_coef[0]*/ + j * m_coef[1];
144 }
145 else {
146 item_id = m_first_item_id + i /*m_coef[0]*/ + j * m_coef[1] + k * m_coef[2];
147 }
148 return item_id;
149 }
150
152 inline IdType id(const IdType3& item_ijk) const
153 {
154 return id(item_ijk[0], item_ijk[1], item_ijk[2]);
155 }
156
158 // NOTE : IdxType is a triplet of Int64 while IdType may be an Int32 or an Int64
159 ARCCORE_HOST_DEVICE inline IdType id(IdxType idx) const
160 {
161 return id(static_cast<IdType>(idx[0]), static_cast<IdType>(idx[1]), static_cast<IdType>(idx[2]));
162 }
163
165 void ijk(IdType item_id, IdType3& item_ijk) const
166 {
167 item_id -= m_first_item_id;
168 // TODO : specialization via template
169 if (m_dimension < 3) {
170 item_ijk[2] = 0;
171 item_ijk[1] = item_id / m_coef[1];
172 item_ijk[0] = item_id % m_coef[1];
173 }
174 else {
175 item_ijk[2] = item_id / m_coef[2];
176 IdType tmp = item_id % m_coef[2];
177 item_ijk[1] = tmp / m_coef[1];
178 item_ijk[0] = tmp % m_coef[1];
179 }
180 }
181
183 ARCCORE_HOST_DEVICE IdxType ijk(IdType item_id) const
184 {
185 item_id -= m_first_item_id;
186 Int64 i, j, k;
187 // TODO : specialization via template
188 if (m_dimension < 3) {
189 k = 0;
190 j = item_id / m_coef[1];
191 i = item_id % m_coef[1];
192 }
193 else {
194 k = item_id / m_coef[2];
195 IdType tmp = item_id % m_coef[2];
196 j = tmp / m_coef[1];
197 i = tmp % m_coef[1];
198 }
199 return { i, j, k };
200 }
201
202 // TODO : use specialized template
204 IdType idxDir0(IdType item_id) const
205 {
206 return (item_id - m_first_item_id) % m_coef[1];
207 }
208
210 IdType idxDir1(IdType item_id) const
211 {
212 return (m_dimension == 3 ? ((item_id - m_first_item_id) % m_coef[2] / m_coef[1]) : (item_id - m_first_item_id) / m_coef[1]);
213 }
214
216 IdType idxDir2(IdType item_id) const
217 {
218 return (item_id - m_first_item_id) / m_coef[2];
219 }
220
221 protected:
222
223 Integer m_dimension = 0;
224
225 IdType3 m_nitems_dir = { 1, 1, 1 }; // Number of elements per direction
226 IdType m_nitems = 0; // Total number of elements
227
228 IdType m_first_item_id = 0;
229
230 IdType3 m_coef = { 0, 0, 0 };
231};
232
233/*---------------------------------------------------------------------------*/
234/*---------------------------------------------------------------------------*/
235
236} // namespace Arcane::CartesianMesh::V2
237
238/*---------------------------------------------------------------------------*/
239/*---------------------------------------------------------------------------*/
240
241#endif
Handling of a Cartesian numbering on a grid of items of up to 3 dimensions Allows conversion from a t...
IdType deltaDir(Integer dir) const
Offset to add to id() to get the ID of the next item in direction dir (if this item exists).
__host__ __device__ IdType id(IdType i, IdType j, IdType k) const
Conversion (i,j,k) => number.
void ijk(IdType item_id, IdType3 &item_ijk) const
Conversion number => (i,j,k).
__host__ __device__ IdxType ijk(IdType item_id) const
Conversion number => (i,j,k).
__host__ __device__ IdType id(IdxType idx) const
Conversion (i,j,k) => number.
IdType idxDir0(IdType item_id) const
Conversion number => i.
IdType3 m_coef
item_id = m_first_item_id + CartesianNumbering(i,j,k), allows an offset in the numbering
const IdType3 & nbItem3() const
Triplet of the number of items in each direction (grid definition).
IdType idxDir1(IdType item_id) const
Conversion number => j.
IdType[3] IdType3
Type for Cartesian triplets (i,j,k) and dimension triplets (ni,nj,nk).
IdType idxDir2(IdType item_id) const
Conversion number => k.
IdType id(const IdType3 &item_ijk) const
Conversion (i,j,k) => number.
Integer dimension() const
Dimension of the Cartesian grid on which the numbering is based.
IdType nbItem() const
Total number of items in the Cartesian grid (product of the number of items in each direction).
IdType nbItemDir(Integer dir) const
Number of items in the Cartesian grid according to direction dir (< dimension()).
const IdType3 & delta3() const
Triplet of offsets in all directions to move to the next items in each direction.
IdType firstId() const
Smallest identifier of the Cartesian numbering of the grid.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.