/[escript]/branches/domexper/dudley/src/Util.c
ViewVC logotype

Diff of /branches/domexper/dudley/src/Util.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/esys2/finley/src/finleyC/Util.c revision 115 by jgs, Fri Mar 4 07:12:47 2005 UTC trunk/finley/src/Util.c revision 751 by bcumming, Mon Jun 26 01:46:34 2006 UTC
# Line 1  Line 1 
1  /* $Id$ */  /*
2     ************************************************************
3     *          Copyright 2006 by ACcESS MNRF                   *
4     *                                                          *
5     *              http://www.access.edu.au                    *
6     *       Primary Business: Queensland, Australia            *
7     *  Licensed under the Open Software License version 3.0    *
8     *     http://www.opensource.org/licenses/osl-3.0.php       *
9     *                                                          *
10     ************************************************************
11    */
12    
13  /**************************************************************/  /**************************************************************/
14    
# Line 6  Line 16 
16    
17  /**************************************************************/  /**************************************************************/
18    
 /*   Copyrights by ACcESS Australia, 2003 */  
19  /*   author: gross@access.edu.au */  /*   author: gross@access.edu.au */
20  /*   Version: $Id$ */  /*   Version: $Id$ */
21    
22  /**************************************************************/  /**************************************************************/
23    
 #include "Common.h"  
