ElInfo1d.cc 9.37 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
25
26
27
  
  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);

  double ElInfo1d::mat_d1_right_val[2][2] = {{0.5, 1.0}, 
					     {0.5, 0.0}};
  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
43
44
  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);
  

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

51
52
53
    macroElement_ = const_cast<MacroElement*>( mel);
    element_ = const_cast<Element*>( mel->getElement());
    parent_ = NULL;
54
    level = 0;
55
56
57
58

    int vertices = mesh_->getGeo(VERTEX);

    if (fillFlag_.isSet(Mesh::FILL_COORDS) || fillFlag_.isSet(Mesh::FILL_DET) ||
59
60
	fillFlag_.isSet(Mesh::FILL_GRD_LAMBDA)) {
      
61
      for (int i = 0; i < vertices; i++)
62
	coord_[i] = mel->coord[i];
63
    }
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    if (fillFlag_.isSet(Mesh::FILL_NEIGH) || fillFlag_.isSet(Mesh::FILL_OPP_COORDS)) {
      WorldVector<double> oppC;
      
      int neighbours =  mesh_->getGeo(NEIGH);
      for (int i = 0; i < neighbours; i++) {
	nb = NULL;
	if ((mnb = const_cast<MacroElement*>( mel->getNeighbour(i)))) {
	  if (fillFlag_.isSet(Mesh::FILL_OPP_COORDS)) {
	    oppC = mnb->coord[i];
	  }

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

	  while (!(nb->isLeaf())) { // make nb nearest element
	    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
89
90
	    }
	    nb = const_cast<Element*>( nb->getChild(1-i));
	  }

	  if (fillFlag_.isSet(Mesh::FILL_OPP_COORDS)) {
	    oppCoord_[i] = oppC;
91
	  }
92
93
94
	}
	neighbour_[i] = nb;
	oppVertex_[i] = nb ? i : -1;
95
      }
96
    }
