Traverse.cc 32.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include "Traverse.h"
#include "Element.h"
#include "Line.h"
#include "Triangle.h"
#include "Tetrahedron.h"
#include "ElInfo.h"
#include "Mesh.h"
#include "Flag.h"
#include "MacroElement.h"
#include "FixVec.h"
#include "Parametric.h"
12
#include "OpenMP.h"
13
14
15
16
17
18

namespace AMDiS {

  ElInfo* TraverseStack::traverseFirst(Mesh *mesh, int level,
				       Flag fill_flag)
  {
19
    FUNCNAME("TraverseStack::traverseFirst()");
20

21
22
23
    traverse_mesh = mesh;
    traverse_level = level;
    traverse_fill_flag = fill_flag; 
24
25
26
27
28
29

    if (stack_size < 1)
      enlargeTraverseStack();

    Flag FILL_ANY = Mesh::getFillAnyFlag(mesh->getDim());

30
    for (int i = 0; i < stack_size; i++)
31
32
      elinfo_stack[i]->setFillFlag(fill_flag & FILL_ANY);

33

34
35
36
37
    elinfo_stack[0]->setMesh(mesh);
    elinfo_stack[1]->setMesh(mesh);

    if (fill_flag.isSet(Mesh::CALL_LEAF_EL_LEVEL)) {
38
      TEST_EXIT_DBG(level >= 0)("invalid level: %d\n", level);
39
40
41
    }

    traverse_mel = NULL;
42
    stack_used = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
43
   
44
45
46
47
48
    return(traverseNext(NULL));  
  }

