ElInfo2d.cc 22.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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 {

17
18
19
20
21
  double ElInfo2d::mat_d1_val[3][3] = {{1.0, 0.0, 0.0}, 
				       {0.0, 1.0, 0.0}, 
				       {0.0, 0.0, 1.0}};
  mtl::dense2D<double> ElInfo2d::mat_d1(mat_d1_val);

22
  
23
  double ElInfo2d::mat_d1_left_val[3][3] = {{0.0, 1.0, 0.5}, 
24
  					    {0.0, 0.0, 0.5},
25
  					    {1.0, 0.0, 0.0}}; 
26
27
  mtl::dense2D<double> ElInfo2d::mat_d1_left(mat_d1_left_val);

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

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


  double ElInfo2d::mat_d2_val[6][6] = {{1.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, 1.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, 1.0, 0.0},
				       {0.0, 0.0, 0.0, 0.0, 0.0, 1.0}};
  mtl::dense2D<double> ElInfo2d::mat_d2(mat_d2_val);

  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);


62
63
64
  ElInfo2d::ElInfo2d(Mesh *aMesh) 
    : ElInfo(aMesh) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
65
66
67
    e1 = new WorldVector<double>;
    e2 = new WorldVector<double>;
    normal = new WorldVector<double>;
68
69
  }

70

71
72
  ElInfo2d::~ElInfo2d()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
73
74
75
    delete e1;
    delete e2;
    delete normal;
76
77
  }

78

79
80
  void ElInfo2d::fillMacroInfo(const MacroElement * mel)
  {
81
82
    FUNCNAME("ElInfo::fillMacroInfo()");
 
Thomas Witkowski's avatar
Thomas Witkowski committed
83
84
85
    macroElement = const_cast<MacroElement*>(mel);
    element = const_cast<Element*>(mel->getElement());
    parent = NULL;
86
    level = 0;
87

Thomas Witkowski's avatar
Thomas Witkowski committed
88
89
90
    if (fillFlag.isSet(Mesh::FILL_COORDS) || 
	fillFlag.isSet(Mesh::FILL_DET)    ||
	fillFlag.isSet(Mesh::FILL_GRD_LAMBDA)) {
91

Thomas Witkowski's avatar
Thomas Witkowski committed
92
      int vertices = mesh->getGeo(VERTEX);
93
      for (int i = 0; i < vertices; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
94
	coord[i] = mel->coord[i];
95
96
    }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
99
100
    if (fillFlag.isSet(Mesh::FILL_OPP_COORDS) || 
	fillFlag.isSet(Mesh::FILL_NEIGH)) {
101

Thomas Witkowski's avatar
Thomas Witkowski committed
102
      bool fill_opp_coords = (fillFlag.isSet(Mesh::FILL_OPP_COORDS));
103
    
104
105
106
107
      for (int i = 0; i < neighbours; i++) {
	MacroElement *macroNeighbour = mel->getNeighbour(i);

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

111
	  int edgeNo = oppVertex[i] = mel->getOppVertex(i);
112
113


114
	  if (nb->getFirstChild() && edgeNo != 2) {  
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

	    // 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.


135
	    if (edgeNo == 0) {
136
137
138
139
140
141
142
143
144
145
146
147
	      // The situation is as follows:
	      //
	      //          -------
	      //          \    /|\
	      //           \  / | \
	      //            \/  |  \
	      //             \  |   \
	      //              \ |    \ 
	      //               \|     \
	      //                -------
	      //
	      //            nb     el
148
              //
149
150
151
152
	      // 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
153
	      nb = neighbour[i] = nb->getSecondChild();
154
	    } else {
155
156
	      // 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
157
	      nb = neighbour[i] = nb->getFirstChild();
158
159
	    }

160
161
	    // In both cases the opp vertex number is 2, as one can see in the 
	    // pictures above.
162
	    oppVertex[i] = 2;
163
164

	    if (fill_opp_coords) {
165
	      if (nb->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
166
		oppCoord[i] = *(nb->getNewCoord());
167
	      } else {
168
169
170
		// 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
171
		oppCoord[i] = (macroNeighbour->coord[0] + 
172
173
				macroNeighbour->coord[1]) * 0.5;
	      }
174
175
176
	      
	      switch (i) {
	      case 0:
177
178
179
180
		// The common edge is the edge 0 of this element.

		switch (edgeNo) {
		case 1:
181
182
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[0];
183
184
		  break;
		case 0:		  
185
186
		  neighbourCoord[i][0] = macroNeighbour->coord[1];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];
187
188
		  break;
		default:
Thomas Witkowski's avatar
Thomas Witkowski committed
189
		  ERROR_EXIT("Should not happen!\n");
190
		}
191
	
Thomas Witkowski's avatar
Thomas Witkowski committed
192
		neighbourCoord[i][2] = oppCoord[i];
193
194
195
		break;
		
	      case 1:
196
197
198
		// The commonedge is the edge 1 of this element.
		switch (edgeNo) {
		case 0:
199
200
		  neighbourCoord[i][0] = macroNeighbour->coord[1];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];
201
202
		  break;
		case 1:
203
204
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[0];
205
206
		  break;
		default:
Thomas Witkowski's avatar
Thomas Witkowski committed
207
		  ERROR_EXIT("Should not happen!\n");
208
209
		}
		
Thomas Witkowski's avatar
Thomas Witkowski committed
210
		neighbourCoord[i][2] = oppCoord[i];
211
212
		break;
		
Thomas Witkowski's avatar
Thomas Witkowski committed
213
	      case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
214
		if (*(macroNeighbour->getElement()->getDOF(2)) == *(element->getDOF(0))) {
215
216
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[1];
Thomas Witkowski's avatar
Thomas Witkowski committed
217
		} else if (*(macroNeighbour->getElement()->getDOF(2)) == *(element->getDOF(1))) {
218
219
		  neighbourCoord[i][0] = macroNeighbour->coord[0];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];		 
220
221
222
223
		} else {
		  ERROR_EXIT("Should not happen!\n");
		}

224
225
226
		// 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.
227
228
		//		ERROR_EXIT("Should not happen!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
229
230
		break;

231
	      default:
Thomas Witkowski's avatar
Thomas Witkowski committed
232
		std::cout << "------------- Error --------------" << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
233
		std::cout << "  Neighbour counter = " << i << "\n";
Thomas Witkowski's avatar
Thomas Witkowski committed
234
		std::cout << "  Element index     = " << element->getIndex() << "\n\n";
Thomas Witkowski's avatar
Thomas Witkowski committed
235
236
237
238
239
240
241
242
		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;
		  }
243
244
245
		  std::cout << "  OppVertex " << j << ": " 
			    << static_cast<int>(mel->getOppVertex(j)) 
			    << std::endl << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
246
		}
247
248
		ERROR_EXIT("should not happen!\n");
		break;
249
250
251
	      }
	    }
	  } else {
252
253
254
255
256
257
258

	    // 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.

259
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
260
	      oppCoord[i] = macroNeighbour->coord[edgeNo];
261
	      neighbourCoord[i] = macroNeighbour->coord;	      
262
	    }
