MFEM  v4.6.0
Finite element discretization library
gslib.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, 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 #ifndef MFEM_GSLIB
13 #define MFEM_GSLIB
14 
15 #include "../config/config.hpp"
16 #include "gridfunc.hpp"
17 
18 #ifdef MFEM_USE_GSLIB
19 
20 namespace gslib
21 {
22 struct comm;
23 struct findpts_data_2;
24 struct findpts_data_3;
25 struct crystal;
26 }
27 
28 namespace mfem
29 {
30 
31 /** \brief FindPointsGSLIB can robustly evaluate a GridFunction on an arbitrary
32  * collection of points. There are three key functions in FindPointsGSLIB:
33  *
34  * 1. Setup - constructs the internal data structures of gslib.
35  *
36  * 2. FindPoints - for any given arbitrary set of points in physical space,
37  * gslib finds the element number, MPI rank, and the reference space
38  * coordinates inside the element that each point is located in. gslib also
39  * returns a code that indicates whether the point was found inside an
40  * element, on element border, or not found in the domain.
41  * For points returned as found on `element border`, the point is either
42  * on an element edge/face or near the domain boundary, and gslib also
43  * returns a distance to the border. Points near (but outside) the domain
44  * boundary must then be marked as not found using the distance returned
45  * by gslib.
46  *
47  * 3. Interpolate - Interpolates any grid function at the points found using 2.
48  *
49  * FindPointsGSLIB provides interface to use these functions individually or
50  * using a single call.
51  */
53 {
54 public:
55  enum AvgType {NONE, ARITHMETIC, HARMONIC}; // Average type for L2 functions
56 
57 protected:
59  Array<Mesh *> mesh_split; // Meshes used to split simplices.
60  // IntegrationRules for simplex->Quad/Hex and to project to highest polynomial
61  // order in-case of p-refinement.
64  fes_rst_map; // FESpaces to map info Quad/Hex->Simplex
65  Array<GridFunction *> gf_rst_map; // GridFunctions to map info Quad/Hex->Simplex
67  struct gslib::findpts_data_2 *fdata2D; // gslib's internal data
68  struct gslib::findpts_data_3 *fdata3D; // gslib's internal data
69  struct gslib::crystal *cr; // gslib's internal data
70  struct gslib::comm *gsl_comm; // gslib's internal data
74  bool setupflag; // flag to indicate whether gslib data has been setup
75  double default_interp_value; // used for points that are not found in the mesh
76  AvgType avgtype; // average type used for L2 functions
80  // Tolerance to ignore points just outside elements at the boundary.
81  double bdr_tol;
82 
83  /// Use GSLIB for communication and interpolation
84  virtual void InterpolateH1(const GridFunction &field_in, Vector &field_out);
85  /// Uses GSLIB Crystal Router for communication followed by MFEM's
86  /// interpolation functions
87  virtual void InterpolateGeneral(const GridFunction &field_in,
88  Vector &field_out);
89 
90  /// Since GSLIB is designed to work with quads/hexes, we split every
91  /// triangle/tet/prism/pyramid element into quads/hexes.
92  virtual void SetupSplitMeshes();
93 
94  /// Setup integration points that will be used to interpolate the nodal
95  /// location at points expected by GSLIB.
97  IntegrationRule *irule,
98  int order);
99 
100  /// Get GridFunction value at the points expected by GSLIB.
101  virtual void GetNodalValues(const GridFunction *gf_in, Vector &node_vals);
102 
103  /// Map {r,s,t} coordinates from [-1,1] to [0,1] for MFEM. For simplices,
104  /// find the original element number (that was split into micro quads/hexes)
105  /// during the setup phase.
106  virtual void MapRefPosAndElemIndices();
107 
108 public:
109  FindPointsGSLIB();
110 
111 #ifdef MFEM_USE_MPI
112  FindPointsGSLIB(MPI_Comm comm_);
113 #endif
114 
115  virtual ~FindPointsGSLIB();
116 
117  /** Initializes the internal mesh in gslib, by sending the positions of the
118  Gauss-Lobatto nodes of the input Mesh object @a m.
119  Note: not tested with periodic (L2).
120  Note: the input mesh @a m must have Nodes set.
121 
122  @param[in] m Input mesh.
123  @param[in] bb_t (Optional) Relative size of bounding box around
124  each element.
125  @param[in] newt_tol (Optional) Newton tolerance for the gslib
126  search methods.
127  @param[in] npt_max (Optional) Number of points for simultaneous
128  iteration. This alters performance and
129  memory footprint.*/
130  void Setup(Mesh &m, const double bb_t = 0.1,
131  const double newt_tol = 1.0e-12,
132  const int npt_max = 256);
133  /** Searches positions given in physical space by @a point_pos.
134  These positions can be ordered byNodes: (XXX...,YYY...,ZZZ) or
135  byVDim: (XYZ,XYZ,....XYZ) specified by @a point_pos_ordering.
136  This function populates the following member variables:
137  #gsl_code Return codes for each point: inside element (0),
138  element boundary (1), not found (2).
139  #gsl_proc MPI proc ids where the points were found.
140  #gsl_elem Element ids where the points were found.
141  Defaults to 0 for points that were not found.
142  #gsl_mfem_elem Element ids corresponding to MFEM-mesh where the points
143  were found. #gsl_mfem_elem != #gsl_elem for simplices
144  Defaults to 0 for points that were not found.
145  #gsl_ref Reference coordinates of the found point.
146  Ordered by vdim (XYZ,XYZ,XYZ...). Defaults to -1 for
147  points that were not found. Note: the gslib reference
148  frame is [-1,1].
149  #gsl_mfem_ref Reference coordinates #gsl_ref mapped to [0,1].
150  Defaults to 0 for points that were not found.
151  #gsl_dist Distance between the sought and the found point
152  in physical space. */
153  void FindPoints(const Vector &point_pos,
154  int point_pos_ordering = Ordering::byNODES);
155  /// Setup FindPoints and search positions
156  void FindPoints(Mesh &m, const Vector &point_pos,
157  int point_pos_ordering = Ordering::byNODES,
158  const double bb_t = 0.1,
159  const double newt_tol = 1.0e-12, const int npt_max = 256);
160 
161  /** Interpolation of field values at prescribed reference space positions.
162  @param[in] field_in Function values that will be interpolated on the
163  reference positions. Note: it is assumed that
164  @a field_in is in H1 and in the same space as the
165  mesh that was given to Setup().
166  @param[out] field_out Interpolated values. For points that are not found
167  the value is set to #default_interp_value. */
168  virtual void Interpolate(const GridFunction &field_in, Vector &field_out);
169  /** Search positions and interpolate. The ordering (byNODES or byVDIM) of
170  the output values in @a field_out corresponds to the ordering used
171  in the input GridFunction @a field_in. */
172  void Interpolate(const Vector &point_pos, const GridFunction &field_in,
173  Vector &field_out,
174  int point_pos_ordering = Ordering::byNODES);
175  /** Setup FindPoints, search positions and interpolate. The ordering (byNODES
176  or byVDIM) of the output values in @a field_out corresponds to the
177  ordering used in the input GridFunction @a field_in. */
178  void Interpolate(Mesh &m, const Vector &point_pos,
179  const GridFunction &field_in, Vector &field_out,
180  int point_pos_ordering = Ordering::byNODES);
181 
182  /// Average type to be used for L2 functions in-case a point is located at
183  /// an element boundary where the function might be multi-valued.
184  virtual void SetL2AvgType(AvgType avgtype_) { avgtype = avgtype_; }
185 
186  /// Set the default interpolation value for points that are not found in the
187  /// mesh.
188  virtual void SetDefaultInterpolationValue(double interp_value_)
189  {
190  default_interp_value = interp_value_;
191  }
192 
193  /// Set the tolerance for detecting points outside the 'curvilinear' boundary
194  /// that gslib may return as found on the boundary. Points found on boundary
195  /// with distance greater than @ bdr_tol are marked as not found.
196  virtual void SetDistanceToleranceForPointsFoundOnBoundary(double bdr_tol_)
197  {
198  bdr_tol = bdr_tol_;
199  }
200 
201  /** Cleans up memory allocated internally by gslib.
202  Note that in parallel, this must be called before MPI_Finalize(), as it
203  calls MPI_Comm_free() for internal gslib communicators. */
204  virtual void FreeData();
205 
206  /// Return code for each point searched by FindPoints: inside element (0), on
207  /// element boundary (1), or not found (2).
208  virtual const Array<unsigned int> &GetCode() const { return gsl_code; }
209  /// Return element number for each point found by FindPoints.
210  virtual const Array<unsigned int> &GetElem() const { return gsl_mfem_elem; }
211  /// Return MPI rank on which each point was found by FindPoints.
212  virtual const Array<unsigned int> &GetProc() const { return gsl_proc; }
213  /// Return reference coordinates for each point found by FindPoints.
214  virtual const Vector &GetReferencePosition() const { return gsl_mfem_ref; }
215  /// Return distance between the sought and the found point in physical space,
216  /// for each point found by FindPoints.
217  virtual const Vector &GetDist() const { return gsl_dist; }
218 
219  /// Return element number for each point found by FindPoints corresponding to
220  /// GSLIB mesh. gsl_mfem_elem != gsl_elem for mesh with simplices.
221  virtual const Array<unsigned int> &GetGSLIBElem() const { return gsl_elem; }
222  /// Return reference coordinates in [-1,1] (internal range in GSLIB) for each
223  /// point found by FindPoints.
224  virtual const Vector &GetGSLIBReferencePosition() const { return gsl_ref; }
225 };
226 
227 /** \brief OversetFindPointsGSLIB enables use of findpts for arbitrary number of
228  overlapping grids.
229  The parameters in this class are the same as FindPointsGSLIB with the
230  difference of additional inputs required to account for more than 1 mesh. */
232 {
233 protected:
234  bool overset;
235  unsigned int u_meshid;
236  Vector distfint; // Used to store nodal vals of grid func. passed to findpts
237 
238 public:
240  overset(true) { }
241 
242 #ifdef MFEM_USE_MPI
243  OversetFindPointsGSLIB(MPI_Comm comm_) : FindPointsGSLIB(comm_),
244  overset(true) { }
245 #endif
246 
247  /** Initializes the internal mesh in gslib, by sending the positions of the
248  Gauss-Lobatto nodes of the input Mesh object @a m.
249  Note: not tested with periodic meshes (L2).
250  Note: the input mesh @a m must have Nodes set.
251 
252  @param[in] m Input mesh.
253  @param[in] meshid A unique # for each overlapping mesh. This id is
254  used to make sure that points being searched are not
255  looked for in the mesh that they belong to.
256  @param[in] gfmax (Optional) GridFunction in H1 that is used as a
257  discriminator when one point is located in multiple
258  meshes. The mesh that maximizes gfmax is chosen.
259  For example, using the distance field based on the
260  overlapping boundaries is helpful for convergence
261  during Schwarz iterations.
262  @param[in] bb_t (Optional) Relative size of bounding box around
263  each element.
264  @param[in] newt_tol (Optional) Newton tolerance for the gslib
265  search methods.
266  @param[in] npt_max (Optional) Number of points for simultaneous
267  iteration. This alters performance and
268  memory footprint.*/
269  void Setup(Mesh &m, const int meshid, GridFunction *gfmax = NULL,
270  const double bb_t = 0.1, const double newt_tol = 1.0e-12,
271  const int npt_max = 256);
272 
273  /** Searches positions given in physical space by @a point_pos. All output
274  Arrays and Vectors are expected to have the correct size.
275 
276  @param[in] point_pos Positions to be found.
277  @param[in] point_id Index of the mesh that the point belongs
278  to (corresponding to @a meshid in Setup).
279  @param[in] point_pos_ordering Ordering of the points:
280  byNodes: (XXX...,YYY...,ZZZ) or
281  byVDim: (XYZ,XYZ,....XYZ) */
282  void FindPoints(const Vector &point_pos,
283  Array<unsigned int> &point_id,
284  int point_pos_ordering = Ordering::byNODES);
285 
286  /** Search positions and interpolate */
287  void Interpolate(const Vector &point_pos, Array<unsigned int> &point_id,
288  const GridFunction &field_in, Vector &field_out,
289  int point_pos_ordering = Ordering::byNODES);
291 };
292 
293 } // namespace mfem
294 
295 #endif // MFEM_USE_GSLIB
296 
297 #endif // MFEM_GSLIB
virtual void Interpolate(const GridFunction &field_in, Vector &field_out)
Definition: gslib.cpp:820
virtual const Array< unsigned int > & GetProc() const
Return MPI rank on which each point was found by FindPoints.
Definition: gslib.hpp:212
Array< unsigned int > gsl_elem
Definition: gslib.hpp:72
Class for an integration rule - an Array of IntegrationPoint.
Definition: intrules.hpp:96
virtual const Array< unsigned int > & GetGSLIBElem() const
Definition: gslib.hpp:221
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:30
virtual ~FindPointsGSLIB()
Definition: gslib.cpp:67
void FindPoints(const Vector &point_pos, int point_pos_ordering=Ordering::byNODES)
Definition: gslib.cpp:176
Array< GridFunction * > gf_rst_map
Definition: gslib.hpp:65
Array< unsigned int > gsl_mfem_elem
Definition: gslib.hpp:72
virtual void SetupSplitMeshes()
Definition: gslib.cpp:301
void Interpolate(const Vector &point_pos, Array< unsigned int > &point_id, const GridFunction &field_in, Vector &field_out, int point_pos_ordering=Ordering::byNODES)
Definition: gslib.cpp:1301
OversetFindPointsGSLIB(MPI_Comm comm_)
Definition: gslib.hpp:243
Array< unsigned int > gsl_proc
Definition: gslib.hpp:72
Array< Mesh * > mesh_split
Definition: gslib.hpp:59
virtual void SetL2AvgType(AvgType avgtype_)
Definition: gslib.hpp:184
virtual const Array< unsigned int > & GetElem() const
Return element number for each point found by FindPoints.
Definition: gslib.hpp:210
virtual void MapRefPosAndElemIndices()
Definition: gslib.cpp:673
struct gslib::findpts_data_3 * fdata3D
Definition: gslib.hpp:68
virtual void GetNodalValues(const GridFunction *gf_in, Vector &node_vals)
Get GridFunction value at the points expected by GSLIB.
Definition: gslib.cpp:572
Array< IntegrationRule * > ir_split
Definition: gslib.hpp:62
virtual const Vector & GetGSLIBReferencePosition() const
Definition: gslib.hpp:224
OversetFindPointsGSLIB enables use of findpts for arbitrary number of overlapping grids...
Definition: gslib.hpp:231
virtual void FreeData()
Definition: gslib.cpp:272
void Setup(Mesh &m, const double bb_t=0.1, const double newt_tol=1.0e-12, const int npt_max=256)
Definition: gslib.cpp:107
double default_interp_value
Definition: gslib.hpp:75
struct gslib::crystal * cr
Definition: gslib.hpp:69
Array< int > split_element_index
Definition: gslib.hpp:78
Array< FiniteElementSpace * > fes_rst_map
Definition: gslib.hpp:64
Array< unsigned int > gsl_code
Definition: gslib.hpp:72
Collection of finite elements from the same family in multiple dimensions. This class is used to matc...
Definition: fe_coll.hpp:26
virtual void InterpolateGeneral(const GridFunction &field_in, Vector &field_out)
Definition: gslib.cpp:975
virtual void SetupIntegrationRuleForSplitMesh(Mesh *mesh, IntegrationRule *irule, int order)
Definition: gslib.cpp:524
struct gslib::findpts_data_2 * fdata2D
Definition: gslib.hpp:67
virtual void SetDefaultInterpolationValue(double interp_value_)
Definition: gslib.hpp:188
virtual void SetDistanceToleranceForPointsFoundOnBoundary(double bdr_tol_)
Definition: gslib.hpp:196
Array< int > split_element_map
Definition: gslib.hpp:77
virtual const Vector & GetDist() const
Definition: gslib.hpp:217
struct gslib::comm * gsl_comm
Definition: gslib.hpp:70
FiniteElementCollection * fec_map_lin
Definition: gslib.hpp:66
FindPointsGSLIB can robustly evaluate a GridFunction on an arbitrary collection of points...
Definition: gslib.hpp:52
Vector data type.
Definition: vector.hpp:58
Definition: gslib.cpp:23
virtual const Array< unsigned int > & GetCode() const
Definition: gslib.hpp:208
void FindPoints(const Vector &point_pos, Array< unsigned int > &point_id, int point_pos_ordering=Ordering::byNODES)
Definition: gslib.cpp:1223
void Setup(Mesh &m, const int meshid, GridFunction *gfmax=NULL, const double bb_t=0.1, const double newt_tol=1.0e-12, const int npt_max=256)
Definition: gslib.cpp:1135
virtual void InterpolateH1(const GridFunction &field_in, Vector &field_out)
Use GSLIB for communication and interpolation.
Definition: gslib.cpp:903
virtual const Vector & GetReferencePosition() const
Return reference coordinates for each point found by FindPoints.
Definition: gslib.hpp:214