File: MovingBand2D.cpp

package info (click to toggle)
getdp 2.9.2+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,384 kB
  • ctags: 8,206
  • sloc: cpp: 55,135; fortran: 13,955; yacc: 8,493; lex: 746; sh: 56; ansic: 34; awk: 33; makefile: 24
file content (377 lines) | stat: -rw-r--r-- 13,890 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// GetDP - Copyright (C) 1997-2016 P. Dular and C. Geuzaine, University of Liege
//
// See the LICENSE.txt file for license information. Please report all
// bugs and problems to the public mailing list <getdp@onelab.info>.
//
// Contributor(s):
//   Johan Gyselinck
//

#include <math.h>
#include "ProData.h"
#include "ExtendedGroup.h"
#include "GeoData.h"
#include "ListUtils.h"
#include "TreeUtils.h"
#include "MallocUtils.h"
#include "Message.h"
#include "Pos_Search.h"

#define SQU(a)     ((a)*(a))

extern struct CurrentData Current ;

/* ------------------------------------------------------------------------ */
/*              M o v i n g B a n d 2 D                                     */
/* ------------------------------------------------------------------------ */

struct ThreeInt { int  Int1, Int2, Int3 ; } ;

int  fcmp_int3(const void * a, const void * b)
{
  return  ((struct ThreeInt *)a)->Int1 - ((struct ThreeInt *)b)->Int1 ;
}

int  fcmp_int32(const void * a, const void * b)
{
  return  ((struct ThreeInt *)a)->Int2 - ((struct ThreeInt *)b)->Int2 ;
}

void Contour_MovingBand2D(List_T * InitialList, List_T ** ExtendedList,
			  int * NbrNodes, int ** NumNodes)
{
  Tree_T  * Element_Tr ;
  struct Geo_Element * GeoElement ;
  struct ThreeInt *ThreeInt, ThreeInt1 ;
  int *Nodes, LeftNode, RightNode, LeftInt, RightInt, i_El, i ;

  Element_Tr = Tree_Create(sizeof(struct ThreeInt), fcmp_int3) ;

  for (i_El = 0 ; i_El < Geo_GetNbrGeoElements() ; i_El++) {
    GeoElement = Geo_GetGeoElement(i_El) ;
    if (List_Search(InitialList, &GeoElement->Region, fcmp_int)) {
      if (GeoElement->Type != LINE)
	Message::Error("MovingBand2D contour must contain only line elements") ;
      ThreeInt1.Int1 = i_El; ThreeInt1.Int2 = 0; ThreeInt1.Int3 = 0;
      Tree_Add(Element_Tr, &ThreeInt1) ;
    }
  }

  *ExtendedList = Tree2List(Element_Tr) ; Tree_Delete(Element_Tr) ;
  if (!List_Nbr(*ExtendedList))
    Message::Error("MovingBand2D contour has no elements!") ;

  ThreeInt = (struct ThreeInt *)List_Pointer(*ExtendedList, 0) ;
  Nodes = Geo_GetGeoElement(ThreeInt->Int1)->NumNodes ;

  RightNode = Nodes[1] ; RightInt = 0 ;
  LeftNode  = Nodes[0] ; LeftInt = 0 ;
  ThreeInt->Int2 = 0 ; ThreeInt->Int3 = 1 ;

  for (i = 1 ; i < List_Nbr(*ExtendedList) ; i++) {
    for (i_El = 1 ; i_El < List_Nbr(*ExtendedList) ; i_El++) {
      if ( ((ThreeInt = (struct ThreeInt *)List_Pointer(*ExtendedList, i_El))->Int3) == 0) {
	Nodes = Geo_GetGeoElement(ThreeInt->Int1)->NumNodes;
	if (Nodes[0] == RightNode) {
	  ThreeInt->Int2 = ++RightInt ; ThreeInt->Int3 =  1 ; RightNode = Nodes[1] ;
	}
	else if (Nodes[1] == RightNode) {
	  ThreeInt->Int2 = ++RightInt ; ThreeInt->Int3 = -1 ; RightNode = Nodes[0] ;
	}
	else if (Nodes[0] == LeftNode) {
	  ThreeInt->Int2 = --LeftInt  ; ThreeInt->Int3 = -1 ; LeftNode  = Nodes[1] ;
	}
	else if (Nodes[1] == LeftNode) {
	  ThreeInt->Int2 = --LeftInt  ; ThreeInt->Int3 =  1 ; LeftNode  = Nodes[0] ;
	}
	if (ThreeInt->Int3) break;
      }
    }
    if (!ThreeInt->Int3) Message::Error("Moving Band contour is not connected") ;
  }

  List_Sort(*ExtendedList, fcmp_int32) ;
  *NbrNodes = List_Nbr(*ExtendedList)+1 ;
  *NumNodes = (int *)Malloc(*NbrNodes*sizeof(int)) ;

  ThreeInt = (struct ThreeInt *)List_Pointer(*ExtendedList, 0) ;
  if (ThreeInt->Int3 == 1) {
    (*NumNodes)[0] = (Geo_GetGeoElement(ThreeInt->Int1)->NumNodes)[0];
    (*NumNodes)[1] = (Geo_GetGeoElement(ThreeInt->Int1)->NumNodes)[1];
  }
  else {
    (*NumNodes)[0] = (Geo_GetGeoElement(ThreeInt->Int1)->NumNodes)[1];
    (*NumNodes)[1] = (Geo_GetGeoElement(ThreeInt->Int1)->NumNodes)[0];
  }

  for (i_El = 1 ; i_El < List_Nbr(*ExtendedList) ; i_El++) {
    ThreeInt = (struct ThreeInt *)List_Pointer(*ExtendedList, i_El) ;
    if (ThreeInt->Int3 == 1)
      (*NumNodes)[i_El+1] = (Geo_GetGeoElement(ThreeInt->Int1)->NumNodes)[1];
    else
      (*NumNodes)[i_El+1] = (Geo_GetGeoElement(ThreeInt->Int1)->NumNodes)[0];
  }
}

