MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
gslib.cpp
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 #include "gslib.hpp"
13 
14 #ifdef MFEM_USE_GSLIB
15 
16 // Ignore warnings from the gslib header (GCC version)
17 #ifdef MFEM_HAVE_GCC_PRAGMA_DIAGNOSTIC
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wunused-function"
20 #endif
21 
22 #include "gslib.h"
23 
24 #ifdef MFEM_HAVE_GCC_PRAGMA_DIAGNOSTIC
25 #pragma GCC diagnostic pop
26 #endif
27 
28 namespace mfem
29 {
30 
32  : mesh(NULL), gsl_mesh(), fdata2D(NULL), fdata3D(NULL), dim(-1)
33 {
34  gsl_comm = new comm;
35 #ifdef MFEM_USE_MPI
36  MPI_Init(NULL, NULL);
37  MPI_Comm comm = MPI_COMM_WORLD;;
38  comm_init(gsl_comm, comm);
39 #else
40  comm_init(gsl_comm, 0);
41 #endif
42 }
43 
45 {
46  delete gsl_comm;
47 }
48 
49 #ifdef MFEM_USE_MPI
51  : mesh(NULL), gsl_mesh(), fdata2D(NULL), fdata3D(NULL), dim(-1)
52 {
53  gsl_comm = new comm;
54  comm_init(gsl_comm, _comm);
55 }
56 #endif
57 
58 void FindPointsGSLIB::Setup(Mesh &m, double bb_t, double newt_tol, int npt_max)
59 {
60  MFEM_VERIFY(m.GetNodes() != NULL, "Mesh nodes are required.");
61 
62  mesh = &m;
63  const GridFunction *nodes = mesh->GetNodes();
64  const FiniteElementSpace *fes = nodes->FESpace();
65 
66  dim = mesh->Dimension();
67  const int NE = mesh->GetNE(),
68  dof_cnt = fes->GetFE(0)->GetDof(),
69  pts_cnt = NE * dof_cnt;
70  gsl_mesh.SetSize(dim * pts_cnt);
71 
72  const TensorBasisElement *tbe =
73  dynamic_cast<const TensorBasisElement *>(fes->GetFE(0));
74  const Array<int> &dof_map = tbe->GetDofMap();
75 
76  DenseMatrix pos(dof_cnt, dim);
77  Vector posV(pos.Data(), dof_cnt * dim);
78  Array<int> xdofs(dof_cnt * dim);
79 
80  int pt_id = 0;
81  for (int i = 0; i < NE; i++)
82  {
83  fes->GetElementVDofs(i, xdofs);
84  nodes->GetSubVector(xdofs, posV);
85  for (int j = 0; j < dof_cnt; j++)
86  {
87  for (int d = 0; d < dim; d++)
88  {
89  gsl_mesh(pts_cnt * d + pt_id) = pos(dof_map[j], d);
90  }
91  pt_id++;
92  }
93  }
94 
95  const unsigned dof1D = fes->GetFE(0)->GetOrder() + 1;
96  if (dim == 2)
97  {
98  unsigned nr[2] = {dof1D, dof1D};
99  unsigned mr[2] = {2*dof1D, 2*dof1D};
100  double * const elx[2] = { &gsl_mesh(0), &gsl_mesh(pts_cnt) };
101  fdata2D = findpts_setup_2(gsl_comm, elx, nr, NE, mr, bb_t,
102  pts_cnt, pts_cnt, npt_max, newt_tol);
103  }
104  else
105  {
106  unsigned nr[3] = {dof1D, dof1D, dof1D};
107  unsigned mr[3] = {2*dof1D, 2*dof1D, 2*dof1D};
108  double * const elx[3] =
109  { &gsl_mesh(0), &gsl_mesh(pts_cnt), &gsl_mesh(2*pts_cnt) };
110  fdata3D = findpts_setup_3(gsl_comm, elx, nr, NE, mr, bb_t,
111  pts_cnt, pts_cnt, npt_max, newt_tol);
112  }
113 }
114 
116  Array<unsigned int> &proc_ids,
117  Array<unsigned int> &elem_ids,
118  Vector &ref_pos, Vector &dist)
119 {
120  const int points_cnt = point_pos.Size() / dim;
121  if (dim == 2)
122  {
123  const double *xv_base[2];
124  xv_base[0] = point_pos.GetData();
125  xv_base[1] = point_pos.GetData() + points_cnt;
126  unsigned xv_stride[2];
127  xv_stride[0] = sizeof(double);
128  xv_stride[1] = sizeof(double);
129  findpts_2(codes.GetData(), sizeof(unsigned int),
130  proc_ids.GetData(), sizeof(unsigned int),
131  elem_ids.GetData(), sizeof(unsigned int),
132  ref_pos.GetData(), sizeof(double) * dim,
133  dist.GetData(), sizeof(double),
134  xv_base, xv_stride, points_cnt, fdata2D);
135  }
136  else
137  {
138  const double *xv_base[3];
139  xv_base[0] = point_pos.GetData();
140  xv_base[1] = point_pos.GetData() + points_cnt;
141  xv_base[2] = point_pos.GetData() + 2*points_cnt;
142  unsigned xv_stride[3];
143  xv_stride[0] = sizeof(double);
144  xv_stride[1] = sizeof(double);
145  xv_stride[2] = sizeof(double);
146  findpts_3(codes.GetData(), sizeof(unsigned int),
147  proc_ids.GetData(), sizeof(unsigned int),
148  elem_ids.GetData(), sizeof(unsigned int),
149  ref_pos.GetData(), sizeof(double) * dim,
150  dist.GetData(), sizeof(double),
151  xv_base, xv_stride, points_cnt, fdata3D);
152  }
153 }
154 
156  Array<unsigned int> &proc_ids,
157  Array<unsigned int> &elem_ids,
158  Vector &ref_pos, const GridFunction &field_in,
159  Vector &field_out)
160 {
161  Vector node_vals;
162  GetNodeValues(field_in, node_vals);
163 
164  const int points_cnt = ref_pos.Size() / dim;
165  if (dim==2)
166  {
167  findpts_eval_2(field_out.GetData(), sizeof(double),
168  codes.GetData(), sizeof(unsigned int),
169  proc_ids.GetData(), sizeof(unsigned int),
170  elem_ids.GetData(), sizeof(unsigned int),
171  ref_pos.GetData(), sizeof(double) * dim,
172  points_cnt, node_vals.GetData(), fdata2D);
173  }
174  else
175  {
176  findpts_eval_3(field_out.GetData(), sizeof(double),
177  codes.GetData(), sizeof(unsigned int),
178  proc_ids.GetData(), sizeof(unsigned int),
179  elem_ids.GetData(), sizeof(unsigned int),
180  ref_pos.GetData(), sizeof(double) * dim,
181  points_cnt, node_vals.GetData(), fdata3D);
182  }
183 }
184 
186 {
187  (dim == 2) ? findpts_free_2(fdata2D) : findpts_free_3(fdata3D);
188 }
189 
191  Vector &node_vals)
192 {
193  MFEM_ASSERT(gf_in.FESpace()->GetVDim() == 1, "Scalar function expected.");
194 
195  const GridFunction *nodes = mesh->GetNodes();
196  const FiniteElementSpace *fes = nodes->FESpace();
197  const IntegrationRule &ir = fes->GetFE(0)->GetNodes();
198 
199  const int NE = mesh->GetNE(), dof_cnt = ir.GetNPoints();
200  node_vals.SetSize(NE * dof_cnt);
201 
202  const TensorBasisElement *tbe =
203  dynamic_cast<const TensorBasisElement *>(fes->GetFE(0));
204  const Array<int> &dof_map = tbe->GetDofMap();
205 
206  int pt_id = 0;
207  Vector vals_el;
208  for (int i = 0; i < NE; i++)
209  {
210  gf_in.GetValues(i, ir, vals_el);
211  for (int j = 0; j < dof_cnt; j++)
212  {
213  node_vals(pt_id++) = vals_el(dof_map[j]);
214  }
215  }
216 }
217 
218 } // namespace mfem
219 
220 #endif // MFEM_USE_GSLIB
int GetNPoints() const
Returns the number of the points in the integration rule.
Definition: intrules.hpp:245
void FindPoints(Vector &point_pos, Array< unsigned int > &codes, Array< unsigned int > &proc_ids, Array< unsigned int > &elem_ids, Vector &ref_pos, Vector &dist)
Definition: gslib.cpp:115
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:27
struct findpts_data_3 * fdata3D
Definition: gslib.hpp:33
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:407
void GetElementVDofs(int i, Array< int > &vdofs) const
Returns indexes of degrees of freedom in array dofs for i&#39;th element.
Definition: fespace.cpp:172
void Setup(Mesh &m, double bb_t, double newt_tol, int npt_max)
Definition: gslib.cpp:58
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Definition: vector.cpp:472
T * GetData()
Returns the data.
Definition: array.hpp:98
int GetOrder() const
Returns the order of the finite element. In the case of anisotropic orders, returns the maximum order...
Definition: fe.hpp:318
Data type dense matrix using column-major storage.
Definition: densemat.hpp:23
int Size() const
Returns the size of the vector.
Definition: vector.hpp:157
int GetNE() const
Returns number of elements.
Definition: mesh.hpp:693
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:166
void GetNodeValues(const GridFunction &gf_in, Vector &node_vals)
Definition: gslib.cpp:190
void Interpolate(Array< unsigned int > &codes, Array< unsigned int > &proc_ids, Array< unsigned int > &elem_ids, Vector &ref_pos, const GridFunction &field_in, Vector &field_out)
Definition: gslib.cpp:155
struct comm * gsl_comm
Definition: gslib.hpp:36
const IntegrationRule & GetNodes() const
Definition: fe.hpp:367
void GetValues(int i, const IntegrationRule &ir, Vector &vals, int vdim=1) const
Definition: gridfunc.cpp:442
int GetVDim() const
Returns vector dimension.
Definition: fespace.hpp:370
FiniteElementSpace * FESpace()
Definition: gridfunc.hpp:442
double * Data() const
Returns the matrix data array.
Definition: densemat.hpp:96
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:87
int GetDof() const
Returns the number of degrees of freedom in the finite element.
Definition: fe.hpp:314
const Array< int > & GetDofMap() const
Get an Array&lt;int&gt; that maps lexicographically ordered indices to the indices of the respective nodes/...
Definition: fe.hpp:1850
int dim
Definition: ex24.cpp:43
const FiniteElement * GetFE(int i) const
Returns pointer to the FiniteElement associated with i&#39;th element.
Definition: fespace.cpp:1642
Vector data type.
Definition: vector.hpp:48
void GetNodes(Vector &node_coord) const
Definition: mesh.cpp:6203
struct findpts_data_2 * fdata2D
Definition: gslib.hpp:32