PLUTO  4.4-patch2
Functions
math_table2D.c File Reference

Miscellaneous functions for handling 2D tables. More...

#include "pluto.h"

Functions

void InitializeTable2D (Table2D *tab, double xmin, double xmax, int nx, double ymin, double ymax, int ny)
 
int LocateIndex (double *yarr, int beg, int end, double y)
 
int InverseLookupTable2D (Table2D *tab, double y, double f, double *x)
 
int Table2DInterpolate (Table2D *tab, double x, double y, double *f)
 
void WriteBinaryTable2D (char *fname, Table2D *tab)
 

Detailed Description

Author
A. Mignone (migno.nosp@m.ne@p.nosp@m.h.uni.nosp@m.to.i.nosp@m.t)
Date
March 16, 2015

Function Documentation

◆ InitializeTable2D()

void InitializeTable2D ( Table2D *  tab,
double  xmin,
double  xmax,
int  nx,
double  ymin,
double  ymax,
int  ny 
)

Allocate memory for the arrays contained in the *tab structure and generate a uniformly spaced grid in log10(x), log10(y) within the range provided by xmin, xmax, ymin, ymax. On output, the function initializes the following structure members:

  • nx, ny
  • lnxmin, lnxmax, lnymin, lnymax
  • lnx[], lny[]
  • dlnx, dlny
  • dlnx_1, dlny_1
  • x[], y[]
  • dx[], dy[]
Parameters
[in,out]tabpointer to a Table2D structure
[in]xminlower column limit.
[in]xmaxupper column limit.
[in]nxnumber of equally-spaced column bins (in log space)
[in]yminlower row limit.
[in]ymaxupper row limit.
[in]nynumber of equally-spaced row bins (in log space)

◆ InverseLookupTable2D()

int InverseLookupTable2D ( Table2D *  tab,
double  y,
double  f,
double *  x 
)

Perform inverse lookup table interpolation: given a 2D table f(i,j)=f(x(i), y(j)) and the value of y, find the value of x. The algorithm proceeds by a combination of 1D lookup table algorithms:

  • Find the value of j such that y[j]<y<y[j+1];
  • The solution must then lie on the intermediate line between j and j+1 where the value of y is given.
  • To find the horizontal coordinate, we first locate initial and final indices ib and ie at j and j+1 using 1D lookup table.
  • Then construct a 1D array between these two indices values, perform again 1D lookup table to find the actual index i and invert the bilinear interpolant by solving

    \[ f = f_{i,j}(1 - x_n)(1 - y_n) + f_{i+1,j}x_n(1 - y_n) + f_{i,j+1}(1 - x_n)y_n + f_{i+1,j+1}x_ny_n; \]

    for $ x_n$ (normlized coordinate between 0 and 1).
Note
The table must be monotonic in both and y.
Parameters
[in]taba pointer to a Table2D structure
[in]ythe ordinata
[in]fthe value of the function
[out]*xthe value of the abscissa.
Returns
Returns 0 on success, otherwise:
  • -2 if y is below range;
  • 2 if y is above range.

◆ LocateIndex()

int LocateIndex ( double *  yarr,
int  beg,
int  end,
double  y 
)

Given an array yarr[beg..end] and a given value y, returns the array index i such that yarr[i] < y < yarr[i+1] (for increasing arrays) or yarr[i+1] < y < yarr[i] (for decreasing arrays). yarr must be monotonically increasing or monotonically decreasing.

Parameters
[in]yarrthe 1D array
[in]beginitial index of the array
[in]endfinal index of the array
[in]ythe value to be searched for
Returns
On success return the index of the array. Otherwise returns -1.

Reference

  • "Numerical Recipes in C", Sect. 3.4 "How to Search an Ordered Table"

◆ Table2DInterpolate()

int Table2DInterpolate ( Table2D *  tab,
double  x,
double  y,
double *  f 
)

Use bilinear interpolation to find the function f(x,y) from the 2D table *tab. Since the grid is equally spaced in log(x) and log(y), the (i,j) indices such that x[i] <= x < x[i+1] and y[j] <= y < y[j+1] are found by a simple division. Then bilinear interpolation can be done in either log or linear coordinates. The latter is expected to be slightly faster since it avoids one pow() operation.

Parameters
[in]*taba pointer to a Table2D structure
[in]xthe abscissa where interpolation is needed.
[in]ythe ordinata where interpolation is needed.
[out]*fthe interpolated value
Returns
- Return 0 on success, otherwise:
  • -1 if x is below column range;
  • 1 if x is above column range;
  • -2 if y is below row range;
  • 2 if y is above row range.

◆ WriteBinaryTable2D()

void WriteBinaryTable2D ( char *  fname,
Table2D *  tab 
)

The binary table is a compact format used to write a 2D array together with simple structured coordinates. The file consists of the following information:

 nx
 ny
 <x[0]..x[nx-1]>
 <y[0]..y[ny-1]>
 <q[0][0]..q[0][nx-1]
  q[1][0]..q[1][nx-1]
    .......
  q[ny-1][0]..q[ny-1][nx-1]>

All fields are written in binary format using double precision arithmetic.