97
98

    if (fillFlag_.isSet(Mesh::FILL_BOUND) ) {
99
      for (int i = 0; i < vertices; i++)
100
101
	boundary_[i] = mel->getBoundary(i);

102
      for (int i = 0; i < element_->getGeo(PROJECTION); i++)
103
104
105
106
107
108
109
110
111
	projection_[i] = mel->getProjection(i);
    }
  }

  /****************************************************************************/
  /*  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;
119
120
121
    e = coord_[1]; 
    e -= coord_[0];
    double adet2 = e * e;
122
123
124

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

    return sqrt(adet2);
  }

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

140
141
142
143
144
145
146
    double lmin;
    double a = coord_[0][0];
    double length = (coord_[1][0] - a);
    int dim = mesh_->getDim();

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

147
148
    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
149
    TEST_EXIT_DBG(dimOfWorld == dim)("not yet for DIM != DIM_OF_WORLD\n");
150
151
152
153
154
155

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

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

159
    int k = -1;
160
    lmin = 0.0;
161
    for (int i = 0; i <= dim; i++) {
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
      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
178
  double ElInfo1d::getNormal(int side, WorldVector<double> &normal)
179
180
  {
    normal = coord_[side] - coord_[(side + 1) % 2];
181
    double det = norm(&normal);
182
    TEST_EXIT_DBG(det > 1.e-30)("det = 0 on side %d\n", side);
183
184
    normal *= 1.0 / det;

185
    return det;
186
187
188
189
190
191
192
193
194
195
  }


  /****************************************************************************/
  /*  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
  {
196
197
    FUNCNAME("ElInfo::getElementNormal()");

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

    elementNormal[0] = coord_[1][1] - coord_[0][1];
    elementNormal[1] = coord_[0][0] - coord_[1][0];

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

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

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


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

218
    Element *nb;
219
    Element *elem = elInfoOld->element_;
220

221
222
223
224
    TEST_EXIT_DBG(elem->getChild(0))("no children?\n");
    element_ = const_cast<Element*>(elem->getChild(ichild));

    TEST_EXIT_DBG(element_)("missing child %d?\n", ichild);
225

226
227
    macroElement_ = elInfoOld->macroElement_;
    fillFlag_ = elInfoOld->fillFlag_;
228
    parent_ = elem;
229
    level = elInfoOld->level + 1;
230
    iChild = ichild;
231
232
233
234

    int neighbours = mesh_->getGeo(NEIGH);

    if (fillFlag_.isSet(Mesh::FILL_COORDS) || fillFlag_.isSet(Mesh::FILL_DET) ||
235
	fillFlag_.isSet(Mesh::FILL_GRD_LAMBDA)) {
236

237
      const FixVec<WorldVector<double>, VERTEX> *old_coord = &(elInfoOld->coord_);
238
239

      coord_[ichild] = (*old_coord)[ichild];
240
      if (elem->isNewCoordSet()) {
241
242
243
	coord_[1 - ichild] = *(elem->getNewCoord());
      } else {
	coord_[1 - ichild] = ((*old_coord)[0] + (*old_coord)[1]) * 0.5;
244
      }
245
    }
246

247
248
249
    if (fillFlag_.isSet(Mesh::FILL_NEIGH) || fillFlag_.isSet(Mesh::FILL_OPP_COORDS)) {
      WorldVector<double> oppC;
      
250
      TEST_EXIT_DBG(fillFlag_.isSet(Mesh::FILL_COORDS))
251
252
253
254
255
256
	("FILL_OPP_COORDS only with FILL_COORDS\n");
      
      for (int i = 0; i < neighbours; i++) {
	if (i != ichild) {
	  nb = const_cast<Element*>( elem->getChild(1-ichild));  
	  if ( fillFlag_.isSet(Mesh::FILL_OPP_COORDS)) {
257
	    oppC = elInfoOld->coord_[i];
258
259
	  }
	} else {
260
	  nb = const_cast<Element*>( elInfoOld->getNeighbour(i));
261

262
	  if (nb && fillFlag_.isSet(Mesh::FILL_OPP_COORDS)) {
263
	    oppC = elInfoOld->oppCoord_[i];
264
	  }
265
	}
266

267
268
269
	if (nb) {
	  while (nb->getChild(0)) {  // make nb nearest element
	    if (fillFlag_.isSet(Mesh::FILL_OPP_COORDS)) {
270
	      if (nb->isNewCoordSet()) {
271
272
273
274
275
276
277
		oppC = *(nb->getNewCoord());
	      } else {
		oppC = (coord_[i] + oppC) * 0.5;
	      }
	    }
	    nb = const_cast<Element*>( nb->getChild(1-i));
	  }
278

279
280
	  if (fillFlag_.isSet(Mesh::FILL_OPP_COORDS)) {
	    oppCoord_[i] = oppC;
281
	  }
282
283
284
	}
	neighbour_[i] = nb;
	oppVertex_[i] = nb ? i : -1;
285
      }
286
    }
287

288
    if (fillFlag_.isSet(Mesh::FILL_BOUND)) {
289
290
      boundary_[ichild] = elInfoOld->getBoundary(ichild);
      boundary_[1 - ichild] = INTERIOR;
291

292
293
294
      if (elInfoOld->getProjection(0) && 
	  elInfoOld->getProjection(0)->getType() == VOLUME_PROJECTION) {
	projection_[0] = elInfoOld->getProjection(0);
295
      }
296
    }   
297
298
  }

299
300
301
  void ElInfo1d::getRefSimplexCoords(const BasisFunction *basisFcts,
				     mtl::dense2D<double>& coords) const
  {
302
    FUNCNAME("ElInfo1d::getRefSimplexCoords()");
303

304
305
306
307
308
309
310
311
312
313
314
    switch (basisFcts->getDegree()) {
    case 1:
      coords = mat_d1;
      break;
    case 2:
      coords = mat_d2;
      break;
    default:
      ERROR_EXIT("Not implemented for basis function with degree %d!\n",\
		 basisFcts->getDegree());
    }
315
316
  }

317
318
319
320
321
322
323
324
325
  void ElInfo1d::getSubElementCoords(const BasisFunction *basisFcts,
				     int iChild,
				     mtl::dense2D<double>& coords) const
  {
    FUNCNAME("ElInfo1d::getSubElementCoords()");

    switch (basisFcts->getDegree()) {
    case 1:
      if (iChild == 0)
326
	coords *= mat_d1_left;
327
      else
328
	coords *= mat_d1_right;
329
330
331

      break;
    case 2:
332
333
334
335
336
      if (iChild == 0)
	coords *= mat_d2_left;
      else
	coords *= mat_d2_right;

337
338
339
340
341
342
      break;
    default:
      ERROR_EXIT("Not yet implemented!\n");
    }
  }

343
}