Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
interpolate.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file
11 /// @brief
12 /// @author
13 
14 #ifndef INCLUDED_basic_interpolate_hh
15 #define INCLUDED_basic_interpolate_hh
16 
17 // Package headers
18 
19 // ObjexxFCL Headers
20 #include <ObjexxFCL/FArray2.hh> // for FArray2
21 #include <ObjexxFCL/FArray2A.hh> // for FArray2A, FArray2A::IR, FArray2A<>:...
22 #include <ObjexxFCL/FArray3A.hh> // for FArray3A, FArray3A::IR, FArray3A<>:...
23 #include <ObjexxFCL/Fmath.hh> // for mod
24 #include "ObjexxFCL/FArray3.hh" // for FArray3
25 
26 // util_interpolate Function Declarations
27 namespace basic {
28 
29 void
31  double const x,
32  double const binrange,
33  int & xbin,
34  int & xbin_next,
35  double & xd
36 );
37 
38 
39 void
41  int const xbin,
42  int const xbin_next,
43  double const xd,
44  int const ybin,
45  int const ybin_next,
46  double const yd,
48  int const xbin_count,
49  int const ybin_count,
50  double const binrange,
51  bool const angles,
52  double & val,
53  double & dval_dx,
54  double & dval_dy
55 );
56 
57 
58 void
60  double const x0y0,
61  double const x1y0,
62  double const x0y1,
63  double const x1y1,
64  double const xd,
65  double const yd,
66  double const binrange,
67  bool const angles,
68  double & val,
69  double & dval_dx,
70  double & dval_dy
71 );
72 
73 void
75  int const xbin,
76  int const xbin_next,
77  double const xd,
78  int const ybin,
79  int const ybin_next,
80  double const yd,
81  int const zbin,
82  int const zbin_next,
83  double const zd,
85  int const xbin_count,
86  int const ybin_count,
87  int const zbin_count,
88  double const binrange, // assumes that have the same bin size in each dimention
89  bool const angles,
90  double & val,
91  double & dval_dx,
92  double & dval_dy,
93  double & dval_dz
94 );
95 
96 void
98  double const x0y0z0,
99  double const x1y0z0,
100  double const x0y1z0,
101  double const x1y1z0,
102  double const x0y0z1,
103  double const x1y0z1,
104  double const x0y1z1,
105  double const x1y1z1,
106  double const xd,
107  double const yd,
108  double const zd,
109  double const binrange, // assumes that have the same bin size in each dimention
110  bool const angles,
111  double & val,
112  double & dval_dx,
113  double & dval_dy,
114  double & dval_dz
115 );
116 
117 void
119  double const x,
120  double const y,
122  double & val,
123  double & dval_dx,
124  double & dval_dy
125 );
126 
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 ///
130 /// @brief get bin information for a periodic value w/ periodic bins
131 ///
132 /// @details
133 ///
134 /// for 'x', an angle in degrees, and angular bins of width
135 /// 'binrange' aligned to start bin 1 at a value of 0. degrees, find
136 /// the two bins, whose average bin values 'x' falls between, and
137 /// report the error between the lower average bin value and the real
138 /// value of 'x'
139 ///
140 /// @param[in] x - in - angle in degrees
141 /// @param[in] binrange - in - degrees per bin
142 /// @param[out] xbin - out - anglular bin whose average value is the lower
143 /// of the two average bin values bracketing 'x'
144 /// @param[out] xbin_next - out - anglular bin whose average value is the lower
145 /// of the two average bin values bracketing 'x'
146 /// @param[out] xd - out - error between the average value of bin 'xbin' and 'x'
147 ///
148 /// @remarks
149 ///
150 /// @references
151 ///
152 /// @author
153 ///
154 /////////////////////////////////////////////////////////////////////////////////
155 inline
156 void
158  double const x,
159  double const binrange,
160  int & xbin,
161  int & xbin_next,
162  double & xd
163 )
164 {
165  //------------------------------------------------------------------------------
166  // ctsa - get nbins for binrange
167  //
168  int const nbins = static_cast< int >( 360.0 / binrange );
169 
170  // ctsa - convert angle to double(anglebin) and
171  // mod value to range: [1.,double(nbins)+1.)
172  //
173  // note that: double(anglebin) = 1. + (angle-.5*binrange)/binrange
174  // ...this solves for the lowest of the two bracketing bin averages,
175  // rather than the nearest bin average, and is thus only
176  // appropriate for interpolation
177 
178 
179  double const xbin_real = 1.0 + ObjexxFCL::mod(
180  ObjexxFCL::mod( (x-(0.5*binrange))/binrange, static_cast< double >(nbins) ) + nbins,
181  static_cast< double >(nbins) );
182 
183  // ctsa - convert double bin values to array lookup bin values
184  //
185  xbin = static_cast< int >( xbin_real );
186 
187  // ctsa - get next lookup bin and convert to range: (1,nbins)
188  //
189  xbin_next = 1 + ObjexxFCL::mod( xbin, nbins );
190 
191  // ctsa - get error
192  //
193  xd = xbin_real - xbin;
194 
195 
196  //// ctsa -- 5-2003
197  //// ...another double resolution bug of type:
198  ////
199  //// static_cast< int >(double(intval)-epsilon) == intval, epsilon << 1
200  ////
201  //// this causes xbin to sporatically resolve to bins+1, a
202  //// very bad thing; check below should fix...
203  ////
204  if ( xbin == nbins + 1 ) {
205  xbin = 1;
206  xd = 0.0;
207  }
208 }
209 
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 ///
213 /// @brief get bilinear interpolate of a 2d periodic function
214 ///
215 /// @details
216 ///
217 /// Value and derivatives of bilinear interpolation on a 2d binned
218 /// periodic function, represented as a 2d array. (see Num. Recipes
219 /// v2, sec 3.6, "Interpolation in two or more dimensions")
220 ///
221 /// Note that it is automatically assumed that the arguments are
222 /// periodic, that is f(a,b),a,b=periodic value; the *value* of the
223 /// function can be treated as periodic ( fixed to a periodicity of
224 /// 360.) as well when the input parameter angle is set to true
225 ///
226 /// The derivatives are of the bilinear interpolation only. This is
227 /// not the same value as a numerical derivative of function
228 /// values. The derivatives here are discontinuous every time you
229 /// cross over a bin boundary in either dimension
230 ///
231 /// @param[in] xbin - in - low bin in 1st dim
232 /// @param[in] xbin_next - in - high bin in 1st dim
233 /// @param[in] xd - in - error between low and real val in 1st dim
234 /// @param[in] ybin - in - " 2nd dim "
235 /// @param[in] ybin_next - in - " 2nd dim "
236 /// @param[in] yd - in - " 2nd dim "
237 /// @param[in] xy_func - in - 2d-array specifying the 2d binned function values,
238 /// assumed to be periodic in each dimension
239 /// @param[in] xbin_count - in - number of bins in the 1st dim of the periodic
240 /// function
241 /// @param[in] ybin_count - in - " 2nd dim "
242 /// @param[in] binrange - in - range of bin in angles ( for both dimensions )
243 /// @param[in] angles - in - if true, treat the the *values* of xy_func as
244 /// as having a periodicity of 360. ( note that
245 /// the bin ranges are already assumed periodic )
246 /// @param[out] val - out - bilinear interpolated value of xy_func
247 /// @param[out] dval_dx - out - derivative of interpolation w.r.t 1st dim
248 /// @param[out] dval_dy - out - " 2nd dim "
249 ///
250 /// @remarks
251 ///
252 /// @references
253 ///
254 /// @author ctsa 8-19-03
255 ///
256 /////////////////////////////////////////////////////////////////////////////////
257 inline
258 void
260  int const xbin,
261  int const xbin_next,
262  double const xd,
263  int const ybin,
264  int const ybin_next,
265  double const yd,
267  int const xbin_count,
268  int const ybin_count,
269  double const binrange,
270  bool const angles,
271  double & val,
272  double & dval_dx,
273  double & dval_dy
274 )
275 {
276  xy_func.dimension( xbin_count, ybin_count );
277 
278  //------------------------------------------------------------------------------
279  double const x0y0 = xy_func(xbin,ybin);
280  double const x1y0 = xy_func(xbin_next,ybin);
281  double const x0y1 = xy_func(xbin,ybin_next);
282  double const x1y1 = xy_func(xbin_next,ybin_next);
283 
284  interpolate_bilinear_by_value(x0y0,x1y0,x0y1,x1y1,xd,yd,binrange,angles,val,
285  dval_dx,dval_dy);
286 }
287 
288 inline
289 void
291  int const xbin,
292  int const xbin_next,
293  double const xd,
294  int const ybin,
295  int const ybin_next,
296  double const yd,
297  int const zbin,
298  int const zbin_next,
299  double const zd,
301  int const xbin_count,
302  int const ybin_count,
303  int const zbin_count,
304  double const binrange, // assumes that have the same bin size in each dimention
305  bool const angles,
306  double & val,
307  double & dval_dx,
308  double & dval_dy,
309  double & dval_dz
310 )
311 {
312  xyz_func.dimension( xbin_count, ybin_count, zbin_count );
313 
314  double const x0y0z0 = xyz_func( xbin, ybin, zbin );
315  double const x1y0z0 = xyz_func( xbin_next, ybin, zbin );
316  double const x0y1z0 = xyz_func( xbin, ybin_next, zbin );
317  double const x1y1z0 = xyz_func( xbin_next,ybin_next, zbin );
318  double const x0y0z1 = xyz_func( xbin, ybin, zbin_next );
319  double const x1y0z1 = xyz_func( xbin_next, ybin, zbin_next );
320  double const x0y1z1 = xyz_func( xbin, ybin_next, zbin_next );
321  double const x1y1z1 = xyz_func( xbin_next, ybin_next, zbin_next );
322 
324  x0y0z0, x1y0z0, x0y1z0, x1y1z0,
325  x0y0z1, x1y0z1, x0y1z1, x1y1z1,
326  xd, yd, zd, binrange, angles,
327  val, dval_dx, dval_dy, dval_dz );
328 }
329 
330 ////////////////////////////////////////////////////////////////////////////////
331 ///
332 /// @brief get bilinear interpolate of a 2d function with degree angle arguments
333 ///
334 /// @details
335 ///
336 /// Value and derivatives of bilinear interpolation on a 2d function
337 /// with degree angle arguments represented by a 2d array with binned
338 /// degree angle indices. (see Num. Recipes v2, sec 3.6,
339 /// "Interpolation in two or more dimensions")
340 ///
341 /// The derivatives are of the bilinear interpolation only. This is
342 /// not the same value as a numerical derivative of function
343 /// values. The derivatives here are discontinuous every time you
344 /// cross over a bin boundary in either dimension
345 ///
346 /// @param[in] x - in - function argument value in 1st dim to find interpolate for
347 /// @param[in] y - in - " 2nd dim "
348 /// @param[in] xy_func - in - 2d array with binned angle value indicies;
349 /// represents known values of 2d function being interpolated.
350 /// @param[out] val - out - bilinear interpolated value
351 /// @param[out] dval_dx - out - derivative of interpolated value w.r.t. 1st dim angle
352 /// @param[out] dval_dy - out - " 2nd dim "
353 ///
354 /// @remarks
355 ///
356 /// !!! assumes 10 degree bins in both dimensions of xy_func
357 ///
358 /// @references
359 ///
360 /// @author ctsa 8-19-03
361 ///
362 /////////////////////////////////////////////////////////////////////////////////
363 inline
364 void
366  double const x,
367  double const y,
369  double & val,
370  double & dval_dx,
371  double & dval_dy
372 )
373 {
374  int const nbins = { 36 };
375  xy_func.dimension( nbins, nbins );
376 
377  //ctsa fixed parameters
378  double const binrange = { 10.0f };
379 
380  //ctsa local
381  int xbin,ybin,xbin_next,ybin_next;
382  double xd,yd;
383 
384  //------------------------------------------------------------------------------
385  interpolate_get_angle_bins(x,binrange,xbin,xbin_next,xd);
386  interpolate_get_angle_bins(y,binrange,ybin,ybin_next,yd);
387 
388  bool const treat_as_angles = false;
389 
390  interpolate_bilinear(xbin,xbin_next,xd,ybin,ybin_next,yd,xy_func,nbins,nbins,
391  binrange,treat_as_angles,val,dval_dx,dval_dy);
392 }
393 
394 
395 } // basic
396 
397 #endif
FArray3A: Fortran-Compatible 3D Argument Array.
Definition: FArray3.hh:29
FArray2A: Fortran-Compatible 2D Argument Array.
Definition: FArray2.hh:29
def x
void interpolate_trilinear(int const xbin, int const xbin_next, double const xd, int const ybin, int const ybin_next, double const yd, int const zbin, int const zbin_next, double const zd, ObjexxFCL::FArray3A< double > xyz_func, int const xbin_count, int const ybin_count, int const zbin_count, double const binrange, bool const angles, double &val, double &dval_dx, double &dval_dy, double &dval_dz)
Definition: interpolate.hh:290
void interpolate_trilinear_by_value(double const x0y0z0, double const x1y0z0, double const x0y1z0, double const x1y1z0, double const x0y0z1, double const x1y0z1, double const x0y1z1, double const x1y1z1, double const xd, double const yd, double const zd, double const binrange, bool const angles, double &val, double &dval_dx, double &dval_dy, double &dval_dz)
paraphazed from above for three dimentions Without additional scaling this will only work if the bin ...
Definition: interpolate.cc:149
FArray3A & dimension(IR const &I1_a, IR const &I2_a, IR const &I3_a)
Dimension by IndexRange.
Definition: FArray3A.hh:791
FArray2A & dimension(IR const &I1_a, IR const &I2_a)
Dimension by IndexRange.
Definition: FArray2A.hh:737
void interpolate_bilinear_by_value(double const x0y0, double const x1y0, double const x0y1, double const x1y1, double const xd, double const yd, double const binrange, bool const angles, double &val, double &dval_dx, double &dval_dy)
get bilinear interpolate, given four points of a 2d periodic function
Definition: interpolate.cc:80
void interpolate_bilinear(int const xbin, int const xbin_next, double const xd, int const ybin, int const ybin_next, double const yd, ObjexxFCL::FArray2A< double > xy_func, int const xbin_count, int const ybin_count, double const binrange, bool const angles, double &val, double &dval_dx, double &dval_dy)
get bilinear interpolate of a 2d periodic function
Definition: interpolate.hh:259
void interpolate_get_angle_bins(double const x, double const binrange, int &xbin, int &xbin_next, double &xd)
get bin information for a periodic value w/ periodic bins
Definition: interpolate.hh:157
T mod(T const &x, T const &y)
x(mod y) computational modulo returning magnitude < | y | and sign of x
Definition: Fmath.hh:475
void interpolate_2d_func_of_angles(double const x, double const y, ObjexxFCL::FArray2A< double > xy_func, double &val, double &dval_dx, double &dval_dy)
get bilinear interpolate of a 2d function with degree angle arguments
Definition: interpolate.hh:365
def y