ParallelDomainBase.cc 90.1 KB
Newer Older
1001
    SerUtil::deserialize(in, mapSize);
1002
1003
    for (int i = 0; i < mapSize; i++) {
      int rank = 0;
1004
      SerUtil::deserialize(in, rank);
1005
1006
1007
1008
      deserialize(in, data[rank], dofMap);      
    }
  }

1009

1010
  double ParallelDomainBase::setElemWeights(AdaptInfo *adaptInfo) 
1011
  {
1012
    FUNCNAME("ParallelDomainBase::setElemWeights()");
1013

1014
    double localWeightSum = 0.0;
1015
    elemWeights.clear();
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
      
    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;
1030

1031
1032
1033
	elemWeights[elNum] = elWeight;
	localWeightSum += elWeight;
      }
1034

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
      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));
1046
	
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
	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;
	  }
1057
	}
1058
1059
	
	elInfo = stack.traverseNext(elInfo);
1060
1061
1062
1063
1064
1065
      }
    }

    return localWeightSum;
  }

1066

1067
  void ParallelDomainBase::partitionMesh(AdaptInfo *adaptInfo)
1068
  {
1069
1070
    FUNCNAME("ParallelDomainBase::partitionMesh()");

1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
    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);
  }

1083

1084
  void ParallelDomainBase::createInteriorBoundaryInfo()
1085
  {
1086
    FUNCNAME("ParallelDomainBase::createInteriorBoundaryInfo()");
Thomas Witkowski's avatar
Thomas Witkowski committed
1087

Thomas Witkowski's avatar
Thomas Witkowski committed
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
    // === 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()");

1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
    int nNeighbours = mesh->getGeo(NEIGH);
    int dim = mesh->getDim();
    GeoIndex subObj = CENTER;
    switch (dim) {
    case 2:
      subObj = EDGE;
      break;
    case 3:
      subObj = FACE;
      break;
    default:
      ERROR_EXIT("What is this?\n");
    }     
1212
1213
1214

    // === 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
1215

1216
    TraverseStack stack;
1217
1218
1219
    ElInfo *elInfo = 
      stack.traverseFirst(mesh, -1, 
			  Mesh::CALL_LEAF_EL | Mesh::FILL_NEIGH | Mesh::FILL_BOUND);
1220
1221
1222
1223
    while (elInfo) {
      Element *element = elInfo->getElement();
      PartitionElementData *partitionData = 
	dynamic_cast<PartitionElementData*>(element->getElementData(PARTITION_ED));   
1224

1225
      // Check, if the element is within rank's partition.
1226
      if (partitionData->getPartitionStatus() == IN) {
1227
	for (int i = 0; i < nNeighbours; i++) {
1228
1229
1230
1231
	  if (!elInfo->getNeighbour(i))
	    continue;

	  PartitionElementData *neighbourPartitionData =
1232
1233
	    dynamic_cast<PartitionElementData*>(elInfo->getNeighbour(i)->
						getElementData(PARTITION_ED));
1234

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

1237
1238
1239
1240
1241
	    // 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. ===

1242
	    AtomicBoundary bound;	    	    
1243
1244
	    bound.rankObj.el = element;
	    bound.rankObj.elIndex = element->getIndex();
1245
	    bound.rankObj.elType = elInfo->getType();
1246
	    bound.rankObj.subObj = subObj;
1247
	    bound.rankObj.ithObj = i;
1248
1249

	    bound.neighObj.el = elInfo->getNeighbour(i);
1250
	    bound.neighObj.elIndex = elInfo->getNeighbour(i)->getIndex();
1251
	    bound.neighObj.elType = -1;
1252
	    bound.neighObj.subObj = subObj;
1253
	    bound.neighObj.ithObj = elInfo->getSideOfNeighbour(i);
1254
	    
1255
1256
1257
1258
1259
	    if (dim == 2) {
	      // i == 2  =>  getSideOfNeighbour(i) == 2
	      TEST_EXIT_DBG(i != 2 || elInfo->getSideOfNeighbour(i) == 2)
		("Should not happen!\n");
	    }
1260

1261
	    // Get rank number of the neighbouring element.
1262
1263
	    int otherElementRank = partitionVec[elInfo->getNeighbour(i)->getIndex()];

1264
1265
1266
1267
1268
1269
1270
1271
1272

	    // === 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.                                         ===

1273
	    if (BoundaryManager::isBoundaryPeriodic(elInfo->getBoundary(subObj, i))) {	      
1274
1275
1276
1277
1278
	      // 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.
1279
1280
1281
1282
1283
1284
1285
1286
1287
	      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);
	      }	      
1288
	    }
1289
1290
1291
1292
1293
1294
 	  }
	}
      }

      elInfo = stack.traverseNext(elInfo);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
