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

Annotation of /branches/domexper/dudley/src/Mesh_findMatchingFaces.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3149 - (hide annotations)
Fri Sep 3 03:49:32 2010 UTC (8 years, 10 months ago) by jfenwick
File MIME type: text/plain
File size: 8483 byte(s)


1 jgs 150
2 ksteube 1312 /*******************************************************
3 ksteube 1811 *
4 jfenwick 2881 * Copyright (c) 2003-2010 by University of Queensland
5 ksteube 1811 * Earth Systems Science Computational Center (ESSCC)
6     * http://www.uq.edu.au/esscc
7     *
8     * Primary Business: Queensland, Australia
9     * Licensed under the Open Software License version 3.0
10     * http://www.opensource.org/licenses/osl-3.0.php
11     *
12     *******************************************************/
13 ksteube 1312
14 ksteube 1811
15 jgs 82 /**************************************************************/
16    
17 jfenwick 3086 /* Dudley: Mesh */
18 jgs 82
19     /* searches for faces in the mesh which are matching */
20    
21     /**************************************************************/
22    
23     #include "Util.h"
24     #include "Mesh.h"
25 gross 2748
26 jgs 82 /**************************************************************/
27    
28 jfenwick 3086 static double Dudley_Mesh_lockingGridSize=0;
29 jgs 82
30 jfenwick 3086 int Dudley_Mesh_findMatchingFaces_compar(const void *arg1 , const void *arg2 ) {
31     Dudley_Mesh_findMatchingFaces_center *e1,*e2;
32 jgs 123 bool_t l,g;
33     dim_t i;
34 jfenwick 3086 e1=(Dudley_Mesh_findMatchingFaces_center*) arg1;
35     e2=(Dudley_Mesh_findMatchingFaces_center*) arg2;
36 jgs 82 for (i=0;i<MAX_numDim;i++) {
37 jfenwick 3086 l= (e1->x[i]<e2->x[i]+Dudley_Mesh_lockingGridSize) ? TRUE : FALSE;
38     g= (e2->x[i]<e1->x[i]+Dudley_Mesh_lockingGridSize) ? TRUE : FALSE;
39 jgs 82 if (! (l && g)) {
40     if (l) return -1;
41     if (g) return 1;
42     }
43     }
44 gross 888 if ( e1->refId < e2->refId ) {
45     return -1;
46     } else if ( e1->refId > e2->refId ) {
47 gross 1134 return 1;
48 gross 888 } else {
49     return 0;
50     }
51 jgs 82 }
52    
53 jfenwick 3086 void Dudley_Mesh_findMatchingFaces(Dudley_NodeFile *nodes, Dudley_ElementFile *faces, double safety_factor,double tolerance,
54 jgs 123 dim_t* numPairs, index_t* elem0,index_t* elem1,index_t* matching_nodes_in_elem1) {
55 jgs 82 #define getDist(_dist_,_e0_,_i0_,_e1_,_i1_) \
56 jgs 123 {dim_t i; \
57 jgs 82 _dist_=0; \
58     for (i=0;i<numDim;i++) _dist_=MAX(_dist_,ABS(X[INDEX3(i,_i0_,_e0_,numDim,NN)]-X[INDEX3(i,_i1_,_e1_,numDim,NN)])); \
59     }
60 jfenwick 3086 Dudley_ReferenceElement* refElement=NULL;
61 jgs 150 char error_msg[LenErrorMsg_MAX];
62 gross 2748 double h=DBLE(HUGE_VAL),h_local,dist,*X=NULL;
63 jfenwick 3086 Dudley_Mesh_findMatchingFaces_center *center;
64 jfenwick 3149 index_t e_0,e_1,*a1=NULL,*a2=NULL,*perm=NULL,*perm_tmp=NULL,*itmp_ptr=NULL, *shiftNodes=NULL, *reverseNodes=NULL ;
65 gross 2748 dim_t e,i,i0,i1,n,NN,numNodesOnFace;
66    
67     dim_t numDim=nodes->numDim;
68 jgs 82
69 jfenwick 3086 refElement= Dudley_ReferenceElementSet_borrowReferenceElement(faces->referenceElementSet, FALSE);
70 gross 2748 NN=faces->numNodes;
71    
72     numNodesOnFace=refElement->Type->numNodesOnFace;
73     shiftNodes=refElement->Type->shiftNodes;
74     reverseNodes=refElement->Type->reverseNodes;
75    
76     if (numNodesOnFace<=0) {
77 jfenwick 3086 sprintf(error_msg,"Dudley_Mesh_findMatchingFaces: matching faces cannot be applied to face elements of type %s",refElement->Type->Name);
78     Dudley_setError(TYPE_ERROR,error_msg);
79 gross 2748 return;
80     }
81 jgs 102 X=TMPMEMALLOC(NN*numDim*faces->numElements,double);
82 jfenwick 3086 center=TMPMEMALLOC(faces->numElements,Dudley_Mesh_findMatchingFaces_center);
83 gross 1028 a1=TMPMEMALLOC(NN,int);
84     a2=TMPMEMALLOC(NN,int);
85 jfenwick 3086 if (!(Dudley_checkPtr(X) || Dudley_checkPtr(center) || Dudley_checkPtr(a1) || Dudley_checkPtr(a2)) ) {
86 jgs 82 /* OMP */
87     for (e=0;e<faces->numElements;e++) {
88     /* get the coordinates of the nodes */
89 jfenwick 3086 Dudley_Util_Gather_double(NN,&(faces->Nodes[INDEX2(0,e,NN)]),numDim,nodes->Coordinates,&(X[INDEX3(0,0,e,numDim,NN)]));
90 jgs 82 /* get the element center */
91     center[e].refId=e;
92     for (i=0;i<MAX_numDim;i++) center[e].x[i]=0;
93 gross 2748 for (i0=0;i0<numNodesOnFace;i0++) {
94 jfenwick 3149 for (i=0;i<numDim;i++) center[e].x[i]+=X[INDEX3(i,i0,e,numDim,NN)];
95 jgs 82 }
96 gross 2748 for (i=0;i<numDim;i++) center[e].x[i]/=numNodesOnFace;
97 jgs 82 /* get the minimum distance between nodes in the element */
98 gross 2748 for (i0=0;i0<numNodesOnFace;i0++) {
99     for (i1=i0+1;i1<numNodesOnFace;i1++) {
100 jfenwick 3149 getDist(h_local,e,i0,e,i1);
101 jgs 82 h=MIN(h,h_local);
102     }
103     }
104     }
105     /* set the */
106 jfenwick 3086 Dudley_Mesh_lockingGridSize=h*MAX(safety_factor,0);
107     #ifdef Dudley_TRACE
108     printf("locking grid size is %e\n",Dudley_Mesh_lockingGridSize);
109 gross 888 printf("absolute tolerance is %e.\n",h * tolerance);
110 jgs 82 #endif
111     /* sort the elements by center center coordinates (lexigraphical)*/
112 jfenwick 3086 qsort(center,faces->numElements,sizeof(Dudley_Mesh_findMatchingFaces_center),Dudley_Mesh_findMatchingFaces_compar);
113 jgs 82 /* find elements with matching center */
114     *numPairs=0;
115     /* OMP */
116 jfenwick 3086 for (e=0;e<faces->numElements-1 && Dudley_noError();e++) {
117 gross 888 dist=0;
118     for (i=0;i<numDim;i++) dist=MAX(dist,ABS(center[e].x[i]-center[e+1].x[i]));
119     if (dist < h * tolerance) {
120 jgs 82 e_0=center[e].refId;
121     e_1=center[e+1].refId;
122     elem0[*numPairs]=e_0;
123     elem1[*numPairs]=e_1;
124     /* now the element e_1 is rotated such that the first node in element e_0 and e_1 have the same coordinates */
125     perm=a1;
126     perm_tmp=a2;
127     for (i=0;i<NN;i++) perm[i]=i;
128 jfenwick 3086 while (Dudley_noError()) {
129 jgs 82 /* if node 0 and perm[0] are the same we are ready */
130     getDist(dist,e_0,0,e_1,perm[0]);
131     if (dist<=h*tolerance) break;
132 gross 2748 if (shiftNodes[0]>=0) {
133 jgs 82 /* rotate the nodes */
134 gross 1377 itmp_ptr=perm;
135     perm=perm_tmp;
136     perm_tmp=itmp_ptr;
137     #pragma ivdep
138 gross 2748 for (i=0;i<NN;i++) perm[i]=perm_tmp[shiftNodes[i]];
139 jgs 82 }
140     /* if the permutation is back at the identity, ie. perm[0]=0, the faces don't match: */
141     if (perm[0]==0) {
142 gross 888 sprintf(error_msg,"Mesh_findMatchingFaces:couldn't match first node of element %d to touching element %d",e_0,e_1);
143 jfenwick 3086 Dudley_setError(VALUE_ERROR,error_msg);
144 jgs 82 }
145     }
146     /* now we check if the second nodes match */
147 jfenwick 3086 if (Dudley_noError()) {
148 gross 2748 if (numNodesOnFace>1) {
149 jfenwick 3149 getDist(dist,e_0,1,e_1,perm[1]);
150 jgs 82 /* if the second node does not match we reverse the direction of the nodes */
151     if (dist>h*tolerance) {
152     /* rotate the nodes */
153 gross 2748 if (reverseNodes[0]<0) {
154 gross 888 sprintf(error_msg,"Mesh_findMatchingFaces:couldn't match the second node of element %d to touching element %d",e_0,e_1);
155 jfenwick 3086 Dudley_setError(VALUE_ERROR,error_msg);
156 jgs 82 } else {
157 gross 1377 itmp_ptr=perm;
158     perm=perm_tmp;
159     perm_tmp=itmp_ptr;
160     #pragma ivdep
161 gross 2748 for (i=0;i<NN;i++) perm[i]=perm_tmp[reverseNodes[i]];
162 jfenwick 3149 getDist(dist,e_0,1,e_1,perm[1]);
163 jgs 82 if (dist>h*tolerance) {
164 gross 888 sprintf(error_msg,"Mesh_findMatchingFaces:couldn't match the second node of element %d to touching element %d",e_0,e_1);
165 jfenwick 3086 Dudley_setError(VALUE_ERROR,error_msg);
166 jgs 82 }
167     }
168     }
169     }
170     }
171     /* we check if the rest of the face nodes match: */
172 jfenwick 3086 if (Dudley_noError()) {
173 gross 2748 for (i=2;i<numNodesOnFace;i++) {
174 jfenwick 3149 n=i;
175 jgs 82 getDist(dist,e_0,n,e_1,perm[n]);
176     if (dist>h*tolerance) {
177 gross 888 sprintf(error_msg,"Mesh_findMatchingFaces:couldn't match the %d-th node of element %d to touching element %d",i,e_0,e_1);
178 jfenwick 3086 Dudley_setError(VALUE_ERROR,error_msg);
179 jgs 82 break;
180     }
181     }
182     }
183     /* copy over the permuted nodes of e_1 into matching_nodes_in_elem1 */
184 jfenwick 3086 if (Dudley_noError()) {
185 jgs 82 for (i=0;i<NN;i++) matching_nodes_in_elem1[INDEX2(i,*numPairs,NN)]=faces->Nodes[INDEX2(perm[i],e_1,NN)];
186     }
187     (*numPairs)++;
188     }
189     }
190 jfenwick 3086 #ifdef Dudley_TRACE
191 jgs 82 printf("number of pairs of matching faces %d\n",*numPairs);
192     #endif
193     }
194     /* clean up */
195     TMPMEMFREE(X);
196     TMPMEMFREE(center);
197 gross 1028 TMPMEMFREE(a1);
198     TMPMEMFREE(a1);
199 jgs 82
200     #undef getDist
201     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26