Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-2021 The VES code team 3 : (see the PEOPLE-VES file at the root of this folder for a list of names) 4 : 5 : See http://www.ves-code.org for more information. 6 : 7 : This file is part of VES code module. 8 : 9 : The VES code module is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : The VES code module is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with the VES code module. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #ifndef __PLUMED_ves_GridLinearInterpolation_h 23 : #define __PLUMED_ves_GridLinearInterpolation_h 24 : 25 : #include <vector> 26 : 27 : 28 : namespace PLMD { 29 : 30 : 31 : class GridBase; 32 : 33 : 34 : namespace ves { 35 : 36 : class GridLinearInterpolation { 37 : private: 38 : static double getGridValueWithLinearInterpolation_1D(GridBase* grid_pntr, const std::vector<double>& arg); 39 : static double getGridValueWithLinearInterpolation_2D(GridBase* grid_pntr, const std::vector<double>& arg); 40 : static double getGridValueWithLinearInterpolation_ND(GridBase* grid_pntr, const std::vector<double>& arg); 41 : static double getGridValueAndDerivativesWithLinearInterpolation_1D(GridBase* grid_pntr, const std::vector<double>& arg, std::vector<double>& der); 42 : static double getGridValueAndDerivativesWithLinearInterpolation_ND(GridBase* grid_pntr, const std::vector<double>& arg, std::vector<double>& der); 43 : static double linearInterpolation(const double x, const double x0, const double x1, const double y0, const double y1); 44 : static double multiLinearInterpolation(const std::vector<double>& x, const std::vector<std::vector<double>>& points, std::vector<double>& values, const double dim); 45 : public: 46 : static double getGridValueWithLinearInterpolation(GridBase* grid_pntr, const std::vector<double>& arg); 47 : static double getGridValueAndDerivativesWithLinearInterpolation(GridBase* grid_pntr, const std::vector<double>& arg, std::vector<double>& der); 48 : static std::vector<std::vector<unsigned>> getAdjacentIndices(GridBase* grid_pntr, const std::vector<double>& arg); 49 : static std::vector<std::vector<unsigned>> getAdjacentPoints(GridBase* grid_pntr, const std::vector<double>& arg); 50 : }; 51 : 52 : 53 : inline 54 : double GridLinearInterpolation::linearInterpolation(const double x, const double x0, const double x1, const double y0, const double y1) { 55 : // https://en.wikipedia.org/wiki/Linear_interpolation 56 2624038 : if(x1!=x0) { 57 2586242 : return y0 + (x-x0) * ((y1-y0)/(x1-x0)); 58 : } 59 : else { 60 : return y0; 61 : } 62 : } 63 : 64 : 65 : inline 66 0 : double GridLinearInterpolation::multiLinearInterpolation(const std::vector<double>& x, const std::vector<std::vector<double>>& points, std::vector<double>& values, const double dim) { 67 0 : for (unsigned direction = 0; direction < dim; ++direction) { 68 0 : unsigned shift = 1<<(direction+1); // shift by 2, then 4, then 8 etc 69 0 : for (unsigned i = 0; i < points.size(); i += shift) { 70 : // replace every second value with interpolated ones 71 0 : values[i] = linearInterpolation( 72 0 : x[direction], points[i][direction], points[i+shift/2][direction], values[i], values[i+shift/2]); 73 : } 74 : } 75 0 : return values[0]; 76 : } 77 : 78 : 79 : } 80 : } 81 : 82 : #endif