1295
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
1296
1297


Thomas Witkowski's avatar
Thomas Witkowski committed
1298
1299
1300
  void ParallelDomainBase::createSubstructureInteriorBoundaryInfo()
  {
    FUNCNAME("ParallelDomainBase::createSubstructureInteriorBoundaryInfo()");
Thomas Witkowski's avatar
Thomas Witkowski committed
1301

Thomas Witkowski's avatar
Thomas Witkowski committed
1302
1303
1304
1305
1306
    // === Seach for all vertices/edges, which are part of an interior boundary,   ===
    // === but are not a part of the interior boundaries that are created based on ===
    // === the information of macro elements.                                      ===

    int dim = mesh->getDim();
1307
    const BasisFunction *basFcts = feSpace->getBasisFcts();
Thomas Witkowski's avatar
Thomas Witkowski committed
1308
1309
1310
1311
1312
1313
1314
    std::vector<DegreeOfFreedom> localIndices(basFcts->getNumber(), 0);

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

1316
1317
    // Maps each edge in the whole domain to the rank that ownes this edge.
    std::map<GlobalEdge, int> edgeOwner;
Thomas Witkowski's avatar
Thomas Witkowski committed
1318
1319
    // Maps each edge in ranks partition of the domain to the element object that 
    // contains this edge.
1320
    std::map<GlobalEdge, BoundaryObject> rankEdges;
1321

1322

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

Thomas Witkowski's avatar
Thomas Witkowski committed
1325
1326
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
1327
1328
1329
1330
1331
1332
1333
    while (elInfo) {
      Element *el = elInfo->getElement();
      basFcts->getLocalIndices(el, feSpace->getAdmin(), localIndices);

      PartitionElementData *partitionData = dynamic_cast<PartitionElementData*>
	(el->getElementData(PARTITION_ED));
      
Thomas Witkowski's avatar
Thomas Witkowski committed
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
      // 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()]);
	  
	  // If the edge is part of an element that is part of rank's domain, add it
	  // to the set of all rank's edges.
	  if (partitionData && partitionData->getPartitionStatus() == IN)
	    rankEdges[edge] = BoundaryObject(el, elInfo->getType(), EDGE, i);
1348
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
      }

      
      // In 2d and 3d, traverse all vertices of current element.
      for (int i = 0; i < 4; i++) {
	DegreeOfFreedom dof0 = localIndices[i];
	dofOwner[dof0] = max(dofOwner[dof0], partitionVec[el->getIndex()]);

	if (partitionData && partitionData->getPartitionStatus() == IN)
	  rankDofs[dof0] = BoundaryObject(el, elInfo->getType(), VERTEX, i);
1359
1360
1361
1362
1363
      }      

      elInfo = stack.traverseNext(elInfo);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1364
1365

    // === Create a set of all edges and vertices at rank's interior boundaries. ===
1366
1367
1368

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

1372
    // First, traverse the rank owned elements af the interior boundaries.
Thomas Witkowski's avatar
Thomas Witkowski committed
1373
    for (RankToBoundMap::iterator rankIt = myIntBoundary.boundary.begin();
1374
	 rankIt != myIntBoundary.boundary.end(); ++rankIt) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1375
1376
1377
      for (std::vector<AtomicBoundary>::iterator boundIt = rankIt->second.begin(); 
	   boundIt != rankIt->second.end(); ++boundIt) {
	Element *el = boundIt->rankObj.el;
1378
1379
	basFcts->getLocalIndices(el, feSpace->getAdmin(), localIndices);

Thomas Witkowski's avatar
Thomas Witkowski committed
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
	if (dim == 3) {
	  for (int j = 0; j < 3; j++) {
	    int edgeNo = el->getEdgeOfFace(boundIt->rankObj.ithObj, j);
	    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)
1392
	      boundIt->rankObj.excludedSubstructures.push_back(std::make_pair(EDGE, edgeNo));
Thomas Witkowski's avatar
Thomas Witkowski committed
1393
1394
1395
	    else
	      rankBoundaryEdges.insert(edge);
	  }
1396
	}
1397
1398
      
	for (int j = 0; j < 3; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1399
	  int dofNo = el->getVertexOfPosition(FACE, boundIt->rankObj.ithObj, j);
1400
1401
1402
	  DegreeOfFreedom dof = localIndices[dofNo];

	  if (dofOwner[dof] > mpiRank)
1403
	    boundIt->rankObj.excludedSubstructures.push_back(std::make_pair(VERTEX, dofNo));
1404
1405
1406
	  else
	    rankBoundaryDofs.insert(dof);
	}
1407
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
1408
1409
    }

1410
    // Now, do the same with all other elements at the interio boundaries.
Thomas Witkowski's avatar
Thomas Witkowski committed
1411
1412
    for (RankToBoundMap::iterator rankIt = otherIntBoundary.boundary.begin();
	 rankIt != otherIntBoundary.boundary.end(); ++rankIt) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1413
1414
1415
      for (std::vector<AtomicBoundary>::iterator boundIt = rankIt->second.begin(); 
	   boundIt != rankIt->second.end(); ++boundIt) {
	Element *el = boundIt->rankObj.el;
1416
1417
1418
	basFcts->getLocalIndices(el, feSpace->getAdmin(), localIndices);

	for (int j = 0; j < 3; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1419
	  int edgeNo = el->getEdgeOfFace(boundIt->rankObj.ithObj, j);
1420
1421
1422
1423
1424
	  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 (edgeOwner[edge] > rankIt->first)
1425
	    boundIt->rankObj.excludedSubstructures.push_back(std::make_pair(EDGE, edgeNo));
1426
1427
	  else
	    rankBoundaryEdges.insert(edge);
1428
1429
1430
1431

	}

	for (int j = 0; j < 3; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1432
	  int dofNo = el->getVertexOfPosition(FACE, boundIt->rankObj.ithObj, j);
1433
1434
1435
	  DegreeOfFreedom dof = localIndices[dofNo];
	  
	  if (dofOwner[dof] > rankIt->first)
1436
	    boundIt->rankObj.excludedSubstructures.push_back(std::make_pair(VERTEX, dofNo));
1437
1438
	  else
	    rankBoundaryDofs.insert(dof);
1439
1440
1441
	}	
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
1442

1443

1444
1445
1446
1447
1448
1449
1450
1451
1452
    // === Create the new interior boundaries consisting only of edges. This    ===
    // === boundaries are created on that ranks, which do not own the boundary  ===
    // === but are on the other side of the edge. Than, theses ranks inform the ===
    // === owner of the edge, that they both share them.                        ===

    // 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
1453
    std::map<int, std::vector<BoundaryObject> > recvObjs;
1454
1455
    for (int i = 0; i < mpiSize; i++) {
      recvEdges[i].clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1456
      recvObjs[i].clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1457
1458
    }

1459
1460
1461
1462
1463
1464
1465
    // Traverse all edges in rank's domain.
    for (std::map<GlobalEdge, BoundaryObject>::iterator it = rankEdges.begin();
	 it != rankEdges.end(); ++it) {
      // If we have found an edge in rank's domain that has an owner with a higher
      // 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
      // domain we have to add to the corresponding interior domain object.
Thomas Witkowski's avatar
Thomas Witkowski committed
1466

1467
1468
      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
1469

1470
1471
1472
	// 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
1473
	  recvObjs[edgeOwner[it->first]].push_back(it->second);
1474
1475
1476

	  rankBoundaryDofs.insert(it->first.first);
	  rankBoundaryDofs.insert(it->first.second);
1477
1478
1479
1480
	}
      }
    }

1481

1482
1483
    // === Send all edge interior boundary infos to the owner of the new edge === 
    // === interior boundaries.                                               ===
Thomas Witkowski's avatar
Thomas Witkowski committed
1484

Thomas Witkowski's avatar
Thomas Witkowski committed
1485
    StdMpi<std::vector<GlobalEdge> > stdMpiEdge(mpiComm, true);
1486
1487
1488
1489
    stdMpiEdge.send(recvEdges);
    stdMpiEdge.recvFromAll();
    stdMpiEdge.startCommunication<int>(MPI_INT);

Thomas Witkowski's avatar
Thomas Witkowski committed
1490
1491
1492
1493
    StdMpi<std::vector<BoundaryObject> > stdMpiObjs(mpiComm, true);
    stdMpiObjs.send(recvObjs);
    stdMpiObjs.recvFromAll();
    stdMpiObjs.startCommunication<int>(MPI_INT);
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508


    // 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
1509
	TEST_EXIT_DBG(stdMpiObjs.getRecvData(it->first).size() > i)
1510
1511
1512
1513
	  ("Should not happen!\n");
	
	AtomicBoundary& b = myIntBoundary.getNewAtomic(it->first); 
	b.rankObj = rankEdges[it->second[i]];
Thomas Witkowski's avatar
Thomas Witkowski committed
1514
	b.neighObj = stdMpiObjs.getRecvData(it->first)[i];
1515
1516
	
	sendObjects[it->first].push_back(b.rankObj);
1517
1518
1519

	rankBoundaryDofs.insert(it->second[i].first);
	rankBoundaryDofs.insert(it->second[i].second);
1520
1521
1522
1523
1524
      }
    }

    // === Send the information about the elements on the owners side of the new ===
    // === edge interior boundaries.                                             ===
1525
    
Thomas Witkowski's avatar
Thomas Witkowski committed
1526
1527
1528
1529
    stdMpiObjs.clear();
    stdMpiObjs.send(sendObjects);
    stdMpiObjs.recvFromAll();
    stdMpiObjs.startCommunication<int>(MPI_INT);
1530
1531
1532
1533

    // === 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
1534
1535
    for (std::map<int, std::vector<BoundaryObject> >::iterator it = recvObjs.begin();
	it != recvObjs.end(); ++it) {
1536
      if (it->second.size() > 0) {	
Thomas Witkowski's avatar
Thomas Witkowski committed
1537
	TEST_EXIT_DBG(it->second.size() == stdMpiObjs.getRecvData(it->first).size())
1538
1539
1540
1541
1542
	  ("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
1543
	  b.neighObj = stdMpiObjs.getRecvData(it->first)[i];  	  
1544
1545
1546
	}
      }
    }
1547
1548


Thomas Witkowski's avatar
Thomas Witkowski committed
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
    // === 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(); 
1562
1563
	 it != rankDofs.end(); ++it) {
      if (dofOwner[it->first] > mpiRank && rankBoundaryDofs.count(it->first) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1564
1565
1566
1567
1568
1569
	std::vector<DegreeOfFreedom> &ownerDofs = recvGlobalDofs[dofOwner[it->first]];

	if (find(ownerDofs.begin(), ownerDofs.end(), it->first) == ownerDofs.end()) {
	  ownerDofs.push_back(it->first);
	  recvObjs[dofOwner[it->first]].push_back(it->second);
	}
1570
1571
      }
    }
1572

Thomas Witkowski's avatar
Thomas Witkowski committed
1573
1574
1575
1576
    StdMpi<std::vector<DegreeOfFreedom> > stdMpiDofs(mpiComm, true);
    stdMpiDofs.send(recvGlobalDofs);
    stdMpiDofs.recvFromAll();
    stdMpiDofs.startCommunication<int>(MPI_INT);
1577

Thomas Witkowski's avatar
Thomas Witkowski committed
1578
1579
1580
1581
    stdMpiObjs.clear();
    stdMpiObjs.send(recvObjs);
    stdMpiObjs.recvFromAll();
    stdMpiObjs.startCommunication<int>(MPI_INT);
Thomas Witkowski's avatar
Thomas Witkowski committed
1582

Thomas Witkowski's avatar
Thomas Witkowski committed
1583
1584
    for (int i = 0; i < mpiSize; i++)
      sendObjects[i].clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1585

