ElInfo2d.cc 19.1 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
22
23
24
25
26
27
28
29
30
31
  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);

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

  double ElInfo2d::mat_d1_right_val[3][3] = {{0.0, 0.0, 0.5}, 
					     {1.0, 0.0, 0.5},
					     {0.0, 1.0, 0.0}};
  mtl::dense2D<double> ElInfo2d::mat_d1_right(mat_d1_right_val);

32
33
34
  ElInfo2d::ElInfo2d(Mesh *aMesh) 
    : ElInfo(aMesh) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
35
36
37
    e1 = new WorldVector<double>;
    e2 = new WorldVector<double>;
    normal = new WorldVector<double>;
38
39
40
41
  }

  ElInfo2d::~ElInfo2d()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
42
43
44
    delete e1;
    delete e2;
    delete normal;
45
46
  }

47
48
  void ElInfo2d::fillMacroInfo(const MacroElement * mel)
  {
49
50
    FUNCNAME("ElInfo::fillMacroInfo()");
 
Thomas Witkowski's avatar
Thomas Witkowski committed
51
52
53
    macroElement = const_cast<MacroElement*>(mel);
    element = const_cast<Element*>(mel->getElement());
    parent = NULL;
54
    level = 0;
55

Thomas Witkowski's avatar
Thomas Witkowski committed
56
57
58
    if (fillFlag.isSet(Mesh::FILL_COORDS) || 
	fillFlag.isSet(Mesh::FILL_DET)    ||
	fillFlag.isSet(Mesh::FILL_GRD_LAMBDA)) {
59

Thomas Witkowski's avatar
Thomas Witkowski committed
60
      int vertices = mesh->getGeo(VERTEX);
61
      for (int i = 0; i < vertices; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
62
	coord[i] = mel->coord[i];
63
64
    }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
67
68
    if (fillFlag.isSet(Mesh::FILL_OPP_COORDS) || 
	fillFlag.isSet(Mesh::FILL_NEIGH)) {
69

Thomas Witkowski's avatar
Thomas Witkowski committed
70
      bool fill_opp_coords = (fillFlag.isSet(Mesh::FILL_OPP_COORDS));
71
    
72
73
74
75
      for (int i = 0; i < neighbours; i++) {
	MacroElement *macroNeighbour = mel->getNeighbour(i);

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

79
	  int edgeNo = oppVertex[i] = mel->getOppVertex(i);
80
81


82
	  if (nb->getFirstChild() && edgeNo != 2) {  
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

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


103
	    if (edgeNo == 0) {
104
105
106
107
108
109
110
111
112
113
114
115
	      // The situation is as follows:
	      //
	      //          -------
	      //          \    /|\
	      //           \  / | \
	      //            \/  |  \
	      //             \  |   \
	      //              \ |    \ 
	      //               \|     \
	      //                -------
	      //
	      //            nb     el
116
              //
117
118
119
120
	      // 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
121
	      nb = neighbour[i] = nb->getSecondChild();
122
	    } else {
123
124
	      // 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
125
	      nb = neighbour[i] = nb->getFirstChild();
126
127
	    }

128
129
	    // In both cases the opp vertex number is 2, as one can see in the 
	    // pictures above.
130
	    oppVertex[i] = 2;
131
132

	    if (fill_opp_coords) {
133
	      if (nb->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
134
		oppCoord[i] = *(nb->getNewCoord());
135
	      } else {
136
137
138
		// 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
139
		oppCoord[i] = (macroNeighbour->coord[0] + 
140
141
				macroNeighbour->coord[1]) * 0.5;
	      }
142
143
144
	      
	      switch (i) {
	      case 0:
145
146
147
148
		// The common edge is the edge 0 of this element.

		switch (edgeNo) {
		case 1:
149
150
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[0];
151
152
		  break;
		case 0:		  
153
154
		  neighbourCoord[i][0] = macroNeighbour->coord[1];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];
155
156
		  break;
		default:
Thomas Witkowski's avatar
Thomas Witkowski committed
157
		  ERROR_EXIT("Should not happen!\n");
158
		}
159
	
Thomas Witkowski's avatar
Thomas Witkowski committed
160
		neighbourCoord[i][2] = oppCoord[i];
161
162
163
		break;
		
	      case 1:
164
165
166
		// The commonedge is the edge 1 of this element.
		switch (edgeNo) {
		case 0:
167
168
		  neighbourCoord[i][0] = macroNeighbour->coord[1];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];
169
170
		  break;
		case 1:
171
172
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[0];
173
174
		  break;
		default:
Thomas Witkowski's avatar
Thomas Witkowski committed
175
		  ERROR_EXIT("Should not happen!\n");
176
177
		}
		
Thomas Witkowski's avatar
Thomas Witkowski committed
178
		neighbourCoord[i][2] = oppCoord[i];
179
180
		break;
		
Thomas Witkowski's avatar
Thomas Witkowski committed
181
	      case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
182
		if (*(macroNeighbour->getElement()->getDOF(2)) == *(element->getDOF(0))) {
183
184
		  neighbourCoord[i][0] = macroNeighbour->coord[2];
		  neighbourCoord[i][1] = macroNeighbour->coord[1];
Thomas Witkowski's avatar
Thomas Witkowski committed
185
		} else if (*(macroNeighbour->getElement()->getDOF(2)) == *(element->getDOF(1))) {
186
187
		  neighbourCoord[i][0] = macroNeighbour->coord[0];
		  neighbourCoord[i][1] = macroNeighbour->coord[2];		 
188
189
190
191
		} else {
		  ERROR_EXIT("Should not happen!\n");
		}

192
193
194
		// 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.
195
196
		//		ERROR_EXIT("Should not happen!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
197
198
		break;

199
	      default:
Thomas Witkowski's avatar
Thomas Witkowski committed
200
		std::cout << "------------- Error --------------" << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
201
		std::cout << "  Neighbour counter = " << i << "\n";
Thomas Witkowski's avatar
Thomas Witkowski committed
202
		std::cout << "  Element index     = " << element->getIndex() << "\n\n";
Thomas Witkowski's avatar
Thomas Witkowski committed
203
204
205
206
207
208
209
210
		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;
		  }
211
212
213
		  std::cout << "  OppVertex " << j << ": " 
			    << static_cast<int>(mel->getOppVertex(j)) 
			    << std::endl << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
214
		}
215
216
		ERROR_EXIT("should not happen!\n");
		break;
217
218
219
	      }
	    }
	  } else {
220
221
222
223
224
225
226

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

227
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
228
	      oppCoord[i] = macroNeighbour->coord[edgeNo];
229
	      neighbourCoord[i] = macroNeighbour->coord;	      
230
	    }
