PetscSolverGlobalMatrix.cc 29.5 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1 2 3 4 5 6 7 8 9 10 11 12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include "AMDiS.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
14 15 16 17 18 19
#include "parallel/PetscSolverGlobalMatrix.h"
#include "parallel/StdMpi.h"
#include "parallel/MpiHelper.h"

namespace AMDiS {

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
20
  void PetscSolverGlobalMatrix::fillPetscMatrix(Matrix<DOFMatrix*> *seqMat)
Thomas Witkowski's avatar
Thomas Witkowski committed
21 22 23
  {
    FUNCNAME("PetscSolverGlobalMatrix::fillPetscMatrix()");

Thomas Witkowski's avatar
Thomas Witkowski committed
24
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
25
      updateSubdomainData();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
26
      fillPetscMatrixWithCoarseSpace(seqMat);
27 28 29
      return;
    }

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
30 31 32 33
    mat.resize(1);
    mat[0].resize(1);
    Mat &matIntInt = mat[0][0];

Thomas Witkowski's avatar
Thomas Witkowski committed
34
    TEST_EXIT_DBG(meshDistributor)("No mesh distributor object defined!\n");
35
    TEST_EXIT_DBG(interiorMap)("No parallel mapping object defined!\n");
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
36
    TEST_EXIT_DBG(seqMat)("No DOF matrix defined!\n");
37
    
Thomas Witkowski's avatar
Thomas Witkowski committed
38
    double wtime = MPI::Wtime();
39

40
    // === If required, recompute non zero structure of the matrix. ===
Thomas Witkowski's avatar
Thomas Witkowski committed
41

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
42 43
    if (checkMeshChange(seqMat))
      nnzInterior.create(seqMat, mpiCommGlobal, *interiorMap, 
44 45
			 &(meshDistributor->getPeriodicMap()),
			 meshDistributor->getElementObjectDb());
Thomas Witkowski's avatar
Thomas Witkowski committed
46

47 48
    // === Create PETSc vector (solution and a temporary vector). ===

49 50
    int nRankRows = interiorMap->getRankDofs();
    int nOverallRows = interiorMap->getOverallDofs();
51

52
    VecCreateMPI(mpiCommGlobal, nRankRows, nOverallRows, &petscSolVec);
53 54


Thomas Witkowski's avatar
Thomas Witkowski committed
55 56
    // === Create PETSc matrix with the computed nnz data structure. ===

57
    MatCreateAIJ(mpiCommGlobal, nRankRows, nRankRows, 
58
		 nOverallRows, nOverallRows,
59 60 61
		 0, nnzInterior.dnnz, 
		 0, nnzInterior.onnz, 
		 &matIntInt);
62 63