void  Init_MovingBand2D (struct Group * Group_P)
{
  struct MovingBand2D * MB ;
  int i ;
  int Different_Sense_MB2D(int nth1, int nth2, int ntr1, int ntr2, int closed1, int closed2,
			   double x1[], double y1[], double x2[], double y2[]) ;

  MB = Group_P->MovingBand2D ;

  if (MB->ExtendedList1 && MB->ExtendedList2) {
    Message::Warning("Init_MovingBand has already been done!: Skipping") ;
    return ;
  }

  Contour_MovingBand2D(MB->InitialList1, &MB->ExtendedList1, &MB->NbrNodes1, &MB->NumNodes1) ;
  Contour_MovingBand2D(MB->InitialList2, &MB->ExtendedList2, &MB->NbrNodes2, &MB->NumNodes2) ;

  MB->x1 = (double *)Malloc(MB->NbrNodes1*sizeof(double)) ;
  MB->y1 = (double *)Malloc(MB->NbrNodes1*sizeof(double)) ;
  MB->z1 = (double *)Malloc(MB->NbrNodes1*sizeof(double)) ;

  MB->x2 = (double *)Malloc(MB->NbrNodes2*sizeof(double)) ;
  MB->y2 = (double *)Malloc(MB->NbrNodes2*sizeof(double)) ;
  MB->z2 = (double *)Malloc(MB->NbrNodes2*sizeof(double)) ;

  MB->Closed1 = MB->Closed2 = 0 ;
  if (MB->NumNodes1[0] == MB->NumNodes1[MB->NbrNodes1-1]) MB->Closed1 = 1 ;
  if (MB->NumNodes2[0] == MB->NumNodes2[MB->NbrNodes2-1]) MB->Closed2 = 1 ;

  MB->ntr1 = MB->NbrNodes1-1 ;
  MB->ntr2 = MB->NbrNodes2-1 ;
  if (MB->Period2 != 1) {
    if ((MB->NbrNodes2-1)%MB->Period2 != 0.)
      Message::Warning("Strange periodicity stuff  (%d %d)! Do you know what you're doing?",
                       MB->NbrNodes2-1, MB->Period2);
    MB->ntr2 = (MB->NbrNodes2-1)/MB->Period2;
    Message::Info("Periodicity data (%d %d %d %d)", MB->ntr1, MB->NbrNodes2-1, MB->Period2, MB->ntr2);
  }

  Geo_GetNodesCoordinates (MB->NbrNodes1, MB->NumNodes1, MB->x1, MB->y1, MB->z1) ;
  Geo_GetNodesCoordinates (MB->NbrNodes2, MB->NumNodes2, MB->x2, MB->y2, MB->z2) ;


  if (Different_Sense_MB2D(MB->NbrNodes1, MB->NbrNodes2, MB->ntr1, MB->ntr2,
                           MB->Closed1, MB->Closed2, MB->x1, MB->y1, MB->x2, MB->y2) ) {
    Message::Debug("Contours have a different sense: inverting!");
    for (i=0 ; i<MB->NbrNodes2/2 ; i++) {
      int dummy = MB->NumNodes2[i];
      MB->NumNodes2[i] = MB->NumNodes2[MB->NbrNodes2-1-i];
      MB->NumNodes2[MB->NbrNodes2-1-i] = dummy;
    }
  }

  Message::Debug("Moving Band Contour 1 has %d nodes :", MB->NbrNodes1);
  for (i=0 ; i<MB->NbrNodes1 ; i++) Message::Debug(" %d ", MB->NumNodes1[i]);
  if (MB->Closed1) Message::Debug("  (closed)"); else Message::Debug("  (open)");

  Message::Debug("Moving Band Contour 2 has %d nodes :", MB->NbrNodes2);
  for (i=0 ; i<MB->NbrNodes2 ; i++) Message::Debug(" %d ", MB->NumNodes2[i]);
  if (MB->Closed2) Message::Debug("  (closed, "); else Message::Debug("  (open, ");

  Message::Debug("periodicity 1/%d, ", MB->Period2);

  MB->b1_p1 = (int *)Malloc((MB->NbrNodes1-1)*sizeof(int)) ;
  MB->b1_p2 = (int *)Malloc((MB->NbrNodes1-1)*sizeof(int)) ;
  MB->b1_p3 = (int *)Malloc((MB->NbrNodes1-1)*sizeof(int)) ;

  MB->b2_p1 = (int *)Malloc((MB->NbrNodes2-1)*sizeof(int)) ;
  MB->b2_p2 = (int *)Malloc((MB->NbrNodes2-1)*sizeof(int)) ;
  MB->b2_p3 = (int *)Malloc((MB->NbrNodes2-1)*sizeof(int)) ;

  MB->StartIndexTr = Geo_GetNbrGeoElements() ;
  MB->StartNumTr = Geo_GetGeoElement(0)->Num ;
  for (i=1 ; i<MB->StartIndexTr ; i++)
    if (MB->StartNumTr < Geo_GetGeoElement(i)->Num)
      MB->StartNumTr = Geo_GetGeoElement(i)->Num ;
  (MB->StartNumTr)++;

  Message::Debug("StartNumTr %d  StartIndexTr %d", MB->StartNumTr, MB->StartIndexTr);
}

