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

namespace AMDiS {


  int Element::getRegion() const 
  {
    ElementRegion_ED* red_;

15
16
17
18
19
    if (!elementData) 
      return -1;

    red_ = dynamic_cast<ElementRegion_ED*>(elementData->getElementData(ELEMENT_REGION));

20
21
22
23
24
25
26
27
    if (red_) 
      return red_->getRegion();
    
    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
39
40
    dof = mesh->createDOFPtrs();  
  }

  Element::Element(Mesh *aMesh)
  {
    mesh = aMesh;

    index = mesh ? mesh->getNextElementIndex() : -1; 
  
41
42
    child[0] = NULL;
    child[1] = NULL;
43
44
45

    newCoord = NULL;
    elementData = NULL;
46
47
48
49
50
51

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

  // call destructor through Mesh::freeElement !!!
  Element::~Element()
  {
57
58
59
60
    if (child[0]) 
      DELETE child[0];
    if (child[1]) 
      DELETE child[1];
61

62
63
64
    if (newCoord) {
      DELETE newCoord;
    }
65
66
67
68
69
70
71
72
73
74
75
  }

  /****************************************************************************/
  /*  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)						\
76
  ldof = el->dof[n0 + i] + nd0;						\
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    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)
  {
    int     i, j, k, n0, nd, nd0;
    DegreeOfFreedom     *ldof;
  
    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_1(this);
      }
    }

110
    if (mesh->getDim() > 1) {
111
112
113
114
115
116
117
118
119
      if ((nd = admin->getNumberOfDOFs(EDGE)))  {
	nd0 = admin->getNumberOfPreDOFs(EDGE);
	n0 = admin->getMesh()->getNode(EDGE);
	for (i = 0; i < edges; i++) {
	  CHANGE_DOFS_1(this);
	}
      }
    }

120
    if (mesh->getDim() == 3) {
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
      if ((nd = admin->getNumberOfDOFs(FACE)))  {
	nd0 = admin->getNumberOfPreDOFs(FACE);
	n0 = admin->getMesh()->getNode(FACE);
	for (i = 0; i < faces; i++) {
	  CHANGE_DOFS_1(this);
	}
      }
    }

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


  void Element::newDOFFct2(const DOFAdmin* admin)
  {
    int     i, j, k, n0, nd, nd0;
    DegreeOfFreedom     *ldof;
   
    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);
      }
    }

156
    if (mesh->getDim() > 1) {
157
158
159
160
161
162
163
164
165
      if ((nd = admin->getNumberOfDOFs(EDGE)))  {
	nd0 = admin->getNumberOfPreDOFs(EDGE);
	n0 = admin->getMesh()->getNode(EDGE);
	for (i = 0; i < edges; i++) {
	  CHANGE_DOFS_2(this);
	}
      }
    }

166
    if (mesh->getDim() == 3) {
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
      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
  {
194
    int nv = 0, ov = 0;
195
196
197
198

    int vertices = mesh->getGeo(VERTEX);
    int dim = mesh->getDim();

199
200
201
202
203
204
205
206
207
208
209
210
211
    for (int i = 0; i < vertices; i++) {
      if (nv < i-1)  
	return(-1);

      for (int j = 0; j < dim; j++) {
	if (dof[i] == pdof[j]) {
	  /****************************************************************************/
	  /* i is a common vertex                                                     */
	  /****************************************************************************/
	  ov += i;
	  nv++;
	  break;
	}
212
      }