    MatSetOption(matIntInt, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
64

Thomas Witkowski's avatar
Thomas Witkowski committed
65 66 67 68 69 70
#if (DEBUG != 0)
    MSG("Fill petsc matrix 1 needed %.5f seconds\n", MPI::Wtime() - wtime);
#endif

#if (DEBUG != 0)
    int a, b;
71 72 73
    MatGetOwnershipRange(matIntInt, &a, &b);
    TEST_EXIT(a == interiorMap->getStartDofs())("Wrong matrix ownership range!\n");
    TEST_EXIT(b == interiorMap->getStartDofs() + nRankRows)
Thomas Witkowski's avatar
Thomas Witkowski committed
74 75 76 77 78 79
      ("Wrong matrix ownership range!\n");
#endif


    // === Transfer values from DOF matrices to the PETSc matrix. === 

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
80
    int nComponents = seqMat->getNumRows();
Thomas Witkowski's avatar
Thomas Witkowski committed
81 82
    for (int i = 0; i < nComponents; i++)
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
83 84
	if ((*seqMat)[i][j])
	  setDofMatrix((*seqMat)[i][j], i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
85 86 87 88 89

#if (DEBUG != 0)
    MSG("Fill petsc matrix 2 needed %.5f seconds\n", MPI::Wtime() - wtime);
#endif

90 91
    MatAssemblyBegin(matIntInt, MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(matIntInt, MAT_FINAL_ASSEMBLY);
Thomas Witkowski's avatar
Thomas Witkowski committed
92

93 94 95 96 97 98 99 100 101 102 103
    
    if (printMatInfo) {
      MatInfo matInfo;
      MatGetInfo(matIntInt, MAT_GLOBAL_SUM, &matInfo);
      MSG("Matrix info:\n");
      MSG("  memory usage: %e MB\n", matInfo.memory / (1024.0 * 1024.0));
      MSG("  mallocs: %d\n", static_cast<int>(matInfo.mallocs));
      MSG("  nz allocated: %d\n", static_cast<int>(matInfo.nz_allocated));
      MSG("  nz used: %d\n", static_cast<int>(matInfo.nz_used));
      MSG("  nz unneeded: %d\n", static_cast<int>(matInfo.nz_unneeded));
    }
104 105 106

    // === Remove Dirichlet BC DOFs. ===

107
    //    removeDirichletBcDofs(mat);
108 109
    

110
    // === Init PETSc solver. ===
111

112 113 114 115 116 117 118
    KSPCreate(mpiCommGlobal, &kspInterior);
    KSPGetPC(kspInterior, &pcInterior);
    KSPSetOperators(kspInterior, matIntInt, matIntInt, SAME_NONZERO_PATTERN); 
    KSPSetTolerances(kspInterior, 0.0, 1e-8, PETSC_DEFAULT, PETSC_DEFAULT);
    KSPSetType(kspInterior, KSPBCGS);
    KSPSetOptionsPrefix(kspInterior, kspPrefix.c_str());
    KSPSetFromOptions(kspInterior);
119

120
    initPreconditioner(pcInterior);
121

122 123
    // Do not delete the solution vector, use it for the initial guess.
    if (!zeroStartVector)
124
      KSPSetInitialGuessNonzero(kspInterior, PETSC_TRUE);
125

Thomas Witkowski's avatar
Thomas Witkowski committed
126 127 128
#if (DEBUG != 0)
    MSG("Fill petsc matrix 3 needed %.5f seconds\n", MPI::Wtime() - wtime);
#endif
129 130 131
  }


132 133 134 135 136 137 138 139
  void PetscSolverGlobalMatrix::fillPetscMatrix(DOFMatrix *mat)
  {
    Matrix<DOFMatrix*> m(1, 1);
    m[0][0] = mat;
    fillPetscMatrix(&m);
  }


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
140
  void PetscSolverGlobalMatrix::fillPetscMatrixWithCoarseSpace(Matrix<DOFMatrix*> *seqMat)
141 142
  {
    FUNCNAME("PetscSolverGlobalMatrix::fillPetscMatrixWithCoarseSpace()");
143 144

    TEST_EXIT_DBG(interiorMap)("Should not happen!\n");
145

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    vector<const FiniteElemSpace*> feSpaces = getFeSpaces(seqMat);

    vector<ParallelDofMapping*> uniqueCoarseMap;
    if (coarseSpaceMap.size()) {
      TEST_EXIT_DBG(coarseSpaceMap.size() == seqMat->getSize())
	("Wrong sizes %d %d\n", coarseSpaceMap.size(), seqMat->getSize());

      std::set<ParallelDofMapping*> tmp;
      for (map<int, ParallelDofMapping*>::iterator it = coarseSpaceMap.begin();
	   it != coarseSpaceMap.end(); ++it) {
	if (tmp.count(it->second) == 0) {	  
	  tmp.insert(it->second);
	  uniqueCoarseMap.push_back(it->second);
	}
      }
    }

    int nCoarseMap = uniqueCoarseMap.size();
    mat.resize(nCoarseMap + 1);
    for (int i = 0; i < nCoarseMap + 1; i++)
      mat[i].resize(nCoarseMap + 1);

    vector<int> componentIthCoarseMap(coarseSpaceMap.size());
    for (unsigned int i = 0; i < componentIthCoarseMap.size(); i++) {
      bool found = false;
      for (int j = 0; j < nCoarseMap; j++) {
	if (coarseSpaceMap[i] == uniqueCoarseMap[j]) {
	  componentIthCoarseMap[i] = j;
	  found = true;
	  break;
	}
      }

      TEST_EXIT_DBG(found)("Should not happen!\n");
    }


183 184 185 186

    int nRowsRankInterior = interiorMap->getRankDofs();
    int nRowsOverallInterior = interiorMap->getOverallDofs();

187 188 189
    // === If required, recompute non zero structure of the matrix. ===

    bool localMatrix = (subdomainLevel == 0);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
190 191
    if (checkMeshChange(seqMat, localMatrix)) {
      nnzInterior.create(seqMat, mpiCommGlobal, *interiorMap, NULL, 
192 193
			 meshDistributor->getElementObjectDb(),
			 localMatrix);
194

Thomas Witkowski's avatar
Thomas Witkowski committed
195
      if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
196 197
	MSG("NO COARSE SPACE NNZ!\n");
	/*
198 199 200
	nnzCoarse.create(mat, mpiCommGlobal, *coarseSpaceMap, NULL, meshDistributor->getElementObjectDb());
	nnzCoarseInt.create(mat, mpiCommGlobal, *coarseSpaceMap, *interiorMap, NULL, meshDistributor->getElementObjectDb());
	nnzIntCoarse.create(mat, mpiCommGlobal, *interiorMap, *coarseSpaceMap, NULL, meshDistributor->getElementObjectDb());
Thomas Witkowski's avatar
Thomas Witkowski committed
201
	*/
202
      }
203 204
    }

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
205 206
    mat.resize(nCoarseMap + 1);

207
    if (localMatrix) {
208
      MatCreateSeqAIJ(mpiCommLocal, nRowsRankInterior, nRowsRankInterior,
209
		      0, nnzInterior.dnnz, 
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
210
		      &mat[0][0]);
211
    } else {
212 213 214
      MatCreateAIJ(mpiCommLocal, 
		   nRowsRankInterior, nRowsRankInterior,
		   nRowsOverallInterior, nRowsOverallInterior,
215
		   0, nnzInterior.dnnz, 0, nnzInterior.onnz, 
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
216
		   &mat[0][0]);
217
    }
218

Thomas Witkowski's avatar
Thomas Witkowski committed
219
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
      for (int i = 0; i < nCoarseMap; i++) {
	ParallelDofMapping* cMap = uniqueCoarseMap[i];

	int nRowsRankCoarse = cMap->getRankDofs();
	int nRowsOverallCoarse = cMap->getOverallDofs();
	
	MatCreateAIJ(mpiCommGlobal,
		     nRowsRankCoarse, nRowsRankCoarse,
		     nRowsOverallCoarse, nRowsOverallCoarse,
		     0, PETSC_NULL, 0, PETSC_NULL, 
		     &mat[i + 1][i + 1]);

	MSG("REMOVE THIS LINE WHEN FINISHED!\n");
	MatSetOption(mat[i + 1][i + 1], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);

	for (int j = 0; j < nCoarseMap + 1; j++) {
	  int nRowsRankMat = (j == 0 ? nRowsRankInterior : uniqueCoarseMap[j - 1]->getRankDofs());
	  int nRowsOverallMat = (j == 0 ? nGlobalOverallInterior : uniqueCoarseMap[j - 1]->getOverallDofs());

	  MatCreateAIJ(mpiCommGlobal,
		       nRowsRankCoarse, nRowsRankMat,
		       nRowsOverallCoarse, nRowsOverallMat,
		       100, PETSC_NULL, 100, PETSC_NULL,
		       &mat[i + 1][j]);	  
	  MSG("REMOVE THIS LINE WHEN FINISHED!\n");
	  MatSetOption(mat[i + 1][j], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);

	  MatCreateAIJ(mpiCommGlobal,
		       nRowsRankMat, nRowsRankCoarse,
		       nRowsOverallMat, nRowsOverallCoarse,
		       0, PETSC_NULL, 0, PETSC_NULL,
		       &mat[j][i + 1]);
	  MSG("REMOVE THIS LINE WHEN FINISHED!\n");
	  MatSetOption(mat[j][i + 1], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
	}
	
	// MatCreateAIJ(mpiCommGlobal,
	// 	     nRowsRankCoarse, nRowsRankInterior,
	// 	     nRowsOverallCoarse, nGlobalOverallInterior,
	// 	     0, nnzCoarseInt.dnnz, 0, nnzCoarseInt.onnz,
	// 	     &matCoarseInt);
	
	// MatCreateAIJ(mpiCommGlobal,
	// 	     nRowsRankInterior, nRowsRankCoarse,
	// 	     nGlobalOverallInterior, nRowsOverallCoarse,
	// 	     0, nnzIntCoarse.dnnz, 0, nnzIntCoarse.onnz,
	// 	     &matIntCoarse);
      }
268 269
    }

270

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    // === Prepare traverse of sequentially created matrices. ===

    using mtl::tag::row; using mtl::tag::nz; using mtl::begin; using mtl::end;
    namespace traits = mtl::traits;
    typedef DOFMatrix::base_matrix_type Matrix;

    typedef traits::range_generator<row, Matrix>::type cursor_type;
    typedef traits::range_generator<nz, cursor_type>::type icursor_type;

    vector<int> cols, colsOther;
    vector<double> values, valuesOther;
    cols.reserve(300);
    colsOther.reserve(300);
    values.reserve(300);
    valuesOther.reserve(300);

    // === Traverse all sequentially created matrices and add the values to ===
    // === the global PETSc matrices.                                       ===

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
290
    int nComponents = seqMat->getSize();
291 292
    for (int i = 0; i < nComponents; i++) {
      for (int j = 0; j < nComponents; j++) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
293
	if (!(*seqMat)[i][j])
294 295
	  continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
296 297 298
	ParallelDofMapping *rowCoarseSpace = coarseSpaceMap[i];
	ParallelDofMapping *colCoarseSpace = coarseSpaceMap[j];

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
299 300
	traits::col<Matrix>::type col((*seqMat)[i][j]->getBaseMatrix());
	traits::const_value<Matrix>::type value((*seqMat)[i][j]->getBaseMatrix());
301 302
	
	// Traverse all rows.
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
303 304
	for (cursor_type cursor = begin<row>((*seqMat)[i][j]->getBaseMatrix()), 
	       cend = end<row>((*seqMat)[i][j]->getBaseMatrix()); cursor != cend; ++cursor) {
305

Thomas Witkowski's avatar
Thomas Witkowski committed
306
	  bool isRowCoarse = isCoarseSpace(i, feSpaces[i], *cursor);
307 308 309 310 311 312 313 314 315 316
  
	  cols.clear();
	  colsOther.clear();
	  values.clear();	  
	  valuesOther.clear();

	  // Traverse all columns.
	  for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor); 
	       icursor != icend; ++icursor) {

Thomas Witkowski's avatar
Thomas Witkowski committed
317
	    bool isColCoarse = isCoarseSpace(j, feSpaces[j], col(*icursor));
318

Thomas Witkowski's avatar
Thomas Witkowski committed
319 320
	    if (isColCoarse) {
	      if (isRowCoarse) {
321 322 323 324 325 326 327
		cols.push_back(col(*icursor));
		values.push_back(value(*icursor));
	      } else {
		colsOther.push_back(col(*icursor));
		valuesOther.push_back(value(*icursor));
	      }
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
328
	      if (isRowCoarse) {
329 330 331 332 333 334 335 336 337 338 339 340
		colsOther.push_back(col(*icursor));
		valuesOther.push_back(value(*icursor));
	      } else {
		cols.push_back(col(*icursor));
		values.push_back(value(*icursor));
	      }
	    }
	  }  // for each nnz in row


	  // === Set matrix values. ===

Thomas Witkowski's avatar
Thomas Witkowski committed
341 342
	  if (isRowCoarse) {
	    int rowIndex = rowCoarseSpace->getMatIndex(i, *cursor);
343
	    for (unsigned int k = 0; k < cols.size(); k++)
Thomas Witkowski's avatar
Thomas Witkowski committed
344
	      cols[k] = colCoarseSpace->getMatIndex(j, cols[k]);
345

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
346 347 348
	    // matCoarseCoarse
	    MatSetValues(mat[componentIthCoarseMap[i] + 1][componentIthCoarseMap[i] + 1], 
			 1, &rowIndex, cols.size(),
349 350 351 352
			 &(cols[0]), &(values[0]), ADD_VALUES);

	    if (colsOther.size()) {
	      if (subdomainLevel == 0) {
353
		for (unsigned int k = 0; k < colsOther.size(); k++)
354 355 356 357 358 359
		  colsOther[k] = interiorMap->getMatIndex(j, colsOther[k]);
	      } else {
		for (unsigned int k = 0; k < colsOther.size(); k++)
		  colsOther[k] = 
		    interiorMap->getMatIndex(j, colsOther[k]) + rStartInterior;
	      }
360

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
361 362 363
	      // matCoarseInt
	      MatSetValues(mat[componentIthCoarseMap[i] + 1][0],
			   1, &rowIndex, colsOther.size(),
364 365 366 367 368 369 370 371 372 373 374 375 376 377
 			   &(colsOther[0]), &(valuesOther[0]), ADD_VALUES);
	    }
	  } else {
	    int localRowIndex = 
	      (subdomainLevel == 0 ? interiorMap->getLocalMatIndex(i, *cursor) :
	       interiorMap->getMatIndex(i, *cursor));

	    for (unsigned int k = 0; k < cols.size(); k++) {
	      if (subdomainLevel == 0)
		cols[k] = interiorMap->getLocalMatIndex(j, cols[k]);
	      else
		cols[k] = interiorMap->getMatIndex(j, cols[k]);
	    }
	    
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
378 379
	    // matIntInt	    
  	    MatSetValues(mat[0][0], 1, &localRowIndex, cols.size(),
380 381 382 383 384 385 386 387 388
  			 &(cols[0]), &(values[0]), ADD_VALUES);

	    if (colsOther.size()) {
	      int globalRowIndex = interiorMap->getMatIndex(i, *cursor);

	      if (subdomainLevel != 0)
		globalRowIndex += rStartInterior;

	      for (unsigned int k = 0; k < colsOther.size(); k++)
Thomas Witkowski's avatar
Thomas Witkowski committed
389
		colsOther[k] = colCoarseSpace->getMatIndex(j, colsOther[k]);
390

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
391 392 393
	      // matIntCoarse
  	      MatSetValues(mat[0][componentIthCoarseMap[i] + 1], 
			   1, &globalRowIndex, colsOther.size(),
394 395 396 397 398 399 400
  			   &(colsOther[0]), &(valuesOther[0]), ADD_VALUES);
	    }
	  }
	} 
      }
    }

