Element.cc 13 KB
Newer Older
1
2
3
4
5
6
7
8
9
#include "Element.h"
#include "DOFAdmin.h"
#include "Mesh.h"
#include "CoarseningManager.h"
#include "FixVec.h"
#include "ElementRegion_ED.h"

namespace AMDiS {

10
  std::map<DegreeOfFreedom*, bool> Element::deletedDOFs;
11
12
13

  int Element::getRegion() const 
  {
14
15
16
    if (!elementData) 
      return -1;

17
18
    ElementRegion_ED* red = 
      dynamic_cast<ElementRegion_ED*>(elementData->getElementData(ELEMENT_REGION));
19

20
21
    if (red)
      return red->getRegion();    
22
23
24
25
26
27
    
    return -1;
  }

  void Element::setDOFPtrs()
  {
28
    FUNCNAME("Element::setDOFPtrs()");
29
30

    TEST_EXIT_DBG(mesh)("no mesh!\n");
31

32
33
34
35
36
37
38
    dof = mesh->createDOFPtrs();  
  }

  Element::Element(Mesh *aMesh)
  {
    mesh = aMesh;
    index = mesh ? mesh->getNextElementIndex() : -1; 
39
40
    child[0] = NULL;
    child[1] = NULL;
41
42
    newCoord = NULL;
    elementData = NULL;
43
    mark = 0;
44
45
46
47
48
49

    if (mesh) {
      setDOFPtrs();
    } else {
      mesh = NULL;
    }
50
51
52
53
  }

  // call destructor through Mesh::freeElement !!!
  Element::~Element()
54
  {  
55
    if (child[0])
Thomas Witkowski's avatar
Thomas Witkowski committed
56
      delete child[0];
57
58
    
    if (child[1])
Thomas Witkowski's avatar
Thomas Witkowski committed
59
      delete child[1];   
60

61
    if (newCoord)
Thomas Witkowski's avatar
Thomas Witkowski committed
62
      delete newCoord;    
63
64
65

    if (elementData) {
      elementData->deleteDecorated();
Thomas Witkowski's avatar
Thomas Witkowski committed
66
      delete elementData;
67
68
69
70
71
72
73
74
75
76
77
    }
  }

  bool Element::deleteElementData(int typeID)
  {
    FUNCNAME("Element::deleteElementData()");

    if (elementData) {
      if (elementData->isOfType(typeID)) {
	ElementData *tmp = elementData;
	elementData = elementData->getDecorated();
Thomas Witkowski's avatar
Thomas Witkowski committed
78
	delete tmp;
79
80
81
82
83
84
85
	tmp = NULL;
	return true;
      } else {
	return elementData->deleteDecorated(typeID);
      }
    }
    return false;
86
87
  }

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  void Element::deleteElementDOFs()
  {
    int dim = mesh->getDim();
    int j = 0;

    for (int pos = 0; pos <= dim; pos++) {
      GeoIndex position = INDEX_OF_DIM(pos, dim);
      int ndof = 0;
     
      for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++) {
	ndof += mesh->getDOFAdmin(i).getNumberOfDOFs(position);
      }

      if (ndof > 0) {
	for (int i = 0; i < mesh->getGeo(position); i++) {
103
104
105
	  if (dof[j]) {
	    if (deletedDOFs.count(dof[j]) == 0) {
	      deletedDOFs[dof[j]] = true;
106
	      delete [] dof[j];
107
	    }
108
	  }  
109
110
111
112
113
114
	  j++;
	}
      }
    }

    FREE_MEMORY(dof, DegreeOfFreedom*, mesh->getNumberOfNodes());
115
    
116
    if (child[0])
117
      child[0]->deleteElementDOFs();
118
    if (child[1])
119
120
121
      child[1]->deleteElementDOFs();
  }

