Canoe
Comprehensive Atmosphere N' Ocean Engine
find_place_in_table.c
Go to the documentation of this file.
1 #include <math.h>
2 
3 #include "core.h"
4 
5 /*
6  * Find place in table using bisection.
7  * Adapted from EPIC model 430
8  *
9  * NOTE: Finds place relative to the x component of the float_triplet table.
10  */
11 int find_place_in_table(int n, struct float_triplet *table, double x,
12  double *dx, int il) {
13  int im, iu, ascend, cmplo, cmphi;
14 
15  iu = n;
16  ascend = (table[n - 1].x >= table[0].x);
17  cmplo = fcmp(x, table[0].x);
18  cmphi = fcmp(x, table[n - 1].x);
19 
20  /*
21  * Check x against table endpoints.
22  */
23  if (ascend) {
24  if (cmplo <= 0) {
25  il = 0;
26  *dx = table[il + 1].x - table[il].x;
27  return il;
28  }
29  if (cmphi >= 0) {
30  il = n - 2;
31  *dx = table[il + 1].x - table[il].x;
32  return il;
33  }
34  } else {
35  if (cmplo >= 0) {
36  il = 0;
37  *dx = table[il + 1].x - table[il].x;
38  return il;
39  }
40  if (cmphi <= 0) {
41  il = n - 2;
42  *dx = table[il + 1].x - table[il].x;
43  return il;
44  }
45  }
46 
47  /*
48  * Use bisection to search table.
49  */
50  while (iu - il > 1) {
51  im = (iu + il) >> 1;
52  if (fcmp(x, table[im].x) >= 0 == ascend) {
53  il = im;
54  } else {
55  iu = im;
56  }
57  }
58 
59  *dx = table[il + 1].x - table[il].x;
60  return il;
61 }
int fcmp(double x1, double x2)
Definition: fcmp.c:34
int find_place_in_table(int n, struct float_triplet *table, double x, double *dx, int il)
double x
Definition: core.h:11