ElementObjectDatabase.cc 39.3 KB
Newer Older
1
2
3
4
5
6
7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9
10
11
12
13
14
15
16
17
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
18
 *
19
 ******************************************************************************/
20
21


22
#include "VertexVector.h"
23
#include "parallel/ElementObjectDatabase.h"
24
#include "parallel/MeshLevelData.h"
25
#include <fstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
26

27
28
#include "Serializer.h"

29
30
using namespace std;

31
namespace AMDiS { namespace Parallel {
32
33

  void ElementObjectDatabase::init(vector<Mesh*>& meshes_,
34
35
36
				   map<Mesh*, vector<const FiniteElemSpace*> >& meshToFeSpaces)
  {
    meshes = meshes_;
37
    for(size_t i = 0; i < meshes.size(); i++)
38
      feSpaces.push_back(meshToFeSpaces[meshes[i]][0]);
39

40
41
42
    macroMesh = meshes[0];
    feSpace = meshToFeSpaces[macroMesh][0];
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
  void ElementObjectData::serialize(std::ostream &out) const
  {
    SerUtil::serialize(out, elIndex);
    SerUtil::serialize(out, ithObject);
  }

  /// Read this element object from disk.
  void ElementObjectData::deserialize(std::istream &in)
  {
    SerUtil::deserialize(in, elIndex);
    SerUtil::deserialize(in, ithObject);
  }


58
  void ElementObjectDatabase::create(map<int, int>& rankMap, MeshLevelData& ld)
59
  {
60
    FUNCNAME_DBG("ElementObjectDatabase::create()");
61
62
63
64

    macroElementRankMap = &rankMap;
    levelData = &ld;

65
    // === Reset temporary data ===
66

67
68
69
70
    tmpVertexElements.clear();
    tmpEdgeElements.clear();
    tmpFaceElements.clear();

71
    // === Fills macro element data structures. ===
72

73
    TraverseStack stack;
74
    ElInfo* elInfo = NULL;
75

76
    for(size_t i = 0; i < meshes.size(); i++) {
77

78
      elInfo = stack.traverseFirst(meshes[i], -1,
79
80
				   Mesh::CALL_LEAF_EL |
				   Mesh::FILL_NEIGH |
81
82
83
				   Mesh::FILL_BOUND);
      while (elInfo) {
	TEST_EXIT_DBG(elInfo->getLevel() == 0)("Should not happen!\n");
84

85
	Element *el = elInfo->getElement();
86

87
88
89
	// Macro data info is stored once.
	if (i == 0) {
	  macroElIndexTypeMap.insert(make_pair(el->getIndex(), elInfo->getType()));
90

91
92
	  // Add all sub object of the element to the variable elObjDb.
	  addElement(el);
93

94
95
	  addElementPeriodicBoundary(elInfo);
	}
96

97
98
        // Element pointer has to be stored for each mesh one copy.
	macroElIndexMap[el->getIndex()][meshes[i]] = el;
99

100
101
102
	elInfo = stack.traverseNext(elInfo);
      }
    }
103

104
    // === Move temporary data to original one ===
105

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    for (map<DegreeOfFreedom, vector<ElementObjectData> >::iterator it = tmpVertexElements.begin();
	 it != tmpVertexElements.end(); ++it)
      vertexElements[it->first] = it->second;
    tmpVertexElements.clear();

    for (map<DofEdge, vector<ElementObjectData> >::iterator it = tmpEdgeElements.begin();
	 it != tmpEdgeElements.end(); ++it)
      edgeElements[it->first] = it->second;
    tmpEdgeElements.clear();

    for (map<DofFace, vector<ElementObjectData> >::iterator it = tmpFaceElements.begin();
	 it != tmpFaceElements.end(); ++it)
      faceElements[it->first] = it->second;
    tmpFaceElements.clear();

Thomas Witkowski's avatar
Thomas Witkowski committed
121
    // Handle periodic boudaries
122
    createPeriodicData();
123
124
125
126

    // Create data about the reverse modes of neighbouring elements.
    createReverseModeData();
  }
127

128
  void ElementObjectDatabase::createMacroElementInfo(std::map<Mesh*, std::vector<MacroElement*> >& mel)
129
130
131
  {
    macroElIndexMap.clear();
    macroElIndexTypeMap.clear();
132

133
134
135
136
137
138
139
140
141
142
    std::map<Mesh*, std::vector<MacroElement*> >::iterator iter = mel.begin();
    while(iter != mel.end()) {
      Mesh* mesh = iter->first;
      for (vector<MacroElement*>::iterator it = iter->second.begin();
	   it != iter->second.end(); it++) {
	macroElIndexMap[(*it)->getIndex()][mesh] = (*it)->getElement();
        if(iter == mel.begin())
          macroElIndexTypeMap.insert(make_pair((*it)->getIndex(), (*it)->getElType()));
      }
      iter++;
143
144
    }
  }
145

146
  void ElementObjectDatabase::addElement(Element *el)
147
148
149
150
151
  {
    // === First, add all element objects to the database. ===

    for (int i = 0; i < el->getGeo(VERTEX); i++)
      addVertex(el, i);
152

153
    for (int i = 0; i < el->getGeo(EDGE); i++)
154
155
      addEdge(el, i);

156
157
    for (int i = 0; i < el->getGeo(FACE); i++)
      addFace(el, i);
158
  }
159

160
161
162
  void ElementObjectDatabase::addElementPeriodicBoundary(ElInfo* elInfo)
  {
    FUNCNAME("ElementObjectDatabase::addElementPeriodicBoundary()");
163

164
    TEST_EXIT_DBG(macroMesh)("Mesh not set!\n");
165

166
    Element *el = elInfo->getElement();
167

168
    switch (macroMesh->getDim()) {
169
170
    case 2:
      for (int i = 0; i < el->getGeo(EDGE); i++) {
171
	if (macroMesh->isPeriodicAssociation(elInfo->getBoundary(EDGE, i))) {
172
173
	  // The current element's i-th edge is periodic.
	  Element *neigh = elInfo->getNeighbour(i);
174
175

	  TEST_EXIT_DBG(neigh)("Should not happen!\n");
176

177
178
179
	  DofEdge edge0 = el->getEdge(i);
	  DofEdge edge1 = neigh->getEdge(elInfo->getOppVertex(i));
	  BoundaryType boundaryType = elInfo->getBoundary(EDGE, i);
180

Thomas Witkowski's avatar
Thomas Witkowski committed
181
182
183
	  // Add the periodic edge.
	  periodicEdges[make_pair(edge0, edge1)] = boundaryType;
	  periodicEdgeAssoc[edge0].insert(edge1);
184

Thomas Witkowski's avatar
Thomas Witkowski committed
185
	  // Add both vertices of the edge to be periodic.
186
	  DegreeOfFreedom mappedDof0 =
187
	    macroMesh->getPeriodicAssociations(boundaryType)[edge0.first];
188
	  DegreeOfFreedom mappedDof1 =
189
	    macroMesh->getPeriodicAssociations(boundaryType)[edge0.second];
190
191
192
	  periodicVertices[make_pair(edge0.first, mappedDof0)] = boundaryType;
	  periodicVertices[make_pair(edge0.second, mappedDof1)] = boundaryType;

Thomas Witkowski's avatar
Thomas Witkowski committed
193
	  periodicDofAssoc[edge0.first].insert(boundaryType);
194
	  periodicDofAssoc[edge0.second].insert(boundaryType);
195
196
197
198
199
	}
      }
      break;
    case 3:
      for (int i = 0; i < el->getGeo(FACE); i++) {
200
	if (macroMesh->isPeriodicAssociation(elInfo->getBoundary(FACE, i))) {
201
202
	  // The current element's i-th face is periodic.
	  Element *neigh = elInfo->getNeighbour(i);
203
204

	  TEST_EXIT_DBG(neigh)("Should not happen!\n");
205

206
207
208
	  DofFace face0 = el->getFace(i);
	  DofFace face1 = neigh->getFace(elInfo->getOppVertex(i));
	  BoundaryType boundaryType = elInfo->getBoundary(FACE, i);
209

210
211
	  // Add the periodic face.
	  periodicFaces[make_pair(face0, face1)] = elInfo->getBoundary(i);
212

213
	  /// Add all three vertices of the face to be periodic.
214
	  DegreeOfFreedom mappedDof0 =
215
	    macroMesh->getPeriodicAssociations(boundaryType)[face0.get<0>()];
216
	  DegreeOfFreedom mappedDof1 =
217
	    macroMesh->getPeriodicAssociations(boundaryType)[face0.get<1>()];
218
	  DegreeOfFreedom mappedDof2 =
219
	    macroMesh->getPeriodicAssociations(boundaryType)[face0.get<2>()];
220
221
222
223

	  periodicVertices[make_pair(face0.get<0>(), mappedDof0)] = boundaryType;
	  periodicVertices[make_pair(face0.get<1>(), mappedDof1)] = boundaryType;
	  periodicVertices[make_pair(face0.get<2>(), mappedDof2)] = boundaryType;
224

225
226
227
	  periodicDofAssoc[face0.get<0>()].insert(boundaryType);
	  periodicDofAssoc[face0.get<1>()].insert(boundaryType);
	  periodicDofAssoc[face0.get<2>()].insert(boundaryType);
228
229

	  TEST_EXIT_DBG(face0.get<0>() ==
230
			macroMesh->getPeriodicAssociations(boundaryType)[face1.get<0>()] &&
231
			face0.get<1>() ==
232
			macroMesh->getPeriodicAssociations(boundaryType)[face1.get<1>()] &&
233
			face0.get<2>() ==
234
			macroMesh->getPeriodicAssociations(boundaryType)[face1.get<2>()])
235
	    ("Should not happen!\n");
236

237
238
239
240
241
242
243
	  // Create all three edges of the element and add them to be periodic.
	  DofEdge elEdge0 = make_pair(face0.get<0>(), face0.get<1>());
	  DofEdge elEdge1 = make_pair(face0.get<0>(), face0.get<2>());
	  DofEdge elEdge2 = make_pair(face0.get<1>(), face0.get<2>());
	  DofEdge neighEdge0 = make_pair(face1.get<0>(), face1.get<1>());
	  DofEdge neighEdge1 = make_pair(face1.get<0>(), face1.get<2>());
	  DofEdge neighEdge2 = make_pair(face1.get<1>(), face1.get<2>());
244

245
246
247
	  periodicEdges[make_pair(elEdge0, neighEdge0)] = boundaryType;
	  periodicEdges[make_pair(elEdge1, neighEdge1)] = boundaryType;
	  periodicEdges[make_pair(elEdge2, neighEdge2)] = boundaryType;
248

249
250
251
252
253
254
255
256
	  periodicEdgeAssoc[elEdge0].insert(neighEdge0);
	  periodicEdgeAssoc[elEdge1].insert(neighEdge1);
	  periodicEdgeAssoc[elEdge2].insert(neighEdge2);
	}
      }
      break;
    default:
      ERROR_EXIT("Should not happen!\n");
257
258
    }

259
260
  }

261

Thomas Witkowski's avatar
Thomas Witkowski committed
262
263
264
265
266
  void ElementObjectDatabase::addVertex(Element *el, int ith)
  {
    DegreeOfFreedom vertex = el->getDof(ith, 0);
    int elIndex = el->getIndex();
    ElementObjectData elObj(elIndex, ith);
267

268
    tmpVertexElements[vertex].push_back(elObj);
Thomas Witkowski's avatar
Thomas Witkowski committed
269
270
271
272
273
274
275
276
277
    vertexLocalMap[elObj] = vertex;
  }


  void ElementObjectDatabase::addEdge(Element *el, int ith)
  {
    DofEdge edge = el->getEdge(ith);
    int elIndex = el->getIndex();
    ElementObjectData elObj(elIndex, ith);
278

279
    tmpEdgeElements[edge].push_back(elObj);
Thomas Witkowski's avatar
Thomas Witkowski committed
280
281
282
    edgeLocalMap[elObj] = edge;
  }

283

Thomas Witkowski's avatar
Thomas Witkowski committed
284
285
286
287
288
  void ElementObjectDatabase::addFace(Element *el, int ith)
  {
    DofFace face = el->getFace(ith);
    int elIndex = el->getIndex();
    ElementObjectData elObj(elIndex, ith);
289

290
    tmpFaceElements[face].push_back(elObj);
Thomas Witkowski's avatar
Thomas Witkowski committed
291
292
293
    faceLocalMap[elObj] = face;
  }

294

295
  void ElementObjectDatabase::createPeriodicData()
296
  {
297
    FUNCNAME_DBG("ElementObjectDatabase::createPeriodicData()");
298

299
    TEST_EXIT_DBG(macroMesh)("Mesh not set!\n");
300

301
302
    // === Return, if there are no periodic vertices, i.e., there are no  ===
    // === periodic boundaries in the mesh.                               ===
303

304
305
    if (periodicVertices.size() == 0)
      return;
306

307
308
309
    // === Calculate smallest periodic boundary ID in mesh. ===

    smallestPeriodicBcType = 0;
310
311
    for (map<BoundaryType, VertexVector*>::iterator it = macroMesh->getPeriodicAssociations().begin();
	 it != macroMesh->getPeriodicAssociations().end(); ++it)
312
      smallestPeriodicBcType = std::min(smallestPeriodicBcType, it->first);
313
314

    // === Get all vertex DOFs that have multiple periodic associations. ===
315

316
    // We group all vertices together, that have either two or three periodic
317
318
319
    // associations. For rectangular domains in 2D, the four corner vertices have
    // all two periodic associations. For box domains in 3D, the eight corner
    // vertices have all three periodic associations.
320
321
322
323

    vector<DegreeOfFreedom> multPeriodicDof2, multPeriodicDof3;
    for (map<DegreeOfFreedom, std::set<BoundaryType> >::iterator it = periodicDofAssoc.begin();
	 it != periodicDofAssoc.end(); ++it) {
324
325
      TEST_EXIT_DBG((macroMesh->getDim() == 2 && it->second.size() <= 2) ||
		    (macroMesh->getDim() == 3 && it->second.size() <= 3))
326
	("Should not happen!\n");
327

328
329
330
331
332
333
      if (it->second.size() == 2)
	multPeriodicDof2.push_back(it->first);
      if (it->second.size() == 3)
	multPeriodicDof3.push_back(it->first);
    }

334
    if (macroMesh->getDim() == 2) {
335
      TEST_EXIT_DBG(multPeriodicDof2.size() == 0 ||
336
337
338
339
		    multPeriodicDof2.size() == 4)
	("Should not happen (%d)!\n", multPeriodicDof2.size());
      TEST_EXIT_DBG(multPeriodicDof3.size() == 0)("Should not happen!\n");
    }
340

341
    if (macroMesh->getDim() == 3) {
342
      TEST_EXIT_DBG(multPeriodicDof3.size() == 0 ||
343
344
345
		    multPeriodicDof3.size() == 8)
	("Should not happen (%d)!\n", multPeriodicDof3.size());
    }
346

347
348
349
350
351
    if (multPeriodicDof2.size() > 0) {
      for (unsigned int i = 0; i < multPeriodicDof2.size(); i++) {
	DegreeOfFreedom dof0 = multPeriodicDof2[i];
	if (dof0 == -1)
	  continue;
352

353
354
355
356
	DegreeOfFreedom dof1 = -1;
	DegreeOfFreedom dof2 = -1;
	BoundaryType type0 = *(periodicDofAssoc[dof0].begin());
	BoundaryType type1 = *(++(periodicDofAssoc[dof0].begin()));
357

358
359
360
361
362
363
	for (PerBoundMap<DegreeOfFreedom>::iterator it = periodicVertices.begin();
	     it != periodicVertices.end(); ++it) {
	  if (it->first.first == dof0 && it->second == type0)
	    dof1 = it->first.second;
	  if (it->first.first == dof0 && it->second == type1)
	    dof2 = it->first.second;
364

365
366
367
	  if (dof1 != -1 && dof2 != -1)
	    break;
	}
368

369
	TEST_EXIT_DBG(dof1 != -1 && dof2 != -1)("Should not happen!\n");
370

371
372
373
374
375
	DegreeOfFreedom dof3 = -1;
	for (PerBoundMap<DegreeOfFreedom>::iterator it = periodicVertices.begin();
	     it != periodicVertices.end(); ++it) {
	  if (it->first.first == dof1 && it->second == type1) {
	    dof3 = it->first.second;
376

377
378
	    TEST_EXIT_DBG(periodicVertices[make_pair(dof2, dof3)] == type0)
	      ("Should not happen!\n");
379

380
381
382
	    break;
	  }
	}
383

384
385
386
387
388
	TEST_EXIT_DBG(dof3 != -1)("Should not happen!\n");
	TEST_EXIT_DBG(periodicVertices.count(make_pair(dof0, dof3)) == 0)
	  ("Should not happen!\n");
	TEST_EXIT_DBG(periodicVertices.count(make_pair(dof3, dof0)) == 0)
	  ("Should not happen!\n");
389

390
 	periodicVertices[make_pair(dof0, dof3)] =
391
	  provideConnectedPeriodicBoundary(type0, type1);
392
 	periodicVertices[make_pair(dof3, dof0)] =
393
 	  provideConnectedPeriodicBoundary(type0, type1);
394

395
396
397
398
399
400
401
402
	for (unsigned int j = i + 1; j < multPeriodicDof2.size(); j++)
	  if (multPeriodicDof2[j] == dof3)
	    multPeriodicDof2[j] = -1;
      }
    }

    if (multPeriodicDof3.size() > 0) {
      int nMultPeriodicDofs = multPeriodicDof3.size();
403

404
405
      for (int i = 0; i < nMultPeriodicDofs; i++) {
	for (int j = i + 1; j < nMultPeriodicDofs; j++) {
406
	  pair<DegreeOfFreedom, DegreeOfFreedom> perDofs0 =
407
	    make_pair(multPeriodicDof3[i], multPeriodicDof3[j]);
408
	  pair<DegreeOfFreedom, DegreeOfFreedom> perDofs1 =
409
	    make_pair(multPeriodicDof3[j], multPeriodicDof3[i]);
410

411
	  if (periodicVertices.count(perDofs0) == 0) {
412
	    BoundaryType b = getNewBoundaryType();
413
414
	    periodicVertices[perDofs0] = b;
	    periodicVertices[perDofs1] = b;
415
416
417
418
	  }
	}
      }
    }
419
420


421
    // === Get all edges that have multiple periodic associations (3D only!). ===
422

423
424
425
    for (map<DofEdge, std::set<DofEdge> >::iterator it = periodicEdgeAssoc.begin();
	 it != periodicEdgeAssoc.end(); ++it) {
      if (it->second.size() > 1) {
426
	TEST_EXIT_DBG(macroMesh->getDim() == 3)("Should not happen!\n");
427
	TEST_EXIT_DBG(it->second.size() == 2)("Should not happen!\n");
428

429
430
431
	DofEdge edge0 = it->first;
	DofEdge edge1 = *(it->second.begin());
	DofEdge edge2 = *(++(it->second.begin()));
432

433
434
435
436
437
438
439
440
441
442
	pair<DofEdge, DofEdge> perEdge0 = make_pair(edge1, edge2);
	pair<DofEdge, DofEdge> perEdge1 = make_pair(edge2, edge1);

	TEST_EXIT_DBG(periodicEdges.count(make_pair(edge0, edge1)) == 1)
	  ("Should not happen!\n");
	TEST_EXIT_DBG(periodicEdges.count(make_pair(edge1, edge0)) == 1)
	  ("Should not happen!\n");

	BoundaryType type0 = periodicEdges[make_pair(edge0, edge1)];
	BoundaryType type1 = periodicEdges[make_pair(edge0, edge2)];
443
	BoundaryType type2 = provideConnectedPeriodicBoundary(type0, type1);
444
445
446

 	periodicEdges[perEdge0] = type2;
 	periodicEdges[perEdge1] = type2;
447
448
449
450
451
452
453
      }
    }


    // === In debug mode we make some tests, if the periodic structures are set ===
    // === in a symmetric way, i.e., if A -> B for a specific boundary type,    ===
    // === there must be a mapping B -> A with the same boundary type.          ===
454

455
456
457
#if (DEBUG != 0)
    for (PerBoundMap<DegreeOfFreedom>::iterator it = periodicVertices.begin();
	 it != periodicVertices.end(); ++it) {
458
      pair<DegreeOfFreedom, DegreeOfFreedom> testVertex =
459
	make_pair(it->first.second, it->first.first);
460

461
462
463
464
      TEST_EXIT_DBG(periodicVertices.count(testVertex) == 1)
	("Should not happen!\n");
      TEST_EXIT_DBG(periodicVertices[testVertex] == it->second)
	("Should not happen!\n");
465
466
467
468
    }

    for (PerBoundMap<DofEdge>::iterator it = periodicEdges.begin();
	 it != periodicEdges.end(); ++it) {
469
      pair<DofEdge, DofEdge> testEdge =
470
	make_pair(it->first.second, it->first.first);
471

472
473
474
      TEST_EXIT_DBG(periodicEdges.count(testEdge) == 1)("Should not happen!\n");
      TEST_EXIT_DBG(periodicEdges[testEdge] == it->second)("Should not happen!\n");
    }
475

476
477
    for (PerBoundMap<DofFace>::iterator it = periodicFaces.begin();
	 it != periodicFaces.end(); ++it) {
478
      pair<DofFace, DofFace> testFace =
479
	make_pair(it->first.second, it->first.first);
480

481
482
483
      TEST_EXIT_DBG(periodicFaces.count(testFace) == 1)("Should not happen!\n");
      TEST_EXIT_DBG(periodicFaces[testFace] == it->second)("Should not happen!\n");
    }
484
#endif
485
486
  }

487
  BoundaryType ElementObjectDatabase::getNewBoundaryType()
488
  {
489
    FUNCNAME_DBG("ElementObjectDatabase::getNewBoundaryType()");
490
491

    BoundaryType newPeriodicBoundaryType = 0;
492
493
    for (map<BoundaryType, VertexVector*>::iterator it = macroMesh->getPeriodicAssociations().begin();
	 it != macroMesh->getPeriodicAssociations().end(); ++it)
494
      newPeriodicBoundaryType = std::min(newPeriodicBoundaryType, it->first);
495

496
497
    TEST_EXIT_DBG(newPeriodicBoundaryType < 0)("Should not happen!\n");
    newPeriodicBoundaryType--;
498

499
    for(size_t i = 0; i < meshes.size(); i++) {
500
      meshes[i]->getPeriodicAssociations()[newPeriodicBoundaryType] =
501
502
        new VertexVector(feSpaces[i]->getAdmin(), "");
    }
503

504
505
    return newPeriodicBoundaryType;
  }
506
507
508

  BoundaryType
  ElementObjectDatabase::provideConnectedPeriodicBoundary(BoundaryType b0,
509
							  BoundaryType b1)
510
  {
511
    FUNCNAME_DBG("ElementObjectDatabase::provideConnectedPeriodicBoundary()");
512

513
    std::pair<BoundaryType, BoundaryType> bConn =
514
      (b0 <= b1 ? make_pair(b0, b1) : make_pair(b1, b0));
515
516

    if (bConnMap.count(bConn) == 0) {
517
      BoundaryType newPeriodicBoundaryType = getNewBoundaryType();
518

519
      for(size_t i = 0; i < meshes.size(); i++) {
520

521
522
523
        VertexVector &vecB0 = meshes[i]->getPeriodicAssociations(b0);
        VertexVector &vecB1 = meshes[i]->getPeriodicAssociations(b1);
        VertexVector &vecC = meshes[i]->getPeriodicAssociations(newPeriodicBoundaryType);
524

525
        DOFIteratorBase it(const_cast<DOFAdmin*>(feSpaces[i]->getAdmin()), USED_DOFS);
526

527
528
529
530
	for (it.reset(); !it.end(); ++it) {
	  if (!it.isDofFree()) {
	    TEST_EXIT_DBG(vecB1[vecB0[it.getDOFIndex()]] == vecB0[vecB1[it.getDOFIndex()]])
	      ("Should not happen!\n");
531

532
533
	    vecC[it.getDOFIndex()] = vecB1[vecB0[it.getDOFIndex()]];
	  }
534
535
	}
      }
536

537
538
      bConnMap[bConn] = newPeriodicBoundaryType;
    }
539

540
541
542
543
    return bConnMap[bConn];
  }


544
  void ElementObjectDatabase::updateRankData()
Thomas Witkowski's avatar
Thomas Witkowski committed
545
  {
546
    FUNCNAME("ElementObjectDatabase::updateRankData()");
547

Thomas Witkowski's avatar
Thomas Witkowski committed
548
549
    TEST_EXIT(macroElementRankMap)("Should not happen!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
550
    vertexInRank.clear();
551
    for (flat_map<DegreeOfFreedom, vector<ElementObjectData> >::iterator it = vertexElements.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
552
	 it != vertexElements.end(); ++it) {
553
      for (vector<ElementObjectData>::iterator it2 = it->second.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
554
	   it2 != it->second.end(); ++it2) {
555
	int elementInRank = (*macroElementRankMap)[it2->elIndex];
Thomas Witkowski's avatar
Thomas Witkowski committed
556
557
	if (it2->elIndex > vertexInRank[it->first][elementInRank].elIndex)
	  vertexInRank[it->first][elementInRank] = *it2;
Thomas Witkowski's avatar
Thomas Witkowski committed
558
559
      }
    }
560

Thomas Witkowski's avatar
Thomas Witkowski committed
561
    edgeInRank.clear();
562
    for (flat_map<DofEdge, vector<ElementObjectData> >::iterator it = edgeElements.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
563
	 it != edgeElements.end(); ++it) {
564
      for (vector<ElementObjectData>::iterator it2 = it->second.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
565
	   it2 != it->second.end(); ++it2) {
566
	int elementInRank = (*macroElementRankMap)[it2->elIndex];
Thomas Witkowski's avatar
Thomas Witkowski committed
567
568
	if (it2->elIndex > edgeInRank[it->first][elementInRank].elIndex)
	  edgeInRank[it->first][elementInRank] = *it2;
Thomas Witkowski's avatar
Thomas Witkowski committed
569
570
      }
    }
571

Thomas Witkowski's avatar
Thomas Witkowski committed
572
    faceInRank.clear();
573
    for (flat_map<DofFace, vector<ElementObjectData> >::iterator it = faceElements.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
574
	 it != faceElements.end(); ++it) {
575
      for (vector<ElementObjectData>::iterator it2 = it->second.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
576
	   it2 != it->second.end(); ++it2) {
577
	int elementInRank = (*macroElementRankMap)[it2->elIndex];
Thomas Witkowski's avatar
Thomas Witkowski committed
578
579
	if (it2->elIndex > faceInRank[it->first][elementInRank].elIndex)
	  faceInRank[it->first][elementInRank] = *it2;
Thomas Witkowski's avatar
Thomas Witkowski committed
580
581
582
583
      }
    }
  }

584

Thomas Witkowski's avatar
bla    
Thomas Witkowski committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  void ElementObjectDatabase::clear()
  {
    vertexElements.clear();
    edgeElements.clear();
    faceElements.clear();

    vertexLocalMap.clear();
    edgeLocalMap.clear();
    faceLocalMap.clear();

    vertexInRank.clear();
    edgeInRank.clear();
    faceInRank.clear();

    bConnMap.clear();
    periodicVertices.clear();
    periodicEdges.clear();
    periodicFaces.clear();
    periodicDofAssoc.clear();
    periodicEdgeAssoc.clear();

    edgeReverseMode.clear();
    faceReverseMode.clear();

609
    macroElementRankMap = NULL;
Thomas Witkowski's avatar
bla    
Thomas Witkowski committed
610
611
612
613
614
    macroElIndexMap.clear();
    macroElIndexTypeMap.clear();
  }


615
  void ElementObjectDatabase::createReverseModeData()
616
  {
617
    FUNCNAME_DBG("ElementObjectDatabase::createReverseModeData()");
618

619
620
    // === In 2D, all reverse modes are always true! ===

621
    if (macroMesh->getDim() == 2)
622
623
      return;

624
625
626

    // === First, create reverse modes for all "directly" neighbouring elements. ===

627
    for (flat_map<DofEdge, vector<ElementObjectData> >::iterator edgeIt = edgeElements.begin();
628
629
630
631
632
	 edgeIt != edgeElements.end(); ++edgeIt) {

      vector<ElementObjectData>& els = edgeIt->second;

      for (unsigned int i = 0; i < els.size(); i++) {
633
634
	BoundaryObject obj0(macroElIndexMap[els[i].elIndex][macroMesh],
			    macroElIndexTypeMap[els[i].elIndex],
635
636
637
			    EDGE, els[i].ithObject);

	for (unsigned int j = i + 1; j < els.size(); j++) {
638
639
	  BoundaryObject obj1(macroElIndexMap[els[j].elIndex][macroMesh],
			      macroElIndexTypeMap[els[j].elIndex],
640
641
			      EDGE, els[j].ithObject);

642
	  bool reverseMode =
643
	    BoundaryObject::computeReverseMode(obj0, obj1, feSpace, INTERIOR);
644
645
646
647
	  if (reverseMode) {
	    edgeReverseMode.insert(make_pair(els[i], els[j]));
	    edgeReverseMode.insert(make_pair(els[j], els[i]));
	  }
648
649
650
651
	}
      }
    }

652
    for (flat_map<DofFace, vector<ElementObjectData> >::iterator faceIt = faceElements.begin();
653
654
655
656
657
	 faceIt != faceElements.end(); ++faceIt) {

      vector<ElementObjectData>& els = faceIt->second;

      for (unsigned int i = 0; i < els.size(); i++) {
658
659
	BoundaryObject obj0(macroElIndexMap[els[i].elIndex][macroMesh],
			    macroElIndexTypeMap[els[i].elIndex],
660
661
662
			    FACE, els[i].ithObject);

	for (unsigned int j = i + 1; j < els.size(); j++) {
663
664
	  BoundaryObject obj1(macroElIndexMap[els[j].elIndex][macroMesh],
			      macroElIndexTypeMap[els[j].elIndex],
665
666
			      FACE, els[j].ithObject);

667
	  bool reverseMode =
668
	    BoundaryObject::computeReverseMode(obj0, obj1, feSpace, INTERIOR);
669
670
671
672
	  if (reverseMode) {
	    faceReverseMode.insert(make_pair(els[i], els[j]));
	    faceReverseMode.insert(make_pair(els[j], els[i]));
	  }
673
674
675
	}
      }
    }
676
677
678
679
680
681
682
683
684
685


    // === And create reverse modes for periodic neighbouring elements. ===

    for (PerBoundMap<DofEdge>::iterator edgeIt = periodicEdges.begin();
	 edgeIt != periodicEdges.end(); ++edgeIt) {
      vector<ElementObjectData> &edges0 = edgeElements[edgeIt->first.first];
      vector<ElementObjectData> &edges1 = edgeElements[edgeIt->first.second];

      for (unsigned int i = 0; i < edges0.size(); i++) {
686
687
	BoundaryObject obj0(macroElIndexMap[edges0[i].elIndex][macroMesh],
			    macroElIndexTypeMap[edges0[i].elIndex],
688
689
690
			    EDGE, edges0[i].ithObject);

	for (unsigned int j = 0; j < edges1.size(); j++) {
691
692
	  BoundaryObject obj1(macroElIndexMap[edges1[j].elIndex][macroMesh],
			      macroElIndexTypeMap[edges1[j].elIndex],
693
694
			      EDGE, edges1[j].ithObject);

695
696
	  bool reverseMode =
	    BoundaryObject::computeReverseMode(obj0, obj1, feSpace,
697
					       edgeIt->second);
698
699
700
701
	  if (reverseMode) {
	    edgeReverseMode.insert(make_pair(edges0[i], edges1[j]));
	    edgeReverseMode.insert(make_pair(edges1[j], edges0[i]));
	  }
702
703
704
705
706
707
708
709
710
	}
      }
    }

    for (PerBoundMap<DofFace>::iterator faceIt = periodicFaces.begin();
	 faceIt != periodicFaces.end(); ++faceIt) {
      vector<ElementObjectData> &faces0 = faceElements[faceIt->first.first];
      vector<ElementObjectData> &faces1 = faceElements[faceIt->first.second];

711
      TEST_EXIT_DBG((faces0.size() == 1) && (faces1.size() == 1))("Should not happen!\n");
712

713
714
715
716
717
      BoundaryObject obj0(macroElIndexMap[faces0[0].elIndex][macroMesh],
			  macroElIndexTypeMap[faces0[0].elIndex],
			  FACE, faces0[0].ithObject);
      BoundaryObject obj1(macroElIndexMap[faces1[0].elIndex][macroMesh],
			  macroElIndexTypeMap[faces1[0].elIndex],
718
			  FACE, faces1[0].ithObject);
719
720

      bool reverseMode =
721
	BoundaryObject::computeReverseMode(obj0, obj1, feSpace, faceIt->second);
722
723
724
725
      if (reverseMode) {
	faceReverseMode.insert(make_pair(faces0[0], faces1[0]));
	faceReverseMode.insert(make_pair(faces1[0], faces0[0]));
      }
726
    }
727
728
729
  }


730
  int ElementObjectDatabase::getIterateOwner(int level)
731
732
733
734
735
  {
    FUNCNAME("ElementObjectDatabase::getIterateOwner()");

    TEST_EXIT_DBG(macroElementRankMap)("Should not happen!\n");

736
737
    switch (iterGeoPos) {
    case VERTEX:
738
      return getOwner(vertexElements[vertexIter->first], level);
739
740
      break;
    case EDGE:
741
      return getOwner(edgeElements[edgeIter->first], level);
742
743
      break;
    case FACE:
744
      return getOwner(faceElements[faceIter->first], level);
745
      break;
746
747
748
    default:
      ERROR_EXIT("There is something reallllly wrong!\n");
      return -1;
749
    }
750

751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
    return -1;
  }


  int ElementObjectDatabase::getOwner(DegreeOfFreedom vertex, int level)
  {
    return getOwner(vertexElements[vertex], level);
  }


  int ElementObjectDatabase::getOwner(DofEdge edge, int level)
  {
    return getOwner(edgeElements[edge], level);
  }


  int ElementObjectDatabase::getOwner(DofFace face, int level)
  {
    return getOwner(faceElements[face], level);
  }


773
  int ElementObjectDatabase::getOwner(vector<ElementObjectData>& objData,
774
775
				      int level)
  {
776
    FUNCNAME_DBG("ElementObjectDatabase::getOwner()");
777

778
779
    int owner = -1;

780
    std::set<int> &levelRanks = levelData->getLevelRanks(level);
781
    bool allRanks = (level == 0);
782

783
784
    for (vector<ElementObjectData>::iterator it = objData.begin();
	 it != objData.end(); ++it) {
785
786
787
788
      int elRank = (*macroElementRankMap)[it->elIndex];
      if (allRanks || levelRanks.count(elRank))
	owner = std::max(owner, elRank);
    }
789

790
    TEST_EXIT_DBG(owner >= 0)("Cannot find owner on level %d\n", level);
791

792
793
794
795
796
797
798
799
    return owner;
  }


  int ElementObjectDatabase::getIterateMaxLevel()
  {
    FUNCNAME("ElementObjectDatabase::getIterateMaxLevel()");

800
    int nLevel = levelData->getNumberOfLevels();
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
    TEST_EXIT_DBG(nLevel > 0)("Should not happen!\n");

    vector<std::set<int> > ranksInLevel;
    ranksInLevel.resize(nLevel);

    switch (iterGeoPos) {
    case VERTEX:
      {
	vector<ElementObjectData>& vertexData = vertexElements[vertexIter->first];
	for (vector<ElementObjectData>::iterator it = vertexData.begin();
	     it != vertexData.end(); ++it)
	  ranksInLevel[0].insert((*macroElementRankMap)[it->elIndex]);
      }
      break;
    case EDGE:
      {
	vector<ElementObjectData>& edgeData = edgeElements[edgeIter->first];
	for (vector<ElementObjectData>::iterator it = edgeData.begin();
	     it != edgeData.end(); ++it)
	  ranksInLevel[0].insert((*macroElementRankMap)[it->elIndex]);
      }
      break;
    case FACE:
824
825
826
827
828
829
      {
	vector<ElementObjectData>& faceData = faceElements[faceIter->first];
	for (vector<ElementObjectData>::iterator it = faceData.begin();
	     it != faceData.end(); ++it)
	  ranksInLevel[0].insert((*macroElementRankMap)[it->elIndex]);
      }
830
831
832
833
834
      break;
    default:
      ERROR_EXIT("Should not happen!\n");
    }

835
836
    for (std::set<int>::iterator it = ranksInLevel[0].begin();
	 it != ranksInLevel[0].end(); ++it)
837
838
839
840
841
842
843
      for (int level = 1; level < nLevel; level++)
	ranksInLevel[level].insert(levelData->getLevelId(level, *it));

    int maxLevel = 0;
    for (int level = 1; level < nLevel; level++)
      if (ranksInLevel[level].size() > 1)
	maxLevel = level;
844

845
846
847
848
    return maxLevel;
  }


849
  void ElementObjectDatabase::serialize(ostream &out)
850
851
852
  {
    int nSize = vertexElements.size();
    SerUtil::serialize(out, nSize);
853
    for (flat_map<DegreeOfFreedom, vector<ElementObjectData> >::iterator it = vertexElements.begin();
854
855
856
857
858
859
860
	 it != vertexElements.end(); ++it) {
      SerUtil::serialize(out, it->first);
      serialize(out, it->second);
    }

    nSize = edgeElements.size();
    SerUtil::serialize(out, nSize);
861
    for (flat_map<DofEdge, vector<ElementObjectData> >::iterator it = edgeElements.begin();
862
863
864
865
866
867
868
	 it != edgeElements.end(); ++it) {
      SerUtil::serialize(out, it->first);
      serialize(out, it->second);
    }

    nSize = faceElements.size();
    SerUtil::serialize(out, nSize);
869
    for (flat_map<DofFace, vector<ElementObjectData> >::iterator it = faceElements.begin();
870
871
872
873
874
875
	 it != faceElements.end(); ++it) {
      SerUtil::serialize(out, it->first);
      serialize(out, it->second);
    }


876

877
878
    nSize = vertexLocalMap.size();
    SerUtil::serialize(out, nSize);
879
    for (boost::container::flat_map<ElementObjectData, DegreeOfFreedom>::iterator it = vertexLocalMap.begin();
880
881
882
883
884
885
886
	 it != vertexLocalMap.end(); ++it) {
      it->first.serialize(out);
      SerUtil::serialize(out, it->second);
    }

    nSize = edgeLocalMap.size();
    SerUtil::serialize(out, nSize);
887
    for (boost::container::flat_map<ElementObjectData, DofEdge>::iterator it = edgeLocalMap.begin();
888
889
890
891
892
893
894
	 it != edgeLocalMap.end(); ++it) {
      it->first.serialize(out);
      SerUtil::serialize(out, it->second);
    }

    nSize = faceLocalMap.size();
    SerUtil::serialize(out, nSize);
895
    for (boost::container::flat_map<ElementObjectData, DofFace>::iterator it = faceLocalMap.begin();
896
897
898
899
900
901
	 it != faceLocalMap.end(); ++it) {
      it->first.serialize(out);
      SerUtil::serialize(out, it->second);
    }


902
    nSize = vertexInRank.size();
903
    SerUtil::serialize(out, nSize);
904
    for (flat_map<DegreeOfFreedom, flat_map<int, ElementObjectData> >::iterator it = vertexInRank.begin();
905
906
907
908
909
910
	 it != vertexInRank.end(); ++it) {
      SerUtil::serialize(out, it->first);
      serialize(out, it->second);
    }

    nSize = edgeInRank.size();
911
    SerUtil::serialize(out, nSize);
912
    for (flat_map<DofEdge, flat_map<int, ElementObjectData> >::iterator it = edgeInRank.begin();
913
914
915
916
917
918
	 it != edgeInRank.end(); ++it) {
      SerUtil::serialize(out, it->first);
      serialize(out, it->second);
    }

    nSize = faceInRank.size();
919
    SerUtil::serialize(out, nSize);
920
    for (flat_map<DofFace, flat_map<int, ElementObjectData> >::iterator it = faceInRank.begin();
921
922
923
924
925
	 it != faceInRank.end(); ++it) {
      SerUtil::serialize(out, it->first);
      serialize(out, it->second);
    }

926
927
928
    SerUtil::serialize(out, periodicVertices);
    SerUtil::serialize(out, periodicEdges);
    SerUtil::serialize(out, periodicFaces);
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949


    nSize = periodicDofAssoc.size();
    SerUtil::serialize(out, nSize);
    for (map<DegreeOfFreedom, std::set<BoundaryType> >::iterator it = periodicDofAssoc.begin();
	 it != periodicDofAssoc.end(); ++it) {
      SerUtil::serialize(out, it->first);
      SerUtil::serialize(out, it->second);
    }

    nSize = periodicEdgeAssoc.size();
    SerUtil::serialize(out, nSize);
    for (map<DofEdge, std::set<DofEdge> >::iterator it = periodicEdgeAssoc.begin();
	 it != periodicEdgeAssoc.end(); ++it) {
      SerUtil::serialize(out, it->first);
      SerUtil::serialize(out, it->second);
    }


    nSize = edgeReverseMode.size();
    SerUtil::serialize(out, nSize);
950
    for (std::set<pair<ElementObjectData, ElementObjectData>