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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 147 - (hide annotations)
Fri Aug 12 01:45:47 2005 UTC (14 years, 6 months ago) by jgs
File MIME type: text/plain
File size: 15562 byte(s)
erge of development branch dev-02 back to main trunk on 2005-08-12

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26