ProblemVec.cc 42 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1001
#pragma omp parallel
1002
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
1003
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
1004
1005
      BoundaryType *bound = 
	useGetBound ? GET_MEMORY(BoundaryType, basisFcts->getNumber()) : NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
1006
1007
1008
1009
1010

      // 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
1011
1012

      if (matrix) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1013
	tmpMatrix = new DOFMatrix(matrix->getRowFESpace(), matrix->getColFESpace(), "tmp");
Thomas Witkowski's avatar
Thomas Witkowski committed
1014
1015
1016
1017
1018
1019

	// 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();
1020
1021
1022
1023

	tmpMatrix->getBaseMatrix().change_dim(matrix->getRowFESpace()->getAdmin()->getUsedSize(),
					      matrix->getColFESpace()->getAdmin()->getUsedSize());
	tmpMatrix->startInsertion();
Thomas Witkowski's avatar
Thomas Witkowski committed
1024
1025
1026
      }

      if (vector) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1027
	tmpVector = new DOFVector<double>(vector->getFESpace(), "tmp");
Thomas Witkowski's avatar
Thomas Witkowski committed
1028
1029
1030
1031
1032
1033
1034
1035

	// 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);
      }

1036
1037
1038
1039
      // 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);

1040
1041
1042
1043
1044
1045
1046
      // 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
1047
      while (elInfo) {
1048
	if (useGetBound)
Thomas Witkowski's avatar
Thomas Witkowski committed
1049
1050
1051
1052
1053
1054
1055
	  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.
1056
1057
	  if (matrix->getBoundaryManager())
	    matrix->getBoundaryManager()->fillBoundaryConditions(elInfo, tmpMatrix);    
Thomas Witkowski's avatar
Thomas Witkowski committed
1058
1059
	}
	
1060
	if (vector)
Thomas Witkowski's avatar
Thomas Witkowski committed
1061
	  tmpVector->assemble(1.0, elInfo, bound, NULL);
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
#ifdef _OPENMP
#pragma omp barrier
#endif

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

1092
1093
1094
1095
1096
1097
1098
      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
1099
	delete tmpMatrix;
Thomas Witkowski's avatar
Thomas Witkowski committed
1100
1101
      }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
1108
	delete tmpVector;
Thomas Witkowski's avatar
Thomas Witkowski committed
1109
1110
      }

1111
      if (useGetBound)
Thomas Witkowski's avatar
Thomas Witkowski committed
1112
	FREE_MEMORY(bound, BoundaryType, basisFcts->getNumber());
Thomas Witkowski's avatar
Thomas Witkowski committed
1113
      
Thomas Witkowski's avatar
Thomas Witkowski committed
1114
    } // pragma omp parallel
Thomas Witkowski's avatar
Thomas Witkowski committed
1115

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
	basisFcts->getBound(rowElInfo, bound);
      
      if (matrix) {
	matrix->assemble(1.0, rowElInfo, colElInfo, smallElInfo, largeElInfo, bound);
	
1150
1151
	if (matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->fillBoundaryConditions(rowElInfo, matrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
1152
1153
      }
      
1154
      if (vector)
Thomas Witkowski's avatar
Thomas Witkowski committed
1155
1156
	vector->assemble(1.0, rowElInfo, bound);

Thomas Witkowski's avatar
Thomas Witkowski committed
1157
      cont = dualTraverse.traverseNext(&rowElInfo, &colElInfo, 
Thomas Witkowski's avatar
Thomas Witkowski committed
1158
1159
1160
				       &smallElInfo, &largeElInfo);
    }

1161
    if (useGetBound)
Thomas Witkowski's avatar
Thomas Witkowski committed
1162
1163
1164
      FREE_MEMORY(bound, BoundaryType, basisFcts->getNumber());
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176

  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;
1177
1178
    if (useGetBound)
      bound = GET_MEMORY(BoundaryType, basisFcts->getNumber());    
1179
1180
1181

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

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

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

1202
      cont = dualTraverse.traverseNext(&mainElInfo, &auxElInfo, 
Thomas Witkowski's avatar
Thomas Witkowski committed
1203
1204
1205
				       &smallElInfo, &largeElInfo);
    }
       