int Different_Sense_MB2D(int nth1, int nth2, int ntr1, int ntr2, int closed1, int closed2,
			 double x1[], double y1[], double x2[], double y2[]){

  double xm = (x1[0]+x1[1])/2.;
  double ym = (y1[0]+y1[1])/2.;
  int imindist = 0;
  double mindist2 = SQU(xm-x2[0]) + SQU(ym-y2[0]);

  for (int i = 1 ; i < nth2 ; i++ ){
    double dist2 = SQU(xm-x2[i]) + SQU(ym-y2[i]) ;
    if (dist2 < mindist2) { imindist = i; mindist2 = dist2; }
  }

  int itry2 = (closed2==1) ? ((imindist+1) % (nth2-1))        : std::min(imindist+1, nth2) ;
  int itry4 = (closed2==1) ? ((imindist-1+nth2-1) % (nth2-1)) : std::max(imindist-1,0) ;

  double dist1 = SQU(x1[2]-x2[itry2]) + SQU(y1[2]-y2[itry2]);
  double dist2 = SQU(x1[2]-x2[itry4]) + SQU(y1[2]-y2[itry4]);

  return( (dist1 < dist2) ? 0. : 1.) ;
}

void Mesh_MB2D(int nth1, int nth2, int ntr1, int ntr2, int closed1, int closed2,
	       double x1[], double y1[], double x2[], double y2[],
	       double * area_moving_band,
	       int b1_p1[], int b1_p2[], int b1_p3[], int b2_p1[], int b2_p2[], int b2_p3[])
{

  double area_tr1,area_tr2;

  int Delauny_1234_MB(double, double, double, double, double, double, double, double,
		      double *, double * );

  double xm = (x1[0]+x1[1])/2.;
  double ym = (y1[0]+y1[1])/2.;
  int imindist = 0;
  double mindist2 = SQU(xm-x2[0]) + SQU(ym-y2[0]);
  for (int i = 1 ; i < nth2 ; i++ ){
    double dist2 = SQU(xm-x2[i]) + SQU(ym-y2[i]) ;
    if (dist2 < mindist2) { imindist = i; mindist2 = dist2; }
  }

  int itry2 = (closed2==1) ? ((imindist+1) % (nth2-1))        : std::min(imindist+1, nth2) ;
  int itry4 = (closed2==1) ? ((imindist-1+nth2-1) % (nth2-1)) : std::max(imindist-1,0) ;

  double dist1 = SQU(x1[2]-x2[itry2]) + SQU(y1[2]-y2[itry2]);
  double dist2 = SQU(x1[2]-x2[itry4]) + SQU(y1[2]-y2[itry4]);
  int d2 = (dist1 < dist2) ? 1 : -1 ;

  Message::Debug("+++++++++++++++++++++++++++++++++++++++++++++++++ %d",d2);

  *area_moving_band =
        fabs( (x2[imindist]-x1[0])*(y1[1]-y1[0])-(x1[1]-x1[0])*(y2[imindist]-y1[0]) )/2. ;
  int itry1 = 1 ;
  int itry3 = 2 ;
  itry2 = imindist ;
  itry4 = (closed2==1) ? ((imindist + d2) % (nth2-1)) : imindist + d2 ;

  int n1 = 0; int n2 = 0;
  b1_p1[n1] = 0; b1_p2[n1] = 1; b1_p3[n1] = itry2;
  n1++;

  for (int i = 1 ; i < ntr1 + ntr2  ; i++ ){
    if ( (Delauny_1234_MB (x1[itry1], y1[itry1], x2[itry2], y2[itry2],
			   x1[itry3], y1[itry3], x2[itry4], y2[itry4],
			   &area_tr1, &area_tr2) == 1) &&
	 itry1 < nth1 && itry1 ){
      b1_p1[n1] = itry1; b1_p2[n1] = itry3; b1_p3[n1] = itry2;
      itry1++; itry3++; if (closed1) {itry1 = itry1 % (nth1-1); itry3 = itry3 % (nth1-1) ;}
      *area_moving_band += area_tr1;
      n1++;
    } else{
      b2_p1[n2] = itry2; b2_p2[n2] = itry4; b2_p3[n2] = itry1;
      itry2+=d2; itry4+=d2;
      if (closed2) {
	itry2 = (nth2-1+itry2) % (nth2-1);
	itry4 = (nth2-1+itry4) % (nth2-1) ;
      }
      *area_moving_band += area_tr2;
      n2++;
    }
  }
  if(n1 != ntr1 || n2 != ntr2){
    Message::Error("Meshing of 2D Moving Band failed (%d != %d || %d != %d)",
                   n1, ntr1, n2, ntr2);
  }
}

