ElInfo2d.cc 28.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "ElInfo2d.h"
#include "BasisFunction.h"
#include "Element.h"
#include "Line.h"
#include "Triangle.h"
#include "Tetrahedron.h"
#include "FiniteElemSpace.h"
#include "Flag.h"
#include "MacroElement.h"
#include "Mesh.h"
#include "Global.h"
#include "FixVec.h"
#include "DOFVector.h"

namespace AMDiS {

29
  double ElInfo2d::mat_d1_left_val[3][3] = {{0.0, 1.0, 0.5}, 
30
  					    {0.0, 0.0, 0.5},
31
  					    {1.0, 0.0, 0.0}}; 
32
33
  mtl::dense2D<double> ElInfo2d::mat_d1_left(mat_d1_left_val);

34
  
35
  double ElInfo2d::mat_d1_right_val[3][3] = {{0.0, 0.0, 0.5}, 
36
  					     {1.0, 0.0, 0.5},
37
  					     {0.0, 1.0, 0.0}}; 
38
39
  mtl::dense2D<double> ElInfo2d::mat_d1_right(mat_d1_right_val);

40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

  double ElInfo2d::mat_d2_left_val[6][6] = {{0.0, 1.0, 0.0, 0.375, -0.125, 0.0}, 
					    {0.0, 0.0, 0.0, -0.125, -0.125, 0.0}, 
					    {1.0, 0.0, 0.0, 0.0, 0.0, 0.0},
					    {0.0, 0.0, 0.0, 0.0, 0.5, 0.0},
					    {0.0, 0.0, 0.0, 0.0, 0.5, 1.0},
					    {0.0, 0.0, 1.0, 0.75, 0.25, 0.0}};
  mtl::dense2D<double> ElInfo2d::mat_d2_left(mat_d2_left_val);

  double ElInfo2d::mat_d2_right_val[6][6] = {{0.0, 0.0, 0.0, -0.125, -0.125, 0.0}, 
					     {1.0, 0.0, 0.0, -0.125, 0.375, 0.0}, 
					     {0.0, 1.0, 0.0, 0.0, 0.0, 0.0},
					     {0.0, 0.0, 0.0, 0.5, 0.0, 1.0},
					     {0.0, 0.0, 0.0, 0.5, 0.0, 0.0},
					     {0.0, 0.0, 1.0, 0.25, 0.75, 0.0}};
  mtl::dense2D<double> ElInfo2d::mat_d2_right(mat_d2_right_val);


59

60
61
  double ElInfo2d::mat_d3_left_val[10][10] = {{0.0, 1.0, -0.0625, 0.3125, 0.0, 0.0, 0.0625, 0.0, 0.0, -0.0625},
					      {0.0, 0.0, -0.0625, 0.0625, 0.0, 0.0, 0.0625, 0.0, 0.0, 0.0625},
62
					      {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
63
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.25, 0.0, 0.0, -0.125},
64
65
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 0.0, 0.0},
66
67
68
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.25, 0.0, 1.0, 0.375},
					      {0.0, 0.0, 0.5625, 0.9375, 1.0, 0.0, -0.0625, 0.0, 0.0, 0.1875},
					      {0.0, 0.0, 0.5625, -0.3125, 0.0, 0.0, -0.0625, 0.0, 0.0, 0.0},
69
70
71
					      {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.5, 0.0, 0.0, 0.75}};
  mtl::dense2D<double> ElInfo2d::mat_d3_left(mat_d3_left_val);

