Mesh.cc 38.7 KB
Newer Older
1001
1002
1003
1004
1005
1006

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

1007
1008
1009
    serializedDOFs.clear();
  }

1010

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

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

    in >> name;
    in.get();

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

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

    diam.deserialize(in);

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

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

    nDOF.deserialize(in);

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

    node.deserialize(in);

1047
1048
1049

    // === Read admins. ===

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

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

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

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

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

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

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

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

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

	  index = elIndexVecIndex[index];

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

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

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

1130

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

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

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

    if (macroFilename.length()) {
1147
1148

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

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

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

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

1173
1174
1175
1176
1177
    }

    initialized = true;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1178

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
#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


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

1332
1333
1334
    return false;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1335

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

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

1358
1359
1360
    return false;
  }

1361

1362
1363
  void Mesh::clearMacroFileInfo()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1364
    macroFileInfo->clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1365
    delete macroFileInfo;
1366
    macroFileInfo = NULL;
1367
  }
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
  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();
1393
1394
	 it != dofCounter.end(); ++it)
      overall += it->second * 15;    
1395
1396
  }

1397

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

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


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