231
	  }
232
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
233
	  neighbour[i] = NULL;
234
        }
235
      }
236
    }
237
    
Thomas Witkowski's avatar
Thomas Witkowski committed
238
239
240
241
242
    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);      
243
244
245
246
247
248
249
250
    }
  }


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

251
  void ElInfo2d::fillElInfo(int ichild, const ElInfo *elInfoOld)
252
  {
253
    FUNCNAME("ElInfo::fillElInfo()");
254

Thomas Witkowski's avatar
Thomas Witkowski committed
255
    Element *elem = elInfoOld->element;
256
257
    Element *nb;

Thomas Witkowski's avatar
Thomas Witkowski committed
258
    Flag fill_flag = elInfoOld->fillFlag;
259

260
    TEST_EXIT_DBG(elem->getFirstChild())("no children?\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
261
    element = const_cast<Element*>((ichild == 0) ? 
262
263
				    elem->getFirstChild() : 
				    elem->getSecondChild());
Thomas Witkowski's avatar
Thomas Witkowski committed
264
    TEST_EXIT_DBG(element)("missing child %d?\n", ichild);
265

Thomas Witkowski's avatar
Thomas Witkowski committed
266
267
268
    macroElement  = elInfoOld->macroElement;
    fillFlag = fill_flag;
    parent = elem;
269
    level = elInfoOld->level + 1;
270
    iChild = ichild;
271

Thomas Witkowski's avatar
Thomas Witkowski committed
272
273
274
    if (fillFlag.isSet(Mesh::FILL_COORDS) || 
	fillFlag.isSet(Mesh::FILL_DET)    ||
	fillFlag.isSet(Mesh::FILL_GRD_LAMBDA)) {
275
      
276
      if (elem->isNewCoordSet())
Thomas Witkowski's avatar
Thomas Witkowski committed
277
	coord[2] = *(elem->getNewCoord());
278
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
279
	coord[2].setMidpoint(elInfoOld->coord[0], elInfoOld->coord[1]);      
280
281
      
      if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
282
283
	coord[0] = elInfoOld->coord[2];
	coord[1] = elInfoOld->coord[0];
284
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
285
286
	coord[0] = elInfoOld->coord[1];
	coord[1] = elInfoOld->coord[2];
287
288
289
290
291
292
293
294
295
296
      }
    }

    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
297
	neighbour[2] = elInfoOld->neighbour[1];
298
	oppVertex[2] = elInfoOld->oppVertex[1];
299
	
Thomas Witkowski's avatar
Thomas Witkowski committed
300
301
	if (neighbour[2] && fill_opp_coords) {
	  oppCoord[2] = elInfoOld->oppCoord[1];
302
	  neighbourCoord[2] = elInfoOld->neighbourCoord[1];
303
	}
304
305
306
307
308
309
310
311
312
	
	
	// 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
313
	  neighbour[1] = elem->getSecondChild()->getSecondChild();
314
	  oppVertex[1] = 2;
315
316
	  
	  if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
317
318
319
320
321
322
323
324
            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];  
