ElInfo1d.cc 10 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
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
    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
71
      for (int i = 0; i < neighbours; i++) {
	nb = NULL;
	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
76
77
78
	    oppC = mnb->coord[i];
	  }

	  nb = const_cast<Element*>( mnb->getElement());

	  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
86
87
88
	    }
	    nb = const_cast<Element*>( nb->getChild(1-i));
	  }

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
193
194
  }


  /****************************************************************************/
  /*  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                                 */
  /****************************************************************************/
  double ElInfo1d::getElementNormal( WorldVector<double> &elementNormal) const
  {
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 {
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
271
272
	    }
	    nb = const_cast<Element*>( nb->getChild(1-i));
	  }
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
287
      if (elInfoOld->getProjection(0) && 
	  elInfoOld->getProjection(0)->getType() == VOLUME_PROJECTION) {
Thomas Witkowski's avatar
Thomas Witkowski committed
288
	projection[0] = elInfoOld->getProjection(0);
289
      }
290
    }   
291
292
  }

293

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

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
    using namespace mtl;

    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;
	    }
	  }
316

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
	  subElemMatrices[1][refinementPath] = mat;  
	}
	
	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)) {
	      tmpMat = mat_d2_right * mat;
	      mat = tmpMat;
	    } else  {
	      tmpMat = mat_d2_left * mat;
	      mat = tmpMat;
	    }
	  }
335

336
337
338
339
340
	  subElemMatrices[2][refinementPath] = mat;  
	}
	break;
      default:
	ERROR_EXIT("Not supported for basis function degree: %d\n", degree);
341
      }
342
    }
343
344

    return subElemMatrices[degree][refinementPath];
345
346
  }

347

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

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

354
    using namespace mtl;
355

356
357
    if (subElemMatrices[degree].count(refinementPath) == 0) {
      dense2D<double> mat(mat_d1);
358

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

362
      subElemGradMatrices[1][refinementPath] = mat;
363
    }
364
365

    return subElemGradMatrices[degree][refinementPath];
366
367
  }

368

369
}