Lagrange.cc 169 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Mesh.h"
#include "Element.h"
#include "Lagrange.h"
#include "DOFAdmin.h"
#include "RCNeighbourList.h"
#include "DOFVector.h"
#include "Traverse.h"
#include "Line.h"
#include "Triangle.h"
#include "Tetrahedron.h"
#include "Parametric.h"

#include <stdio.h>
#include <algorithm>
#include <list>

namespace AMDiS {

19
20
21
22
23
24
  std::vector<DimVec<double>* > Lagrange::baryDimDegree[MAX_DIM + 1][MAX_DEGREE + 1];
  DimVec<int>* Lagrange::ndofDimDegree[MAX_DIM + 1][MAX_DEGREE + 1];
  int Lagrange::nBasFctsDimDegree[MAX_DIM + 1][MAX_DEGREE + 1];
  std::vector<BasFctType*> Lagrange::phiDimDegree[MAX_DIM + 1][MAX_DEGREE + 1];
  std::vector<GrdBasFctType*> Lagrange::grdPhiDimDegree[MAX_DIM + 1][MAX_DEGREE + 1];
  std::vector<D2BasFctType*> Lagrange::D2PhiDimDegree[MAX_DIM + 1][MAX_DEGREE + 1];
25
  std::list<Lagrange*> Lagrange::allBasFcts;
26
27
28


  Lagrange::Lagrange(int dim, int degree)
29
    : BasisFunction(std::string("Lagrange"), dim, degree)
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  {
    // set name
    char dummy[3];
    sprintf(dummy, "%d", dim);
    name.append(dummy);
    name.append(" ");
    sprintf(dummy, "%d", degree);  
    name.append(dummy);

    // set nDOF
    setNDOF();

    // set barycentric coordinates
    setBary();

    // set function pointer
    setFunctionPointer();
  }

  Lagrange::~Lagrange()
  {
51
52
    for (int i = 0; i < static_cast<int>(bary->size()); i++) {
      if ((*bary)[i]) {
53
	delete (*bary)[i];
54
55
56
	(*bary)[i] = NULL;
      }
    }
57
58
59
60
  }

  Lagrange* Lagrange::getLagrange(int dim, int degree)
  {
61
    std::list<Lagrange*>::iterator it;
Thomas Witkowski's avatar
Thomas Witkowski committed
62
63
    for (it = allBasFcts.begin(); it != allBasFcts.end(); it++)
      if (((*it)->dim == dim) && ((*it)->degree == degree))
64
65
	return (*it);

66
    Lagrange* newLagrange = new Lagrange(dim, degree);
67
68
69
70
    allBasFcts.push_back(newLagrange);
    return newLagrange;
  }

71
72
73
74
  void Lagrange::clear()
  {    
    std::list<Lagrange*>::iterator it;
    for (it = allBasFcts.begin(); it != allBasFcts.end(); it++) {
75
      if (*it) {
76
	delete *it;
77
78
	*it = NULL;
      }     
79
80
81
    }
  }

82
83
  void Lagrange::setFunctionPointer()
  {
84
    if (static_cast<int>(phiDimDegree[dim][degree].size()) == 0) {
85
      // for all positions
86
      for (int i = 0; i < dim+1; i++) {
87
	// no vertex dofs for degree 0 ?
88
89
	if (degree == 0 && i != dim) 
	  continue;
90
	// for all vertices/edges/...
91
	for (int j = 0; j < Global::getGeo(INDEX_OF_DIM(i, dim),dim); j++) {
92
	  // for all dofs at this position
93
	  for (int k = 0; k < (*nDOF)[INDEX_OF_DIM(i, dim)]; k++) {
94
	    // basis function
95
	    phiDimDegree[dim][degree].push_back(new Phi(this, 
96
							INDEX_OF_DIM(i ,dim),j, k));
97
	    // gradients
98
	    grdPhiDimDegree[dim][degree].push_back(new GrdPhi(this, 
99
100
							      INDEX_OF_DIM(i, dim),
							      j, k));
101
	    // D2-Matrices
102
	    D2PhiDimDegree[dim][degree].push_back(new D2Phi(this, 
103
104
							    INDEX_OF_DIM(i, dim),
							    j, k));
105
106
107
108
	  }
	}
      }
    }
109
    phi = &phiDimDegree[dim][degree];
110
    grdPhi = &grdPhiDimDegree[dim][degree];
111
    d2Phi = &D2PhiDimDegree[dim][degree];
112

Thomas Witkowski's avatar
Thomas Witkowski committed
113
    switch (degree) {
114
115
116
117
118
119
120
121
122
123
124
    case 0:
      refineInter_fct = refineInter0;
      coarseRestr_fct = coarseRestr0;
      coarseInter_fct = coarseInter0;  // not yet implemented
      break;
    case 1:
      refineInter_fct = refineInter1;
      coarseRestr_fct = coarseRestr1;
      coarseInter_fct = NULL;  // not yet implemented
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
125
      switch (dim) {
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
      case 1:
	refineInter_fct = refineInter2_1d;
	coarseRestr_fct = NULL; // not yet implemented
	coarseInter_fct = coarseInter2_1d;
	break;
      case 2: 
	refineInter_fct = refineInter2_2d;
	coarseRestr_fct = coarseRestr2_2d;
	coarseInter_fct = coarseInter2_2d;
	break;
      case 3:
	refineInter_fct = refineInter2_3d;
	coarseRestr_fct = coarseRestr2_3d;
	coarseInter_fct = coarseInter2_3d;
	break;
      default: ERROR_EXIT("invalid dim\n");
      }
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
145
      switch (dim) {
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
      case 1:
	refineInter_fct = refineInter3_1d;
	coarseRestr_fct = coarseRestr3_1d;
	coarseInter_fct = coarseInter3_1d;
	break;
      case 2: 
	refineInter_fct = refineInter3_2d;
	coarseRestr_fct = coarseRestr3_2d;
	coarseInter_fct = coarseInter3_2d;
	break;
      case 3:
	refineInter_fct = refineInter3_3d;
	coarseRestr_fct = coarseRestr3_3d;
	coarseInter_fct = coarseInter3_3d;
	break;
      default: ERROR_EXIT("invalid dim\n");
      }
      break;
    case 4:
Thomas Witkowski's avatar
Thomas Witkowski committed
165
      switch (dim) {
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
      case 1: 
	refineInter_fct = refineInter4_1d;
	coarseRestr_fct = coarseRestr4_1d;
	coarseInter_fct = coarseInter4_1d;
	break;
      case 2:
	refineInter_fct = refineInter4_2d;
	coarseRestr_fct = coarseRestr4_2d;
	coarseInter_fct = coarseInter4_2d;
	break;
      case 3:
	refineInter_fct = refineInter4_3d;
	coarseRestr_fct = coarseRestr4_3d;
	coarseInter_fct = coarseInter4_3d;
	break;
      default: ERROR_EXIT("invalid dim\n");
      }
      break;
    default:
      ERROR_EXIT("invalid degree\n");
    }
  }

