#include #include /********************************************************* This is code for read-in tables in XPP This should probably be accessible from within the program as well. It will probably be added to the Numerics Menu The files consist of y-values of a function evaluated at equally spaced points as well as some header information. They are ascii files of the form: npts <-- Integer xlo <--- fp xhi <-- fp y1 <-- fp y2 ... yn Thus dx = (xhi-xlo)/(npts-1) If the first line of the file says "xyvals" then the table is of the form: x1 < x2 < .... < xn npts x1 y1 x2 y2 ... xn yn In the creation of the file, one can instead use the following: table % numpts xlo xhi formula to create a "formula" table which is linearly interpolated table @ filename creates an array for two-valued functions filename has the following info: nxpts nypts xlo xhi ylo yhi nx*ny points as follows f(x1,y1), f(x2,y1),....,f(xn,y1), ... f(x1,ym), ..., f(xn,ym) to be added later **************************************************************/ #include #include #define MAX_TAB 50 typedef struct { double xlo,xhi,dx; double *y,*x; int n,flag,interp,autoeval; int xyvals; /* flag=0 if virgin array, flag=1 if already allocated; flag=2 for function interp=0 for normal interpolation, interp=1 for 'step' interp=2 for cubic spline table and finally, xyvals=1 if both x and y vals are needed (xyvals=0 is faster lookup )*/ char filename[128],name[12]; }TABULAR; TABULAR my_table[MAX_TAB]; double atof(); double get_ivar(); double evaluate(); extern int NTable; extern int NCON,NSYM,NCON_START,NSYM_START; extern int MAXSTOR; extern float **storage; set_auto_eval_flags(int f) { int i; for(i=0;i=MAXSTOR)len=MAXSTOR-1; for(i=0;i=xv[n-1]) return(yv[n-1]+(yv[n-2]-yv[n-1])*(x-xv[n-1])/(xv[n-1]-xv[n-2])); x1=xv[0]; y1=yv[0]; for(i=1;i0&&i1<(n-2)) return tab_interp(xlo,dx,x,y,n,i1); /* if it is on the edge - use linear */ i2=i1+1; if(i1>-1&&i2=n)return(y[n-1]+(y[n-1]-y[n-2])*(x-xhi)/dx); } init_table() { int i; for(i=0;ixhi){ err_msg("Xlo > Xhi ???"); return(0); } if(npts<2){ err_msg("Too few points..."); return(0); } if(my_table[index].flag==0){ my_table[index].y=(double *)malloc(length*sizeof(double)); } else { my_table[index].y= (double *)realloc((void *)my_table[index].y,length*sizeof(double)); } if(my_table[index].y==NULL){ err_msg("Unable to allocate table"); return(0); } my_table[index].flag=2; if(eval_fun_table(npts,xlo,xhi,formula,my_table[index].y)){ my_table[index].xlo=xlo; my_table[index].xhi=xhi; my_table[index].n=npts; my_table[index].dx=(xhi-xlo)/((double)(npts-1)); strcpy(my_table[index].filename,formula); return(1); } return(0); } load_table(filename,index) char *filename; int index; { int i; char bobtab[100]; char * bob; int length; double xlo,xhi; FILE *fp; bob = bobtab; if(my_table[index].flag==2){ err_msg("Not a file table..."); return(0); } fp=fopen(filename,"r"); if(fp==NULL){ err_msg("File not found"); return(0); } my_table[index].interp=0; fgets(bob,100,fp); if (bob[0]=='i') /* closest step value */ { my_table[index].interp=1; bob++; /* skip past initial "i" to length */ }; if (bob[0]=='s') /* cubic spline */ { my_table[index].interp=2; bob++; /* skip past initial "i" to length */ }; length=atoi(bob); if(length<2){ err_msg("Length too small"); fclose(fp); return(0); } fgets(bob,100,fp); xlo=atof(bob); fgets(bob,100,fp); xhi=atof(bob); if(xlo>=xhi){ err_msg("xlo >= xhi ??? "); fclose(fp); return(0); } if(my_table[index].flag==0){ my_table[index].y=(double *)malloc(length*sizeof(double)); if(my_table[index].y==NULL){ err_msg("Unable to allocate table"); fclose(fp); return(0); } for(i=0;i ... arguments ... conv npts weight variable_name klo khi end_cond sparse npts variable filename name(0 ... npts-1) conv: name(i) = sum(k=klo,khi) weight(k-klo)*variable(i+k) with end_cond = zero means skip if off end = periodic means wrap around sparse: need a file with the structure: ncon i_1 w_1 ... i_ncon w_ncon for npts lines name(i) = sum(j=1,ncon_i) w_j name(i_j) */