void  Mesh_MovingBand2D (struct Group * Group_P)
{
  struct MovingBand2D * MB ;
  struct Geo_Element Geo_Element ;
  struct GeoData  * GeoData ;
  int *n ;

  int *NumNodes1, *NumNodes2;
  int *b1_p1, *b1_p2, *b1_p3, *b2_p1, *b2_p2, *b2_p3;

  MB = Group_P->MovingBand2D ;
  if (!MB->ExtendedList1 || !MB->ExtendedList2) Init_MovingBand2D(Group_P) ;

  NumNodes1 = MB->NumNodes1; NumNodes2 = MB->NumNodes2;
  b1_p1 = MB->b1_p1; b1_p2 = MB->b1_p2; b1_p3 = MB->b1_p3;
  b2_p1 = MB->b2_p1; b2_p2 = MB->b2_p2; b2_p3 = MB->b2_p3;

  Geo_GetNodesCoordinates (MB->NbrNodes1, NumNodes1, MB->x1, MB->y1, MB->z1) ;
  Geo_GetNodesCoordinates (MB->NbrNodes2, NumNodes2, MB->x2, MB->y2, MB->z2) ;

  Mesh_MB2D(MB->NbrNodes1, MB->NbrNodes2, MB->ntr1, MB->ntr2, MB->Closed1, MB->Closed2,
	    MB->x1, MB->y1, MB->x2, MB->y2, &MB->Area,
	    b1_p1, b1_p2, b1_p3, b2_p1, b2_p2, b2_p3);

  GeoData = Current.GeoData ;

  Geo_Element.NbrEdges = Geo_Element.NbrFacets = 0 ;
  Geo_Element.NumEdges = Geo_Element.NumFacets = NULL ;
  Geo_Element.Region = MB->PhysNum ;
  Geo_Element.NbrNodes = 3 ; Geo_Element.Type = TRIANGLE ;

  int NbrGeo = Geo_GetNbrGeoElements();

  for (int i = 0 ; i < MB->ntr1 ; i++){
    int index = MB->StartIndexTr+i ;
    if (index < NbrGeo) {
      n = (int *)(((struct Geo_Element *)List_Pointer(GeoData->Elements, index))->NumNodes) ;
      n[0] = NumNodes1[b1_p1[i]] ; n[1] = NumNodes1[b1_p2[i]] ; n[2] = NumNodes2[b1_p3[i]] ;
    }
    else {
      Geo_Element.Num =  MB->StartNumTr + i ;
      Geo_Element.NumNodes = n = (int *)Malloc(3 * sizeof(int)) ;
      n[0] = NumNodes1[b1_p1[i]] ; n[1] = NumNodes1[b1_p2[i]] ; n[2] = NumNodes2[b1_p3[i]] ;
      List_Put(GeoData->Elements, index, &Geo_Element) ;
    }
    // printf("Tr1 %d : %d %d %d \n",MB->StartNumTr+i, n[0], n[1], n[2]);
  }

  for (int i = 0 ; i < MB->ntr2  ; i++){
    Geo_Element.Num =  MB->StartNumTr + MB->ntr1 +i ;
    int index = MB->StartIndexTr+MB->ntr1+i ;
    if (index < NbrGeo) {
      n = (int *)(((struct Geo_Element *)List_Pointer(GeoData->Elements, index))->NumNodes) ;
      n[0] = NumNodes2[b2_p1[i]] ; n[1] = NumNodes2[b2_p2[i]] ; n[2] = NumNodes1[b2_p3[i]] ;
    }
    else {
      Geo_Element.Num =  MB->StartNumTr + MB->ntr1 + i ;
      Geo_Element.NumNodes = n = (int *)Malloc(3 * sizeof(int)) ;
      n[0] = NumNodes2[b2_p1[i]] ; n[1] = NumNodes2[b2_p2[i]] ; n[2] = NumNodes1[b2_p3[i]] ;
      List_Put(GeoData->Elements, index, &Geo_Element) ;
    }
    Message::Debug("Tr2 %d : %d %d %d",MB->StartNumTr+MB->ntr1+i, n[0], n[1], n[2]);
  }

  Message::Debug("Moving band meshed (area = %e)", MB->Area);

  Free_SearchGrid(&Current.GeoData->Grid);
}

int Delauny_1234_MB (double x1, double y1, double x2, double y2,
		     double x3, double y3, double x4, double y4,
		     double * area1, double * area2)
{

  double Det1 = (x3-x1)*(y2-y1)-(x2-x1)*(y3-y1);
  double Det2 = (x4-x1)*(y2-y1)-(x2-x1)*(y4-y1);
  if( !Det1 || !Det2 ) {
    Message::Error("Colinear points in Delauny_1234 ("
                   "Det1 %g Det2 %g "
                   "x1 %e y1 %e x2 %e y2 %e x3 %e y3 %e x4 %e y4 %e)",
                   Det1, Det2, x1, y1, x2, y2, x3, y3, x4, y4);
  }
  double t1 = ( (x3-x1)*(x3-x2)+(y3-y1)*(y3-y2) ) / Det1 ;
  double t2 = ( (x4-x1)*(x4-x2)+(y4-y1)*(y4-y2) ) / Det2 ;
  if ( fabs(t1) < fabs(t2) ){
    *area1 = fabs(Det1)/2.;
    return (1);
  }
  else{
    *area2 = fabs(Det2)/2.;
    return (2);
  }
}