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

Annotation of /trunk/finley/src/Util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 757 - (hide annotations)
Mon Jun 26 13:12:56 2006 UTC (13 years, 9 months ago) by woo409
File MIME type: text/plain
File size: 17649 byte(s)
+ Merge of intelc_win32 branch (revision 741:755) with trunk. Tested on iVEC altix (run_tests and py_tests all pass)

1 jgs 150 /*
2 elspeth 616 ************************************************************
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 jgs 150 */
12 jgs 82
13     /**************************************************************/
14    
15     /* Some utility routines: */
16    
17     /**************************************************************/
18    
19     /* author: gross@access.edu.au */
20     /* Version: $Id$ */
21    
22     /**************************************************************/
23    
24     #include "Finley.h"
25     #include "Util.h"
26 woo409 757
27 jgs 113 #ifdef _OPENMP
28     #include <omp.h>
29     #endif
30 jgs 82
31     /**************************************************************/
32    
33 jgs 147 /* returns true if any of the values in the short array values is not equalt to Zero */
34    
35     bool_t Finley_Util_anyNonZeroDouble(dim_t N, double* values) {
36     dim_t q;
37     for (q=0;q<N;++q) if (ABS(values[q])>0) return TRUE;
38     return FALSE;
39     }
40     /**************************************************************/
41    
42 jgs 82 /* gathers double values out from in by index: */
43    
44     /* out(1:numData,1:len)=in(1:numData,index(1:len)) */
45    
46 jgs 123 void Finley_Util_Gather_double(dim_t len,index_t* index,dim_t numData,double* in, double * out){
47     dim_t s,i;
48 jgs 82 for (s=0;s<len;s++) {
49     for (i=0;i<numData;i++) {
50     out[INDEX2(i,s,numData)]=in[INDEX2(i,index[s],numData)];
51     }
52     }
53     }
54    
55     /**************************************************************/
56    
57    
58     /* gathers maybelong values out from in by index: */
59    
60     /* out(1:numData,1:len)=in(1:numData,index(1:len)) */
61    
62 jgs 123 void Finley_Util_Gather_int(dim_t len,index_t* index,dim_t numData, index_t* in, index_t * out){
63     dim_t s,i;
64 jgs 82 for (s=0;s<len;s++) {
65     for (i=0;i<numData;i++) {
66     out[INDEX2(i,s,numData)]=in[INDEX2(i,index[s],numData)];
67     }
68     }
69     }
70    
71     /**************************************************************/
72    
73     /* adds a vector in into out using and index. */
74    
75     /* out(1:numData,index(1:len))+=in(1:numData,1:len) */
76    
77 jgs 123 void Finley_Util_AddScatter(dim_t len,index_t* index,dim_t numData,double* in,double * out){
78     dim_t i,s;
79 jgs 82 for (s=0;s<len;s++) {
80     for(i=0;i<numData;i++) {
81     #pragma omp atomic
82     out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];
83     }
84     }
85     }
86    
87 bcumming 751 #ifdef PASO_MPI
88     /* same as AddScatter(), but checks that value index[] is below an upper bound upperBound before
89     addition. This is used to ensure that only the influence of local DOF is added */
90     /* out(1:numData,index[p])+=in(1:numData,p)
91     where p = {k=1...len , index[k]<upperBound}*/
92     void Finley_Util_AddScatter_upperBound(dim_t len,index_t* index,dim_t numData,double* in,double * out, index_t upperBound){
93     dim_t i,s;
94     for (s=0;s<len;s++) {
95     for(i=0;i<numData;i++) {
96     //#pragma omp atomic
97     if( index[s]<upperBound )
98     out[INDEX2(i,index[s],numData)]+=in[INDEX2(i,s,numData)];
99     }
100     }
101     }
102    
103    
104     #endif
105    
106 jgs 82 /* multiplies two matrices */
107    
108     /* A(1:A1,1:A2)=B(1:A1,1:B2)*C(1:B2,1:A2) */
109    
110 jgs 123 void Finley_Util_SmallMatMult(dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
111     dim_t i,j,s;
112 jgs 82 for (i=0;i<A1*A2;i++) A[i]=0;
113     for (i=0;i<A1;i++) {
114     for (j=0;j<A2;j++) {
115     for (s=0;s<B2;s++) {
116     A[INDEX2(i,j,A1)]+=B[INDEX2(i,s,A1)]*C[INDEX2(s,j,B2)];
117     }
118     }
119     }
120     }
121    
122     /* multiplies a two sets of matries: */
123    
124     /* A(1:A1,1:A2,i)=B(1:A1,1:B2,i)*C(1:B2,1:A2,i) i=1,len */
125    
126 jgs 123 void Finley_Util_SmallMatSetMult(dim_t len,dim_t A1,dim_t A2, double* A, dim_t B2, double*B, double* C) {
127     dim_t q,i,j,s;
128 jgs 82 for (i=0;i<A1*A2*len;i++) A[i]=0;
129     for (q=0;q<len;q++) {
130     for (i=0;i<A1;i++) {
131     for (j=0;j<A2;j++) {
132     for (s=0;s<B2;s++) {
133     A[INDEX3(i,j,q,A1,A2)]+=B[INDEX3(i,s,q,A1,B2)]*C[INDEX3(s,j,q,B2,A2)];
134     }
135     }
136     }
137     }
138     }
139     /* inverts the set of dim x dim matrices A(:,:,1:len) with dim=1,2,3 */
140     /* the determinante is returned. */
141    
142 jgs 123 void Finley_Util_InvertSmallMat(dim_t len,dim_t dim,double* A,double *invA, double* det){
143     dim_t q;
144     register double D,A11,A12,A13,A21,A22,A23,A31,A32,A33;
145 jgs 82
146     switch(dim) {
147     case 1:
148     for (q=0;q<len;q++) {
149 jgs 115 D=A[q];
150 jgs 102 if (ABS(D) > 0 ){
151     det[q]=D;
152     D=1./D;
153 jgs 115 invA[q]=D;
154 jgs 102 } else {
155 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
156 jgs 82 return;
157     }
158     }
159     break;
160    
161     case 2:
162     for (q=0;q<len;q++) {
163 jgs 115 A11=A[INDEX3(0,0,q,2,2)];
164     A12=A[INDEX3(0,1,q,2,2)];
165     A21=A[INDEX3(1,0,q,2,2)];
166     A22=A[INDEX3(1,1,q,2,2)];
167 jgs 82
168     D = A11*A22-A12*A21;
169 jgs 102 if (ABS(D) > 0 ){
170     det[q]=D;
171     D=1./D;
172 jgs 115 invA[INDEX3(0,0,q,2,2)]= A22*D;
173     invA[INDEX3(1,0,q,2,2)]=-A21*D;
174     invA[INDEX3(0,1,q,2,2)]=-A12*D;
175     invA[INDEX3(1,1,q,2,2)]= A11*D;
176 jgs 102 } else {
177 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
178 jgs 82 return;
179     }
180     }
181     break;
182    
183     case 3:
184     for (q=0;q<len;q++) {
185 jgs 115 A11=A[INDEX3(0,0,q,3,3)];
186     A21=A[INDEX3(1,0,q,3,3)];
187     A31=A[INDEX3(2,0,q,3,3)];
188     A12=A[INDEX3(0,1,q,3,3)];
189     A22=A[INDEX3(1,1,q,3,3)];
190     A32=A[INDEX3(2,1,q,3,3)];
191     A13=A[INDEX3(0,2,q,3,3)];
192     A23=A[INDEX3(1,2,q,3,3)];
193     A33=A[INDEX3(2,2,q,3,3)];
194 jgs 82
195     D = A11*(A22*A33-A23*A32)+ A12*(A31*A23-A21*A33)+A13*(A21*A32-A31*A22);
196 jgs 102 if (ABS(D) > 0 ){
197     det[q] =D;
198     D=1./D;
199 jgs 115 invA[INDEX3(0,0,q,3,3)]=(A22*A33-A23*A32)*D;
200     invA[INDEX3(1,0,q,3,3)]=(A31*A23-A21*A33)*D;
201     invA[INDEX3(2,0,q,3,3)]=(A21*A32-A31*A22)*D;
202     invA[INDEX3(0,1,q,3,3)]=(A13*A32-A12*A33)*D;
203     invA[INDEX3(1,1,q,3,3)]=(A11*A33-A31*A13)*D;
204     invA[INDEX3(2,1,q,3,3)]=(A12*A31-A11*A32)*D;
205     invA[INDEX3(0,2,q,3,3)]=(A12*A23-A13*A22)*D;
206     invA[INDEX3(1,2,q,3,3)]=(A13*A21-A11*A23)*D;
207     invA[INDEX3(2,2,q,3,3)]=(A11*A22-A12*A21)*D;
208 jgs 102 } else {
209 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: Non-regular matrix");
210 jgs 82 return;
211     }
212     }
213     break;
214    
215     }
216     return;
217     }
218    
219     /* sets the derterminate of a set of dim x dim matrices A(:,:,1:len) with dim=1,2,3 */
220    
221 jgs 123 void Finley_Util_DetOfSmallMat(dim_t len,dim_t dim,double* A, double* det){
222     dim_t q;
223     register double A11,A12,A13,A21,A22,A23,A31,A32,A33;
224 jgs 82
225     switch(dim) {
226     case 1:
227     for (q=0;q<len;q++) {
228 jgs 115 det[q]=A[q];
229 jgs 82 }
230     break;
231    
232     case 2:
233     for (q=0;q<len;q++) {
234 jgs 115 A11=A[INDEX3(0,0,q,2,2)];
235     A12=A[INDEX3(0,1,q,2,2)];
236     A21=A[INDEX3(1,0,q,2,2)];
237     A22=A[INDEX3(1,1,q,2,2)];
238 jgs 82
239     det[q] = A11*A22-A12*A21;
240     }
241     break;
242    
243     case 3:
244     for (q=0;q<len;q++) {
245 jgs 115 A11=A[INDEX3(0,0,q,3,3)];
246     A21=A[INDEX3(1,0,q,3,3)];
247     A31=A[INDEX3(2,0,q,3,3)];
248     A12=A[INDEX3(0,1,q,3,3)];
249     A22=A[INDEX3(1,1,q,3,3)];
250     A32=A[INDEX3(2,1,q,3,3)];
251     A13=A[INDEX3(0,2,q,3,3)];
252     A23=A[INDEX3(1,2,q,3,3)];
253     A33=A[INDEX3(2,2,q,3,3)];
254 jgs 82
255     det[q] = A11*(A22*A33-A23*A32)+ A12*(A31*A23-A21*A33)+A13*(A21*A32-A31*A22);
256     }
257     break;
258    
259     }
260     return;
261     }
262     /* returns the normalized vector Normal[dim,len] orthogonal to A(:,0,q) and A(:,1,q) in the case of dim=3 */
263     /* or the vector A(:,0,q) in the case of dim=2 */
264    
265 jgs 123 void Finley_NormalVector(dim_t len, dim_t dim, dim_t dim1, double* A,double* Normal) {
266     dim_t q;
267     register double A11,A12,CO_A13,A21,A22,CO_A23,A31,A32,CO_A33,length,invlength;
268 jgs 82
269     switch(dim) {
270     case 1:
271 jgs 115 for (q=0;q<len;q++) Normal[q] =1;
272 jgs 82 break;
273     case 2:
274     for (q=0;q<len;q++) {
275 jgs 115 A11=A[INDEX3(0,0,q,2,dim1)];
276     A21=A[INDEX3(1,0,q,2,dim1)];
277 jgs 82 length = sqrt(A11*A11+A21*A21);
278     if (! length>0) {
279 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
280 jgs 82 return;
281     } else {
282     invlength=1./length;
283 jgs 115 Normal[INDEX2(0,q,2)]=A21*invlength;
284     Normal[INDEX2(1,q,2)]=-A11*invlength;
285 jgs 82 }
286     }
287     break;
288     case 3:
289     for (q=0;q<len;q++) {
290 jgs 115 A11=A[INDEX3(0,0,q,3,dim1)];
291     A21=A[INDEX3(1,0,q,3,dim1)];
292     A31=A[INDEX3(2,0,q,3,dim1)];
293     A12=A[INDEX3(0,1,q,3,dim1)];
294     A22=A[INDEX3(1,1,q,3,dim1)];
295     A32=A[INDEX3(2,1,q,3,dim1)];
296 jgs 82 CO_A13=A21*A32-A31*A22;
297     CO_A23=A31*A12-A11*A32;
298     CO_A33=A11*A22-A21*A12;
299     length=sqrt(CO_A13*CO_A13+CO_A23*CO_A23+CO_A33*CO_A33);
300     if (! length>0) {
301 jgs 150 Finley_setError(ZERO_DIVISION_ERROR,"__FILE__: area equals zero.");
302 jgs 82 return;
303     } else {
304     invlength=1./length;
305 jgs 115 Normal[INDEX2(0,q,3)]=CO_A13*invlength;
306     Normal[INDEX2(1,q,3)]=CO_A23*invlength;
307     Normal[INDEX2(2,q,3)]=CO_A33*invlength;
308 jgs 82 }
309    
310     }
311     break;
312    
313     }
314     return;
315     }
316    
317     /* 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 */
318     /* or the vector A(:,0,q) in the case of dim=2 */
319    
320 jgs 123 void Finley_LengthOfNormalVector(dim_t len, dim_t dim, dim_t dim1, double* A,double* length) {
321     dim_t q;
322 jgs 82 double A11,A12,CO_A13,A21,A22,CO_A23,A31,A32,CO_A33;
323    
324     switch(dim) {
325     case 1:
326 jgs 115 for (q=0;q<len;q++) length[q] =1;
327 jgs 82 break;
328     case 2:
329     for (q=0;q<len;q++) {
330 jgs 115 A11=A[INDEX3(0,0,q,2,dim1)];
331     A21=A[INDEX3(1,0,q,2,dim1)];
332 jgs 82 length[q] = sqrt(A11*A11+A21*A21);
333     }
334     break;
335     case 3:
336     for (q=0;q<len;q++) {
337 jgs 115 A11=A[INDEX3(0,0,q,3,dim1)];
338     A21=A[INDEX3(1,0,q,3,dim1)];
339     A31=A[INDEX3(2,0,q,3,dim1)];
340     A12=A[INDEX3(0,1,q,3,dim1)];
341     A22=A[INDEX3(1,1,q,3,dim1)];
342     A32=A[INDEX3(2,1,q,3,dim1)];
343 jgs 82 CO_A13=A21*A32-A31*A22;
344     CO_A23=A31*A12-A11*A32;
345     CO_A33=A11*A22-A21*A12;
346     length[q]=sqrt(CO_A13*CO_A13+CO_A23*CO_A23+CO_A33*CO_A33);
347     }
348     break;
349    
350     }
351     return;
352     }
353    
354     /* inverts the map map of length len */
355     /* there is no range checking! */
356     /* at output Map[invMap[i]]=i for i=0:lenInvMap */
357    
358 jgs 123 void Finley_Util_InvertMap(dim_t lenInvMap, index_t* invMap,dim_t lenMap, index_t* Map) {
359     dim_t i;
360 jgs 82 for (i=0;i<lenInvMap;i++) invMap[i]=0;
361     for (i=0;i<lenMap;i++) {
362     if (Map[i]>=0) invMap[Map[i]]=i;
363     }
364     }
365    
366     /* orders a Finley_Util_ValueAndIndex array by value */
367     /* it is assumed that n is large */
368    
369     int Finley_Util_ValueAndIndex_compar(const void *arg1 , const void *arg2 ) {
370     Finley_Util_ValueAndIndex *e1,*e2;
371     e1=(Finley_Util_ValueAndIndex*) arg1;
372     e2=(Finley_Util_ValueAndIndex*) arg2;
373     if (e1->value < e2->value) return -1;
374     if (e1->value > e2->value) return 1;
375     return 0;
376     }
377 woo409 757
378 jgs 123 void Finley_Util_sortValueAndIndex(dim_t n,Finley_Util_ValueAndIndex* array) {
379 jgs 82 /* OMP : needs parallelization !*/
380     qsort(array,n,sizeof(Finley_Util_ValueAndIndex),Finley_Util_ValueAndIndex_compar);
381     }
382    
383    
384     /**************************************************************/
385    
386     /* calculates the minimum value from a dim X N integer array */
387    
388 jgs 123 index_t Finley_Util_getMinInt(dim_t dim,dim_t N,index_t* values) {
389     dim_t i,j;
390     index_t out,out_local;
391     out=INDEX_T_MAX;
392 jgs 82 if (values!=NULL && dim*N>0 ) {
393     out=values[0];
394 jgs 115 #pragma omp parallel private(out_local)
395     {
396     out_local=out;
397     #pragma omp for private(i,j) schedule(static)
398     for (j=0;j<N;j++) {
399     for (i=0;i<dim;i++) out_local=MIN(out_local,values[INDEX2(i,j,dim)]);
400     }
401     #pragma omp critical
402     out=MIN(out_local,out);
403 jgs 82 }
404     }
405     return out;
406     }
407    
408     /* calculates the maximum value from a dim X N integer array */
409    
410 jgs 123 index_t Finley_Util_getMaxInt(dim_t dim,dim_t N,index_t* values) {
411     dim_t i,j;
412     index_t out,out_local;
413     out=-INDEX_T_MAX;
414 jgs 82 if (values!=NULL && dim*N>0 ) {
415     out=values[0];
416 jgs 115 #pragma omp parallel private(out_local)
417     {
418     out_local=out;
419     #pragma omp for private(i,j) schedule(static)
420     for (j=0;j<N;j++) {
421     for (i=0;i<dim;i++) out_local=MAX(out_local,values[INDEX2(i,j,dim)]);
422     }
423     #pragma omp critical
424     out=MAX(out_local,out);
425     }
426 jgs 82 }
427     return out;
428     }
429    
430     /* set the index of the positive entries in mask. The length of index is returned. */
431    
432 jgs 123 dim_t Finley_Util_packMask(dim_t N,index_t* mask,index_t* index) {
433     dim_t out,k;
434 jgs 82 out=0;
435     /*OMP */
436     for (k=0;k<N;k++) {
437     if (mask[k]>=0) {
438     index[out]=k;
439     out++;
440     }
441     }
442     return out;
443     }
444    
445     /* returns true if array contains value */
446 jgs 123 bool_t Finley_Util_isAny(dim_t N,index_t* array,index_t value) {
447     bool_t out=FALSE;
448     dim_t i;
449 jgs 82 #pragma omp parallel for private(i) schedule(static) reduction(||:out)
450 jgs 115 for (i=0;i<N;i++) out = out || (array[i]==value);
451 jgs 82 return out;
452     }
453 jgs 113 /* calculates the cummultative sum in array and returns the total sum */
454 jgs 123 index_t Finley_Util_cumsum(dim_t N,index_t* array) {
455     index_t out=0,tmp;
456     dim_t i;
457 jgs 113 #ifdef _OPENMP
458 jgs 123 index_t partial_sums[omp_get_max_threads()],sum;
459 jgs 113 #pragma omp parallel private(sum,i,tmp)
460     {
461     sum=0;
462 jgs 115 #pragma omp for schedule(static)
463     for (i=0;i<N;++i) sum+=array[i];
464 jgs 113 partial_sums[omp_get_thread_num()]=sum;
465 jgs 115 #pragma omp barrier
466 jgs 113 #pragma omp master
467     {
468     out=0;
469     for (i=0;i<omp_get_max_threads();++i) {
470     tmp=out;
471     out+=partial_sums[i];
472     partial_sums[i]=tmp;
473     }
474     }
475 jgs 115 #pragma omp barrier
476 jgs 113 sum=partial_sums[omp_get_thread_num()];
477 jgs 115 #pragma omp for schedule(static)
478     for (i=0;i<N;++i) {
479     tmp=sum;
480     sum+=array[i];
481     array[i]=tmp;
482     }
483 jgs 113 }
484     #else
485     for (i=0;i<N;++i) {
486     tmp=out;
487     out+=array[i];
488     array[i]=tmp;
489     }
490     #endif
491     return out;
492     }
493 jgs 82
494 jgs 123 void Finley_copyDouble(dim_t n,double* source, double* target) {
495     dim_t i;
496 jgs 82 for (i=0;i<n;i++) target[i]=source[i];
497     }
498 jgs 123
499 bcumming 751 #ifdef PASO_MPI
500     void Finley_printDoubleArray( FILE *fid, dim_t n, double *array, char *name )
501     {
502     index_t i;
503    
504     if( name )
505     fprintf( fid, "%s [ ", name );
506     else
507     fprintf( fid, "[ " );
508     for( i=0; i<(n<30 ? n : 30); i++ )
509     fprintf( fid, "%g ", array[i] );
510     if( n>=30 )
511     fprintf( fid, "... " );
512     fprintf( fid, "]\n" );
513     }
514     void Finley_printIntArray( FILE *fid, dim_t n, int *array, char *name )
515     {
516     index_t i;
517    
518     if( name )
519     fprintf( fid, "%s [ ", name );
520     else
521     fprintf( fid, "[ " );
522     for( i=0; i<(n<30 ? n : 30); i++ )
523     fprintf( fid, "%d ", array[i] );
524     if( n>=30 )
525     fprintf( fid, "... " );
526     fprintf( fid, "]\n" );
527     }
528     void Finley_printMaskArray( FILE *fid, dim_t n, int *array, char *name )
529     {
530     index_t i;
531    
532     if( name )
533     fprintf( fid, "%s [ ", name );
534     else
535     fprintf( fid, "[ " );
536     for( i=0; i<(n<30 ? n : 30); i++ )
537     if( array[i]!=-1 )
538     fprintf( fid, "%d ", array[i] );
539     else
540     fprintf( fid, "* " );
541     if( n>=30 )
542     fprintf( fid, "... " );
543     fprintf( fid, "]\n" );
544     }
545     #endif
546    
547 jgs 123 /*
548 jgs 147 * Revision 1.8 2005/08/12 01:45:43 jgs
549     *
550 jgs 150 * Revision 1.7.2.2 2005/09/07 06:26:22 gross
551     * the solver from finley are put into the standalone package paso now
552     *
553 jgs 147 * Revision 1.7.2.1 2005/08/04 22:41:11 gross
554     * some extra routines for finley that might speed-up RHS assembling in some cases (not actived right now)
555     *
556 jgs 123 * Revision 1.7 2005/07/08 04:07:59 jgs
557     * Merge of development branch back to main trunk on 2005-07-08
558     *
559     * Revision 1.1.1.1.2.4 2005/06/29 02:34:57 gross
560     * some changes towards 64 integers in finley
561     *
562     * Revision 1.1.1.1.2.3 2005/03/02 23:35:06 gross
563     * reimplementation of the ILU in Finley. block size>1 still needs some testing
564     *
565     * Revision 1.1.1.1.2.2 2005/02/18 02:27:31 gross
566     * two function that will be used for a reimplementation of the ILU preconditioner
567     *
568     * Revision 1.1.1.1.2.1 2004/11/12 06:58:19 gross
569     * 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
570     *
571     * Revision 1.1.1.1 2004/10/26 06:53:57 jgs
572     * initial import of project esys2
573     *
574     * Revision 1.3 2004/08/26 12:03:52 gross
575     * Some other bug in Finley_Assemble_gradient fixed.
576     *
577     * Revision 1.2 2004/07/02 04:21:13 gross
578     * Finley C code has been included
579     *
580     * Revision 1.1.1.1 2004/06/24 04:00:40 johng
581     * Initial version of eys using boost-python.
582     *
583     *
584     */

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26