213
214
215
216
217

    }
    
    if (nv != mesh->getDim()) 
      return(-1);
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    /****************************************************************************/
    /*  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:
      return 3-ov;
      break;
    case 3:
      return 6-ov;
      break;
    default:
      ERROR_EXIT("invalid dim\n");
      return 0;
    }
  }

  double Element::getNewCoord(int j) const {
240
241
    if (j >=0 ) {
      TEST_EXIT_DBG(newCoord)("newCoord = NULL\n");
242
243
244
245
246
247
248
249
250
251
252
253
      return (*newCoord)[j];
    } else { 
      return (newCoord!=NULL);
    }
  }

  void Element::eraseNewCoord() {
    if (newCoord!=NULL) {
      DELETE newCoord;
      newCoord=NULL;
    };
  }
254
 
Thomas Witkowski's avatar
Thomas Witkowski committed
255
  void Element::serialize(std::ostream &out) 
256
257
  {
    // write children
258
    if (child[0]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
259
      out << child[0]->getTypeName() << "\n";
260
261
262
      child[0]->serialize(out);
      child[1]->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
263
      out << "NULL\n";
264
265
266
267
268
    }

    // write dofs
    int dim = mesh->getDim();
    int nodes = mesh->getNumberOfNodes();
Thomas Witkowski's avatar
Thomas Witkowski committed
269
    int j = 0;
270
    out.write(reinterpret_cast<const char*>(&nodes), sizeof(int));
Thomas Witkowski's avatar
Thomas Witkowski committed
271
272
   
    for (int pos = 0; pos <= dim; pos++) {
273
      GeoIndex position = INDEX_OF_DIM(pos ,dim);
Thomas Witkowski's avatar
Thomas Witkowski committed
274
      int ndof = 0;
275

Thomas Witkowski's avatar
Thomas Witkowski committed
276
      for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++) {
277
	const DOFAdmin *localAdmin = &mesh->getDOFAdmin(i);
278
	TEST_EXIT_DBG(localAdmin)("no admin[%d]\n", i);
279
280
281
	ndof += localAdmin->getNumberOfDOFs(position);	
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
282
283
284
285
      if (ndof > 0) {
	for (int i = 0; i < mesh->getGeo(position); i++) {
	  if (dof[j] != NULL) {
	    if (Mesh::serializedDOFs[dof[j][0]] == NULL) {
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
	      Mesh::serializedDOFs[dof[j][0]] = dof[j];
	      out.write(reinterpret_cast<const char*>(&ndof), sizeof(int));
	      out.write(reinterpret_cast<const char*>(dof[j]), 
			ndof * sizeof(DegreeOfFreedom));
	    } 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
311
312
    if (newCoord) {
      out << "WorldVector\n";
313
314
      newCoord->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
315
      out << "NULL\n";
316
317
318
    }

    // write element data
Thomas Witkowski's avatar
Thomas Witkowski committed
319
320
    if (elementData) {
      out << elementData->getTypeName() << "\n";
321
322
      elementData->serialize(out);
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
323
      out << "NULL\n";
324
325
326
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
327
  void Element::deserialize(std::istream &in)
328
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
329
    std::string typeName;
330
331
332
333
334

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

Thomas Witkowski's avatar
Thomas Witkowski committed
335
336
    if (typeName != "NULL") {
      if (typeName == "Line") {
337
338
339
	child[0] = new Line(NULL);
	child[1] = new Line(NULL);      
      };
Thomas Witkowski's avatar
Thomas Witkowski committed
340
      if (typeName == "Triangle") {
341
342
343
	child[0] = new Triangle(NULL);
	child[1] = new Triangle(NULL);      
      };
Thomas Witkowski's avatar
Thomas Witkowski committed
344
      if (typeName == "Tetrahedron") {
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
	child[0] = new Tetrahedron(NULL);
	child[1] = new Tetrahedron(NULL);      
      };
      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));

    dof = GET_MEMORY(DegreeOfFreedom*, nodes); 

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

      if(dofs) {
	if(dofs != -1) {
	  dof[i] = GET_MEMORY(DegreeOfFreedom, dofs);
	  in.read(reinterpret_cast<char*>(dof[i]), dofs * sizeof(DegreeOfFreedom));
	  if(Mesh::serializedDOFs[dof[i][0]] != NULL) {
	    //WARNING("christina -> dofs already deserialized\n");
	    DegreeOfFreedom *dofPtr = Mesh::serializedDOFs[dof[i][0]];
	    FREE_MEMORY(dof[i], DegreeOfFreedom, dofs);
	    dof[i] = dofPtr;
	  }
	  Mesh::serializedDOFs[dof[i][0]] = dof[i];
	} 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));

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

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

    if(typeName != "NULL") {
      if(typeName == "WorldVector") {
	newCoord = NEW WorldVector<double>;
	newCoord->deserialize(in);
      } else {
	ERROR_EXIT("unexpected type name\n");
      }
    } else {
      newCoord = NULL;
    }

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

    if(typeName != "NULL") {
      elementData = CreatorMap<ElementData>::getCreator(typeName)->create();
      if(elementData) {
	elementData->deserialize(in);
      } else {
	ERROR_EXIT("unexpected type name\n");
      }
    } else {
      elementData = NULL;
    }
  }

423
424
425
426
427
428
429
430
431
432
433
434
  int Element::calcMemoryUsage()
  {
    int result = 0;

    result += sizeof(Element);
    if (child[0]) {
      result += child[0]->calcMemoryUsage() + child[1]->calcMemoryUsage();
    }

    return result;
  }

435
}