263
	  }
264
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
265
	  neighbour[i] = NULL;
266
        }
267
      }
268
    }
269
    
Thomas Witkowski's avatar
Thomas Witkowski committed
270
271
272
273
274
    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);      
275
276
277
278
279
280
281
282
    }
  }


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

283
  void ElInfo2d::fillElInfo(int ichild, const ElInfo *elInfoOld)
284
  {
285
    FUNCNAME("ElInfo::fillElInfo()");
286

Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
    Element *elem = elInfoOld->element;
    Flag fill_flag = elInfoOld->fillFlag;
289

290
    TEST_EXIT_DBG(elem->getFirstChild())("no children?\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
291
    element = const_cast<Element*>((ichild == 0) ? 
292
293
				    elem->getFirstChild() : 
				    elem->getSecondChild());
Thomas Witkowski's avatar
Thomas Witkowski committed
294
    TEST_EXIT_DBG(element)("missing child %d?\n", ichild);
295

Thomas Witkowski's avatar
Thomas Witkowski committed
296
297
298
    macroElement  = elInfoOld->macroElement;
    fillFlag = fill_flag;
    parent = elem;
299
    level = elInfoOld->level + 1;
300
    iChild = ichild;
301

Thomas Witkowski's avatar
Thomas Witkowski committed
302
303
304
    if (fillFlag.isSet(Mesh::FILL_COORDS) || 
	fillFlag.isSet(Mesh::FILL_DET)    ||
	fillFlag.isSet(Mesh::FILL_GRD_LAMBDA)) {
305
      
306
      if (elem->isNewCoordSet())
Thomas Witkowski's avatar
Thomas Witkowski committed
307
	coord[2] = *(elem->getNewCoord());
308
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
309
	coord[2].setMidpoint(elInfoOld->coord[0], elInfoOld->coord[1]);      
310
311
      
      if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
312
313
	coord[0] = elInfoOld->coord[2];
	coord[1] = elInfoOld->coord[0];
314
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
315
316
	coord[0] = elInfoOld->coord[1];
	coord[1] = elInfoOld->coord[2];
317
318
319
320
321
322
323
324
325
326
      }
    }

    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
327
	neighbour[2] = elInfoOld->neighbour[1];
328
	oppVertex[2] = elInfoOld->oppVertex[1];
329
	
Thomas Witkowski's avatar
Thomas Witkowski committed
330
331
	if (neighbour[2] && fill_opp_coords) {
	  oppCoord[2] = elInfoOld->oppCoord[1];
332
	  neighbourCoord[2] = elInfoOld->neighbourCoord[1];
333
	}
334
335
336
337
338
339
340
341
342
	
	
	// 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
343
	  neighbour[1] = elem->getSecondChild()->getSecondChild();
344
	  oppVertex[1] = 2;
345
346
	  
	  if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
347
348
349
350
351
352
353
354
            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];  
