Mesh.cc 38.7 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
	("No DOFAdmin found for serialization of periodic associations!\n");

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

1008
1009
1010
    serializedDOFs.clear();
  }

1011

Thomas Witkowski's avatar
Thomas Witkowski committed
1012
  void Mesh::deserialize(std::istream &in)
1013
  {
1014
1015
    FUNCNAME("Mesh::deserialize()");

1016
1017
1018
1019
1020
1021
    serializedDOFs.clear();

    in >> name;
    in.get();

    int oldVal = dim;
1022
1023
    SerUtil::deserialize(in, dim);
    TEST_EXIT_DBG(oldVal == 0 || dim == oldVal)("Invalid dimension!\n");
1024

1025
1026
1027
1028
1029
1030
    SerUtil::deserialize(in, nVertices);
    SerUtil::deserialize(in, nEdges);
    SerUtil::deserialize(in, nLeaves);
    SerUtil::deserialize(in, nElements);
    SerUtil::deserialize(in, nFaces);
    SerUtil::deserialize(in, maxEdgeNeigh);
1031
1032
1033

    diam.deserialize(in);

1034
    SerUtil::deserialize(in, preserveCoarseDOFs);
1035
1036

    oldVal = nDOFEl;
1037
1038
    SerUtil::deserialize(in, nDOFEl);
    TEST_EXIT_DBG(oldVal == 0 || nDOFEl == oldVal)("Invalid nDOFEl!\n");
1039
1040
1041
1042

    nDOF.deserialize(in);

    oldVal = nNodeEl;
1043
1044
    SerUtil::deserialize(in, nNodeEl);
    TEST_EXIT_DBG(oldVal == 0 || nNodeEl == oldVal)("Invalid nNodeEl!\n");
1045
1046
1047

    node.deserialize(in);

1048
1049
1050

    // === Read admins. ===

1051
    int size;
1052
    SerUtil::deserialize(in, size);
1053
    admin.resize(size, NULL);
1054
    for (int i = 0; i < size; i++) {
1055
      if (!admin[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
1056
	admin[i] = new DOFAdmin(this);
1057

1058
1059
1060
      admin[i]->deserialize(in);
    }

1061
    SerUtil::deserialize(in, size);
Thomas Witkowski's avatar
Thomas Witkowski committed
1062
    std::vector< std::vector<int> > neighbourIndices(size);
1063

1064
    deleteMeshStructure();
1065
1066
1067
1068
1069
1070
1071
1072

    // 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;
1073

1074
    macroElements.resize(size);
1075
    for (int i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1076
      macroElements[i] = new MacroElement(dim);
1077
1078
      macroElements[i]->writeNeighboursTo(&(neighbourIndices[i]));
      macroElements[i]->deserialize(in);
1079
      elIndexVecIndex[macroElements[i]->getIndex()] = i;
1080
1081
    }

1082
1083
    SerUtil::deserialize(in, elementIndex);
    SerUtil::deserialize(in, initialized);
1084
1085

    // set neighbour pointer in macro elements
1086
    int nNeighbour = getGeo(NEIGH);
1087
    for (int i = 0; i < static_cast<int>(macroElements.size()); i++) {
1088
      for (int j = 0; j < nNeighbour; j++) {
1089
	int index = neighbourIndices[i][j];
1090
1091
1092
1093
1094
1095
1096

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

	  index = elIndexVecIndex[index];

1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
	  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);
1107
    while (elInfo) {
1108
1109
1110
1111
1112
      elInfo->getElement()->setMesh(this);
      elInfo = stack.traverseNext(elInfo);
    }

    serializedDOFs.clear();
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128

    
    /// === 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;      
    }
1129
1130
  }

1131

1132
1133
  void Mesh::initialize() 
  {
1134
1135
    FUNCNAME("Mesh::initialize()");

Thomas Witkowski's avatar
Thomas Witkowski committed
1136
1137
    std::string macroFilename("");
    std::string valueFilename("");
1138
    std::string periodicFilename("");
1139
1140
1141
1142
    int check = 1;

    GET_PARAMETER(0, name + "->macro file name",  &macroFilename);
    GET_PARAMETER(0, name + "->value file name",  &valueFilename);
1143
    GET_PARAMETER(0, name + "->periodic file", &periodicFilename);
1144
1145
1146
1147
    GET_PARAMETER(0, name + "->check", "%d", &check);
    GET_PARAMETER(0, name + "->preserve coarse dofs", "%d", &preserveCoarseDOFs);

    if (macroFilename.length()) {
1148
1149

#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
1150
1151
1152
1153
1154
      if (checkParallelMacroFile(macroFilename, periodicFilename, check)) {
	std::string newPeriodicFilename = "";
	if (periodicFilename != "")
	  newPeriodicFilename = periodicFilename + ".tmp";

1155
1156
	macroFileInfo = 
	  MacroReader::readMacro(macroFilename + ".tmp", this, 
1157
1158
				 newPeriodicFilename, check);
      } else {
1159
1160
	macroFileInfo = 
	  MacroReader::readMacro(macroFilename, this, periodicFilename, check);
1161
      }
1162
1163
1164
1165
1166
1167

#else
      // In sequentiel computations just read the macro file to the mesh.
      macroFileInfo = 
	MacroReader::readMacro(macroFilename, this, periodicFilename, check);
#endif
1168
1169
1170

      // If there is no value file which should be written, we can delete
      // the information of the macro file.
1171
1172
      if (!valueFilename.length())
  	clearMacroFileInfo();
1173

1174
1175
1176
1177
1178
    }

    initialized = true;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1179

1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
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
#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);
    DOFAdmin *localAdmin = new DOFAdmin(&testMesh, admin[0]->getName());
    localAdmin->setNumberOfDOFs(admin[0]->getNumberOfDOFs());
    testMesh.addDOFAdmin(localAdmin);
    
    MacroInfo *testMacroInfo = 
      MacroReader::readMacro(macroFilename, &testMesh, periodicFilename, check);


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


1325
1326
  bool Mesh::associated(DegreeOfFreedom dof1, DegreeOfFreedom dof2) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1327
1328
    std::map<BoundaryType, VertexVector*>::iterator it;
    std::map<BoundaryType, VertexVector*>::iterator end = periodicAssociations.end();
1329
    for (it = periodicAssociations.begin(); it != end; ++it)
1330
1331
      if ((*(it->second))[dof1] == dof2)
	return true;
1332

1333
1334
1335
    return false;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1336

1337
1338
  bool Mesh::indirectlyAssociated(DegreeOfFreedom dof1, DegreeOfFreedom dof2) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1339
1340
1341
    std::vector<DegreeOfFreedom> associatedToDOF1;
    std::map<BoundaryType, VertexVector*>::iterator it;
    std::map<BoundaryType, VertexVector*>::iterator end = periodicAssociations.end();
1342
1343
1344
    DegreeOfFreedom dof, assDOF;

    associatedToDOF1.push_back(dof1);
Thomas Witkowski's avatar
Thomas Witkowski committed
1345
1346
1347
    for (it = periodicAssociations.begin(); it != end; ++it) {
      int size = static_cast<int>(associatedToDOF1.size());
      for (int i = 0; i < size; i++) {
1348
1349
	dof = associatedToDOF1[i];
	assDOF = (*(it->second))[dof];
Thomas Witkowski's avatar
Thomas Witkowski committed
1350
	if (assDOF == dof2) {
1351
1352
	  return true;
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
1353
1354
	  if (assDOF != dof) 
	    associatedToDOF1.push_back(assDOF);
1355
1356
1357
	}
      }
    }
1358

1359
1360
1361
    return false;
  }

1362

1363
1364
  void Mesh::clearMacroFileInfo()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1365
    macroFileInfo->clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1366
    delete macroFileInfo;
1367
    macroFileInfo = NULL;
1368
  }
1369

1370

1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
  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();
1394
1395
	 it != dofCounter.end(); ++it)
      overall += it->second * 15;    
1396
1397
  }

1398

1399
1400
  int Mesh::calcMemoryUsage()
  {
1401
    int result = sizeof(Mesh);
1402

1403
1404
1405
1406
1407
1408
    result += nDOFEl;
    for (int i = 0; i < static_cast<int>(admin.size()); i++) {
      result += admin[i]->calcMemoryUsage();
      result += admin[i]->getUsedSize() * sizeof(DegreeOfFreedom);
    }
    
1409
1410
    for (int i = 0; i < static_cast<int>(macroElements.size()); i++)
      result += macroElements[i]->calcMemoryUsage();    
1411
1412
1413
    
    return result;
  }
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427


  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();
  }
1428
}
For faster browsing, not all history is shown. View entire blame