325
326
	  }
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
327
	  neighbour[1] = elem->getSecondChild();
328
	  oppVertex[1] = 0;
329
330

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

Thomas Witkowski's avatar
Thomas Witkowski committed
333
334
335
	    neighbourCoord[1][0] = elInfoOld->coord[1];
	    neighbourCoord[1][1] = elInfoOld->coord[2];
	    neighbourCoord[1][2] = coord[2];
336
337
338
339
	  }
	}


340
341
342
	// Calculation of the neighbour 0, its oppCoords and the
	// cooresponding oppVertex.
	
Thomas Witkowski's avatar
Thomas Witkowski committed
343
	nb = elInfoOld->neighbour[2];
344
	if (nb) {
345
	  TEST(elInfoOld->oppVertex[2] == 2)("invalid neighbour\n"); 
346
347
	  TEST_EXIT_DBG(nb->getFirstChild())("missing first child?\n");
	  TEST_EXIT_DBG(nb->getSecondChild())("missing second child?\n");
348
349
350
351
	 
	  nb = nb->getSecondChild();

	  if (nb->getFirstChild()) {
352
	    oppVertex[0] = 2;
353

354
	    if (fill_opp_coords) {
355
	      if (nb->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
356
		oppCoord[0] = *(nb->getNewCoord());
357
	      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
358
		oppCoord[0].setMidpoint(elInfoOld->neighbourCoord[2][1],
359
					 elInfoOld->neighbourCoord[2][2]);
360
	      }
361

362
363
364
	      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
365
	      neighbourCoord[0][2] = oppCoord[0];
366
367
368
369
	    }	   
 
	    nb = nb->getFirstChild();
	  } else {
370
	    oppVertex[0] = 1;
371

372
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
373
	      oppCoord[0] = elInfoOld->oppCoord[2];    
374

375
376
377
378
	      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]);
379
380
	    }
	  }
381
382
	}
	
Thomas Witkowski's avatar
Thomas Witkowski committed
383
	neighbour[0] = nb;
384
385
386
387
      } else {   /* ichild == 1 */
	// Calculation of the neighbour 2, its oppCoords and the
	// cooresponding oppVertex.

Thomas Witkowski's avatar
Thomas Witkowski committed
388
	neighbour[2] = elInfoOld->neighbour[0];
389
	oppVertex[2] = elInfoOld->oppVertex[0];
390

Thomas Witkowski's avatar
Thomas Witkowski committed
391
392
	if (neighbour[2] && fill_opp_coords) {
	  oppCoord[2] = elInfoOld->oppCoord[0];
393
	  neighbourCoord[2] = elInfoOld->neighbourCoord[0];
394
395
396
397
398
399
400
	}
	

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

	if (elem->getFirstChild()->getFirstChild()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
401
	  neighbour[0] = elem->getFirstChild()->getFirstChild();
402
	  oppVertex[0] = 2;
403
404

	  if (fill_opp_coords) {
405
            if (elem->getFirstChild()->isNewCoordSet()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
406
	      oppCoord[0] = *(elem->getFirstChild()->getNewCoord());
407
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
408
409
	      oppCoord[0].setMidpoint(elInfoOld->coord[0], 
				       elInfoOld->coord[2]);
410
	    }
411

Thomas Witkowski's avatar
Thomas Witkowski committed
412
413
414
	    neighbourCoord[0][0] = coord[2];
	    neighbourCoord[0][1] = coord[1];
	    neighbourCoord[0][2] = oppCoord[0];
415
	  }
416
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
417
	  neighbour[0] = elem->getFirstChild();
418
	  oppVertex[0] = 1;
419
420

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

Thomas Witkowski's avatar
Thomas Witkowski committed
423
424
425
	    neighbourCoord[0][0] = elInfoOld->coord[2];
	    neighbourCoord[0][1] = elInfoOld->coord[0];
	    neighbourCoord[0][2] = coord[2];
426
	  }
427
428
429
430
431
	}

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

