ProblemVec.cc 41.7 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1001
1002
      BoundaryType *bound = 
	useGetBound ? GET_MEMORY(BoundaryType, basisFcts->getNumber()) : NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
1003
1004
1005
1006
1007

      // Create for every thread its private matrix and vector, on that
      // the thread will assemble its part of the mesh.
      DOFMatrix *tmpMatrix = NULL;
      DOFVector<double> *tmpVector = NULL; 
Thomas Witkowski's avatar
Thomas Witkowski committed
1008
1009

      if (matrix) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1010
	tmpMatrix = NEW DOFMatrix(matrix->getRowFESpace(), matrix->getColFESpace(), "tmp");
Thomas Witkowski's avatar
Thomas Witkowski committed
1011
1012
1013
1014
1015
1016

	// Copy the global matrix to the private matrix, because we need the
	// operators defined on the global matrix in the private one. Only the
	// values have to be set to zero.
	*tmpMatrix = *matrix;
	tmpMatrix->clear();
1017
1018
1019
1020

	tmpMatrix->getBaseMatrix().change_dim(matrix->getRowFESpace()->getAdmin()->getUsedSize(),
					      matrix->getColFESpace()->getAdmin()->getUsedSize());
	tmpMatrix->startInsertion();
Thomas Witkowski's avatar
Thomas Witkowski committed
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
      }

      if (vector) {
	tmpVector = NEW DOFVector<double>(vector->getFESpace(), "tmp");

	// Copy the global vector to the private vector, because we need the
	// operatirs defined on the global vector in the private one. But set
	// the values to zero of the private vector after copying.
	*tmpVector = *vector;
	tmpVector->set(0.0);
      }

1033
1034
1035
1036
      // Because we are using the parallel traverse stack, each thread will
      // traverse only a part of the mesh.
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, assembleFlag);

1037
1038
1039
1040
1041
1042
1043
      // After creating privat copies of the DOFMatrix and the DOFVector, all threads
      // have to wait at this barrier. Especially for small problems this is required,
      // because otherwise one thread may be finished with assembling, before another
      // has made his private copy.
