meshfree_interpolation.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_MESHFREE_INTERPOLATION_H
21 #define LIBMESH_MESHFREE_INTERPOLATION_H
22 
23 // Local includes
24 #include "libmesh/libmesh_config.h"
25 #include "libmesh/libmesh_common.h"
26 #include "libmesh/auto_ptr.h"
27 #include "libmesh/point.h"
29 #ifdef LIBMESH_HAVE_NANOFLANN
30 # include "libmesh/nanoflann.hpp"
31 #endif
32 
33 // C++ includes
34 #include <string>
35 #include <vector>
36 
37 
38 
39 namespace libMesh
40 {
41 
52 {
53 public:
54 
73  LIBMESH_CAN_DEFAULT_TO_COMMWORLD) :
74  ParallelObject(comm_in),
76  {}
77 
82  void print_info (std::ostream& os=libMesh::out) const;
83 
87  friend std::ostream& operator << (std::ostream& os,
88  const MeshfreeInterpolation& mfi);
89 
94  virtual void clear();
95 
99  unsigned int n_field_variables () const
100  { return libmesh_cast_int<unsigned int>(_names.size()); }
101 
106  void set_field_variables (const std::vector<std::string> &names)
107  { _names = names; }
108 
112  const std::vector<std::string> & field_variables() const
113  { return _names; }
114 
118  std::vector<Point> & get_source_points ()
119  { return _src_pts; }
120 
124  std::vector<Number> & get_source_vals ()
125  { return _src_vals; }
126 
130  virtual void add_field_data (const std::vector<std::string> &field_names,
131  const std::vector<Point> &pts,
132  const std::vector<Number> &vals);
133 
140  virtual void prepare_for_use ();
141 
146  virtual void interpolate_field_data (const std::vector<std::string> &field_names,
147  const std::vector<Point> &tgt_pts,
148  std::vector<Number> &tgt_vals) const = 0;
149 
150 protected:
151 
160  virtual void gather_remote_data ();
161 
163  std::vector<std::string> _names;
164  std::vector<Point> _src_pts;
165  std::vector<Number> _src_vals;
166 };
167 
168 
169 
173 template <unsigned int KDDim>
175 {
176 protected:
177 
178 #ifdef LIBMESH_HAVE_NANOFLANN
179 
185  template <unsigned int PLDim>
187  {
188  private:
189  const std::vector<Point> &_pts;
190 
191  public:
192  PointListAdaptor (const std::vector<Point> &pts) :
193  _pts(pts)
194  {}
195 
199  typedef Real coord_t;
200 
204  inline size_t kdtree_get_point_count() const { return _pts.size(); }
205 
210  inline coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2, size_t size) const
211  {
212  libmesh_assert_equal_to (size, PLDim);
213  libmesh_assert_less (idx_p2, _pts.size());
214 
215  const Point &p2(_pts[idx_p2]);
216 
217  switch (size)
218  {
219  case 3:
220  {
221  const coord_t d0=p1[0] - p2(0);
222  const coord_t d1=p1[1] - p2(1);
223  const coord_t d2=p1[2] - p2(2);
224 
225  return d0*d0 + d1*d1 + d2*d2;
226  }
227 
228  case 2:
229  {
230  const coord_t d0=p1[0] - p2(0);
231  const coord_t d1=p1[1] - p2(1);
232 
233  return d0*d0 + d1*d1;
234  }
235 
236  case 1:
237  {
238  const coord_t d0=p1[0] - p2(0);
239 
240  return d0*d0;
241  }
242 
243  default:
244  libMesh::err << "ERROR: unknown size " << size << std::endl;
245  libmesh_error();
246  }
247 
248  return -1.;
249  }
250 
256  inline coord_t kdtree_get_pt(const size_t idx, int dim) const
257  {
258  libmesh_assert_less (dim, (int) PLDim);
259  libmesh_assert_less (idx, _pts.size());
260  libmesh_assert_less (dim, 3);
261 
262  const Point &p(_pts[idx]);
263 
264  if (dim==0) return p(0);
265  if (dim==1) return p(1);
266  return p(2);
267  }
268 
275  template <class BBOX>
276  bool kdtree_get_bbox(BBOX & /* bb */) const { return false; }
277  };
278 
280 
281  // template <int KDDIM>
282  // class KDTree : public KDTreeSingleIndexAdaptor<L2_Simple_Adaptor<num_t, PointListAdaptor >,
283  // PointListAdaptor,
284  // KDDIM>
285  // {
286  // };
287 
288  typedef nanoflann::KDTreeSingleIndexAdaptor<nanoflann::L2_Simple_Adaptor<Real, PointListAdaptor<KDDim> >,
290 
292 
293 #endif // LIBMESH_HAVE_NANOFLANN
294 
298  virtual void construct_kd_tree ();
299 
304  virtual void interpolate (const Point &pt,
305  const std::vector<size_t> &src_indices,
306  const std::vector<Real> &src_dist_sqr,
307  std::vector<Number>::iterator &out_it) const;
308 
310  const unsigned int _n_interp_pts;
311 
315  mutable std::vector<Number> _vals;
316 
317 public:
318 
324  const unsigned int n_interp_pts = 8,
325  const Real power = 2) :
326  MeshfreeInterpolation(comm),
327 #if LIBMESH_HAVE_NANOFLANN
329 #endif
330  _half_power(power/2.0),
331  _n_interp_pts(n_interp_pts)
332  {}
333 
338  virtual void clear();
339 
344  virtual void interpolate_field_data (const std::vector<std::string> &field_names,
345  const std::vector<Point> &tgt_pts,
346  std::vector<Number> &tgt_vals) const;
347 
348 };
349 
350 } // namespace libMesh
351 
352 
353 #endif // #define LIBMESH_MESHFREE_INTERPOLATION_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo