ParallelDomainBase.cc 64.9 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1
#include <algorithm>
2
3
#include <iostream>
#include <fstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
4
#include <boost/lexical_cast.hpp>
5
#include "parallel/ParallelDomainBase.h"
6
#include "parallel/ParallelDomainDbg.h"
7
#include "parallel/StdMpi.h"
8
9
10
11
12
13
14
#include "ParMetisPartitioner.h"
#include "Mesh.h"
#include "Traverse.h"
#include "ElInfo.h"
#include "Element.h"
#include "MacroElement.h"
#include "PartitionElementData.h"
15
16
#include "DOFMatrix.h"
#include "DOFVector.h"
17
#include "SystemVector.h"
18
#include "VtkWriter.h"
19
#include "ElementDofIterator.h"
20
21
#include "ProblemStatBase.h"
#include "StandardProblemIteration.h"
22
#include "ElementFileWriter.h"
23
#include "VertexVector.h"
24
#include "MeshStructure.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
25
26
#include "ProblemVec.h"
#include "ProblemInstat.h"
27
#include "Debug.h"
28

29
30
namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
31
32
  using boost::lexical_cast;

33
34
35
36
37
  inline bool cmpDofsByValue(const DegreeOfFreedom* dof1, const DegreeOfFreedom* dof2)
  {
    return (*dof1 < *dof2);
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
38
39
40
41
42
43
44
45
46
47
  ParallelDomainBase::ParallelDomainBase(ProblemVec *problemStat,
					 ProblemInstatVec *problemInstat)
    : iterationIF(problemStat),
      timeIF(problemInstat),
      probStat(problemStat),
      name(problemStat->getName()),
      feSpace(problemStat->getFESpace(0)),
      mesh(feSpace->getMesh()),
      refineManager(problemStat->getRefinementManager(0)),
      info(problemStat->getInfo()),
48
      initialPartitionMesh(true),
49
      nRankDofs(0),
50
      rstart(0),
Thomas Witkowski's avatar
Thomas Witkowski committed
51
      nComponents(problemStat->getNumComponents()),
52
53
      deserialized(false),
      lastMeshChangeIndex(0)
54
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
55
56
57
58
59
60
61
    FUNCNAME("ParallelDomainBase::ParalleDomainBase()");

    TEST_EXIT(mesh->getNumberOfDOFAdmin() == 1)
      ("Only meshes with one DOFAdmin are supported!\n");
    TEST_EXIT(mesh->getDOFAdmin(0).getNumberOfPreDOFs(0) == 0)
      ("Wrong pre dof number for DOFAdmin!\n");

62
63
64
65
    mpiRank = MPI::COMM_WORLD.Get_rank();
    mpiSize = MPI::COMM_WORLD.Get_size();
    mpiComm = MPI::COMM_WORLD;
    partitioner = new ParMetisPartitioner(mesh, &mpiComm);
Thomas Witkowski's avatar
Thomas Witkowski committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    // Test if all fe spaces are equal. Yet, different fe spaces are not supported for
    // domain parallelization.
    const FiniteElemSpace *fe = probStat->getFESpace(0);
    for (int i = 0; i < nComponents; i++)
      TEST_EXIT(fe == probStat->getFESpace(i))
	("Parallelization does not supported different FE spaces!\n");

    // Create parallel serialization file writer, if needed.
    int writeSerialization = 0;
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);
    if (writeSerialization)
      probStat->getFileWriterList().push_back(new Serializer<ParallelDomainBase>(this));

    int readSerialization = 0;
    GET_PARAMETER(0, name + "->input->read serialization", "%d", &readSerialization);
    if (readSerialization) {
      std::string filename = "";
      GET_PARAMETER(0, name + "->input->serialization filename", &filename);
      filename += ".p" + lexical_cast<std::string>(mpiRank);
      MSG("Start serialization with %s\n", filename.c_str());
      std::ifstream in(filename.c_str());
      deserialize(in);
      in.close();
    }
91
92
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
93

94
  void ParallelDomainBase::initParallelization(AdaptInfo *adaptInfo)