401

402 403
    // === Start global assembly procedure. ===

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
404 405 406 407 408
    for (int i = 0; i < nCoarseMap  + 1; i++) {
      for (int j = 0; j < nCoarseMap  + 1; j++) {
	MatAssemblyBegin(mat[i][j], MAT_FINAL_ASSEMBLY);
	MatAssemblyEnd(mat[i][j], MAT_FINAL_ASSEMBLY);
      }
409 410
    }

411

412 413
    // === Remove Dirichlet BC DOFs. ===

414
    //    removeDirichletBcDofs(mat);
415

416 417 418
    // === Create solver for the non primal (thus local) variables. ===

    KSPCreate(mpiCommLocal, &kspInterior);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
419
    KSPSetOperators(kspInterior, mat[0][0], mat[0][0], SAME_NONZERO_PATTERN);
420 421 422 423 424 425 426 427 428 429 430 431
    KSPSetOptionsPrefix(kspInterior, "interior_");
    KSPSetType(kspInterior, KSPPREONLY);
    KSPGetPC(kspInterior, &pcInterior);
    PCSetType(pcInterior, PCLU);
    if (subdomainLevel == 0)
      PCFactorSetMatSolverPackage(pcInterior, MATSOLVERUMFPACK);
    else
      PCFactorSetMatSolverPackage(pcInterior, MATSOLVERMUMPS);
    KSPSetFromOptions(kspInterior);  
  }


