Grid Interpolation
tests.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <vector>
5 #include <string>
6 
7 #include "gridInterp.h"
8 
9 
10 void test2D(){
11 
12  std::cout << "2D Interpolation using evenly spaced data" << std::endl;
14  peaks.getDataFromFile("Rgridinterp/peak_data_cnst.tmp");
15 
16  std::vector<double> vv;
17  std::vector<std::vector<double> > xx;
18  for(double y = -29.42; y < 30.54; y = y+0.5047){
19  for(double x = -29.52; x < 30.64; x = x+0.4856){
20  xx.push_back(std::vector<double> {x,y});
21  vv.push_back(peaks.interpolate(x, y));
22  }
23  }
24  GRID_INTERP::writeCoordsValues("Rgridinterp/res_cnst_peak_l.tmp", xx, vv);
26  vv.clear();
27  xx.clear();
28  for(double y = -29.4; y < 30.3; y = y+0.5047){
29  for(double x = -29.5; x < 30.5; x = x+0.4856){
30  xx.push_back(std::vector<double> {x,y});
31  vv.push_back(peaks.interpolate(x, y));
32  }
33  }
34  GRID_INTERP::writeCoordsValues("Rgridinterp/res_cnst_peak_n.tmp", xx, vv);
35 
36  std::cout << "2D Interpolation using variably spaced data" << std::endl;
37 
38  double fromX = -21.0;
39  double toX = 21.0;
40  double dx = 0.2;
41 
42  double fromY = -10.0;
43  double toY = 9.5;
44  double dy = 0.2;
45 
46  // Read the data for the POINT mode and LINEAR method
48  testD.getDataFromFile("Rgridinterp/test2D_data_var.tmp");
49 
50  vv.clear();
51  xx.clear();
52  for(double y = fromY; y < toY; y = y+dy){
53  for(double x = fromX; x < toX; x = x+dx){
54  xx.push_back(std::vector<double> {x,y});
55  vv.push_back(testD.interpolate(x, y));
56  }
57  }
58  GRID_INTERP::writeCoordsValues("Rgridinterp/res_var_test2D_l.tmp", xx, vv);
59 
61  vv.clear();
62  xx.clear();
63  for(double y = fromY; y < toY; y = y+dy){
64  for(double x = fromX; x < toX; x = x+dx){
65  xx.push_back(std::vector<double> {x,y});
66  vv.push_back(testD.interpolate(x, y));
67  }
68  }
69  GRID_INTERP::writeCoordsValues("Rgridinterp/res_var_test2D_n.tmp", xx, vv);
70 }
71 
72 void test1D(){
73  std::cout << "1D Interpolation using evenly spaced data" << std::endl;
74  // Read the data for LINEAR method
76  oneD.getDataFromFile("Rgridinterp/test1_cnst_data.tmp");
77 
78  std::vector<double> vv;
79  std::vector<std::vector<double> > xx;
80  for(double x = 39.0; x < 66.5; x = x+0.1){
81  xx.push_back(std::vector<double> {x});
82  vv.push_back(oneD.interpolate(x));
83  }
84  GRID_INTERP::writeCoordsValues("Rgridinterp/res_cnst_test1_l.tmp", xx, vv);
85 
86  // Set the method to NEAREST and repeat the interpolation
87 
89  vv.clear();
90  xx.clear();
91  for(double x = 39.0; x < 66.5; x = x+0.1){
92  xx.push_back(std::vector<double> {x});
93  vv.push_back(oneD.interpolate(x));
94  }
95  GRID_INTERP::writeCoordsValues("Rgridinterp/res_cnst_test1_n.tmp", xx, vv);
96 
97  std::cout << "1D Interpolation using variably spaced data" << std::endl;
98  oneD.reset();
99  oneD.getDataFromFile("Rgridinterp/test1_var_data.tmp");
100  vv.clear();
101  xx.clear();
102  for(double x = 38.0; x < 82.2; x = x+0.1){
103  xx.push_back(std::vector<double> {x});
104  vv.push_back(oneD.interpolate(x));
105  }
106  GRID_INTERP::writeCoordsValues("Rgridinterp/res_var_test1_l.tmp", xx, vv);
107 
109  vv.clear();
110  xx.clear();
111  for(double x = 38.0; x < 82.2; x = x+0.1){
112  xx.push_back(std::vector<double> {x});
113  vv.push_back(oneD.interpolate(x));
114  }
115  GRID_INTERP::writeCoordsValues("Rgridinterp/res_var_test1_n.tmp", xx, vv);
116 }
117 
void getDataFromFile(std::string filename)
Get the Data From File object.
Definition: gridInterp.h:493
void reset()
Resets the class to the empty state.
Definition: gridInterp.h:455
void writeCoordsValues(std::string filename, std::vector< std::vector< double >> &coords, std::vector< double > &v)
This is a utility function which is used to print the results of the interpolation to a file so that ...
Definition: gridInterp.h:738
void test1D()
Definition: tests.h:72
void setMethod(METHOD method_in)
sets the method
Definition: gridInterp.h:487
void test2D()
Definition: tests.h:10
double interpolate(double x, double y=0, double z=0) const
This is the method to call to do the interpolation.
Definition: gridInterp.h:596
Main interpolation class.
Definition: gridInterp.h:326