Thomas Witkowski's avatar
Thomas Witkowski committed
432
	nb = elInfoOld->neighbour[2];
433
	if (nb) {
434
	  TEST(elInfoOld->oppVertex[2] == 2)("invalid neighbour\n"); 
435
436
437
	  TEST((nb = nb->getFirstChild()))("missing child?\n");

	  if (nb->getFirstChild()) {
438
	    oppVertex[1] = 2;
439

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

448
449
450
	      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
451
	      neighbourCoord[1][2] = oppCoord[1];
452
	    }
453
454
	    nb = nb->getSecondChild();

455
	  } else {
456
	    oppVertex[1] = 0;
457

458
	    if (fill_opp_coords) {
Thomas Witkowski's avatar
Thomas Witkowski committed
459
	      oppCoord[1] = elInfoOld->oppCoord[2];
460

461
462
463
464
	      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]);
465
466
467
	    }
	  }
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
468
	neighbour[1] = nb;
469
      } // if (ichild == 0) {} else
Thomas Witkowski's avatar
Thomas Witkowski committed
470
    } // if (fill_flag.isSet(Mesh::FILL_NEIGH) || fillFlag.isSet(Mesh::FILL_OPP_COORDS))
471
472
    

473
    if (fill_flag.isSet(Mesh::FILL_BOUND)) {
474
      if (elInfoOld->getBoundary(2))
Thomas Witkowski's avatar
Thomas Witkowski committed
475
	boundary[5] = elInfoOld->getBoundary(2);
476
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
477
	boundary[5] = INTERIOR;
478

479
      if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
480
481
482
483
484
	boundary[3] = elInfoOld->getBoundary(5);
	boundary[4] = elInfoOld->getBoundary(3);
	boundary[0] = elInfoOld->getBoundary(2);
	boundary[1] = INTERIOR;
	boundary[2] = elInfoOld->getBoundary(1);
485
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
486
487
488
489
490
	boundary[3] = elInfoOld->getBoundary(4);
	boundary[4] = elInfoOld->getBoundary(5);
	boundary[0] = INTERIOR;
	boundary[1] = elInfoOld->getBoundary(2);
	boundary[2] = elInfoOld->getBoundary(0);
491
492
      }

493
494
      if (elInfoOld->getProjection(0) && 
	  elInfoOld->getProjection(0)->getType() == VOLUME_PROJECTION) {
495
	
Thomas Witkowski's avatar
Thomas Witkowski committed
496
	projection[0] = elInfoOld->getProjection(0);
497
498
      } else { // boundary projection
	if (ichild == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
499
500
501
	  projection[0] = elInfoOld->getProjection(2);
	  projection[1] = NULL;
	  projection[2] = elInfoOld->getProjection(1);
502
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
503
504
505
	  projection[0] = NULL;
	  projection[1] = elInfoOld->getProjection(2);
	  projection[2] = elInfoOld->getProjection(0);
506
507
508
509
510
	}
      }
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
511
  double ElInfo2d::calcGrdLambda(DimVec<WorldVector<double> >& grd_lam)
