ElInfo1d.cc 10.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "ElInfo1d.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 {
16
17
18
19
20
21
22
23
24
  
  double ElInfo1d::mat_d1_val[2][2] = {{1.0, 0.0}, 
				       {0.0, 1.0}};
  mtl::dense2D<double> ElInfo1d::mat_d1(mat_d1_val);

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

25
26
  double ElInfo1d::mat_d1_right_val[2][2] = {{0.5, 0.0}, 
					     {0.5, 1.0}};
27
  mtl::dense2D<double> ElInfo1d::mat_d1_right(mat_d1_right_val);
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
  double ElInfo1d::mat_d2_val[3][3] = {{1.0, 0.0, 0.0},
				       {0.0, 1.0, 0.0},
				       {0.0, 0.0, 1.0}};
  mtl::dense2D<double> ElInfo1d::mat_d2(mat_d2_val);

  double ElInfo1d::mat_d2_left_val[3][3] = {{1.0, 0.375, 0.0},
					    {0.0, 0.75, 1.0},
					    {0.0, -0.125, 0.0}};
  mtl::dense2D<double> ElInfo1d::mat_d2_left(mat_d2_left_val);
  
  double ElInfo1d::mat_d2_right_val[3][3] = {{0.0, -0.125, 0.0},
					     {1.0, 0.75, 0.0},
					     {0.0, 0.375, 1.0}};
  mtl::dense2D<double> ElInfo1d::mat_d2_right(mat_d2_right_val);
43
 
44

