/[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 471 by jgs, Fri Jan 27 01:33:02 2006 UTC
# Line 1  Line 1 
1  /* $Id$ */  /*
2     ******************************************************************************
3     *                                                                            *
4     *       COPYRIGHT  ACcESS 2003,2004,2005 -  All Rights Reserved              *
5     *                                                                            *
6     * This software is the property of ACcESS. No part of this code              *
7     * may be copied in any form or by any means without the expressed written    *
8     * consent of ACcESS.  Copying, use or modification of this software          *
9     * by any unauthorised person is illegal unless that person has a software    *
10     * license agreement with ACcESS.                                             *
11     *                                                                            *
12     ******************************************************************************
13    */
14    
15  /**************************************************************/  /**************************************************************/
16    
# Line 6  Line 18 
18    
19  /**************************************************************/  /**************************************************************/
20    
 /*   Copyrights by ACcESS Australia, 2003 */  
21  /*   author: gross@access.edu.au */  /*   author: gross@access.edu.au */
22  /*   Version: $Id$ */  /*   Version: $Id$ */
23    
24  /**************************************************************/  /**************************************************************/
25    
 #include "Common.h"  
26  #include "Finley.h"  #include "Finley.h"
27  #include "Util.h"  #include "Util.h"
28  #ifdef _OPENMP  #ifdef _OPENMP
# Line 21  Line 31 
31    
32  /**************************************************************/  /**************************************************************/
33    
34    /*   returns true if any of the values in the short array values is not equalt to Zero */
35    
36    bool_t Finley_Util_anyNonZeroDouble(dim_t N, double* values) {
37       dim_t q;
38       for (q=0;q<N;++q) if (ABS(values[q])>0) return TRUE;
39       return FALSE;
40    }
41    /**************************************************************/
42    
43  /*   gathers double values out from in by index: */  /*   gathers double values out from in by index: */
44    
45  /*        out(1:numData,1:len)=in(1:numData,index(1:len)) */  /*        out(1:numData,1:len)=in(1:numData,index(1:len)) */
46    
47  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){
48      int s,i;      dim_t s,i;
49      for (s=0;s<len;s++) {      for (s=0;s<len;s++) {
50         for (i=0;i<numData;i++) {         for (i=0;i<numData;i++) {
51            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 60  void Finley_Util_Gather_double(int len,m
60    
61  /*        out(1:numData,1:len)=in(1:numData,index(1:len)) */  /*        out(1:numData,1:len)=in(1:numData,index(1:len)) */
62    
63  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){
64      int s,i;      dim_t s,i;
65      for (s=0;s<len;s++) {      for (s=0;s<len;s++) {
66         for (i=0;i<numData;i++) {         for (i=0;i<numData;i++) {
67            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 75  void Finley_Util_Gather_int(int len,mayb
75    
76  /*        out(1:numData,index(1:len))+=in(1:numData,1:len) */  /*        out(1:numData,index(1:len))+=in(1:numData,1:len) */
77    
78  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){
79     int i,s;     dim_t i,s;
80     for (s=0;s<len;s++) {     for (s=0;s<len;s++) {
81         for(i=0;i<numData;i++) {         for(i=0;i<numData;i++) {
82            #pragma omp atomic            #pragma omp atomic
# Line 70  void Finley_Util_AddScatter(int len,mayb Line 89  void Finley_Util_AddScatter(int len,mayb
89    
90  /*          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) */
91    
92  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) {
93      int i,j,s;      dim_t i,j,s;
94      for (i=0;i<A1*A2;i++) A[i]=0;      for (i=0;i<A1*A2;i++) A[i]=0;
95         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
96            for (j=0;j<A2;j++) {            for (j=0;j<A2;j++) {
# Line 86  void Finley_Util_SmallMatMult(int A1,int Line 105  void Finley_Util_SmallMatMult(int A1,int
105    
106  /*        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 */
107    
108  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) {
109      int q,i,j,s;      dim_t q,i,j,s;
110      for (i=0;i<A1*A2*len;i++) A[i]=0;      for (i=0;i<A1*A2*len;i++) A[i]=0;
111      for (q=0;q<len;q++) {      for (q=0;q<len;q++) {
112         for (i=0;i<A1;i++) {         for (i=0;i<A1;i++) {
# Line 102  void Finley_Util_SmallMatSetMult(int len Line 121  void Finley_Util_SmallMatSetMult(int len
121  /*    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 */
122  /*    the determinante is returned. */  /*    the determinante is returned. */
123    
124  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){
125     int q;     dim_t q;
126     double D,A11,A12,A13,A21,A22,A23,A31,A32,A33;     register double D,A11,A12,A13,A21,A22,A23,A31,A32,A33;
127    
128     switch(dim) {     switch(dim) {
129        case 1:        case 1:
# Line 115  void Finley_Util_InvertSmallMat(int len, Line 134  void Finley_Util_InvertSmallMat(int len,
134                 D=1./D;                 D=1./D;
135                 invA[q]=D;                 invA[q]=D;
136              } else {              } else {
137                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
138                 return;                 return;
139              }              }
140           }           }
# Line 138  void Finley_Util_InvertSmallMat(int len, Line 156  void Finley_Util_InvertSmallMat(int len,
156                 invA[INDEX3(0,1,q,2,2)]=-A12*D;                 invA[INDEX3(0,1,q,2,2)]=-A12*D;
157                 invA[INDEX3(1,1,q,2,2)]= A11*D;                 invA[INDEX3(1,1,q,2,2)]= A11*D;
158              } else {              } else {
159                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
160                 return;                 return;
161              }              }
162           }           }
# Line 171  void Finley_Util_InvertSmallMat(int len, Line 188  void Finley_Util_InvertSmallMat(int len,
188                 invA[INDEX3(1,2,q,3,3)]=(A13*A21-A11*A23)*D;                 invA[INDEX3(1,2,q,3,3)]=(A13*A21-A11*A23)*D;
189                 invA[INDEX3(2,2,q,3,3)]=(A11*A22-A12*A21)*D;                 invA[INDEX3(2,2,q,3,3)]=(A11*A22-A12*A21)*D;
190              } else {              } else {
191                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
                sprintf(Finley_ErrorMsg,"Non-regular matrix");  
192                 return;                 return;
193              }              }
194           }           }
# Line 184  void Finley_Util_InvertSmallMat(int len, Line 200  void Finley_Util_InvertSmallMat(int len,
200    
201  /*    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 */
202    
203  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){
204     int q;     dim_t q;
205     double A11,A12,A13,A21,A22,A23,A31,A32,A33;     register double A11,A12,A13,A21,A22,A23,A31,A32,A33;
206    
207     switch(dim) {     switch(dim) {
208        case 1:        case 1:
# Line 228  void Finley_Util_DetOfSmallMat(int len,i Line 244  void Finley_Util_DetOfSmallMat(int len,i
244  /*    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  */
245  /*    or the vector A(:,0,q) in the case of dim=2                                             */  /*    or the vector A(:,0,q) in the case of dim=2                                             */
246    
247  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) {
248     int q;     dim_t q;
249     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;
250    
251     switch(dim) {     switch(dim) {
252        case 1:        case 1:
# Line 242  void  Finley_NormalVector(int len, int d Line 258  void  Finley_NormalVector(int len, int d
258              A21=A[INDEX3(1,0,q,2,dim1)];              A21=A[INDEX3(1,0,q,2,dim1)];
259              length = sqrt(A11*A11+A21*A21);              length = sqrt(A11*A11+A21*A21);
260              if (! length>0) {              if (! length>0) {
261                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
                sprintf(Finley_ErrorMsg,"area equals zero.");  
262                 return;                 return;
263              } else {              } else {
264                 invlength=1./length;                 invlength=1./length;
# Line 265  void  Finley_NormalVector(int len, int d Line 280  void  Finley_NormalVector(int len, int d
280              CO_A33=A11*A22-A21*A12;              CO_A33=A11*A22-A21*A12;
281              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);
282              if (! length>0) {              if (! length>0) {
283                 Finley_ErrorCode=ZERO_DIVISION_ERROR;                 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
                sprintf(Finley_ErrorMsg,"area equals zero.");  
284                 return;                 return;
285              } else {              } else {
286                 invlength=1./length;                 invlength=1./length;
# Line 285  void  Finley_NormalVector(int len, int d Line 299  void  Finley_NormalVector(int len, int d
299  /*    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 */
300  /*    or the vector A(:,0,q) in the case of dim=2                                                                   */  /*    or the vector A(:,0,q) in the case of dim=2                                                                   */
301    
302  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) {
303     int q;     dim_t q;
304     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;
305    
306     switch(dim) {     switch(dim) {
# Line 323  void  Finley_LengthOfNormalVector(int le Line 337  void  Finley_LengthOfNormalVector(int le
337  /* there is no range checking! */  /* there is no range checking! */
338  /* at output Map[invMap[i]]=i for i=0:lenInvMap */  /* at output Map[invMap[i]]=i for i=0:lenInvMap */
339    
340  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) {
341     int i;     dim_t i;
342     for (i=0;i<lenInvMap;i++) invMap[i]=0;     for (i=0;i<lenInvMap;i++) invMap[i]=0;
343     for (i=0;i<lenMap;i++) {     for (i=0;i<lenMap;i++) {
344        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 356  int Finley_Util_ValueAndIndex_compar(con
356     if (e1->value > e2->value) return  1;     if (e1->value > e2->value) return  1;
357     return 0;     return 0;
358  }  }
359  void Finley_Util_sortValueAndIndex(int n,Finley_Util_ValueAndIndex* array) {  void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {
360       /* OMP : needs parallelization !*/       /* OMP : needs parallelization !*/
361       qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);       qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);
362  }  }
# Line 352  void Finley_Util_sortValueAndIndex(int n Line 366  void Finley_Util_sortValueAndIndex(int n
366    
367  /* calculates the minimum value from a dim X N integer array */  /* calculates the minimum value from a dim X N integer array */
368    
369  maybelong Finley_Util_getMinInt(int dim,int N,maybelong* values) {  index_t Finley_Util_getMinInt(dim_t dim,dim_t N,index_t* values) {
370     maybelong i,j,out,out_local;     dim_t i,j;
371     out=MAYBELONG_MAX;     index_t out,out_local;
372       out=INDEX_T_MAX;
373     if (values!=NULL && dim*N>0 ) {     if (values!=NULL && dim*N>0 ) {
374       out=values[0];       out=values[0];
375       #pragma omp parallel private(out_local)       #pragma omp parallel private(out_local)
# Line 373  maybelong Finley_Util_getMinInt(int dim, Line 388  maybelong Finley_Util_getMinInt(int dim,
388                                                                                                                                                                                                                                                                                                        
389  /* calculates the maximum value from a dim X N integer array */  /* calculates the maximum value from a dim X N integer array */
390    
391  maybelong Finley_Util_getMaxInt(int dim,int N,maybelong* values) {  index_t Finley_Util_getMaxInt(dim_t dim,dim_t N,index_t* values) {
392     maybelong i,j,out,out_local;     dim_t i,j;
393     out=-MAYBELONG_MAX;     index_t out,out_local;
394       out=-INDEX_T_MAX;
395     if (values!=NULL && dim*N>0 ) {     if (values!=NULL && dim*N>0 ) {
396       out=values[0];       out=values[0];
397       #pragma omp parallel private(out_local)       #pragma omp parallel private(out_local)
# Line 394  maybelong Finley_Util_getMaxInt(int dim, Line 410  maybelong Finley_Util_getMaxInt(int dim,
410    
411  /* 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. */
412    
413  maybelong Finley_Util_packMask(maybelong N,maybelong* mask,maybelong* index) {  dim_t Finley_Util_packMask(dim_t N,index_t* mask,index_t* index) {
414        maybelong out,k;        dim_t out,k;
415        out=0;        out=0;
416        /*OMP */        /*OMP */
417        for (k=0;k<N;k++) {        for (k=0;k<N;k++) {
# Line 408  maybelong Finley_Util_packMask(maybelong Line 424  maybelong Finley_Util_packMask(maybelong
424  }  }
425    
426  /* returns true if array contains value */  /* returns true if array contains value */
427  int Finley_Util_isAny(maybelong N,maybelong* array,maybelong value) {  bool_t Finley_Util_isAny(dim_t N,index_t* array,index_t value) {
428     int out=FALSE;     bool_t out=FALSE;
429     maybelong i;     dim_t i;
430     #pragma omp parallel for private(i) schedule(static) reduction(||:out)     #pragma omp parallel for private(i) schedule(static) reduction(||:out)
431     for (i=0;i<N;i++) out = out || (array[i]==value);     for (i=0;i<N;i++) out = out || (array[i]==value);
432     return out;     return out;
433  }  }
434  /* calculates the cummultative sum in array and returns the total sum */  /* calculates the cummultative sum in array and returns the total sum */
435  maybelong Finley_Util_cumsum(maybelong N,maybelong* array) {  index_t Finley_Util_cumsum(dim_t N,index_t* array) {
436     maybelong out=0,tmp,i;     index_t out=0,tmp;
437       dim_t i;
438     #ifdef _OPENMP     #ifdef _OPENMP
439        maybelong partial_sums[omp_get_max_threads()],sum;        index_t partial_sums[omp_get_max_threads()],sum;
440        #pragma omp parallel private(sum,i,tmp)        #pragma omp parallel private(sum,i,tmp)
441        {        {
442          sum=0;          sum=0;
# Line 455  maybelong Finley_Util_cumsum(maybelong N Line 472  maybelong Finley_Util_cumsum(maybelong N
472     return out;     return out;
473  }  }
474    
475  void Finley_copyDouble(int n,double* source, double* target) {  void Finley_copyDouble(dim_t n,double* source, double* target) {
476    int i;    dim_t i;
477    for (i=0;i<n;i++) target[i]=source[i];    for (i=0;i<n;i++) target[i]=source[i];
478  }  }
479    
480    /*
481     * Revision 1.8  2005/08/12 01:45:43  jgs
482     * erge of development branch dev-02 back to main trunk on 2005-08-12
483     *
484     * Revision 1.7.2.2  2005/09/07 06:26:22  gross
485     * the solver from finley are put into the standalone package paso now
486     *
487     * Revision 1.7.2.1  2005/08/04 22:41:11  gross
488     * some extra routines for finley that might speed-up RHS assembling in some cases (not actived right now)
489     *
490     * Revision 1.7  2005/07/08 04:07:59  jgs
491     * Merge of development branch back to main trunk on 2005-07-08
492     *
493     * Revision 1.1.1.1.2.4  2005/06/29 02:34:57  gross
494     * some changes towards 64 integers in finley
495     *
496     * Revision 1.1.1.1.2.3  2005/03/02 23:35:06  gross
497     * reimplementation of the ILU in Finley. block size>1 still needs some testing
498     *
499     * Revision 1.1.1.1.2.2  2005/02/18 02:27:31  gross
500     * two function that will be used for a reimplementation of the ILU preconditioner
501     *
502     * Revision 1.1.1.1.2.1  2004/11/12 06:58:19  gross
503     * 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
504     *
505     * Revision 1.1.1.1  2004/10/26 06:53:57  jgs
506     * initial import of project esys2
507     *
508     * Revision 1.3  2004/08/26 12:03:52  gross
509     * Some other bug in Finley_Assemble_gradient fixed.
510     *
511     * Revision 1.2  2004/07/02 04:21:13  gross
512     * Finley C code has been included
513     *
514     * Revision 1.1.1.1  2004/06/24 04:00:40  johng
515     * Initial version of eys using boost-python.
516     *
517     *
518     */

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

  ViewVC Help
Powered by ViewVC 1.1.26