355
356
	  }
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
357
	  neighbour[1] = elem->getSecondChild();
358
	  oppVertex[1] = 0;
359
360

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

Thomas Witkowski's avatar
Thomas Witkowski committed
363
364
365
	    neighbourCoord[1][0] = elInfoOld->coord[1];
	    neighbourCoord[1][1] = elInfoOld->coord[2];
	    neighbourCoord[1][2] = coord[2];
366
367
368
369
	  }
	}


370
371
372
	// Calculation of the neighbour 0, its oppCoords and the
	// cooresponding oppVertex.
	
373
	Element *nb = elInfoOld->neighbour[2];
374
	if (nb) {
375
	  TEST(elInfoOld->oppVertex[2] == 2)("invalid neighbour\n"); 
376
377
	  TEST_EXIT_DBG(nb->getFirstChild())("missing first child?\n");
	  TEST_EXIT_DBG(nb->getSecondChild())("missing second child?\n");
378
379
380
381
	 
	  nb = nb->getSecondChild();

	  if (nb->getFirstChild()) {
382
	    oppVertex[0] = 2;
383

384
	    if (fill_opp_coords) {
385
	      if (nb->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
386
		oppCoord[0] = *(nb->getNewCoord());
387
	      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
388
		oppCoord[0].setMidpoint(elInfoOld->neighbourCoord[2][1],
389
					 elInfoOld->neighbourCoord[2][2]);
390
	      }
391

392
393
394
	      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
395
	      neighbourCoord[0][2] = oppCoord[0];
396
397
398
399
	    }	   
 
	    nb = nb->getFirstChild();
	  } else {
400
	    oppVertex[0] = 1;
401

402
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
403
	      oppCoord[0] = elInfoOld->oppCoord[2];    
404

405
406
407
408
	      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]);
409
410
	    }
	  }
411
412
	}
	
Thomas Witkowski's avatar
Thomas Witkowski committed
413
	neighbour[0] = nb;
414
415
416
417
      } else {   /* ichild == 1 */
	// Calculation of the neighbour 2, its oppCoords and the
	// cooresponding oppVertex.

Thomas Witkowski's avatar
Thomas Witkowski committed
418
	neighbour[2] = elInfoOld->neighbour[0];
419
	oppVertex[2] = elInfoOld->oppVertex[0];
420

Thomas Witkowski's avatar
Thomas Witkowski committed
421
422
	if (neighbour[2] && fill_opp_coords) {
	  oppCoord[2] = elInfoOld->oppCoord[0];
423
	  neighbourCoord[2] = elInfoOld->neighbourCoord[0];
424
425
426
427
428
429
430
	}
	

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

	if (elem->getFirstChild()->getFirstChild()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
431
	  neighbour[0] = elem->getFirstChild()->getFirstChild();
432
	  oppVertex[0] = 2;
433
434

	  if (fill_opp_coords) {
435
            if (elem->getFirstChild()->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
436
	      oppCoord[0] = *(elem->getFirstChild()->getNewCoord());
437
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
438
439
	      oppCoord[0].setMidpoint(elInfoOld->coord[0], 
				       elInfoOld->coord[2]);
440
	    }
441

Thomas Witkowski's avatar
Thomas Witkowski committed
442
443
444
	    neighbourCoord[0][0] = coord[2];
	    neighbourCoord[0][1] = coord[1];
	    neighbourCoord[0][2] = oppCoord[0];
445
	  }
446
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
447
	  neighbour[0] = elem->getFirstChild();
448
	  oppVertex[0] = 1;
449
450

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

Thomas Witkowski's avatar
Thomas Witkowski committed
453
454
455
	    neighbourCoord[0][0] = elInfoOld->coord[2];
	    neighbourCoord[0][1] = elInfoOld->coord[0];
	    neighbourCoord[0][2] = coord[2];
456
	  }
457
458
459
460
461
	}

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