512
  {
513
    FUNCNAME("ElInfo2d::calcGrdLambda()");
514
515

    testFlag(Mesh::FILL_COORDS);
516
517
  
    double adet = 0.0;
Thomas Witkowski's avatar
Thomas Witkowski committed
518
    int dim = mesh->getDim();
519

Thomas Witkowski's avatar
Thomas Witkowski committed
520
    for (int i = 0; i < dimOfWorld; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
521
522
      (*e1)[i] = coord[1][i] - coord[0][i];
      (*e2)[i] = coord[2][i] - coord[0][i];
523
524
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
525
    if (dimOfWorld == 2) {
526
      double sdet = (*e1)[0] * (*e2)[1] - (*e1)[1] * (*e2)[0];
527
528
529
530
      adet = abs(sdet);

      if (adet < 1.0E-25) {
	MSG("abs(det) = %f\n", adet);
531
	for (int i = 0; i <= dim; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
532
	  for (int j = 0; j < dimOfWorld; j++)
533
	    grd_lam[i][j] = 0.0;
534
535
      } else {
	double det1 = 1.0 / sdet;
536
537
538
539
540

	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
541
542
	grd_lam[0][0] = - grd_lam[1][0] - grd_lam[2][0];
	grd_lam[0][1] = - grd_lam[1][1] - grd_lam[2][1];
543
      }
544
545
    } else {  
      vectorProduct(*e1, *e2, *normal);
546

547
      adet = norm(normal);
548
549
550

      if (adet < 1.0E-15) {
	MSG("abs(det) = %lf\n", adet);
551
	for (int i = 0; i <= dim; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
552
	  for (int j = 0; j < dimOfWorld; j++)
553
554
	    grd_lam[i][j] = 0.0;
      } else {
555
556
	vectorProduct(*e2, *normal, grd_lam[1]);
	vectorProduct(*normal, *e1, grd_lam[2]);
557
      
558
	double adet2 = 1.0 / (adet * adet);
559

Thomas Witkowski's avatar
Thomas Witkowski committed
560
	for (int i = 0; i < dimOfWorld; i++) {
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
	  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;
  }

  const int ElInfo2d::worldToCoord(const WorldVector<double>& xy,
				   DimVec<double>* lambda) const
  {
577
    FUNCNAME("ElInfo::worldToCoord()");
578

579
    TEST_EXIT_DBG(lambda)("lambda must not be NULL\n");
580

Thomas Witkowski's avatar
Thomas Witkowski committed
581
    DimVec<WorldVector<double> > edge(mesh->getDim(), NO_INIT);
582
    WorldVector<double> x; 
Thomas Witkowski's avatar
Thomas Witkowski committed
583
    static DimVec<double> vec(mesh->getDim(), NO_INIT);
584

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

587
    for (int j = 0; j < dimOfWorld; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
588
      double x0 = coord[dim][j];
589
      x[j] = xy[j] - x0;
590
      for (int i = 0; i < dim; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
591
	edge[i][j] = coord[i][j] - x0;
592
593
    }
  
594
595
596
    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]; 
597
598
599

    if (abs(det) < DBL_TOL) {
      ERROR("det = %le; abort\n", det);
600
601
      for (int i = 0; i <= dim; i++) 
	(*lambda)[i] = 1.0/dim;
602
603
604
605
606
607
608
609
610
      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;
611
    for (int i = 0; i <= dim; i++) {
612
613
614
615
616
617
618
619
620
621
622
623
      if ((*lambda)[i] < -1.E-5) {
	if ((*lambda)[i] < lmin) {
	  k = i;
	  lmin = (*lambda)[i];
	}
      }
    }

    return k;
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
624
  double ElInfo2d::getNormal(int side, WorldVector<double> &normal)
625
  {
626
    FUNCNAME("ElInfo::getNormal()");
627

628
629
    int i0 = (side + 1) % 3;
    int i1 = (side + 2) % 3;
630

Thomas Witkowski's avatar
Thomas Witkowski committed
631
    if (dimOfWorld == 2){
Thomas Witkowski's avatar
Thomas Witkowski committed
632
633
      normal[0] = coord[i1][1] - coord[i0][1];
      normal[1] = coord[i0][0] - coord[i1][0];
634
635
636
    } else { // dow == 3
      WorldVector<double> e0, e1,e2, elementNormal;

Thomas Witkowski's avatar
Thomas Witkowski committed
637
638
639
640
641
642
      e0 = coord[i1]; 
      e0 -= coord[i0];
      e1 = coord[i1]; 
      e1 -= coord[side];
      e2 = coord[i0]; 
      e2 -= coord[side];
643
644
645
646
647

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

648
    double det = norm(&normal);
649

650
    TEST_EXIT_DBG(det > 1.e-30)("det = 0 on face %d\n", side);
651
652
653

    normal *= 1.0 / det;
    
Thomas Witkowski's avatar
Thomas Witkowski committed
654
    return det;
655
656
657
658
659
660
661
  }

  /****************************************************************************/
  /*  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
662
  double ElInfo2d::getElementNormal(WorldVector<double> &elementNormal) const
663
  {
664
    FUNCNAME("ElInfo::getElementNormal()");
665

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

Thomas Witkowski's avatar
Thomas Witkowski committed
669
670
    WorldVector<double> e0 = coord[1] - coord[0];
    WorldVector<double> e1 = coord[2] - coord[0];
671
672
673

    vectorProduct(e0, e1, elementNormal);

674
    double det = norm(&elementNormal);
675

676
    TEST_EXIT_DBG(det > 1.e-30)("det = 0");
677
678
679

    elementNormal *= 1.0 / det;
    
Thomas Witkowski's avatar
Thomas Witkowski committed
680
    return det;
681
  }
682

683
}