  ElInfo* TraverseStack::traverseNext(ElInfo* elinfo_old)
  {
49
    FUNCNAME("TraverseStack::traverseNext()");
Thomas Witkowski's avatar
Thomas Witkowski committed
50
\
51
    ElInfo *elinfo = NULL;
52
53
54
55
56
57
58
    ElInfo::traverseId = id;
    Parametric *parametric = traverse_mesh->getParametric();

    if (stack_used) {
      if (parametric) 
	elinfo_old = parametric->removeParametricInfo(elinfo_old);

59
      TEST_EXIT_DBG(elinfo_old == elinfo_stack[stack_used])("invalid old elinfo\n");
60
61
    }
    else {
62
      TEST_EXIT_DBG(elinfo_old == NULL)("invalid old elinfo != nil\n");
63
64
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
65
    if (traverse_fill_flag.isSet(Mesh::CALL_LEAF_EL)) {
66
      elinfo = traverseLeafElement();
Thomas Witkowski's avatar
Thomas Witkowski committed
67
    } else {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
      if (traverse_fill_flag.isSet(Mesh::CALL_LEAF_EL_LEVEL))
	elinfo = traverseLeafElementLevel();
      else if (traverse_fill_flag.isSet(Mesh::CALL_EL_LEVEL))
	elinfo = traverseElementLevel();
      else if (traverse_fill_flag.isSet(Mesh::CALL_MG_LEVEL))
	elinfo = traverseMultiGridLevel();
      else
	if (traverse_fill_flag.isSet(Mesh::CALL_EVERY_EL_PREORDER))
	  elinfo = traverseEveryElementPreorder();
	else if (traverse_fill_flag.isSet(Mesh::CALL_EVERY_EL_INORDER))
	  elinfo = traverseEveryElementInorder();
	else if (traverse_fill_flag.isSet(Mesh::CALL_EVERY_EL_POSTORDER))
	  elinfo = traverseEveryElementPostorder();
	else
	  ERROR_EXIT("invalid traverse_flag\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
83
    }
84

85
86
    if (elinfo) {
      if (parametric) {
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
	elinfo = parametric->addParametricInfo(elinfo);
      }
      elinfo->fillDetGrdLambda();
    }

    return(elinfo);
  }



  /****************************************************************************/
  /* common (static) traversal routines for 2d and 3d                         */
  /* to be #included in traverse_r.c                                          */
  /****************************************************************************/

  /* traverse hierarchical mesh,  call el_fct() for each/some element */

  /****************************************************************************/
  /*   recursive_traverse:                                		    */
  /*   -------------------                                		    */
  /*   recursively traverse the mesh hierarchy tree       		    */
  /*   call the routine traverse_info->el_fct(el_info) with    		    */
  /*    - all tree leaves                                 		    */
  /*    - all tree leaves at a special level              		    */
  /*    - all tree elements in pre-/in-/post-order        		    */
  /*   depending on the traverse_info->level variable     		    */
  /****************************************************************************/

Thomas Witkowski's avatar
Thomas Witkowski committed
115
116
  //  int Traverse::recursive(ElInfo *elinfo)
  int Traverse::recursive(ElInfoStack *elInfoStack)
117
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
118
    FUNCNAME("Traverse::recursive()");
119

Thomas Witkowski's avatar
Thomas Witkowski committed
120
121
    ElInfo *elinfo = elInfoStack->getCurrentElement();

Thomas Witkowski's avatar
Thomas Witkowski committed
122
    Element *el = elinfo->getElement();
Thomas Witkowski's avatar
Thomas Witkowski committed
123
    int mg_level, sum = 0;
124
125
126
127
128
    Parametric *parametric = mesh->getParametric();
    ElInfo::traverseId = id;

    if (flag.isSet(Mesh::CALL_LEAF_EL)) {
      if (el->getFirstChild()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
129
	ElInfo* elinfo_new = elInfoStack->getNextElement();
130
	elinfo_new->fillElInfo(0, elinfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
131
	sum += recursive(elInfoStack);
132
	elinfo_new->fillElInfo(1, elinfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
133
134
	sum += recursive(elInfoStack);
	elInfoStack->getBackElement();
135
136
137
138
      } else {
	if (el_fct != NULL) {
	  if (parametric) 
	    elinfo = parametric->addParametricInfo(elinfo);
139
	  elinfo->fillDetGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
140
	  sum += el_fct(elinfo);
141
142
	  if (parametric) 
	    elinfo = parametric->removeParametricInfo(elinfo);
143
144
	}
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
145
      return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;
146
147
148
149
150
    }

    if (flag.isSet(Mesh::CALL_LEAF_EL_LEVEL)) {
      if (el->getFirstChild()) {
	if ((elinfo->getLevel() >= level)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
151
	  return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;
152
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
153
	ElInfo* elinfo_new = elInfoStack->getNextElement();
154
	elinfo_new->fillElInfo(0, elinfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
155
	sum += recursive(elInfoStack);
156
	elinfo->fillElInfo(1, elinfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
157
158
159
	sum += recursive(elInfoStack);
	elInfoStack->getBackElement();
      } else {
160
161
162
	if ((elinfo->getLevel() == level) && (el_fct!=NULL)) {
	  if (parametric) 
	    elinfo = parametric->addParametricInfo(elinfo);
163
	  elinfo->fillDetGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
164
	  sum += el_fct(elinfo);
165
166
	  if (parametric) 
	    elinfo = parametric->removeParametricInfo(elinfo);
167
168
	}
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
169
      return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;
170
171
172
173
    }

    if (flag.isSet(Mesh::CALL_EL_LEVEL)) {
      if (elinfo->getLevel() == level) {
Thomas Witkowski's avatar
Thomas Witkowski committed
174
175
176
	if (NULL != el_fct) {
	  if (parametric) 
	    elinfo = parametric->addParametricInfo(elinfo);
177
	  elinfo->fillDetGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
178
179
180
	  sum += el_fct(elinfo); 
	  if (parametric) 
	    elinfo = parametric->removeParametricInfo(elinfo);
181
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
182
      } else {
183
	if (elinfo->getLevel() > level){
Thomas Witkowski's avatar
Thomas Witkowski committed
184
185
186
187
188
189
190
191
	  return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;
	} else if (el->getFirstChild()) {
	  ElInfo* elinfo_new = elInfoStack->getNextElement();
	  elinfo_new->fillElInfo(0, elinfo);
	  sum += recursive(elInfoStack);
	  elinfo_new->fillElInfo(1, elinfo);
	  sum += recursive(elInfoStack);
	  elInfoStack->getBackElement();
192
193
194
	}
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
195
      return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;
196
197
198
199
    }

    if (flag.isSet(Mesh::CALL_MG_LEVEL)) {

Thomas Witkowski's avatar
Thomas Witkowski committed
200
201
      mg_level = (static_cast<int>(elinfo->getLevel()) + mesh->getDim() - 1) / 
	mesh->getDim();
202
203
204
205
206
207

      if (mg_level > static_cast<int>(level))  {
	return 0;
      }

      if (!(el->getFirstChild())) {
Thomas Witkowski's avatar
Thomas Witkowski committed
208
209
210
	if (el_fct != NULL) {
	  if (parametric) 
	    elinfo = parametric->addParametricInfo(elinfo);
211
	  elinfo->fillDetGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
212
213
214
	  sum += el_fct(elinfo);
	  if (parametric) 
	    elinfo = parametric->removeParametricInfo(elinfo);
215
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
216
	return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;
217
218
      }
    
Thomas Witkowski's avatar
Thomas Witkowski committed
219
220
221
222
223
224
225
226
      if ((mg_level == level) && ((elinfo->getLevel() % mesh->getDim()) == 0)) {
	if (el_fct != NULL) {
	  if (parametric) 
	    elinfo = parametric->addParametricInfo(elinfo);
	  elinfo->fillDetGrdLambda();
	  sum += el_fct(elinfo); 
	  if (parametric) 
	    elinfo = parametric->removeParametricInfo(elinfo);
227
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
228
229
	return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;
      }
230

Thomas Witkowski's avatar
Thomas Witkowski committed
231
      ElInfo* elinfo_new = elInfoStack->getNextElement();
232
233
 
      elinfo_new->fillElInfo(0, elinfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
234
      sum += recursive(elInfoStack);
235
236

      elinfo_new->fillElInfo(1, elinfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
237
      sum += recursive(elInfoStack);
238

Thomas Witkowski's avatar
Thomas Witkowski committed
239
240
241
      elInfoStack->getBackElement();

      return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;   
242
243
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
244
    if (flag.isSet(Mesh::CALL_EVERY_EL_PREORDER)) {
245
      if (el_fct != NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
246
247
	if (parametric) 
	  elinfo = parametric->addParametricInfo(elinfo);
248
	elinfo->fillDetGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
249
250
251
	sum += el_fct(elinfo);
	if (parametric) 
	  elinfo = parametric->removeParametricInfo(elinfo);
252
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
253
    }
254

Thomas Witkowski's avatar
Thomas Witkowski committed
255
256
257
    if (el->getFirstChild()) {
      ElInfo* elinfo_new = elInfoStack->getNextElement();
   
258
      elinfo_new->fillElInfo(0, elinfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
259
      sum += recursive(elInfoStack);
260
261
262

      if (flag.isSet(Mesh::CALL_EVERY_EL_INORDER)) 
	if (el_fct!=NULL) { 
Thomas Witkowski's avatar
Thomas Witkowski committed
263
264
	  if (parametric) 
	    elinfo = parametric->addParametricInfo(elinfo);
265
	  elinfo->fillDetGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
266
267
268
	  sum += el_fct(elinfo);
	  if (parametric) 
	    elinfo = parametric->removeParametricInfo(elinfo);
269
270
	}
      elinfo_new->fillElInfo(1, elinfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
271
      sum += recursive(elInfoStack);
272

Thomas Witkowski's avatar
Thomas Witkowski committed
273
274
      elInfoStack->getBackElement();
    } else {
275
      if (flag.isSet(Mesh::CALL_EVERY_EL_INORDER)) 
Thomas Witkowski's avatar
Thomas Witkowski committed
276
277
278
	if (el_fct != NULL) {
	  if (parametric) 
	    elinfo = parametric->addParametricInfo(elinfo);
279
	  elinfo->fillDetGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
280
281
282
	  sum += el_fct(elinfo);
	  if (parametric) 
	    elinfo = parametric->removeParametricInfo(elinfo);
283
284
285
286
	}
    }

    if (flag.isSet(Mesh::CALL_EVERY_EL_POSTORDER)) 
Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
289
      if (el_fct != NULL) {
	if (parametric) 
	  elinfo = parametric->addParametricInfo(elinfo);
290
	elinfo->fillDetGrdLambda();
Thomas Witkowski's avatar
Thomas Witkowski committed
291
292
293
	sum += el_fct(elinfo);
	if (parametric) 
	  elinfo = parametric->removeParametricInfo(elinfo);
294
295
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
296
    return (flag.isSet(Mesh::FILL_ADD_ALL)) ? sum : 0;
297
298
299
300
  }

  void TraverseStack::enlargeTraverseStack()
  {
301
302
303
    FUNCNAME("TraverseStack::enlargeTraverseStack()");

    int new_stack_size = stack_size + 10;
304
305

    elinfo_stack.resize(new_stack_size, NULL);
306
 
307
    // create new elinfos
308
309
    for (int i = stack_size; i < new_stack_size; i++) {
      TEST_EXIT_DBG(elinfo_stack[i] == NULL)("???\n");
310
311
312
      elinfo_stack[i] = traverse_mesh->createNewElInfo();
    }

313
314
315
316
317
    if (stack_size > 0) {
      for (int i = stack_size; i < new_stack_size; i++) {
	elinfo_stack[i]->setFillFlag(elinfo_stack[0]->getFillFlag());
      }
    }
318
319
320
321
322

    info_stack.resize(new_stack_size);
    save_elinfo_stack.resize(new_stack_size, NULL);

    // create new elinfos
323
324
    for (int i = stack_size; i < new_stack_size; i++) {
      TEST_EXIT_DBG(save_elinfo_stack[i] == NULL)("???\n");
325
326
327
328
329
330
331
332
333
334
      save_elinfo_stack[i] = traverse_mesh->createNewElInfo();
    }
    save_info_stack.resize(new_stack_size);

    stack_size = new_stack_size;    
  }

  
  ElInfo* TraverseStack::traverseLeafElement()
  {
335
    FUNCNAME("TraverseStack::traverseLeafElement()");
336
337
338

    Element *el;

339
340
    if (stack_used == 0) {   /* first call */
      currentMacro = traverse_mesh->firstMacroElement();
341
342
343
344
345
346

      while (((*currentMacro)->getIndex() % maxThreads_ != myThreadId_) &&
      	     (currentMacro != traverse_mesh->endOfMacroElements())) {
      	currentMacro++;
      }

347
348
      if (currentMacro == traverse_mesh->endOfMacroElements()) 
	return NULL;
349

350
351
352
353
354
      traverse_mel = *currentMacro;

      stack_used = 1;
      elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
      info_stack[stack_used] = 0;
355

356
357
      el = elinfo_stack[stack_used]->getElement();
      if ((el == NULL) || (el->getFirstChild() == NULL)) {
358
	return (elinfo_stack[stack_used]);
359
360
361
362
363
      }
    } else {
      el = elinfo_stack[stack_used]->getElement();
      
      /* go up in tree until we can go down again */
364
365
366
367
368
      while ((stack_used > 0) && 
	     ((info_stack[stack_used] >= 2) || (el->getFirstChild() == NULL))) {
	stack_used--;
	el = elinfo_stack[stack_used]->getElement();
      }
369
370
371
      
      /* goto next macro element */
      if (stack_used < 1) {
372
373
374
375
376
	do {	
	  currentMacro++;
	} while ((currentMacro != traverse_mesh->endOfMacroElements()) && 
		 ((*currentMacro)->getIndex() % maxThreads_ != myThreadId_));

377
378
379
380
	if (currentMacro == traverse_mesh->endOfMacroElements()) 
	  return NULL;
	traverse_mel = *currentMacro;
	
381
382
383
	stack_used = 1;
	elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
	info_stack[stack_used] = 0;
384
	
385
386
	el = elinfo_stack[stack_used]->getElement();
	if ((el == NULL) || (el->getFirstChild() == NULL)) {
387
	  return (elinfo_stack[stack_used]);
388
389
	}
      }
390
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
391
   
392
    /* go down tree until leaf */
393
394
395
396
397
398
399
400
401
402
403
    while (el->getFirstChild()) {
      if (stack_used >= stack_size-1) 
	enlargeTraverseStack();
      
      int i = info_stack[stack_used];
      el = const_cast<Element*>( ((i == 0) ? el->getFirstChild() : el->getSecondChild()));
      info_stack[stack_used]++;
      elinfo_stack[stack_used + 1]->fillElInfo(i, elinfo_stack[stack_used]);
      stack_used++;
      
      
404
      TEST_EXIT_DBG(stack_used < stack_size)
405
406
407
408
409
	("stack_size=%d too small, level=(%d,%d)\n",
	 stack_size, elinfo_stack[stack_used]->getLevel());
      
      info_stack[stack_used] = 0;
    }
410
411
412
413
414
415
    
    return elinfo_stack[stack_used];
  }

  ElInfo* TraverseStack::traverseLeafElementLevel()
  {
416
    FUNCNAME("TraverseStack::traverseLeafElementLevel()");
417
    ERROR_EXIT("not yet implemented\n");
418

419
420
421
422
423
    return NULL;
  }

  ElInfo* TraverseStack::traverseElementLevel()
  {
424
    FUNCNAME("TraverseStack::traverseElementLevel()");
425
426
427
428
429
430

    ElInfo *elInfo;
    do {
      elInfo = traverseEveryElementPreorder();
    } while (elInfo != NULL && elInfo->getLevel() != traverse_level);

431
    return elInfo;
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
  }

  ElInfo* TraverseStack::traverseMultiGridLevel()
  {
    FUNCNAME("TraverseStack::traverseMultiGridLevel");
    Element *el;
    int i;

    if (stack_used == 0)   /* first call */
      {
	currentMacro = traverse_mesh->firstMacroElement();
	traverse_mel = *currentMacro;
	if (traverse_mel == NULL)  return NULL;

	stack_used = 1;
	elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
	info_stack[stack_used] = 0;
    
	if((elinfo_stack[stack_used]->getLevel() == traverse_level) ||
	   (elinfo_stack[stack_used]->getLevel() < traverse_level && 
	    elinfo_stack[stack_used]->getElement()->isLeaf()))
	  return(elinfo_stack[stack_used]);
      }
  
    el = elinfo_stack[stack_used]->getElement();

    /* go up in tree until we can go down again */
    while((stack_used > 0) && 
	  ((info_stack[stack_used] >= 2) || (el->getFirstChild()==NULL)))
      {
	stack_used--;
	el = elinfo_stack[stack_used]->getElement();
      }


    /* goto next macro element */
    if (stack_used < 1) {
      currentMacro++;
      if (currentMacro == traverse_mesh->endOfMacroElements())  return(NULL);
      traverse_mel = *currentMacro;

      stack_used = 1;
      elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
      info_stack[stack_used] = 0;

      if((elinfo_stack[stack_used]->getLevel() == traverse_level) ||
	 (elinfo_stack[stack_used]->getLevel() < traverse_level && 
	  elinfo_stack[stack_used]->getElement()->isLeaf()))
	return(elinfo_stack[stack_used]);
    }


    /* go down tree */

    if (stack_used >= stack_size-1) enlargeTraverseStack();
    i = info_stack[stack_used];
    info_stack[stack_used]++;

    elinfo_stack[stack_used + 1]->fillElInfo(i, elinfo_stack[stack_used]);
    stack_used++;

493
    TEST_EXIT_DBG(stack_used < stack_size)
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
      ("stack_size=%d too small, level=%d\n",
       stack_size, elinfo_stack[stack_used]->getLevel());

    info_stack[stack_used] = 0;
  
    if((elinfo_stack[stack_used]->getLevel() == traverse_level) ||
       (elinfo_stack[stack_used]->getLevel() < traverse_level && 
	elinfo_stack[stack_used]->getElement()->isLeaf()))
      return(elinfo_stack[stack_used]);

    return traverseMultiGridLevel();
  }

  ElInfo* TraverseStack::traverseEveryElementPreorder()
  {
509
    FUNCNAME("TraverseStack::traverseEveryElementPreorder()");
510
511
512

    Element *el;

513
514
515
516
517
518
519
520
521
    if (stack_used == 0) {   /* first call */
      currentMacro = traverse_mesh->firstMacroElement();
      traverse_mel = *currentMacro;
      if (traverse_mel == NULL)  
	return NULL;
      
      stack_used = 1;
      elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
      info_stack[stack_used] = 0;
522

523
524
      return(elinfo_stack[stack_used]);
    }
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
  
    el = elinfo_stack[stack_used]->getElement();

    /* go up in tree until we can go down again */
    while((stack_used > 0) && 
	  ((info_stack[stack_used] >= 2) || (el->getFirstChild()==NULL)))
      {
	stack_used--;
	el = elinfo_stack[stack_used]->getElement();
      }


    /* goto next macro element */
    if (stack_used < 1) {
      currentMacro++;
540
541
      if (currentMacro == traverse_mesh->endOfMacroElements()) 
	return(NULL);
542
543
544
545
546
547
548
549
550
551
552
553
      traverse_mel = *currentMacro;

      stack_used = 1;
      elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
      info_stack[stack_used] = 0;

      return(elinfo_stack[stack_used]);
    }


    /* go down tree */

554
    if (stack_used >= stack_size - 1) 
555
556
557
      enlargeTraverseStack();

    int i = info_stack[stack_used];
558
559
560
561
562
    info_stack[stack_used]++;

    elinfo_stack[stack_used + 1]->fillElInfo(i, elinfo_stack[stack_used]);
    stack_used++;

563
    TEST_EXIT_DBG(stack_used < stack_size)
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
      ("stack_size=%d too small, level=%d\n",
       stack_size, elinfo_stack[stack_used]->getLevel());

    info_stack[stack_used] = 0;
  
    return(elinfo_stack[stack_used]);
  }

  ElInfo* TraverseStack::traverseEveryElementInorder()
  {
    FUNCNAME("TraverseStack::traverseEveryElementInorder");
    ERROR_EXIT("not yet implemented\n");
    return NULL;
  }

  ElInfo* TraverseStack::traverseEveryElementPostorder()
  {
    FUNCNAME("TraverseStack::traverseEveryElementPostorder");

    Element *el;
    int i;

    if (stack_used == 0)   /* first call */
      {
	currentMacro = traverse_mesh->firstMacroElement();
	if(currentMacro == traverse_mesh->endOfMacroElements()) return NULL;
	traverse_mel = *currentMacro;

	stack_used = 1;
	elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
	info_stack[stack_used] = 0;

	//return(elinfo_stack[stack_used]);
      }
    else { /* don't go up on first call */
  
      el = elinfo_stack[stack_used]->getElement();

      /* go up in tree until we can go down again */          /* postorder!!! */
      while((stack_used > 0) && 
	    ((info_stack[stack_used] >= 3) || (el->getFirstChild()==NULL)))
	{
	  stack_used--;
	  el = elinfo_stack[stack_used]->getElement();
	}


      /* goto next macro element */
      if (stack_used < 1) {
	currentMacro++;
	if(currentMacro == traverse_mesh->endOfMacroElements()) return NULL;
	traverse_mel = *currentMacro;

	stack_used = 1;
	elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
	info_stack[stack_used] = 0;

	/*    return(elinfo_stack+stack_used); */
      }
    }
    /* go down tree */

    while(elinfo_stack[stack_used]->getElement()->getFirstChild()
	  && (info_stack[stack_used] < 2)) {
      if (stack_used >= stack_size-1) enlargeTraverseStack();
      i = info_stack[stack_used];
      info_stack[stack_used]++;
      elinfo_stack[stack_used + 1]->fillElInfo(i, elinfo_stack[stack_used]);
      stack_used++;
      info_stack[stack_used] = 0;
    }
  
    info_stack[stack_used]++;      /* postorder!!! */

    return(elinfo_stack[stack_used]);
  }

  ElInfo* TraverseStack::traverseNeighbour(ElInfo* elinfo_old, int neighbour)
  {
    int dim = elinfo_old->getMesh()->getDim();
    switch(dim) {
    case 1:
      ERROR_EXIT("invalid dim\n");
      break;
    case 2:
      return traverseNeighbour2d(elinfo_old, neighbour);
      break;
    case 3:
      return traverseNeighbour3d(elinfo_old, neighbour);
      break;
    default: ERROR_EXIT("invalid dim\n");
    }
    return NULL;
  }

  ElInfo* TraverseStack::traverseNeighbour3d(ElInfo* elinfo_old, int neighbour)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
661
662
    FUNCNAME("TraverseStackLLtraverseNeighbour3d()");

663
    Element *el2;
664
665
666
667
668
669
670
671
672
    ElInfo *old_elinfo, *elinfo, *elinfo2;
    const DegreeOfFreedom *dof;
    int i, nb, opp_vertex, stack2_used, typ = 0;
    int sav_index, sav_neighbour = neighbour;

    static int coarse_nb[3][3][4] = {{{-2,-2,-2,-2}, {-1,2,3,1}, {-1,3,2,0}},
				     {{-2,-2,-2,-2}, {-1,2,3,1}, {-1,2,3,0}},
				     {{-2,-2,-2,-2}, {-1,2,3,1}, {-1,2,3,0}}};

673
    TEST_EXIT_DBG(stack_used > 0)("no current element\n");
674
675
676

    Parametric *parametric = traverse_mesh->getParametric();

677
678
    if (parametric) 
      elinfo_old = parametric->removeParametricInfo(elinfo_old);
679

680
    TEST_EXIT_DBG(elinfo_old == elinfo_stack[stack_used])
681
682
      ("invalid old elinfo\n");

683
    TEST_EXIT_DBG(elinfo_stack[stack_used]->getFillFlag().isSet(Mesh::FILL_NEIGH))
684
      ("FILL_NEIGH not set");
685
    Element *el = elinfo_stack[stack_used]->getElement();
686
687
688
689
690
    sav_index = el->getIndex();

    /* first, goto to leaf level, if necessary... */
    if ((traverse_fill_flag & Mesh::CALL_LEAF_EL).isAnySet()) {
      if ((el->getChild(0)) && (neighbour < 2)) {
691
	if (stack_used >= stack_size - 1)
692
693
	  enlargeTraverseStack();
	i = 1 - neighbour;
694
695
	elinfo_stack[stack_used + 1]->fillElInfo(i, elinfo_stack[stack_used]);
	info_stack[stack_used] = i + 1;
696
697
698
699
700
701
702
703
704
	stack_used++;
	info_stack[stack_used] = 0;
	neighbour = 3;
      }
    }


    /* save information about current element and its position in the tree */
    save_traverse_mel = traverse_mel;
705
    save_stack_used = stack_used;
706
707
708

    nb = neighbour;

709
710
711
712
713
714
715
716
    while (stack_used > 1) { /* go up in tree until we can go down again */
      stack_used--;
      typ = dynamic_cast<ElInfo3d*>( elinfo_stack[stack_used])->getType();
      nb = coarse_nb[typ][info_stack[stack_used]][nb];
      if (nb == -1) 
	break;
      TEST_EXIT_DBG(nb >= 0)("invalid coarse_nb %d\n",nb);
    }
717
718
719

    /* save hierarchy information about current element */

720
721
    for (i = stack_used; i <= save_stack_used; i++) {
      save_info_stack[i] = info_stack[i];
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
      *(save_elinfo_stack[i]) = *(elinfo_stack[i]);
    }

    old_elinfo = save_elinfo_stack[save_stack_used];
    opp_vertex = old_elinfo->getOppVertex(neighbour);

    if (nb >= 0) {                           /* go to macro element neighbour */

      i = traverse_mel->getOppVertex(nb);
      traverse_mel = traverse_mel->getNeighbour(nb);
      if (traverse_mel == NULL)  return(NULL);
    
      if ((nb < 2) && (save_stack_used > 1)) {
	stack2_used = 2;                /* go down one level in OLD hierarchy */
      }
      else {
	stack2_used = 1;
      }
      elinfo2 = save_elinfo_stack[stack2_used];
      el2 = elinfo2->getElement();

      stack_used = 1;
      elinfo_stack[stack_used]->fillMacroInfo(traverse_mel);
      info_stack[stack_used] = 0;
      nb = i;
Thomas Witkowski's avatar
Thomas Witkowski committed
747
    } else {                                                /* goto other child */
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
      stack2_used = stack_used + 1;
      if (save_stack_used > stack2_used) {
	stack2_used++;                  /* go down one level in OLD hierarchy */
      }
      elinfo2 = save_elinfo_stack[stack2_used];
      el2 = elinfo2->getElement();

      if (stack_used >= stack_size-1)
	enlargeTraverseStack();
      i = 2 - info_stack[stack_used];
      info_stack[stack_used] = i+1;
      elinfo_stack[stack_used+1]->fillElInfo(i, elinfo_stack[stack_used]);
      stack_used++;
      info_stack[stack_used] = 0;
      nb = 0;

    }


    elinfo = elinfo_stack[stack_used];
    el = elinfo->getElement();

    while(el->getChild(0)) {
 
      if (nb < 2) {                         /* go down one level in hierarchy */
	if (stack_used >= stack_size-1)
	  enlargeTraverseStack();
	info_stack[stack_used] = 2-nb;
Thomas Witkowski's avatar
Thomas Witkowski committed
776
	elinfo_stack[stack_used+1]->fillElInfo(1 - nb, elinfo_stack[stack_used]);
777
778
779
780
781
782
783
784
	stack_used++;
	info_stack[stack_used] = 0;
	elinfo = elinfo_stack[stack_used];
	el = elinfo->getElement();
	nb = 3;
      }

      if (save_stack_used > stack2_used) { /* `refine' both el and el2 */
785
	TEST_EXIT_DBG(el->getChild(0))("invalid NEW refinement?\n");
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827

	if (el->getDOF(0) == el2->getDOF(0))
	  i = save_info_stack[stack2_used] - 1;
	else if (el->getDOF(1) == el2->getDOF(0))
	  i = 2 - save_info_stack[stack2_used];
	else {
	  if (traverse_mesh->associated(el->getDOF(0, 0), el2->getDOF(0, 0)))
	    i = save_info_stack[stack2_used] - 1;
	  else if (traverse_mesh->associated(el->getDOF(1, 0), el2->getDOF(0, 0)))
	    i = 2 - save_info_stack[stack2_used];
	  else {
	    ERROR_EXIT("no common refinement edge\n");
	  }
	}

	if (el->getChild(0)  &&  
	    (el->getChild(i)->getDOF(1) == el->getDOF(nb) ||
	     traverse_mesh->associated(el->getChild(i)->getDOF(1, 0), el->getDOF(nb, 0)))) 
	  {   /*  el->child[0]  Kuni  22.08.96  */
	    nb = 1;
	  }
	else {
	  nb = 2;
	}

	info_stack[stack_used] = i+1;
	if (stack_used >= stack_size-1)
	  enlargeTraverseStack();
	elinfo_stack[stack_used+1]->fillElInfo(i, elinfo_stack[stack_used]);
	stack_used++;
	info_stack[stack_used] = 0;

	elinfo = elinfo_stack[stack_used];
	el = elinfo->getElement();

	stack2_used++;
	elinfo2 = save_elinfo_stack[stack2_used];
	el2 = elinfo2->getElement();
	if (save_stack_used > stack2_used) {
	  dof = el2->getDOF(1);
	  if (dof != el->getDOF(1) && dof != el->getDOF(2) &&
	      !traverse_mesh->associated(dof[0], el->getDOF(1, 0)) &&
828
829
830
831
832
833
	      !traverse_mesh->associated(dof[0], el->getDOF(2, 0))) {
	  
	    stack2_used++;               /* go down one level in OLD hierarchy */
	    elinfo2 = save_elinfo_stack[stack2_used];
	    el2 = elinfo2->getElement();
	  } 
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
	}

      }
      else {   /* now we're done... */
	elinfo = elinfo_stack[stack_used];
	el = elinfo->getElement();
	break;
      }
    }


    if (elinfo->getNeighbour(opp_vertex) != old_elinfo->getElement()) {
      MSG(" looking for neighbour %d of element %d at %p\n",
	  neighbour, old_elinfo->getElement()->getIndex(), reinterpret_cast<void*>( old_elinfo->getElement()));
      MSG(" originally: neighbour %d of element %d at %p\n",
	  sav_neighbour, sav_index, reinterpret_cast<void*>( old_elinfo->getElement()));
      MSG(" got element %d at %p with opp_vertex %d neigh %d\n",
	  elinfo->getElement()->getIndex(), reinterpret_cast<void*>( elinfo->getElement()),
	  opp_vertex, (elinfo->getNeighbour(opp_vertex))?(elinfo->getNeighbour(opp_vertex))->getIndex():-1);
853
      TEST_EXIT_DBG(elinfo->getNeighbour(opp_vertex) == old_elinfo->getElement())
854
855
856
857
858
859
860
861
862
	("didn't succeed !?!?!?\n");
    }


    if ((traverse_fill_flag & Mesh::CALL_EVERY_EL_POSTORDER).isAnySet())
      info_stack[stack_used] = 3;
    else if ((traverse_fill_flag & Mesh::CALL_EVERY_EL_INORDER).isAnySet())
      info_stack[stack_used] = 1;  /* ??? */

863
864
865
    if (elinfo) {
      if (parametric) 
	elinfo = parametric->addParametricInfo(elinfo);
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
      elinfo->fillDetGrdLambda();
    }
    return(elinfo);
  }

  ElInfo* TraverseStack::traverseNeighbour2d(ElInfo* elinfo_old, int neighbour)
  {
    FUNCNAME("TraverseStack::traverseNeighbour2d");
    Triangle   *el, *el2, *sav_el;
    ElInfo *old_elinfo, *elinfo, *elinfo2;
    int     i, nb, opp_vertex, stack2_used;

    static int coarse_nb[3][3] = {{-2,-2,-2}, {2,-1,1}, {-1,2,0}};
    /* father.neigh[coarse_nb[i][j]] == child[i-1].neigh[j] */

    int     sav_index, sav_neighbour = neighbour;

883
884
    TEST_EXIT_DBG(stack_used > 0)("no current element");
    TEST_EXIT_DBG(traverse_fill_flag.isSet(Mesh::CALL_LEAF_EL))("invalid traverse_fill_flag");
885
886
887
888
889
890

    Parametric *parametric = traverse_mesh->getParametric();

    if (parametric) 
      elinfo_old = parametric->removeParametricInfo(elinfo_old);

891
    TEST_EXIT_DBG(elinfo_old == elinfo_stack[stack_used])("invalid old elinfo");
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932

    elinfo_stack[stack_used]->testFlag(Mesh::FILL_NEIGH);
    el = dynamic_cast<Triangle*>(const_cast<Element*>( elinfo_stack[stack_used]->getElement()));
    sav_index = el->getIndex();
    sav_el    = el;

    /* first, goto to leaf level, if necessary... */
    if (!(el->isLeaf()) && (neighbour < 2)) {
      if (stack_used >= static_cast<int>( elinfo_stack.size())-1) enlargeTraverseStack();
      i = 1 - neighbour;
      elinfo_stack[stack_used+1]->fillElInfo(i, elinfo_stack[stack_used]);
      info_stack[stack_used] = i + 1;
      stack_used++;
      neighbour = 2;
    }

    /* save information about current element and its position in the tree */
    save_traverse_mel = traverse_mel;
    save_stack_used   = stack_used;
    for (i=0; i<=stack_used; i++)
      save_info_stack[i] = info_stack[i];
    for (i=0; i<=stack_used; i++)
      (*(save_elinfo_stack[i])) = (*(elinfo_stack[i]));
    old_elinfo = save_elinfo_stack[stack_used];
    opp_vertex = old_elinfo->getOppVertex(neighbour);


    /****************************************************************************/
    /* First phase: go up in tree until we can go down again.                   */
    /*                                                                          */
    /* During this first phase, nb is the neighbour index which points from an  */
    /* element of the OLD hierarchy branch to the NEW branch                    */
    /****************************************************************************/

    nb = neighbour;

    while (stack_used > 1)
      {
	stack_used--;
	nb = coarse_nb[info_stack[stack_used]][nb];
	if (nb == -1) break;
933
	TEST_EXIT_DBG(nb >= 0)("invalid coarse_nb %d\n",nb);
934
935
936
937
938
939
940
941
942
943
944
945
      }

    /****************************************************************************/
    /* Now, goto neighbouring element at the local hierarchy entry              */
    /* This is either a macro element neighbour or the other child of parent.   */
    /* initialize nb for second phase (see below)                               */
    /****************************************************************************/

    if (nb >= 0) {                        /* go to macro element neighbour */

      if ((nb < 2) && (save_stack_used > 1)) {
	stack2_used = 2;           /* go down one level in OLD hierarchy */
946
      } else {
947
948
949
950
951
952
953
	stack2_used = 1;
      }
      elinfo2 = save_elinfo_stack[stack2_used];
      el2 = dynamic_cast<Triangle*>(const_cast<Element*>( elinfo2->getElement()));

      i = traverse_mel->getOppVertex(nb);
      traverse_mel = traverse_mel->getNeighbour(nb);
954
955
      if (traverse_mel == NULL)
	return NULL;
956
957
958
959
960
961
      nb = i;
    
      stack_used = 1;
      elinfo_stack[stack_used]->fillMacroInfo(const_cast<MacroElement*>( traverse_mel));
      info_stack[stack_used] = 0;
    
962
    } else {                                               /* goto other child */
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

      stack2_used = stack_used + 1;
      if (save_stack_used > stack2_used) {
	stack2_used++;               /* go down one level in OLD hierarchy */
      }
      elinfo2 = save_elinfo_stack[stack2_used];
      el2 = dynamic_cast<Triangle*>(const_cast<Element*>( elinfo2->getElement()));


      if (stack_used >= stack_size-1)
	enlargeTraverseStack();
      i = 2 - info_stack[stack_used];
      info_stack[stack_used] = i+1;
      elinfo_stack[stack_used+1]->fillElInfo(i, elinfo_stack[stack_used]);
      stack_used++;
      nb = 1-i;
    }

    /****************************************************************************/
    /* Second phase: go down in a new hierarchy branch until leaf level.        */
    /* Now, nb is the neighbour index which points from an element of the       */
    /* NEW hierarchy branch to the OLD branch.                                  */
    /****************************************************************************/

    elinfo = elinfo_stack[stack_used];
    el = dynamic_cast<Triangle*>(const_cast<Element*>( elinfo->getElement()));

    while(el->getFirstChild()) {

      if (nb < 2) {   /* go down one level in hierarchy */
	if (stack_used >= stack_size-1)
	  enlargeTraverseStack();
	elinfo_stack[stack_used+1]->fillElInfo(1-nb, elinfo_stack[stack_used]);
	info_stack[stack_used] = 2-nb;
	stack_used++;
	nb = 2;
      }