EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
egutils.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file egutils.cpp
1 /*
2  ElmerGrid - A simple mesh generation and manipulation utility
3  Copyright (C) 1995- , CSC - IT Center for Science Ltd.
4 
5  Author: Peter Råback
6  Email: Peter.Raback@csc.fi
7  Address: CSC - IT Center for Science Ltd.
8  Keilaranta 14
9  02101 Espoo, Finland
10 
11  This program is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License
13  as published by the Free Software Foundation; either version 2
14  of the License, or (at your option) any later version.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 */
25 
26 
27 /* -------------------------------: egutils.c :----------------------------
28  Includes common operations for operating vectors and such.
29 */
30 
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <math.h>
35 
36 #include "egutils.h"
37 
38 
39 #define FREE_ARG char*
40 #define SHOWMEM 0
41 
42 #if SHOWMEM
43 static int nfloat=0, nint=0, cumnfloat=0, cumnint=0, locnint, locnfloat;
44 
45 int MemoryUsage()
46 {
47  if(locnint > 1000 || locnfloat > 1000)
48  printf("Memory used real %d %d %d int %d %d %d\n",
49  nfloat,cumnfloat,locnfloat,nint,cumnint,locnint);
50  locnfloat = locnint = 0;
51 }
52 #endif
53 
54 
55 void nrerror(const char error_text[])
56 /* standerd error handler */
57 {
58  fprintf(stderr,"run-time error...\n");
59  fprintf(stderr,"%s\n",error_text);
60  fprintf(stderr,"...now exiting to system...\n");
61  exit(1);
62 }
63 
64 
65 /* Vector initialization */
66 float *vector(int nl,int nh)
67 /* allocate a float vector with subscript range v[nl..nh] */
68 {
69  float *v;
70 
71  v = (float*)malloc((size_t) (nh-nl+1+1)*sizeof(float));
72  if (!v) nrerror("allocation failure in vector()");
73  return(v-nl+1);
74 }
75 
76 
77 
78 int *ivector(int nl,int nh)
79 /* Allocate an int vector with subscript range v[nl..nh] */
80 {
81  int *v;
82 
83  v=(int*) malloc((size_t) (nh-nl+1+1)*sizeof(int));
84  if (!v) nrerror("allocation failure in ivector()");
85 
86  #if SHOWMEM
87  locnint = (nh-nl+1+1);
88  nint += locnint;
89  cumnint += locnint;
90  MemoryUsage();
91 #endif
92 
93  return(v-nl+1);
94 }
95 
96 
97 unsigned char *cvector(int nl,int nh)
98 /* allocate an unsigned char vector with subscript range v[nl..nh] */
99 {
100  unsigned char *v;
101 
102  v=(unsigned char *)malloc((size_t) (nh-nl+1+1)*sizeof(unsigned char));
103  if (!v) nrerror("allocation failure in cvector()");
104  return(v-nl+1);
105 }
106 
107 
108 unsigned long *lvector(int nl,int nh)
109 /* allocate an unsigned long vector with subscript range v[nl..nh] */
110 {
111  unsigned long *v;
112 
113  v=(unsigned long *)malloc((size_t) (nh-nl+1+1)*sizeof(unsigned long));
114  if (!v) nrerror("allocation failure in lvector()");
115  return(v-nl+1);
116 }
117 
118 
119 
120 double *dvector(int nl,int nh)
121 /* allocate a double vector with subscript range v[nl..nh] */
122 {
123  double *v;
124 
125  v=(double *)malloc((size_t) (nh-nl+1+1)*sizeof(double));
126  if (!v) nrerror("allocation failure in dvector()");
127 
128 #if SHOWMEM
129  locnfloat = nh-nl+1+1;
130  nfloat += locnfloat;
131  cumnfloat += locnfloat;
132  MemoryUsage();
133 #endif
134 
135  return(v-nl+1);
136 }
137 
138 
139 /* Matrix initialization */
140 
141 float **matrix(int nrl,int nrh,int ncl,int nch)
142 /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
143 {
144  int i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
145  float **m;
146 
147  /* allocate pointers to rows */
148  m=(float **) malloc((size_t) (nrow+1)*sizeof(float*));
149  if (!m) nrerror("allocation failure 1 in matrix()");
150  m += 1;
151  m -= nrl;
152 
153  /* allocate rows and set pointers to them */
154  m[nrl]=(float *) malloc((size_t)((nrow*ncol+1)*sizeof(float)));
155  if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
156  m[nrl] += 1;
157  m[nrl] -= ncl;
158 
159  for(i=nrl+1;i<=nrh;i++)
160  m[i]=m[i-1]+ncol;
161 
162  return(m);
163 }
164 
165 
166 double **dmatrix(int nrl,int nrh,int ncl,int nch)
167 /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
168 {
169  int i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
170  double **m;
171 
172  /* allocate pointers to rows */
173  m=(double **) malloc((size_t) (nrow+1)*sizeof(double*));
174  if (!m) nrerror("allocation failure 1 in dmatrix()");
175  m += 1;
176  m -= nrl;
177 
178 
179  /* allocate rows and set pointers to them */
180  m[nrl]=(double *) malloc((size_t)((nrow*ncol+1)*sizeof(double)));
181  if (!m[nrl]) nrerror("allocation failure 2 in dmatrix()");
182  m[nrl] += 1;
183  m[nrl] -= ncl;
184 
185  for(i=nrl+1;i<=nrh;i++)
186  m[i]=m[i-1]+ncol;
187 
188 #if SHOWMEM
189  locnfloat = (nrow+1 + (nrow*ncol+1));
190  nfloat += locnfloat;
191  cumnfloat += locnfloat;
192  MemoryUsage();
193 #endif
194 
195  return(m);
196 }
197 
198 
199 int **imatrix(int nrl,int nrh,int ncl,int nch)
200 /* allocate an int matrix with subscript range m[nrl..nrh][ncl..nch] */
201 {
202  int i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
203  int **m;
204 
205  /* allocate pointers to rows */
206  m=(int **) malloc((size_t) (nrow+1)*sizeof(int*));
207  if (!m) nrerror("allocation failure 1 in imatrix()");
208  m += 1;
209  m -= nrl;
210 
211  /* allocate rows and set pointers to them */
212  m[nrl]=(int *) malloc((size_t)((nrow*ncol+1)*sizeof(int)));
213  if (!m[nrl]) nrerror("allocation failure 2 in imatrix()");
214  m[nrl] += 1;
215  m[nrl] -= ncl;
216 
217  for(i=nrl+1;i<=nrh;i++)
218  m[i]=m[i-1]+ncol;
219 
220 #if SHOWMEM
221  locnint = (nrow+1 + (nrow*ncol+1));
222  nint += locnint;
223  cumnint += locnint;
224  MemoryUsage();
225 #endif
226 
227  return(m);
228 }
229 
230 
231 /* Deallocation routines */
232 
233 void free_vector(float *v,int nl,int nh)
234 {
235  free((FREE_ARG) (v+nl-1));
236 }
237 
238 void free_ivector(int *v,int nl,int nh)
239 {
240 #if SHOWMEM
241  cumnint -= (nh-nl+1+1);
242 #endif
243 
244  free((FREE_ARG) (v+nl-1));
245 }
246 
247 void free_cvector(unsigned char *v,int nl,int nh)
248 {
249  free((FREE_ARG) (v+nl-1));
250 }
251 
252 void free_lvector(unsigned long *v,int nl,int nh)
253 {
254  free((FREE_ARG) (v+nl-1));
255 }
256 
257 
258 void free_dvector(double *v,int nl,int nh)
259 {
260 #if SHOWMEM
261  cumnfloat -= (nh-nl+1+1);
262 #endif
263 
264  free((FREE_ARG) (v+nl-1));
265 }
266 
267 void free_matrix(float **m,int nrl,int nrh,int ncl,int nch)
268 {
269  free((FREE_ARG) (m[nrl]+ncl-1));
270  free((FREE_ARG) (m+nrl-1));
271 }
272 
273 void free_dmatrix(double **m,int nrl,int nrh,int ncl,int nch)
274 {
275 #if SHOWMEM
276  int nrow=nrh-nrl+1;
277  int ncol=nch-ncl+1;
278  cumnfloat -= (nrow+1 + (nrow*ncol+1));
279 #endif
280 
281  free((FREE_ARG) (m[nrl]+ncl-1));
282  free((FREE_ARG) (m+nrl-1));
283 }
284 
285 void free_imatrix(int **m,int nrl,int nrh,int ncl,int nch)
286 {
287 #if SHOWMEM
288  int nrow=nrh-nrl+1;
289  int ncol=nch-ncl+1;
290  cumnint -= (nrow+1 + (nrow*ncol+1));
291 #endif
292 
293  free((FREE_ARG) (m[nrl]+ncl-1));
294  free((FREE_ARG) (m+nrl-1));
295 }
296 
297 
298 void bigerror(const char error_text[])
299 {
300  fprintf(stderr,"The program encountered a major error...\n");
301  fprintf(stderr,"%s\n",error_text);
302  fprintf(stderr,"...now exiting to system...\n");
303  exit(1);
304 }
305 
306 
307 void smallerror(const char error_text[])
308 {
309  fprintf(stderr,"The program encountered a minor error...\n");
310  fprintf(stderr,"%s\n",error_text);
311  fprintf(stderr,"...we'll try to continue...\n");
312  exit(1);
313 }
314 
315 
316 
318 {
319  FILE *in;
320 
321  if((in = fopen(filename,"r")) == NULL)
322  return(FALSE);
323  else {
324  fclose(in);
325  return(TRUE);
326  }
327 }
328 
329 
330 Real Minimum(Real *vector,int first,int last)
331 /* Returns the smallest value of vector in range [first,last]. */
332 {
333  Real min;
334  int i;
335 
336  min=vector[first];
337  for(i=first+1;i<=last;i++)
338  if(min>vector[i]) min=vector[i];
339 
340  return(min);
341 }
342 
343 
344 int Minimi(Real *vector,int first,int last)
345 /* Returns the position of the smallest value of vector in range [first,last]. */
346 {
347  Real min;
348  int i,mini = 0;
349 
350  min=vector[first];
351  for(i=first+1;i<=last;i++)
352  if(min>vector[i])
353  min=vector[(mini=i)];
354 
355  return(mini);
356 }
357 
358 
359 Real Maximum(Real *vector,int first,int last)
360 /* Returns the largest value of vector in range [first,last]. */
361 {
362  Real max;
363  int i;
364 
365  max=vector[first];
366  for(i=first+1;i<=last;i++)
367  if(max<vector[i]) max=vector[i];
368 
369  return(max);
370 }
371 
372 
373 int Maximi(Real *vector,int first,int last)
374 /* Returns the position of the largest value of vector in range [first,last]. */
375 {
376  Real max;
377  int i,maxi = 0;
378 
379  max=vector[first];
380  for(i=first+1;i<=last;i++)
381  if(max<vector[i])
382  max=vector[(maxi=i)];
383 
384  return(maxi);
385 }
386 
387 
388 void AddExtension(const char *fname1,char *fname2,const char *newext)
389 /* Changes the extension of a filename.
390  'fname1' - the original filename
391  'fname2' - the new filename
392  'newext' - the new extension
393  If there is originally no extension it's appended. In this case
394  there has to be room for the extension.
395  */
396 {
397  char *ptr1;
398 
399  strcpy(fname2,fname1);
400 
401  //ML:
402  return;
403 
404  ptr1 = strchr(fname2, '.');
405  if (ptr1) *ptr1 = '\0';
406  strcat(fname2, ".");
407  strcat(fname2,newext);
408 }
409 
410 int StringToStrings(const char *buf,char args[10][10],int maxcnt,char separator)
411 /* Finds real numbers separated by a certain separator from a string.
412  'buf' - input string ending to a EOF
413  'dest' - a vector of real numbers
414  'maxcnt' - maximum number of real numbers to be read
415  'separator' - the separator of real numbers
416  The number of numbers found is returned in the function value.
417  */
418 {
419  int i,cnt,totlen,finish;
420  char *ptr1 = (char *)buf, *ptr2;
421 
422 
423  totlen = strlen(buf);
424  finish = 0;
425  cnt = 0;
426 
427  if (!buf[0]) return 0;
428 
429  do {
430  ptr2 = strchr(ptr1,separator);
431  if(ptr2) {
432  for(i=0;i<10;i++) {
433  args[cnt][i] = ptr1[i];
434  if(ptr1 + i >= ptr2) break;
435  }
436  args[cnt][i] = '\0';
437  ptr1 = ptr2+1;
438  }
439  else {
440  for(i=0;i<10;i++) {
441  if(ptr1 + i >= buf+totlen) break;
442  args[cnt][i] = ptr1[i];
443  }
444  args[cnt][i] = '\0';
445  finish = 1;
446  }
447 
448  cnt++;
449  } while (cnt < maxcnt && !finish);
450 
451  return cnt;
452 }
453 
454 
455 int StringToReal(const char *buf,Real *dest,int maxcnt,char separator)
456 /* Finds real numbers separated by a certain separator from a string.
457  'buf' - input string ending to a EOF
458  'dest' - a vector of real numbers
459  'maxcnt' - maximum number of real numbers to be read
460  'separator' - the separator of real numbers
461  The number of numbers found is returned in the function value.
462  */
463 {
464  int cnt = 0;
465  char *ptr1 = (char *)buf, *ptr2;
466 
467  if (!buf[0]) return 0;
468  do {
469  ptr2 = strchr(ptr1,separator);
470  if (ptr2) ptr2[0] = '\0';
471  dest[cnt++] = atof(ptr1);
472  if (ptr2) ptr1 = ptr2+1;
473  } while (cnt < maxcnt && ptr2 != NULL);
474 
475  return cnt;
476 }
477 
478 
479 int StringToInteger(const char *buf,int *dest,int maxcnt,char separator)
480 {
481  int cnt = 0;
482  char *ptr1 = (char *)buf, *ptr2;
483 
484  if (!buf[0]) return 0;
485  do {
486  ptr2 = strchr(ptr1,separator);
487  if (ptr2) ptr2[0] = '\0';
488  dest[cnt++] = atoi(ptr1);
489  if (ptr2) ptr1 = ptr2+1;
490  } while (cnt < maxcnt && ptr2 != NULL);
491 
492  return cnt;
493 }
494 
495 
496 int next_int(char **start)
497 {
498  int i;
499  char *end;
500 
501  i = strtol(*start,&end,10);
502  *start = end;
503  return(i);
504 }
505 
506 
508 {
509  Real r;
510  char *end;
511 
512  r = strtod(*start,&end);
513 
514  *start = end;
515  return(r);
516 }
517 
518 
519 
520 /* Indexing algorithm, Creates an index table */
521 #define SWAPI(a,b) itemp=(a);(a)=(b);(b)=itemp;
522 #define M 7
523 #define NSTACK 50
524 
525 void SortIndex(int n,double *arr,int *indx)
526 {
527  int i,indxt,ir,itemp,j,k,l;
528  int jstack,*istack;
529  double a;
530 
531  ir = n;
532  l = 1;
533  jstack = 0;
534  istack = ivector(1,NSTACK);
535 
536  for(j=1;j<=n;j++)
537  indx[j] = j;
538 
539  for(;;) {
540  if (ir-l < M) {
541  for(j=l+1;j<=ir;j++) {
542  indxt = indx[j];
543  a = arr[indxt];
544  for(i=j-1;i>=1;i--) {
545  if(arr[indx[i]] <= a) break;
546  indx[i+1] = indx[i];
547  }
548  indx[i+1] = indxt;
549  }
550  if(jstack == 0) break;
551  ir = istack[jstack--];
552  l = istack[jstack--];
553  }
554  else {
555  k = (l+ir) >> 1;
556  SWAPI(indx[k],indx[l+1]);
557  if(arr[indx[l+1]] > arr[indx[ir]]) {
558  SWAPI(indx[l+1],indx[ir]);
559  }
560  if(arr[indx[l]] > arr[indx[ir]]) {
561  SWAPI(indx[l],indx[ir]);
562  }
563  if(arr[indx[l+1]] > arr[indx[l]]) {
564  SWAPI(indx[l+1],indx[l]);
565  }
566  i = l+1;
567  j = ir;
568  indxt = indx[l];
569  a = arr[indxt];
570  for(;;) {
571  do i++; while(arr[indx[i]] < a);
572  do j--; while(arr[indx[j]] > a);
573  if(j < i) break;
574  SWAPI(indx[i],indx[j]);
575  }
576  indx[l] = indx[j];
577  indx[j] = indxt;
578  jstack += 2;
579  if(jstack > NSTACK) printf("NSTACK too small in SortIndex.");
580  if(ir-i+1 >= j-l) {
581  istack[jstack] = ir;
582  istack[jstack-1] = i;
583  ir = j-1;
584  } else {
585  istack[jstack] = j-1;
586  istack[jstack-1] = l;
587  l = i;
588  }
589  }
590  }
591  free_ivector(istack,1,NSTACK);
592 }
593 
594 
595 
596