MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
bb_grid_map.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12/* The BBoxTensorGridMap class is adapted from similar functionality in the
13gslib library. Below is the gslib license and copyright statement:
14
15Copyright (c) 2008-2024, UCHICAGO ARGONNE, LLC.
16
17The UChicago Argonne, LLC as Operator of Argonne National
18Laboratory holds copyright in the Software. The copyright holder
19reserves all rights except those expressly granted to licensees,
20and U.S. Government license rights.
21
22Redistribution and use in source and binary forms, with or without
23modification, are permitted provided that the following conditions
24are met:
25
261. Redistributions of source code must retain the above copyright
27notice, this list of conditions and the disclaimer below.
28
292. Redistributions in binary form must reproduce the above copyright
30notice, this list of conditions and the disclaimer (as noted below)
31in the documentation and/or other materials provided with the
32distribution.
33
343. Neither the name of ANL nor the names of its contributors
35may be used to endorse or promote products derived from this software
36without specific prior written permission.
37
38THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
42UCHICAGO ARGONNE, LLC, THE U.S. DEPARTMENT OF
43ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
45TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50*/
51
52#ifndef MFEM_BB_GRID_MAP
53#define MFEM_BB_GRID_MAP
54
55#include "../config/config.hpp"
56#ifdef MFEM_USE_MPI
57#include "../fem/pgridfunc.hpp"
58#else
59#include "../fem/gridfunc.hpp"
60#endif
61
62namespace mfem
63{
64
65/** \brief Map a point in physical space to candidate elements of a curved mesh.
66 *
67 * This class builds a Cartesian-aligned tensor grid that covers the domain
68 * and precomputes, for each grid cell, the set of curved mesh elements whose
69 * axis-aligned bounding boxes (AABBs) intersect that cell. Given a point (xyz)
70 * in physical coordinates, the Cartesian grid cell containing the point is
71 * determined, and the list of candidate element indices whose AABBs are
72 * intersecting that cell is returned. This yields a fast, conservative
73 * point-to-element candidate query.
74 *
75 * The mapping procedure uses a half-open interval convention in each
76 * dimension. If an element bounding-box minimum lies exactly on a grid-cell
77 * boundary, it is assigned to the cell on the right/high side of that
78 * boundary. If an element bounding-box maximum lies exactly on a grid-cell
79 * boundary, it is assigned to the cell on the left/low side.
80 *
81 * The map itself is stored as a single array CSR structure where the offsets
82 * and values are stored in the same array. For a tensor grid with a total of
83 * N cells, the first N+1 entries store the offsets and the remaining entries
84 * store the values.
85 *
86 * The "lgrid_map" looks something like this:
87 *
88 * Index: 0 1 ... N N+1 ...
89 * Value: [start_0] [start_1] ... [Length(Map)] [elem_A] [elem_B] [elem_C]...
90 * | | ^ ^
91 * | |__________________________|_________________|
92 * |_____________________________________|
93 *
94 * For grid cell index i, the element indices are stored in
95 * lgrid_map[j], where lgrid_map[i] <= j < lgrid_map[i+1].
96 *
97 * If lgrid_map[i] = lgrid_map[i+1], the grid cell i does not intersect any
98 * elements.
99 *
100 * See Mittal et al., "General Field Evaluation in High-Order Meshes on GPUs".
101 * (2025). Computers & Fluids. for technical details.
102 */
104{
105private:
106 int sdim; // spatial dimension
107 Array<int> lmap_nx; // grid resolution in each direction
108 Vector lmap_bnd_min, lmap_bnd_max; // min and max extend of grid in x/y/z
109 Vector lmap_fac; // number of cells per unit extent
110 Array<unsigned int> lgrid_map; // actual map from grid cell to mesh elements.
111 unsigned int lmap_nxd; // total number of grid cells
112
113public:
114 /// Constructor for a given mesh and resolution of Cartesian grid.
115 BBoxTensorGridMap(Mesh &mesh, int nx);
116
117 /** @brief Constructor with mesh element bounding boxes and spatial dimension.
118 *
119 * @details When by_max_size=false, nx gives the Cartesian grid resolution
120 * in each direction. When by_max_size=true, nx[0] gives the requested
121 * maximum size of lgrid_map. If nx[0] < 2 + nel, lgrid_map is resized to
122 * the minimum feasible size 2 + nel.
123 *
124 * Assumes elmin, elmax Ordering::byNodes:
125 * elmin -> [x_{0,min},x_{1,min},... ,y_{0,min},y_{1,min},..,z_{nel-1,min}]
126 * elmax -> [x_{0,max},x_{1,max},... ,y_{0,max},y_{1,max},..,z_{nel-1,max}]
127 * Note elmin, elmax can be obtained using GridFunction::GetElementBounds()
128 */
129 BBoxTensorGridMap(Vector &elmin, Vector &elmax,
130 int nel, int sdim, Array<int> &nx,
131 bool by_max_size=false);
132
133 /** @brief Constructor for given element bounds and spatial dimension.
134 *
135 * @details The user can either specify the max size of map
136 * (by_max_size=true) or the number of divisions (by_max_size=false).
137 *
138 * @details When by_max_size=true, n gives the requested maximum size of
139 * lgrid_map. If n >= 2 + nel, then lgrid_map.Size() <= n. Otherwise,
140 * lgrid_map is resized to the minimum feasible size 2 + nel.
141 *
142 * Assumes elmin, elmax Ordering::byNodes:
143 * elmin -> [x_{0,min},x_{1,min},... ,y_{0,min},y_{1,min},..,z_{nel-1,min}]
144 * elmax -> [x_{0,max},x_{1,max},... ,y_{0,max},y_{1,max},..,z_{nel-1,max}]
145 * Note elmin, elmax can be obtained using GridFunction::GetElementBounds()
146 */
147 BBoxTensorGridMap(Vector &elmin, Vector &elmax,
148 int nel, int sdim, int n, bool by_max_size=false);
149
150 /// Map a point to possible overlapping elements.
152
153 /// Get grid cell index for a given point.
154 int GetGridCellFromPoint(Vector &xyz) const;
155
156 /// Get list of elements corresponding to a grid cell.
157 Array<int> GridCellToElements(int i) const;
158
159 /// Return the map from grid cells to candidate elements whose bounding
160 /// boxes intersect each cell.
161 const Array<unsigned int> &GetGridMap() const { return lgrid_map; }
162 /// Return the number of grid cells per unit extent in each direction.
163 const Vector &GetGridFac() const { return lmap_fac; }
164 /// Return the minimum extent of the grid in each direction.
165 const Vector &GetGridMin() const { return lmap_bnd_min; }
166 /// Return the maximum extent of the grid in each direction.
167 const Vector &GetGridMax() const { return lmap_bnd_max; }
168 /// Return the grid resolution (number of cells) in each direction.
169 const Array<int> &GetGridN() const { return lmap_nx; }
170private:
171 /** @brief Setup using the element-wise bounding boxes.
172 *
173 * @details When by_max_size = false, nx gives number of cells in each
174 * direction. When by_max_size = true, nx[0] gives the requested maximum
175 * size of lgrid_map. If nx[0] < 2 + nel, lgrid_map is resized to the
176 * minimum feasible size 2 + nel. */
177 void Setup(Vector &elmin, Vector &elmax,
178 int nel, Array<int> &nx, bool by_max_size);
179
180public:
181 /** @brief Get local (1D) indices for cells of tensor grid that intersect
182 * with the given bounding box. */
183 static void GetGridRange(const int d, const Array<int> &lh_n,
184 const Vector &lh_fac,
185 const Vector &lh_bnd_min,
186 const real_t &xmin, const real_t &xmax,
187 int &imin, int &imax);
188
189 /// Set grid fac - number of grid cells per unit grid extent.
190 static void SetGridFac(Vector &lh_fac, const Array<int> &nx,
191 const Vector &lh_bnd_min, const Vector &lh_bnd_max);
192
193 /** @brief Get grid count and range - total number of grid cells that
194 * intersect with all elements of the mesh and get corresponding ranges. */
195 static int GetGridCountAndRange(const Array<int> &lh_n, const Vector &lh_fac,
196 const Vector &lh_bnd_min,
197 const Vector &lh_bnd_max,
198 const Vector &elmin, const Vector &elmax,
199 Array<int> &elmin_h, Array<int> &elmax_h);
200};
201
202} // namespace mfem
203
204#endif // MFEM_BB_GRID_MAP
Map a point in physical space to candidate elements of a curved mesh.
const Array< int > & GetGridN() const
Return the grid resolution (number of cells) in each direction.
Array< int > GridCellToElements(int i) const
Get list of elements corresponding to a grid cell.
static void GetGridRange(const int d, const Array< int > &lh_n, const Vector &lh_fac, const Vector &lh_bnd_min, const real_t &xmin, const real_t &xmax, int &imin, int &imax)
Get local (1D) indices for cells of tensor grid that intersect with the given bounding box.
int GetGridCellFromPoint(Vector &xyz) const
Get grid cell index for a given point.
BBoxTensorGridMap(Mesh &mesh, int nx)
Constructor for a given mesh and resolution of Cartesian grid.
const Vector & GetGridMax() const
Return the maximum extent of the grid in each direction.
const Vector & GetGridFac() const
Return the number of grid cells per unit extent in each direction.
static void SetGridFac(Vector &lh_fac, const Array< int > &nx, const Vector &lh_bnd_min, const Vector &lh_bnd_max)
Set grid fac - number of grid cells per unit grid extent.
const Vector & GetGridMin() const
Return the minimum extent of the grid in each direction.
static int GetGridCountAndRange(const Array< int > &lh_n, const Vector &lh_fac, const Vector &lh_bnd_min, const Vector &lh_bnd_max, const Vector &elmin, const Vector &elmax, Array< int > &elmin_h, Array< int > &elmax_h)
Get grid count and range - total number of grid cells that intersect with all elements of the mesh an...
const Array< unsigned int > & GetGridMap() const
Array< int > MapPointToElements(Vector &xyz) const
Map a point to possible overlapping elements.
Mesh data type.
Definition mesh.hpp:67
Vector data type.
Definition vector.hpp:82
float real_t
Definition config.hpp:46