72
73
  double ElInfo2d::mat_d3_right_val[10][10] = {{0.0, 0.0, -0.0625, 0.0625, 0.0, 0.0, 0.0625, 0.0, 0.0, 0.0625},
					       {1.0, 0.0, -0.0625, 0.0625, 0.0, 0.0, 0.3125, 0.0, 0.0, -0.0625},
74
					       {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
75
					       {0.0, 0.0, 0.0, -0.25, 0.0, 0.0, 0.0, 1.0, 0.0, 0.375},
76
77
					       {0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0},
					       {0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
78
79
80
					       {0.0, 0.0, 0.0, -0.25, 0.0, 0.0, 0.0, 0.0, 0.0, -0.125},
					       {0.0, 0.0, 0.5625, -0.0625, 0.0, 0.0, -0.3125, 0.0, 0.0, -0.1875},
					       {0.0, 0.0, 0.5625, -0.0625, 0.0, 1.0, 0.9375, 0.0, 0.0, 0.1875},
81
82
83
84
85
					       {0.0, 0.0, 0.0, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.75}};
  mtl::dense2D<double> ElInfo2d::mat_d3_right(mat_d3_right_val);



86
87
  double ElInfo2d::mat_d4_left_val[15][15] = {{0.0, 1.0, 0.0, 2.734375e-01, 0.0, -3.90625e-02, 2.34375e-02, 0.0, -3.90625e-02, 0.0, 0.0, 0.0, 2.34375e-02, -3.90625e-02, 0.0},
					      {0.0, 0.0, 0.0, -3.90625e-02, 0.0, 2.34375e-02, 2.34375e-02, 0.0, -3.90625e-02, 0.0, 0.0, 0.0, -3.90625e-02, -3.90625e-02, 0.0},
88
					      {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
89
90
91
92
93
94
95
96
97
98
99
100
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -6.25e-02, 0.0, 1.875e-01, 0.0, 0.0, 0.0, 1.25e-01, 6.25e-02, 0.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3.75e-01, 0.0, 0.0, 0.0, -1.25e-01, 0.0, 0.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.e-01, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.e-01, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3.75e-01, 0.0, 1.0, 0.0, 3.75e-01, 0.0, 0.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -6.25e-02, 0.0, 1.875e-01, 0.0, 0.0, 1.0, -1.25e-01, 3.125e-01, 0.0},
					      {0.0, 0.0, 0.0, 1.09375e+00, 1.0, 4.6875e-01, -9.375e-02, 0.0, 3.125e-02, 0.0, 0.0, 0.0, -3.125e-02, 1.5625e-01, 0.0},
					      {0.0, 0.0, 1.0, -5.46875e-01, 0.0, 7.03125e-01, 1.40625e-01, 0.0, 1.5625e-02, 0.0, 0.0, 0.0, -4.6875e-02, -2.34375e-01, 0.0},
					      {0.0, 0.0, 0.0, 2.1875e-01, 0.0, -1.5625e-01, -9.375e-02, 0.0, 3.125e-02, 0.0, 0.0, 0.0, 9.375e-02, 1.5625e-01, 0.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.625e-01, 0.0, -1.875e-01, 0.0, 0.0, 0.0, 3.75e-01, 9.375e-01, 1.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.625e-01, 0.0, -1.875e-01, 0.0, 0.0, 0.0, -3.75e-01, -3.125e-01, 0.0},
					      {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 7.5e-01, 0.0, 0.0, 0.0, 7.5e-01, 0.0, 0.0}};
101
102
  mtl::dense2D<double> ElInfo2d::mat_d4_left(mat_d4_left_val);

103
104
  double ElInfo2d::mat_d4_right_val[15][15] = {{0.0, 0.0, 0.0, -3.90625e-02, 0.0, 2.34375e-02, 2.34375e-02, 0.0, -3.90625e-02, 0.0, 0.0, 0.0, -3.90625e-02, -3.90625e-02, 0.0},
					       {1.0, 0.0, 0.0, -3.90625e-02, 0.0, 2.34375e-02, -3.90625e-02, 0.0, 2.734375e-01, 0.0, 0.0, 0.0, -3.90625e-02, 2.34375e-02, 0.0},
105
					       {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
106
107
108
109
110
111
112
113
114
115
116
117
					       {0.0, 0.0, 0.0, 1.875e-01, 0.0, -6.25e-02, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 3.125e-01, -1.25e-01, 0.0},
					       {0.0, 0.0, 0.0, -3.75e-01, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 3.75e-01, 0.0},
					       {0.0, 0.0, 0.0, 5.0e-01, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0},
					       {0.0, 0.0, 0.0, 5.0e-01, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
					       {0.0, 0.0, 0.0, -3.75e-01, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.25e-01, 0.0},
					       {0.0, 0.0, 0.0, 1.875e-01, 0.0, -6.25e-02, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6.25e-02, 1.25e-01, 0.0},
					       {0.0, 0.0, 0.0, 3.125e-02, 0.0, -9.375e-02, -1.5625e-01, 0.0, 2.1875e-01, 0.0, 0.0, 0.0, 1.5625e-01, 9.375e-02, 0.0},
					       {0.0, 0.0, 1.0, 1.5625e-02, 0.0, 1.40625e-01, 7.03125e-01, 0.0, -5.46875e-01, 0.0, 0.0, 0.0, -2.34375e-01, -4.6875e-02, 0.0},
					       {0.0, 0.0, 0.0, 3.125e-02, 0.0, -9.375e-02, 4.6875e-01, 1.0, 1.09375e+00, 0.0, 0.0, 0.0, 1.5625e-01, -3.125e-02, 0.0},
					       {0.0, 0.0, 0.0, -1.875e-01, 0.0, 5.625e-01, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3.125e-01, -3.75e-01, 0.0},
					       {0.0, 0.0, 0.0, -1.875e-01, 0.0, 5.625e-01, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 9.375e-01, 3.75e-01, 1.0},
					       {0.0, 0.0, 0.0, 7.5e-01, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.5e-01, 0.0}};
118
119
120
  mtl::dense2D<double> ElInfo2d::mat_d4_right(mat_d4_right_val);


121
122
  ElInfo2d::ElInfo2d(Mesh *aMesh) 
    : ElInfo(aMesh) 
123
  {}
124

125

126
  ElInfo2d::~ElInfo2d()
127
  {}
128

129

130
131
  void ElInfo2d::fillMacroInfo(const MacroElement * mel)
  {
132
133
    FUNCNAME("ElInfo::fillMacroInfo()");
 
Thomas Witkowski's avatar
Thomas Witkowski committed
134
135
136
    macroElement = const_cast<MacroElement*>(mel);
    element = const_cast<Element*>(mel->getElement());
    parent = NULL;
137
    level = 0;
138

Thomas Witkowski's avatar
Thomas Witkowski committed
139
140
141
    if (fillFlag.isSet(Mesh::FILL_COORDS) || 
	fillFlag.isSet(Mesh::FILL_DET)    ||
	fillFlag.isSet(Mesh::FILL_GRD_LAMBDA)) {
142

Thomas Witkowski's avatar
Thomas Witkowski committed
143
      int vertices = mesh->getGeo(VERTEX);
144
      for (int i = 0; i < vertices; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
145
	coord[i] = mel->coord[i];
146
147
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
148
    int neighbours = mesh->getGeo(NEIGH);
149

Thomas Witkowski's avatar
Thomas Witkowski committed
150
151
    if (fillFlag.isSet(Mesh::FILL_OPP_COORDS) || 
	fillFlag.isSet(Mesh::FILL_NEIGH)) {
152

Thomas Witkowski's avatar
Thomas Witkowski committed
153
      bool fill_opp_coords = (fillFlag.isSet(Mesh::FILL_OPP_COORDS));
154
    
155
156
157
158
      for (int i = 0; i < neighbours; i++) {
	MacroElement *macroNeighbour = mel->getNeighbour(i);

	if (macroNeighbour) {
Thomas Witkowski's avatar
Thomas Witkowski committed
159
160
	  neighbour[i] = macroNeighbour->getElement();	  
	  Element *nb = const_cast<Element*>(neighbour[i]);
161

162
	  int edgeNo = oppVertex[i] = mel->getOppVertex(i);
163
164


165
	  if (nb->getFirstChild() && edgeNo != 2) {  
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
            /*
	    * Search for the next neighbour. In many cases, the neighbour element 
	    * may be refinemed in a way, such that there is no new vertex on the 
	    * common edge. This situation is shown in the following picture: 
	    *
	    *               /|\
	    *              / | \
	    *             /  |  \
	    *            /\  |   \
	    *           /  \ |    \ 
	    *          /    \|     \
	    *          -------------
	    *
	    *            nb     el
	    *
	    * Note that we know (because of the last if statement), that the 
	    * neighbour element has children and the common edge is not the 
	    * refinement edge, which has always the number 2, of our element.
	    */
185

186
	    if (edgeNo == 0) {
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
	      /*	
	      * The situation is as follows:
	      *
	      *          -------
	      *          \    /|\
	      *           \  / | \
	      *            \/  |  \
	      *             \  |   \
	      *              \ |    \ 
	      *               \|     \
	      *                -------
	      *
	      *            nb     el
              *
	      * That means, the edge 0 of the same level neighbour is the common
	      * edge, i.e., the direct neighbour is the second child of the same
	      * level neighbour.
	      */
Thomas Witkowski's avatar
Thomas Witkowski committed
205
	      nb = neighbour[i] = nb->getSecondChild();
206
	    } else {
207
208
	      // The situation is as shown in the picture above. So the next
	      // neighbour is the first child of the same level neighbour element.
Thomas Witkowski's avatar
Thomas Witkowski committed
209
	      nb = neighbour[i] = nb->getFirstChild();
210
211
	    }

212
213
	    // In both cases the opp vertex number is 2, as one can see in the 
	    // pictures above.
214
	    oppVertex[i] = 2;
215
216

	    if (fill_opp_coords) {
217
	      if (nb->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
218
		oppCoord[i] = *(nb->getNewCoord());
219
	      } else {
220
221
222
		// In both cases, that are shown in the pictures above, the opp
		// vertex of the neighbour edge is the midpoint of the vertex 0
		// and vertex 1 of the same level neighbour element.
Thomas Witkowski's avatar
Thomas Witkowski committed
223
		oppCoord[i] = (macroNeighbour->coord[0] + 
224
225
				macroNeighbour->coord[1]) * 0.5;
	      }
226
227
228
	      
	      switch (i) {
	      case 0:
229
230
231
232
		// The common edge is the edge 0 of this element.

		switch (edgeNo) {
		case 1:
233
234
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[0];
235
236
		  break;
		case 0:		  
237
238
		  neighbourCoord[i][0] = macroNeighbour->coord[1];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];
239
240
		  break;
		default:
Thomas Witkowski's avatar
Thomas Witkowski committed
241
		  ERROR_EXIT("Should not happen!\n");
242
		}
243
	
Thomas Witkowski's avatar
Thomas Witkowski committed
244
		neighbourCoord[i][2] = oppCoord[i];
245
246
247
		break;
		
	      case 1:
Thomas Witkowski's avatar
a  
Thomas Witkowski committed
248
		// The common edge is the edge 1 of this element.
249
250
		switch (edgeNo) {
		case 0:
251
252
		  neighbourCoord[i][0] = macroNeighbour->coord[1];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];
253
254
		  break;
		case 1:
255
256
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[0];
257
258
		  break;
		default:
Thomas Witkowski's avatar
Thomas Witkowski committed
259
		  ERROR_EXIT("Should not happen!\n");
260
261
		}
		
Thomas Witkowski's avatar
Thomas Witkowski committed
262
		neighbourCoord[i][2] = oppCoord[i];
263
264
		break;
		
Thomas Witkowski's avatar
Thomas Witkowski committed
265
	      case 2:
266
		if (*(macroNeighbour->getElement()->getDof(2)) == *(element->getDof(0))) {
267
268
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[1];
269
		} else if (*(macroNeighbour->getElement()->getDof(2)) == *(element->getDof(1))) {
270
271
		  neighbourCoord[i][0] = macroNeighbour->coord[0];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];		 
272
		} else {
273
		  ERROR_EXIT("Should not happen! Non-conforming AMDiS-mesh? Periodic mesh with corrected index-circles?\n");
274
275
		}

276
277
278
		// I've deleted here some code, be I think that this case is not
		// possible. If an error occurs in this line, please check AMDiS
		// revision <= 476 at the same position.
279
280
		//		ERROR_EXIT("Should not happen!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
281
282
		break;

283
	      default:
Thomas Witkowski's avatar
Thomas Witkowski committed
284
		std::cout << "------------- Error --------------" << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
285
		std::cout << "  Neighbour counter = " << i << "\n";
Thomas Witkowski's avatar
Thomas Witkowski committed
286
		std::cout << "  Element index     = " << element->getIndex() << "\n\n";
Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
289
290
291
292
293
294
		for (int j = 0; j < neighbours; j++) {
		  if (mel->getNeighbour(j)) {
		    std::cout << "  Neighbour " << j << ": " 
			      << mel->getNeighbour(j)->getElement()->getIndex() 
			      << std::endl;
		  } else {
		    std::cout << "  Neighbour " << j << ": not existing" << std::endl;
		  }
295
296
297
		  std::cout << "  OppVertex " << j << ": " 
			    << static_cast<int>(mel->getOppVertex(j)) 
			    << std::endl << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
298
		}
299
300
		ERROR_EXIT("should not happen!\n");
		break;
301
302
303
	      }
	    }
	  } else {
304
305
306
307
308
309
310

	    // In this case, we know that the common edge is the refinement edge.
	    // This makes everything much more simpler, because we know that the
	    // next neighbour is equal to the samel level neighbour. If the same
	    // level neighbour would be refinement, also this element must to be 
	    // refinement, because they share the refinement edge.

311
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
312
	      oppCoord[i] = macroNeighbour->coord[edgeNo];
313
	      neighbourCoord[i] = macroNeighbour->coord;	      
314
	    }
315
	  }
316
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
317
	  neighbour[i] = NULL;
318
        }
319
      }
320
    }
321
    
Thomas Witkowski's avatar
Thomas Witkowski committed
322
323
324
325
326
    if (fillFlag.isSet(Mesh::FILL_BOUND)) {   
      for (int i = 0; i < element->getGeo(BOUNDARY); i++)
	boundary[i] = mel->getBoundary(i);
      for (int i = 0; i < element->getGeo(PROJECTION); i++)
	projection[i] = mel->getProjection(i);      
327
328
329
330
331
332
333
334
    }
  }


  /****************************************************************************/
  /*   fill ElInfo structure for one child of an element   		    */
  /****************************************************************************/

335
  void ElInfo2d::fillElInfo(int ichild, const ElInfo *elInfoOld)
336
  {
337
    FUNCNAME("ElInfo::fillElInfo()");
338

Thomas Witkowski's avatar
Thomas Witkowski committed
339
340
    Element *elem = elInfoOld->element;
    Flag fill_flag = elInfoOld->fillFlag;
341

342
    TEST_EXIT_DBG(elem->getFirstChild())("no children?\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
343
    element = const_cast<Element*>((ichild == 0) ? 
344
345
				    elem->getFirstChild() : 
				    elem->getSecondChild());
Thomas Witkowski's avatar
Thomas Witkowski committed
346
    TEST_EXIT_DBG(element)("missing child %d?\n", ichild);
347

Thomas Witkowski's avatar
Thomas Witkowski committed
348
349
350
    macroElement  = elInfoOld->macroElement;
    fillFlag = fill_flag;
    parent = elem;
351
    level = elInfoOld->level + 1;
352
    iChild = ichild;
353

Thomas Witkowski's avatar
Thomas Witkowski committed
354
355
356
    if (fillFlag.isSet(Mesh::FILL_COORDS) || 
	fillFlag.isSet(Mesh::FILL_DET)    ||
	fillFlag.isSet(Mesh::FILL_GRD_LAMBDA)) {
357
      
358
      if (elem->isNewCoordSet())
Thomas Witkowski's avatar
Thomas Witkowski committed
359
	coord[2] = *(elem->getNewCoord());
360
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
361
	coord[2].setMidpoint(elInfoOld->coord[0], elInfoOld->coord[1]);      
362
363
      
      if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
364
365
	coord[0] = elInfoOld->coord[2];
	coord[1] = elInfoOld->coord[0];
366
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
367
368
	coord[0] = elInfoOld->coord[1];
	coord[1] = elInfoOld->coord[2];
369
370
371
372
373
374
375
376
377
378
      }
    }

    bool fill_opp_coords = (fill_flag.isSet(Mesh::FILL_OPP_COORDS));

    if (fill_flag.isSet(Mesh::FILL_NEIGH) || fill_opp_coords) {     
      if (ichild == 0) {
	// Calculation of the neighbour 2, its oppCoords and the
	// cooresponding oppVertex.

Thomas Witkowski's avatar
Thomas Witkowski committed
379
	neighbour[2] = elInfoOld->neighbour[1];
380
	oppVertex[2] = elInfoOld->oppVertex[1];
381
	
Thomas Witkowski's avatar
Thomas Witkowski committed
382
383
	if (neighbour[2] && fill_opp_coords) {
	  oppCoord[2] = elInfoOld->oppCoord[1];
384
	  neighbourCoord[2] = elInfoOld->neighbourCoord[1];
385
	}
386
387
388
389
390
391
392
393
394
	
	
	// Calculation of the neighbour 1, its oppCoords and the
	// cooresponding oppVertex.
	
	if (elem->getFirstChild()  &&  
	    elem->getSecondChild()->getFirstChild()  &&  
	    elem->getSecondChild()->getFirstChild()) {
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
395
	  neighbour[1] = elem->getSecondChild()->getSecondChild();
396
	  oppVertex[1] = 2;
397
398
	  
	  if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
399
400
401
402
403
404
405
406
            if (elem->getSecondChild()->isNewCoordSet())
	      oppCoord[1] = *(elem->getSecondChild()->getNewCoord());
	    else
	      oppCoord[1].setMidpoint(elInfoOld->coord[1], elInfoOld->coord[2]);

	    neighbourCoord[1][0] = coord[0];
	    neighbourCoord[1][1] = coord[2];
	    neighbourCoord[1][2] = oppCoord[1];  
407
408
	  }
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
409
	  neighbour[1] = elem->getSecondChild();
410
	  oppVertex[1] = 0;
411
412

	  if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
413
	    oppCoord[1] = elInfoOld->coord[1];
414

Thomas Witkowski's avatar
Thomas Witkowski committed
415
416
417
	    neighbourCoord[1][0] = elInfoOld->coord[1];
	    neighbourCoord[1][1] = elInfoOld->coord[2];
	    neighbourCoord[1][2] = coord[2];
418
419
420
421
	  }
	}


422
423
424
	// Calculation of the neighbour 0, its oppCoords and the
	// cooresponding oppVertex.
	
425
	Element *nb = elInfoOld->neighbour[2];
426
	if (nb) {
427
428
429
430
431
432
433
434
435
436
437
	  TEST_EXIT_DBG(elInfoOld->oppVertex[2] == 2)
	    ("Fill child %d of element %d (mel %d): Invalid neighbour %d!\n",
	     ichild,
	     elInfoOld->getElement()->getIndex(), 
	     elInfoOld->getMacroElement()->getIndex(),
	     nb->getIndex());

	  TEST_EXIT_DBG(nb->getFirstChild())
	    ("Missing first child in element %d!\n", nb->getIndex());	  
	  TEST_EXIT_DBG(nb->getSecondChild())
	    ("Missing second child in element %d!\n", nb->getIndex());
438
439
440
441
	 
	  nb = nb->getSecondChild();

	  if (nb->getFirstChild()) {
442
	    oppVertex[0] = 2;
443

444
	    if (fill_opp_coords) {
445
	      if (nb->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
446
		oppCoord[0] = *(nb->getNewCoord());
447
	      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
448
		oppCoord[0].setMidpoint(elInfoOld->neighbourCoord[2][1],
449
					 elInfoOld->neighbourCoord[2][2]);
450
	      }
451

452
453
454
	      neighbourCoord[0][0].setMidpoint(elInfoOld->neighbourCoord[2][0],
						elInfoOld->neighbourCoord[2][1]);
	      neighbourCoord[0][1] = elInfoOld->neighbourCoord[2][1];
Thomas Witkowski's avatar
Thomas Witkowski committed
455
	      neighbourCoord[0][2] = oppCoord[0];
456
457
458
459
	    }	   
 
	    nb = nb->getFirstChild();
	  } else {
460
	    oppVertex[0] = 1;
461

462
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
463
	      oppCoord[0] = elInfoOld->oppCoord[2];    
464

465
466
467
468
	      neighbourCoord[0][0] = elInfoOld->neighbourCoord[2][0];
	      neighbourCoord[0][1] = elInfoOld->neighbourCoord[2][2];
	      neighbourCoord[0][2].setMidpoint(elInfoOld->neighbourCoord[2][0],
						elInfoOld->neighbourCoord[2][1]);
469
470
	    }
	  }
471
472
	}
	
Thomas Witkowski's avatar
Thomas Witkowski committed
473
	neighbour[0] = nb;
474
475
476
477
      } else {   /* ichild == 1 */
	// Calculation of the neighbour 2, its oppCoords and the
	// cooresponding oppVertex.

Thomas Witkowski's avatar
Thomas Witkowski committed
478
	neighbour[2] = elInfoOld->neighbour[0];
479
	oppVertex[2] = elInfoOld->oppVertex[0];
480

Thomas Witkowski's avatar
Thomas Witkowski committed
481
482
	if (neighbour[2] && fill_opp_coords) {
	  oppCoord[2] = elInfoOld->oppCoord[0];
483
	  neighbourCoord[2] = elInfoOld->neighbourCoord[0];
484
485
486
487
488
489
490
	}
	

	// Calculation of the neighbour 0, its oppCoords and the
	// cooresponding oppVertex.

	if (elem->getFirstChild()->getFirstChild()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
491
	  neighbour[0] = elem->getFirstChild()->getFirstChild();
492
	  oppVertex[0] = 2;
493
494

	  if (fill_opp_coords) {
495
            if (elem->getFirstChild()->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
496
	      oppCoord[0] = *(elem->getFirstChild()->getNewCoord());
497
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
498
499
	      oppCoord[0].setMidpoint(elInfoOld->coord[0], 
				       elInfoOld->coord[2]);
500
	    }
501

Thomas Witkowski's avatar
Thomas Witkowski committed
502
503
504
	    neighbourCoord[0][0] = coord[2];
	    neighbourCoord[0][1] = coord[1];
	    neighbourCoord[0][2] = oppCoord[0];
505
	  }
506
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
507
	  neighbour[0] = elem->getFirstChild();
508
	  oppVertex[0] = 1;
509
510

	  if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
511
	    oppCoord[0] = elInfoOld->coord[0];
512

Thomas Witkowski's avatar
Thomas Witkowski committed
513
514
515
	    neighbourCoord[0][0] = elInfoOld->coord[2];
	    neighbourCoord[0][1] = elInfoOld->coord[0];
	    neighbourCoord[0][2] = coord[2];
516
	  }
517
518
519
520
521
	}

	// Calculation of the neighbour 1, its oppCoords and the
	// cooresponding oppVertex.

522
	Element *nb = elInfoOld->neighbour[2];
523
	if (nb) {
524
	  TEST(elInfoOld->oppVertex[2] == 2)("invalid neighbour\n"); 
525
526
527
	  TEST((nb = nb->getFirstChild()))("missing child?\n");

	  if (nb->getFirstChild()) {
528
	    oppVertex[1] = 2;
529

530
	    if (fill_opp_coords) {
531
	      if (nb->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
532
		oppCoord[1] = *(nb->getNewCoord());
533
	      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
534
		oppCoord[1].setMidpoint(elInfoOld->neighbourCoord[2][0],
535
					 elInfoOld->neighbourCoord[2][2]);
536
	      }
537

538
539
540
	      neighbourCoord[1][0] = elInfoOld->neighbourCoord[2][0];
	      neighbourCoord[1][1].setMidpoint(elInfoOld->neighbourCoord[2][0],
					       elInfoOld->neighbourCoord[2][1]);
Thomas Witkowski's avatar
Thomas Witkowski committed
541
	      neighbourCoord[1][2] = oppCoord[1];
542
	    }
543
544
	    nb = nb->getSecondChild();

545
	  } else {
546
	    oppVertex[1] = 0;
547

548
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
549
	      oppCoord[1] = elInfoOld->oppCoord[2];
550

551
552
553
554
	      neighbourCoord[1][0] = elInfoOld->neighbourCoord[2][2];	      
	      neighbourCoord[1][1] = elInfoOld->neighbourCoord[2][0];
	      neighbourCoord[1][2].setMidpoint(elInfoOld->neighbourCoord[2][0],
					       elInfoOld->neighbourCoord[2][1]);
555
556
557
	    }
	  }
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
558
	neighbour[1] = nb;
559
      } // if (ichild == 0) {} else