Thomas Witkowski's avatar
Thomas Witkowski committed
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
    for (std::map<int, std::vector<DegreeOfFreedom> >::iterator it = stdMpiDofs.getRecvData().begin();
	 it != stdMpiDofs.getRecvData().end(); ++it) {
      for (unsigned int i = 0; i < it->second.size(); i++) {
	TEST_EXIT_DBG(stdMpiObjs.getRecvData(it->first).size() > i)
	  ("Should not happen!\n");
	
	AtomicBoundary& b = myIntBoundary.getNewAtomic(it->first); 
	b.rankObj = rankDofs[it->second[i]];
	b.neighObj = stdMpiObjs.getRecvData(it->first)[i];
	
	sendObjects[it->first].push_back(b.rankObj);
Thomas Witkowski's avatar
Thomas Witkowski committed
1597
1598
1599
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1600
1601
1602
1603
    stdMpiObjs.clear();
    stdMpiObjs.send(sendObjects);
    stdMpiObjs.recvFromAll();
    stdMpiObjs.startCommunication<int>(MPI_INT);
1604
1605


Thomas Witkowski's avatar
Thomas Witkowski committed
1606
1607
1608
1609
1610
    for (std::map<int, std::vector<BoundaryObject> >::iterator it = recvObjs.begin();
	it != recvObjs.end(); ++it) {
      if (it->second.size() > 0) {	
	TEST_EXIT_DBG(it->second.size() == stdMpiObjs.getRecvData(it->first).size())
	  ("Should not happen!\n");
1611

Thomas Witkowski's avatar
Thomas Witkowski committed
1612
1613
1614
1615
	for (unsigned int i = 0; i < it->second.size(); i++) {
	  AtomicBoundary& b = otherIntBoundary.getNewAtomic(it->first); 
	  b.rankObj = it->second[i];
	  b.neighObj = stdMpiObjs.getRecvData(it->first)[i];  	  
1616
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
1617
1618
      }
    }   
1619
1620
  }

1621

1622
  void ParallelDomainBase::removeMacroElements()
1623
  {
1624
1625
1626
    FUNCNAME("ParallelDomainBase::removeMacroElements()");

    std::set<MacroElement*> macrosToRemove;
1627
1628
1629
1630
1631
1632
1633
    for (std::deque<MacroElement*>::iterator it = mesh->firstMacroElement();
	 it != mesh->endOfMacroElements();
	 ++it) {
      PartitionElementData *partitionData = 
	dynamic_cast<PartitionElementData*>
	((*it)->getElement()->getElementData(PARTITION_ED));
      if (partitionData->getPartitionStatus() != IN)
1634
	macrosToRemove.insert(*it);
1635
1636
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1637
    mesh->removeMacroElements(macrosToRemove, feSpace);
1638
1639
1640
  }


1641
  void ParallelDomainBase::createLocalGlobalNumbering()
1642
  {
1643
    FUNCNAME("ParallelDomainBase::createLocalGlobalNumbering()");
1644
1645

    // === Get rank information about DOFs. ===
1646
1647

    // Stores to each DOF pointer the set of ranks the DOF is part of.
1648
1649
    DofToPartitions partitionDofs;
    DofContainer rankDofs, rankAllDofs;
1650
    DofToRank boundaryDofs;
1651

1652
    createDofMemberInfo(partitionDofs, rankDofs, rankAllDofs, boundaryDofs, vertexDof);
1653

1654
    nRankDofs = rankDofs.size();
1655
    int nOverallDofs = partitionDofs.size();
1656

1657

1658
    // === Get starting position for global rank dof ordering. ====
1659

1660
    rstart = 0;
1661
1662
    mpiComm.Scan(&nRankDofs, &rstart, 1, MPI_INT, MPI_SUM);
    rstart -= nRankDofs;
1663

1664
1665
1666
    // === Create for all dofs in rank new indices. The new index must start at ===
    // === index 0, must be continues and have the same order as the indices    ===
    // === had before.                                                          ===
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1667

1668
    // Do not change the indices now, but create a new indexing and store it here.
1669
1670
    DofIndexMap rankDofsNewLocalIndex;
    isRankDof.clear();
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1671
1672
1673
    int i = 0;
    for (DofContainer::iterator dofIt = rankAllDofs.begin();
	 dofIt != rankAllDofs.end(); ++dofIt) {
1674
1675
1676
      rankDofsNewLocalIndex[*dofIt] = i;
      // First, we set all dofs in ranks partition to be owend by the rank. Later,
      // the dofs in ranks partition that are owned by other rank are set to false.
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1677
      isRankDof[i] = true;
1678
      i++;
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1679
1680
    }

1681

1682
    // === Create for all rank owned dofs a new global indexing. ===
1683

1684
1685
    // Stores for dofs in rank a new global index.
    DofIndexMap rankDofsNewGlobalIndex;
1686
1687
1688
    // Stores for all rank owned dofs a continues local index.
    DofIndexMap rankOwnedDofsNewLocalIndex;

Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1689
    i = 0;
1690
1691
    for (DofContainer::iterator dofIt = rankDofs.begin();
	 dofIt != rankDofs.end(); ++dofIt) {
1692
      rankDofsNewGlobalIndex[*dofIt] = i + rstart;
1693
      rankOwnedDofsNewLocalIndex[*dofIt] = i;
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1694
1695
1696
      i++;
    }

1697

1698
1699
1700
1701
1702
    // === Create information which dof indices must be send and which must  ===
    // === be received.                                                      ===

    // Stores to each rank a map from DOF pointers (which are all owned by the rank
    // and lie on an interior boundary) to new global DOF indices.
1703
    std::map<int, DofIndexMap> sendNewDofs;
1704

1705
1706
1707
1708
    // Maps to each rank the number of new DOF indices this rank will receive from.
    // All these DOFs are at an interior boundary on this rank, but are owned by
    // another rank.
    std::map<int, int> recvNewDofs;
1709

1710
    for (DofToRank::iterator it = boundaryDofs.begin(); it != boundaryDofs.end(); ++it) {
1711
1712
1713
1714
1715

      if (it->second == mpiRank) {
	// If the boundary dof is a rank dof, it must be send to other ranks.

	// Search for all ranks that have this dof too.
1716
1717
	for (std::set<int>::iterator itRanks = partitionDofs[it->first].begin();
	     itRanks != partitionDofs[it->first].end();
1718
	     ++itRanks) {
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1719
	  if (*itRanks != mpiRank) {
1720
	    TEST_EXIT_DBG(rankDofsNewGlobalIndex.count(it->first) == 1)
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1721
1722
	      ("DOF Key not found!\n");

1723
	    sendNewDofs[*itRanks][it->first] = rankDofsNewGlobalIndex[it->first];
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1724
	  }
1725
1726
	}
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
1727
1728
	// If the boundary dof is not a rank dof, its new dof index (and later
	// also the dof values) must be received from another rank.
1729
1730
1731
1732
	if (recvNewDofs.find(it->second) == recvNewDofs.end()) 
	  recvNewDofs[it->second] = 1;
	else
	  recvNewDofs[it->second]++;
1733
1734
1735
      }
    }

Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1736

1737
    // === Send and receive the dof indices at boundary. ===
1738

1739
    sendDofs.clear();
1740
    for (std::map<int, DofIndexMap>::iterator sendIt = sendNewDofs.begin();
1741
	 sendIt != sendNewDofs.end(); ++sendIt)
1742
      for (DofIndexMap::iterator dofIt = sendIt->second.begin();
1743
	   dofIt != sendIt->second.end(); ++dofIt)
1744
	sendDofs[sendIt->first].push_back(dofIt->first);
1745

Thomas Witkowski's avatar
Thomas Witkowski committed
1746
1747
1748
    typedef std::vector<std::pair<DegreeOfFreedom, DegreeOfFreedom> > DofMapVec;

    StdMpi<DofIndexMap, DofMapVec> stdMpi(mpiComm);
1749
    stdMpi.send(sendNewDofs);
1750
    for (std::map<int, int>::iterator recvIt = recvNewDofs.begin();
1751
1752
	 recvIt != recvNewDofs.end(); ++recvIt, i++)
      stdMpi.recv(recvIt->first, recvIt->second * 2); 
1753
    stdMpi.startCommunication<int>(MPI_INT);
Thomas Witkowski's avatar
Thomas Witkowski committed
1754
    std::map<int, DofMapVec> &dofMap = stdMpi.getRecvData();
1755

1756

1757
    // === Change dof indices at boundary from other ranks. ===
1758

Thomas Witkowski's avatar
Thomas Witkowski committed
1759
1760
    // Within this small data structure we track which dof index was already changed.
    // This is used to avoid the following situation: Assume, there are two dof indices
1761
    // a and b in boundaryDofs. Then we have to change index a to b and b to c. When
Thomas Witkowski's avatar
Thomas Witkowski committed
1762
1763
1764
1765
1766
    // the second rule applies, we have to avoid that not the first b, resulted from
    // changing a to b, is set to c, but the second one. Therefore, after the first
    // rule was applied, the dof pointer is set to false in this data structure and 
    // is not allowed to be changed anymore.
    std::map<const DegreeOfFreedom*, bool> dofChanged;
1767
1768
    recvDofs.clear();

1769
    for (DofToRank::iterator dofIt = boundaryDofs.begin(); dofIt != boundaryDofs.end();
Thomas Witkowski's avatar
Thomas Witkowski committed
1770
	 ++dofIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
1771
1772
      dofChanged[dofIt->first] = false;

1773
    for (std::map<int, std::vector<std::pair<DegreeOfFreedom, DegreeOfFreedom> > >::iterator rankIt = dofMap.begin();
1774
1775
 	 rankIt != dofMap.end(); ++rankIt) {
      
1776
      for (std::vector<std::pair<DegreeOfFreedom, DegreeOfFreedom> >::iterator recvDofIt = rankIt->second.begin();
1777
	   recvDofIt != rankIt->second.end(); ++recvDofIt) {
1778

1779
1780
1781
	DegreeOfFreedom oldDof = recvDofIt->first;
	DegreeOfFreedom newGlobalDof = recvDofIt->second;
	
1782
	bool found = false;
1783
	
1784
	// Iterate over all boundary dofs to find the dof which index we have to change.
1785
	
1786
	for (DofToRank::iterator dofIt = boundaryDofs.begin(); 
1787
1788
	     dofIt != boundaryDofs.end(); ++dofIt) {   
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
1789
1790
	  if (*(dofIt->first) == oldDof && !dofChanged[dofIt->first]) {
	    dofChanged[dofIt->first] = true;
1791
1792
	    
	    recvDofs[rankIt->first].push_back(dofIt->first);
1793
	    rankDofsNewGlobalIndex[dofIt->first] = newGlobalDof;
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1794
	    isRankDof[rankDofsNewLocalIndex[dofIt->first]] = false;
1795
	    
1796
	    found = true;
1797
1798
1799
	    break;
	  }
	}
1800
	
Thomas Witkowski's avatar
Thomas Witkowski committed
1801
	TEST_EXIT_DBG(found)("Should not happen!\n");
1802
      }      
1803
    }
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1804

1805

1806
    // === Create now the local to global index and local to dof index mappings.  ===
1807

1808
1809
    createLocalMappings(rankDofsNewLocalIndex, rankOwnedDofsNewLocalIndex,
			rankDofsNewGlobalIndex);
1810
1811
1812
1813
1814

    nRankRows = nRankDofs * nComponents;
    nOverallRows = nOverallDofs * nComponents;

    lastMeshChangeIndex = mesh->getChangeIndex();
1815
1816
  }

1817

1818
  void ParallelDomainBase::updateLocalGlobalNumbering()
1819
  {
1820
    FUNCNAME("ParallelDomainBase::updateLocalGlobalNumbering()");
Thomas Witkowski's avatar
Thomas Witkowski committed
1821

1822
1823
1824
1825
1826
#if (DEBUG != 0)
    ElementIdxToDofs elMap;
    dbgCreateElementMap(elMap);
#endif

1827
    typedef std::set<const DegreeOfFreedom*> DofSet;
Thomas Witkowski's avatar
Thomas Witkowski committed
1828

1829
    // === Get all DOFs in ranks partition. ===
1830

Thomas Witkowski's avatar
Thomas Witkowski committed
1831
    ElementDofIterator elDofIt(feSpace);
1832
    DofSet rankDofSet;
1833
    
1834
1835
1836
    // The vertexDof list must be recreated from the scratch. Otherwise, it is possible
    // that it maps dofs, that were removed (this is also possible, if the mesh was
    // refined, e.g., center dofs of an element are not dofs of the children).
1837
    DofToBool oldVertexDof = vertexDof;
1838
1839
    vertexDof.clear();

1840
1841
1842
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1843
      Element *element = elInfo->getElement();     
Thomas Witkowski's avatar
Thomas Witkowski committed
1844
1845
      elDofIt.reset(element);
      do {
1846
1847
1848
1849
1850
1851
	rankDofSet.insert(elDofIt.getDofPtr());

	if (elDofIt.getCurrentPos() == 0) 
	  vertexDof[elDofIt.getDofPtr()] = true;
	else
	  vertexDof[elDofIt.getDofPtr()] = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
1852
      } while(elDofIt.next());
1853
1854
1855
1856

      elInfo = stack.traverseNext(elInfo);
    }

1857
    DofContainer rankAllDofs;
1858
    for (DofSet::iterator dofIt = rankDofSet.begin(); dofIt != rankDofSet.end(); ++dofIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
1859
      rankAllDofs.push_back(*dofIt);    
1860
    sort(rankAllDofs.begin(), rankAllDofs.end(), cmpDofsByValue);
1861
    DofContainer rankDofs = rankAllDofs;
1862
1863


Thomas Witkowski's avatar
Thomas Witkowski committed
1864
    // === Traverse on interior boundaries and move all not ranked owned DOFs from ===
1865
    // === rankDofs to boundaryDOFs.                                               ===
1866

1867
1868
1869
    RankToDofContainer oldSendDofs = sendDofs;
    RankToDofContainer oldRecvDofs = recvDofs;

1870
1871
    sendDofs.clear();
    recvDofs.clear();
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888

    for (RankToDofContainer::iterator it = oldSendDofs.begin();
	 it != oldSendDofs.end(); ++it) {
      for (DofContainer::iterator dofIt = it->second.begin();
	   dofIt != it->second.end(); ++dofIt) {
	if (oldVertexDof[*dofIt])
	  sendDofs[it->first].push_back(*dofIt);
      }
    }

    for (RankToDofContainer::iterator it = oldRecvDofs.begin();
	 it != oldRecvDofs.end(); ++it) {
      for (DofContainer::iterator dofIt = it->second.begin();
	   dofIt != it->second.end(); ++dofIt) {
	if (oldVertexDof[*dofIt]) {
	  recvDofs[it->first].push_back(*dofIt);

1889
1890
	  DofContainer::iterator eraseIt = 
	    find(rankDofs.begin(), rankDofs.end(), *dofIt);
1891
1892
1893
1894
1895
1896
	  if (eraseIt != rankDofs.end())
	    rankDofs.erase(eraseIt);    
	}
      }
    }

1897
    
1898
1899
    for (RankToBoundMap::iterator it = myIntBoundary.boundary.begin();
	 it != myIntBoundary.boundary.end(); ++it) {    
1900
     
1901
      DofContainer &dofsToSend = sendDofs[it->first];
Thomas Witkowski's avatar
Thomas Witkowski committed
1902

Thomas Witkowski's avatar
Thomas Witkowski committed
1903
      for (std::vector<AtomicBoundary>::iterator boundIt = it->second.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
1904
	   boundIt != it->second.end(); ++boundIt) {
1905
	DofContainer dofs;	
1906
1907
	boundIt->rankObj.el->getVertexDofs(feSpace, boundIt->rankObj, dofs);
   	boundIt->rankObj.el->getNonVertexDofs(feSpace, boundIt->rankObj, dofs);
1908

1909
	for (int i = 0; i < static_cast<int>(dofs.size()); i++)
1910
	  dofsToSend.push_back(dofs[i]);
1911
      }
1912
    } 
1913

Thomas Witkowski's avatar
Thomas Witkowski committed
1914
1915
1916
    for (RankToBoundMap::iterator it = otherIntBoundary.boundary.begin();
	 it != otherIntBoundary.boundary.end(); ++it) {

1917
      DofContainer &dofsToRecv = recvDofs[it->first];
1918

1919
1920
      for (std::vector<AtomicBoundary>::iterator boundIt = it->second.begin();
	   boundIt != it->second.end(); ++boundIt) {
1921
	DofContainer dofs;
1922
1923
   	boundIt->rankObj.el->getNonVertexDofs(feSpace, boundIt->rankObj, dofs);
	boundIt->rankObj.el->getVertexDofs(feSpace, boundIt->rankObj, dofs);
1924
1925
1926
1927

	for (int i = 0; i < static_cast<int>(dofs.size()); i++) {
	  DofContainer::iterator eraseIt = 
	    find(rankDofs.begin(), rankDofs.end(), dofs[i]);
1928
1929
	  if (eraseIt != rankDofs.end()) 
	    rankDofs.erase(eraseIt);	 
1930
  
1931
	  dofsToRecv.push_back(dofs[i]);
1932
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
1933
1934
1935
      }
    }

1936
    nRankDofs = rankDofs.size();
1937
1938
1939

    // === Get starting position for global rank dof ordering. ====

1940
1941
    mpiComm.Scan(&nRankDofs, &rstart, 1, MPI_INT, MPI_SUM);
    rstart -= nRankDofs;
1942
1943
1944
1945


    // === Calculate number of overall DOFs of all partitions. ===

1946
1947
    int nOverallDofs = 0;
    mpiComm.Allreduce(&nRankDofs, &nOverallDofs, 1, MPI_INT, MPI_SUM);
1948
1949


1950
    // Do not change the indices now, but create a new indexing and store it here.
1951
1952
    DofIndexMap rankDofsNewLocalIndex;
    isRankDof.clear();
1953
    int i = 0;
1954
1955
    for (DofContainer::iterator dofIt = rankAllDofs.begin();
	 dofIt != rankAllDofs.end(); ++dofIt) {
1956

1957
1958
1959
      rankDofsNewLocalIndex[*dofIt] = i;
      // First, we set all dofs in ranks partition to be owend by the rank. Later,
      // the dofs in ranks partition that are owned by other rank are set to false.
Thomas Witkowski's avatar
n    
Thomas Witkowski committed
1960
      isRankDof[i] = true;
1961
      i++;
1962
1963
    }

1964
    // Stores for all rank owned dofs a new global index.
1965
    DofIndexMap rankDofsNewGlobalIndex;
1966
1967
1968
1969
    // Stores for all rank owned dofs a continues local index.
    DofIndexMap rankOwnedDofsNewLocalIndex;

    i = 0;
1970
1971
    for (DofContainer::iterator dofIt = rankDofs.begin();
	 dofIt != rankDofs.end(); ++dofIt) {
1972
      rankDofsNewGlobalIndex[*dofIt] = i + rstart;
1973
1974
1975
1976
      rankOwnedDofsNewLocalIndex[*dofIt] = i;
      i++;
    }

1977
1978
    // === Send new DOF indices. ===

1979
    StdMpi<std::vector<DegreeOfFreedom> > stdMpi(mpiComm, false);
1980
1981
1982
1983
1984
1985
1986
1987
    for (RankToDofContainer::iterator sendIt = sendDofs.begin();
	 sendIt != sendDofs.end(); ++sendIt, i++) {
      stdMpi.getSendData(sendIt->first).resize(0);
      stdMpi.getSendData(sendIt->first).reserve(sendIt->second.size());
      for (DofContainer::iterator dofIt = sendIt->second.begin();
	   dofIt != sendIt->second.end(); ++dofIt)
	stdMpi.getSendData(sendIt->first).push_back(rankDofsNewGlobalIndex[*dofIt]);
    }
1988
1989
1990
    stdMpi.updateSendDataSize();
    stdMpi.recv(recvDofs);
    stdMpi.startCommunication<int>(MPI_INT);
1991

1992
1993
    for (RankToDofContainer::iterator recvIt = recvDofs.begin();
	 recvIt != recvDofs.end(); ++recvIt) {      
1994
      int j = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
1995
      for (DofContainer::iterator dofIt = recvIt->second.begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
1996
	   dofIt != recvIt->second.end(); ++dofIt) {
1997
	rankDofsNewGlobalIndex[*dofIt] = stdMpi.getRecvData(recvIt->first)[j++];