[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
rbf_interpolation.cpp
1 #include "rbf_interpolation.h"
2 #include <eigen/Eigen/Dense>
3 #include <eigen/Eigen/LU>
4 #include <iostream>
5 #include "ROL_StdVector.hpp"
6 
7 namespace PHiLiP {
8 namespace ProperOrthogonalDecomposition {
9 
10 RBFInterpolation::RBFInterpolation(const MatrixXd& data_coordinates, const VectorXd& data_values, std::string kernel)
11  : data_coordinates(data_coordinates)
12  , data_values(data_values)
13  , kernel(kernel)
14 {
16 }
17 
19  long N = data_coordinates.rows();
20 
21  MatrixXd A;
22  A.resize(N,N);
23 
24  for(unsigned int i = 0 ; i < N ; i++){
25  for(unsigned int j = i ; j < N ; j++){
26  double point = (data_coordinates.row(i) - data_coordinates.row(j)).norm();
27  //std::cout << point << std::endl;
28  A(i,j) = radialBasisFunction(point);
29  A(j,i) = A(i,j);
30  }
31  }
32 
33  weights = A.lu().solve(data_values);
34 }
35 
37  if(kernel == "thin_plate_spline"){
38  if(r > 0){
39  return std::pow(r, 2) * std::log(r);
40  }
41  else{
42  return 0;
43  }
44  }
45  else if(kernel == "cubic"){
46  return std::pow(r, 3);
47  }
48  else if(kernel == "linear"){
49  return r;
50  }
51  else{
52  return std::pow(r, 2) * std::log(r);
53  }
54 }
55 
56 double RBFInterpolation::evaluate(const RowVectorXd& evaluate_coordinate) const {
57  long N = data_coordinates.rows();
58 
59  RowVectorXd s(1,N);
60 
61  for(unsigned int i = 0 ; i < N ; i++){
62  double point = (evaluate_coordinate - data_coordinates.row(i)).norm();
63  s(0,i) = radialBasisFunction(point);
64  }
65 
66  //std::cout << s*weights << std::endl;
67 
68  return s*weights;
69 }
70 
71 double RBFInterpolation::value(const ROL::Vector<double> &x, double &/*tol*/ ) {
72  ROL::Ptr<const vector> xp = getVector<ROL::StdVector<double>>(x);
73  RowVectorXd evaluate_coordinate(2);
74  evaluate_coordinate(0) = (*xp)[0];
75  evaluate_coordinate(1) = (*xp)[1];
76  double val = evaluate(evaluate_coordinate);
77 
78  //For optimization, return -abs(val) to consider only magnitude of error, not sign
79  return -std::abs(val);
80 }
81 
82 }
83 }
double evaluate(const RowVectorXd &evaluate_coordinate) const
Evaluate RBF.
Files for the baseline physics.
Definition: ADTypes.hpp:10
RBFInterpolation(const MatrixXd &data_coordinates, const VectorXd &data_values, std::string kernel)
Constructor.
double radialBasisFunction(double r) const
Choose radial basis function.
void computeWeights()
Compute RBF interpolation weights.
real1 norm(const dealii::Tensor< 1, dim, real1 > x)
Returns norm of dealii::Tensor<1,dim,real>
double value(const ROL::Vector< double > &x, double &)
ROL evaluate value.