122
123
124
  Element* Element::cloneWithDOFs()
  {
    Element *el;
125
    
126
    if (isLine()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
127
      el = new Line(NULL);
128
    } else if (isTriangle()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
129
      el = new Triangle(NULL);
130
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
131
      el = new Tetrahedron(NULL);
132
    }
133
134

    el->mesh = mesh;
135
136
137
    el->index = index;
    el->mark = mark;
    if (newCoord) {
Thomas Witkowski's avatar
Thomas Witkowski committed
138
      WorldVector<double> *nc = new WorldVector<double>();
139
140
141
      *nc = *newCoord;
      el->newCoord = nc;
    }
142
    
143
    /* =========== And here we clone the DOFs =========== */
144
   
145
    el->dof = new DegreeOfFreedom*[mesh->getNumberOfNodes()];
146
147
148
149
150
151
152
153

    int dim = mesh->getDim();
    int j = 0;

    for (int pos = 0; pos <= dim; pos++) {
      GeoIndex position = INDEX_OF_DIM(pos, dim);
      int ndof = 0;

154
      for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++)
155
156
157
158
159
160
	ndof += mesh->getDOFAdmin(i).getNumberOfDOFs(position);

      if (ndof > 0) {
	for (int i = 0; i < mesh->getGeo(position); i++) {
	  if (dof[j] != NULL) {
	    if (Mesh::serializedDOFs[dof[j][0]] == NULL) {
161
162
	      el->dof[j] = new DegreeOfFreedom[ndof];
	      for (int k = 0; k < ndof; k++)
163
		el->dof[j][k] = dof[j][k];
164

165
166
167
168
169
170
171
172
173
174
175
	      Mesh::serializedDOFs[dof[j][0]] = el->dof[j];
	    } else {
	      el->dof[j] = Mesh::serializedDOFs[dof[j][0]];
	    }
	  } else {
	    el->dof[j] = NULL;
	  }
	  j++;
	}
      }
    }
176
    
177
178
179
    /* =========== And clone the children ============= */

    if (child[0]) {
180
      el->child[0] = child[0]->cloneWithDOFs();
181
182
    }
    if (child[1]) {
183
      el->child[1] = child[1]->cloneWithDOFs();
184
185
186
187
188
    }

    return el;
  }

189
190
191
192
193
194
195
196
197
  /****************************************************************************/
  /*  ATTENTION:                                                              */
  /*  new_dof_fct() destroys new_dof !!!!!!!!!!                               */
  /*  should be used only at the end of dof_compress()!!!!!                   */
  /****************************************************************************/

  /* CHANGE_DOFS_1 changes old dofs to NEGATIVE new dofs */

#define CHANGE_DOFS_1(el)						\
198
  ldof = el->dof[n0 + i] + nd0;						\
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    for (j = 0; j < nd; j++) {						\
      if ((k = ldof[j]) >= 0) {						\
	/* do it only once! (dofs are visited more than once) */	\
	ldof[j] = - admin->getMesh()->newDOF[k] - 1;			\
      } }

  /* CHANGE_DOFS_2 changes NEGATIVE new dofs to POSITIVE */

