/[escript]/trunk/finley/src/Util.c
ViewVC logotype

Diff of /trunk/finley/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 616 by elspeth, Wed Mar 22 02:46:56 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 70  void Finley_Util_AddScatter(int len,mayb Line 87  void Finley_Util_AddScatter(int len,mayb
87    
88  /*          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) */
89    
90  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) {
91      int i,j,s;      dim_t i,j,s;
92      for (i=0;i<A1*A2;i++) A[i]=0;      for (i=0;i<A1*A2;i++) A[i]=0;
93         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
94            for (j=0;j<A2;j++) {            for (j=0;j<A2;j++) {
# Line 86  void Finley_Util_SmallMatMult(int A1,int Line 103  void Finley_Util_SmallMatMult(int A1,int
103    
104  /*        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 */
105    
106  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) {
107      int q,i,j,s;      dim_t q,i,j,s;
108      for (i=0;i<A1*A2*len;i++) A[i]=0;      for (i=0;i<A1*A2*len;i++) A[i]=0;
109      for (q=0;q<len;q++) {      for (q=0;q<len;q++) {
110         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
# Line 102  void Finley_Util_SmallMatSetMult(int len Line 119  void Finley_Util_SmallMatSetMult(int len
119  /*    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 */
120  /*    the determinante is returned. */  /*    the determinante is returned. */
121    
122  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){
123     int q;     dim_t q;
124     double D,A11,A12,A13,A21,A22,A23,A31,A32,A33;     register double D,A11,A12,A13,A21,A22,A23,A31,A32,A33;
125    
126     switch(dim) {     switch(dim) {
127        case 1:        case 1:
# Line 115  void Finley_Util_InvertSmallMat(int len, Line 132  void Finley_Util_InvertSmallMat(int len,
132                 D=1./D;                 D=1./D;
133                 invA[q]=D;                 invA[q]=D;
134              } else {              } else {
135                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
136                 return;                 return;
137              }              }
138           }           }
# Line 138  void Finley_Util_InvertSmallMat(int len, Line 154  void Finley_Util_InvertSmallMat(int len,
154                 invA[INDEX3(0,1,q,2,2)]=-A12*D;                 invA[INDEX3(0,1,q,2,2)]=-A12*D;
155                 invA[INDEX3(1,1,q,2,2)]= A11*D;                 invA[INDEX3(1,1,q,2,2)]= A11*D;
156              } else {              } else {
157                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
158                 return;                 return;
159              }              }
160           }           }
# Line 171  void Finley_Util_InvertSmallMat(int len, Line 186  void Finley_Util_InvertSmallMat(int len,
186                 invA[INDEX3(1,2,q,3,3)]=(A13*A21-A11*A23)*D;                 invA[INDEX3(1,2,q,3,3)]=(A13*A21-A11*A23)*D;
187                 invA[INDEX3(2,2,q,3,3)]=(A11*A22-A12*A21)*D;                 invA[INDEX3(2,2,q,3,3)]=(A11*A22-A12*A21)*D;
188              } else {              } else {
189                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
190                 return;                 return;
191              }              }
192           }           }
# Line 184  void Finley_Util_InvertSmallMat(int len, Line 198  void Finley_Util_InvertSmallMat(int len,
198    
199  /*    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 */
200    
201  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){
202     int q;     dim_t q;
203     double A11,A12,A13,A21,A22,A23,A31,A32,A33;     register double A11,A12,A13,A21,A22,A23,A31,A32,A33;
204    
205     switch(dim) {     switch(dim) {
206        case 1:        case 1:
# Line 228  void Finley_Util_DetOfSmallMat(int len,i Line 242  void Finley_Util_DetOfSmallMat(int len,i
242  /*    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  */
243  /*    or the vector A(:,0,q) in the case of dim=2                                             */  /*    or the vector A(:,0,q) in the case of dim=2                                             */
244    
245  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) {
246     int q;     dim_t q;
247     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;
248    
249     switch(dim) {     switch(dim) {
250        case 1:        case 1:
# Line 242  void  Finley_NormalVector(int len, int d Line 256  void  Finley_NormalVector(int len, int d
256              A21=A[INDEX3(1,0,q,2,dim1)];              A21=A[INDEX3(1,0,q,2,dim1)];
257              length = sqrt(A11*A11+A21*A21);              length = sqrt(A11*A11+A21*A21);
258              if (! length>0) {              if (! length>0) {
259                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
                sprintf(Finley_ErrorMsg,"area equals zero.");  
260                 return;                 return;
261              } else {              } else {
262                 invlength=1./length;                 invlength=1./length;
# Line 265  void  Finley_NormalVector(int len, int d Line 278  void  Finley_NormalVector(int len, int d
278              CO_A33=A11*A22-A21*A12;              CO_A33=A11*A22-A21*A12;
279              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);
280              if (! length>0) {              if (! length>0) {
281                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
                sprintf(Finley_ErrorMsg,"area equals zero.");  
282                 return;                 return;
283              } else {              } else {
284                 invlength=1./length;                 invlength=1./length;
# Line 285  void  Finley_NormalVector(int len, int d Line 297  void  Finley_NormalVector(int len, int d
297  /*    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 */
298  /*    or the vector A(:,0,q) in the case of dim=2                                                                   */  /*    or the vector A(:,0,q) in the case of dim=2                                                                   */
299    
300  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) {
301     int q;     dim_t q;
302     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;
303    
304     switch(dim) {     switch(dim) {
# Line 323  void  Finley_LengthOfNormalVector(int le Line 335  void  Finley_LengthOfNormalVector(int le
335  /* there is no range checking! */  /* there is no range checking! */
336  /* at output Map[invMap[i]]=i for i=0:lenInvMap */  /* at output Map[invMap[i]]=i for i=0:lenInvMap */
337    
338  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) {
339     int i;     dim_t i;
340     for (i=0;i<lenInvMap;i++) invMap[i]=0;     for (i=0;i<lenInvMap;i++) invMap[i]=0;
341     for (i=0;i<lenMap;i++) {     for (i=0;i<lenMap;i++) {
342        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 354  int Finley_Util_ValueAndIndex_compar(con
354     if (e1->value > e2->value) return  1;     if (e1->value > e2->value) return  1;
355     return 0;     return 0;
356  }  }
357  void Finley_Util_sortValueAndIndex(int n,Finley_Util_ValueAndIndex* array) {  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {
358       /* OMP : needs parallelization !*/       /* OMP : needs parallelization !*/
359       qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);       qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);
360  }  }
# Line 352  void Finley_Util_sortValueAndIndex(int n Line 364  void Finley_Util_sortValueAndIndex(int n
364    
365  /* calculates the minimum value from a dim X N integer array */  /* calculates the minimum value from a dim X N integer array */
366    
367  maybelong Finley_Util_getMinInt(int dim,int N,maybelong* values) {  index_t Finley_Util_getMinInt(dim_t dim,dim_t N,index_t* values) {
368     maybelong i,j,out,out_local;     dim_t i,j;
369     out=MAYBELONG_MAX;     index_t out,out_local;
370       out=INDEX_T_MAX;
371     if (values!=NULL && dim*N>0 ) {     if (values!=NULL && dim*N>0 ) {
372       out=values[0];       out=values[0];
373       #pragma omp parallel private(out_local)       #pragma omp parallel private(out_local)
# Line 373  maybelong Finley_Util_getMinInt(int dim, Line 386  maybelong Finley_Util_getMinInt(int dim,
386                                                                                                                                                                                                                                                                                                        
387  /* calculates the maximum value from a dim X N integer array */  /* calculates the maximum value from a dim X N integer array */
388    
389  maybelong Finley_Util_getMaxInt(int dim,int N,maybelong* values) {  index_t Finley_Util_getMaxInt(dim_t dim,dim_t N,index_t* values) {
390     maybelong i,j,out,out_local;     dim_t i,j;
391     out=-MAYBELONG_MAX;     index_t out,out_local;
392       out=-INDEX_T_MAX;
393     if (values!=NULL && dim*N>0 ) {     if (values!=NULL && dim*N>0 ) {
394       out=values[0];       out=values[0];
395       #pragma omp parallel private(out_local)       #pragma omp parallel private(out_local)
# Line 394  maybelong Finley_Util_getMaxInt(int dim, Line 408  maybelong Finley_Util_getMaxInt(int dim,
408    
409  /* 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. */
410    
411  maybelong Finley_Util_packMask(maybelong N,maybelong* mask,maybelong* index) {  dim_t Finley_Util_packMask(dim_t N,index_t* mask,index_t* index) {
412        maybelong out,k;        dim_t out,k;
413        out=0;        out=0;
414        /*OMP */        /*OMP */
415        for (k=0;k<N;k++) {        for (k=0;k<N;k++) {
# Line 408  maybelong Finley_Util_packMask(maybelong Line 422  maybelong Finley_Util_packMask(maybelong
422  }  }
423    
424  /* returns true if array contains value */  /* returns true if array contains value */
425  int Finley_Util_isAny(maybelong N,maybelong* array,maybelong value) {  bool_t Finley_Util_isAny(dim_t N,index_t* array,index_t value) {
426     int out=FALSE;     bool_t out=FALSE;
427     maybelong i;     dim_t i;
428     #pragma omp parallel for private(i) schedule(static) reduction(||:out)     #pragma omp parallel for private(i) schedule(static) reduction(||:out)
429     for (i=0;i<N;i++) out = out || (array[i]==value);     for (i=0;i<N;i++) out = out || (array[i]==value);
430     return out;     return out;
431  }  }
432  /* calculates the cummultative sum in array and returns the total sum */  /* calculates the cummultative sum in array and returns the total sum */
433  maybelong Finley_Util_cumsum(maybelong N,maybelong* array) {  index_t Finley_Util_cumsum(dim_t N,index_t* array) {
434     maybelong out=0,tmp,i;     index_t out=0,tmp;
435       dim_t i;
436     #ifdef _OPENMP     #ifdef _OPENMP
437        maybelong partial_sums[omp_get_max_threads()],sum;        index_t partial_sums[omp_get_max_threads()],sum;
438        #pragma omp parallel private(sum,i,tmp)        #pragma omp parallel private(sum,i,tmp)
439        {        {
440          sum=0;          sum=0;
# Line 455  maybelong Finley_Util_cumsum(maybelong N Line 470  maybelong Finley_Util_cumsum(maybelong N
470     return out;     return out;
471  }  }
472    
473  void Finley_copyDouble(int n,double* source, double* target) {  void Finley_copyDouble(dim_t n,double* source, double* target) {
474    int i;    dim_t i;
475    for (i=0;i<n;i++) target[i]=source[i];    for (i=0;i<n;i++) target[i]=source[i];
476  }  }
477    
478    /*
479     * Revision 1.8  2005/08/12 01:45:43  jgs
480     * erge of development branch dev-02 back to main trunk on 2005-08-12
481     *
482     * Revision 1.7.2.2  2005/09/07 06:26:22  gross
483     * the solver from finley are put into the standalone package paso now
484     *
485     * Revision 1.7.2.1  2005/08/04 22:41:11  gross
486     * some extra routines for finley that might speed-up RHS assembling in some cases (not actived right now)
487     *
488     * Revision 1.7  2005/07/08 04:07:59  jgs
489     * Merge of development branch back to main trunk on 2005-07-08
490     *
491     * Revision 1.1.1.1.2.4  2005/06/29 02:34:57  gross
492     * some changes towards 64 integers in finley
493     *
494     * Revision 1.1.1.1.2.3  2005/03/02 23:35:06  gross
495     * reimplementation of the ILU in Finley. block size>1 still needs some testing
496     *
497     * Revision 1.1.1.1.2.2  2005/02/18 02:27:31  gross
498     * two function that will be used for a reimplementation of the ILU preconditioner
499     *
500     * Revision 1.1.1.1.2.1  2004/11/12 06:58:19  gross
501     * 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
502     *
503     * Revision 1.1.1.1  2004/10/26 06:53:57  jgs
504     * initial import of project esys2
505     *
506     * Revision 1.3  2004/08/26 12:03:52  gross
507     * Some other bug in Finley_Assemble_gradient fixed.
508     *
509     * Revision 1.2  2004/07/02 04:21:13  gross
510     * Finley C code has been included
511     *
512     * Revision 1.1.1.1  2004/06/24 04:00:40  johng
513     * Initial version of eys using boost-python.
514     *
515     *
516     */

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

  ViewVC Help
Powered by ViewVC 1.1.26