45
46
  void ElInfo1d::fillMacroInfo(const MacroElement * mel)
  {
47
    FUNCNAME("ElInfo1d::fillMacroInfo()");
48
49
50
    Element *nb;
    MacroElement *mnb;

Thomas Witkowski's avatar
Thomas Witkowski committed
51
52
    macroElement = const_cast<MacroElement*>(mel);
    element = const_cast<Element*>(mel->getElement());
Thomas Witkowski's avatar
Thomas Witkowski committed
53
    parent = NULL;
54
    level = 0;
55

Thomas Witkowski's avatar
Thomas Witkowski committed
56
    int vertices = mesh->getGeo(VERTEX);
57

Thomas Witkowski's avatar
Thomas Witkowski committed
58
59
    if (fillFlag.isSet(Mesh::FILL_COORDS) || fillFlag.isSet(Mesh::FILL_DET) ||
	fillFlag.isSet(Mesh::FILL_GRD_LAMBDA)) {
60
      
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
    if (fillFlag.isSet(Mesh::FILL_NEIGH) || fillFlag.isSet(Mesh::FILL_OPP_COORDS)) {
66
67
      WorldVector<double> oppC;
      
Thomas Witkowski's avatar
Thomas Witkowski committed
68
      int neighbours =  mesh->getGeo(NEIGH);
69
70
      for (int i = 0; i < neighbours; i++) {
	nb = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
71
	if ((mnb = const_cast<MacroElement*>(mel->getNeighbour(i)))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
72
	  if (fillFlag.isSet(Mesh::FILL_OPP_COORDS)) {
73
74
75
	    oppC = mnb->coord[i];
	  }

Thomas Witkowski's avatar
Thomas Witkowski committed
76
	  nb = const_cast<Element*>(mnb->getElement());
77
78

	  while (!(nb->isLeaf())) { // make nb nearest element
Thomas Witkowski's avatar
Thomas Witkowski committed
79
	    if (fillFlag.isSet(Mesh::FILL_OPP_COORDS)) {
80
	      if (nb->isNewCoordSet()) {
81
82
83
		oppC = *(nb->getNewCoord());
	      } else {
		oppC = (mel->coord[i] + oppC) * 0.5;
84
	      }
85
	    }
Thomas Witkowski's avatar
Thomas Witkowski committed
86
	    nb = const_cast<Element*>(nb->getChild(1-i));
87
88
	  }

Thomas Witkowski's avatar
Thomas Witkowski committed
89
90
	  if (fillFlag.isSet(Mesh::FILL_OPP_COORDS)) {
	    oppCoord[i] = oppC;
91
	  }
92
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
93
	neighbour[i] = nb;
94
	oppVertex[i] = nb ? i : -1;
95
      }
96
    }
97

Thomas Witkowski's avatar
Thomas Witkowski committed
98
    if (fillFlag.isSet(Mesh::FILL_BOUND) ) {
99
      for (int i = 0; i < vertices; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
100
	boundary[i] = mel->getBoundary(i);
101

Thomas Witkowski's avatar
Thomas Witkowski committed
102
103
      for (int i = 0; i < element->getGeo(PROJECTION); i++)
	projection[i] = mel->getProjection(i);
104
105
106
107
108
109
110
111
    }
  }

  /****************************************************************************/
  /*  compute gradients of basis functions on element; return the absulute    */
  /*  value of the determinante from the transformation to the reference      */
  /*  element                                                                 */
  /****************************************************************************/
Thomas Witkowski's avatar
Thomas Witkowski committed
112
  double ElInfo1d::calcGrdLambda(DimVec<WorldVector<double> >& grd_lam)
113
  {
114
    FUNCNAME("ElInfo1d::calcGrdLambda()");
115
116
117
118

    testFlag(Mesh::FILL_COORDS);

    WorldVector<double> e;
Thomas Witkowski's avatar
Thomas Witkowski committed
119
120
    e = coord[1]; 
    e -= coord[0];
121
    double adet2 = e * e;
122
123
124

    if (adet2 < 1.0E-15) {
      MSG("det*det = %lf\n", adet2);
125
      grd_lam[0] = grd_lam[1] = 0.0;
126
    } else {
127
      grd_lam[1] = e * (1.0 / adet2);
128
      grd_lam[0] = grd_lam[1] * -1.0;
129
130
131
132
133
134
135
136
    }

    return sqrt(adet2);
  }

  const int ElInfo1d::worldToCoord(const WorldVector<double>& x,
				   DimVec<double>* lambda) const
  {
137
138
    FUNCNAME("ElInfo1d::worldToCoord()");

139
    double lmin;
Thomas Witkowski's avatar
Thomas Witkowski committed
140
141
142
    double a = coord[0][0];
    double length = (coord[1][0] - a);
    int dim = mesh->getDim();
143
144
145

    static DimVec<double> vec(dim, NO_INIT);

146
147
    TEST_EXIT_DBG(lambda)("lambda must not be NULL\n");
    TEST_EXIT_DBG(dim == 1)("dim!=1\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
148
    TEST_EXIT_DBG(dimOfWorld == dim)("not yet for DIM != DIM_OF_WORLD\n");
149
150
151
152
153
154

    if (abs(length) < DBL_TOL) {
      ERROR_EXIT("length = %le; abort\n", length);
      return 0;
    }

155
    (*lambda)[1] = (x[0] - a) / length;
156
157
    (*lambda)[0] = 1.0 - (*lambda)[1];

158
    int k = -1;
159
    lmin = 0.0;
160
    for (int i = 0; i <= dim; i++) {
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
      if ((*lambda)[i] < -1.E-5) {
	if ((*lambda)[i] < lmin) {
	  k = i;
	  lmin = (*lambda)[i];
	}
      }
    }

    return k;
  }

  /****************************************************************************/
  /*  calculate a facenormal of edge side of a triangle with coordinates      */
  /*  coord; return the absulute value of the determinant from the           */
  /*  transformation to the reference element                                 */
  /****************************************************************************/
Thomas Witkowski's avatar
Thomas Witkowski committed
177
  double ElInfo1d::getNormal(int side, WorldVector<double> &normal)
178
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
179
    normal = coord[side] - coord[(side + 1) % 2];
180
    double det = norm(&normal);
181
    TEST_EXIT_DBG(det > 1.e-30)("det = 0 on side %d\n", side);
182
183
    normal *= 1.0 / det;

184
    return det;
185
186
187
188
189
190
191
192
  }


  /****************************************************************************/
  /*  calculate the normal of the element for dim of world = 2                */
  /*  return the absulute value of the determinant from the                   */
  /*  transformation to the reference element                                 */
  /****************************************************************************/
Thomas Witkowski's avatar
Thomas Witkowski committed
193
  double ElInfo1d::getElementNormal(WorldVector<double> &elementNormal) const
194
  {
195
196
    FUNCNAME("ElInfo::getElementNormal()");

Thomas Witkowski's avatar
Thomas Witkowski committed
197
198
    TEST_EXIT_DBG(dimOfWorld == 2)
      (" element normal only well defined for  DIM_OF_WORLD = DIM + 1 !!");
199

Thomas Witkowski's avatar
Thomas Witkowski committed
200
201
    elementNormal[0] = coord[1][1] - coord[0][1];
    elementNormal[1] = coord[0][0] - coord[1][0];
202

Thomas Witkowski's avatar
Thomas Witkowski committed
203
    double det = norm(&elementNormal);
204

205
    TEST_EXIT_DBG(det > 1.e-30)("det = 0");
206
207
208

    elementNormal *= 1.0 / det;
    
209
    return det;
210
211
212
  }


213
  void ElInfo1d::fillElInfo(int ichild, const ElInfo *elInfoOld)
214
  {
215
216
    FUNCNAME("ElInfo1d::fillElInfo()");

217
    Element *nb;
Thomas Witkowski's avatar
Thomas Witkowski committed
218
    Element *elem = elInfoOld->element;
219

220
    TEST_EXIT_DBG(elem->getChild(0))("no children?\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
221
    element = const_cast<Element*>(elem->getChild(ichild));
222

Thomas Witkowski's avatar
Thomas Witkowski committed
223
    TEST_EXIT_DBG(element)("missing child %d?\n", ichild);
224

Thomas Witkowski's avatar
Thomas Witkowski committed
225
226
227
    macroElement = elInfoOld->macroElement;
    fillFlag = elInfoOld->fillFlag;
    parent = elem;
228
    level = elInfoOld->level + 1;
229
    iChild = ichild;
230

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

Thomas Witkowski's avatar
Thomas Witkowski committed
233
234
    if (fillFlag.isSet(Mesh::FILL_COORDS) || fillFlag.isSet(Mesh::FILL_DET) ||
	fillFlag.isSet(Mesh::FILL_GRD_LAMBDA)) {
235

Thomas Witkowski's avatar
Thomas Witkowski committed
236
      const FixVec<WorldVector<double>, VERTEX> *old_coord = &(elInfoOld->coord);
237

Thomas Witkowski's avatar
Thomas Witkowski committed
238
      coord[ichild] = (*old_coord)[ichild];
239
      if (elem->isNewCoordSet())
Thomas Witkowski's avatar
Thomas Witkowski committed
240
	coord[1 - ichild] = *(elem->getNewCoord());
241
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
242
	coord[1 - ichild] = ((*old_coord)[0] + (*old_coord)[1]) * 0.5;
243
    }
244

Thomas Witkowski's avatar
Thomas Witkowski committed
245
    if (fillFlag.isSet(Mesh::FILL_NEIGH) || fillFlag.isSet(Mesh::FILL_OPP_COORDS)) {
246
247
      WorldVector<double> oppC;
      
Thomas Witkowski's avatar
Thomas Witkowski committed
248
      TEST_EXIT_DBG(fillFlag.isSet(Mesh::FILL_COORDS))
249
250
251
252
	("FILL_OPP_COORDS only with FILL_COORDS\n");
      
      for (int i = 0; i < neighbours; i++) {
	if (i != ichild) {
253
	  nb = const_cast<Element*>(elem->getChild(1-ichild));  
Thomas Witkowski's avatar
Thomas Witkowski committed
254
255
	  if (fillFlag.isSet(Mesh::FILL_OPP_COORDS))
	    oppC = elInfoOld->coord[i];
256
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
257
	  nb = const_cast<Element*>(elInfoOld->getNeighbour(i));
258

Thomas Witkowski's avatar
Thomas Witkowski committed
259
260
	  if (nb && fillFlag.isSet(Mesh::FILL_OPP_COORDS))
	    oppC = elInfoOld->oppCoord[i];
261
	}
262

263
264
	if (nb) {
	  while (nb->getChild(0)) {  // make nb nearest element
Thomas Witkowski's avatar
Thomas Witkowski committed
265
	    if (fillFlag.isSet(Mesh::FILL_OPP_COORDS)) {
266
	      if (nb->isNewCoordSet())
267
		oppC = *(nb->getNewCoord());
268
	      else
Thomas Witkowski's avatar
Thomas Witkowski committed
269
		oppC = (coord[i] + oppC) * 0.5;
270
	    }
Thomas Witkowski's avatar
Thomas Witkowski committed
271
	    nb = const_cast<Element*>(nb->getChild(1-i));
272
	  }
273

Thomas Witkowski's avatar
Thomas Witkowski committed
274
275
	  if (fillFlag.isSet(Mesh::FILL_OPP_COORDS))
	    oppCoord[i] = oppC;
276
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
277
	neighbour[i] = nb;
278
	oppVertex[i] = nb ? i : -1;
279
      }
280
    }
281

Thomas Witkowski's avatar
Thomas Witkowski committed
282
283
284
    if (fillFlag.isSet(Mesh::FILL_BOUND)) {
      boundary[ichild] = elInfoOld->getBoundary(ichild);
      boundary[1 - ichild] = INTERIOR;
285

286
      if (elInfoOld->getProjection(0) && 
Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
	  elInfoOld->getProjection(0)->getType() == VOLUME_PROJECTION)
	projection[0] = elInfoOld->getProjection(0);      
289
    }   
290
291
  }

292

293
  mtl::dense2D<double>& ElInfo1d::getSubElemCoordsMat(int degree) const
294
  {
295
296
    FUNCNAME("ElInfo1d::getSubElemCoordsMat()");

297
298
    using namespace mtl;

Thomas Witkowski's avatar
Thomas Witkowski committed
299
    if (subElemMatrices[degree].count(std::make_pair(refinementPathLength, refinementPath)) == 0) {
300
301
302
303
304
305
306
307
      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)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
308
	      tmpMat = mat * mat_d1_right;
309
310
	      mat = tmpMat;
	    } else  {
Thomas Witkowski's avatar
Thomas Witkowski committed
311
	      tmpMat = mat * mat_d1_left;
312
313
314
	      mat = tmpMat;
	    }
	  }
315

Thomas Witkowski's avatar
Thomas Witkowski committed
316
	  subElemMatrices[1][std::make_pair(refinementPathLength, refinementPath)] = mat;
317
318
319
320
321
322
323
324
325
326
	}
	
	break;	
      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)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
327
	      tmpMat = mat * mat_d2_right;
328
329
	      mat = tmpMat;
	    } else  {
Thomas Witkowski's avatar
Thomas Witkowski committed
330
	      tmpMat = mat * mat_d2_left;
331
332
333
	      mat = tmpMat;
	    }
	  }