#define CHANGE_DOFS_2(el)						\
  ldof = el->dof[n0+i] + nd0;						\
    for (j = 0; j < nd; j++) {						\
      if ((k = ldof[j]) < 0) {						\
	/* do it only once! (dofs are visited more than once) */	\
	ldof[j] = - k - 1;						\
      } }

  void Element::newDOFFct1(const DOFAdmin* admin)
  {
217
218
    int j, k, n0, nd, nd0;
    DegreeOfFreedom *ldof;  
219
220
221
222
223
224
225
    int vertices = mesh->getGeo(VERTEX);
    int edges = mesh->getGeo(EDGE); 
    int faces = mesh->getGeo(FACE);

    if ((nd = admin->getNumberOfDOFs(VERTEX)))  {
      nd0 = admin->getNumberOfPreDOFs(VERTEX);
      n0 = admin->getMesh()->getNode(VERTEX);
226
      for (int i = 0; i < vertices; i++) {
227
228
229
230
	CHANGE_DOFS_1(this);
      }
    }

231
    if (mesh->getDim() > 1) {
232
233
234
      if ((nd = admin->getNumberOfDOFs(EDGE)))  {
	nd0 = admin->getNumberOfPreDOFs(EDGE);
	n0 = admin->getMesh()->getNode(EDGE);
235
	for (int i = 0; i < edges; i++) {
236
237
238
239
240
	  CHANGE_DOFS_1(this);
	}
      }
    }

241
    if (mesh->getDim() == 3) {
242
243
244
      if ((nd = admin->getNumberOfDOFs(FACE)))  {
	nd0 = admin->getNumberOfPreDOFs(FACE);
	n0 = admin->getMesh()->getNode(FACE);
245
	for (int i = 0; i < faces; i++) {
246
247
248
249
250
251
252
253
	  CHANGE_DOFS_1(this);
	}
      }
    }

    if ((nd = admin->getNumberOfDOFs(CENTER)))  {
      nd0 = admin->getNumberOfPreDOFs(CENTER);
      n0 = admin->getMesh()->getNode(CENTER);
254
      int i = 0;          /* only one center */
255
256
257
258
259
260
261
      CHANGE_DOFS_1(this);
    }
  }


  void Element::newDOFFct2(const DOFAdmin* admin)
  {
262
263
    int i, j, k, n0, nd, nd0;
    DegreeOfFreedom  *ldof;
264
265
266
267
268
269
270
271
272
273
274
275
    int vertices = mesh->getGeo(VERTEX);
    int edges = mesh->getGeo(EDGE); 
    int faces = mesh->getGeo(FACE);

    if ((nd = admin->getNumberOfDOFs(VERTEX)))  {
      nd0 = admin->getNumberOfPreDOFs(VERTEX);
      n0 = admin->getMesh()->getNode(VERTEX);
      for (i = 0; i < vertices; i++) {
	CHANGE_DOFS_2(this);
      }
    }

276
    if (mesh->getDim() > 1) {
277
278
279
280
281
282
283
284
285
      if ((nd = admin->getNumberOfDOFs(EDGE)))  {
	nd0 = admin->getNumberOfPreDOFs(EDGE);
	n0 = admin->getMesh()->getNode(EDGE);
	for (i = 0; i < edges; i++) {
	  CHANGE_DOFS_2(this);
	}
      }
    }

286
    if (mesh->getDim() == 3) {
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
      if ((nd = admin->getNumberOfDOFs(FACE)))  {
	nd0 = admin->getNumberOfPreDOFs(FACE);
	n0 = admin->getMesh()->getNode(FACE);
	for (i = 0; i < faces; i++) {
	  CHANGE_DOFS_2(this);
	}
      }
    }

    if ((nd = admin->getNumberOfDOFs(CENTER)))  {
      nd0 = admin->getNumberOfPreDOFs(CENTER);
      n0 = admin->getMesh()->getNode(CENTER);
      i = 0;          /* only one center */
      CHANGE_DOFS_2(this);
    }
  }

