MFEM  v4.2.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
gslib.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, 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 struct comm;
21 struct findpts_data_2;
22 struct findpts_data_3;
23 struct array;
24 struct crystal;
25 
26 namespace mfem
27 {
28 
29 /** \brief FindPointsGSLIB can robustly evaluate a GridFunction on an arbitrary
30  * collection of points. There are three key functions in FindPointsGSLIB:
31  *
32  * 1. Setup - constructs the internal data structures of gslib.
33  *
34  * 2. FindPoints - for any given arbitrary set of points in physical space,
35  * gslib finds the element number, MPI rank, and the reference space
36  * coordinates inside the element that each point is located in. gslib also
37  * returns a code that indicates whether the point was found inside an
38  * element, on element border, or not found in the domain.
39  *
40  * 3. Interpolate - Interpolates any grid function at the points found using 2.
41  *
42  * FindPointsGSLIB provides interface to use these functions individually or
43  * using a single call.
44  */
46 {
47 public:
48  enum AvgType {NONE, ARITHMETIC, HARMONIC}; // Average type for L2 functions
49 
50 protected:
52  IntegrationRule *ir_simplex; // IntegrationRule to split quads/hex -> simplex
53  struct findpts_data_2 *fdata2D; // gslib's internal data
54  struct findpts_data_3 *fdata3D; // gslib's internal data
55  struct crystal *cr; // gslib's internal data
56  struct comm *gsl_comm; // gslib's internal data
60  bool setupflag; // flag to indicate whether gslib data has been setup
61  double default_interp_value; // used for points that are not found in the mesh
62  AvgType avgtype; // average type used for L2 functions
63 
64  /// Get GridFunction from MFEM format to GSLIB format
65  void GetNodeValues(const GridFunction &gf_in, Vector &node_vals);
66  /// Get nodal coordinates from mesh to the format expected by GSLIB for quads
67  /// and hexes
69  /// Convert simplices to quad/hexes and then get nodal coordinates for each
70  /// split element into format expected by GSLIB
72 
73  /// Use GSLIB for communication and interpolation
74  void InterpolateH1(const GridFunction &field_in, Vector &field_out);
75  /// Uses GSLIB Crystal Router for communication followed by MFEM's
76  /// interpolation functions
77  void InterpolateGeneral(const GridFunction &field_in, Vector &field_out);
78  /// Map {r,s,t} coordinates from [-1,1] to [0,1] for MFEM. For simplices mesh
79  /// find the original element number (that was split into micro quads/hexes
80  /// by GetSimplexNodalCoordinates())
82 
83 public:
85 
86 #ifdef MFEM_USE_MPI
87  FindPointsGSLIB(MPI_Comm _comm);
88 #endif
89 
91 
92  /** Initializes the internal mesh in gslib, by sending the positions of the
93  Gauss-Lobatto nodes of the input Mesh object @a m.
94  Note: not tested with periodic (DG meshes).
95  Note: the input mesh @a m must have Nodes set.
96 
97  @param[in] m Input mesh.
98  @param[in] bb_t Relative size of bounding box around each element.
99  @param[in] newt_tol Newton tolerance for the gslib search methods.
100  @param[in] npt_max Number of points for simultaneous iteration. This
101  alters performance and memory footprint. */
102  void Setup(Mesh &m, const double bb_t = 0.1, const double newt_tol = 1.0e-12,
103  const int npt_max = 256);
104 
105  /** Searches positions given in physical space by @a point_pos. These positions
106  must by ordered by nodes: (XXX...,YYY...,ZZZ).
107  This function populates the following member variables:
108  #gsl_code Return codes for each point: inside element (0),
109  element boundary (1), not found (2).
110  #gsl_proc MPI proc ids where the points were found.
111  #gsl_elem Element ids where the points were found.
112  Defaults to 0 for points that were not found.
113  #gsl_mfem_elem Element ids corresponding to MFEM-mesh where the points
114  were found. #gsl_mfem_elem != #gsl_elem for simplices
115  Defaults to 0 for points that were not found.
116  #gsl_ref Reference coordinates of the found point.
117  Ordered by vdim (XYZ,XYZ,XYZ...). Defaults to -1 for
118  points that were not found. Note: the gslib reference
119  frame is [-1,1].
120  #gsl_mfem_ref Reference coordinates #gsl_ref mapped to [0,1].
121  Defaults to 0 for points that were not found.
122  #gsl_dist Distance between the sought and the found point
123  in physical space. */
124  void FindPoints(const Vector &point_pos);
125  /// Setup FindPoints and search positions
126  void FindPoints(Mesh &m, const Vector &point_pos, const double bb_t = 0.1,
127  const double newt_tol = 1.0e-12, const int npt_max = 256);
128 
129  /** Interpolation of field values at prescribed reference space positions.
130  @param[in] field_in Function values that will be interpolated on the
131  reference positions. Note: it is assumed that
132  @a field_in is in H1 and in the same space as the
133  mesh that was given to Setup().
134  @param[out] field_out Interpolated values. For points that are not found
135  the value is set to #default_interp_value. */
136  void Interpolate(const GridFunction &field_in, Vector &field_out);
137  /** Search positions and interpolate */
138  void Interpolate(const Vector &point_pos, const GridFunction &field_in,
139  Vector &field_out);
140  /** Setup FindPoints, search positions and interpolate */
141  void Interpolate(Mesh &m, const Vector &point_pos,
142  const GridFunction &field_in, Vector &field_out);
143 
144  /// Average type to be used for L2 functions in-case a point is located at
145  /// an element boundary where the function might be multi-valued.
146  void SetL2AvgType(AvgType avgtype_) { avgtype = avgtype_; }
147 
148  /// Set the default interpolation value for points that are not found in the
149  /// mesh.
150  void SetDefaultInterpolationValue(double interp_value_)
151  {
152  default_interp_value = interp_value_;
153  }
154 
155  /** Cleans up memory allocated internally by gslib.
156  Note that in parallel, this must be called before MPI_Finalize(), as it
157  calls MPI_Comm_free() for internal gslib communicators. */
158  void FreeData();
159 
160  /// Return code for each point searched by FindPoints: inside element (0), on
161  /// element boundary (1), or not found (2).
162  const Array<unsigned int> &GetCode() const { return gsl_code; }
163  /// Return element number for each point found by FindPoints.
164  const Array<unsigned int> &GetElem() const { return gsl_mfem_elem; }
165  /// Return MPI rank on which each point was found by FindPoints.
166  const Array<unsigned int> &GetProc() const { return gsl_proc; }
167  /// Return reference coordinates for each point found by FindPoints.
168  const Vector &GetReferencePosition() const { return gsl_mfem_ref; }
169  /// Return distance Distance between the sought and the found point
170  /// in physical space, for each point found by FindPoints.
171  const Vector &GetDist() const { return gsl_dist; }
172 
173  /// Return element number for each point found by FindPoints corresponding to
174  /// GSLIB mesh. gsl_mfem_elem != gsl_elem for mesh with simplices.
175  const Array<unsigned int> &GetGSLIBElem() const { return gsl_elem; }
176  /// Return reference coordinates in [-1,1] (internal range in GSLIB) for each
177  /// point found by FindPoints.
178  const Vector &GetGSLIBReferencePosition() const { return gsl_ref; }
179 };
180 
181 } // namespace mfem
182 
183 #endif // MFEM_USE_GSLIB
184 
185 #endif // MFEM_GSLIB
void Interpolate(const GridFunction &field_in, Vector &field_out)
Definition: gslib.cpp:575
Array< unsigned int > gsl_elem
Definition: gslib.hpp:58
Class for an integration rule - an Array of IntegrationPoint.
Definition: intrules.hpp:90
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:30
const Vector & GetDist() const
Definition: gslib.hpp:171
void SetL2AvgType(AvgType avgtype_)
Definition: gslib.hpp:146
struct findpts_data_3 * fdata3D
Definition: gslib.hpp:54
Array< unsigned int > gsl_mfem_elem
Definition: gslib.hpp:58
const Array< unsigned int > & GetCode() const
Definition: gslib.hpp:162
Array< unsigned int > gsl_proc
Definition: gslib.hpp:58
const Array< unsigned int > & GetProc() const
Return MPI rank on which each point was found by FindPoints.
Definition: gslib.hpp:166
void GetNodeValues(const GridFunction &gf_in, Vector &node_vals)
Get GridFunction from MFEM format to GSLIB format.
Definition: gslib.cpp:229
const Array< unsigned int > & GetElem() const
Return element number for each point found by FindPoints.
Definition: gslib.hpp:164
void MapRefPosAndElemIndices()
Definition: gslib.cpp:485
struct comm * gsl_comm
Definition: gslib.hpp:56
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:71
double default_interp_value
Definition: gslib.hpp:61
void SetDefaultInterpolationValue(double interp_value_)
Definition: gslib.hpp:150
const Vector & GetGSLIBReferencePosition() const
Definition: gslib.hpp:178
Array< unsigned int > gsl_code
Definition: gslib.hpp:58
void GetSimplexNodalCoordinates()
Definition: gslib.cpp:321
void FindPoints(const Vector &point_pos)
Definition: gslib.cpp:125
void InterpolateGeneral(const GridFunction &field_in, Vector &field_out)
Definition: gslib.cpp:692
void GetQuadHexNodalCoordinates()
Definition: gslib.cpp:286
const Vector & GetReferencePosition() const
Return reference coordinates for each point found by FindPoints.
Definition: gslib.hpp:168
const Array< unsigned int > & GetGSLIBElem() const
Definition: gslib.hpp:175
struct crystal * cr
Definition: gslib.hpp:55
IntegrationRule * ir_simplex
Definition: gslib.hpp:52
FindPointsGSLIB can robustly evaluate a GridFunction on an arbitrary collection of points...
Definition: gslib.hpp:45
Vector data type.
Definition: vector.hpp:51
struct findpts_data_2 * fdata2D
Definition: gslib.hpp:53
void InterpolateH1(const GridFunction &field_in, Vector &field_out)
Use GSLIB for communication and interpolation.
Definition: gslib.cpp:650