432 433 434
  void PetscSolverGlobalMatrix::fillPetscRhs(SystemVector *vec)
  {
    FUNCNAME("PetscSolverGlobalMatrix::fillPetscRhs()");
Thomas Witkowski's avatar
Thomas Witkowski committed
435

Thomas Witkowski's avatar
Thomas Witkowski committed
436 437 438 439
    VecCreateMPI(mpiCommGlobal, 
		 interiorMap->getRankDofs(), 
		 nGlobalOverallInterior,
		 &rhsInterior);
440

Thomas Witkowski's avatar
Thomas Witkowski committed
441
    if (coarseSpaceMap.size()) 
Thomas Witkowski's avatar
Thomas Witkowski committed
442
      VecCreateMPI(mpiCommGlobal, 
Thomas Witkowski's avatar
Thomas Witkowski committed
443 444
		   coarseSpaceMap[0]->getRankDofs(), 
		   coarseSpaceMap[0]->getOverallDofs(),
Thomas Witkowski's avatar
Thomas Witkowski committed
445 446
		   &rhsCoarseSpace);
    
447
    TEST_EXIT_DBG(vec)("No DOF vector defined!\n");
448
    TEST_EXIT_DBG(interiorMap)("No parallel DOF map defined!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
449
    
450

Thomas Witkowski's avatar
Thomas Witkowski committed
451
    // === Transfer values from DOF vector to the PETSc vector. === 
Thomas Witkowski's avatar
Thomas Witkowski committed
452
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
453 454
      for (int i = 0; i < vec->getSize(); i++)
	setDofVector(rhsInterior, rhsCoarseSpace, vec->getDOFVector(i), i);
Thomas Witkowski's avatar
Thomas Witkowski committed
455 456 457 458
    } else {
      for (int i = 0; i < vec->getSize(); i++)
	setDofVector(rhsInterior, vec->getDOFVector(i), i);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
459

460 461
    VecAssemblyBegin(rhsInterior);
    VecAssemblyEnd(rhsInterior);
462

Thomas Witkowski's avatar
Thomas Witkowski committed
463
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
464 465 466 467
      VecAssemblyBegin(rhsCoarseSpace);
      VecAssemblyEnd(rhsCoarseSpace);
    }

468 469 470

    // === Remove Dirichlet BC DOFs. ===
    //    removeDirichletBcDofs(vec);
Thomas Witkowski's avatar
Thomas Witkowski committed
471
 
Thomas Witkowski's avatar
Thomas Witkowski committed
472 473 474
  }


475 476
  void PetscSolverGlobalMatrix::solvePetscMatrix(SystemVector &vec, 
						 AdaptInfo *adaptInfo)
