Element.cc 13.8 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
  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;
     
Thomas Witkowski's avatar
Thomas Witkowski committed
97
      for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++)
98
99
100
101
	ndof += mesh->getDOFAdmin(i).getNumberOfDOFs(position);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
113
    delete [] dof;
114
    
115
    if (child[0])
116
      child[0]->deleteElementDOFs();
117
    if (child[1])
118
119
120
      child[1]->deleteElementDOFs();
  }

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

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

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

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

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

      if (ndof > 0) {
	for (int i = 0; i < mesh->getGeo(position); i++) {
	  if (dof[j] != NULL) {
159
160
161
	    std::pair<DegreeOfFreedom, int> idx = std::make_pair(dof[j][0], pos);

	    if (Mesh::serializedDOFs[idx] == NULL) {
162
163
	      el->dof[j] = new DegreeOfFreedom[ndof];
	      for (int k = 0; k < ndof; k++)
164
		el->dof[j][k] = dof[j][k];
165

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

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

    return el;
  }

188
189
190
191
192
193
194
195
196
  /****************************************************************************/
  /*  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)						\
197
  ldof = el->dof[n0 + i] + nd0;						\
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    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)
  {
216
217
    int j, k, n0, nd, nd0;
    DegreeOfFreedom *ldof;  
218
219
220
221
222
223
224
    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);
225
      for (int i = 0; i < vertices; i++) {
226
227
228
229
	CHANGE_DOFS_1(this);
      }
    }

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

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

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


  void Element::newDOFFct2(const DOFAdmin* admin)
  {
261
262
    int i, j, k, n0, nd, nd0;
    DegreeOfFreedom  *ldof;
263
264
265
266
    int vertices = mesh->getGeo(VERTEX);
    int edges = mesh->getGeo(EDGE); 
    int faces = mesh->getGeo(FACE);

267
    if (nd = admin->getNumberOfDOFs(VERTEX)) {
268
269
270
271
272
273
274
      nd0 = admin->getNumberOfPreDOFs(VERTEX);
      n0 = admin->getMesh()->getNode(VERTEX);
      for (i = 0; i < vertices; i++) {
	CHANGE_DOFS_2(this);
      }
    }

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

285
    if (mesh->getDim() == 3) {
286
      if (nd = admin->getNumberOfDOFs(FACE)) {
287
288
289
290
291
292
293
294
	nd0 = admin->getNumberOfPreDOFs(FACE);
	n0 = admin->getMesh()->getNode(FACE);
	for (i = 0; i < faces; i++) {
	  CHANGE_DOFS_2(this);
	}
      }
    }

295
    if (nd = admin->getNumberOfDOFs(CENTER)) {
296
297
      nd0 = admin->getNumberOfPreDOFs(CENTER);
      n0 = admin->getMesh()->getNode(CENTER);
298
299
      // only one center
      i = 0;   
300
301
302
303
304
305
306
307
308
309
310
311
312
313
      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

387
388
      for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++)
	ndof += mesh->getDOFAdmin(i).getNumberOfDOFs(position);	      
389

Thomas Witkowski's avatar
Thomas Witkowski committed
390
391
392
      if (ndof > 0) {
	for (int i = 0; i < mesh->getGeo(position); i++) {
	  if (dof[j] != NULL) {
393
394
395
396
397
	    // Create index to check if the dofs were already written.
	    std::pair<DegreeOfFreedom, int> idx = std::make_pair(dof[j][0], pos);

	    if (Mesh::serializedDOFs[idx] == NULL) {
	      Mesh::serializedDOFs[idx] = dof[j];
398
	      out.write(reinterpret_cast<const char*>(&ndof), sizeof(int));
399
400
401
	      out.write(reinterpret_cast<const char*>(&pos), sizeof(int));
	      out.write(reinterpret_cast<const char*>(dof[j]), 
			ndof * sizeof(DegreeOfFreedom));
402
403
404
	    } else {
	      int minusOne = -1;
	      out.write(reinterpret_cast<const char*>(&minusOne), sizeof(int));
405
406
407
	      out.write(reinterpret_cast<const char*>(&pos), sizeof(int));
	      out.write(reinterpret_cast<const char*>(&(dof[j][0])), 
			sizeof(DegreeOfFreedom));
408
409
410
411
	    }
	  } else {
	    int zero = 0;
	    out.write(reinterpret_cast<const char*>(&zero), sizeof(int));
412
	    out.write(reinterpret_cast<const char*>(&pos), sizeof(int));
413
414
415
416
417
418
419
420
421
422
423
424
425
	  }
	  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
426
427
    if (newCoord) {
      out << "WorldVector\n";
428
429
      newCoord->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
430
      out << "NULL\n";
431
432
433
    }

    // write element data
Thomas Witkowski's avatar
Thomas Witkowski committed
434
435
    if (elementData) {
      out << elementData->getTypeName() << "\n";
436
437
      elementData->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
438
      out << "NULL\n";
439
440
441
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
442
  void Element::deserialize(std::istream &in)
443
  {
444
445
    FUNCNAME("Element::deserialize()");

Thomas Witkowski's avatar
Thomas Witkowski committed
446
    std::string typeName;
447
448
449
450
451

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

Thomas Witkowski's avatar
Thomas Witkowski committed
452
453
    if (typeName != "NULL") {
      if (typeName == "Line") {
Thomas Witkowski's avatar
Thomas Witkowski committed
454
455
	child[0] = new Line(NULL);
	child[1] = new Line(NULL);      
456
      }else  if (typeName == "Triangle") {
Thomas Witkowski's avatar
Thomas Witkowski committed
457
458
	child[0] = new Triangle(NULL);
	child[1] = new Triangle(NULL);      
459
      } else  if (typeName == "Tetrahedron") {
Thomas Witkowski's avatar
Thomas Witkowski committed
460
461
	child[0] = new Tetrahedron(NULL);
	child[1] = new Tetrahedron(NULL);      
462
463
      } else {
	ERROR_EXIT("Wrong element type!\n");
464
      }
465

466
467
468
469
470
471
472
473
474
475
      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));

476
    dof = new DegreeOfFreedom*[nodes]; 
477

478
    for (int i = 0; i < nodes; i++) {
479
      int nDofs, pos;
480
      in.read(reinterpret_cast<char*>(&nDofs), sizeof(int));
481
      in.read(reinterpret_cast<char*>(&pos), sizeof(int));
482

483
484
485
486
      if (nDofs) {
	if (nDofs != -1) {
	  dof[i] = new DegreeOfFreedom[nDofs];
	  in.read(reinterpret_cast<char*>(dof[i]), nDofs * sizeof(DegreeOfFreedom));
487
488
489
490
491
492

	  // Create index to check if the dofs were alread read from file.
	  std::pair<DegreeOfFreedom, int> idx = std::make_pair(dof[i][0], pos);

	  if (Mesh::serializedDOFs[idx] != NULL) {
	    DegreeOfFreedom *dofPtr = Mesh::serializedDOFs[idx];
493
	    delete [] dof[i];
494
	    dof[i] = dofPtr;
495
	  } else {
496
	    Mesh::serializedDOFs[idx] = dof[i];
497
	  }
498

499
500
501
	} else {
	  DegreeOfFreedom index;
	  in.read(reinterpret_cast<char*>(&index), sizeof(DegreeOfFreedom));
502
503
504

	  std::pair<DegreeOfFreedom, int> idx = std::make_pair(index, pos);
	  TEST_EXIT(Mesh::serializedDOFs.find(idx) !=  Mesh::serializedDOFs.end())
505
	    ("This should never happen!\n");
506
	  dof[i] = Mesh::serializedDOFs[idx];
507
508
509
510
511
	}
      } else {
	dof[i] = NULL;
      }
    }
512
   
513
514
515
    // read index
    in.read(reinterpret_cast<char*>(&index), sizeof(int));

516
    // read mark
517
518
519
520
521
522
    in.read(reinterpret_cast<char*>(&mark), sizeof(signed char));

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

523
524
    if (typeName != "NULL") {
      if (typeName == "WorldVector") {
Thomas Witkowski's avatar
Thomas Witkowski committed
525
	newCoord = new WorldVector<double>;
526
527
528
529
530
531
532
533
534
535
536
537
	newCoord->deserialize(in);
      } else {
	ERROR_EXIT("unexpected type name\n");
      }
    } else {
      newCoord = NULL;
    }

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

538
    if (typeName != "NULL") {
539
      elementData = CreatorMap<ElementData>::getCreator(typeName)->create();
540
541

      if (elementData)
542
	elementData->deserialize(in);
543
544
      else
	ERROR_EXIT("unexpected type name\n");      
545
546
547
548
549
    } else {
      elementData = NULL;
    }
  }

550
551
552
553
554
  int Element::calcMemoryUsage()
  {
    int result = 0;

    result += sizeof(Element);
555
556
    result += mesh->getNumberOfNodes() * sizeof(DegreeOfFreedom*);

557
558
    if (child[0])
      result += child[0]->calcMemoryUsage() + child[1]->calcMemoryUsage();    
559
560
561
562

    return result;
  }

563
}