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

namespace AMDiS {

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

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

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

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

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

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

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

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

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

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

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

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

  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
79
	delete tmp;
80
81
82
83
84
85
86
	tmp = NULL;
	return true;
      } else {
	return elementData->deleteDecorated(typeID);
      }
    }
    return false;
87
88
  }

89
90
91
92
93
94
95
96
97
  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
98
      for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++)
99
100
101
102
	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
	  j++;
	}
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
114
    delete [] dof;
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
	ndof += mesh->getDOFAdmin(i).getNumberOfDOFs(position);

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

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

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

181
    if (child[0]) 
182
      el->child[0] = child[0]->cloneWithDOFs();
183
    if (child[1])
184
      el->child[1] = child[1]->cloneWithDOFs();
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
    int i, j, k, n0, nd0;
263
    DegreeOfFreedom  *ldof;
264
265
266
267
    int vertices = mesh->getGeo(VERTEX);
    int edges = mesh->getGeo(EDGE); 
    int faces = mesh->getGeo(FACE);

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

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

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

299
300
    nd = admin->getNumberOfDOFs(CENTER);
    if (nd) {
301
302
      nd0 = admin->getNumberOfPreDOFs(CENTER);
      n0 = admin->getMesh()->getNode(CENTER);
303
304
      // only one center
      i = 0;   
305
306
307
308
309
310
311
312
313
314
315
316
317
318
      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
  {
319
320
    int nv = 0;
    int ov = 0;
321
322
323
    int vertices = mesh->getGeo(VERTEX);
    int dim = mesh->getDim();

324
    for (int i = 0; i < vertices; i++) {
325
      if (nv < i - 1)  
326
327
328
329
330
331
332
333
334
335
336
	return(-1);

      for (int j = 0; j < dim; j++) {
	if (dof[i] == pdof[j]) {
	  /****************************************************************************/
	  /* i is a common vertex                                                     */
	  /****************************************************************************/
	  ov += i;
	  nv++;
	  break;
	}
337
      }
338
339
340
341
342

    }
    
    if (nv != mesh->getDim()) 
      return(-1);
343
344
345
346
347
348
349
350
351
352
    /****************************************************************************/
    /*  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:
353
      return 3 - ov;
354
355
      break;
    case 3:
356
      return 6 - ov;
357
358
359
360
361
362
363
364
      break;
    default:
      ERROR_EXIT("invalid dim\n");
      return 0;
    }
  }

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

    // write dofs
    int dim = mesh->getDim();
    int nodes = mesh->getNumberOfNodes();
Thomas Witkowski's avatar
Thomas Witkowski committed
385
    int j = 0;
386
    SerUtil::serialize(out, nodes);
Thomas Witkowski's avatar
Thomas Witkowski committed
387
388
   
    for (int pos = 0; pos <= dim; pos++) {
389
      GeoIndex position = INDEX_OF_DIM(pos, dim);
Thomas Witkowski's avatar
Thomas Witkowski committed
390
      int ndof = 0;
391

392
393
      for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++)
	ndof += mesh->getDOFAdmin(i).getNumberOfDOFs(position);	      
394

Thomas Witkowski's avatar
Thomas Witkowski committed
395
396
397
      if (ndof > 0) {
	for (int i = 0; i < mesh->getGeo(position); i++) {
	  if (dof[j] != NULL) {
398
399
400
401
402
	    // 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];
403
404
	      SerUtil::serialize(out, ndof);
	      SerUtil::serialize(out, pos);
405
406
	      out.write(reinterpret_cast<const char*>(dof[j]), 
			ndof * sizeof(DegreeOfFreedom));
407
408
	    } else {
	      int minusOne = -1;
409
410
	      SerUtil::serialize(out, minusOne);
	      SerUtil::serialize(out, pos);
411
412
	      out.write(reinterpret_cast<const char*>(&(dof[j][0])), 
			sizeof(DegreeOfFreedom));
413
414
415
	    }
	  } else {
	    int zero = 0;
416
417
	    SerUtil::serialize(out, zero);
	    SerUtil::serialize(out, pos);
418
419
420
421
422
423
424
	  }
	  j++;
	}
      }
    }

    // write index
425
    SerUtil::serialize(out, index);
426
427

    // write mark
428
    SerUtil::serialize(out, mark);
429
430

    // write newCoord
Thomas Witkowski's avatar
Thomas Witkowski committed
431
432
    if (newCoord) {
      out << "WorldVector\n";
433
434
      newCoord->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
435
      out << "NULL\n";
436
437
438
    }

    // write element data
Thomas Witkowski's avatar
Thomas Witkowski committed
439
440
    if (elementData) {
      out << elementData->getTypeName() << "\n";
441
442
      elementData->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
443
      out << "NULL\n";
444
445
446
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
447
  void Element::deserialize(std::istream &in)
448
  {
449
450
    FUNCNAME("Element::deserialize()");

451
    std::string typeName = "";
452
453
454
455
456

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

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

471
472
473
474
475
476
477
478
      child[0]->deserialize(in);
      child[1]->deserialize(in);
    } else {
      child[0] = child[1] = NULL;
    }

    // read dofs
    int nodes;
479
    SerUtil::deserialize(in, nodes);
480

481
    dof = new DegreeOfFreedom*[nodes]; 
482

483
    for (int i = 0; i < nodes; i++) {
484
      int nDofs, pos;
485
486
      SerUtil::deserialize(in, nDofs);
      SerUtil::deserialize(in, pos);
487

488
489
490
491
      if (nDofs) {
	if (nDofs != -1) {
	  dof[i] = new DegreeOfFreedom[nDofs];
	  in.read(reinterpret_cast<char*>(dof[i]), nDofs * sizeof(DegreeOfFreedom));
492
493
494
495
496
497

	  // 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];
498
	    delete [] dof[i];
499
	    dof[i] = dofPtr;
500
	  } else {
501
	    Mesh::serializedDOFs[idx] = dof[i];
502
	  }
503

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

521
    // read mark
522
    SerUtil::deserialize(in, mark);
523
524
525
526
527

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

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

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

543
    if (typeName != "NULL") {
544
      elementData = CreatorMap<ElementData>::getCreator(typeName)->create();
545
546

      if (elementData)
547
	elementData->deserialize(in);
548
549
      else
	ERROR_EXIT("unexpected type name\n");      
550
551
552
553
554
    } else {
      elementData = NULL;
    }
  }

555
556
557
558
559
  int Element::calcMemoryUsage()
  {
    int result = 0;

    result += sizeof(Element);
560
561
    result += mesh->getNumberOfNodes() * sizeof(DegreeOfFreedom*);

562
563
    if (child[0])
      result += child[0]->calcMemoryUsage() + child[1]->calcMemoryUsage();    
564
565
566
567

    return result;
  }

568
}