12 void PlotCubic(
double a,
double b,
double c,
double d);
16 double ymin,
double ymax,
int ny)
62 for (i = 0; i < tab->
nx; i++){
63 for (j = 0; j < tab->
ny; j++) tab->
f[j][i] = 0.0;
64 for (j = 0; j < tab->
ny; j++) tab->
defined[j][i] = 1;
104 for (i = 0; i < tab->
nx; i++) {
106 tab->
x[
i] = pow(10.0, tab->
lnx[i]);
109 for (j = 0; j < tab->
ny; j++) {
111 tab->
y[
j] = pow(10.0, tab->
lny[j]);
116 for (i = 0; i < tab->
nx-1; i++) tab->
dx[i] = tab->
x[i+1] - tab->
x[i];
117 for (j = 0; j < tab->
ny-1; j++) tab->
dy[j] = tab->
y[j+1] - tab->
y[j];
132 for (j = 0; j < tab->
ny; j++){
133 for (i = 0; i < tab->
nx-1; i++){
134 tab->
dfx[
j][
i] = tab->
f[
j][i+1] - tab->
f[
j][
i];
136 for (j = 0; j < tab->
ny-1; j++){
137 for (i = 0; i < tab->
nx; i++){
138 tab->
dfy[
j][
i] = tab->
f[j+1][
i] - tab->
f[
j][
i];
168 ascnd = (yarr[end] >= yarr[beg]);
174 if ( (ascnd && (y < yarr[beg] || y > yarr[end])) ||
175 (!ascnd && (y > yarr[beg] || y < yarr[end]))){
184 while ( (iu - il) > 1) {
186 if ((y >= yarr[im]) == ascnd) il = im;
189 if (y == yarr[beg])
return beg;
190 else if (y == yarr[end])
return end;
194 #define LOG_INTERPOLATION NO
230 int i,
j,
k, i0, i1, ib, ie;
232 double **ftab, **dfx, **dfy;
235 if (f1 == NULL) f1 =
ARRAY_1D(8192,
double);
242 if (lny < tab->lnymin){
243 print (
"! InverseLookupTable2D: lny outside range: %12.6e < %12.6e\n",
248 print (
"! InverseLookupTable2D: lny outside range: %12.6e > %12.6e\n",
254 #if LOG_INTERPOLATION == YES
257 yn = (y - tab->
y[j])/tab->
dy[
j];
263 if (i0 < 0 || i1 < 0)
return 2;
269 for (i = ib; i <= ie; i++) {
270 f1[
i] = tab->
f[
j][
i]*(1.0 - yn) + tab->
f[j+1][i]*yn;
283 xn = f - ftab[
j][
i] - yn*dfy[
j][
i];
284 xn /= yn*(dfx[j+1][
i] - dfx[
j][
i]) + dfx[j][i];
291 double fn, dfn, d2fn, dxn;
293 a = tab->
a[
j][
i]*(1.0 - yn) + tab->
a[j+1][i]*yn;
294 b = tab->
b[j][i]*(1.0 - yn) + tab->
b[j+1][
i]*yn;
295 c = tab->
c[
j][
i]*(1.0 - yn) + tab->
c[j+1][i]*yn;
296 d = tab->
d[j][i]*(1.0 - yn) + tab->
d[j+1][
i]*yn - f;
300 xn = f - ftab[
j][
i] - yn*dfy[
j][
i];
301 xn /= yn*(dfx[j+1][
i] - dfx[
j][
i]) + dfx[j][i];
305 for (k = 0; k <= kmax; k++){
306 fn = d + xn*(c + xn*(b + xn*
a));
307 dfn = c + xn*(2.0*b + 3.0*a*xn);
310 if (fabs(dxn) < 1.e-11)
break;
312 printf (
"! InverseLookupTable2D(): too many iterations in Newton-cubic\n");
332 if (xn < 0.0 || xn > 1.0) {
334 print (
"! InverseLookupTable2D(): no root in [0,1]; i,j = %d,%d\n",i,j);
335 print (
"! xn = %8.3e\n",xn);
336 print (
"! Cubic tabulated in 'cubic.dat'\n");
341 double Q, R, A, B, R_Q;
342 double sn, cn, tn, sQ, th, xf;
346 a = tab->
a[
j][
i]*(1.0 - yn) + tab->
a[j+1][i]*yn;
347 b = tab->
b[j][i]*(1.0 - yn) + tab->
b[j+1][
i]*yn;
348 c = tab->
c[
j][
i]*(1.0 - yn) + tab->
c[j+1][i]*yn;
349 d = tab->
d[j][i]*(1.0 - yn) + tab->
d[j+1][
i]*yn - f;
355 Q = (b*b - 3.0*
c)/9.0;
356 R = (2.0*b*b*b - 9.0*b*c + 27.0*d)/54.0;
362 cn = 1.0/sqrt(1.0 + tn*tn);
384 if (xf < 0.0) xn = sQ*(cn + sqrt(3.0)*sn) - b/3.0;
385 else xn = -2.0*sQ*cn - b/3.0;
388 xn = sQ*(cn - sqrt(3.0)*sn) - b/3.0;
393 A = fabs(R)*(1.0 + sqrt(1.0 - A*A*Q));
394 A = -
DSIGN(R)*pow(A, 1.0/3.0);
397 xn = (A + B) - b/3.0;
400 if (xn < 0.0 || xn > 1.0) {
402 print (
"! InverseLookupTable2D(): no root in [0,1]; i,j = %d,%d\n",i,j);
403 print (
"! xn = %8.3e\n",xn);
404 print (
"! R = %8.3e; Q = %8.3e\n",R,Q);
405 print (
"! R^2 - Q^3 = %8.3e\n",R*R-Q*Q*Q);
406 print (
"! |R/Q|/sqrt(Q) = %8.3e\n",fabs(R/Q)/sqrt(fabs(Q)));
407 print (
"! a = %8.3e; b = %8.3e; c = %8.3e; d = %8.3e\n",a,b,c,d);
408 print (
"! Cubic tabulated in 'cubic.dat'\n");
419 #if LOG_INTERPOLATION == YES
421 (*x) = pow(10.0, *x);
423 (*x) = xn*tab->
dx[
i] + tab->
x[
i];
456 double lnx, lny, xn, yn;
466 if (lnx < tab->lnxmin){
468 print (
"! Table2DInterpolate: lnx outside range %12.6e < %12.6e\n",
476 print (
"! Table2DInterpolate: lnx outside range: %12.6e > %12.6e\n",
482 if (lny < tab->lnymin){
484 print (
"! Table2DInterpolate: lny outside range: %12.6e < %12.6e\n",
492 print (
"! Table2DInterpolate: lny outside range: %12.6e > %12.6e\n",
505 #if LOG_INTERPOLATION == YES
507 yn = (lny - tab->
lny[j])/tab->
dlny;
509 xn = (x - tab->
x[
i])/tab->
dx[i];
510 yn = (y - tab->
y[j])/tab->
dy[
j];
514 (*f) = (tab->
f[
j][
i] + (tab->
f[
j][i+1] - tab->
f[
j][
i])*xn)*(1.0 - yn)
515 + (tab->
f[j+1][i] + (tab->
f[j+1][i+1] - tab->
f[j+1][i])*xn)*yn;
517 s1 = tab->
d[
j][
i] + xn*(tab->
c[
j][
i] + xn*(tab->
b[
j][
i] + xn*tab->
a[
j][
i]));
518 s2 = tab->
d[j+1][
i] + xn*(tab->
c[j+1][
i] + xn*(tab->
b[j+1][
i] + xn*tab->
a[j+1][
i]));
519 *f = s1*(1.0 - yn) + s2*yn;
521 print1 (
"! Table2DInterpolate(): table interpolation not corretly defined\n");
552 fp = fopen(fname,
"wb");
554 scrh = (double)tab->
nx;
555 fwrite(&scrh,
sizeof(
double), 1,
fp);
557 scrh = (double)tab->
ny;
558 fwrite(&scrh,
sizeof(
double), 1,
fp);
560 fwrite(tab->
lnx,
sizeof(
double), tab->
nx, fp);
561 fwrite(tab->
lny,
sizeof(
double), tab->
ny, fp);
563 for (j = 0; j < tab->
ny; j++){
564 fwrite (tab->
f[j],
sizeof(
double), tab->
nx, fp);
573 double t, f, dt = 1.e-1;
576 fp = fopen(
"cubic.dat",
"w");
577 for (t = -50.0; t <= 50.0; t += dt){
578 f = d + t*(c + t*(b + t*
a));
579 fprintf (fp,
"%12.6e %12.6e\n",t,f);
double * y
array of y-values (not uniform)
int LocateIndex(double *yarr, int beg, int end, double y)
void print1(const char *fmt,...)
int nx
Number of columns or points in the x direction.
double ** a
Spline coefficient (x^3)
double ** c
Spline coefficient (x)
double ** b
Spline coefficient (x^2)
void WriteBinaryTable2D(char *fname, Table2D *tab)
double * dy
grid spacing array in the y direction (not uniform)
void PlotCubic(double a, double b, double c, double d)
int InverseLookupTable2D(Table2D *tab, double y, double f, double *x)
double lnxmax
upper limit (in log10) in the x-direction
int ny
Number of rows or points in the y direction.
int Table2DInterpolate(Table2D *tab, double x, double y, double *f)
void print(const char *fmt,...)
double * lny
array of log10(y) values (uniform)
double dlnx
uniform spacing in log10(x)
int interpolation
LINEAR/SPLINE1.
#define ARRAY_1D(nx, type)
void InitializeTable2D(Table2D *tab, double xmin, double xmax, int nx, double ymin, double ymax, int ny)
double lnymax
upper limit (in log10) in the y-direction
double * lnx
array of log10(x) values (uniform)
double dlny
uniform spacing in log10(y)
double lnxmin
lower limit (in log10) in the x-direction
void FinalizeTable2D(Table2D *tab)
double lnymin
lower limit (in log10) in the y-direction
#define ARRAY_2D(nx, ny, type)
double ** d
Spline coefficiten (1)
double * dx
grid spacing array in the x direction (not uniform)
#define QUIT_PLUTO(e_code)
double * x
array of x-values (not uniform)