#undef CHANGE_DOFS_1
#undef CHANGE_DOFS_2

  /****************************************************************************/
  /* opp_vertex checks whether the face with vertices dof[0],..,dof[DIM-1] is */
  /* part of mel's boundary. returns the opposite vertex if true, -1 else     */
  /****************************************************************************/

  int Element::oppVertex(FixVec<DegreeOfFreedom*, DIMEN> pdof) const
  {
314
315
    int nv = 0;
    int ov = 0;
316
317
318
    int vertices = mesh->getGeo(VERTEX);
    int dim = mesh->getDim();

319
    for (int i = 0; i < vertices; i++) {
320
      if (nv < i - 1)  
321
322
323
324
325
326
327
328
329
330
331
	return(-1);

      for (int j = 0; j < dim; j++) {
	if (dof[i] == pdof[j]) {
	  /****************************************************************************/
	  /* i is a common vertex                                                     */
	  /****************************************************************************/
	  ov += i;
	  nv++;
	  break;
	}
332
      }
333
334
335
336
337

    }
    
    if (nv != mesh->getDim()) 
      return(-1);
338
339
340
341
342
343
344
345
346
347
    /****************************************************************************/
    /*  the opposite vertex is 3(6) - (sum of indices of common vertices) in    */
    /*  2d(3d)                                                                  */
    /****************************************************************************/

    switch(mesh->getDim()) {
    case 1:
      return ov;
      break;
    case 2:
348
      return 3 - ov;
349
350
      break;
    case 3:
351
      return 6 - ov;
352
353
354
355
356
357
358
359
      break;
    default:
      ERROR_EXIT("invalid dim\n");
      return 0;
    }
  }

  void Element::eraseNewCoord() {
360
    if (newCoord != NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
361
      delete newCoord;
362
363
      newCoord = NULL;
    }
364
  }
365
 
Thomas Witkowski's avatar
Thomas Witkowski committed
366
  void Element::serialize(std::ostream &out) 