334

Thomas Witkowski's avatar
Thomas Witkowski committed
335
	  subElemMatrices[2][std::make_pair(refinementPathLength, refinementPath)] = mat;  
336
337
338
339
	}
	break;
      default:
	ERROR_EXIT("Not supported for basis function degree: %d\n", degree);
340
      }
341
    }
342

Thomas Witkowski's avatar
Thomas Witkowski committed
343
    return subElemMatrices[degree][std::make_pair(refinementPathLength, refinementPath)];
344
345
  }

346

347
  mtl::dense2D<double>& ElInfo1d::getSubElemGradCoordsMat(int degree) const
348
  {
349
    FUNCNAME("ElInfo1d::getSubElemGradCoordsMat()");
350

351
    TEST_EXIT(degree == 1)("Not supported for basis functions with degree > 1!\n");
352

353
    using namespace mtl;
354

Thomas Witkowski's avatar
Thomas Witkowski committed
355
    if (subElemGradMatrices[degree].count(std::make_pair(refinementPathLength, refinementPath)) == 0) {
356
      dense2D<double> mat(mat_d1);
357

358
359
      for (int i = 0; i < refinementPathLength; i++)
	mat *= 0.5;
360

Thomas Witkowski's avatar
Thomas Witkowski committed
361
      subElemGradMatrices[1][std::make_pair(refinementPathLength, refinementPath)] = mat;
362
    }
363

Thomas Witkowski's avatar
Thomas Witkowski committed
364
    return subElemGradMatrices[degree][std::make_pair(refinementPathLength, refinementPath)];
365
366
  }

367

368
}