1206
1207
    if (useGetBound)
      FREE_MEMORY(bound, BoundaryType, basisFcts->getNumber());   
Thomas Witkowski's avatar
Thomas Witkowski committed
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
  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.
Thomas Witkowski's avatar
Thomas Witkowski committed
1234
1235
      DOFVector<double> *tmpRhsVec = new DOFVector<double>(rhs->getFESpace(), "tmpRhs");
      DOFVector<double> *tmpSolVec = new DOFVector<double>(solution->getFESpace(), "tmpSol");
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
      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) {	     
Thomas Witkowski's avatar
Thomas Witkowski committed
1267
1268
	  *rhsIt += *tmpRhsIt;
	  *solIt += *tmpSolIt;	  
1269
1270
1271
1272
	}
      } // pragma omp critical


Thomas Witkowski's avatar
Thomas Witkowski committed
1273
1274
      delete tmpRhsVec;
      delete tmpSolVec;
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
    } // 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
1287
  void ProblemVec::writeResidualMesh(int comp, AdaptInfo *adaptInfo, const std::string name)
1288
1289
1290
  {
    FUNCNAME("ProblemVec::writeResidualMesh()");

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

    ElementFileWriter fw(name, this->getFESpace(comp), vec);
1302
1303
1304
    fw.writeFiles(adaptInfo, true);    
  }

1305
  void ProblemVec::serialize(std::ostream &out) 
1306
1307
1308
  {
    FUNCNAME("ProblemVec::serialize()");

Thomas Witkowski's avatar
Thomas Witkowski committed
1309
    SerializerUtil::serializeBool(out, &allowFirstRef);
1310
    
Thomas Witkowski's avatar
Thomas Witkowski committed
1311
    for (int i = 0; i < static_cast<int>(meshes.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
1312
      meshes[i]->serialize(out);
1313

Thomas Witkowski's avatar
Thomas Witkowski committed
1314
    solution->serialize(out);
1315
1316
  }

1317
  void ProblemVec::deserialize(std::istream &in) 
1318
1319
1320
  {
    FUNCNAME("ProblemVec::deserialize()");

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

Thomas Witkowski's avatar
Thomas Witkowski committed
1323
    for (int i = 0; i < static_cast<int>(meshes.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
1324
      meshes[i]->deserialize(in);
1325

Thomas Witkowski's avatar
Thomas Witkowski committed
1326
    solution->deserialize(in);
1327
  }
Thomas Witkowski's avatar
Thomas Witkowski committed
1328
1329
1330
1331
1332
1333
1334
1335
1336

  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
Thomas Witkowski's avatar
Thomas Witkowski committed
1337
      DOFVector<double> *tmp = new DOFVector<double>(componentSpaces[i], "tmp");
Thomas Witkowski's avatar
Thomas Witkowski committed
1338
1339
      tmp->interpol(exactSolutionFcts[i]);
      double solMax = tmp->absMax();
Thomas Witkowski's avatar
Thomas Witkowski committed
1340
      *tmp -= *(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
1341
1342
1343
1344
1345
1346
1347
1348
1349
      
      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.
Thomas Witkowski's avatar
Thomas Witkowski committed
1350
      DOFVector<double> *sol = new DOFVector<double>(componentSpaces[i], "tmp");
Thomas Witkowski's avatar
Thomas Witkowski committed
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
      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;
Thomas Witkowski's avatar
Thomas Witkowski committed
1371
	for (int j = 0; j < componentSpaces[i]->getBasisFcts()->getNumber(); j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
1372
	  estimate += (*tmp)[locInd[j]];
Thomas Witkowski's avatar
Thomas Witkowski committed
1373

Thomas Witkowski's avatar
Thomas Witkowski committed
1374
1375
1376
1377
1378
1379
	elInfo->getElement()->setEstimation(estimate, i);
	elInfo->getElement()->setMark(0);
								
	elInfo = stack.traverseNext(elInfo);
      }  
      
Thomas Witkowski's avatar
Thomas Witkowski committed
1380
1381
      delete tmp;	
      delete sol;
Thomas Witkowski's avatar
Thomas Witkowski committed
1382
1383
    }						           
  }
1384
1385
}
 
For faster browsing, not all history is shown. View entire blame