9.2
general documentation
fvm_morton.h
Go to the documentation of this file.
1#ifndef FVM_MORTON_H
2#define FVM_MORTON_H
3
4/*============================================================================
5 * Morton encoding for 2D or 3D coordinates.
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2026 EDF S.A.
12
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; either version 2 of the License, or (at your option) any later
16 version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25 Street, Fifth Floor, Boston, MA 02110-1301, USA.
26*/
27
28/*----------------------------------------------------------------------------*/
29
30#include "base/cs_defs.h"
31
32/*----------------------------------------------------------------------------
33 * Standard C library headers
34 *----------------------------------------------------------------------------*/
35
36#include <stdio.h>
37
38/*----------------------------------------------------------------------------
39 * Local headers
40 *----------------------------------------------------------------------------*/
41
42#include "fvm/fvm_defs.h"
43
44/*============================================================================
45 * Macro and type definitions
46 *============================================================================*/
47
48typedef enum {
49
53
55
56typedef unsigned int fvm_morton_int_t;
57
58typedef struct {
59
60 fvm_morton_int_t L; /* Level in the tree structure */
61 fvm_morton_int_t X[3]; /* X, Y, Z coordinates in Cartesian grid */
62
64
65/*============================================================================
66 * Public function definitions
67 *============================================================================*/
68
69/*----------------------------------------------------------------------------
70 * Determine the global extents associated with a set of coordinates
71 *
72 * parameters:
73 * dim <-- spatial dimension
74 * n_coords <-- local number of coordinates
75 * coords <-- entity coordinates; size: n_entities*dim (interlaced)
76 * g_extents --> global extents (size: dim*2)
77 * comm <-- associated MPI communicator
78 *---------------------------------------------------------------------------*/
79
80#if defined(HAVE_MPI)
81
82void
84 size_t n_coords,
85 const cs_coord_t coords[],
86 cs_coord_t g_extents[],
87 MPI_Comm comm);
88
89#else
90
91void
93 size_t n_coords,
94 const cs_coord_t coords[],
95 cs_coord_t g_extents[]);
96
97#endif
98
99/*----------------------------------------------------------------------------
100 * Determine the global extents associated with a set of local extents
101 *
102 * parameters:
103 * dim <-- spatial dimension
104 * n_extents <-- local number of coordinates
105 * extents <-- entity coordinates; size: n_entities*dim*2 (interlaced)
106 * g_extents --> global extents (size: dim*2)
107 * comm <-- associated MPI communicator
108 *---------------------------------------------------------------------------*/
109
110#if defined(HAVE_MPI)
111
112void
114 size_t n_extents,
115 const cs_coord_t extents[],
116 cs_coord_t g_extents[],
117 MPI_Comm comm);
118
119#else
120
121void
123 size_t n_extents,
124 const cs_coord_t extents[],
125 cs_coord_t g_extents[]);
126
127#endif
128
129/*----------------------------------------------------------------------------
130 * Build a Morton code according to the level in an octree grid and its
131 * coordinates in the grid.
132 *
133 * parameters:
134 * dim <-- 1D, 2D or 3D
135 * level <-- level in the grid
136 * coords <-- coordinates in the grid (normalized)
137 *
138 * returns:
139 * a Morton code
140 *----------------------------------------------------------------------------*/
141
143fvm_morton_encode(int dim,
144 fvm_morton_int_t level,
145 const cs_coord_t coords[]);
146
147/*----------------------------------------------------------------------------
148 * Encode an array of coordinates.
149 *
150 * The caller is responsible for freeing the returned array once it is
151 * no longer useful.
152 *
153 * parameters:
154 * dim <-- 1D, 2D or 3D
155 * level <-- level in the grid
156 * extents <-- coordinate extents for normalization (size: dim*2)
157 * n_coords <-- nomber of coordinates in array
158 * coords <-- coordinates in the grid (interlaced, not normalized)
159 * m_code --> array of corresponding Morton codes
160 *----------------------------------------------------------------------------*/
161
162void
164 fvm_morton_int_t level,
165 const cs_coord_t extents[],
166 size_t n_coords,
167 const cs_coord_t coords[],
168 fvm_morton_code_t m_code[]);
169
170/*----------------------------------------------------------------------------
171 * Given a Morton code in the grid, compute the Morton codes of its
172 * children when refining the grid by one level.
173 *
174 * parameters:
175 * dim <-- 1D, 2D or 3D
176 * parent <-- Morton code associated with parent
177 * children --> array of children Morton codes
178 * (size: 8 in 3D, 4 in 2D, 2 in 1D)
179 *----------------------------------------------------------------------------*/
180
181void
183 fvm_morton_code_t parent,
184 fvm_morton_code_t children[]);
185
186/*----------------------------------------------------------------------------
187 * Compare two Morton encoding and check if these two codes are equal,
188 * different or shared the same anchor.
189 *
190 * parameters:
191 * dim <-- 2D or 3D
192 * code_a <-- first Morton code to compare
193 * code_b <-- second Morton code to compare
194 *
195 * returns:
196 * a type on the kind of relation between the two Morton encodings.
197 *----------------------------------------------------------------------------*/
198
200fvm_morton_compare(int dim,
201 fvm_morton_code_t code_a,
202 fvm_morton_code_t code_b);
203
204/*----------------------------------------------------------------------------
205 * Locally order a list of Morton ids.
206 *
207 * parameters:
208 * n_codes <-- number of Morton ids to order
209 * morton_codes <-- array of Morton ids to order
210 * order --> pointer to pre-allocated ordering table
211 *----------------------------------------------------------------------------*/
212
213void
215 const fvm_morton_code_t morton_codes[],
216 cs_lnum_t order[]);
217
218/*----------------------------------------------------------------------------
219 * Locally sort a list of Morton ids.
220 *
221 * parameters:
222 * n_codes <-- number of Morton ids to order
223 * morton_codes <-> array of Morton ids to sort
224 *----------------------------------------------------------------------------*/
225
226void
228 fvm_morton_code_t morton_codes[]);
229
230/*----------------------------------------------------------------------------
231 * Test if Morton code "a" is greater than Morton code "b"
232 *
233 * parameters:
234 * code_a <-- first Morton code to compare
235 * code_b <-- second Morton code to compare
236 *
237 * returns:
238 * true or false
239 *----------------------------------------------------------------------------*/
240
241bool
244
245/*----------------------------------------------------------------------------
246 * Test if Morton code "a" is greater or equal to Morton code "b"
247 *
248 * parameters:
249 * code_a <-- first Morton code to compare
250 * code_b <-- second Morton code to compare
251 *
252 * returns:
253 * true or false
254 *----------------------------------------------------------------------------*/
255
256bool
259
260/*----------------------------------------------------------------------------
261 * Get the index associated to a Morton code using a binary search.
262 *
263 * No check is done to ensure that the code is present in the array.
264 *
265 * parameters:
266 * size <-- size of the array
267 * code <-- code we are searching for
268 * codes <-- array of Morton codes
269 *
270 * returns:
271 * id associated to the given code in the codes array.
272 *----------------------------------------------------------------------------*/
273
274int
277 fvm_morton_code_t *codes);
278
279#if defined(HAVE_MPI)
280
281/*----------------------------------------------------------------------------
282 * Build a global Morton encoding rank index.
283 *
284 * The rank_index[i] contains the first Morton code assigned to rank [i].
285 *
286 * parameters:
287 * dim <-- 1D, 2D or 3D
288 * gmax_level <-- level in octree used to build the Morton encoding
289 * n_codes <-- number of Morton codes to be indexed
290 * morton_code <-- array of Morton codes to be indexed
291 * weight <-- weighting related to each code
292 * order <-- ordering array
293 * rank_index <-> pointer to the global Morton encoding rank index
294 * comm <-- MPI communicator on which we build the global index
295 *
296 * returns:
297 * the fit related to the Morton encoding distribution (lower is better).
298 *----------------------------------------------------------------------------*/
299
300double
302 int gmax_level,
303 cs_gnum_t n_codes,
304 const fvm_morton_code_t code[],
305 const cs_lnum_t weight[],
306 const cs_lnum_t order[],
307 fvm_morton_code_t rank_index[],
308 MPI_Comm comm);
309
310#endif /* if HAVE_MPI */
311
312/*----------------------------------------------------------------------------*/
323/*----------------------------------------------------------------------------*/
324
325void
326fvm_morton_s_to_code(double s,
327 void *elt,
328 const void *input);
329
330/*----------------------------------------------------------------------------*/
342/*----------------------------------------------------------------------------*/
343
344int
345fvm_morton_compare_o(const void *elt1,
346 const void *elt2,
347 const void *input);
348
349/*----------------------------------------------------------------------------
350 * Dump a Morton to standard output or to a file.
351 *
352 * parameters:
353 * dim <-- 2D or 3D
354 * code <-- Morton code to dump
355 *----------------------------------------------------------------------------*/
356
357void
358fvm_morton_dump(int dim,
359 fvm_morton_code_t code);
360
361/*----------------------------------------------------------------------------*/
362
363#endif /* FVM_MORTON_H */
double cs_coord_t
Definition: cs_defs.h:330
unsigned cs_gnum_t
global mesh entity number
Definition: cs_defs.h:317
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
void fvm_morton_local_sort(cs_lnum_t n_codes, fvm_morton_code_t morton_codes[])
Definition: fvm_morton.cpp:1272
fvm_morton_code_t fvm_morton_encode(int dim, fvm_morton_int_t level, const cs_coord_t coords[])
Definition: fvm_morton.cpp:1041
bool fvm_morton_a_ge_b(fvm_morton_code_t a, fvm_morton_code_t b)
Definition: fvm_morton.cpp:1392
void fvm_morton_get_coord_extents(int dim, size_t n_coords, const cs_coord_t coords[], cs_coord_t g_extents[], MPI_Comm comm)
Definition: fvm_morton.cpp:923
void fvm_morton_local_order(cs_lnum_t n_codes, const fvm_morton_code_t morton_codes[], cs_lnum_t order[])
Definition: fvm_morton.cpp:1223
int fvm_morton_compare_o(const void *elt1, const void *elt2, const void *input)
Function pointer for comparison of 2 Morton codes.
Definition: fvm_morton.cpp:1565
fvm_morton_compare_t fvm_morton_compare(int dim, fvm_morton_code_t code_a, fvm_morton_code_t code_b)
Definition: fvm_morton.cpp:1320
int fvm_morton_binary_search(cs_lnum_t size, fvm_morton_code_t code, fvm_morton_code_t *codes)
Definition: fvm_morton.cpp:1413
void fvm_morton_s_to_code(double s, void *elt, const void *input)
Function pointer for conversion of a double precision value in range [0, 1] to a given Morton code.
Definition: fvm_morton.cpp:1538
fvm_morton_compare_t
Definition: fvm_morton.h:48
@ FVM_MORTON_SAME_ANCHOR
Definition: fvm_morton.h:51
@ FVM_MORTON_EQUAL_ID
Definition: fvm_morton.h:50
@ FVM_MORTON_DIFFERENT_ID
Definition: fvm_morton.h:52
double fvm_morton_build_rank_index(int dim, int gmax_level, cs_gnum_t n_codes, const fvm_morton_code_t code[], const cs_lnum_t weight[], const cs_lnum_t order[], fvm_morton_code_t rank_index[], MPI_Comm comm)
Definition: fvm_morton.cpp:1455
void fvm_morton_get_global_extents(int dim, size_t n_extents, const cs_coord_t extents[], cs_coord_t g_extents[], MPI_Comm comm)
Definition: fvm_morton.cpp:981
void fvm_morton_dump(int dim, fvm_morton_code_t code)
Definition: fvm_morton.cpp:1591
unsigned int fvm_morton_int_t
Definition: fvm_morton.h:56
void fvm_morton_encode_coords(int dim, fvm_morton_int_t level, const cs_coord_t extents[], size_t n_coords, const cs_coord_t coords[], fvm_morton_code_t m_code[])
Definition: fvm_morton.cpp:1082
bool fvm_morton_a_gt_b(fvm_morton_code_t a, fvm_morton_code_t b)
Definition: fvm_morton.cpp:1374
void fvm_morton_get_children(int dim, fvm_morton_code_t parent, fvm_morton_code_t children[])
Definition: fvm_morton.cpp:1165
Definition: fvm_morton.h:58
fvm_morton_int_t L
Definition: fvm_morton.h:60