95
  {
96
97
98
99
100
101
102
    FUNCNAME("ParallelDomainBase::initParallelization()");

    TEST_EXIT(mpiSize > 1)
      ("Parallelization does not work with only one process!\n");

    // If the problem has been already read from a file, we do not need to do anything.
    if (deserialized)
103
104
      return;

105
106
107
108
109
    // Test, if the mesh is the macro mesh only! Paritioning of the mesh is supported
    // only for macro meshes, so it will not work yet if the mesh is already refined
    // in some way.
    testForMacroMesh();

110
111
112
113
114
115
116
    // create an initial partitioning of the mesh
    partitioner->createPartitionData();
    // set the element weights, which are 1 at the very first begin
    setElemWeights(adaptInfo);
    // and now partition the mesh
    partitionMesh(adaptInfo);   

Thomas Witkowski's avatar
Thomas Witkowski committed
117
#if (DEBUG != 0)
118
119
    debug::ElementIdxToDofs elMap;
    debug::createSortedDofs(mesh, elMap);
120
121
122
    if (mpiRank == 0) {
      int writePartMesh = 1;
      GET_PARAMETER(0, "dbg->write part mesh", "%d", &writePartMesh);
123

124
125
126
127
128
      if (writePartMesh > 0)
	writePartitioningMesh("part.vtu");
      else 
	MSG("Skip write part mesh!\n");
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
129
#endif
130

131
    // === Create interior boundary information. ===
Thomas Witkowski's avatar
Thomas Witkowski committed
132

133
    createInteriorBoundaryInfo();
Thomas Witkowski's avatar
Thomas Witkowski committed
134

135
136
137
138
    // === Create new global and local DOF numbering. ===

    createLocalGlobalNumbering();

Thomas Witkowski's avatar
n    
Thomas Witkowski committed
139
140
141
142
    // === Remove all macro elements that are not part of the rank partition. ===

    removeMacroElements();

143
144
145
146
    // === Reset all DOFAdmins of the mesh. ===

    updateDofAdmins();

147
148
    // === If in debug mode, make some tests. ===

Thomas Witkowski's avatar
Thomas Witkowski committed
149
#if (DEBUG != 0)
150
    MSG("AMDiS runs in debug mode, so make some test ...\n");
151
152
153
    debug::testSortedDofs(mesh, elMap);
    ParallelDomainDbg::testInteriorBoundary(*this);
    ParallelDomainDbg::testCommonDofs(*this, true);
154
    MSG("Debug mode tests finished!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
155

156
157
    debug::writeMesh(feSpace, -1, "macromesh");   
#endif
158

159
160
161
    // === Create periodic dof mapping, if there are periodic boundaries. ===

    createPeriodicMap();
162

163
    // === Global refinements. ===
Thomas Witkowski's avatar
Thomas Witkowski committed
164

Thomas Witkowski's avatar
Thomas Witkowski committed
165
    int globalRefinement = 0;
166
    GET_PARAMETER(0, mesh->getName() + "->global refinements", "%d", &globalRefinement);
Thomas Witkowski's avatar
Thomas Witkowski committed
167

Thomas Witkowski's avatar
Thomas Witkowski committed
168
    if (globalRefinement > 0) {
169
      refineManager->globalRefine(mesh, globalRefinement);
170

171
172
173
174
#if (DEBUG != 0)
    debug::writeMesh(feSpace, -1, "grmesh");
#endif

175
      updateLocalGlobalNumbering();
176

177
      // === Update periodic mapping, if there are periodic boundaries. ===
178

179
      createPeriodicMap();
Thomas Witkowski's avatar
Thomas Witkowski committed
180
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

    /// === Set DOF rank information to all matrices and vectors. ===

    for (int i = 0; i < nComponents; i++) {
      for (int j = 0; j < nComponents; j++)
 	if (probStat->getSystemMatrix(i, j))
 	  probStat->getSystemMatrix(i, j)->setRankDofs(isRankDof);      

      TEST_EXIT_DBG(probStat->getRHS()->getDOFVector(i))("No rhs vector!\n");
      TEST_EXIT_DBG(probStat->getSolution()->getDOFVector(i))("No solution vector!\n");

      probStat->getRHS()->getDOFVector(i)->setRankDofs(isRankDof);
      probStat->getSolution()->getDOFVector(i)->setRankDofs(isRankDof);
    }

    // === Remove periodic boundary conditions in sequential problem definition. ===

    // Remove periodic boundaries in boundary manager on matrices and vectors.
    for (int i = 0; i < nComponents; i++) {
      for (int j = 0; j < nComponents; j++) {
	DOFMatrix* mat = probStat->getSystemMatrix(i, j);
 	if (mat && mat->getBoundaryManager())
	  removeBoundaryCondition(const_cast<BoundaryIndexMap&>(mat->getBoundaryManager()->getBoundaryConditionMap()));
      }

      if (probStat->getSolution()->getDOFVector(i)->getBoundaryManager())
	removeBoundaryCondition(const_cast<BoundaryIndexMap&>(probStat->getSolution()->getDOFVector(i)->getBoundaryManager()->getBoundaryConditionMap()));

      if (probStat->getRHS()->getDOFVector(i)->getBoundaryManager())
	removeBoundaryCondition(const_cast<BoundaryIndexMap&>(probStat->getRHS()->getDOFVector(i)->getBoundaryManager()->getBoundaryConditionMap()));
    }

    // Remove periodic boundaries on elements in mesh.
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh,  -1, Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
      elInfo->getElement()->deleteElementData(PERIODIC);
      elInfo = stack.traverseNext(elInfo);
    }    
220
221
  }

222
223

  void ParallelDomainBase::exitParallelization(AdaptInfo *adaptInfo)
224
  {}
225

226
227
228
  
  void ParallelDomainBase::updateDofAdmins()
  {
229
230
231
    FUNCNAME("ParallelDomainBase::updateDofAdmins()");

    for (int i = 0; i < mesh->getNumberOfDOFAdmin(); i++) {
232
      DOFAdmin& admin = const_cast<DOFAdmin&>(mesh->getDOFAdmin(i));
233
234
235

      // There must be always more allocated DOFs than used DOFs in DOFAdmin. Otherwise,
      // it is not possible to define a first DOF hole.
236
      if (static_cast<unsigned int>(admin.getSize()) == mapLocalGlobalDofs.size())
237
	admin.enlargeDOFLists();
238
239
240
      
      for (int j = 0; j < admin.getSize(); j++)
	admin.setDOFFree(j, true);
241
      for (unsigned int j = 0; j < mapLocalGlobalDofs.size(); j++)
242
243
 	admin.setDOFFree(j, false);

244
245
246
      admin.setUsedSize(mapLocalGlobalDofs.size());
      admin.setUsedCount(mapLocalGlobalDofs.size());
      admin.setFirstHole(mapLocalGlobalDofs.size());
247
248
249
    }
  }

250

251
252
253
254
255
256
257
258
259
260
  void ParallelDomainBase::testForMacroMesh()
  {
    FUNCNAME("ParallelDomainBase::testForMacroMesh()");

    int nMacroElements = 0;

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      TEST_EXIT(elInfo->getLevel() == 0)
261
	("Mesh is already refined! This does not work with parallelization!\n");
262
263
264
265
266
267
268
269
270
271
      
      nMacroElements++;

      elInfo = stack.traverseNext(elInfo);
    }

    TEST_EXIT(nMacroElements >= mpiSize)
      ("The mesh has less macro elements than number of mpi processes!\n");
  }

272

Thomas Witkowski's avatar
Thomas Witkowski committed
273
  void ParallelDomainBase::synchVector(DOFVector<double> &vec)
274
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
275
    StdMpi<std::vector<double> > stdMpi(mpiComm);
276
277

    for (RankToDofContainer::iterator sendIt = sendDofs.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
278
	 sendIt != sendDofs.end(); ++sendIt) {
279
      std::vector<double> dofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
280
281
      int nSendDofs = sendIt->second.size();
      dofs.reserve(nSendDofs);
282
      
Thomas Witkowski's avatar
Thomas Witkowski committed
283
284
      for (int i = 0; i < nSendDofs; i++)
	dofs.push_back(vec[*((sendIt->second)[i])]);
285
286
287
288

      stdMpi.send(sendIt->first, dofs);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
289
290
291
    for (RankToDofContainer::iterator recvIt = recvDofs.begin();
	 recvIt != recvDofs.end(); ++recvIt)
      stdMpi.recv(recvIt->first, recvIt->second.size());
292

Thomas Witkowski's avatar
Thomas Witkowski committed
293
    stdMpi.startCommunication<double>(MPI_DOUBLE);
294

Thomas Witkowski's avatar
Thomas Witkowski committed
295
296
297
298
299
    for (RankToDofContainer::iterator recvIt = recvDofs.begin();
	 recvIt != recvDofs.end(); ++recvIt)
      for (unsigned int i = 0; i < recvIt->second.size(); i++)
	vec[*(recvIt->second)[i]] = stdMpi.getRecvData(recvIt->first)[i];
  }
300
301


Thomas Witkowski's avatar
Thomas Witkowski committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  void ParallelDomainBase::synchVector(SystemVector &vec)
  {
    StdMpi<std::vector<double> > stdMpi(mpiComm);

    for (RankToDofContainer::iterator sendIt = sendDofs.begin();
	 sendIt != sendDofs.end(); ++sendIt) {
      std::vector<double> dofs;
      int nSendDofs = sendIt->second.size();
      dofs.reserve(nComponents * nSendDofs);
      
      for (int i = 0; i < nComponents; i++) {
	DOFVector<double> *dofvec = vec.getDOFVector(i);
	for (int j = 0; j < nSendDofs; j++)
	  dofs.push_back((*dofvec)[*((sendIt->second)[j])]);
316
317
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
318
      stdMpi.send(sendIt->first, dofs);
319
320
321
    }

    for (RankToDofContainer::iterator recvIt = recvDofs.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
322
323
	 recvIt != recvDofs.end(); ++recvIt)
      stdMpi.recv(recvIt->first, recvIt->second.size() * nComponents);
324

Thomas Witkowski's avatar
Thomas Witkowski committed
325
    stdMpi.startCommunication<double>(MPI_DOUBLE);
326
327

    for (RankToDofContainer::iterator recvIt = recvDofs.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
328
329
	 recvIt != recvDofs.end(); ++recvIt) {
      int nRecvDofs = recvIt->second.size();
330
331

      int counter = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
332
333
334
335
336
      for (int i = 0; i < nComponents; i++) {
	DOFVector<double> *dofvec = vec.getDOFVector(i);
 	for (int j = 0; j < nRecvDofs; j++)
	  (*dofvec)[*(recvIt->second)[j]] = 
	    stdMpi.getRecvData(recvIt->first)[counter++];
337
338
339
340
      }
    }
  }

341

Thomas Witkowski's avatar
Thomas Witkowski committed
342
343
344
345
346
347
348
349
350
351
352
353
  void ParallelDomainBase::removeBoundaryCondition(BoundaryIndexMap& boundaryMap)
  {
    BoundaryIndexMap::iterator it = boundaryMap.begin();
    while (it != boundaryMap.end()) {
      if (it->second->isPeriodic())
	boundaryMap.erase(it++);
      else
	++it;      
    }    
  }


354
355
  void ParallelDomainBase::checkMeshChange()
  {
356
357
    FUNCNAME("ParallelDomainBase::checkMeshChange()");

Thomas Witkowski's avatar
Thomas Witkowski committed
358
359
    int dim = mesh->getDim();

360
361
362
363
364
    // === If mesh has not been changed on all ranks, return. ===

    int recvAllValues = 0;
    int sendValue = static_cast<int>(mesh->getChangeIndex() != lastMeshChangeIndex);
    mpiComm.Allreduce(&sendValue, &recvAllValues, 1, MPI_INT, MPI_SUM);
365

366
    if (recvAllValues == 0)
367
368
      return;

369
370
    // === At least one rank mesh has been changed, so the boundaries must be ===
    // === adapted to the new mesh structure.                                 ===
371

372
373
374
375
376
    clock_t first = clock();
    
    do {
      // To check the interior boundaries, the ownership of the boundaries is not 
      // important. Therefore, we add all boundaries to one boundary container.
377
      RankToBoundMap allBound;
Thomas Witkowski's avatar
Thomas Witkowski committed
378
379

      for (InteriorBoundary::iterator it(myIntBoundary); !it.end(); ++it)
380
381
	if (it->rankObj.subObj == INDEX_OF_DIM(dim - 1, dim))
	  allBound[it.getRank()].push_back(*it);
Thomas Witkowski's avatar
Thomas Witkowski committed
382
383

      for (InteriorBoundary::iterator it(otherIntBoundary); !it.end(); ++it)
384
385
	if (it->rankObj.subObj == INDEX_OF_DIM(dim - 1, dim))
	  allBound[it.getRank()].push_back(*it);
Thomas Witkowski's avatar
Thomas Witkowski committed
386

387

388
      // === Check the boundaries and adapt mesh if necessary. ===
389

390
391
392
393
394
395
396
397
      bool meshChanged = checkAndAdaptBoundary(allBound);

      // === Check on all ranks if at least one rank's mesh has changed. ===

      int sendValue = static_cast<int>(!meshChanged);
      recvAllValues = 0;
      mpiComm.Allreduce(&sendValue, &recvAllValues, 1, MPI_INT, MPI_SUM);
    } while (recvAllValues != 0);
398

399
400

#if (DEBUG != 0)
401
    debug::writeMesh(feSpace, -1, "mesh");
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#endif

    INFO(info, 8)("Parallel mesh adaption needed %.5f seconds\n", 
		  TIME_USED(first, clock()));

    // === Because the mesh has been changed, update the DOF numbering and mappings. ===
   
    updateLocalGlobalNumbering();
  }

  
  bool ParallelDomainBase::checkAndAdaptBoundary(RankToBoundMap &allBound)
  {
    FUNCNAME("ParallelDomainBase::checkAndAdaptBoundary()");

    // === Create mesh structure codes for all ranks boundary elements. ===
       
    std::map<int, MeshCodeVec> sendCodes;
   
    for (RankToBoundMap::iterator it = allBound.begin(); it != allBound.end(); ++it) {
422
423
424
      for (std::vector<AtomicBoundary>::iterator boundIt = it->second.begin();
	   boundIt != it->second.end(); ++boundIt) {
	MeshStructure elCode;
425
	elCode.init(boundIt->rankObj);
426
427
428
429
	sendCodes[it->first].push_back(elCode);
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
430
    StdMpi<MeshCodeVec> stdMpi(mpiComm, true);
431
    stdMpi.send(sendCodes);
432
433
434
    stdMpi.recv(allBound);
    stdMpi.startCommunication<unsigned long int>(MPI_UNSIGNED_LONG);
 
435
    // === Compare received mesh structure codes. ===
436
    
437
438
    bool meshFitTogether = true;

439
440
    for (RankToBoundMap::iterator it = allBound.begin(); it != allBound.end(); ++it) {
      
441
442
      MeshCodeVec &recvCodes = stdMpi.getRecvData()[it->first];
      int i = 0;
443
      
444
445
      for (std::vector<AtomicBoundary>::iterator boundIt = it->second.begin();
	   boundIt != it->second.end(); ++boundIt) {
446

447
448
449
	MeshStructure elCode;	
	elCode.init(boundIt->rankObj);
	
450
451
	if (elCode.getCode() != recvCodes[i].getCode()) {
	  TEST_EXIT_DBG(refineManager)("Refinement manager is not set correctly!\n");
452
453
454

	  int refCounter = 0;
	  bool b = fitElementToMeshCode(recvCodes[i], 
455
456
					boundIt->rankObj.el,
					boundIt->rankObj.ithObj, 
457
458
					boundIt->rankObj.elType, refCounter);  

459
460
	  if (b)
	    meshFitTogether = false;
461
	}
462
	
463
	i++;
464
465
466
      }
    }

467
    return meshFitTogether;
468
  }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541


  bool ParallelDomainBase::fitElementToMeshCode(MeshStructure &code, 
						Element *el, 
						int ithSide, 
						int elType,
						int &refCounter)
  {
    FUNCNAME("fitElementToMeshCode()");

    if (code.empty())
      return false;
    
    int s1 = el->getSideOfChild(0, ithSide, elType);
    int s2 = el->getSideOfChild(1, ithSide, elType);

    TEST_EXIT_DBG(s1 != -1 || s2 != -1)("This should not happen!\n");

    if (s1 != -1 && s2 != -1)
      return fitElementToMeshCode2(code, el, ithSide, elType, refCounter);

    if (el->isLeaf()) {
      if (code.getNumElements() == 1 && code.isLeafElement())
	return false;
      
      el->setMark(1);
      refineManager->refineElement(el->getMesh(), el);
      refCounter++;
    }
   
    if (s1 != -1)
      return fitElementToMeshCode2(code, 
				   el->getFirstChild(), s1, el->getChildType(elType), refCounter);
    else
      return fitElementToMeshCode2(code, 
				   el->getSecondChild(), s2, el->getChildType(elType), refCounter);
  }

  bool ParallelDomainBase::fitElementToMeshCode2(MeshStructure &code, 
						 Element *el, 
						 int ithSide, 
						 int elType, int &refCounter)
  {
    if (code.isLeafElement())
      return false;

    bool value = false;

    if (el->isLeaf()) {
      el->setMark(1);
      refineManager->refineElement(el->getMesh(), el);     
      value = true;
      refCounter++;
    }

    int s1 = el->getSideOfChild(0, ithSide, elType);
    int s2 = el->getSideOfChild(1, ithSide, elType);
    
    if (s1 != -1) {
      code.nextElement();
      value |= fitElementToMeshCode2(code, el->getFirstChild(), 
				     s1, el->getChildType(elType), refCounter);
    }  

    if (s2 != -1) {
      code.nextElement();	
      value |= fitElementToMeshCode2(code, el->getSecondChild(), 
				     s2, el->getChildType(elType), refCounter);
    }

    return value;
  }

542
543
  
  void ParallelDomainBase::serialize(std::ostream &out, DofContainer &data)
Thomas Witkowski's avatar
Thomas Witkowski committed
544
  {    
545
    int vecSize = data.size();
546
    SerUtil::serialize(out, vecSize);
547
548
    for (int i = 0; i < vecSize; i++) {
      int dofIndex = (*data[i]);
549
      SerUtil::serialize(out, dofIndex);
550
551
552
553
554
555
556
557
558
559
    }
  }


  void ParallelDomainBase::deserialize(std::istream &in, DofContainer &data,
				       std::map<int, const DegreeOfFreedom*> &dofMap)
  {
    FUNCNAME("ParallelDomainBase::deserialize()");

    int vecSize = 0;
560
    SerUtil::deserialize(in, vecSize);
561
562
563
    data.resize(vecSize);
    for (int i = 0; i < vecSize; i++) {
      int dofIndex = 0;
564
      SerUtil::deserialize(in, dofIndex);
565
566
567
568
569
570
571
572
573
574
575
576

      TEST_EXIT_DBG(dofMap.count(dofIndex) != 0)
	("Dof index could not be deserialized correctly!\n");

      data[i] = dofMap[dofIndex];
    }
  }


  void ParallelDomainBase::serialize(std::ostream &out, RankToDofContainer &data)
  {
    int mapSize = data.size();
577
    SerUtil::serialize(out, mapSize);
578
579
    for (RankToDofContainer::iterator it = data.begin(); it != data.end(); ++it) {
      int rank = it->first;
580
      SerUtil::serialize(out, rank);
581
582
583
584
585
586
587
588
589
      serialize(out, it->second);
    }
  }

  
  void ParallelDomainBase::deserialize(std::istream &in, RankToDofContainer &data,
				       std::map<int, const DegreeOfFreedom*> &dofMap)
  {
    int mapSize = 0;
590
    SerUtil::deserialize(in, mapSize);
591
592
    for (int i = 0; i < mapSize; i++) {
      int rank = 0;
593
      SerUtil::deserialize(in, rank);
594
595
596
597
      deserialize(in, data[rank], dofMap);      
    }
  }

598

599
  double ParallelDomainBase::setElemWeights(AdaptInfo *adaptInfo) 
600
  {
601
    FUNCNAME("ParallelDomainBase::setElemWeights()");
602

603
    double localWeightSum = 0.0;
604
    elemWeights.clear();
605
606
607
608
609
610
611
612
613
614
615
616
617
618
      
    std::string filename = "";
    GET_PARAMETER(0, mesh->getName() + "->macro weights", &filename);
    if (filename != "") {
      MSG("Read macro weights from %s\n", filename.c_str());

      std::ifstream infile;
      infile.open(filename.c_str(), std::ifstream::in);
      while (!infile.eof()) {
	int elNum, elWeight;
	infile >> elNum;
	if (infile.eof())
	  break;
	infile >> elWeight;
619

620
621
622
	elemWeights[elNum] = elWeight;
	localWeightSum += elWeight;
      }
623

624
625
626
627
628
629
630
631
632
633
634
      infile.close();
    } else {
           
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	Element *element = elInfo->getElement();
	
	// get partition data
	PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	  (element->getElementData(PARTITION_ED));
635
	
636
637
638
639
640
641
642
643
644
645
	if (partitionData && partitionData->getPartitionStatus() == IN) {
	  int elNum = -1;
	  if (partitionData->getLevel() == 0)
	    elNum = element->getIndex();
	  
	  TEST_EXIT_DBG(elNum != -1)("invalid element number\n");
	  if (element->isLeaf()) {
	    elemWeights[elNum] += 1.0;
	    localWeightSum += 1.0;
	  }
646
	}
647
648
	
	elInfo = stack.traverseNext(elInfo);
649
650
651
652
653
654
      }
    }

    return localWeightSum;
  }

655

656
  void ParallelDomainBase::partitionMesh(AdaptInfo *adaptInfo)
657
  {
658
659
    FUNCNAME("ParallelDomainBase::partitionMesh()");

660
661
662
663
664
665
666
667
668
669
670
671
    if (initialPartitionMesh) {
      initialPartitionMesh = false;
      partitioner->fillCoarsePartitionVec(&oldPartitionVec);
      partitioner->partition(&elemWeights, INITIAL);
    } else {
      oldPartitionVec = partitionVec;
      partitioner->partition(&elemWeights, ADAPTIVE_REPART, 100.0 /*0.000001*/);
    }    

    partitioner->fillCoarsePartitionVec(&partitionVec);
  }

672

Thomas Witkowski's avatar
Thomas Witkowski committed
673
674
675
676
677
678
679
680
681
  void ParallelDomainBase::solveInitialProblem(AdaptInfo *adaptInfo)
  {     
    if (timeIF)
      timeIF->solveInitialProblem(adaptInfo);
    
    synchVector(*(probStat->getSolution()));
  }


682
  void ParallelDomainBase::createInteriorBoundaryInfo()
683
  {
684
    FUNCNAME("ParallelDomainBase::createInteriorBoundaryInfo()");
Thomas Witkowski's avatar
Thomas Witkowski committed
685

Thomas Witkowski's avatar
Thomas Witkowski committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
    // === First, create the interior boundaries based on macro element's  ===
    // === neighbour informations.                                         ===

    createMacroElementInteriorBoundaryInfo();

    // === Second, search the whole mesh for interior boundaries that consists of ===
    // === substructures of the macro elements.                                   ===

    createSubstructureInteriorBoundaryInfo();


    // === Once we have this information, we must care about the order of the atomic ===
    // === bounds in the three boundary handling object. Eventually all the bound-   ===
    // === aries have to be in the same order on both ranks that share the bounday.  ===

    StdMpi<std::vector<AtomicBoundary> > stdMpi(mpiComm);
    stdMpi.send(myIntBoundary.boundary);
    stdMpi.recv(otherIntBoundary.boundary);
    stdMpi.startCommunication<int>(MPI_INT);
   
    // === The information about all neighbouring boundaries has been received. So ===
    // === the rank tests if its own atomic boundaries are in the same order. If   ===
    // === not, the atomic boundaries are swaped to the correct order.             ===

    for (RankToBoundMap::iterator rankIt = otherIntBoundary.boundary.begin();
	 rankIt != otherIntBoundary.boundary.end(); ++rankIt) {

      // === We have received from rank "rankIt->first" the ordered list of element ===
      // === indices. We now have to sort the corresponding list in this rank to    ===
      // === get the same order.                                                    ===
      
      for (int j = 0; j < static_cast<int>(rankIt->second.size()); j++) {
	// If the expected object is not at place, search for it.

	BoundaryObject &recvedBound = stdMpi.getRecvData()[rankIt->first][j].rankObj;

	if ((rankIt->second)[j].neighObj != recvedBound) {
	  int k = j + 1;
	  for (; k < static_cast<int>(rankIt->second.size()); k++)
 	    if ((rankIt->second)[k].neighObj == recvedBound)
	      break;

	  // The element must always be found, because the list is just in another order.
	  TEST_EXIT_DBG(k < static_cast<int>(rankIt->second.size()))
	    ("Should never happen!\n");

	  // Swap the current with the found element.
	  AtomicBoundary tmpBound = (rankIt->second)[k];
	  (rankIt->second)[k] = (rankIt->second)[j];
	  (rankIt->second)[j] = tmpBound;	
	}
      }
    }

    // === Do the same for the periodic boundaries. ===

    if (periodicBoundary.boundary.size() > 0) {
      stdMpi.clear();

      InteriorBoundary sendBounds, recvBounds;     
      for (RankToBoundMap::iterator rankIt = periodicBoundary.boundary.begin();
	   rankIt != periodicBoundary.boundary.end(); ++rankIt) {

	TEST_EXIT_DBG(rankIt->first != mpiRank)
	  ("It is no possible to have an interior boundary within a rank partition!\n");

	if (rankIt->first < mpiRank)
	  sendBounds.boundary[rankIt->first] = rankIt->second;
	else
	  recvBounds.boundary[rankIt->first] = rankIt->second;
      }

      stdMpi.send(sendBounds.boundary);
      stdMpi.recv(recvBounds.boundary);
      stdMpi.startCommunication<int>(MPI_INT);

      for (RankToBoundMap::iterator rankIt = periodicBoundary.boundary.begin();
	   rankIt != periodicBoundary.boundary.end(); ++rankIt) {
	if (rankIt->first <= mpiRank)
	  continue;
	  
	for (int j = 0; j < static_cast<int>(rankIt->second.size()); j++) {
	  
	  BoundaryObject &recvedBound = stdMpi.getRecvData()[rankIt->first][j].rankObj;
	  
	  if (periodicBoundary.boundary[rankIt->first][j].neighObj != recvedBound) {    
	    int k = j + 1;	    
	    for (; k < static_cast<int>(rankIt->second.size()); k++)
	      if (periodicBoundary.boundary[rankIt->first][k].neighObj == recvedBound)
		break;
	    
	    // The element must always be found, because the list is just in 
	    // another order.
	    TEST_EXIT_DBG(k < static_cast<int>(rankIt->second.size()))
	      ("Should never happen!\n");
	    
	    // Swap the current with the found element.
	    AtomicBoundary tmpBound = (rankIt->second)[k];
	    (rankIt->second)[k] = (rankIt->second)[j];
	    (rankIt->second)[j] = tmpBound;	
	  }  	  
	}
      }     
    } // periodicBoundary.boundary.size() > 0
  }


  void ParallelDomainBase::createMacroElementInteriorBoundaryInfo()
  {
    FUNCNAME("ParallelDomainBase::createMacroElementInteriorBoundaryInfo()");

797
798
    int nNeighbours = mesh->getGeo(NEIGH);
    int dim = mesh->getDim();
799
    GeoIndex subObj = INDEX_OF_DIM(dim - 1, dim);
800
801
802

    // === First, traverse the mesh and search for all elements that have an  ===
    // === boundary with an element within another partition.                 ===
Thomas Witkowski's avatar
Thomas Witkowski committed
803

804
    TraverseStack stack;
805
806
807
    ElInfo *elInfo = 
      stack.traverseFirst(mesh, -1, 
			  Mesh::CALL_LEAF_EL | Mesh::FILL_NEIGH | Mesh::FILL_BOUND);
808
809
810
811
    while (elInfo) {
      Element *element = elInfo->getElement();
      PartitionElementData *partitionData = 
	dynamic_cast<PartitionElementData*>(element->getElementData(PARTITION_ED));   
812

813
      // Check, if the element is within rank's partition.
814
      if (partitionData->getPartitionStatus() == IN) {
815
	for (int i = 0; i < nNeighbours; i++) {
816
817
818
819
	  if (!elInfo->getNeighbour(i))
	    continue;

	  PartitionElementData *neighbourPartitionData =
820
821
	    dynamic_cast<PartitionElementData*>(elInfo->getNeighbour(i)->
						getElementData(PARTITION_ED));
822

823
 	  if (neighbourPartitionData->getPartitionStatus() == OUT) {
Thomas Witkowski's avatar
Thomas Witkowski committed
824

825
826
827
828
829
	    // We have found an element that is in rank's partition, but has a 
	    // neighbour outside of the rank's partition.

	    // === Create information about the boundary between the two elements. ===

830
	    AtomicBoundary bound;	    	    
831
832
	    bound.rankObj.el = element;
	    bound.rankObj.elIndex = element->getIndex();
833
	    bound.rankObj.elType = elInfo->getType();
834
	    bound.rankObj.subObj = subObj;
835
	    bound.rankObj.ithObj = i;
836
837

	    bound.neighObj.el = elInfo->getNeighbour(i);
838
	    bound.neighObj.elIndex = elInfo->getNeighbour(i)->getIndex();
839
	    bound.neighObj.elType = -1;
840
	    bound.neighObj.subObj = subObj;
841
	    bound.neighObj.ithObj = elInfo->getSideOfNeighbour(i);
842
	    
843
	    // Get rank number of the neighbouring element.
844
845
	    int otherElementRank = partitionVec[elInfo->getNeighbour(i)->getIndex()];

846
847
848
849
850
851
852
853
	    // === Add the boundary information object to the corresponding overall ===
	    // === boundary. There are three possibilities: if the boundary is a    ===
	    // === periodic boundary, just add it to \ref periodicBounadry. Here it ===
	    // === does not matter which rank is responsible for this boundary.     ===
	    // === Otherwise, the boundary is added either to \ref myIntBoundary or ===
	    // === to \ref otherIntBoundary. It dependes on which rank is respon-   ===
	    // === sible for this boundary.                                         ===

854
	    if (BoundaryManager::isBoundaryPeriodic(elInfo->getBoundary(subObj, i))) {	      
855
856
857
858
859
	      // We have found an element that is at an interior, periodic boundary.
	      AtomicBoundary& b = periodicBoundary.getNewAtomic(otherElementRank);
	      b = bound;
	    } else {
	      // We have found an element that is at an interior, non-periodic boundary.
860
861
862
863
864
865
866
867
868
	      if (mpiRank > otherElementRank) {
		AtomicBoundary& b = myIntBoundary.getNewAtomic(otherElementRank);
		b = bound;
		b.rankObj.setReverseMode(b.neighObj, feSpace);
	      } else {
		AtomicBoundary& b = otherIntBoundary.getNewAtomic(otherElementRank);
		b = bound;	 
		b.neighObj.setReverseMode(b.rankObj, feSpace);
	      }	      
869
	    }
870
871
872
873
874
875
 	  }
	}
      }

      elInfo = stack.traverseNext(elInfo);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
876
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
877
878


Thomas Witkowski's avatar
Thomas Witkowski committed
879
880
881
  void ParallelDomainBase::createSubstructureInteriorBoundaryInfo()
  {
    FUNCNAME("ParallelDomainBase::createSubstructureInteriorBoundaryInfo()");
Thomas Witkowski's avatar
Thomas Witkowski committed
882

883
884
885
    // === Seach for all vertices/edges, which are part of an interior boundary,  ===
    // === but are not part of the interior boundaries that are created based on  ===
    // === the information of macro elements.                                     ===
Thomas Witkowski's avatar
Thomas Witkowski committed
886
887

    int dim = mesh->getDim();
888
    const BasisFunction *basFcts = feSpace->getBasisFcts();
889
    std::vector<DegreeOfFreedom> localIndices(basFcts->getNumber());
Thomas Witkowski's avatar
Thomas Witkowski committed
890
891
892

    // Maps each DOF in the whole domain to the rank that ownes this DOF.
    std::map<DegreeOfFreedom, int> dofOwner;
893
    // Maps each DOF in ranks partition to the element object that contains this DOF.
Thomas Witkowski's avatar
Thomas Witkowski committed
894
    std::map<DegreeOfFreedom, BoundaryObject> rankDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
895

896
897
    // Maps each edge in the whole domain to the rank that ownes this edge.
    std::map<GlobalEdge, int> edgeOwner;
898
    // Maps each edge in ranks partition to the element object that contains this edge.
899
    std::map<GlobalEdge, BoundaryObject> rankEdges;
900

901

Thomas Witkowski's avatar
Thomas Witkowski committed
902
    // === Traverse whole mesh and fill the maps defined above.                    ===
903

Thomas Witkowski's avatar
Thomas Witkowski committed
904
905
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
906
907
908
909
910
911
    while (elInfo) {
      Element *el = elInfo->getElement();
      basFcts->getLocalIndices(el, feSpace->getAdmin(), localIndices);

      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(el->getElementData(PARTITION_ED));
912
913
          
      // In 2d and 3d, traverse all vertices of current element.
914
      for (int i = 0; i < mesh->getGeo(VERTEX); i++) {
915
916
917
918
919
920
921
922
923
924
	DegreeOfFreedom dof0 = localIndices[i];
	// Update the owner of the current vertex DOF.
	dofOwner[dof0] = max(dofOwner[dof0], partitionVec[el->getIndex()]);

	// If the DOF is part of an element that is within ranks partition, add it
	// to the set of all ranks vertex DOFs.
	if (partitionData && partitionData->getPartitionStatus() == IN)
	  rankDofs[dof0] = BoundaryObject(el, elInfo->getType(), VERTEX, i);
      }      

Thomas Witkowski's avatar
Thomas Witkowski committed
925
926
927
928
929
930
931
932
933
934
      // In 3d, traverse all edges of current element.
      if (dim == 3) {
	for (int i = 0; i < 6; i++) {
	  DegreeOfFreedom dof0 = localIndices[el->getVertexOfEdge(i, 0)];
	  DegreeOfFreedom dof1 = localIndices[el->getVertexOfEdge(i, 1)];
	  GlobalEdge edge = std::make_pair(min(dof0, dof1), max(dof0, dof1));

	  // Update the owner of the current edge.
	  edgeOwner[edge] = max(edgeOwner[edge], partitionVec[el->getIndex()]);
	  
935
936
	  // If the edge is part of an element that is within ranks partition, add it
	  // to the set of all ranks edges.
Thomas Witkowski's avatar
Thomas Witkowski committed
937
938
	  if (partitionData && partitionData->getPartitionStatus() == IN)
	    rankEdges[edge] = BoundaryObject(el, elInfo->getType(), EDGE, i);
939
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
940
941
      }

942
943
944
      elInfo = stack.traverseNext(elInfo);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
945

946
    // === Create a set of all edges and vertices at ranks interior boundaries. ===
947
948
949

    // Stores all edges at rank's interior boundaries.
    std::set<GlobalEdge> rankBoundaryEdges;
Thomas Witkowski's avatar
Thomas Witkowski committed
950
    // Stores all vertices at rank's interior boundaries.
951
952
    std::set<DegreeOfFreedom> rankBoundaryDofs;

Thomas Witkowski's avatar
Thomas Witkowski committed
953
954
   
    // First, traverse the rank owned elements at the interior boundaries.
Thomas Witkowski's avatar
Thomas Witkowski committed
955
    for (InteriorBoundary::iterator it(myIntBoundary); !it.end(); ++it) {
956
      Element *el = it->rankObj.el;
Thomas Witkowski's avatar
Thomas Witkowski committed
957
      basFcts->getLocalIndices(el, feSpace->getAdmin(), localIndices);
958
      
959
960
      for (int i = 0; i < mesh->getGeo(VERTEX); i++) {
	int dofNo = el->getVertexOfPosition(it->rankObj.subObj, it->rankObj.ithObj, i);
961
962
963
964
965
966
967
968
	DegreeOfFreedom dof = localIndices[dofNo];
	
	if (dofOwner[dof] > mpiRank)
	  it->rankObj.excludedSubstructures.push_back(std::make_pair(VERTEX, dofNo));
	else
	  rankBoundaryDofs.insert(dof);
      }    

Thomas Witkowski's avatar
Thomas Witkowski committed
969
      if (dim == 3) {
970
971
	for (int i = 0; i < 3; i++) {
	  int edgeNo = el->getEdgeOfFace(it->rankObj.ithObj, i);
Thomas Witkowski's avatar
Thomas Witkowski committed
972
973
974
975
976
977
978
979
980
	  DegreeOfFreedom dof0 = localIndices[el->getVertexOfEdge(edgeNo, 0)];
	  DegreeOfFreedom dof1 = localIndices[el->getVertexOfEdge(edgeNo, 1)];
	  GlobalEdge edge = std::make_pair(min(dof0, dof1), max(dof0, dof1));	
	  
	  // If the edge at rank's interior boundarie has a higher owner rank, than
	  // we have to remove this edge from the corresponding boundary element.
	  // Otherwise, it is part of the interior boundary and we add it to the set
	  // rankBoundaryEdges.
	  if (edgeOwner[edge] > mpiRank)
981
	    it->rankObj.excludedSubstructures.push_back(std::make_pair(EDGE, edgeNo));
982
	  else
Thomas Witkowski's avatar
Thomas Witkowski committed
983
	    rankBoundaryEdges.insert(edge);
984
	}
985
      }     
Thomas Witkowski's avatar
Thomas Witkowski committed
986
987
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
988
989
    // Now, do the same with all other elements at the interior boundaries.
    for (InteriorBoundary::iterator it(otherIntBoundary); !it.end(); ++it) {
990
      Element *el = it->rankObj.el;
Thomas Witkowski's avatar
Thomas Witkowski committed
991
992
      basFcts->getLocalIndices(el, feSpace->getAdmin(), localIndices);
      
993
994
      for (int i = 0; i < mesh->getGeo(VERTEX); i++) {
	int dofNo = el->getVertexOfPosition(it->rankObj.subObj, it->rankObj.ithObj, i);
995
996
997
998
999
1000
1001
1002
1003

	DegreeOfFreedom dof = localIndices[dofNo];
	
	if (dofOwner[dof] > it.getRank())
	  it->rankObj.excludedSubstructures.push_back(std::make_pair(VERTEX, dofNo));
	else
	  rankBoundaryDofs.insert(dof);
      }	      

Thomas Witkowski's avatar
Thomas Witkowski committed
1004
      if (dim == 3) {
1005
1006
	for (int i = 0; i < 3; i++) {
	  int edgeNo = el->getEdgeOfFace(it->rankObj.ithObj, i);
1007
1008
1009
	  DegreeOfFreedom dof0 = localIndices[el->getVertexOfEdge(edgeNo, 0)];
	  DegreeOfFreedom dof1 = localIndices[el->getVertexOfEdge(edgeNo, 1)];
	  GlobalEdge edge = std::make_pair(min(dof0, dof1), max(dof0, dof1));	
1010
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
1011
	  if (edgeOwner[edge] > it.getRank())
1012
	    it->rankObj.excludedSubstructures.push_back(std::make_pair(EDGE, edgeNo));
1013
	  else
Thomas Witkowski's avatar
Thomas Witkowski committed
1014
1015
	    rankBoundaryEdges.insert(edge);	    
	}
1016
      }     
1017
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
1018

1019
    // === Create the new interior boundaries consisting only of edges. These   ===
1020
    // === boundaries are created on that ranks, which do not own the boundary  ===
1021
1022
    // === but are on the other side of the edge. Then, these ranks inform the  ===
    // === owner of the edge.                                                   ===
1023
1024
1025
1026
1027

    // Maps that stores to each rank number the global edges this rank has an 
    // interior boundary with. 
    std::map<int, std::vector<GlobalEdge> > recvEdges;
    // Stores to the map above the corresponding element objects that include the edge.
Thomas Witkowski's avatar
Thomas Witkowski committed
1028
    std::map<int, std::vector<BoundaryObject> > recvObjs;
1029
1030
    for (int i = 0; i < mpiSize; i++) {
      recvEdges[i].clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1031
      recvObjs[i].clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1032
1033
    }

1034
    // Traverse all edges in ranks domain.
1035
1036
    for (std::map<GlobalEdge, BoundaryObject>::iterator it = rankEdges.begin();
	 it != rankEdges.end(); ++it) {
1037
      // If we have found an edge in ranks domain that has an owner with a higher 
1038
1039
      // rank number, i.e., it must be part of an interior domain, but have not found
      // it before to be part of an interior domain, we have found an edge interior
1040
      // boundary. 
Thomas Witkowski's avatar
Thomas Witkowski committed
1041

1042
1043
      if (edgeOwner[it->first] > mpiRank && rankBoundaryEdges.count(it->first) == 0) {
	std::vector<GlobalEdge> &ownerEdges = recvEdges[edgeOwner[it->first]];
Thomas Witkowski's avatar
Thomas Witkowski committed
1044

1045
1046
1047
	// Check, we have not add this edge before.
	if (find(ownerEdges.begin(), ownerEdges.end(), it->first) == ownerEdges.end()) {
	  ownerEdges.push_back(it->first);	
Thomas Witkowski's avatar
Thomas Witkowski committed
1048
	  recvObjs[edgeOwner[it->first]].push_back(it->second);
1049
1050
1051

	  rankBoundaryDofs.insert(it->first.first);
	  rankBoundaryDofs.insert(it->first.second);
1052
1053
1054
1055
	}
      }
    }

1056

1057
1058
    // === Send all edge interior boundary infos to the owner of the new edge === 
    // === interior boundaries.                                               ===
Thomas Witkowski's avatar
Thomas Witkowski committed
1059

Thomas Witkowski's avatar
Thomas Witkowski committed
1060
    StdMpi<std::vector<GlobalEdge> > stdMpiEdge(mpiComm, true);
1061
1062
1063
1064
    stdMpiEdge.send(recvEdges);
    stdMpiEdge.recvFromAll();
    stdMpiEdge.startCommunication<int>(MPI_INT);

Thomas Witkowski's avatar
Thomas Witkowski committed
1065
1066
1067
1068
    StdMpi<std::vector<BoundaryObject> > stdMpiObjs(mpiComm, true);
    stdMpiObjs.send(recvObjs);
    stdMpiObjs.recvFromAll();
    stdMpiObjs.startCommunication<int>(MPI_INT);
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083


    // The owner of an edge interior boundary must send back information about the
    // mesh element that stores the edge at the owners domain. These information are
    // stored in this map.
    std::map<int, std::vector<BoundaryObject> > sendObjects;
    for (int i = 0; i < mpiSize; i++)
      sendObjects[i].clear();
    
    // === All owner of a new edge interior boundary will create this now from the ===
    // === received data.                                                          ===

    for (std::map<int, std::vector<GlobalEdge> >::iterator it = stdMpiEdge.getRecvData().begin();
	 it != stdMpiEdge.getRecvData().end(); ++it) {
      for (unsigned int i = 0; i < it->second.size(); i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1084
	TEST_EXIT_DBG(stdMpiObjs.getRecvData(it->first).size() > i)
1085
1086
1087
1088
	  ("Should not happen!\n");
	
	AtomicBoundary& b = myIntBoundary.getNewAtomic(it->first); 
	b.rankObj = rankEdges[it->second[i]];
Thomas Witkowski's avatar
Thomas Witkowski committed
1089
	b.neighObj = stdMpiObjs.getRecvData(it->first)[i];
1090
1091
	
	sendObjects[it->first].push_back(b.rankObj);
1092
1093
1094

	rankBoundaryDofs.insert(it->second[i].first);
	rankBoundaryDofs.insert(it->second[i].second);
1095
1096
1097
1098
1099
      }
    }

    // === Send the information about the elements on the owners side of the new ===
    // === edge interior boundaries.                                             ===
1100
    
Thomas Witkowski's avatar
Thomas Witkowski committed
1101
1102
1103
1104
    stdMpiObjs.clear();
    stdMpiObjs.send(sendObjects);
    stdMpiObjs.recvFromAll();
    stdMpiObjs.startCommunication<int>(MPI_INT);
1105
1106
1107
1108

    // === To the last, all non-owners of a edge interior boundary will now create ===
    // === the necessary boundary objects.                                         ===

Thomas Witkowski's avatar
Thomas Witkowski committed
1109
1110
    for (std::map<int, std::vector<BoundaryObject> >::iterator it = recvObjs.begin();
	it != recvObjs.end(); ++it) {
1111
      if (it->second.size() > 0) {	
Thomas Witkowski's avatar
Thomas Witkowski committed
1112
	TEST_EXIT_DBG(it->second.size() == stdMpiObjs.getRecvData(it->first).size())
1113
1114
1115
1116
1117
	  ("Should not happen!\n");

	for (unsigned int i = 0; i < it->second.size(); i++) {
	  AtomicBoundary& b = otherIntBoundary.getNewAtomic(it->first); 
	  b.rankObj = it->second[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
1118
	  b.neighObj = stdMpiObjs.getRecvData(it->first)[i];  	  
1119
1120
1121
	}
      }
    }
1122
1123


Thomas Witkowski's avatar
Thomas Witkowski committed
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
    // === Create the new interior boundaries consisting only of vertices. As done ===
    // === above for edge interior boundaries, also the vertex interior boundaries ===
    // === are created on that ranks, which do not own this part of the boundary.  ===

    // Maps that stores to each rank number the DOF this rank has an interior
    // boundary with. 
    std::map<int, std::vector<DegreeOfFreedom> > recvGlobalDofs;
    for (int i = 0; i < mpiSize; i++) {
      recvGlobalDofs[i].clear();
      recvObjs[i].clear();
    }

    for (std::map<DegreeOfFreedom, BoundaryObject>::iterator it = rankDofs.begin(); 
1137
1138
	 it != rankDofs.end(); ++it) {
      if (dofOwner[it->first] > mpiRank && rankBoundaryDofs.count(it->first) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1139
1140
1141
1142
1143