DualTraverse.cc 4.33 KB
Newer Older
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
#include "DualTraverse.h"
#include "Mesh.h"
#include "ElInfo.h"

namespace AMDiS {

  bool DualTraverse::traverseFirst(Mesh    *mesh1, 
				   Mesh    *mesh2, 
				   int      level1, 
				   int      level2, 
				   Flag     flag1,
				   Flag     flag2,
				   ElInfo **elInfo1,
				   ElInfo **elInfo2,
				   ElInfo **elInfoSmall,
				   ElInfo **elInfoLarge)
  {
    // replace CALL_EL_LEVEL by CALL_MG_LEVEL (covers whole domain)
    if(flag1.isSet(Mesh::CALL_EL_LEVEL)) {
      flag1 &= ~Mesh::CALL_EL_LEVEL;
      flag1 |= Mesh::CALL_MG_LEVEL;
      level1_ = level1;
    } else {
      level1_ = -1;
    }

    if(flag2.isSet(Mesh::CALL_EL_LEVEL)) {
      flag2 &= ~Mesh::CALL_EL_LEVEL;
      flag2 |= Mesh::CALL_MG_LEVEL;
      level2_ = level2;
    } else {
      level2_ = -1;
    }

    // replace CALL_LEAF_EL_LEVEL by CALL_MG_LEVEL (covers whole domain)
    if(flag1.isSet(Mesh::CALL_LEAF_EL_LEVEL)) {
      flag1 &= ~Mesh::CALL_LEAF_EL_LEVEL;
      flag1 |= Mesh::CALL_MG_LEVEL;
      level1_ = level1;
      callLeafElLevel1_ = true;
    } else {
      level1_ = -1;
      callLeafElLevel1_ = false;
    }

    if(flag2.isSet(Mesh::CALL_LEAF_EL_LEVEL)) {
      flag2 &= ~Mesh::CALL_LEAF_EL_LEVEL;
      flag2 |= Mesh::CALL_MG_LEVEL;
      level2_ = level2;
      callLeafElLevel2_ = true;
    } else {
      level2_ = -1;
      callLeafElLevel2_ = false;
    }

    // call standard traverse
    *elInfo1 = stack1.traverseFirst(mesh1, level1, flag1);
    while(*elInfo1 != NULL && skipEl1(*elInfo1)) 
      *elInfo1 = stack1.traverseNext(*elInfo1);
    *elInfo2 = stack2.traverseFirst(mesh2, level2, flag2);
    while(*elInfo2 != NULL && skipEl2(*elInfo2)) 
      *elInfo2 = stack2.traverseNext(*elInfo2);

    // finished ?
65
66
    if (*elInfo1 == NULL || *elInfo2 == NULL) {
      TEST_EXIT_DBG(*elInfo1 == *elInfo2)("invalid dual traverse\n");
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
      return false;
    }

    rest_ = 1.0;

    bool accepted = check(elInfo1, elInfo2, 
			  elInfoSmall, elInfoLarge);

    // check for non domain covering level traverse
    if(!accepted ||
       (level1_ != -1 && (*elInfo1)->getLevel() != level1_) ||
       (callLeafElLevel1_ && !(*elInfo1)->getElement()->isLeaf()) ||
       (level2_ != -1 && (*elInfo2)->getLevel() != level2_) ||
       (callLeafElLevel2_ && !(*elInfo2)->getElement()->isLeaf())) 
      { 
	return traverseNext(elInfo1, elInfo2, elInfoSmall, elInfoLarge);
      }

    return true;
  }

  bool DualTraverse::traverseNext(ElInfo **elInfo1,
				  ElInfo **elInfo2,
				  ElInfo **elInfoSmall,
				  ElInfo **elInfoLarge)
  {
    // call standard traverse
    if(inc1_) {
      do {
	*elInfo1 = stack1.traverseNext(*elInfo1);
      } while(*elInfo1 != NULL && skipEl1(*elInfo1));
    }
    if(inc2_) {
      do {
	*elInfo2 = stack2.traverseNext(*elInfo2);
      } while (*elInfo2 != NULL && skipEl2(*elInfo2));
    }

    // finished ?
106
107
    if (*elInfo1 == NULL || *elInfo2 == NULL) {
      TEST_EXIT_DBG(*elInfo1 == *elInfo2)("invalid dual traverse\n");
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
      return false;
    }

    bool accepted = check(elInfo1, elInfo2, 
			  elInfoSmall, elInfoLarge);

    // check for non domain covering level traverse
    if(!accepted ||
       (level1_ != -1 && (*elInfo1)->getLevel() != level1_) ||
       (callLeafElLevel1_ && !(*elInfo1)->getElement()->isLeaf()) ||
       (level2_ != -1 && (*elInfo2)->getLevel() != level2_) ||
       (callLeafElLevel2_ && !(*elInfo2)->getElement()->isLeaf())) 
      { 
	return traverseNext(elInfo1, elInfo2, elInfoSmall, elInfoLarge);
      }

    return true;
  }

  void DualTraverse::prepareNextStep(ElInfo **elInfo1,
				     ElInfo **elInfo2,
				     ElInfo **elInfoSmall,
				     ElInfo **elInfoLarge)
  {
    // which is the smaller element ?
    *elInfoSmall = 
      (*elInfo1)->getLevel() > (*elInfo2)->getLevel() ?
      *elInfo1 :
      *elInfo2;
    *elInfoLarge = 
      (*elInfo1)->getLevel() <= (*elInfo2)->getLevel() ?
      *elInfo1 :
      *elInfo2;

    int smallLevel = (*elInfoSmall)->getLevel();
    int largeLevel = (*elInfoLarge)->getLevel();

    // update rest
    rest_ -= 1.0 / (1 << (smallLevel - largeLevel));

    if(rest_ < 1e-32) {
      // large element finished -> increment both elements
      rest_ = 1.0; 
      inc1_ = true;
      inc2_ = true;
    } else {
      // increment only small element
      inc1_ = (*elInfo1 == *elInfoSmall) ? true : false;
      inc2_ = (*elInfo2 == *elInfoSmall) ? true : false;
    }
  }

}