Thomas Witkowski's avatar
Thomas Witkowski committed
560
    } // if (fill_flag.isSet(Mesh::FILL_NEIGH) || fillFlag.isSet(Mesh::FILL_OPP_COORDS))
561
562
    

563
    if (fill_flag.isSet(Mesh::FILL_BOUND)) {
564
      if (elInfoOld->getBoundary(2))
Thomas Witkowski's avatar
Thomas Witkowski committed
565
	boundary[5] = elInfoOld->getBoundary(2);
566
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
567
	boundary[5] = INTERIOR;
568

569
      if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
570
571
572
573
574
	boundary[3] = elInfoOld->getBoundary(5);
	boundary[4] = elInfoOld->getBoundary(3);
	boundary[0] = elInfoOld->getBoundary(2);
	boundary[1] = INTERIOR;
	boundary[2] = elInfoOld->getBoundary(1);
575
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
576
577
578
579
580
	boundary[3] = elInfoOld->getBoundary(4);
	boundary[4] = elInfoOld->getBoundary(5);
	boundary[0] = INTERIOR;
	boundary[1] = elInfoOld->getBoundary(2);
	boundary[2] = elInfoOld->getBoundary(0);
581
582
      }

583
584
      if (elInfoOld->getProjection(0) && 
	  elInfoOld->getProjection(0)->getType() == VOLUME_PROJECTION) {
585
	
Thomas Witkowski's avatar
Thomas Witkowski committed
586
	projection[0] = elInfoOld->getProjection(0);
587
588
      } else { // boundary projection
	if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
589
590
591
	  projection[0] = elInfoOld->getProjection(2);
	  projection[1] = NULL;
	  projection[2] = elInfoOld->getProjection(1);
592
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
593
594
595
	  projection[0] = NULL;
	  projection[1] = elInfoOld->getProjection(2);
	  projection[2] = elInfoOld->getProjection(0);
596
597
598
599
600
	}
      }
    }
  }

