[3851] | 1 | /*
|
---|
| 2 | --- SOPHYA software - NTools module ---
|
---|
| 3 | --- Class SLinInterp1D : Simple linear 1D interpolation class
|
---|
| 4 | R. Ansari , C. Magneville 2000-2010
|
---|
| 5 | (C) UPS+LAL IN2P3/CNRS (C) IRFU-SPP/CEA
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include "slininterp.h"
|
---|
| 9 | #include <fstream>
|
---|
| 10 |
|
---|
| 11 | namespace SOPHYA {
|
---|
| 12 |
|
---|
| 13 | /*!
|
---|
| 14 | \class SLinInterp1D
|
---|
| 15 | \ingroup NTools
|
---|
| 16 | \brief Simple linear interpolation class
|
---|
| 17 |
|
---|
| 18 | \code
|
---|
| 19 | #include "slininterp.h"
|
---|
| 20 | #include "srandgen.h"
|
---|
| 21 |
|
---|
| 22 | ...
|
---|
| 23 | vector<double> ys;
|
---|
| 24 | double xmin = 0.5;
|
---|
| 25 | double xmax = 0.;
|
---|
| 26 | for(int i=0; i<=12; i++) {
|
---|
| 27 | xmax = xmin+i*0.1;
|
---|
| 28 | yreg.push_back(sin(xmax)*cos(2.2*xmax));
|
---|
| 29 | }
|
---|
| 30 | SLinInterp1D interpYR(xmin, xmax, yreg);
|
---|
| 31 | cout << interpYR
|
---|
| 32 | for(int i=0; i<=12; i++) {
|
---|
| 33 | double x = drand01()*2.;
|
---|
| 34 | cout << " Interpol result for X=" << x << " -> " << interpYR(x) << " ?= " << sin(x)*cos(2.2*x) << endl;
|
---|
| 35 | }
|
---|
| 36 | \endcode
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | /* --Methode-- */
|
---|
| 40 | SLinInterp1D::SLinInterp1D()
|
---|
| 41 | : xmin_(0.), xmax_(1.), dx_(1.), ksmx_(1), npoints_(0)
|
---|
| 42 | {
|
---|
| 43 | xs_.push_back(0.);
|
---|
| 44 | xs_.push_back(1.);
|
---|
| 45 | ys_.push_back(0.);
|
---|
| 46 | ys_.push_back(1.);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[3869] | 49 | /*!
|
---|
[3851] | 50 | \brief Constructor from regularly spaced points in X with Y values defined by yreg
|
---|
| 51 |
|
---|
| 52 | \b xmin and \b xmax are the two extreme points in X corresponding to yreg.
|
---|
| 53 | Example: xmin=1, xmax=10, with yreg.size()=10 ,
|
---|
| 54 | yreg[0]=Y(1) , yreg[1]=Y(2) ... yreg[9]=Y(10)
|
---|
| 55 | */
|
---|
| 56 | SLinInterp1D::SLinInterp1D(double xmin, double xmax, vector<double>& yreg)
|
---|
| 57 | : xmin_(0.), xmax_(1.), dx_(1.), ksmx_(1), npoints_(0)
|
---|
| 58 | {
|
---|
| 59 | DefinePoints(xmin, xmax, yreg);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | /*!
|
---|
| 64 | \brief Constructor from a set of \b (xs[i],ys[i]) pairs.
|
---|
| 65 |
|
---|
| 66 | if (npt > 0), interpolates to a finer regularly spaced grid, from \b xmin to \b xmax
|
---|
| 67 | with npt points. use \b (xs[0],xs[xs.size()-1]) as limits if \b xmax<xmin
|
---|
| 68 | */
|
---|
| 69 | SLinInterp1D::SLinInterp1D(vector<double>& xs, vector<double>& ys, double xmin, double xmax, size_t npt)
|
---|
| 70 | : xmin_(0.), xmax_(1.), dx_(1.), ksmx_(1), npoints_(0)
|
---|
| 71 | {
|
---|
| 72 | DefinePoints(xs, ys, xmin, xmax, npt);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /* --Methode-- */
|
---|
| 76 | double SLinInterp1D::YInterp(double x) const
|
---|
| 77 | {
|
---|
| 78 | if (npoints_>0) { // on utilise les points regulierement espace
|
---|
| 79 | long i = (long)((x-xmin_)/dx_);
|
---|
| 80 | if (i<0) return ( yreg_[0]+(x-xmin_)*(yreg_[1]-yreg_[0])/dx_ );
|
---|
| 81 | if (i>=npoints_) return ( yreg_[npoints_]+(x-xmax_)*(yreg_[npoints_]-yreg_[npoints_-1])/dx_ );
|
---|
| 82 | return (yreg_[i]+(x-X(i))/dx_*(yreg_[i+1]-yreg_[i]));
|
---|
| 83 | }
|
---|
| 84 | else { // On utilise les points xs_,ys_ directement
|
---|
| 85 | if (x<=xs_[0]) return ( ys_[0]+(x-xs_[0])*(ys_[1]-ys_[0])/(xs_[1]-xs_[0]) );
|
---|
| 86 | if (x>=xs_[ksmx_]) return ( ys_[ksmx_]+(x-xs_[ksmx_])*(ys_[ksmx_]-ys_[ksmx_-1])/(xs_[ksmx_]-xs_[ksmx_-1]) );
|
---|
| 87 |
|
---|
| 88 | size_t k=1;
|
---|
| 89 | while(x>xs_[k]) k++;
|
---|
| 90 | if (k>=xs_.size()) { // ne devrait pas arriver ...
|
---|
| 91 | string emsg = " SLinInterp1D::YInterp() out of range k -> BUG in code ";
|
---|
| 92 | throw out_of_range(emsg);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | double rv=ys_[k-1]+(x-xs_[k-1])*(ys_[k]-ys_[k-1])/(xs_[k]-xs_[k-1]);
|
---|
| 96 | // cout << " DBG- x=" << x << " k=" << k << " xs[k]=" << xs_[k] << " ys[k]" << ys_[k]
|
---|
| 97 | // << " rv=" << rv << endl;
|
---|
| 98 | return rv;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[3869] | 102 | /*!
|
---|
[3851] | 103 | \brief Defines the interpolation points from regularly spaced points in X with Y values defined by yreg
|
---|
| 104 |
|
---|
| 105 | \b xmin and \b xmax are the two extreme points in X corresponding to yreg.
|
---|
| 106 | Example: xmin=1, xmax=10, with yreg.size()=10 ,
|
---|
| 107 | yreg[0]=Y(1) , yreg[1]=Y(2) ... yreg[9]=Y(10)
|
---|
| 108 | */
|
---|
| 109 | void SLinInterp1D::DefinePoints(double xmin, double xmax, vector<double>& yreg)
|
---|
| 110 | {
|
---|
| 111 | if (yreg.size()<2) {
|
---|
| 112 | string emsg = "SLinInterp1D::DefinePoints(xmin,xmax,yreg) Bad parameters yreg.size()<2 ";
|
---|
| 113 | throw range_error(emsg);
|
---|
| 114 | }
|
---|
| 115 | xmin_ = xmin;
|
---|
| 116 | xmax_ = xmax;
|
---|
| 117 | npoints_ = yreg.size()-1;
|
---|
| 118 | dx_ = (xmax_-xmin_)/(double)npoints_;
|
---|
| 119 | yreg_ = yreg;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | /*!
|
---|
| 124 | \brief Define the interpolation points from a set of \b (xs[i],ys[i]) pairs.
|
---|
| 125 |
|
---|
| 126 | if (npt > 0), interpolates to a finer regularly spaced grid, from \b xmin to \b xmax
|
---|
| 127 | with npt points. use \b (xs[0],xs[xs.size()-1]) as limits if \b xmax<xmin
|
---|
| 128 | */
|
---|
| 129 | void SLinInterp1D::DefinePoints(vector<double>& xs, vector<double>& ys, double xmin, double xmax, size_t npt)
|
---|
| 130 | {
|
---|
| 131 | if ((xs.size() != ys.size())||(xs.size()<2)) {
|
---|
| 132 | string emsg = "SLinInterp1D::DefinePoints() Bad parameters (xs.size() != ys.size())||(xs.size()<2) ";
|
---|
| 133 | throw range_error(emsg);
|
---|
| 134 | }
|
---|
| 135 | for(size_t k=1; k<xs.size(); k++) {
|
---|
| 136 | if (xs[k-1]>=xs[k]) {
|
---|
| 137 | string emsg = "SLinInterp1D::DefinePoints() unsorted xs ";
|
---|
| 138 | throw range_error(emsg);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | xs_=xs;
|
---|
| 142 | ys_=ys;
|
---|
| 143 | ksmx_=xs_.size()-1;
|
---|
| 144 | npoints_ = npt;
|
---|
| 145 | if (xmin>=xmax) {
|
---|
| 146 | xmin_ = xs_[0];
|
---|
| 147 | xmax_ = xs_[ksmx_];
|
---|
| 148 | }
|
---|
| 149 | else {
|
---|
| 150 | xmin_ = xmin;
|
---|
| 151 | xmax_ = xmax;
|
---|
| 152 | }
|
---|
| 153 | if (npoints_<1) {
|
---|
| 154 | dx_=(xmax_-xmin_)/(double)(xs_.size()-1);
|
---|
| 155 | return;
|
---|
| 156 | }
|
---|
| 157 | dx_ = (xmax_-xmin_)/(double)npoints_;
|
---|
| 158 | yreg_.resize(npoints_+1);
|
---|
| 159 |
|
---|
| 160 | // Compute the the y values for regularly spaced x xmin <= x <= xmax
|
---|
| 161 | // and keep values in the yreg vector
|
---|
| 162 | yreg_[0] = ys_[0];
|
---|
| 163 | yreg_[npoints_] = ys_[ksmx_];
|
---|
| 164 | size_t k=1;
|
---|
| 165 | for(size_t i=0; i<npoints_; i++) {
|
---|
| 166 | double x = X(i);
|
---|
| 167 | while(x>xs_[k]) k++;
|
---|
| 168 | if (k>=xs_.size()) k=xs_.size()-1;
|
---|
| 169 | yreg_[i] = ys_[k-1]+(x-xs_[k-1])*(ys_[k]-ys_[k-1])/(xs_[k]-xs_[k-1]);
|
---|
| 170 | //DBG cout << " DBG* i=" << i << " X(i)=" << X(i) << " yreg_[i]= " << yreg_[i] << " X^2= " << X(i)*X(i)
|
---|
| 171 | //DBG << " k=" << k << " xs[k]=" << xs_[k] << endl;
|
---|
| 172 | }
|
---|
| 173 | return;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | /* --Methode-- */
|
---|
| 178 | size_t SLinInterp1D::ReadYFromFile(string const& filename, double xmin, double xmax)
|
---|
| 179 | {
|
---|
| 180 | ifstream inputFile;
|
---|
| 181 | inputFile.open(filename.c_str(), ifstream::in);
|
---|
[3868] | 182 | #ifndef __DECCXX
|
---|
| 183 | // ifstream.is_open() ne passe pas avec OSF-cxx
|
---|
[3851] | 184 | if(! inputFile.is_open()) {
|
---|
| 185 | string emsg = " SLinInterp1D::ReadYFromFile() problem opening file ";
|
---|
| 186 | emsg += filename;
|
---|
| 187 | throw runtime_error(emsg);
|
---|
| 188 | }
|
---|
[3868] | 189 | #endif
|
---|
| 190 |
|
---|
[3851] | 191 | vector<double> xsv, ysv;
|
---|
| 192 | size_t cnt=0;
|
---|
| 193 | double cola;
|
---|
| 194 | while(!inputFile.eof()) {
|
---|
| 195 | inputFile.clear();
|
---|
| 196 | inputFile >> cola;
|
---|
| 197 | if ( (!inputFile.good()) || inputFile.eof()) break;
|
---|
| 198 | //cout << cola<< " "<<colb<<endl;
|
---|
| 199 | ysv.push_back(cola);
|
---|
| 200 | cnt++;
|
---|
| 201 | }
|
---|
| 202 | inputFile.close();
|
---|
| 203 | cout << " SLinInterp1D::ReadYFromFile()/Info: " << cnt << " Y-values read from file " << filename << endl;
|
---|
| 204 | DefinePoints(xmin, xmax, ysv);
|
---|
| 205 | return cnt;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* --Methode-- */
|
---|
| 209 | size_t SLinInterp1D::ReadXYFromFile(string const& filename, double xmin, double xmax, size_t npt)
|
---|
| 210 | {
|
---|
| 211 | ifstream inputFile;
|
---|
[3868] | 212 | inputFile.open(filename.c_str(), ifstream::in);
|
---|
| 213 | #ifndef __DECCXX
|
---|
| 214 | // ifstream.is_open() ne passe pas avec OSF-cxx
|
---|
[3851] | 215 | if(! inputFile.is_open()) {
|
---|
| 216 | string emsg = " SLinInterp1D::ReadXYFromFile() problem opening file ";
|
---|
| 217 | emsg += filename;
|
---|
| 218 | throw runtime_error(emsg);
|
---|
| 219 | }
|
---|
[3868] | 220 | #endif
|
---|
| 221 |
|
---|
[3851] | 222 | vector<double> xsv, ysv;
|
---|
| 223 | size_t cnt=0;
|
---|
| 224 | double cola, colb;
|
---|
| 225 | while(!inputFile.eof()) {
|
---|
| 226 | inputFile.clear();
|
---|
| 227 | inputFile >> cola >> colb;
|
---|
| 228 | if ( (!inputFile.good()) || inputFile.eof()) break;
|
---|
| 229 | // cout << " DEBUG - cnt=" << cnt << " x=" << cola << " y= "<<colb<<endl;
|
---|
| 230 | xsv.push_back(cola);
|
---|
| 231 | ysv.push_back(colb);
|
---|
| 232 | cnt++;
|
---|
| 233 | }
|
---|
| 234 | inputFile.close();
|
---|
| 235 | cout << " SLinInterp1D::ReadXYFromFile()/Info: " << cnt << " (x,y) pairs read from file " << filename << endl;
|
---|
| 236 | DefinePoints(xsv, ysv, xmin, xmax, npt);
|
---|
| 237 | return cnt;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | /* --Methode-- */
|
---|
| 242 | ostream& SLinInterp1D::Print(ostream& os, int lev) const
|
---|
| 243 | {
|
---|
| 244 | os << " ---- SLinInterp1D::Print() XMin=" << XMin() << " XMax=" << XMax() << " NPoints=" << npoints_ << endl;
|
---|
| 245 | os << " xs_.size()= " << xs_.size() << " ys_.size()= " << ys_.size() << " yreg_.size()= " << yreg_.size() << endl;
|
---|
| 246 | if ((lev>0)&&(xs_.size()>0)) {
|
---|
| 247 | for(size_t i=0; i<xs_.size(); i++)
|
---|
| 248 | os << " xs[" << i << " ]=" << xs_[i] << " -> ys[" << i << "]=" << ys_[i] << endl;
|
---|
| 249 | }
|
---|
| 250 | if ((lev>0)&&(yreg_.size()>0)) {
|
---|
| 251 | for(size_t i=0; i<yreg_.size(); i++)
|
---|
| 252 | os << " Regularly Spaced X(" << i << " )=" << X(i) << " -> yreg_[" << i << "]=" << yreg_[i] << endl;
|
---|
| 253 | }
|
---|
| 254 | os << " -----------------------------------------------------------------------" << endl;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | } // End namespace SOPHYA
|
---|
| 258 |
|
---|