367
368
  {
    // write children
369
    if (child[0]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
370
      out << child[0]->getTypeName() << "\n";
371
372
373
      child[0]->serialize(out);
      child[1]->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
374
      out << "NULL\n";
375
376
377
378
379
    }

    // write dofs
    int dim = mesh->getDim();
    int nodes = mesh->getNumberOfNodes();
Thomas Witkowski's avatar
Thomas Witkowski committed
380
    int j = 0;
381
    out.write(reinterpret_cast<const char*>(&nodes), sizeof(int));
Thomas Witkowski's avatar
Thomas Witkowski committed
382
383
   
    for (int pos = 0; pos <= dim; pos++) {
384
      GeoIndex position = INDEX_OF_DIM(pos, dim);
Thomas Witkowski's avatar
Thomas Witkowski committed
385
      int ndof = 0;
386

Thomas Witkowski's avatar
Thomas Witkowski committed
387
      for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++) {
388
	ndof += mesh->getDOFAdmin(i).getNumberOfDOFs(position);	
389
390
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
391
392
393
394
      if (ndof > 0) {
	for (int i = 0; i < mesh->getGeo(position); i++) {
	  if (dof[j] != NULL) {
	    if (Mesh::serializedDOFs[dof[j][0]] == NULL) {
395
396
	      Mesh::serializedDOFs[dof[j][0]] = dof[j];
	      out.write(reinterpret_cast<const char*>(&ndof), sizeof(int));
397
	      out.write(reinterpret_cast<const char*>(dof[j]), ndof * sizeof(DegreeOfFreedom));
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
	    } else {
	      int minusOne = -1;
	      out.write(reinterpret_cast<const char*>(&minusOne), sizeof(int));
	      out.write(reinterpret_cast<const char*>(&(dof[j][0])), sizeof(DegreeOfFreedom));
	    }
	  } else {
	    int zero = 0;
	    out.write(reinterpret_cast<const char*>(&zero), sizeof(int));
	  }
	  j++;
	}
      }
    }

    // write index
    out.write(reinterpret_cast<const char*>(&index), sizeof(int));

    // write mark
    out.write(reinterpret_cast<const char*>(&mark), sizeof(signed char));

    // write newCoord
Thomas Witkowski's avatar
Thomas Witkowski committed
419
420
    if (newCoord) {
      out << "WorldVector\n";
421
422
      newCoord->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
423
      out << "NULL\n";
424
425
426
    }

    // write element data
Thomas Witkowski's avatar
Thomas Witkowski committed
427
428
    if (elementData) {
      out << elementData->getTypeName() << "\n";
429
430
      elementData->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
431
      out << "NULL\n";
432
433
434
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
435
  void Element::deserialize(std::istream &in)
436
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
437
    std::string typeName;
438
439
440
441
442

    // read children
    in >> typeName;
    in.get();

Thomas Witkowski's avatar
Thomas Witkowski committed
443
444
    if (typeName != "NULL") {
      if (typeName == "Line") {
Thomas Witkowski's avatar
Thomas Witkowski committed
445
446
	child[0] = new Line(NULL);
	child[1] = new Line(NULL);      
447
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
448
      if (typeName == "Triangle") {
Thomas Witkowski's avatar
Thomas Witkowski committed
449
450
	child[0] = new Triangle(NULL);
	child[1] = new Triangle(NULL);      
451
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
452
      if (typeName == "Tetrahedron") {
Thomas Witkowski's avatar
Thomas Witkowski committed
453
454
	child[0] = new Tetrahedron(NULL);
	child[1] = new Tetrahedron(NULL);      
455
      }
456
457
458
459
460
461
462
463
464
465
      child[0]->deserialize(in);
      child[1]->deserialize(in);
    } else {
      child[0] = child[1] = NULL;
    }

    // read dofs
    int nodes;
    in.read(reinterpret_cast<char*>(&nodes), sizeof(int));

466
    dof = new DegreeOfFreedom*[nodes]; 
467

468
    for (int i = 0; i < nodes; i++) {
469
470
471
      int dofs;
      in.read(reinterpret_cast<char*>(&dofs), sizeof(int));

472
473
      if (dofs) {
	if (dofs != -1) {
474
	  dof[i] = new DegreeOfFreedom[dofs];
475
	  in.read(reinterpret_cast<char*>(dof[i]), dofs * sizeof(DegreeOfFreedom));
476
	  if (Mesh::serializedDOFs[dof[i][0]] != NULL) {
477
	    DegreeOfFreedom *dofPtr = Mesh::serializedDOFs[dof[i][0]];
478
	    delete [] dof[i];
479
	    dof[i] = dofPtr;
480
481
	  } else {
	    Mesh::serializedDOFs[dof[i][0]] = dof[i];
482
483
484
485
486
487
488
489
490
491
492
493
494
495
	  }
	} else {
	  DegreeOfFreedom index;
	  in.read(reinterpret_cast<char*>(&index), sizeof(DegreeOfFreedom));
	  dof[i] = Mesh::serializedDOFs[index];
	}
      } else {
	dof[i] = NULL;
      }
    }
    
    // read index
    in.read(reinterpret_cast<char*>(&index), sizeof(int));

496
    // read mark
497
498
499
500
501
502
    in.read(reinterpret_cast<char*>(&mark), sizeof(signed char));

    // read newCoord
    in >> typeName;
    in.get();

503
504
    if (typeName != "NULL") {
      if (typeName == "WorldVector") {
Thomas Witkowski's avatar
Thomas Witkowski committed
505
	newCoord = new WorldVector<double>;
506
507
508
509
510
511
512
513
514
515
516
517
	newCoord->deserialize(in);
      } else {
	ERROR_EXIT("unexpected type name\n");
      }
    } else {
      newCoord = NULL;
    }

    // read element data
    in >> typeName;
    in.get();

518
    if (typeName != "NULL") {
519
      elementData = CreatorMap<ElementData>::getCreator(typeName)->create();
520
      if (elementData) {
521
522
523
524
525
526
527
528
529
	elementData->deserialize(in);
      } else {
	ERROR_EXIT("unexpected type name\n");
      }
    } else {
      elementData = NULL;
    }
  }

530
531
532
533
534
  int Element::calcMemoryUsage()
  {
    int result = 0;

    result += sizeof(Element);
535
536
    result += mesh->getNumberOfNodes() * sizeof(DegreeOfFreedom*);

537
538
539
540
541
542
543
    if (child[0]) {
      result += child[0]->calcMemoryUsage() + child[1]->calcMemoryUsage();
    }

    return result;
  }

544
}