21 #include "libmesh/radial_basis_interpolation.h" 23 #include "libmesh/eigen_core_support.h" 24 #include "libmesh/int_range.h" 25 #include "libmesh/libmesh_logging.h" 26 #include "libmesh/mesh_tools.h" 27 #include "libmesh/radial_basis_functions.h" 29 #ifdef LIBMESH_HAVE_EIGEN 30 # include "libmesh/ignore_warnings.h" 31 # include <Eigen/Dense> 32 # include "libmesh/restore_warnings.h" 43 template <
unsigned int KDDim,
class RBF>
52 template <
unsigned int KDDim,
class RBF>
59 #ifndef LIBMESH_HAVE_EIGEN 61 libmesh_error_msg(
"ERROR: this functionality presently requires Eigen!");
64 LOG_SCOPE (
"prepare_for_use()",
"RadialBasisInterpolation<>");
67 _src_bbox.invalidate();
69 const std::size_t n_src_pts = this->_src_pts.size();
70 const unsigned int n_vars = this->n_field_variables();
71 libmesh_assert_equal_to (this->_src_vals.size(), n_src_pts*this->n_field_variables());
74 LOG_SCOPE (
"prepare_for_use():bbox",
"RadialBasisInterpolation<>");
77 &p_min(_src_bbox.min()),
78 &p_max(_src_bbox.max());
80 for (std::size_t p=0; p<n_src_pts; p++)
82 const Point & p_src(_src_pts[p]);
84 for (
unsigned int d=0; d<LIBMESH_DIM; d++)
86 p_min(d) = std::min(p_min(d), p_src(d));
87 p_max(d) = std::max(p_max(d), p_src(d));
100 _r_bbox = (_src_bbox.max() - _src_bbox.min()).
norm();
102 _r_bbox = _r_override;
114 typedef Eigen::Matrix<Number, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> DynamicMatrix;
117 DynamicMatrix A(n_src_pts, n_src_pts), x(n_src_pts,
n_vars), b(n_src_pts,
n_vars);
120 LOG_SCOPE (
"prepare_for_use():mat",
"RadialBasisInterpolation<>");
122 for (std::size_t i=0; i<n_src_pts; i++)
124 const Point & x_i (_src_pts[i]);
129 for (std::size_t j=i+1; j<n_src_pts; j++)
131 const Point & x_j (_src_pts[j]);
133 const Real r_ij = (x_j - x_i).
norm();
135 A(i,j) = A(j,i) = rbf(r_ij);
139 for (
unsigned int var=0; var<
n_vars; var++)
140 b(i,var) = _src_vals[i*
n_vars + var];
146 LOG_SCOPE (
"prepare_for_use():solve",
"RadialBasisInterpolation<>");
149 x = A.ldlt().solve(b);
154 _weights.resize (this->_src_vals.size());
156 for (std::size_t i=0; i<n_src_pts; i++)
157 for (
unsigned int var=0; var<
n_vars; var++)
158 _weights[i*
n_vars + var] = x(i,var);
166 template <
unsigned int KDDim,
class RBF>
168 const std::vector<Point> & tgt_pts,
169 std::vector<Number> & tgt_vals)
const 171 LOG_SCOPE (
"interpolate_field_data()",
"RadialBasisInterpolation<>");
173 libmesh_experimental();
176 n_vars = this->n_field_variables();
179 n_src_pts = this->_src_pts.size(),
180 n_tgt_pts = tgt_pts.size();
182 libmesh_assert_equal_to (_weights.size(), this->_src_vals.size());
183 libmesh_assert_equal_to (field_names.size(), this->n_field_variables());
187 libmesh_error_msg_if(this->_names.size() != field_names.size(),
188 "ERROR: when adding field data to an existing list the \nvariable list must be the same!");
191 libmesh_error_msg_if(_names[v] != field_names[v],
192 "ERROR: when adding field data to an existing list the \nvariable list must be the same!");
197 tgt_vals.resize (n_tgt_pts*
n_vars); std::fill (tgt_vals.begin(), tgt_vals.end(),
Number(0.));
199 for (std::size_t tgt=0; tgt<n_tgt_pts; tgt++)
201 const Point & p (tgt_pts[tgt]);
203 for (std::size_t i=0; i<n_src_pts; i++)
205 const Point & x_i(_src_pts[i]);
207 r_i = (p - x_i).
norm(),
210 for (
unsigned int var=0; var<
n_vars; var++)
211 tgt_vals[tgt*
n_vars + var] += _weights[i*
n_vars + var]*phi_i;
Radial Basis Function interpolation.
virtual void prepare_for_use() override
Prepares data structures for use.
virtual void interpolate_field_data(const std::vector< std::string > &field_names, const std::vector< Point > &tgt_pts, std::vector< Number > &tgt_vals) const override
Interpolate source data at target points.
The libMesh namespace provides an interface to certain functionality in the library.
virtual void construct_kd_tree()
Build & initialize the KD tree, if needed.
virtual void clear() override
Clears all internal data structures and restores to a pristine state.
virtual void clear() override
Clears all internal data structures and restores to a pristine state.
virtual void prepare_for_use()
Prepares data structures for use.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
A Point defines a location in LIBMESH_DIM dimensional Real space.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...