462
	Element *nb = elInfoOld->neighbour[2];
463
	if (nb) {
464
	  TEST(elInfoOld->oppVertex[2] == 2)("invalid neighbour\n"); 
465
466
467
	  TEST((nb = nb->getFirstChild()))("missing child?\n");

	  if (nb->getFirstChild()) {
468
	    oppVertex[1] = 2;
469

470
	    if (fill_opp_coords) {
471
	      if (nb->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
472
		oppCoord[1] = *(nb->getNewCoord());
473
	      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
474
		oppCoord[1].setMidpoint(elInfoOld->neighbourCoord[2][0],
475
					 elInfoOld->neighbourCoord[2][2]);
476
	      }
477

478
479
480
	      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
481
	      neighbourCoord[1][2] = oppCoord[1];
482
	    }
483
484
	    nb = nb->getSecondChild();

485
	  } else {
486
	    oppVertex[1] = 0;
487

488
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
489
	      oppCoord[1] = elInfoOld->oppCoord[2];
490

491
492
493
494
	      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]);
495
496
497
	    }
	  }
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
498
	neighbour[1] = nb;
499
      } // if (ichild == 0) {} else
Thomas Witkowski's avatar
Thomas Witkowski committed
500
    } // if (fill_flag.isSet(Mesh::FILL_NEIGH) || fillFlag.isSet(Mesh::FILL_OPP_COORDS))
501
502
    

503
    if (fill_flag.isSet(Mesh::FILL_BOUND)) {
504
      if (elInfoOld->getBoundary(2))
Thomas Witkowski's avatar
Thomas Witkowski committed
505
	boundary[5] = elInfoOld->getBoundary(2);
506
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
507
	boundary[5] = INTERIOR;
508

509
      if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
510
511
512
513
514
	boundary[3] = elInfoOld->getBoundary(5);
	boundary[4] = elInfoOld->getBoundary(3);
	boundary[0] = elInfoOld->getBoundary(2);
	boundary[1] = INTERIOR;
	boundary[2] = elInfoOld->getBoundary(1);
515
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
516
517
518
519
520
	boundary[3] = elInfoOld->getBoundary(4);
	boundary[4] = elInfoOld->getBoundary(5);
	boundary[0] = INTERIOR;
	boundary[1] = elInfoOld->getBoundary(2);
	boundary[2] = elInfoOld->getBoundary(0);
521
522
      }

523
524
      if (elInfoOld->getProjection(0) && 
	  elInfoOld->getProjection(0)->getType() == VOLUME_PROJECTION) {
525
	
Thomas Witkowski's avatar
Thomas Witkowski committed
526
	projection[0] = elInfoOld->getProjection(0);
527
528
      } else { // boundary projection
	if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
529
530
531
	  projection[0] = elInfoOld->getProjection(2);
	  projection[1] = NULL;
	  projection[2] = elInfoOld->getProjection(1);
532
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
533
534
535
	  projection[0] = NULL;
	  projection[1] = elInfoOld->getProjection(2);
	  projection[2] = elInfoOld->getProjection(0);
536
537
538
539
540
	}
      }
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
541
  double ElInfo2d::calcGrdLambda(DimVec<WorldVector<double> >& grd_lam)
