Adding DisEMBL dependency Tisean executable
[jabaws.git] / binaries / src / disembl / Tisean_3.0.1 / source_c / fsle.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
6  *   TISEAN is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   TISEAN is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with TISEAN; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 /*Author: Rainer Hegger, last modified: Mar 20, 1999 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <limits.h>
25 #include <string.h>
26 #include "routines/tsa.h"
27
28 #define WID_STR "Estimates the finite size Lyapunov exponent; Vulpiani et al."
29
30
31 #define NMAX 256
32
33 char *outfile=NULL;
34 char *infile=NULL;
35 char epsset=0,stdo=1;
36 double *series;
37 long box[NMAX][NMAX],*list;
38 unsigned int dim=2,delay=1,mindist=0;
39 unsigned int column=1;
40 unsigned int verbosity=0xff;
41 const unsigned int nmax=NMAX-1;
42 unsigned long length=ULONG_MAX,exclude=0;
43 double eps0=1.e-3,eps,epsinv,epsmax,epsfactor;
44 int howmany;
45
46 struct fsle {
47   double time,factor,eps;
48   long count;
49 } *data;
50
51 void show_options(char *progname)
52 {
53   what_i_do(progname,WID_STR);
54   fprintf(stderr," Usage: %s [options]\n",progname);
55   fprintf(stderr," Options:\n");
56   fprintf(stderr,"Everything not being a valid option will be interpreted"
57           " as a possible"
58           " datafile.\nIf no datafile is given stdin is read. Just - also"
59           " means stdin\n");
60   fprintf(stderr,"\t-l # of datapoints [default is whole file]\n");
61   fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n");
62   fprintf(stderr,"\t-c column to read[default 1]\n");
63   fprintf(stderr,"\t-m embedding dimension [default 2]\n");
64   fprintf(stderr,"\t-d delay  [default 1]\n");
65   fprintf(stderr,"\t-t time window to omit [default 0]\n");
66   fprintf(stderr,"\t-r epsilon size to start with [default "
67           "(std. dev. of data)/1000]\n");
68   fprintf(stderr,"\t-o name of output file [default 'datafile'.fsl ,"
69           "without -o: stdout]\n");
70   fprintf(stderr,"\t-V verbosity level [default 1]\n\t\t"
71           "0='only panic messages'\n\t\t"
72           "1='+ input/output messages'\n");
73   fprintf(stderr,"\t-h show these options\n");
74   fprintf(stderr,"\n");
75   exit(0);
76 }
77
78 void scan_options(int n,char **argv)
79 {
80   char *out;
81
82   if ((out=check_option(argv,n,'l','u')) != NULL)
83     sscanf(out,"%lu",&length);
84   if ((out=check_option(argv,n,'x','u')) != NULL)
85     sscanf(out,"%lu",&exclude);
86   if ((out=check_option(argv,n,'c','u')) != NULL)
87     sscanf(out,"%u",&column);
88   if ((out=check_option(argv,n,'m','u')) != NULL)
89     sscanf(out,"%u",&dim);
90   if ((out=check_option(argv,n,'d','u')) != NULL)
91     sscanf(out,"%u",&delay);
92   if ((out=check_option(argv,n,'t','u')) != NULL)
93     sscanf(out,"%u",&mindist);
94   if ((out=check_option(argv,n,'V','u')) != NULL)
95     sscanf(out,"%u",&verbosity);
96   if ((out=check_option(argv,n,'r','f')) != NULL) {
97     epsset=1;
98     sscanf(out,"%lf",&eps0);
99   }
100   if ((out=check_option(argv,n,'o','o')) != NULL) {
101     stdo=0;
102     if (strlen(out) > 0)
103       outfile=out;
104   }
105 }
106       
107 void put_in_boxes(void)
108 {
109   int i,j,x,y,del;
110
111   for (i=0;i<NMAX;i++)
112     for (j=0;j<NMAX;j++)
113       box[i][j]= -1;
114
115   del=delay*(dim-1);
116   for (i=0;i<length-del;i++) {
117     x=(int)(series[i]*epsinv)&nmax;
118     y=(int)(series[i+del]*epsinv)&nmax;
119     list[i]=box[x][y];
120     box[x][y]=i;
121   }
122 }
123
124 char make_iterate(long act)
125 {
126   char ok=0;
127   int x,y,i,j,i1,k,del1=dim*delay,which;
128   long element,minelement= -1;
129   double dx=0.0,mindx=2.0,stime;
130
131   x=(int)(series[act]*epsinv)&nmax;
132   y=(int)(series[act+delay*(dim-1)]*epsinv)&nmax;
133   for (i=x-1;i<=x+1;i++) {
134     i1=i&nmax;
135     for (j=y-1;j<=y+1;j++) {
136       element=box[i1][j&nmax];
137       while (element != -1) {
138         if (labs(act-element) > mindist) {
139           for (k=0;k<del1;k+=delay) {
140             dx = fabs(series[act+k]-series[element+k]);
141             if (dx > eps)
142               break;
143           }
144           if (k==del1) {
145             if (dx < mindx) {
146               ok=1;
147               if (dx > 0.0) {
148                 mindx=dx;
149                 minelement=element;
150               }
151             }
152           }
153         }
154         element=list[element];
155       }
156     }
157   }
158   
159   if ((minelement != -1) && (mindx < eps)) {
160     act += del1-delay+1;
161     minelement += del1-delay+1;
162     which=(int)(log(mindx/eps0)/log(epsfactor));
163     if (which < 0) {
164       while ((dx=fabs(series[act]-series[minelement])) < data[0].eps) {
165         act++;
166         minelement++;
167         if ((act >= length) || (minelement >= length))
168           return ok;
169       }
170       mindx=dx;
171       which=(int)(log(mindx/eps0)/log(epsfactor));
172     }
173     for (i=which;i<howmany-1;i++) {
174       stime=0;
175       while ((dx=fabs(series[act]-series[minelement])) < data[i+1].eps) {
176         act++;
177         minelement++;
178         if ((act >= length) || (minelement >= length))
179           return ok;
180         stime++;
181       }
182       if (stime > 0) {
183         data[i].time += stime;
184         data[i].factor += log(dx/mindx);
185         data[i].count++;
186       }
187       mindx=dx;
188     }
189   }
190   return ok;
191 }
192
193 int main(int argc,char **argv)
194 {
195   char stdi=0,*done,alldone;
196   int i;
197   long n;
198   long maxlength;
199   double min,max,se_av,se_var,se0_av,se0_var;
200   FILE *file;
201   
202   if (scan_help(argc,argv))
203     show_options(argv[0]);
204   
205   scan_options(argc,argv);
206 #ifndef OMIT_WHAT_I_DO
207   if (verbosity&VER_INPUT)
208     what_i_do(argv[0],WID_STR);
209 #endif
210
211   infile=search_datafile(argc,argv,&column,verbosity);
212   if (infile == NULL)
213     stdi=1;
214   
215   if (outfile == NULL) {
216     if (!stdi) {
217       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
218       strcpy(outfile,infile);
219       strcat(outfile,".fsl");
220     }
221     else {
222       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
223       strcpy(outfile,"stdin.fsl");
224     }
225   }
226   if (!stdo)
227     test_outfile(outfile);
228
229   series=(double*)get_series(infile,&length,exclude,column,verbosity);
230   variance(series,length,&se0_av,&se0_var);
231   rescale_data(series,length,&min,&max);
232   variance(series,length,&se_av,&se_var);
233   
234   if (epsset) {
235     eps0 /= max;
236     epsmax=se0_var;
237   }
238   else {
239     eps0 *= se_var;
240     epsmax=se_var;
241   }
242   if (eps0 >= epsmax) {
243     fprintf(stderr,"The minimal epsilon is too large. Exiting!\n");
244     exit(FSLE__TOO_LARGE_MINEPS);
245   }
246   epsfactor=sqrt(2.0);
247
248   howmany=(int)(log(epsmax/eps0)/log(epsfactor))+1;
249   check_alloc(data=(struct fsle*)malloc(sizeof(struct fsle)*howmany));
250   eps=eps0/epsfactor;
251   for (i=0;i<howmany;i++) {
252     data[i].time=data[i].factor=0.0;
253     data[i].eps= (eps *= epsfactor);
254     data[i].count=0;
255   }
256   
257   check_alloc(list=(long*)malloc(length*sizeof(long)));
258   check_alloc(done=(char*)malloc(length));
259
260   for (i=0;i<length;i++)
261     done[i]=0;
262   
263   maxlength=length-delay*(dim-1)-1-mindist;
264   alldone=0;
265   for (eps=eps0;(eps<=epsmax) && (!alldone);eps*=epsfactor) {
266     epsinv=1.0/eps;
267     put_in_boxes();
268     alldone=1;
269     for (n=0;n<=maxlength;n++) {
270       if (!done[n])
271         done[n]=make_iterate(n);
272       alldone &= done[n];
273     }
274   } 
275   if (!stdo) {
276     file=fopen(outfile,"w");
277     if (verbosity&VER_INPUT)
278       fprintf(stderr,"Opened %s for writing\n",outfile);
279     for (i=0;i<howmany;i++)
280       if (data[i].factor > 0.0)
281         fprintf(file,"%e %e %ld\n",data[i].eps*max,
282                 data[i].factor/data[i].time,data[i].count);
283     fclose(file);
284   }
285   else {
286     if (verbosity&VER_INPUT)
287       fprintf(stderr,"Writing to stdout\n");
288     for (i=0;i<howmany;i++)
289       if (data[i].factor > 0.0)
290         fprintf(stdout,"%e %e %ld\n",data[i].eps*max,
291                 data[i].factor/data[i].time,data[i].count);
292   }    
293
294   if (infile != NULL)
295     free(infile);
296   if (outfile != NULL)
297     free(outfile);
298   free(series);
299   free(data);
300   free(list);
301   free(done);
302
303   return 0;
304 }