24  #include "Finley.h"  #include "Finley.h"
25  #include "Util.h"  #include "Util.h"
26  #ifdef _OPENMP  #ifdef _OPENMP
# Line 21  Line 29 
29    
30  /**************************************************************/  /**************************************************************/
31    
32    /*   returns true if any of the values in the short array values is not equalt to Zero */
33    
34    bool_t Finley_Util_anyNonZeroDouble(dim_t N, double* values) {
35       dim_t q;
36       for (q=0;q<N;++q) if (ABS(values[q])>0) return TRUE;
37       return FALSE;
38    }
39    /**************************************************************/
40    
41  /*   gathers double values out from in by index: */  /*   gathers double values out from in by index: */
42    
43  /*        out(1:numData,1:len)=in(1:numData,index(1:len)) */  /*        out(1:numData,1:len)=in(1:numData,index(1:len)) */
44    
45  void Finley_Util_Gather_double(int len,maybelong* index,int numData,double* in, double * out){  void Finley_Util_Gather_double(dim_t len,index_t* index,dim_t numData,double* in, double * out){
46      int s,i;      dim_t s,i;
47      for (s=0;s<len;s++) {      for (s=0;s<len;s++) {
48         for (i=0;i<numData;i++) {         for (i=0;i<numData;i++) {
49            out[INDEX2(i,s,numData)]=in[INDEX2(i,index[s],numData)];            out[INDEX2(i,s,numData)]=in[INDEX2(i,index[s],numData)];
# Line 41  void Finley_Util_Gather_double(int len,m Line 58  void Finley_Util_Gather_double(int len,m
58    
59  /*        out(1:numData,1:len)=in(1:numData,index(1:len)) */  /*        out(1:numData,1:len)=in(1:numData,index(1:len)) */
60    
61  void Finley_Util_Gather_int(int len,maybelong* index,int numData, maybelong* in, maybelong * out){  void Finley_Util_Gather_int(dim_t len,index_t* index,dim_t numData, index_t* in, index_t * out){
62      int s,i;      dim_t s,i;
63      for (s=0;s<len;s++) {      for (s=0;s<len;s++) {
64         for (i=0;i<numData;i++) {         for (i=0;i<numData;i++) {
65            out[INDEX2(i,s,numData)]=in[INDEX2(i,index[s],numData)];            out[INDEX2(i,s,numData)]=in[INDEX2(i,index[s],numData)];
# Line 56  void Finley_Util_Gather_int(int len,mayb Line 73  void Finley_Util_Gather_int(int len,mayb
73    
74  /*        out(1:numData,index(1:len))+=in(1:numData,1:len) */  /*        out(1:numData,index(1:len))+=in(1:numData,1:len) */
75    
76  void Finley_Util_AddScatter(int len,maybelong* index,int numData,double* in,double * out){  void Finley_Util_AddScatter(dim_t len,index_t* index,dim_t numData,double* in,double * out){
77     int i,s;     dim_t i,s;
78     for (s=0;s<len;s++) {     for (s=0;s<len;s++) {
79         for(i=0;i<numData;i++) {         for(i=0;i<numData;i++) {
80            #pragma omp atomic            #pragma omp atomic
# Line 66  void Finley_Util_AddScatter(int len,mayb Line 83  void Finley_Util_AddScatter(int len,mayb
83     }     }
84  }  }
85    
86    #ifdef PASO_MPI
87    /* same as AddScatter(), but checks that value index[] is below an upper bound upperBound before  
88       addition. This is used to ensure that only the influence of local DOF is added */
89    /*        out(1:numData,index[p])+=in(1:numData,p)
90            where p = {k=1...len , index[k]<upperBound}*/
91    void Finley_Util_AddScatter_upperBound(dim_t len,index_t* index,dim_t numData,double* in,double * out, index_t upperBound){
92       dim_t i,s;
93       for (s=0;s<len;s++) {
94           for(i=0;i<numData;i++) {
95              //#pragma omp atomic
96              if( index[s]<upperBound )
97                out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];
98           }
99       }
100    }  
101    
102    
103    #endif
104    
105  /*    multiplies two matrices */  /*    multiplies two matrices */
106    
107  /*          A(1:A1,1:A2)=B(1:A1,1:B2)*C(1:B2,1:A2) */  /*          A(1:A1,1:A2)=B(1:A1,1:B2)*C(1:B2,1:A2) */
108    
109  void Finley_Util_SmallMatMult(int A1,int A2, double* A, int B2, double*B, double* C) {  void Finley_Util_SmallMatMult(dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
110      int i,j,s;      dim_t i,j,s;
111      for (i=0;i<A1*A2;i++) A[i]=0;      for (i=0;i<A1*A2;i++) A[i]=0;
112         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
113            for (j=0;j<A2;j++) {            for (j=0;j<A2;j++) {
# Line 86  void Finley_Util_SmallMatMult(int A1,int Line 122  void Finley_Util_SmallMatMult(int A1,int
122    
123  /*        A(1:A1,1:A2,i)=B(1:A1,1:B2,i)*C(1:B2,1:A2,i) i=1,len */  /*        A(1:A1,1:A2,i)=B(1:A1,1:B2,i)*C(1:B2,1:A2,i) i=1,len */
124    
125  void Finley_Util_SmallMatSetMult(int len,int A1,int A2, double* A, int B2, double*B, double* C) {  void Finley_Util_SmallMatSetMult(dim_t len,dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
126      int q,i,j,s;      dim_t q,i,j,s;
127      for (i=0;i<A1*A2*len;i++) A[i]=0;      for (i=0;i<A1*A2*len;i++) A[i]=0;
128      for (q=0;q<len;q++) {      for (q=0;q<len;q++) {
129         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
# Line 102  void Finley_Util_SmallMatSetMult(int len Line 138  void Finley_Util_SmallMatSetMult(int len
138  /*    inverts the set of dim x dim matrices A(:,:,1:len) with dim=1,2,3 */  /*    inverts the set of dim x dim matrices A(:,:,1:len) with dim=1,2,3 */
139  /*    the determinante is returned. */  /*    the determinante is returned. */
140    
141  void Finley_Util_InvertSmallMat(int len,int dim,double* A,double *invA, double* det){  void Finley_Util_InvertSmallMat(dim_t len,dim_t dim,double* A,double *invA, double* det){
142     int q;     dim_t q;
143     double D,A11,A12,A13,A21,A22,A23,A31,A32,A33;     register double D,A11,A12,A13,A21,A22,A23,A31,A32,A33;
144    
145     switch(dim) {     switch(dim) {
146        case 1:        case 1:
# Line 115  void Finley_Util_InvertSmallMat(int len, Line 151  void Finley_Util_InvertSmallMat(int len,
151                 D=1./D;                 D=1./D;
152                 invA[q]=D;                 invA[q]=D;
153              } else {              } else {
154                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
155                 return;                 return;
156              }              }
157           }           }
# Line 138  void Finley_Util_InvertSmallMat(int len, Line 173  void Finley_Util_InvertSmallMat(int len,
173                 invA[INDEX3(0,1,q,2,2)]=-A12*D;                 invA[INDEX3(0,1,q,2,2)]=-A12*D;
174                 invA[INDEX3(1,1,q,2,2)]= A11*D;                 invA[INDEX3(1,1,q,2,2)]= A11*D;
175              } else {              } else {
176                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
177                 return;                 return;
178              }              }
179           }           }
# Line 171  void Finley_Util_InvertSmallMat(int len, Line 205  void Finley_Util_InvertSmallMat(int len,
205                 invA[INDEX3(1,2,q,3,3)]=(A13*A21-A11*A23)*D;                 invA[INDEX3(1,2,q,3,3)]=(A13*A21-A11*A23)*D;
206                 invA[INDEX3(2,2,q,3,3)]=(A11*A22-A12*A21)*D;                 invA[INDEX3(2,2,q,3,3)]=(A11*A22-A12*A21)*D;
207              } else {              } else {
208                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
209                 return;                 return;
210              }              }
211           }           }
# Line 184  void Finley_Util_InvertSmallMat(int len, Line 217  void Finley_Util_InvertSmallMat(int len,
217    
218  /*    sets the derterminate of a set of dim x dim matrices A(:,:,1:len) with dim=1,2,3 */  /*    sets the derterminate of a set of dim x dim matrices A(:,:,1:len) with dim=1,2,3 */
219    
220  void Finley_Util_DetOfSmallMat(int len,int dim,double* A, double* det){  void Finley_Util_DetOfSmallMat(dim_t len,dim_t dim,double* A, double* det){
221     int q;     dim_t q;
222     double A11,A12,A13,A21,A22,A23,A31,A32,A33;     register double A11,A12,A13,A21,A22,A23,A31,A32,A33;
223    
224     switch(dim) {     switch(dim) {
225        case 1:        case 1:
# Line 228  void Finley_Util_DetOfSmallMat(int len,i Line 261  void Finley_Util_DetOfSmallMat(int len,i
261  /*    returns the normalized vector Normal[dim,len] orthogonal to A(:,0,q) and A(:,1,q) in the case of dim=3  */  /*    returns the normalized vector Normal[dim,len] orthogonal to A(:,0,q) and A(:,1,q) in the case of dim=3  */
262  /*    or the vector A(:,0,q) in the case of dim=2                                             */  /*    or the vector A(:,0,q) in the case of dim=2                                             */
263    
264  void  Finley_NormalVector(int len, int dim, int dim1, double* A,double* Normal) {  void  Finley_NormalVector(dim_t len, dim_t dim, dim_t dim1, double* A,double* Normal) {
265     int q;     dim_t q;
266     double A11,A12,CO_A13,A21,A22,CO_A23,A31,A32,CO_A33,length,invlength;     register double A11,A12,CO_A13,A21,A22,CO_A23,A31,A32,CO_A33,length,invlength;
267    
268     switch(dim) {     switch(dim) {
269        case 1:        case 1:
# Line 242  void  Finley_NormalVector(int len, int d Line 275  void  Finley_NormalVector(int len, int d
275              A21=A[INDEX3(1,0,q,2,dim1)];              A21=A[INDEX3(1,0,q,2,dim1)];
276              length = sqrt(A11*A11+A21*A21);              length = sqrt(A11*A11+A21*A21);
277              if (! length>0) {              if (! length>0) {
278                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
                sprintf(Finley_ErrorMsg,"area equals zero.");  
279                 return;                 return;
280              } else {              } else {
281                 invlength=1./length;                 invlength=1./length;
# Line 265  void  Finley_NormalVector(int len, int d Line 297  void  Finley_NormalVector(int len, int d
297              CO_A33=A11*A22-A21*A12;              CO_A33=A11*A22-A21*A12;
298              length=sqrt(CO_A13*CO_A13+CO_A23*CO_A23+CO_A33*CO_A33);              length=sqrt(CO_A13*CO_A13+CO_A23*CO_A23+CO_A33*CO_A33);
299              if (! length>0) {              if (! length>0) {
300                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
                sprintf(Finley_ErrorMsg,"area equals zero.");  
301                 return;                 return;
302              } else {              } else {
303                 invlength=1./length;                 invlength=1./length;
# Line 285  void  Finley_NormalVector(int len, int d Line 316  void  Finley_NormalVector(int len, int d
316  /*    return the length of the vector which is orthogonal to the vectors A(:,0,q) and A(:,1,q) in the case of dim=3 */  /*    return the length of the vector which is orthogonal to the vectors A(:,0,q) and A(:,1,q) in the case of dim=3 */
317  /*    or the vector A(:,0,q) in the case of dim=2                                                                   */  /*    or the vector A(:,0,q) in the case of dim=2                                                                   */
318    
319  void  Finley_LengthOfNormalVector(int len, int dim, int dim1, double* A,double* length) {  void  Finley_LengthOfNormalVector(dim_t len, dim_t dim, dim_t dim1, double* A,double* length) {
320     int q;     dim_t q;
321     double A11,A12,CO_A13,A21,A22,CO_A23,A31,A32,CO_A33;     double A11,A12,CO_A13,A21,A22,CO_A23,A31,A32,CO_A33;
322    
323     switch(dim) {     switch(dim) {
# Line 323  void  Finley_LengthOfNormalVector(int le Line 354  void  Finley_LengthOfNormalVector(int le
354  /* there is no range checking! */  /* there is no range checking! */
355  /* at output Map[invMap[i]]=i for i=0:lenInvMap */  /* at output Map[invMap[i]]=i for i=0:lenInvMap */
356    
357  void Finley_Util_InvertMap(int lenInvMap, maybelong* invMap,int lenMap, maybelong* Map) {  void Finley_Util_InvertMap(dim_t lenInvMap, index_t* invMap,dim_t lenMap, index_t* Map) {
358     int i;     dim_t i;
359     for (i=0;i<lenInvMap;i++) invMap[i]=0;     for (i=0;i<lenInvMap;i++) invMap[i]=0;
360     for (i=0;i<lenMap;i++) {     for (i=0;i<lenMap;i++) {
361        if (Map[i]>=0) invMap[Map[i]]=i;        if (Map[i]>=0) invMap[Map[i]]=i;
# Line 342  int Finley_Util_ValueAndIndex_compar(con Line 373  int Finley_Util_ValueAndIndex_compar(con
373     if (e1->value > e2->value) return  1;     if (e1->value > e2->value) return  1;
374     return 0;     return 0;
375  }  }
376  void Finley_Util_sortValueAndIndex(int n,Finley_Util_ValueAndIndex* array) {  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {
377       /* OMP : needs parallelization !*/       /* OMP : needs parallelization !*/
378       qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);       qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);
379  }  }
# Line 352  void Finley_Util_sortValueAndIndex(int n Line 383  void Finley_Util_sortValueAndIndex(int n
383    
384  /* calculates the minimum value from a dim X N integer array */  /* calculates the minimum value from a dim X N integer array */
385    
386  maybelong Finley_Util_getMinInt(int dim,int N,maybelong* values) {  index_t Finley_Util_getMinInt(dim_t dim,dim_t N,index_t* values) {
387     maybelong i,j,out,out_local;     dim_t i,j;
388     out=MAYBELONG_MAX;     index_t out,out_local;
389       out=INDEX_T_MAX;
390     if (values!=NULL && dim*N>0 ) {     if (values!=NULL && dim*N>0 ) {
391       out=values[0];       out=values[0];
392       #pragma omp parallel private(out_local)       #pragma omp parallel private(out_local)
# Line 373  maybelong Finley_Util_getMinInt(int dim, Line 405  maybelong Finley_Util_getMinInt(int dim,
405                                                                                                                                                                                                                                                                                                        
406  /* calculates the maximum value from a dim X N integer array */  /* calculates the maximum value from a dim X N integer array */
407    
408  maybelong Finley_Util_getMaxInt(int dim,int N,maybelong* values) {  index_t Finley_Util_getMaxInt(dim_t dim,dim_t N,index_t* values) {
409     maybelong i,j,out,out_local;     dim_t i,j;
410     out=-MAYBELONG_MAX;     index_t out,out_local;
411       out=-INDEX_T_MAX;
412     if (values!=NULL && dim*N>0 ) {     if (values!=NULL && dim*N>0 ) {
413       out=values[0];       out=values[0];
414       #pragma omp parallel private(out_local)       #pragma omp parallel private(out_local)
# Line 394  maybelong Finley_Util_getMaxInt(int dim, Line 427  maybelong Finley_Util_getMaxInt(int dim,
427    
428  /* set the index of the positive entries in mask. The length of index is returned. */  /* set the index of the positive entries in mask. The length of index is returned. */
429    
430  maybelong Finley_Util_packMask(maybelong N,maybelong* mask,maybelong* index) {  dim_t Finley_Util_packMask(dim_t N,index_t* mask,index_t* index) {
431        maybelong out,k;        dim_t out,k;
432        out=0;        out=0;
433        /*OMP */        /*OMP */
434        for (k=0;k<N;k++) {        for (k=0;k<N;k++) {
# Line 408  maybelong Finley_Util_packMask(maybelong Line 441  maybelong Finley_Util_packMask(maybelong
441  }  }
442    
443  /* returns true if array contains value */  /* returns true if array contains value */
444  int Finley_Util_isAny(maybelong N,maybelong* array,maybelong value) {  bool_t Finley_Util_isAny(dim_t N,index_t* array,index_t value) {
445     int out=FALSE;     bool_t out=FALSE;
446     maybelong i;     dim_t i;
447     #pragma omp parallel for private(i) schedule(static) reduction(||:out)     #pragma omp parallel for private(i) schedule(static) reduction(||:out)
448     for (i=0;i<N;i++) out = out || (array[i]==value);     for (i=0;i<N;i++) out = out || (array[i]==value);
449     return out;     return out;
450  }  }
451  /* calculates the cummultative sum in array and returns the total sum */  /* calculates the cummultative sum in array and returns the total sum */
452  maybelong Finley_Util_cumsum(maybelong N,maybelong* array) {  index_t Finley_Util_cumsum(dim_t N,index_t* array) {
453     maybelong out=0,tmp,i;     index_t out=0,tmp;
454       dim_t i;
455     #ifdef _OPENMP     #ifdef _OPENMP
456        maybelong partial_sums[omp_get_max_threads()],sum;        index_t partial_sums[omp_get_max_threads()],sum;
457        #pragma omp parallel private(sum,i,tmp)        #pragma omp parallel private(sum,i,tmp)
458        {        {
459          sum=0;          sum=0;
# Line 455  maybelong Finley_Util_cumsum(maybelong N Line 489  maybelong Finley_Util_cumsum(maybelong N
489     return out;     return out;
490  }  }
491    
492  void Finley_copyDouble(int n,double* source, double* target) {  void Finley_copyDouble(dim_t n,double* source, double* target) {
493    int i;    dim_t i;
494    for (i=0;i<n;i++) target[i]=source[i];    for (i=0;i<n;i++) target[i]=source[i];
495  }  }
496    
497    #ifdef PASO_MPI
498    void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name  )
499    {
500      index_t i;
501      
502      if( name )
503        fprintf( fid, "%s [ ", name );
504      else
505        fprintf( fid, "[ " );  
506      for( i=0; i<(n<30 ? n : 30); i++ )
507        fprintf( fid, "%g ", array[i] );
508      if( n>=30 )
509        fprintf( fid, "... " );
510      fprintf( fid, "]\n" );
511    }
512    void Finley_printIntArray( FILE *fid, dim_t n, int *array, char *name  )
513    {
514      index_t i;
515      
516      if( name )
517        fprintf( fid, "%s [ ", name );
518      else
519        fprintf( fid, "[ " );  
520      for( i=0; i<(n<30 ? n : 30); i++ )
521        fprintf( fid, "%d ", array[i] );
522      if( n>=30 )
523        fprintf( fid, "... " );
524      fprintf( fid, "]\n" );
525    }
526    void Finley_printMaskArray( FILE *fid, dim_t n, int *array, char *name  )
527    {
528      index_t i;
529      
530      if( name )
531        fprintf( fid, "%s [ ", name );
532      else
533        fprintf( fid, "[ " );  
534      for( i=0; i<(n<30 ? n : 30); i++ )
535        if( array[i]!=-1 )
536          fprintf( fid, "%d ", array[i] );
537        else
538          fprintf( fid, "* " );
539      if( n>=30 )
540        fprintf( fid, "... " );
541      fprintf( fid, "]\n" );
542    }
543    #endif
544    
545    /*
546     * Revision 1.8  2005/08/12 01:45:43  jgs
547     *
548     * Revision 1.7.2.2  2005/09/07 06:26:22  gross
549     * the solver from finley are put into the standalone package paso now
550     *
551     * Revision 1.7.2.1  2005/08/04 22:41:11  gross
552     * some extra routines for finley that might speed-up RHS assembling in some cases (not actived right now)
553     *
554     * Revision 1.7  2005/07/08 04:07:59  jgs
555     * Merge of development branch back to main trunk on 2005-07-08
556     *
557     * Revision 1.1.1.1.2.4  2005/06/29 02:34:57  gross
558     * some changes towards 64 integers in finley
559     *
560     * Revision 1.1.1.1.2.3  2005/03/02 23:35:06  gross
561     * reimplementation of the ILU in Finley. block size>1 still needs some testing
562     *
563     * Revision 1.1.1.1.2.2  2005/02/18 02:27:31  gross
564     * two function that will be used for a reimplementation of the ILU preconditioner
565     *
566     * Revision 1.1.1.1.2.1  2004/11/12 06:58:19  gross
567     * a lot of changes to get the linearPDE class running: most important change is that there is no matrix format exposed to the user anymore. the format is chosen by the Domain according to the solver and symmetry
568     *
569     * Revision 1.1.1.1  2004/10/26 06:53:57  jgs
570     * initial import of project esys2
571     *
572     * Revision 1.3  2004/08/26 12:03:52  gross
573     * Some other bug in Finley_Assemble_gradient fixed.
574     *
575     * Revision 1.2  2004/07/02 04:21:13  gross
576     * Finley C code has been included
577     *
578     * Revision 1.1.1.1  2004/06/24 04:00:40  johng
579     * Initial version of eys using boost-python.
580     *
581     *
582     */

Legend:
Removed from v.115  
changed lines
  Added in v.751

  ViewVC Help
Powered by ViewVC 1.1.26