542
  {
543
    FUNCNAME("ElInfo2d::calcGrdLambda()");
544
545

    testFlag(Mesh::FILL_COORDS);
546
547
  
    double adet = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
548
    int dim = mesh->getDim();
549

Thomas Witkowski's avatar
Thomas Witkowski committed
550
    for (int i = 0; i < dimOfWorld; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
551
552
      (*e1)[i] = coord[1][i] - coord[0][i];
      (*e2)[i] = coord[2][i] - coord[0][i];
553
554
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
555
    if (dimOfWorld == 2) {
556
      double sdet = (*e1)[0] * (*e2)[1] - (*e1)[1] * (*e2)[0];
557
558
559
560
      adet = abs(sdet);

      if (adet < 1.0E-25) {
	MSG("abs(det) = %f\n", adet);
561
	for (int i = 0; i <= dim; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
562
	  for (int j = 0; j < dimOfWorld; j++)
563
	    grd_lam[i][j] = 0.0;
564
565
      } else {
	double det1 = 1.0 / sdet;
566
567
568
569
570

	grd_lam[1][0] = (*e2)[1] * det1;  // a11: (a_ij) = A^{-T}
	grd_lam[1][1] = -(*e2)[0] * det1; // a21
	grd_lam[2][0] = -(*e1)[1] * det1; // a12
	grd_lam[2][1] = (*e1)[0] * det1;  // a22
571
572
	grd_lam[0][0] = - grd_lam[1][0] - grd_lam[2][0];
	grd_lam[0][1] = - grd_lam[1][1] - grd_lam[2][1];
573
      }
574
575
    } else {  
      vectorProduct(*e1, *e2, *normal);
576

577
      adet = norm(normal);
578
579
580

      if (adet < 1.0E-15) {
	MSG("abs(det) = %lf\n", adet);
581
	for (int i = 0; i <= dim; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
582
	  for (int j = 0; j < dimOfWorld; j++)
583
584
	    grd_lam[i][j] = 0.0;
      } else {
585
586
	vectorProduct(*e2, *normal, grd_lam[1]);
	vectorProduct(*normal, *e1, grd_lam[2]);
587
      
588
	double adet2 = 1.0 / (adet * adet);
589

Thomas Witkowski's avatar
Thomas Witkowski committed
590
	for (int i = 0; i < dimOfWorld; i++) {
591
592
593
594
595
596
597
598
599
600
601
602
603
	  grd_lam[1][i] *= adet2;
	  grd_lam[2][i] *= adet2;
	}

	grd_lam[0][0] = - grd_lam[1][0] - grd_lam[2][0];
	grd_lam[0][1] = - grd_lam[1][1] - grd_lam[2][1];
	grd_lam[0][2] = - grd_lam[1][2] - grd_lam[2][2];
      }
    }

    return adet;
  }

604
605

  const int ElInfo2d::worldToCoord(const WorldVector<double>& xy, 
606
607
				   DimVec<double>* lambda) const
  {
608
    FUNCNAME("ElInfo::worldToCoord()");
609

610
    TEST_EXIT_DBG(lambda)("lambda must not be NULL\n");
611

Thomas Witkowski's avatar
Thomas Witkowski committed
612
    DimVec<WorldVector<double> > edge(mesh->getDim(), NO_INIT);
613
    WorldVector<double> x; 
Thomas Witkowski's avatar
Thomas Witkowski committed
614
    static DimVec<double> vec(mesh->getDim(), NO_INIT);
615

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

618
    for (int j = 0; j < dimOfWorld; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
619
      double x0 = coord[dim][j];
620
      x[j] = xy[j] - x0;
621
      for (int i = 0; i < dim; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
622
	edge[i][j] = coord[i][j] - x0;
623
624
    }
  
625
626
627
    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]; 
628
629
630

    if (abs(det) < DBL_TOL) {
      ERROR("det = %le; abort\n", det);
631
632
      for (int i = 0; i <= dim; i++) 
	(*lambda)[i] = 1.0/dim;
633
634
635
636
637
638
639
640
641
      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;
642
    for (int i = 0; i <= dim; i++) {
643
644
645
646
647
648
649
650
651
652
653
654
      if ((*lambda)[i] < -1.E-5) {
	if ((*lambda)[i] < lmin) {
	  k = i;
	  lmin = (*lambda)[i];
	}
      }
    }

    return k;
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
655
  double ElInfo2d::getNormal(int side, WorldVector<double> &normal)
656
  {
657
    FUNCNAME("ElInfo::getNormal()");
658

659
660
    int i0 = (side + 1) % 3;
    int i1 = (side + 2) % 3;
661

Thomas Witkowski's avatar
Thomas Witkowski committed
662
    if (dimOfWorld == 2){
Thomas Witkowski's avatar
Thomas Witkowski committed
663
664
      normal[0] = coord[i1][1] - coord[i0][1];
      normal[1] = coord[i0][0] - coord[i1][0];
665
666
667
    } else { // dow == 3
      WorldVector<double> e0, e1,e2, elementNormal;

Thomas Witkowski's avatar
Thomas Witkowski committed
668
669
670
671
672
673
      e0 = coord[i1]; 
      e0 -= coord[i0];
      e1 = coord[i1]; 
      e1 -= coord[side];
      e2 = coord[i0]; 
      e2 -= coord[side];
674
675
676
677
678

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

679
    double det = norm(&normal);
680

681
    TEST_EXIT_DBG(det > 1.e-30)("det = 0 on face %d\n", side);
682
683
684

    normal *= 1.0 / det;
    
Thomas Witkowski's avatar
Thomas Witkowski committed
685
    return det;
686
687
  }

688

689
690
691
692
693
  /****************************************************************************/
  /*  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
694
  double ElInfo2d::getElementNormal(WorldVector<double> &elementNormal) const
695
  {
696
    FUNCNAME("ElInfo::getElementNormal()");
697

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

Thomas Witkowski's avatar
Thomas Witkowski committed
701
702
    WorldVector<double> e0 = coord[1] - coord[0];
    WorldVector<double> e1 = coord[2] - coord[0];
703
704
705

    vectorProduct(e0, e1, elementNormal);

706
    double det = norm(&elementNormal);
707

708
    TEST_EXIT_DBG(det > 1.e-30)("det = 0");
709
710
711

    elementNormal *= 1.0 / det;
    
Thomas Witkowski's avatar
Thomas Witkowski committed
712
    return det;
713
  }
714

715

716
  mtl::dense2D<double>& ElInfo2d::getSubElemCoordsMat(int degree) const
717
718
719
720
721
  {
    FUNCNAME("ElInfo2d::getSubElemCoordsMat()");

    using namespace mtl;

722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
    if (subElemMatrices[degree].count(refinementPath) == 0) {
      switch (degree) {
      case 1:
	{
	  dense2D<double> mat(mat_d1);
	  dense2D<double> tmpMat(num_rows(mat), num_rows(mat));
	  
	  for (int i = 0; i < refinementPathLength; i++) {
	    if (refinementPath & (1 << i)) {
	      tmpMat = mat_d1_right * mat;
	      mat = tmpMat;
	    } else  {
	      tmpMat = mat_d1_left * mat;
	      mat = tmpMat;
	    }
737
738
	  }

739
740
741
	  subElemMatrices[1][refinementPath] = mat;  
	}
	break;
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
      case 2:
	{
	  dense2D<double> mat(mat_d2);
	  dense2D<double> tmpMat(num_rows(mat), num_rows(mat));
	  
	  for (int i = 0; i < refinementPathLength; i++) {
	    if (refinementPath & (1 << i)) {
	      tmpMat = mat_d2_right * mat;
	      mat = tmpMat;
	    } else  {
	      tmpMat = mat_d2_left * mat;
	      mat = tmpMat;
	    }
	  }

	  subElemMatrices[2][refinementPath] = mat;  
	}
	break;
760
761
      default:
	ERROR_EXIT("Not supported for basis function degree: %d\n", degree);
762
763
      }
    }
764
765

    return subElemMatrices[degree][refinementPath];
766
767
768
  }


769
  mtl::dense2D<double>& ElInfo2d::getSubElemGradCoordsMat(int degree) const
770
  {
771
    FUNCNAME("ElInfo2d::getSubElemGradCoordsMat()");
772

773
774
775
776
    TEST_EXIT(degree == 1)("Not supported for basis functions with degree > 1!\n");

    using namespace mtl;

777
    if (subElemGradMatrices[degree].count(refinementPath) == 0) {
778
      dense2D<double> mat(mat_d1);
779
780
781
782
783
784
785
786
787
788
789
790
      dense2D<double> tmpMat(num_rows(mat), num_rows(mat));

      double test_left[3][3] = {{0.0, 0.0, 0.5},
				{-0.5, -0.5, 0.0},
				{1.0, 0.0, 0.0}};
      double test_right[3][3] = {{0.0, 0.0, 0.5},
				 {0.5, -0.5, 0.0},
				 {0.0, 1.0, 0.0}};
      
      mtl::dense2D<double> mat_left(test_left);
      mtl::dense2D<double> mat_right(test_right);

791
792

      for (int i = 0; i < refinementPathLength; i++)
793
794
795
796
797
798
799
	if (refinementPath & (1 << i)) {
	  tmpMat = mat_right * mat;
	  mat = tmpMat;
	} else  {
	  tmpMat = mat_left * mat;
	  mat = tmpMat;
	}
800
801
802
803
804

      subElemGradMatrices[1][refinementPath] = mat;
    }

    return subElemGradMatrices[degree][refinementPath];
805

806
  }
807
}