601
  double ElInfo2d::calcGrdLambda(DimVec<WorldVector<double> >& grd)
602
  {
603
    FUNCNAME("ElInfo2d::calcGrdLambda()");
604
605

    testFlag(Mesh::FILL_COORDS);
606
607
  
    double adet = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
608
    int dim = mesh->getDim();
609

Thomas Witkowski's avatar
Thomas Witkowski committed
610
    for (int i = 0; i < dimOfWorld; i++) {
611
612
      e1[i] = coord[1][i] - coord[0][i];
      e2[i] = coord[2][i] - coord[0][i];
613
614
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
615
    if (dimOfWorld == 2) {
616
      double sdet = e1[0] * e2[1] - e1[1] * e2[0];
617
618
619
620
      adet = abs(sdet);

      if (adet < 1.0E-25) {
	MSG("abs(det) = %f\n", adet);
621
	for (int i = 0; i <= dim; i++)
622
	  grd[i].set(0.0);
623
624
      } else {
	double det1 = 1.0 / sdet;
625

626
627
628
629
630
631
	grd[1][0] = e2[1] * det1;  // a11: (a_ij) = A^{-T}
	grd[1][1] = -e2[0] * det1; // a21
	grd[2][0] = -e1[1] * det1; // a12
	grd[2][1] = e1[0] * det1;  // a22
	grd[0][0] = -grd[1][0] - grd[2][0];
	grd[0][1] = -grd[1][1] - grd[2][1];
632
      }
633
    } else {  
634
      vectorProduct(e1, e2, normal);
635

636
      adet = norm(normal);
637
638
639

      if (adet < 1.0E-15) {
	MSG("abs(det) = %lf\n", adet);
640
	for (int i = 0; i <= dim; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
641
	  for (int j = 0; j < dimOfWorld; j++)
642
	    grd[i][j] = 0.0;
643
      } else {
644
645
	vectorProduct(e2, normal, grd[1]);
	vectorProduct(normal, e1, grd[2]);
646
      
647
	double adet2 = 1.0 / (adet * adet);
648

Thomas Witkowski's avatar
Thomas Witkowski committed
649
	for (int i = 0; i < dimOfWorld; i++) {
650
651
	  grd[1][i] *= adet2;
	  grd[2][i] *= adet2;
652
653
	}

654
655
656
	grd[0][0] = -grd[1][0] - grd[2][0];
	grd[0][1] = -grd[1][1] - grd[2][1];
	grd[0][2] = -grd[1][2] - grd[2][2];
657
658
659
660
661
662
      }
    }

    return adet;
  }

663

664
  int ElInfo2d::worldToCoord(const WorldVector<double>& xy, 
665
666
				   DimVec<double>* lambda) const
  {
667
    FUNCNAME("ElInfo::worldToCoord()");
668

669
    TEST_EXIT_DBG(lambda)("lambda must not be NULL\n");
670

Thomas Witkowski's avatar
Thomas Witkowski committed
671
    DimVec<WorldVector<double> > edge(mesh->getDim(), NO_INIT);
672
    WorldVector<double> x; 
Thomas Witkowski's avatar
Thomas Witkowski committed
673
    static DimVec<double> vec(mesh->getDim(), NO_INIT);
674

Thomas Witkowski's avatar
Thomas Witkowski committed
675
    int dim = mesh->getDim();
676

677
    for (int j = 0; j < dimOfWorld; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
678
      double x0 = coord[dim][j];
679
      x[j] = xy[j] - x0;
680
      for (int i = 0; i < dim; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
681
	edge[i][j] = coord[i][j] - x0;
682
    }
683

684
685
686
    double det  = edge[0][0] * edge[1][1] - edge[0][1] * edge[1][0]; 
    double det0 =       x[0] * edge[1][1] -       x[1] * edge[1][0]; 
    double det1 = edge[0][0] * x[1]       - edge[0][1] * x[0]; 
687
688
689

    if (abs(det) < DBL_TOL) {
      ERROR("det = %le; abort\n", det);
690
      for (int i = 0; i <= dim; i++) 
691
	(*lambda)[i] = 1.0 / dim;
692
693
694
695
696
697
698
699
700
      return 0;
    }

    (*lambda)[0] = det0 / det;
    (*lambda)[1] = det1 / det;
    (*lambda)[2] = 1.0 - (*lambda)[0] - (*lambda)[1];

    int k = -1;
    double lmin = 0.0;
701
    for (int i = 0; i <= dim; i++) {
702
      if ((*lambda)[i] < -1.e-5) {
703
704
705
706
707
708
709
710
711
712
713
	if ((*lambda)[i] < lmin) {
	  k = i;
	  lmin = (*lambda)[i];
	}
      }
    }

    return k;
  }


714
  double ElInfo2d::getNormal(int side, WorldVector<double> &normal) const
715
  {
716
    FUNCNAME_DBG("ElInfo::getNormal()");
717

718
719
    int i0 = (side + 1) % 3;
    int i1 = (side + 2) % 3;
720

Thomas Witkowski's avatar
Thomas Witkowski committed
721
    if (dimOfWorld == 2){
Thomas Witkowski's avatar
Thomas Witkowski committed
722
723
      normal[0] = coord[i1][1] - coord[i0][1];
      normal[1] = coord[i0][0] - coord[i1][0];
724
    } else { // dow == 3
725
      WorldVector<double> e0, e1, e2, elementNormal;
726

Thomas Witkowski's avatar
Thomas Witkowski committed
727
728
729
730
731
732
      e0 = coord[i1]; 
      e0 -= coord[i0];
      e1 = coord[i1]; 
      e1 -= coord[side];
      e2 = coord[i0]; 
      e2 -= coord[side];
733
734
735
736
737

      vectorProduct(e1, e2, elementNormal);
      vectorProduct(elementNormal, e0, normal);
    }

738
    double detn = norm(&normal);
739

740
    TEST_EXIT_DBG(detn > 1.e-30)("det = 0 on face %d\n", side);
741

742
    normal *= 1.0 / detn;
743
    
744
    return detn;
745
746
  }

747

748
749
750
751
752
  /****************************************************************************/
  /*  calculate the normal of the element for dim of world = dim + 1          */
  /*  return the absulute value of the determinant from the                   */
  /*  transformation to the reference element                                 */
  /****************************************************************************/
Thomas Witkowski's avatar
Thomas Witkowski committed
753
  double ElInfo2d::getElementNormal(WorldVector<double> &elementNormal) const
754
  {
755
    FUNCNAME_DBG("ElInfo::getElementNormal()");
756

Thomas Witkowski's avatar
Thomas Witkowski committed
757
758
    TEST_EXIT_DBG(dimOfWorld == 3)
      (" element normal only well defined for  DIM_OF_WORLD = DIM + 1 !!");
759

Thomas Witkowski's avatar
Thomas Witkowski committed
760
761
    WorldVector<double> e0 = coord[1] - coord[0];
    WorldVector<double> e1 = coord[2] - coord[0];
762
763
764

    vectorProduct(e0, e1, elementNormal);

765
    double detn = norm(&elementNormal);
766

767
    TEST_EXIT_DBG(detn > 1.e-30)("det = 0");
768

769
    elementNormal *= 1.0 / detn;
770
    
771
    return detn;
772
  }
773

774

775
  mtl::dense2D<double>& ElInfo2d::getSubElemCoordsMat(int degree) const
776
777
778
779
780
  {
    FUNCNAME("ElInfo2d::getSubElemCoordsMat()");

    using namespace mtl;

Thomas Witkowski's avatar
Thomas Witkowski committed
781
    if (subElemMatrices[degree].count(std::make_pair(refinementPathLength, refinementPath)) == 0) {
782
783
784
      switch (degree) {
      case 1:
	{
785
786
787
	  dense2D<double> mat(3, 3), tmpMat(3, 3);
	  mat = 1;

788
789
	  for (int i = 0; i < refinementPathLength; i++) {
	    if (refinementPath & (1 << i)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
790
	      tmpMat = mat * mat_d1_right;
791
792
	      mat = tmpMat;
	    } else  {
Thomas Witkowski's avatar
Thomas Witkowski committed
793
	      tmpMat = mat * mat_d1_left;
794
795
	      mat = tmpMat;
	    }
796
797
	  }

Thomas Witkowski's avatar
Thomas Witkowski committed
798
	  subElemMatrices[1][std::make_pair(refinementPathLength, refinementPath)] = mat;