Mesh.cc 33.2 KB
Newer Older
1001
1002
1003
1004
1005
      admin[i]->serialize(out);

    // write macroElements
    size = static_cast<int>(macroElements.size());
    out.write(reinterpret_cast<const char*>(&size), sizeof(int));
1006
    for (int i = 0; i < size; i++)
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
      macroElements[i]->serialize(out);

    // write elementIndex
    out.write(reinterpret_cast<const char*>(&elementIndex), sizeof(int));

    // write initialized
    out.write(reinterpret_cast<const char*>(&initialized), sizeof(bool));

    serializedDOFs.clear();
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1018
  void Mesh::deserialize(std::istream &in)
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
  {
    serializedDOFs.clear();

    // read name
    in >> name;
    in.get();

    // read dim
    int oldVal = dim;
    in.read(reinterpret_cast<char*>(&dim), sizeof(int));
1029
    TEST_EXIT_DBG((oldVal == 0) || (dim == oldVal))("invalid dimension\n");
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057

    // read nVertices
    in.read(reinterpret_cast<char*>(&nVertices), sizeof(int));

    // read nEdges
    in.read(reinterpret_cast<char*>(&nEdges), sizeof(int));

    // read nLeaves
    in.read(reinterpret_cast<char*>(&nLeaves), sizeof(int));

    // read nElements
    in.read(reinterpret_cast<char*>(&nElements), sizeof(int));

    // read nFaces
    in.read(reinterpret_cast<char*>(&nFaces), sizeof(int));

    // read maxEdgeNeigh
    in.read(reinterpret_cast<char*>(&maxEdgeNeigh), sizeof(int));

    // diam
    diam.deserialize(in);

    // read preserveCoarseDOFs
    in.read(reinterpret_cast<char*>(&preserveCoarseDOFs), sizeof(bool));

    // read nDOFEl
    oldVal = nDOFEl;
    in.read(reinterpret_cast<char*>(&nDOFEl), sizeof(int));
1058
    TEST_EXIT_DBG((oldVal == 0) || (nDOFEl == oldVal))("invalid nDOFEl\n");
1059
1060
1061
1062
1063
1064
1065

    // read nDOF
    nDOF.deserialize(in);

    // read nNodeEl
    oldVal = nNodeEl;
    in.read(reinterpret_cast<char*>(&nNodeEl), sizeof(int));
1066
    TEST_EXIT_DBG((oldVal == 0) || (nNodeEl == oldVal))("invalid nNodeEl\n");
1067
1068
1069
1070
1071

    // read node
    node.deserialize(in);

    // read admins
1072
    int size;
1073
1074
    in.read(reinterpret_cast<char*>(&size), sizeof(int));
    admin.resize(size, NULL);
1075
    for (int i = 0; i < size; i++) {
1076
      if (!admin[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
1077
	admin[i] = new DOFAdmin(this);
1078

1079
1080
1081
1082
1083
1084
      admin[i]->deserialize(in);
    }

    // read macroElements
    in.read(reinterpret_cast<char*>(&size), sizeof(int));

Thomas Witkowski's avatar
Thomas Witkowski committed
1085
    std::vector< std::vector<int> > neighbourIndices(size);
1086

1087
    for (int i = 0; i < static_cast<int>(macroElements.size()); i++) {
1088
      if (macroElements[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
1089
	delete macroElements[i];
1090
    }
1091

1092
    macroElements.resize(size);
1093
    for (int i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1094
      macroElements[i] = new MacroElement(dim);
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
      macroElements[i]->writeNeighboursTo(&(neighbourIndices[i]));
      macroElements[i]->deserialize(in);
    }

    // read elementIndex
    in.read(reinterpret_cast<char*>(&elementIndex), sizeof(int));

    // read initialized
    in.read(reinterpret_cast<char*>(&initialized), sizeof(bool));

    // set neighbour pointer in macro elements
1106
1107
1108
    int neighs = getGeo(NEIGH);
    for (int i = 0; i < static_cast<int>(macroElements.size()); i++) {
      for (int j = 0; j < neighs; j++) {
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
	int index = neighbourIndices[i][j];
	if(index != -1) {
	  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);
1121
    while (elInfo) {
1122
1123
1124
1125
1126
1127
1128
1129
1130
      elInfo->getElement()->setMesh(this);
      elInfo = stack.traverseNext(elInfo);
    }

    serializedDOFs.clear();
  }

  void Mesh::initialize() 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1131
1132
1133
    std::string macroFilename("");
    std::string valueFilename("");
    std::string periodicFile("");
1134
1135
1136
1137
1138
1139
1140
1141
1142
    int check = 1;

    GET_PARAMETER(0, name + "->macro file name",  &macroFilename);
    GET_PARAMETER(0, name + "->value file name",  &valueFilename);
    GET_PARAMETER(0, name + "->periodic file", &periodicFile);
    GET_PARAMETER(0, name + "->check", "%d", &check);
    GET_PARAMETER(0, name + "->preserve coarse dofs", "%d", &preserveCoarseDOFs);

    if (macroFilename.length()) {
1143
1144
1145
      macroFileInfo = MacroReader::readMacro(macroFilename.c_str(), this,
					     periodicFile == "" ? NULL : periodicFile.c_str(),
					     check);
1146
1147
1148

      // If there is no value file which should be written, we can delete
      // the information of the macro file.
1149
      if (!valueFilename.length())
1150
1151
1152
1153
1154
1155
	clearMacroFileInfo();
    }

    initialized = true;
  }

1156
1157
  bool Mesh::associated(DegreeOfFreedom dof1, DegreeOfFreedom dof2) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1158
1159
    std::map<BoundaryType, VertexVector*>::iterator it;
    std::map<BoundaryType, VertexVector*>::iterator end = periodicAssociations.end();
1160
    for (it = periodicAssociations.begin(); it != end; ++it)
1161
1162
1163
1164
1165
      if ((*(it->second))[dof1] == dof2)
	return true;
    return false;
  }

1166
1167
  bool Mesh::indirectlyAssociated(DegreeOfFreedom dof1, DegreeOfFreedom dof2) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1168
1169
1170
    std::vector<DegreeOfFreedom> associatedToDOF1;
    std::map<BoundaryType, VertexVector*>::iterator it;
    std::map<BoundaryType, VertexVector*>::iterator end = periodicAssociations.end();
1171
1172
1173
    DegreeOfFreedom dof, assDOF;

    associatedToDOF1.push_back(dof1);
Thomas Witkowski's avatar
Thomas Witkowski committed
1174
1175
1176
    for (it = periodicAssociations.begin(); it != end; ++it) {
      int size = static_cast<int>(associatedToDOF1.size());
      for (int i = 0; i < size; i++) {
1177
1178
	dof = associatedToDOF1[i];
	assDOF = (*(it->second))[dof];
Thomas Witkowski's avatar
Thomas Witkowski committed
1179
	if (assDOF == dof2) {
1180
1181
	  return true;
	} else {
Thomas Witkowski's avatar
Thomas Witkowski committed
1182
1183
	  if (assDOF != dof) 
	    associatedToDOF1.push_back(assDOF);
1184
1185
1186
1187
1188
1189
1190
1191
	}
      }
    }
    return false;
  }

  void Mesh::clearMacroFileInfo()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1192
    macroFileInfo->clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
1193
    delete macroFileInfo;
1194
    macroFileInfo = NULL;
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
  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();
	 it != dofCounter.end(); ++it) {
      overall += it->second * 15;
    }
  }

1225
1226
  int Mesh::calcMemoryUsage()
  {
1227
    int result = sizeof(Mesh);
1228

1229
1230
1231
1232
1233
1234
    result += nDOFEl;
    for (int i = 0; i < static_cast<int>(admin.size()); i++) {
      result += admin[i]->calcMemoryUsage();
      result += admin[i]->getUsedSize() * sizeof(DegreeOfFreedom);
    }
    
1235
1236
    for (int i = 0; i < static_cast<int>(macroElements.size()); i++)
      result += macroElements[i]->calcMemoryUsage();    
1237
1238
1239
    
    return result;
  }
1240
}
For faster browsing, not all history is shown. View entire blame