Mesh.cc 39.6 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
    // === Write periodic associations. ===
    int mapSize = periodicAssociations.size();
    SerUtil::serialize(out, mapSize);
    for (std::map<BoundaryType, VertexVector*>::iterator it = periodicAssociations.begin();
	 it != periodicAssociations.end(); ++it) {
      BoundaryType b = it->first;

      // Check which DOFAdmin is used for the current VertexVector we want to serialize.
      int ithAdmin = -1;
      for (int i = 0; i < static_cast<int>(admin.size()); i++) {
	if (admin[i] == it->second->getAdmin()) {
	  ithAdmin = i;
	  break;
	}
      }
      TEST_EXIT(ithAdmin >= 0)
	("No DOFAdmin found for serialization of periodic associations!\n");

      SerUtil::serialize(out, b);
      SerUtil::serialize(out, ithAdmin);
      it->second->serialize(out);
    }

1024
1025
1026
    serializedDOFs.clear();
  }

1027

Thomas Witkowski's avatar
Thomas Witkowski committed
1028
  void Mesh::deserialize(std::istream &in)
1029
  {
1030
1031
    FUNCNAME("Mesh::deserialize()");

1032
1033
1034
1035
1036
1037
    serializedDOFs.clear();

    in >> name;
    in.get();

    int oldVal = dim;
1038
1039
    SerUtil::deserialize(in, dim);
    TEST_EXIT_DBG(oldVal == 0 || dim == oldVal)("Invalid dimension!\n");
1040

1041
1042
1043
1044
1045
1046
    SerUtil::deserialize(in, nVertices);
    SerUtil::deserialize(in, nEdges);
    SerUtil::deserialize(in, nLeaves);
    SerUtil::deserialize(in, nElements);
    SerUtil::deserialize(in, nFaces);
    SerUtil::deserialize(in, maxEdgeNeigh);
1047
1048
1049

    diam.deserialize(in);

1050
    SerUtil::deserialize(in, preserveCoarseDOFs);
1051
1052

    oldVal = nDOFEl;
1053
1054
    SerUtil::deserialize(in, nDOFEl);
    TEST_EXIT_DBG(oldVal == 0 || nDOFEl == oldVal)("Invalid nDOFEl!\n");
1055
1056
1057
1058

    nDOF.deserialize(in);

    oldVal = nNodeEl;
1059
1060
    SerUtil::deserialize(in, nNodeEl);
    TEST_EXIT_DBG(oldVal == 0 || nNodeEl == oldVal)("Invalid nNodeEl!\n");
1061
1062
1063

    node.deserialize(in);

1064
1065
1066

    // === Read admins. ===

1067
    int size;
1068
    SerUtil::deserialize(in, size);
1069
    admin.resize(size, NULL);
1070
    for (int i = 0; i < size; i++) {
1071
      if (!admin[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
1072
	admin[i] = new DOFAdmin(this);
1073

1074
1075
1076
      admin[i]->deserialize(in);
    }

1077
    SerUtil::deserialize(in, size);
Thomas Witkowski's avatar
Thomas Witkowski committed
1078
    std::vector< std::vector<int> > neighbourIndices(size);
1079

1080
    deleteMeshStructure();
1081
1082
1083
1084
1085
1086
1087
1088

    // All macro elements are stored in the list \ref macroElements with continous 
    // index from 0 to n - 1. But the macro element index may be different and may
    // contain holes (for example when macro elements were removed because of domain
    // decomposition based parallelization. Therefore we create a temporary map
    // from macro element indices to the continous index of \ref macroElements. This
    // will be used later to find the correct neighbours of the macro elements.
    std::map<int, int> elIndexVecIndex;
1089

1090
    macroElements.resize(size);
1091
    for (int i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1092
      macroElements[i] = new MacroElement(dim);
1093
1094
      macroElements[i]->writeNeighboursTo(&(neighbourIndices[i]));
      macroElements[i]->deserialize(in);
1095
      elIndexVecIndex[macroElements[i]->getIndex()] = i;
1096
1097
    }

1098
1099
    SerUtil::deserialize(in, elementIndex);
    SerUtil::deserialize(in, initialized);
1100
1101

    // set neighbour pointer in macro elements
1102
    int nNeighbour = getGeo(NEIGH);
1103
    for (int i = 0; i < static_cast<int>(macroElements.size()); i++) {
1104
      for (int j = 0; j < nNeighbour; j++) {
1105
	int index = neighbourIndices[i][j];
1106
1107
1108
1109
1110
1111
1112

	if (index != -1) {
	  TEST_EXIT_DBG(elIndexVecIndex.count(index) == 1)
	    ("Cannot find correct index from neighbouring macro element!\n");

	  index = elIndexVecIndex[index];

1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
	  macroElements[i]->setNeighbour(j, macroElements[index]);
	} else {
	  macroElements[i]->setNeighbour(j, NULL);
	}
      }
    }

    // set mesh pointer in elements
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(this, -1, CALL_EVERY_EL_PREORDER);
1123
    while (elInfo) {
1124
1125
1126
1127
1128
      elInfo->getElement()->setMesh(this);
      elInfo = stack.traverseNext(elInfo);
    }

    serializedDOFs.clear();
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144

    
    /// === Read periodic assoications. ===

    int mapSize = 0;
    SerUtil::deserialize(in, mapSize);
    for (int i = 0; i < mapSize; i++) {
      BoundaryType b = 0;
      int ithAdmin = 0;
      SerUtil::deserialize(in, b);
      SerUtil::deserialize(in, ithAdmin);

      VertexVector *tmpvec = new VertexVector(admin[ithAdmin], "");
      tmpvec->deserialize(in);
      periodicAssociations[b] = tmpvec;      
    }
1145
1146
  }

1147

1148
1149
  void Mesh::initialize() 
  {
1150
1151
    FUNCNAME("Mesh::initialize()");

Thomas Witkowski's avatar
Thomas Witkowski committed
1152
1153
    std::string macroFilename("");
    std::string valueFilename("");
1154
    std::string periodicFilename("");
1155
1156
1157
1158
    int check = 1;

    GET_PARAMETER(0, name + "->macro file name",  &macroFilename);
    GET_PARAMETER(0, name + "->value file name",  &valueFilename);
1159
    GET_PARAMETER(0, name + "->periodic file", &periodicFilename);
1160
1161
1162
1163
    GET_PARAMETER(0, name + "->check", "%d", &check);
    GET_PARAMETER(0, name + "->preserve coarse dofs", "%d", &preserveCoarseDOFs);

    if (macroFilename.length()) {
1164
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
1165
      if (checkParallelMacroFile(macroFilename, periodicFilename, check)) {
1166
1167
1168
 	std::string newPeriodicFilename = "";
 	if (periodicFilename != "")
 	  newPeriodicFilename = periodicFilename + ".tmp";
1169

1170
1171
1172
 	macroFileInfo = 
 	  MacroReader::readMacro(macroFilename + ".tmp", this, 
 				 newPeriodicFilename, check);
1173
      } else {
1174
1175
	macroFileInfo = 
	  MacroReader::readMacro(macroFilename, this, periodicFilename, check);
1176
      }
1177
1178
1179
1180
1181
#else
      // In sequentiel computations just read the macro file to the mesh.
      macroFileInfo = 
	MacroReader::readMacro(macroFilename, this, periodicFilename, check);
#endif
1182

1183
1184
1185
1186
1187
      // In the sequentiell case, if there is no value file which should 
      // be written, we can delete the information of the macro file. In
      // parallel computations, the macro file infos are always needed for
      // mesh repartitioning.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
1188
      if (!valueFilename.length())
1189
1190
       	clearMacroFileInfo();
#endif
1191
1192
1193
1194
1195
    }

    initialized = true;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1196

1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  bool Mesh::checkParallelMacroFile(std::string macroFilename, 
				    std::string periodicFilename,
				    int check)
  {
    FUNCNAME("Mesh::checkParallelMacroFile()");

    // In parallel computations, first the mesh must be checked if it is refined
    // in an apropriate way.
    
    TEST_EXIT(admin.size() == 1)("Not yet implemented!\n");


    // === Create a temporary mesh and load the macro file to it. ===
    
    Mesh testMesh(name, dim);
1213
    testMesh.setElementDataPrototype(new LeafDataEstimatableVec(new LeafDataCoarsenableVec));
1214
1215
1216
1217
1218
1219
    DOFAdmin *localAdmin = new DOFAdmin(&testMesh, admin[0]->getName());
    localAdmin->setNumberOfDOFs(admin[0]->getNumberOfDOFs());
    testMesh.addDOFAdmin(localAdmin);
    
    MacroInfo *testMacroInfo = 
      MacroReader::readMacro(macroFilename, &testMesh, periodicFilename, check);
1220
1221
    testMacroInfo->clear();
    delete testMacroInfo;
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344


    // === Check the mesh structure. ===
    
    int nMacroElements = 0;
    int elType = -1;
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(&testMesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      if (elType == -1) {
	elType = elInfo->getType();
      } else {
	TEST_EXIT(elType == elInfo->getType())
	  ("All elements in mesh must have the same element type!\n");
      }
      
      nMacroElements++;
      
      elInfo = stack.traverseNext(elInfo);
    }

    
    // === Calculate the number of global refinements such that the new mesh ===
    // === would fulfill all requirements.                                   ===

    // There should be at least 10 macro Elements per processor, therefore:
    //        nMacroElements * 2^gr >= nProcs * 10
    //    =>  gr = log_2(nProcs * 10 / nMacroElements)
    
    double tmp = 10.0 * MPI::COMM_WORLD.Get_size() / nMacroElements;
    int nrRefine = ceil(log(tmp) / log(2));
    if (nrRefine < 0)
      nrRefine = 0;
    
    if (dim == 3) {
      int newElType = (elType + nrRefine) % 3;
      switch (newElType) {
      case 1:
	if (nrRefine > 0)
	  nrRefine--;
	else 
	  nrRefine = 2;
	break;
      case 2:
	nrRefine++;
	break;	
      }
      
      TEST_EXIT((elType + nrRefine) % 3 == 0)("This should not happen!\n");
    }	    


    // === If we do not need to refine the mesh, return back. ===

    if (nrRefine == 0)
      return false;


    // === If macro weights are explicitly given, we cannot change the mesh. ===

    std::string macroWeightsFilename = "";
    GET_PARAMETER(0, name + "->macro weights", &macroWeightsFilename);
    if (macroWeightsFilename != "") {
      ERROR_EXIT("Should not happen!\n");
    }


    // === Rank 0 creates a new mesh file. ===

    if (MPI::COMM_WORLD.Get_rank() == 0) {
      RefinementManager *refManager;
      if (dim == 2)
       	refManager = new RefinementManager2d();
      else
       	refManager = new RefinementManager3d();

      refManager->globalRefine(&testMesh, nrRefine);      
      delete refManager;

      Lagrange* basFcts = Lagrange::getLagrange(dim, 1);
      FiniteElemSpace *feSpace = 
	FiniteElemSpace::provideFeSpace(localAdmin, basFcts, &testMesh, "tmp");

      DataCollector dc(feSpace);
      MacroWriter::writeMacro(&dc, (macroFilename + ".tmp").c_str());

      if (periodicFilename != "")
	MacroWriter::writePeriodicFile(&dc, (periodicFilename + ".tmp").c_str());      
    }


    // === All ranks must wait until rank 0 has created the new macro mesh file. ===

    MPI::COMM_WORLD.Barrier();


    // === We have refined the mesh, so reduce the number of global refinements. ===

    int globalRefinements = 0;
    GET_PARAMETER(0, name + "->global refinements", "%d", &globalRefinements);

    if (globalRefinements < nrRefine)
      globalRefinements = 0;
    else 
      globalRefinements -= nrRefine;

    std::stringstream oss;
    oss << globalRefinements;

    ADD_PARAMETER(0, name + "->global refinements", oss.str().c_str());


    // === Print a note to the screen that another mesh file will be used. ===

    MSG("The macro mesh file \"%s\" was refined %d times and stored to file \"%s\".\n",
	macroFilename.c_str(), nrRefine, (macroFilename + ".tmp").c_str());


    return true;
  }
#endif


1345
1346
  bool Mesh::associated(DegreeOfFreedom dof1, DegreeOfFreedom dof2) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1347
1348
    std::map<BoundaryType, VertexVector*>::iterator it;
    std::map<BoundaryType, VertexVector*>::iterator end = periodicAssociations.end();
1349
    for (it = periodicAssociations.begin(); it != end; ++it)
1350
1351
      if ((*(it->second))[dof1] == dof2)
	return true;
1352

1353
1354
1355
    return false;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1356

1357
1358
  bool Mesh::indirectlyAssociated(DegreeOfFreedom dof1, DegreeOfFreedom dof2) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1359
1360
1361
    std::vector<DegreeOfFreedom> associatedToDOF1;
    std::map<BoundaryType, VertexVector*>::iterator it;
    std::map<BoundaryType, VertexVector*>::iterator end = periodicAssociations.end();
1362
1363
1364
    DegreeOfFreedom dof, assDOF;

    associatedToDOF1.push_back(dof1);
Thomas Witkowski's avatar
Thomas Witkowski committed
1365
1366
1367
    for (it = periodicAssociations.begin(); it != end; ++it) {
      int size = static_cast<int>(associatedToDOF1.size());
      for (int i = 0; i < size; i++) {
1368
1369
	dof = associatedToDOF1[i];
	assDOF = (*(it->second))[dof];
Thomas Witkowski's avatar
Thomas Witkowski committed
1370
	if (assDOF == dof2) {
1371
1372
	  return true;
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
1373
1374
	  if (assDOF != dof) 
	    associatedToDOF1.push_back(assDOF);
1375
1376
1377
	}
      }
    }
1378

1379
1380
1381
    return false;
  }

1382

1383
1384
  void Mesh::clearMacroFileInfo()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1385
    macroFileInfo->clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1386
    delete macroFileInfo;
1387
    macroFileInfo = NULL;
1388
  }
1389

1390

1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
  void Mesh::computeMatrixFillin(const FiniteElemSpace *feSpace, 
				 std::vector<int> &nnzInRow, int &overall, int &average)
  {
    std::map<DegreeOfFreedom, int> dofCounter;

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(this, -1, Mesh::CALL_LEAF_EL);
    ElementDofIterator elDofIter(feSpace);
    while (elInfo) {
      elDofIter.reset(elInfo->getElement());
      do {
	DegreeOfFreedom dof = elDofIter.getDof();
	if (dofCounter.count(dof) == 0) {
	  dofCounter[dof] = 1;
	} else {
	  dofCounter[dof]++;
	}
      } while (elDofIter.next());
      elInfo = stack.traverseNext(elInfo);
    } 

    overall = 0;
    for (std::map<DegreeOfFreedom, int>::iterator it = dofCounter.begin();
1414
1415
	 it != dofCounter.end(); ++it)
      overall += it->second * 15;    
1416
1417
  }

1418

1419
1420
  int Mesh::calcMemoryUsage()
  {
1421
    int result = sizeof(Mesh);
1422

1423
1424
1425
1426
1427
1428
    result += nDOFEl;
    for (int i = 0; i < static_cast<int>(admin.size()); i++) {
      result += admin[i]->calcMemoryUsage();
      result += admin[i]->getUsedSize() * sizeof(DegreeOfFreedom);
    }
    
1429
1430
    for (int i = 0; i < static_cast<int>(macroElements.size()); i++)
      result += macroElements[i]->calcMemoryUsage();    
1431
1432
1433
    
    return result;
  }
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447


  void Mesh::deleteMeshStructure()
  {
    Element::deletedDOFs.clear();
    
    for (std::deque<MacroElement*>::const_iterator it = macroElements.begin();
	 it != macroElements.end(); ++it) {
      (*it)->getElement()->deleteElementDOFs();
      delete *it;
    }    

    Element::deletedDOFs.clear();
  }
1448
}
For faster browsing, not all history is shown. View entire blame