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

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

Parent Directory Parent Directory | Revision Log Revision Log


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


1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 by University of Queensland
5 * 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
14
15 /**************************************************************/
16
17 /* Dudley: Mesh */
18
19 /* searches for faces in the mesh which are matching */
20
21 /**************************************************************/
22
23 #include "Util.h"
24 #include "Mesh.h"
25
26 /**************************************************************/
27
28 static double Dudley_Mesh_lockingGridSize=0;
29
30 int Dudley_Mesh_findMatchingFaces_compar(const void *arg1 , const void *arg2 ) {
31 Dudley_Mesh_findMatchingFaces_center *e1,*e2;
32 bool_t l,g;
33 dim_t i;
34 e1=(Dudley_Mesh_findMatchingFaces_center*) arg1;
35 e2=(Dudley_Mesh_findMatchingFaces_center*) arg2;
36 for (i=0;i<MAX_numDim;i++) {
37 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 if (! (l && g)) {
40 if (l) return -1;
41 if (g) return 1;
42 }
43 }
44 if ( e1->refId < e2->refId ) {
45 return -1;
46 } else if ( e1->refId > e2->refId ) {
47 return 1;
48 } else {
49 return 0;
50 }
51 }
52
53 void Dudley_Mesh_findMatchingFaces(Dudley_NodeFile *nodes, Dudley_ElementFile *faces, double safety_factor,double tolerance,
54 dim_t* numPairs, index_t* elem0,index_t* elem1,index_t* matching_nodes_in_elem1) {
55 #define getDist(_dist_,_e0_,_i0_,_e1_,_i1_) \
56 {dim_t i; \
57 _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 Dudley_ReferenceElement* refElement=NULL;
61 char error_msg[LenErrorMsg_MAX];
62 double h=DBLE(HUGE_VAL),h_local,dist,*X=NULL;
63 Dudley_Mesh_findMatchingFaces_center *center;
64 index_t e_0,e_1,*a1=NULL,*a2=NULL,*perm=NULL,*perm_tmp=NULL,*itmp_ptr=NULL, *shiftNodes=NULL, *reverseNodes=NULL ;
65 dim_t e,i,i0,i1,n,NN,numNodesOnFace;
66
67 dim_t numDim=nodes->numDim;
68
69 refElement= Dudley_ReferenceElementSet_borrowReferenceElement(faces->referenceElementSet, FALSE);
70 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 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 return;
80 }
81 X=TMPMEMALLOC(NN*numDim*faces->numElements,double);
82 center=TMPMEMALLOC(faces->numElements,Dudley_Mesh_findMatchingFaces_center);
83 a1=TMPMEMALLOC(NN,int);
84 a2=TMPMEMALLOC(NN,int);
85 if (!(Dudley_checkPtr(X) || Dudley_checkPtr(center) || Dudley_checkPtr(a1) || Dudley_checkPtr(a2)) ) {
86 /* OMP */
87 for (e=0;e<faces->numElements;e++) {
88 /* get the coordinates of the nodes */
89 Dudley_Util_Gather_double(NN,&(faces->Nodes[INDEX2(0,e,NN)]),numDim,nodes->Coordinates,&(X[INDEX3(0,0,e,numDim,NN)]));
90 /* get the element center */
91 center[e].refId=e;
92 for (i=0;i<MAX_numDim;i++) center[e].x[i]=0;
93 for (i0=0;i0<numNodesOnFace;i0++) {
94 for (i=0;i<numDim;i++) center[e].x[i]+=X[INDEX3(i,i0,e,numDim,NN)];
95 }
96 for (i=0;i<numDim;i++) center[e].x[i]/=numNodesOnFace;
97 /* get the minimum distance between nodes in the element */
98 for (i0=0;i0<numNodesOnFace;i0++) {
99 for (i1=i0+1;i1<numNodesOnFace;i1++) {
100 getDist(h_local,e,i0,e,i1);
101 h=MIN(h,h_local);
102 }
103 }
104 }
105 /* set the */
106 Dudley_Mesh_lockingGridSize=h*MAX(safety_factor,0);
107 #ifdef Dudley_TRACE
108 printf("locking grid size is %e\n",Dudley_Mesh_lockingGridSize);
109 printf("absolute tolerance is %e.\n",h * tolerance);
110 #endif
111 /* sort the elements by center center coordinates (lexigraphical)*/
112 qsort(center,faces->numElements,sizeof(Dudley_Mesh_findMatchingFaces_center),Dudley_Mesh_findMatchingFaces_compar);
113 /* find elements with matching center */
114 *numPairs=0;
115 /* OMP */
116 for (e=0;e<faces->numElements-1 && Dudley_noError();e++) {
117 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 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 while (Dudley_noError()) {
129 /* 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 if (shiftNodes[0]>=0) {
133 /* rotate the nodes */
134 itmp_ptr=perm;
135 perm=perm_tmp;
136 perm_tmp=itmp_ptr;
137 #pragma ivdep
138 for (i=0;i<NN;i++) perm[i]=perm_tmp[shiftNodes[i]];
139 }
140 /* if the permutation is back at the identity, ie. perm[0]=0, the faces don't match: */
141 if (perm[0]==0) {
142 sprintf(error_msg,"Mesh_findMatchingFaces:couldn't match first node of element %d to touching element %d",e_0,e_1);
143 Dudley_setError(VALUE_ERROR,error_msg);
144 }
145 }
146 /* now we check if the second nodes match */
147 if (Dudley_noError()) {
148 if (numNodesOnFace>1) {
149 getDist(dist,e_0,1,e_1,perm[1]);
150 /* if the second node does not match we reverse the direction of the nodes */
151 if (dist>h*tolerance) {
152 /* rotate the nodes */
153 if (reverseNodes[0]<0) {
154 sprintf(error_msg,"Mesh_findMatchingFaces:couldn't match the second node of element %d to touching element %d",e_0,e_1);
155 Dudley_setError(VALUE_ERROR,error_msg);
156 } else {
157 itmp_ptr=perm;
158 perm=perm_tmp;
159 perm_tmp=itmp_ptr;
160 #pragma ivdep
161 for (i=0;i<NN;i++) perm[i]=perm_tmp[reverseNodes[i]];
162 getDist(dist,e_0,1,e_1,perm[1]);
163 if (dist>h*tolerance) {
164 sprintf(error_msg,"Mesh_findMatchingFaces:couldn't match the second node of element %d to touching element %d",e_0,e_1);
165 Dudley_setError(VALUE_ERROR,error_msg);
166 }
167 }
168 }
169 }
170 }
171 /* we check if the rest of the face nodes match: */
172 if (Dudley_noError()) {
173 for (i=2;i<numNodesOnFace;i++) {
174 n=i;
175 getDist(dist,e_0,n,e_1,perm[n]);
176 if (dist>h*tolerance) {
177 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 Dudley_setError(VALUE_ERROR,error_msg);
179 break;
180 }
181 }
182 }
183 /* copy over the permuted nodes of e_1 into matching_nodes_in_elem1 */
184 if (Dudley_noError()) {
185 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 #ifdef Dudley_TRACE
191 printf("number of pairs of matching faces %d\n",*numPairs);
192 #endif
193 }
194 /* clean up */
195 TMPMEMFREE(X);
196 TMPMEMFREE(center);
197 TMPMEMFREE(a1);
198 TMPMEMFREE(a1);
199
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