#ifdef _OPENMP
#pragma omp barrier
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
1044
      while (elInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1045
	if (useGetBound) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1046
1047
1048
1049
1050
1051
1052
1053
1054
	  basisFcts->getBound(elInfo, bound);
	}
	
	if (matrix) {
	  tmpMatrix->assemble(1.0, elInfo, bound);
	  
	  // Take the matrix boundary manager from the public matrix,
	  // but assemble the boundary conditions on the thread private matrix.
	  if (matrix->getBoundaryManager()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1055
	    matrix->getBoundaryManager()->fillBoundaryConditions(elInfo, tmpMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
1056
1057
1058
1059
	  }		      
	}
	
	if (vector) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1060
	  tmpVector->assemble(1.0, elInfo, bound, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
1061
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
1062
	
Thomas Witkowski's avatar
Thomas Witkowski committed
1063
	elInfo = stack.traverseNext(elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
1064
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
1065

1066
1067
      tmpMatrix->finishInsertion();

Thomas Witkowski's avatar
Thomas Witkowski committed
1068
1069
1070
1071
1072
1073
      // After mesh traverse, all thread have to added their private matrices and
      // vectors to the global public matrix and public vector. Therefore, this is 
      // a critical section, which is allowed to be executed by on thread only at 
      // the same time.

      if (matrix) {
1074
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
1075
#pragma omp critical
1076
#endif
1077
1078
	matrix->getBaseMatrix() += tmpMatrix->getBaseMatrix();
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
1079

1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
#ifdef _OPENMP
#pragma omp barrier
#endif

#ifdef _OPENMP
#pragma omp master
#endif
      {
	if (matrix) {
	  matrix->startInsertion();
Thomas Witkowski's avatar
Thomas Witkowski committed
1090
	}
1091
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
1092

1093
1094
1095
1096
1097
1098
1099
      if (matrix) {
	// Remove rows corresponding to DOFs on a Dirichlet boundary.
#ifdef _OPENMP
#pragma omp critical
#endif
	matrix->removeRowsWithDBC(tmpMatrix->getApplyDBCs());
	      
Thomas Witkowski's avatar
Thomas Witkowski committed
1100
1101
1102
	DELETE tmpMatrix;
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
1103
      if (vector) {
1104
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
1105
#pragma omp critical
1106
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
1107
1108
1109
	*vector += *tmpVector;

	DELETE tmpVector;
Thomas Witkowski's avatar
Thomas Witkowski committed
1110
1111
      }

1112
      if (useGetBound)
Thomas Witkowski's avatar
Thomas Witkowski committed
1113
1114
1115
	FREE_MEMORY(bound, BoundaryType, basisFcts->getNumber());

    } // pragma omp parallel
Thomas Witkowski's avatar
Thomas Witkowski committed
1116
1117
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1118
1119
  void ProblemVec::assembleOnDifMeshes(FiniteElemSpace *rowFeSpace, 
				       FiniteElemSpace *colFeSpace,
Thomas Witkowski's avatar
Thomas Witkowski committed
1120
				       Flag assembleFlag,
Thomas Witkowski's avatar
Thomas Witkowski committed
1121
1122
				       DOFMatrix *matrix, 
				       DOFVector<double> *vector)
Thomas Witkowski's avatar
Thomas Witkowski committed
1123
1124
1125
  {
    const BasisFunction *basisFcts = rowFeSpace->getBasisFcts();
    BoundaryType *bound = NULL;
1126
    if (useGetBound)
Thomas Witkowski's avatar
Thomas Witkowski committed
1127
1128
      bound = GET_MEMORY(BoundaryType, basisFcts->getNumber());

1129
1130
1131
    if (matrix) 
      matrix->startInsertion();
    
Thomas Witkowski's avatar
Thomas Witkowski committed
1132
1133
1134
1135
    DualTraverse dualTraverse;
    ElInfo *rowElInfo, *colElInfo;
    ElInfo *largeElInfo, *smallElInfo;

1136
    dualTraverse.setFillSubElemMat(true, basisFcts);
Thomas Witkowski's avatar
Thomas Witkowski committed
1137
1138
1139
    bool cont = dualTraverse.traverseFirst(rowFeSpace->getMesh(), 
					   colFeSpace->getMesh(),
					   -1, -1,
Thomas Witkowski's avatar
Thomas Witkowski committed
1140
1141
1142
1143
					   assembleFlag, assembleFlag,
					   &rowElInfo, &colElInfo,
					   &smallElInfo, &largeElInfo);
    while (cont) {
1144
      if (useGetBound)
1145
1146
1147
1148
1149
1150
1151
1152
1153
	basisFcts->getBound(rowElInfo, bound);
      
      if (matrix) {
	matrix->assemble(1.0, rowElInfo, colElInfo, smallElInfo, largeElInfo, bound);
	
	if (matrix->getBoundaryManager()) {
	  matrix->getBoundaryManager()->
	    fillBoundaryConditions(rowElInfo, matrix);
	}		      
Thomas Witkowski's avatar
Thomas Witkowski committed
1154
1155
1156
1157
1158
1159
      }
      
      if (vector) {
	vector->assemble(1.0, rowElInfo, bound);
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
1160
      cont = dualTraverse.traverseNext(&rowElInfo, &colElInfo, 
Thomas Witkowski's avatar
Thomas Witkowski committed
1161
1162
1163
				       &smallElInfo, &largeElInfo);
    }

1164
    if (useGetBound)
Thomas Witkowski's avatar
Thomas Witkowski committed
1165
1166
1167
      FREE_MEMORY(bound, BoundaryType, basisFcts->getNumber());
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179

  void ProblemVec::assembleOnDifMeshes2(const FiniteElemSpace *mainFeSpace, 
					const FiniteElemSpace *auxFeSpace,
					Flag assembleFlag,
					DOFMatrix *matrix,
					DOFVector<double> *vector)
  {
    Mesh *mainMesh = mainFeSpace->getMesh();
    Mesh *auxMesh = auxFeSpace->getMesh();

    const BasisFunction *basisFcts = mainFeSpace->getBasisFcts();
    BoundaryType *bound = NULL;
1180
1181
    if (useGetBound)
      bound = GET_MEMORY(BoundaryType, basisFcts->getNumber());    
1182
1183
1184

    if (matrix) 
      matrix->startInsertion();
Thomas Witkowski's avatar
Thomas Witkowski committed
1185
1186
1187
1188
1189
    
    DualTraverse dualTraverse;
    ElInfo *mainElInfo, *auxElInfo;
    ElInfo *largeElInfo, *smallElInfo;

1190
    dualTraverse.setFillSubElemMat(true, basisFcts);
Thomas Witkowski's avatar
Thomas Witkowski committed
1191
1192
1193
1194
1195
    bool cont = dualTraverse.traverseFirst(mainMesh, auxMesh, -1, -1,
					   assembleFlag, assembleFlag,
					   &mainElInfo, &auxElInfo,
					   &smallElInfo, &largeElInfo);
    while (cont) {
1196
      if (useGetBound)
Thomas Witkowski's avatar
Thomas Witkowski committed
1197
1198
	basisFcts->getBound(mainElInfo, bound);

1199
      if (matrix)
Thomas Witkowski's avatar
Thomas Witkowski committed
1200
1201
	matrix->assemble2(1.0, mainElInfo, auxElInfo, smallElInfo, largeElInfo, bound);
      
1202
      if (vector)
Thomas Witkowski's avatar
Thomas Witkowski committed
1203
1204
	vector->assemble2(1.0, mainElInfo, auxElInfo, smallElInfo, largeElInfo, bound);

1205
      cont = dualTraverse.traverseNext(&mainElInfo, &auxElInfo, 
Thomas Witkowski's avatar
Thomas Witkowski committed
1206
1207
1208
				       &smallElInfo, &largeElInfo);
    }
       
1209
1210
    if (useGetBound)
      FREE_MEMORY(bound, BoundaryType, basisFcts->getNumber());   
Thomas Witkowski's avatar
Thomas Witkowski committed
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
  void ProblemVec::assembleBoundaryConditions(DOFVector<double> *rhs,
					      DOFVector<double> *solution,
					      Mesh *mesh,
					      Flag assembleFlag)
  {
    /* ================ Initialization of vectors ==================== */

    if (rhs->getBoundaryManager())
      rhs->getBoundaryManager()->initVector(rhs);      
    if (solution->getBoundaryManager())
      solution->getBoundaryManager()->initVector(solution);
    
#ifdef _OPENMP
    TraverseParallelStack stack;
#else
    TraverseStack stack;
#endif

    /* ================= Parallel Boundary Assemblage ================= */
#ifdef _OPENMP
#pragma omp parallel
#endif
    {
      // Each thread assembles on its own dof-vectors.
      DOFVector<double> *tmpRhsVec = NEW DOFVector<double>(rhs->getFESpace(), "tmpRhs");
      DOFVector<double> *tmpSolVec = NEW DOFVector<double>(solution->getFESpace(), "tmpSol");
      tmpRhsVec->set(0.0);
      tmpSolVec->set(0.0);


      // (Parallel) traverse of mesh.
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, assembleFlag);
      while (elInfo) {
	if (rhs->getBoundaryManager())
	  rhs->getBoundaryManager()-> fillBoundaryConditions(elInfo, tmpRhsVec);
	
	if (solution->getBoundaryManager())
	  solution->getBoundaryManager()->fillBoundaryConditions(elInfo, tmpSolVec);
      
	elInfo = stack.traverseNext(elInfo);
      }


      // After (parallel) mesh traverse, the result is applied to the final
      // vectors. This section is not allowed to be executed by more than one
      // thread at the same time.
#ifdef _OPENMP
#pragma omp critical
#endif
      {
	DOFVector<double>::Iterator rhsIt(rhs, USED_DOFS);
	DOFVector<double>::Iterator solIt(solution, USED_DOFS);
	DOFVector<double>::Iterator tmpRhsIt(tmpRhsVec, USED_DOFS);
	DOFVector<double>::Iterator tmpSolIt(tmpSolVec, USED_DOFS);
	for (rhsIt.reset(), solIt.reset(), tmpRhsIt.reset(), tmpSolIt.reset();
	     !rhsIt.end();
	     ++rhsIt, ++solIt, ++tmpRhsIt, ++tmpSolIt) {	     
	  if (*tmpRhsIt != 0.0)
	    *rhsIt = *tmpRhsIt;
	  
	  if (*tmpSolIt != 0.0)
	    *solIt = *tmpSolIt;	  
	}
      } // pragma omp critical


      DELETE tmpRhsVec;
      DELETE tmpSolVec;
    } // pragma omp parallel
     

    /* ======================= Finalize vectors ================== */

    if (rhs->getBoundaryManager())
      rhs->getBoundaryManager()->exitVector(rhs);
    if (solution->getBoundaryManager())
      solution->getBoundaryManager()->exitVector(solution);
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
1293
  void ProblemVec::writeResidualMesh(int comp, AdaptInfo *adaptInfo, const std::string name)
1294
1295
1296
  {
    FUNCNAME("ProblemVec::writeResidualMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
1297
    std::map<int, double> vec;    
1298
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
1299
1300
    ElInfo *elInfo = stack.traverseFirst(this->getMesh(comp),  -1, 
					 Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
1301
1302
    
    while (elInfo) {		  
Thomas Witkowski's avatar
Thomas Witkowski committed
1303
      vec[elInfo->getElement()->getIndex()] = elInfo->getElement()->getEstimation(comp);
1304
1305
      elInfo = stack.traverseNext(elInfo);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
1306
1307

    ElementFileWriter fw(name, this->getFESpace(comp), vec);
1308
1309
1310
    fw.writeFiles(adaptInfo, true);    
  }

1311
  void ProblemVec::serialize(std::ostream &out) 
1312
1313
1314
  {
    FUNCNAME("ProblemVec::serialize()");

Thomas Witkowski's avatar
Thomas Witkowski committed
1315
    SerializerUtil::serializeBool(out, &allowFirstRef);
1316
    
Thomas Witkowski's avatar
Thomas Witkowski committed
1317
1318
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
      meshes[i]->serialize(out);
1319
1320
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1321
    solution->serialize(out);
1322
1323
  }

1324
  void ProblemVec::deserialize(std::istream &in) 
1325
1326
1327
  {
    FUNCNAME("ProblemVec::deserialize()");

Thomas Witkowski's avatar
Thomas Witkowski committed
1328
    SerializerUtil::deserializeBool(in, &allowFirstRef);
1329

Thomas Witkowski's avatar
Thomas Witkowski committed
1330
1331
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
      meshes[i]->deserialize(in);
1332
1333
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1334
    solution->deserialize(in);
1335
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347

  void ProblemVec::computeError(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::computeError()");

    for (int i = 0; i < nComponents; i++) {		
      TEST_EXIT(exactSolutionFcts[i])("No solution function given!\n");

      // Compute the difference between exact and computed solution
      DOFVector<double> *tmp = NEW DOFVector<double>(componentSpaces[i], "tmp");
      tmp->interpol(exactSolutionFcts[i]);
      double solMax = tmp->absMax();
Thomas Witkowski's avatar
Thomas Witkowski committed
1348
      *tmp -= *(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
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
      
      MSG("L2    error = %.8e\n", tmp->L2Norm());
      MSG("L-inf error = %.8e\n", tmp->absMax() / solMax);
      
      adaptInfo->setEstSum(tmp->absMax() / solMax, i);
      adaptInfo->setEstMax(tmp->absMax() / solMax, i);
      
      // To set element estimates, compute a vector with the difference
      // between exact and computed solution for each DOF.
      DOFVector<double> *sol = NEW DOFVector<double>(componentSpaces[i], "tmp");
      sol->interpol(exactSolutionFcts[i]);
      DOFVector<double>::Iterator it1(sol, USED_DOFS);
      DOFVector<double>::Iterator it2(tmp, USED_DOFS);
      for (it1.reset(), it2.reset(); !it1.end(); ++it1, ++it2) {
	if ((abs(*it1) <= DBL_TOL) || (abs(*it2) <= DBL_TOL)) {
	  *it2 = 0.0;
	} else {
	  *it2 = abs(*it2 / *it1);
	}
      }

      // Compute estimate for every mesh element
      Vector<DegreeOfFreedom> locInd(componentSpaces[i]->getBasisFcts()->getNumber());
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(componentMeshes[i], -1, Mesh::CALL_LEAF_EL);
      while (elInfo) {
	componentSpaces[i]->getBasisFcts()->getLocalIndicesVec(elInfo->getElement(),
							       componentSpaces[i]->getAdmin(),
							       &locInd);
	double estimate = 0.0;
	for (int j = 0; j < componentSpaces[i]->getBasisFcts()->getNumber(); j++) {
	  estimate += (*tmp)[locInd[j]];
	}
	elInfo->getElement()->setEstimation(estimate, i);
	elInfo->getElement()->setMark(0);
								
	elInfo = stack.traverseNext(elInfo);
      }  
      
      DELETE tmp;	
      DELETE sol;
    }						           
  }
1392
1393
}
 
For faster browsing, not all history is shown. View entire blame