  void Lagrange::setNDOF()
  {
191
    if (static_cast<int>(baryDimDegree[dim][degree].size()) == 0) {
192
      ndofDimDegree[dim][degree] = new DimVec<int>(dim, DEFAULT_VALUE, 0);
193

Thomas Witkowski's avatar
Thomas Witkowski committed
194
      if (degree != 0)
195
	(*ndofDimDegree[dim][degree])[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
196
197
      else
	(*ndofDimDegree[dim][degree])[VERTEX] = 0;      
198

199
      for (int i = 1; i < dim + 1; i++) {
200
201
	nBasFcts = getNumberOfDOFs(i, degree);
	(*ndofDimDegree[dim][degree])[INDEX_OF_DIM(i, dim)] = nBasFcts;
202
	for (int j = 0; j < i; j++) {
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	  (*ndofDimDegree[dim][degree])[INDEX_OF_DIM(i, dim)] -= 
	    Global::getGeo(INDEX_OF_DIM(j, dim), i) * 
	    (*ndofDimDegree[dim][degree])[INDEX_OF_DIM(j, dim)];
	}
      }
      nBasFctsDimDegree[dim][degree] = nBasFcts;
    }
    nBasFcts = nBasFctsDimDegree[dim][degree];
    nDOF = ndofDimDegree[dim][degree];
  } 

  DimVec<double> *Lagrange::getCoords(int i) const
  {
    return (*bary)[i];
  }

  void Lagrange::setVertices(int dim, int degree, 
			     GeoIndex position, int positionIndex, int nodeIndex, 
			     int** vertices)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
223
224
    FUNCNAME("Lagrange::setVertices()");

225
    TEST_EXIT_DBG((*vertices) == NULL)("vertices != NULL\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
226

227
228
    int dimOfPosition = DIM_OF_INDEX(position, dim);

Thomas Witkowski's avatar
Thomas Witkowski committed
229
    *vertices = new int[dimOfPosition + 1];
230

231
    if ((degree == 4) && (dimOfPosition==1)) {
232
233
234
235
236
237
238
239
240
      (*vertices)[(nodeIndex != 2) ? 0 : 1] = 
	Global::getReferenceElement(dim)->getVertexOfPosition(position,
							      positionIndex,
							      0);
      (*vertices)[(nodeIndex != 2) ? 1 : 0] = 
	Global::getReferenceElement(dim)->getVertexOfPosition(position,
							      positionIndex, 
							      1);
    } else if ((degree==4) && (dimOfPosition==2)) {
241
242
      for (int i = 0; i < dimOfPosition + 1; i++) {
	(*vertices)[(i+dimOfPosition*nodeIndex) % (dimOfPosition + 1)] = 
243
244
245
246
247
	  Global::getReferenceElement(dim)->getVertexOfPosition(position,
								positionIndex,
								i);
      }    
    } else {
248
249
      for (int i = 0; i < dimOfPosition + 1; i++) {
	(*vertices)[(i+nodeIndex) % (dimOfPosition + 1)] = 
250
251
252
253
254
255
256
	  Global::getReferenceElement(dim)->getVertexOfPosition(position,
								positionIndex,
								i);
      }
    }
  }

257
  Lagrange::Phi::Phi(Lagrange* owner, 
258
259
260
		     GeoIndex position, 
		     int positionIndex, 
		     int nodeIndex)
261
    : vertices(NULL)
262
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
263
264
265
266
267
268
269
270
271
    FUNCNAME("Lagrange::Phi::Phi()");

    // get relevant vertices
    Lagrange::setVertices(owner->getDim(), 
			  owner->getDegree(), 
			  position, 
			  positionIndex, 
			  nodeIndex, 
			  &vertices);
272
273
  
    // set function pointer
274
    switch (owner->getDegree()) {
275
    case 0:
276
      switch (position) {
277
278
279
280
281
282
283
284
      case CENTER:
	func = phi0c;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 1:
285
      switch (position) {
286
287
288
289
290
291
292
293
      case VERTEX:
	func = phi1v;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 2:
294
      switch (position) {
295
296
297
298
      case VERTEX:
	func = phi2v;
	break;
      case EDGE:
299
	TEST_EXIT_DBG(owner->getDim() > 1)("no edge in 1d\n");
300
301
302
	func = phi2e;
	break;
      case CENTER:
303
	TEST_EXIT_DBG(owner->getDim() == 1)("no center dofs for dim != 1\n");
304
305
306
307
308
309
310
	func = phi2e;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 3:
311
      switch (position) {
312
313
314
315
316
317
318
      case VERTEX:
	func = phi3v;
	break;
      case EDGE:
	func = phi3e;
	break;
      case FACE:
319
	TEST_EXIT_DBG(owner->getDim() >= 3)("no faces in dim < 3\n");
320
321
322
	func = phi3f;
	break;
      case CENTER:
323
	switch (owner->getDim()) {
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
	case 1:
	  func = phi3e;
	  break;
	case 2:
	  func = phi3f;
	  break;
	default:
	  ERROR_EXIT("invalid dim\n");
	  break;
	}
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 4:
340
      switch (position) {
341
342
343
344
      case VERTEX:
	func = phi4v;
	break;
      case EDGE:
345
	if (nodeIndex == 1)
346
347
348
349
350
	  func = phi4e1;
	else 
	  func = phi4e0;
	break;
      case FACE:
351
	TEST_EXIT_DBG(owner->getDim() >= 3)("no faces in dim < 3\n");
352
353
354
	func = phi4f;      
	break;
      case CENTER:
355
	switch (owner->getDim()) {
356
	case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
357
	  if (nodeIndex == 1)
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
	    func = phi4e1;
	  else 
	    func = phi4e0;
	  break;
	case 2:
	  func = phi4f;
	  break;
	case 3:
	  func = phi4c;
	  break;
	default:
	  ERROR_EXIT("invalid dim\n");
	  break;
	}
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    default:
      ERROR_EXIT("invalid degree\n");
    }
  }

382
  Lagrange::Phi::~Phi() { 
383
    delete [] vertices; 
384
  }
385
386


387
  Lagrange::GrdPhi::GrdPhi(Lagrange* owner, 
388
389
390
			   GeoIndex position, 
			   int positionIndex, 
			   int nodeIndex)
391
    : vertices(NULL)
392
393
394
395
396
397
398
399
400
401
  {
    // get relevant vertices
    Lagrange::setVertices(owner->getDim(), 
			  owner->getDegree(), 
			  position, 
			  positionIndex, 
			  nodeIndex, 
			  &vertices);

    // set function pointer
402
    switch (owner->getDegree()) {
403
    case 0:
404
      switch (position) {
405
406
407
408
409
410
411
412
      case CENTER:
	func = grdPhi0c;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 1:
413
      switch (position) {
414
415
416
417
418
419
420
421
      case VERTEX:
	func = grdPhi1v;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 2:
422
      switch (position) {
423
424
425
426
427
428
429
      case VERTEX:
	func = grdPhi2v;
	break;
      case EDGE:
	func = grdPhi2e;
	break;
      case CENTER:
430
	TEST_EXIT_DBG(owner->getDim() == 1)("no center dofs for dim != 1\n");
431
432
433
434
435
436
437
	func = grdPhi2e;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 3:
438
      switch (position) {
439
440
441
442
443
444
445
      case VERTEX:
	func = grdPhi3v;
	break;
      case EDGE:
	func = grdPhi3e;
	break;
      case FACE:
446
	TEST_EXIT_DBG(owner->getDim() >= 3)("no faces in dim < 3\n");
447
448
449
	func = grdPhi3f;
	break;
      case CENTER:
450
	switch (owner->getDim()) {
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
	case 1:
	  func = grdPhi3e;
	  break;
	case 2:
	  func = grdPhi3f;
	  break;
	default:
	  ERROR_EXIT("invalid dim\n");
	  break;
	}
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 4:
467
      switch (position) {
468
469
470
471
      case VERTEX:
	func = grdPhi4v;
	break;
      case EDGE:
472
	if (nodeIndex == 1)
473
474
475
476
477
	  func = grdPhi4e1;
	else 
	  func = grdPhi4e0;
	break;
      case FACE:
478
	TEST_EXIT_DBG(owner->getDim() >= 3)("no faces in dim < 3\n");
479
480
481
	func = grdPhi4f;      
	break;
      case CENTER:
482
	switch (owner->getDim()) {
483
	case 1:
484
	  if (nodeIndex == 1)
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
	    func = grdPhi4e1;
	  else 
	    func = grdPhi4e0;
	  break;
	case 2:
	  func = grdPhi4f;
	  break;
	case 3:
	  func = grdPhi4c;
	  break;
	default:
	  ERROR_EXIT("invalid dim\n");
	  break;
	}
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    default:
      ERROR_EXIT("invalid degree\n");
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
509
510
  Lagrange::GrdPhi::~GrdPhi() 
  { 
511
    delete [] vertices; 
512
  }
513

514
  Lagrange::D2Phi::D2Phi(Lagrange* owner, 
515
516
517
			 GeoIndex position, 
			 int positionIndex, 
			 int nodeIndex)
518
    : vertices(NULL)
519
520
521
522
523
524
525
526
527
528
  {
    // get relevant vertices
    Lagrange::setVertices(owner->getDim(), 
			  owner->getDegree(), 
			  position, 
			  positionIndex, 
			  nodeIndex, 
			  &vertices);

    // set function pointer
529
    switch (owner->getDegree()) {
530
    case 0:
531
      switch (position) {
532
533
534
535
536
537
538
539
      case CENTER:
	func = D2Phi0c;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 1:
540
      switch (position) {
541
542
543
544
545
546
547
548
      case VERTEX:
	func = D2Phi1v;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 2:
549
      switch (position) {
550
551
552
553
      case VERTEX:
	func = D2Phi2v;
	break;
      case EDGE:
554
	TEST_EXIT_DBG(owner->getDim() > 1)("no edge in 1d\n");
555
556
557
	func = D2Phi2e;
	break;
      case CENTER:
558
	TEST_EXIT_DBG(owner->getDim() == 1)("no center dofs for dim != 1\n");
559
560
561
562
563
564
565
	func = D2Phi2e;
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 3:
566
      switch (position) {
567
568
569
570
571
572
573
      case VERTEX:
	func = D2Phi3v;
	break;
      case EDGE:
	func = D2Phi3e;
	break;
      case FACE:
574
	TEST_EXIT_DBG(owner->getDim() >= 3)("no faces in dim < 3\n");
575
576
577
	func = D2Phi3f;
	break;
      case CENTER:
578
	switch (owner->getDim()) {
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
	case 1:
	  func = D2Phi3e;
	  break;
	case 2:
	  func = D2Phi3f;
	  break;
	default:
	  ERROR_EXIT("invalid dim\n");
	  break;
	}
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    case 4:
595
      switch (position) {
596
597
598
599
      case VERTEX:
	func = D2Phi4v;
	break;
      case EDGE:
Thomas Witkowski's avatar
Thomas Witkowski committed
600
	if (nodeIndex == 1)
601
602
603
604
605
	  func = D2Phi4e1;
	else 
	  func = D2Phi4e0;
	break;
      case FACE:
606
	TEST_EXIT_DBG(owner->getDim() >= 3)("no faces in dim < 3\n");
607
608
609
	func = D2Phi4f;      
	break;
      case CENTER:
610
	switch (owner->getDim()) {
611
	case 1:
612
	  if (nodeIndex == 1)
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
	    func = D2Phi4e1;
	  else 
	    func = D2Phi4e0;
	  break;
	case 2:
	  func = D2Phi4f;
	  break;
	case 3:
	  func = D2Phi4c;
	  break;
	default:
	  ERROR_EXIT("invalid dim\n");
	  break;
	}
	break;
      default:
	ERROR_EXIT("invalid position\n");
      }
      break;
    default:
      ERROR_EXIT("invalid degree\n");
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
637
638
  Lagrange::D2Phi::~D2Phi() 
  { 
639
    delete [] vertices; 
640
  }
641

Thomas Witkowski's avatar
Thomas Witkowski committed
642
  void Lagrange::createCoords(int* coordInd, int numCoords, int dimIndex, int rest, 
643
644
			      DimVec<double>* vec)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
645
    if (vec == NULL)
646
      vec = new DimVec<double>(dim, DEFAULT_VALUE, 0.0);
647

648
649
    if (dimIndex == numCoords - 1) {
      (*vec)[coordInd[dimIndex]] = double(rest) / degree;
650
      DimVec<double>* newCoords = new DimVec<double>(*vec);
651
652
      bary->push_back(newCoords);
    } else {
653
      for (int i = rest - 1; i >= 1; i--) {
654
	(*vec)[coordInd[dimIndex]] = double(i) / degree;
655
	createCoords(coordInd, numCoords, dimIndex + 1, rest - i, vec);
656
657
658
659
660
661
      }
    }
  }

  void Lagrange::setBary()
  {
662
    bary = &baryDimDegree[dim][degree];
663
664
665

    if (static_cast<int>(bary->size()) == 0) {
      for (int i = 0; i <= dim; i++) { // for all positions
666
	int partsAtPos = Global::getGeo(INDEX_OF_DIM(i, dim), dim);
667
	for (int j = 0; j < partsAtPos; j++) { // for all vertices/edges/faces/...
Thomas Witkowski's avatar
Thomas Witkowski committed
668
	  int *coordInd = new int[i + 1];      // indices of relevant coords
669
	  for (int k = 0; k < i + 1; k++) { 
670
	    coordInd[k] = Global::getReferenceElement(dim)->
Thomas Witkowski's avatar
Thomas Witkowski committed
671
	      getVertexOfPosition(INDEX_OF_DIM(i, dim), j, k);
672
	  }
673
	  createCoords(coordInd, i + 1, 0, degree);
Thomas Witkowski's avatar
Thomas Witkowski committed
674
675
676
	  delete [] coordInd;
	  if (static_cast<int>(bary->size()) == nBasFcts) 
	    return;
677
678
679
680
681
682
683
684
	}
      }
    }
  }

  int Lagrange::getNumberOfDOFs(int dim, int degree)
  {
    int result = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
685
    for (int i = 0; i <= degree; i++)
686
      result += fac(dim - 1 + i) / (fac(i) * fac(dim - 1));
Thomas Witkowski's avatar
Thomas Witkowski committed
687

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
    return result;
  }


  const DegreeOfFreedom *Lagrange::getDOFIndices(const Element *el, 
						 const DOFAdmin& admin,
						 DegreeOfFreedom *idof) const
  {
    WARNING("depricated: use getLocalIndices() instead\n");
    return getLocalIndices(el, &admin, idof);
  }

  int* Lagrange::orderOfPositionIndices(const Element* el, 
					GeoIndex position, 
					int positionIndex) const
  {
    static int sortedVertex = 0;
    static int sortedEdgeDeg2 = 0;
    static int sortedEdgeDeg3[2][2] = {{0, 1}, {1, 0}};
    static int sortedEdgeDeg4[2][3] = {{0, 1, 2}, {2, 1, 0}};
    static int sortedFaceDeg3 = 0;
    static int sortedFaceDeg4[7][3] = {{0,0,0}, {0,2,1}, {1,0,2}, {0,1,2},
				       {2,1,0}, {1,2,0}, {2,0,1}};
    static int sortedCenterDeg4 = 0;

    int dimOfPosition = DIM_OF_INDEX(position, dim);

    // vertex
Thomas Witkowski's avatar
Thomas Witkowski committed
716
717
    if (dimOfPosition == 0)
      return &sortedVertex;   
718
719

    // edge
Thomas Witkowski's avatar
Thomas Witkowski committed
720
721
    if ((dimOfPosition == 1) && (degree == 2))
      return &sortedEdgeDeg2;    
722
723
      
    int vertex[3];
724
    int** dof = const_cast<int**>(el->getDOF());
725
726
    int verticesOfPosition = dimOfPosition + 1;

Thomas Witkowski's avatar
Thomas Witkowski committed
727
    for (int i = 0; i < verticesOfPosition; i++)
728
      vertex[i] = Global::getReferenceElement(dim)->
Thomas Witkowski's avatar
Thomas Witkowski committed
729
730
	getVertexOfPosition(position, positionIndex, i);   
    
731
732
    if (dimOfPosition == 1) {
      if (degree == 3) {
Thomas Witkowski's avatar
Thomas Witkowski committed
733
	if (dof[vertex[0]][0] < dof[vertex[1]][0])
734
	  return sortedEdgeDeg3[0];
Thomas Witkowski's avatar
Thomas Witkowski committed
735
736
	else
	  return sortedEdgeDeg3[1];	
737
      } else { // degree == 4
Thomas Witkowski's avatar
Thomas Witkowski committed
738
	if (dof[vertex[0]][0] < dof[vertex[1]][0])
739
	  return sortedEdgeDeg4[0];
Thomas Witkowski's avatar
Thomas Witkowski committed
740
741
	else
	  return sortedEdgeDeg4[1];	
742
743
744
745
      }
    }

    // face
746
747
    if (dimOfPosition == 2) {
      if (degree == 3) {
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
	return &sortedFaceDeg3;
      } else {  // degree == 4!
	int no = 0;
	const Element *refElem = Global::getReferenceElement(dim);

	if (dof[refElem->getVertexOfPosition(position, positionIndex, 0)][0] <
	    dof[refElem->getVertexOfPosition(position, positionIndex, 1)][0]) 
	  no++;

	if (dof[refElem->getVertexOfPosition(position, positionIndex, 1)][0] <
	    dof[refElem->getVertexOfPosition(position, positionIndex, 2)][0])
	  no += 2;

	if (dof[refElem->getVertexOfPosition(position, positionIndex, 2)][0] <
	    dof[refElem->getVertexOfPosition(position, positionIndex, 0)][0])
	  no += 4;

	return sortedFaceDeg4[no];
      }
    }

    // center
Thomas Witkowski's avatar
Thomas Witkowski committed
770
771
    if ((dimOfPosition == 3) && (degree == 4))
      return &sortedCenterDeg4;    
772
773
774
775
776

    ERROR_EXIT("should not be reached\n");
    return NULL;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
777
  void Lagrange::getBound(const ElInfo* el_info, BoundaryType* bound) const
778
779
780
781
  {
    el_info->testFlag(Mesh::FILL_BOUND);

    // boundaries
782
    int index = 0;
783
784
    int offset = 0;
    BoundaryType boundaryType;
785
    for (int i = dim - 1; i > 0; i--)
786
      offset += Global::getGeo(INDEX_OF_DIM(i, dim), dim);
787

788
    for (int i = 0; i < dim; i++) {
789
790
      int jto = offset + Global::getGeo(INDEX_OF_DIM(i, dim), dim);
      for (int j = offset; j < jto; j++) {
791
	boundaryType = el_info->getBoundary(j);
792
793
	int kto = (*nDOF)[INDEX_OF_DIM(i, dim)];
	for (int k = 0; k < kto; k++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
794
	  bound[index++] = boundaryType;
795
796
	}
      }
797
      offset -= Global::getGeo(INDEX_OF_DIM(i + 1, dim), dim);
798
799
800
    }

    // interior nodes in the center
801
    for (int i = 0; i < (*nDOF)[CENTER]; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
802
      bound[index++] = INTERIOR;
803
804
    }

805
    TEST_EXIT_DBG(index == nBasFcts)("found not enough boundarys\n");
806
807
808
809
810
811
812
813
  }


  const double* Lagrange::interpol(const ElInfo *el_info, 
				   int no, const int *b_no, 
				   AbstractFunction<double, WorldVector<double> > *f, 
				   double *vec)
  {
814
815
816
    FUNCNAME("Lagrange::interpol()");

    static double *inter = NULL;
817
818
819
820
    static int inter_size = 0;

    double *rvec =  NULL;

821
    if (vec) {
822
823
      rvec = vec;
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
824
825
826
      if (inter) 
	delete [] inter;
      inter = new double[nBasFcts];
827
828
829
830
      inter_size = nBasFcts;
      rvec = inter;
    } 

831
    WorldVector<double> x;
832
833

    el_info->testFlag(Mesh::FILL_COORDS);
834
835
836
837
838
839
 
    if (b_no) {
      if (no <= 0  ||  no > getNumber()) {
	ERROR("something is wrong, doing nothing\n");
	rvec[0] = 0.0;
	return(const_cast<const double *>( rvec));
840
      }
841
842
843
844
845
	
      for (int i = 0; i < no; i++) {
	if (b_no[i] < Global::getGeo(VERTEX, dim)) {
	  rvec[i] = (*f)(el_info->getCoord(b_no[i]));
	} else {
846
	  el_info->coordToWorld(*(*bary)[b_no[i]], x);
847
848
849
850
851
852
853
854
	  rvec[i] = (*f)(x);
	}
      }
    } else {
      int vertices = Global::getGeo(VERTEX, dim);
      for (int i = 0; i < vertices; i++)
	rvec[i] = (*f)(el_info->getCoord(i));
      for (int i = vertices; i < nBasFcts; i++) {
855
	el_info->coordToWorld(*(*bary)[i], x);
856
857
858
	rvec[i] = (*f)(x);
      }
    }  
859
860
861
862
863
864
865
866
867
868
    
    return(const_cast<const double *>( rvec));
  }

  const WorldVector<double>* 
  Lagrange::interpol(const ElInfo *el_info, int no, 
		     const int *b_no,
		     AbstractFunction<WorldVector<double>, WorldVector<double> > *f, 
		     WorldVector<double> *vec)
  {
869
870
    FUNCNAME("*Lagrange::interpol_d()");

871
    static WorldVector<double> *inter;
872
    WorldVector<double> *rvec = 
873
      vec ? vec : (inter ? inter : inter = new WorldVector<double>[getNumber()]);
874
    WorldVector<double> x;
875
876
877
878
879

    el_info->testFlag(Mesh::FILL_COORDS);
  
    int vertices = Global::getGeo(VERTEX, dim);

880
881
882
883
884
885
    if (b_no) {
      if (no <= 0  ||  no > getNumber()) {
	ERROR("something is wrong, doing nothing\n");
	for (int i = 0; i < dow; i++)
	  rvec[0][i] = 0.0;
	return(const_cast<const WorldVector<double> *>( rvec));
886
      }
887
888
889
890
      for (int i = 0; i < no; i++) {
	if (b_no[i] < Global::getGeo(VERTEX, dim)) {
	  rvec[i] = (*f)(el_info->getCoord(b_no[i]));
	} else {
891
	  el_info->coordToWorld(*(*bary)[b_no[i]], x);
892
893
894
895
896
897
898
	  rvec[i] = (*f)(x);
	}
      }
    } else {
      for (int i = 0; i < vertices; i++)
	rvec[i] = (*f)(el_info->getCoord(i));
      for (int i = vertices; i < nBasFcts; i++) {
899
	el_info->coordToWorld(*(*bary)[i], x);
900
901
902
	rvec[i] = (*f)(x);
      }
    }  
903
904
905
906
907
908
909
910
911
912
913
914
915
    
    return(const_cast<const WorldVector<double> *>( rvec));
  }


  const DegreeOfFreedom *Lagrange::getLocalIndices(const Element* el,
						   const DOFAdmin *admin,
						   DegreeOfFreedom *indices) const
  {
    static DegreeOfFreedom *localVec = NULL;
    static int localVecSize = 0;

    const DegreeOfFreedom **dof = el->getDOF();
Thomas Witkowski's avatar
Thomas Witkowski committed
916

917
    int nrDOFs, n0, node0, num = 0;
918
919
    GeoIndex posIndex;
    DegreeOfFreedom* result;
Thomas Witkowski's avatar
Thomas Witkowski committed
920

921
    if (indices) {
922
923
      result = indices;
    } else {
924
      if (localVec) 
Thomas Witkowski's avatar
Thomas Witkowski committed
925
926
	delete [] localVec;
      localVec = new DegreeOfFreedom[nBasFcts];
927
928
929
      localVecSize = nBasFcts;
      result = localVec;
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
930

931
    for (int pos = 0, j = 0; pos <= dim; pos++) {
932
933
934
      posIndex = INDEX_OF_DIM(pos, dim);
      nrDOFs = admin->getNumberOfDOFs(posIndex);

Thomas Witkowski's avatar
Thomas Witkowski committed
935
936
937
938
939
940
      if (nrDOFs) {
	n0 = admin->getNumberOfPreDOFs(posIndex);
	node0 = admin->getMesh()->getNode(posIndex);
	num = Global::getGeo(posIndex, dim);

	for (int i = 0; i < num; node0++, i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
941
	  const int *indi = orderOfPositionIndices(el, posIndex, i);
942

Thomas Witkowski's avatar
Thomas Witkowski committed
943
	  for (int k = 0; k < nrDOFs; k++)
Thomas Witkowski's avatar
Thomas Witkowski committed
944
	    result[j++] = dof[node0][n0 + indi[k]];  
945
946
947
	}
      }
    }
948

949
950
951
    return result;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
952
953
954
955
  void Lagrange::getLocalIndicesVec(const Element* el,
				    const DOFAdmin *admin,
				    Vector<DegreeOfFreedom> *indices) const
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
956
    if (indices->getSize() < nBasFcts)
Thomas Witkowski's avatar
Thomas Witkowski committed
957
958
959
960
961
      indices->resize(nBasFcts);

    getLocalIndices(el, admin, &((*indices)[0]));
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
  void Lagrange::getLocalDofPtrVec(const Element *el, 
				   const DOFAdmin *admin,
				   std::vector<const DegreeOfFreedom*>& vec) const
  {
    if (static_cast<int>(vec.size()) < nBasFcts)
      vec.resize(nBasFcts);

    const DegreeOfFreedom **dof = el->getDOF();
    GeoIndex posIndex;
    int nrDOFs, n0, node0, num = 0;

    for (int pos = 0, j = 0; pos <= dim; pos++) {
      posIndex = INDEX_OF_DIM(pos, dim);
      nrDOFs = admin->getNumberOfDOFs(posIndex);

      if (nrDOFs) {
	n0 = admin->getNumberOfPreDOFs(posIndex);
	node0 = admin->getMesh()->getNode(posIndex);
	num = Global::getGeo(posIndex, dim);

	for (int i = 0; i < num; node0++, i++) {
	  const int *indi = orderOfPositionIndices(el, posIndex, i);

	  for (int k = 0; k < nrDOFs; k++)
	    vec[j++] = &(dof[node0][n0 + indi[k]]);
	}
      }
    }
  }

992
993
994
995
996
997
998
999
1000
1001
1002
  void Lagrange::l2ScpFctBas(Quadrature *q,
			     AbstractFunction<WorldVector<double>, WorldVector<double> >* f,
			     DOFVector<WorldVector<double> >* fh)
  {
    ERROR_EXIT("not yet\n");
  }

  void Lagrange::l2ScpFctBas(Quadrature *q,
			     AbstractFunction<double, WorldVector<double> >* f,
			     DOFVector<double>* fh)
  {
1003
1004
1005
1006
1007
1008
1009
    FUNCNAME("Lagrange::l2ScpFctBas()");

    TraverseStack stack;
    double *wdetf_qp = NULL;
    double val, det;
    WorldVector<double> x;
    int iq, j;
1010
1011
1012
    const DegreeOfFreedom  *dof;
    const Parametric *parametric;

1013
    TEST_EXIT_DBG(fh)("no DOF_REAL_VEC fh\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
1014
1015
1016
1017
    TEST_EXIT_DBG(fh->getFESpace())
      ("no fe_space in DOF_REAL_VEC %s\n", fh->getName().c_str());
    TEST_EXIT_DBG(fh->getFESpace()->getBasisFcts() == this)
      ("wrong basis fcts for fh\n");
1018
1019

    Mesh* mesh = fh->getFESpace()->getMesh();
1020
    int n_phi = nBasFcts;
1021

Thomas Witkowski's avatar
Thomas Witkowski committed
1022
    if (!q)
1023
      q = Quadrature::provideQuadrature(dim, 2 * degree - 2);
1024

Thomas Witkowski's avatar
Thomas Witkowski committed
1025
1026
    const FastQuadrature *quad_fast = 
      FastQuadrature::provideFastQuadrature(this, *q, INIT_PHI);
1027

Thomas Witkowski's avatar
Thomas Witkowski committed
1028
    if ((parametric = mesh->getParametric()))
1029
      ERROR_EXIT("not yet implemented\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
1030
    else
Thomas Witkowski's avatar
Thomas Witkowski committed
1031
      wdetf_qp = new double[q->getNumPoints()];
1032

Thomas Witkowski's avatar
Thomas Witkowski committed
1033
1034
    ElInfo *el_info = stack.traverseFirst(mesh, -1, 
					  Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
1035
1036
1037

    int numPoints = q->getNumPoints();
     
1038
1039
1040
    while (el_info) {
      dof = getLocalIndices(el_info->getElement(), (fh->getFESpace()->getAdmin()), NULL);      
      det = el_info->getDet();
1041

1042
      for (iq = 0; iq < numPoints; iq++) {
1043
	el_info->coordToWorld(q->getLambda(iq), x);
1044
1045
1046
1047
1048
1049
	wdetf_qp[iq] = q->getWeight(iq)*det*((*f)(x));
      }
      
      for (j = 0; j < n_phi; j++) {
	for (val = iq = 0; iq < numPoints; iq++)
	  val += quad_fast->getPhi(iq,j)*wdetf_qp[iq];
Thomas Witkowski's avatar
Thomas Witkowski committed
1050

1051
	(*fh)[dof[j]] += val;
1052
1053
      }

1054
1055
1056
      el_info = stack.traverseNext(el_info);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1057
    delete [] wdetf_qp;
1058
1059
1060
1061
1062
1063
1064
  }


  // ===== refineInter functions =====

  void  Lagrange::refineInter0(DOFIndexed<double> *drv, RCNeighbourList* list, int n, BasisFunction* basFct)
  {
1065
    FUNCNAME("Lagrange::refineInter0()");
1066

1067
1068
1069
1070
1071
1072
    if (n < 1) 
      return;

    int n0 = drv->getFESpace()->getAdmin()->getNumberOfPreDOFs(CENTER);
    Element *el = list->getElement(0);
    int node = drv->getFESpace()->getMesh()->getNode(CENTER);
1073
1074
1075
1076
    // Parent center
    DegreeOfFreedom dof0 = el->getDOF(node, n0);           
    // Newest vertex is center 
    DegreeOfFreedom dof_new = el->getChild(0)->getDOF(node, n0);  
1077
1078

    (*drv)[dof_new] = (*drv)[dof0];
1079
1080
    // Newest vertex is center 
    dof_new = el->getChild(1)->getDOF(node, n0);  
1081
1082
1083
    (*drv)[dof_new] = (*drv)[dof0];
  }

1084
1085
  void Lagrange::refineInter1(DOFIndexed<double> *drv, RCNeighbourList* list, int n, 
			      BasisFunction* basFct)
1086
  {
1087
    FUNCNAME("Lagrange::refineInter1_1d()");
1088

1089
1090
    if (n < 1) 
      return;
1091

1092
1093
1094
1095
1096
1097
1098
    int dim = drv->getFESpace()->getMesh()->getDim();
    int n0 = drv->getFESpace()->getAdmin()->getNumberOfPreDOFs(VERTEX);
    Element *el = list->getElement(0);
    DegreeOfFreedom dof0 = el->getDOF(0, n0);           /* 1st endpoint of refinement edge */
    DegreeOfFreedom dof1 = el->getDOF(1, n0);           /* 2nd endpoint of refinement edge */
    DegreeOfFreedom dof_new = el->getChild(0)->getDOF(dim, n0);  /*      newest vertex is DIM */
    (*drv)[dof_new] = 0.5 * ((*drv)[dof0] + (*drv)[dof1]);
1099
1100
  }

1101
1102
  void Lagrange::refineInter2_1d(DOFIndexed<double> *drv, RCNeighbourList* list, int n, 
				 BasisFunction* basFct)
1103
  {
1104
    FUNCNAME("Lagrange::refineInter2_1d()");
1105

1106
1107
1108
1109
1110
1111
    if (n < 1) 
      return;
    
    Element *el = list->getElement(0);
    const DOFAdmin *admin = drv->getFESpace()->getAdmin();
    const DegreeOfFreedom *pdof = basFct->getLocalIndices(el, admin, NULL);
1112
  
1113
1114
    int node = drv->getFESpace()->getMesh()->getNode(VERTEX);        
    int n0 = admin->getNumberOfPreDOFs(VERTEX);
1115
1116
1117
1118
1119

    /****************************************************************************/
    /*  newest vertex of child[0] and child[1]                                  */
    /****************************************************************************/

1120
    DegreeOfFreedom cdof = el->getChild(0)->getDOF(node+1, n0);  /*      newest vertex is DIM */
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
    (*drv)[cdof] = (*drv)[pdof[2]];

    node = drv->getFESpace()->getMesh()->getNode(CENTER);        
    n0 = admin->getNumberOfPreDOFs(CENTER);

    /****************************************************************************/
    /*  midpoint of edge on child[0] at the refinement edge                     */
    /****************************************************************************/
  
    cdof = el->getChild(0)->getDOF(node, n0); 
    (*drv)[cdof] = 
1132
      0.375 * (*drv)[pdof[0]] - 0.125 * (*drv)[pdof[1]] + 0.75 * (*drv)[pdof[2]];
1133
1134
1135
1136
1137
1138
1139

    /****************************************************************************/
    /*  midpoint of edge on child[1] at the refinement edge                     */
    /****************************************************************************/
  
    cdof = el->getChild(1)->getDOF(node, n0); 
    (*drv)[cdof] = 
1140
      -0.125 * (*drv)[pdof[0]] + 0.375 * (*drv)[pdof[1]] + 0.75 * (*drv)[pdof[2]];
1141
1142
1143
1144
1145
1146
  }

  void Lagrange::refineInter2_2d(DOFIndexed<double> *drv, 
				 RCNeighbourList* list, 
				 int n, BasisFunction* basFct)
  {
1147
    FUNCNAME("Lagrange::refineInter2_2d()");
1148

1149
1150
    if (n < 1) 
      return;
1151
1152

    const DOFAdmin *admin = drv->getFESpace()->getAdmin();
1153
1154
    Element *el = list->getElement(0);
    const DegreeOfFreedom *pdof = basFct->getLocalIndices(el, admin, NULL);
1155
  
1156
1157
    int node = drv->getFESpace()->getMesh()->getNode(VERTEX);        
    int n0 = admin->getNumberOfPreDOFs(VERTEX);
1158
1159
1160
1161
1162

    /****************************************************************************/
    /*  newest vertex of child[0] and child[1]                                  */
    /****************************************************************************/

1163
    DegreeOfFreedom cdof = el->getChild(0)->getDOF(node+2, n0);  /*      newest vertex is DIM */
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
    (*drv)[cdof] = (*drv)[pdof[5]];

    node = drv->getFESpace()->getMesh()->getNode(EDGE);        
    n0 = admin->getNumberOfPreDOFs(EDGE);

    /****************************************************************************/
    /*  midpoint of edge on child[0] at the refinement edge                     */
    /****************************************************************************/
  
    cdof = el->getChild(0)->getDOF(node, n0); 
    (*drv)[cdof] = 
1175
      0.375 * (*drv)[pdof[0]] - 0.125 * (*drv)[pdof[1]] + 0.75 * (*drv)[pdof[5]];
1176
1177
1178
1179
1180
1181
1182

    /****************************************************************************/
    /* node in the common edge of child[0] and child[1]                         */
    /****************************************************************************/

    cdof = el->getChild(0)->getDOF(node+1, n0); 
    (*drv)[cdof] =
1183
1184
      -0.125 * ((*drv)[pdof[0]] + (*drv)[pdof[1]]) + 0.25 * (*drv)[pdof[5]]
      + 0.5 * ((*drv)[pdof[3]] + (*drv)[pdof[4]]);
1185
1186
1187
1188
1189
1190
1191

    /****************************************************************************/
    /*  midpoint of edge on child[1] at the refinement edge                     */
    /****************************************************************************/
  
    cdof = el->getChild(1)->getDOF(node+1, n0); 
    (*drv)[cdof] = 
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
      -0.125 * (*drv)[pdof[0]] + 0.375 * (*drv)[pdof[1]]  + 0.75 * (*drv)[pdof[5]];

    if (n > 1) {
      /****************************************************************************/
      /*  adjust the value at the midpoint of the common edge of neigh's children */
      /****************************************************************************/
      el = list->getElement(1);
      pdof = basFct->getLocalIndices(el, admin, NULL);
      
      cdof = el->getChild(0)->getDOF(node+1, n0); 
      (*drv)[cdof] = 
	-0.125 * ((*drv)[pdof[0]] + (*drv)[pdof[1]]) + 0.25 * (*drv)[pdof[5]]
	+ 0.5 * ((*drv)[pdof[3]] + (*drv)[pdof[4]]);
    }