1 #include <extUtil.h>
2 
3 #define TEST 0
4 
5 /*******************************************************************/
6 /* in:                                                             */
7 /*   elems: he8 elements and amount: numElems                     */
8 /* out:                                                            */
9 /*   ptr: all inner faces pointing to the "next" element           */
10 /*******************************************************************/
11 typedef struct {
12     int e0;
13     int e1;
14     int i;
15 }Esort;
16 
17 
18 
compareFaces(int * a,int * b)19 int compareFaces (int *a, int *b) {
20   /* wird von qsort aufgerufen, vergleicht die ersten drei Zahlen von
21      Integer-Feldern */
22 
23   if ( a[0] < b[0] )
24     return -1 ;
25   else if ( a[0] > b[0] )
26     return 1 ;
27   else if ( a[1] < b[1] )
28     return -1 ;
29   else if ( a[1] > b[1] )
30     return 1 ;
31   else if ( a[2] < b[2] )
32     return -1 ;
33   else if ( a[2] > b[2] )
34     return 1 ;
35   else
36     return 0 ;
37 }
38 
39 
compareElems(Esort * a,Esort * b)40 int compareElems(Esort *a, Esort *b)
41 {
42   /* wird von qsort aufgerufen, vergleicht 2 Int-Felder */
43 
44   if ( a[0].e0 < b[0].e0 )
45     return (-1) ;
46   else if ( a[0].e0 > b[0].e0 )
47     return (1) ;
48   else
49   {
50     if ( a[0].e1 < b[0].e1 )
51       return (-1) ;
52     else if ( a[0].e1 > b[0].e1 )
53       return (1) ;
54     else
55     {
56       return (0) ;
57     }
58   }
59 }
60 
innerFacesHe8(Elements * elems,int numElems,Faces ** ptr)61 int *innerFacesHe8(Elements *elems, int numElems, Faces **ptr)
62 {
63   int i,j,n, numFaces, size ;
64   int *pfaces, *pCurrentFace, *pFaceN, *pmax;
65   int nif, nof;
66   static int nf[2];
67   Faces *iface=NULL;
68   Faces *iface2=NULL;
69   Esort *esort=NULL;
70 
71   int fnr[2], enr[2], fnod[2][4];
72 
73   extern int compareElnum () ;
74   extern void vdsort ( int *) ;
75   extern int commonEdge (int *, int *) ;
76 
77 
78   numFaces = 6 * numElems ;
79   size = (6* numFaces) * sizeof(int) ;
80   if ( size < 1920 ) size = 1920;
81 
82   if ( (pfaces = (int *)malloc( size )) == NULL )
83   { printf("\n\n ERROR: malloc pfaces\n\n") ; exit(-1); }
84 
85   pCurrentFace = pfaces ;
86 
87   for ( i = 0; i < numElems; i++ )
88   {
89     *pCurrentFace = elems[i].nod[0] ;
90     *(pCurrentFace+1) = elems[i].nod[3] ;
91     *(pCurrentFace+2) = elems[i].nod[2] ;
92     *(pCurrentFace+3) = elems[i].nod[1] ;
93     vdsort( pCurrentFace ) ;
94     *(pCurrentFace+3) = 1 ;
95     *(pCurrentFace+4) = i ;
96     pCurrentFace += 6 ;
97     *pCurrentFace = elems[i].nod[4] ;
98     *(pCurrentFace+1) = elems[i].nod[5] ;
99     *(pCurrentFace+2) = elems[i].nod[6] ;
100     *(pCurrentFace+3) = elems[i].nod[7] ;
101     vdsort( pCurrentFace ) ;
102     *(pCurrentFace+3) = 2 ;
103     *(pCurrentFace+4) = i ;
104     pCurrentFace += 6 ;
105     *pCurrentFace = elems[i].nod[0] ;
106     *(pCurrentFace+1) = elems[i].nod[1] ;
107     *(pCurrentFace+2) = elems[i].nod[5] ;
108     *(pCurrentFace+3) = elems[i].nod[4] ;
109     vdsort( pCurrentFace ) ;
110     *(pCurrentFace+3) = 3 ;
111     *(pCurrentFace+4) = i ;
112     pCurrentFace += 6 ;
113     *pCurrentFace = elems[i].nod[1] ;
114     *(pCurrentFace+1) = elems[i].nod[2] ;
115     *(pCurrentFace+2) = elems[i].nod[6] ;
116     *(pCurrentFace+3) = elems[i].nod[5] ;
117     vdsort( pCurrentFace ) ;
118     *(pCurrentFace+3) = 4 ;
119     *(pCurrentFace+4) = i ;
120     pCurrentFace += 6 ;
121     *pCurrentFace = elems[i].nod[3] ;
122     *(pCurrentFace+1) = elems[i].nod[7] ;
123     *(pCurrentFace+2) = elems[i].nod[6] ;
124     *(pCurrentFace+3) = elems[i].nod[2] ;
125     vdsort( pCurrentFace ) ;
126     *(pCurrentFace+3) = 5 ;
127     *(pCurrentFace+4) = i ;
128     pCurrentFace += 6 ;
129     *pCurrentFace = elems[i].nod[0] ;
130     *(pCurrentFace+1) = elems[i].nod[4] ;
131     *(pCurrentFace+2) = elems[i].nod[7] ;
132     *(pCurrentFace+3) = elems[i].nod[3] ;
133     vdsort( pCurrentFace ) ;
134     *(pCurrentFace+3) = 6 ;
135     *(pCurrentFace+4) = i ;
136     pCurrentFace += 6 ;
137   }
138   pmax = pCurrentFace ;  /* gr��ter zul�ssiger Face-Pointer */
139 
140   /* nach Knotennummern sortieren */
141   qsort (pfaces, numFaces, 6*sizeof(int), (void *)compareFaces) ;
142 
143 
144   /* search inner faces */
145   nif=nof=n=0;
146   pFaceN = pfaces ;
147 
148   while ( pFaceN <pmax )
149   {
150     /* if two faces are identical, store the one with the lower elem-index */
151     /* and store the second element and face in nod[4] and nod[5] */
152     if ((pFaceN < pmax-6)&&( compareFaces3 (pFaceN, (pFaceN+6)) == 0 ))
153     {
154 	n+=2;
155       /* recover the two faces in the right order */
156       enr[0]=*(pFaceN + 4);
157       enr[1]=*(pFaceN + 10);
158       fnr[0]=*(pFaceN + 3);
159       fnr[1]=*(pFaceN + 9);
160       for(j=0; j<2; j++)
161       {
162         i=enr[j];
163         switch(fnr[j])
164         {
165           case 1:
166             fnod[j][0] = elems[i].nod[0] ;
167             fnod[j][1] = elems[i].nod[3] ;
168             fnod[j][2] = elems[i].nod[2] ;
169             fnod[j][3] = elems[i].nod[1] ;
170             break;
171           case 2:
172             fnod[j][0] = elems[i].nod[4] ;
173             fnod[j][1] = elems[i].nod[5] ;
174             fnod[j][2] = elems[i].nod[6] ;
175             fnod[j][3] = elems[i].nod[7] ;
176             break;
177           case 3:
178             fnod[j][0] = elems[i].nod[0] ;
179             fnod[j][1] = elems[i].nod[1] ;
180             fnod[j][2] = elems[i].nod[5] ;
181             fnod[j][3] = elems[i].nod[4] ;
182             break;
183           case 4:
184             fnod[j][0] = elems[i].nod[1] ;
185             fnod[j][1] = elems[i].nod[2] ;
186             fnod[j][2] = elems[i].nod[6] ;
187             fnod[j][3] = elems[i].nod[5] ;
188             break;
189           case 5:
190             fnod[j][0] = elems[i].nod[3] ;
191             fnod[j][1] = elems[i].nod[7] ;
192             fnod[j][2] = elems[i].nod[6] ;
193             fnod[j][3] = elems[i].nod[2] ;
194             break;
195           case 6:
196             fnod[j][0] = elems[i].nod[0] ;
197             fnod[j][1] = elems[i].nod[4] ;
198             fnod[j][2] = elems[i].nod[7] ;
199             fnod[j][3] = elems[i].nod[3] ;
200             break;
201         }
202       }
203 
204       if((iface=(Faces *)realloc((Faces *)iface, (nif+1)*sizeof(Faces)))==NULL)
205         printf("\n\n ERROR: realloc failed iface\n\n") ;
206 
207       if(enr[0]<enr[1])
208       {
209         iface[nif].elem_nr =elems[enr[0]].nr;
210         iface[nif].nr=fnr[0];
211         iface[nif].nod[4]=elems[enr[1]].nr;
212         iface[nif].nod[5]=fnr[1];
213         for(j=0; j<4; j++) iface[nif].nod[j]=fnod[0][j];
214       }
215       else
216       {
217         iface[nif].elem_nr =elems[enr[1]].nr;
218         iface[nif].nr=fnr[1];
219         iface[nif].nod[4]=elems[enr[0]].nr;
220         iface[nif].nod[5]=fnr[0];
221         for(j=0; j<4; j++) iface[nif].nod[j]=fnod[1][j];
222       }
223 
224       iface[nif].type    =9;
225       iface[nif].group   =0;
226       iface[nif].mat     =0;
227       iface[nif].side    = NULL;
228 
229 #if TEST
230       printf("n:%d  e1:%d f:%d n:%d %d %d %d\n",n,elems[i].nr, iface[nif].nr,iface[nif].nod[0],iface[nif].nod[1],iface[nif].nod[2], iface[nif].nod[3]);
231       printf("  e2:%d f:%d\n",iface[nif].nod[4], iface[nif].nod[5]);
232 #endif
233 
234       nif++;
235 
236       pFaceN += 12 ;
237     }
238     else
239     {
240       n++;
241 #if TEST
242       printf("n:%d e:%d f:%d n:%d %d %d xx\n",n,elems[*(pFaceN+4)].nr,*(pFaceN+3),*(pFaceN+0),*(pFaceN+1),*(pFaceN+2));
243 #endif
244       nof++;
245       pFaceN += 6 ;
246     }
247   }
248 
249   /* sort the faces according to the related element indexes */
250   if ( (esort = (Esort *)malloc( (nif+1) * sizeof(Esort))) == NULL )
251     printf("ERROR: realloc failed: Rsort\n\n" );
252   for( i=0; i<nif; i++)
253   {
254     esort[i].e0=iface[i].elem_nr;
255     esort[i].e1=iface[i].nod[4];
256     esort[i].i=i;
257   }
258   qsort( esort, nif, sizeof(Esort), (void *)compareElems );
259 
260   if((iface2=(Faces *)malloc((nif+1)*sizeof(Faces)))==NULL)
261     printf("\n\n ERROR: realloc failed iface\n\n") ;
262 
263   for( i=0; i<nif; i++)
264   {
265     iface2[i].elem_nr =iface[esort[i].i].elem_nr;
266     iface2[i].nr      =iface[esort[i].i].nr     ;
267     iface2[i].nod[4]  =iface[esort[i].i].nod[4] ;
268     iface2[i].nod[5]  =iface[esort[i].i].nod[5] ;
269     for(j=0; j<4; j++) iface2[i].nod[j]=iface[esort[i].i].nod[j];
270   }
271   free(iface);
272   free(esort);
273 
274   *ptr=iface2;
275 
276   nf[0]=nif; nf[1]=nof;
277   return(nf);
278 }
279