Thomas Witkowski's avatar
Thomas Witkowski committed
477 478 479 480 481 482 483 484 485 486
  {
    FUNCNAME("PetscSolverGlobalMatrix::solvePetscMatrix()");

    int nComponents = vec.getSize();

    // === Set old solution to be initiual guess for PETSc solver. ===
    if (!zeroStartVector) {
      VecSet(petscSolVec, 0.0);
      
      for (int i = 0; i < nComponents; i++)
487
	setDofVector(petscSolVec, vec.getDOFVector(i), i, true);
Thomas Witkowski's avatar
Thomas Witkowski committed
488 489 490 491 492
      
      VecAssemblyBegin(petscSolVec);
      VecAssemblyEnd(petscSolVec);
    }

493

Thomas Witkowski's avatar
Thomas Witkowski committed
494 495
    MatNullSpace matNullspace;
    Vec nullspaceBasis;
496 497 498
    if (nullspace.size() > 0 || 
	hasConstantNullspace ||
	constNullspaceComponent.size() > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
499
      TEST_EXIT_DBG(nullspace.size() <= 1)("Not yet implemented!\n");
500

501 502 503 504 505 506 507 508 509 510
      if (constNullspaceComponent.size() > 0) {
	nullspace.clear();
	SystemVector *basisVec = new SystemVector(vec);
	basisVec->set(0.0);
	for (unsigned int i = 0; i < constNullspaceComponent.size(); i++)
	  basisVec->getDOFVector(constNullspaceComponent[i])->set(1.0);

	nullspace.push_back(basisVec);
      } 

Thomas Witkowski's avatar
Thomas Witkowski committed
511 512 513 514 515 516 517
      if (nullspace.size() > 0) {
	VecDuplicate(petscSolVec, &nullspaceBasis);
	for (int i = 0; i < nComponents; i++)
	  setDofVector(nullspaceBasis, nullspace[0]->getDOFVector(i), i, true);
	
	VecAssemblyBegin(nullspaceBasis);
	VecAssemblyEnd(nullspaceBasis);
518 519

	VecNormalize(nullspaceBasis, PETSC_NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
520 521 522 523
	
	MatNullSpaceCreate(mpiCommGlobal, (hasConstantNullspace ? PETSC_TRUE : PETSC_FALSE), 
			   1, &nullspaceBasis, &matNullspace);

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
524
	MatMult(mat[0][0], nullspaceBasis, petscSolVec);
Thomas Witkowski's avatar
Thomas Witkowski committed
525 526 527 528 529 530
	PetscReal n;
	VecNorm(petscSolVec, NORM_2, &n);
	MSG("NORM IS: %e\n", n);
      } else {
	MatNullSpaceCreate(mpiCommGlobal, PETSC_TRUE, 0, PETSC_NULL, &matNullspace);
      }
531

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
532
      MatSetNullSpace(mat[0][0], matNullspace);
Thomas Witkowski's avatar
Thomas Witkowski committed
533
      KSPSetNullSpace(kspInterior, matNullspace);
534

Thomas Witkowski's avatar
Thomas Witkowski committed
535
      // === Remove null space, if requested. ===
536

Thomas Witkowski's avatar
Thomas Witkowski committed
537
      if (removeRhsNullspace) {
Thomas Witkowski's avatar
Thomas Witkowski committed
538
	TEST_EXIT_DBG(coarseSpaceMap.empty())("Not supported!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
539 540 541 542 543 544
	
	MatNullSpaceRemove(matNullspace, rhsInterior, PETSC_NULL);
      }
    } else {
      TEST_EXIT(removeRhsNullspace == false)
	("No nullspace provided that should be removed from rhs!\n");
545 546
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
547
    // PETSc.
548
    solve(rhsInterior, petscSolVec);
Thomas Witkowski's avatar
Thomas Witkowski committed
549

550 551

    if (nullspace.size() > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
552
      MatNullSpaceDestroy(&matNullspace);
553 554 555 556
      VecDestroy(&nullspaceBasis);
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
557 558
    // === Transfere values from PETSc's solution vectors to the DOF vectors. ===
    PetscScalar *vecPointer;
559
    VecGetArray(petscSolVec, &vecPointer);    
Thomas Witkowski's avatar
Thomas Witkowski committed
560

561
    int c = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
562
    for (int i = 0; i < nComponents; i++) {
563
      DOFVector<double> &dv = *(vec.getDOFVector(i));
564

565
      DofMap& d = (*interiorMap)[dv.getFeSpace()].getMap();
566 567 568
      for (DofMap::iterator it = d.begin(); it != d.end(); ++it)
	if (it->second.local != -1)
	  dv[it->first] = vecPointer[c++];
Thomas Witkowski's avatar
Thomas Witkowski committed
569 570 571
    }

    VecRestoreArray(petscSolVec, &vecPointer);
Thomas Witkowski's avatar
Thomas Witkowski committed
572 573 574 575

    // === Synchronize DOFs at common DOFs, i.e., DOFs that correspond to ===  
    // === more than one partition.                                       ===  
    meshDistributor->synchVector(vec);
576 577 578
  }


579 580 581 582
  void PetscSolverGlobalMatrix::solveGlobal(Vec &rhs, Vec &sol)
  {
    FUNCNAME("PetscSolverGlobalMatrix::solveGlobal()");

583 584 585
    double wtime = MPI::Wtime();
    double t0 = 0.0, t1 = 0.0;

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    Vec tmp;
    if (mpiCommLocal.Get_size() == 1)
      VecCreateSeq(mpiCommLocal, interiorMap->getRankDofs(), &tmp);
    else
      VecCreateMPI(mpiCommLocal,
		   interiorMap->getRankDofs(),
		   interiorMap->getOverallDofs(),
		   &tmp);

    PetscScalar *tmpValues, *rhsValues;
    VecGetArray(tmp, &tmpValues);
    VecGetArray(rhs, &rhsValues);

    for (int i = 0; i < interiorMap->getRankDofs(); i++)
      tmpValues[i] = rhsValues[i];

    VecRestoreArray(rhs, &rhsValues);
    VecRestoreArray(tmp, &tmpValues);

605 606 607
    t0 = MPI::Wtime() - wtime;

    wtime = MPI::Wtime();
608
    KSPSolve(kspInterior, tmp, tmp);
609 610 611
    t1 = MPI::Wtime() - wtime;

    wtime = MPI::Wtime();
612 613 614 615 616 617 618 619 620 621 622

    VecGetArray(tmp, &tmpValues);
    VecGetArray(sol, &rhsValues);

    for (int i = 0; i < interiorMap->getRankDofs(); i++) 
      rhsValues[i] = tmpValues[i];

    VecRestoreArray(sol, &rhsValues);
    VecRestoreArray(tmp, &tmpValues);

    VecDestroy(&tmp);
623 624
    t0 += MPI::Wtime() - wtime;

625
    //    MSG("TIMEING: %.5f %.5f\n", t0, t1);
626 627 628
  }


629 630 631 632
  void PetscSolverGlobalMatrix::destroyMatrixData()
  {
    FUNCNAME("PetscSolverGlobalMatrix::destroyMatrixData()");

633 634
    exitPreconditioner(pcInterior);

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
635 636 637
    for (unsigned int i = 0; i < mat.size(); i++)
      for (unsigned int j = 0; j < mat.size(); j++)
	MatDestroy(&mat[i][j]);
Thomas Witkowski's avatar
Thomas Witkowski committed
638

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
639
    KSPDestroy(&kspInterior);
640 641

    if (petscSolVec != PETSC_NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
642
      VecDestroy(&petscSolVec);
643 644
      petscSolVec = PETSC_NULL;
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
645 646 647
  }


648 649 650 651
  void PetscSolverGlobalMatrix::destroyVectorData()
  {
    FUNCNAME("PetscSolverGlobalMatrix::destroyVectorData()");

652
    VecDestroy(&rhsInterior);
653

Thomas Witkowski's avatar
Thomas Witkowski committed
654
    if (coarseSpaceMap.size())
655
      VecDestroy(&rhsCoarseSpace);
656 657 658
  }


659 660 661 662 663 664 665 666 667 668
  bool PetscSolverGlobalMatrix::checkMeshChange(Matrix<DOFMatrix*> *mat,
						bool localMatrix)
  {
    FUNCNAME("PetscSolverGlobalMatrix::checkMeshChange()");

    int recvAllValues = 0;
    int sendValue = 
      static_cast<int>(meshDistributor->getLastMeshChangeIndex() != lastMeshNnz);
    mpiCommGlobal.Allreduce(&sendValue, &recvAllValues, 1, MPI_INT, MPI_SUM);

669
    if (!nnzInterior.dnnz || recvAllValues != 0 || alwaysCreateNnzStructure) {
670 671
      vector<const FiniteElemSpace*> feSpaces = getFeSpaces(mat);
      
672
      interiorMap->setComputeMatIndex(!localMatrix);
673 674
      interiorMap->update(feSpaces);

675
      nnzInterior.clear();
676

Thomas Witkowski's avatar
Thomas Witkowski committed
677
      if (coarseSpaceMap.size()) {
678 679 680
	nnzCoarse.clear();
	nnzCoarseInt.clear();
	nnzIntCoarse.clear();
681 682 683 684 685 686 687 688 689 690 691 692
      }

      updateSubdomainData();
      lastMeshNnz = meshDistributor->getLastMeshChangeIndex();

      return true;
    }

    return false;
  }


693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
  void PetscSolverGlobalMatrix::createFieldSplit(PC pc)
  {
    FUNCNAME("PetscSolverGlobalMatrix::createFieldSplit()");

    vector<string> isNames;
    Parameters::get("parallel->solver->is blocks", isNames);

    int nBlocks = isNames.size();
    if (nBlocks == 0)
      return;

    for (int i = 0; i < nBlocks; i++) {
      MSG("Create for block %s\n", isNames[i].c_str());

      vector<int> blockComponents;
      Parameters::get("parallel->solver->is block " + lexical_cast<string>(i),
		      blockComponents);
      int nComponents = static_cast<int>(blockComponents.size());

      TEST_EXIT(nComponents > 0)("No is block for block %d defined!\n", i);

      // Check if blocks are continous
      for (int j = 0; j < nComponents; j++) {
	TEST_EXIT(blockComponents[j] == blockComponents[0] + j)
	  ("Does not yet support not continous IS blocks! Block %s\n", 
	   isNames[i].c_str());
      }

      IS is;
      interiorMap->createIndexSet(is, blockComponents[0], nComponents);
      PCFieldSplitSetIS(pc, isNames[i].c_str(), is);
      ISDestroy(&is);
    }
  }


729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
  void PetscSolverGlobalMatrix::initPreconditioner(PC pc)
  {
    FUNCNAME("PetscSolverGlobalMatrix::initPreconditioner()");

    PCSetFromOptions(pc);
    createFieldSplit(pc);
  }

 
  void PetscSolverGlobalMatrix::exitPreconditioner(PC pc)
  {
    FUNCNAME("PetscSolverGlobalMatrix::exitPreconditioner()");
  }


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
744
  void PetscSolverGlobalMatrix::setDofMatrix(DOFMatrix* seqMat,
745
					     int nRowMat, int nColMat)
Thomas Witkowski's avatar
Thomas Witkowski committed
746 747 748
  {
    FUNCNAME("PetscSolverGlobalMatrix::setDofMatrix()");

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
749
    TEST_EXIT(seqMat)("No DOFMatrix!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
750 751

    using mtl::tag::row; using mtl::tag::nz; using mtl::begin; using mtl::end;
752
    namespace traits = mtl::traits;
Thomas Witkowski's avatar
Thomas Witkowski committed
753 754
    typedef DOFMatrix::base_matrix_type Matrix;

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
755 756
    traits::col<Matrix>::type col(seqMat->getBaseMatrix());
    traits::const_value<Matrix>::type value(seqMat->getBaseMatrix());
Thomas Witkowski's avatar
Thomas Witkowski committed
757 758 759 760 761 762 763 764 765 766 767

    typedef traits::range_generator<row, Matrix>::type cursor_type;
    typedef traits::range_generator<nz, cursor_type>::type icursor_type;

    vector<int> cols;
    vector<double> values;
    cols.reserve(300);
    values.reserve(300);
    
    vector<int> globalCols;

768 769 770
    // Get periodic mapping object
    PeriodicMap &perMap = meshDistributor->getPeriodicMap();

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
771 772
    const FiniteElemSpace *rowFe = seqMat->getRowFeSpace();
    const FiniteElemSpace *colFe = seqMat->getColFeSpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
773 774 775 776
    DofMap& rowMap = (*interiorMap)[rowFe].getMap();
    DofMap& colMap = (*interiorMap)[colFe].getMap();

    // === Traverse all rows of the DOF matrix and insert row wise the values ===
Thomas Witkowski's avatar
Thomas Witkowski committed
777 778
    // === to the PETSc matrix.                                               ===

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
779 780
    for (cursor_type cursor = begin<row>(seqMat->getBaseMatrix()), 
	   cend = end<row>(seqMat->getBaseMatrix()); cursor != cend; ++cursor) {
Thomas Witkowski's avatar
Thomas Witkowski committed
781
      // Global index of the current row DOF.
Thomas Witkowski's avatar
Thomas Witkowski committed
782
      int globalRowDof = rowMap[*cursor].global;
783

Thomas Witkowski's avatar
Thomas Witkowski committed
784
      // Test if the current row DOF is a periodic DOF.
785
      bool periodicRow = perMap.isPeriodic(rowFe, globalRowDof);
786

Thomas Witkowski's avatar
Thomas Witkowski committed
787 788 789
      if (!periodicRow) {
	// === Row DOF index is not periodic. ===

790
	// Get PETSc's mat row index.
791
	int rowIndex = interiorMap->getMatIndex(nRowMat, globalRowDof);
Thomas Witkowski's avatar
Thomas Witkowski committed
792 793 794 795 796 797 798 799

	cols.clear();
	values.clear();

	for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor); 
	     icursor != icend; ++icursor) {

	  // Global index of the current column index.
Thomas Witkowski's avatar
Thomas Witkowski committed
800
	  int globalColDof = colMap[col(*icursor)].global;
Thomas Witkowski's avatar
Thomas Witkowski committed
801
	  // Test if the current col dof is a periodic dof.
802
	  bool periodicCol = perMap.isPeriodic(colFe, globalColDof);
803
	  // Get PETSc's mat col index.
804
	  int colIndex = interiorMap->getMatIndex(nColMat, globalColDof);
Thomas Witkowski's avatar
Thomas Witkowski committed
805 806 807 808 809 810 811

	  // Ignore all zero entries, expect it is a diagonal entry.
 	  if (value(*icursor) == 0.0 && rowIndex != colIndex)
 	    continue;

	  if (!periodicCol) {
	    // Calculate the exact position of the column index in the PETSc matrix.
Thomas Witkowski's avatar
Thomas Witkowski committed
812 813
 	    cols.push_back(colIndex);
 	    values.push_back(value(*icursor));
Thomas Witkowski's avatar
Thomas Witkowski committed
814 815 816 817 818
	  } else {
	    // === Row index is not periodic, but column index is. ===

	    // Create set of all periodic associations of the column index.
	    std::set<int> perAsc;
819 820 821
	    perMap.fillAssociations(colFe, globalColDof, 
				    meshDistributor->getElementObjectDb(), perAsc);

Thomas Witkowski's avatar
Thomas Witkowski committed
822 823 824 825 826 827 828 829 830
	    // Scale value to the number of periodic associations of the column index.
	    double scaledValue = 
	      value(*icursor) * pow(0.5, static_cast<double>(perAsc.size()));

	    
	    // === Create set of all matrix column indices due to the periodic ===
	    // === associations of the column DOF index.                       ===

	    vector<int> newCols;
831
	    perMap.mapDof(colFe, globalColDof, perAsc, newCols);
Thomas Witkowski's avatar
Thomas Witkowski committed
832
	    for (unsigned int i = 0; i < newCols.size(); i++) {
833
	      cols.push_back(interiorMap->getMatIndex(nColMat, newCols[i]));
Thomas Witkowski's avatar
Thomas Witkowski committed
834 835 836 837 838
	      values.push_back(scaledValue);	      
	    }
	  }
	}

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
839
  	MatSetValues(mat[0][0], 1, &rowIndex, cols.size(), 
Thomas Witkowski's avatar
Thomas Witkowski committed
840
  		     &(cols[0]), &(values[0]), ADD_VALUES);	
Thomas Witkowski's avatar
Thomas Witkowski committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854
      } else {
	// === Row DOF index is periodic. ===

	// Because this row is periodic, we will have to add the entries of this 
	// matrix row to multiple rows. The following maps store to each row an
	// array of column indices and values of the entries that must be added to
	// the PETSc matrix.
	map<int, vector<int> > colsMap;
	map<int, vector<double> > valsMap;

	// Traverse all column entries.
	for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor); 
	     icursor != icend; ++icursor) {
	  // Global index of the current column index.
855
	  int globalColDof = (*interiorMap)[colFe][col(*icursor)].global;
Thomas Witkowski's avatar
Thomas Witkowski committed
856 857 858 859 860 861 862 863 864

	  // Ignore all zero entries, expect it is a diagonal entry.
 	  if (value(*icursor) == 0.0 && globalRowDof != globalColDof)
 	    continue;

	  // === Add all periodic associations of both, the row and the column ===
	  // === indices to the set perAsc.                                    ===

	  std::set<int> perAsc;
865 866 867 868
	  perMap.fillAssociations(colFe, globalColDof, 
				  meshDistributor->getElementObjectDb(), perAsc);
	  perMap.fillAssociations(rowFe, globalRowDof, 
				  meshDistributor->getElementObjectDb(), perAsc);
Thomas Witkowski's avatar
Thomas Witkowski committed
869 870 871 872 873 874 875 876 877 878

	  // Scale the value with respect to the number of periodic associations.
	  double scaledValue = 
	    value(*icursor) * pow(0.5, static_cast<double>(perAsc.size()));


	  // === Create all matrix entries with respect to the periodic  ===
	  // === associations of the row and column indices.             ===

	  vector<pair<int, int> > entry;
Thomas Witkowski's avatar
Blbu  
Thomas Witkowski committed
879 880
	  perMap.mapDof(rowFe, colFe, make_pair(globalRowDof, globalColDof),
			perAsc, entry);
Thomas Witkowski's avatar
Thomas Witkowski committed
881 882 883

	  // === Translate the matrix entries to PETSc's matrix.

884
	  for (unsigned int i = 0; i < entry.size(); i++) {
885 886
	    int rowIdx = interiorMap->getMatIndex(nRowMat, entry[i].first);
	    int colIdx = interiorMap->getMatIndex(nColMat, entry[i].second);
Thomas Witkowski's avatar
Thomas Witkowski committed
887

888 889
	    colsMap[rowIdx].push_back(colIdx);
	    valsMap[rowIdx].push_back(scaledValue);
Thomas Witkowski's avatar
Thomas Witkowski committed
890 891 892 893 894 895 896 897 898 899 900 901
	  }
	}


	// === Finally, add all periodic rows to the PETSc matrix. ===

	for (map<int, vector<int> >::iterator rowIt = colsMap.begin();
	     rowIt != colsMap.end(); ++rowIt) {
	  TEST_EXIT_DBG(rowIt->second.size() == valsMap[rowIt->first].size())
	    ("Should not happen!\n");

	  int rowIndex = rowIt->first;
902

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
903
	  MatSetValues(mat[0][0], 1, &rowIndex, rowIt->second.size(),
Thomas Witkowski's avatar
Thomas Witkowski committed
904 905 906 907 908 909 910
		       &(rowIt->second[0]), &(valsMap[rowIt->first][0]), ADD_VALUES);
	}
      }
    }
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
911 912
  void PetscSolverGlobalMatrix::setDofVector(Vec vecInterior, 
					     Vec vecCoarse,
913
					     DOFVector<double>* vec, 
914
					     int nRowVec, 
915
					     bool rankOnly)
Thomas Witkowski's avatar
Thomas Witkowski committed
916 917 918
  {
    FUNCNAME("PetscSolverGlobalMatrix::setDofVector()");

919
    const FiniteElemSpace *feSpace = vec->getFeSpace();
920
    PeriodicMap &perMap = meshDistributor->getPeriodicMap();
Thomas Witkowski's avatar
Thomas Witkowski committed
921 922 923 924
    
    ParallelDofMapping *rowCoarseSpace = NULL;
    if (coarseSpaceMap.size())
      rowCoarseSpace = coarseSpaceMap[nRowVec];
925

Thomas Witkowski's avatar
Thomas Witkowski committed
926 927 928
    // Traverse all used DOFs in the dof vector.
    DOFVector<double>::Iterator dofIt(vec, USED_DOFS);
    for (dofIt.reset(); !dofIt.end(); ++dofIt) {
929

930
      if (rankOnly && !(*interiorMap)[feSpace].isRankDof(dofIt.getDOFIndex()))
Thomas Witkowski's avatar
Thomas Witkowski committed
931 932
	continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
933
      if (isCoarseSpace(nRowVec, feSpace, dofIt.getDOFIndex())) {
934 935
	TEST_EXIT_DBG(vecCoarse != PETSC_NULL)("Should not happen!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
936
	int index = rowCoarseSpace->getMatIndex(nRowVec, dofIt.getDOFIndex());
937 938 939 940 941 942 943 944 945 946 947 948 949 950
	VecSetValue(vecCoarse, index, *dofIt, ADD_VALUES);
      } else {
	// Calculate global row index of the DOF.
	DegreeOfFreedom globalRowDof = 
	  (*interiorMap)[feSpace][dofIt.getDOFIndex()].global;
	
	// Get PETSc's mat index of the row DOF.
	int index = 0;
	if (interiorMap->isMatIndexFromGlobal())
	  index = 
	    interiorMap->getMatIndex(nRowVec, globalRowDof) + rStartInterior;
	else
	  index =
	    interiorMap->getMatIndex(nRowVec, dofIt.getDOFIndex()) + rStartInterior;
951

952
	if (perMap.isPeriodic(feSpace, globalRowDof)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
953 954 955
	  std::set<int>& perAsc = perMap.getAssociations(feSpace, globalRowDof);
	  double value = *dofIt / (perAsc.size() + 1.0);
	  VecSetValue(vecInterior, index, value, ADD_VALUES);
956

Thomas Witkowski's avatar
Thomas Witkowski committed
957 958 959 960
	  for (std::set<int>::iterator perIt = perAsc.begin(); 
	       perIt != perAsc.end(); ++perIt) {
	    int mappedDof = perMap.map(feSpace, *perIt, globalRowDof);
	    int mappedIndex = interiorMap->getMatIndex(nRowVec, mappedDof);
961

Thomas Witkowski's avatar
Thomas Witkowski committed
962
	    VecSetValue(vecInterior, mappedIndex, value, ADD_VALUES);
963 964 965 966
	  }	  
	} else {	  
	  // The DOF index is not periodic.
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
967 968
	  VecSetValue(vecInterior, index, *dofIt, ADD_VALUES);
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
969 970 971 972 973
      }
    }
  }


974 975
  void PetscSolverGlobalMatrix::removeDirichletBcDofs(Matrix<DOFMatrix*> *mat)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
976
    FUNCNAME("PetscSolverGlobalMatrix::removeDirichletBcDofs()");    
977 978 979 980 981 982 983 984
  }


  void PetscSolverGlobalMatrix::removeDirichletBcDofs(SystemVector *vec)
  {
    FUNCNAME("PetscSolverGlobalMatrix::removeDirichletBcDofs()");
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
985
}