PetscSolverGlobalMatrix.cc 28.7 KB
Newer Older
1 2 3 4 5 6 7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9 10 11 12 13 14 15 16 17
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
18
 *
19
 ******************************************************************************/
Thomas Witkowski's avatar
Thomas Witkowski committed
20

21
#include <mpi.h>
22
// #include "DirichletBC.h"
23
#include "DOFVector.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
24
#include "parallel/PetscSolverGlobalMatrix.h"
25
#include "parallel/PetscHelper.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
26 27
#include "parallel/StdMpi.h"
#include "parallel/MpiHelper.h"
28
#include "solver/PetscTypes.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
29

30 31
using namespace std;

32
namespace AMDiS { namespace Parallel {
33

34
  PetscSolverGlobalMatrix::PetscSolverGlobalMatrix(string name, bool setOptions)
35 36 37
    : PetscSolver(name),
      zeroStartVector(false),
      printMatInfo(false)
38
  { FUNCNAME_DBG("PetscSolverGlobalMatrix()");
39

40
    bool matSolverPackage = false;
41 42
    if (setOptions) {
      PetscParameters params;
43

44 45 46
      // set the solver
      std::string solverName = "petsc";
      Parameters::get(name, solverName);
47
      if (solverName == "petsc")
48
	Parameters::get(name + "->ksp_type", solverName);
49

50
      std::string kspSolver = params.solverMap[solverName];
51

52 53
      if (params.matSolverPackage.find(kspSolver) != params.matSolverPackage.end()) {
	// direct solvers
54 55 56
	petsc::options_insert_string(("-" + kspPrefix + "ksp_type preonly").c_str());
	petsc::options_insert_string(("-" + kspPrefix + "pc_type lu").c_str());
	petsc::options_insert_string(("-" + kspPrefix + "pc_factor_mat_solver_package " + kspSolver).c_str());
57 58 59
	setMaxIterations(1);
	zeroStartVector = true;
	matSolverPackage = true;
60
      } else if (params.emptyParam.find(kspSolver) == params.emptyParam.end() && solverName != "petsc") {
61
	// other solvers
62
	petsc::options_insert_string(("-" + kspPrefix + "ksp_type " + kspSolver).c_str());
63
      }
64

65 66 67 68 69
      // set the preconditioner
      string precon = "";
      Parameters::get(name + "->pc_type", precon);
      if (!precon.size())
	Parameters::get(name + "->left precon", precon);
70 71
      if (!precon.size())
	Parameters::get(name + "->right precon", precon);
72 73
      if (!matSolverPackage && params.emptyParam.find(precon) == params.emptyParam.end()) {
	precon = (params.preconMap.find(precon) != params.preconMap.end() ? params.preconMap[precon] : precon);
74
	petsc::options_insert_string(("-" + kspPrefix + "pc_type " + precon).c_str());
75
      }
76

77 78 79
      petsc::options_insert_string(("-" + kspPrefix + "ksp_max_it " + boost::lexical_cast<std::string>(getMaxIterations())).c_str());
      petsc::options_insert_string(("-" + kspPrefix + "ksp_rtol " + boost::lexical_cast<std::string>(getRelative())).c_str());
      petsc::options_insert_string(("-" + kspPrefix + "ksp_atol " + boost::lexical_cast<std::string>(getTolerance())).c_str());
80

81
      if (getInfo() >= 20)
82
	petsc::options_insert_string(("-" + kspPrefix + "ksp_monitor_true_residual").c_str());
83
      else if (getInfo() >= 10)
84
	petsc::options_insert_string(("-" + kspPrefix + "ksp_monitor").c_str());
85 86
    }
    if (!matSolverPackage) {
87
      Parameters::get(name + "->use zero start vector", zeroStartVector);
88
    }
89
    Parameters::get("parallel->print matrix info", printMatInfo);
90

91
#ifndef NDEBUG
92 93 94 95 96 97 98
    bool printOptionsInfo = false;
    Parameters::get("parallel->debug->print options info", printOptionsInfo);
    if (printOptionsInfo) {
    MSG("PetscOptionsView:\n");
    PetscViewer viewer;
    PetscViewerCreate(PETSC_COMM_WORLD, &viewer);
    PetscViewerSetType(viewer, PETSCVIEWERASCII);
99
    petsc::options_view(viewer);
100
    PetscViewerDestroy(&viewer);
101

102 103
    }
#endif
104 105 106
  }


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
107
  void PetscSolverGlobalMatrix::fillPetscMatrix(Matrix<DOFMatrix*> *seqMat)
Thomas Witkowski's avatar
Thomas Witkowski committed
108 109 110
  {
    FUNCNAME("PetscSolverGlobalMatrix::fillPetscMatrix()");

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
111 112 113
    TEST_EXIT_DBG(meshDistributor)("No mesh distributor object defined!\n");
    TEST_EXIT_DBG(interiorMap)("No parallel mapping object defined!\n");
    TEST_EXIT_DBG(seqMat)("No DOF matrix defined!\n");
114

115
#ifndef NDEBUG
116
    Timer t;
117
#endif
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
118

119
    createMatVec(*seqMat);
120

Thomas Witkowski's avatar
Thomas Witkowski committed
121
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
122
      fillPetscMatrixWithCoarseSpace(seqMat);
123 124
      return;
    }
125

126 127
    // === Create PETSc vector (solution and a temporary vector). ===

128
#ifndef NDEBUG
129 130
    MSG("Fill petsc matrix 1 needed %.5f seconds\n", t.elapsed());
    t.reset();
Thomas Witkowski's avatar
Thomas Witkowski committed
131 132
#endif

133
    // === Transfer values from DOF matrices to the PETSc matrix. ===
Thomas Witkowski's avatar
Thomas Witkowski committed
134

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
135
    int nComponents = seqMat->getNumRows();
Thomas Witkowski's avatar
Thomas Witkowski committed
136 137
    for (int i = 0; i < nComponents; i++)
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
138 139
	if ((*seqMat)[i][j])
	  setDofMatrix((*seqMat)[i][j], i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
140

141
#ifndef NDEBUG
142 143
    MSG("Fill petsc matrix 2 needed %.5f seconds\n", t.elapsed());
    t.reset();
Thomas Witkowski's avatar
Thomas Witkowski committed
144 145
#endif

146
    matAssembly();
147

148 149
    if (printMatInfo) {
      MatInfo matInfo;
150
      MatGetInfo(getMatInterior(), MAT_GLOBAL_SUM, &matInfo);
151 152 153 154 155 156 157
      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));
    }
158 159


160
    // === Init PETSc solver and preconditioner objects. ===
161

162 163
    initSolver(kspInterior);
    KSPGetPC(kspInterior, &pcInterior);
Praetorius, Simon's avatar
Praetorius, Simon committed
164
    initPreconditioner(*seqMat, mat[0][0]);
165

166

167
#ifndef NDEBUG
168
    MSG("Fill petsc matrix 3 needed %.5f seconds\n", t.elapsed());
Thomas Witkowski's avatar
Thomas Witkowski committed
169
#endif
170 171


172 173 174 175 176 177 178 179 180 181 182
    // === For debugging allow to write the matrix to a file. ===

    bool dbgWriteMatrix = false;
    Parameters::get("parallel->debug->write matrix", dbgWriteMatrix);
    if (dbgWriteMatrix) {
      PetscViewer matView;
      PetscViewerBinaryOpen(PETSC_COMM_WORLD, "mpi.mat",
			    FILE_MODE_WRITE, &matView);
      MatView(getMatInterior(), matView);
      PetscViewerDestroy(&matView);
    }
183 184 185
  }


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
186
  void PetscSolverGlobalMatrix::fillPetscMatrixWithCoarseSpace(Matrix<DOFMatrix*> *seqMat)
187
  {
188
    FUNCNAME_DBG("PetscSolverGlobalMatrix::fillPetscMatrixWithCoarseSpace()");
189

190
    TEST_EXIT_DBG(interiorMap)("No interiorMap! Should not happen!\n");
191
    TEST_EXIT_DBG(coarseSpaceMap.size() == (size_t)seqMat->getSize())
192
      ("Wrong sizes %d %d\n", coarseSpaceMap.size(), seqMat->getSize());
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    // === 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);

210
    bool localMatrix =
211 212
      (meshDistributor->getMeshLevelData().getMpiComm(meshLevel) == MPI::COMM_SELF);

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

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
216
    int nComponents = seqMat->getSize();
Thomas Witkowski's avatar
Thomas Witkowski committed
217 218 219 220 221
    for (int rowComponent = 0; rowComponent < nComponents; rowComponent++) {
      for (int colComponent = 0; colComponent < nComponents; colComponent++) {
	DOFMatrix* dofMat = (*seqMat)[rowComponent][colComponent];

	if (!dofMat)
222 223
	  continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
224 225
	ParallelDofMapping *rowCoarseSpace = coarseSpaceMap[rowComponent];
	ParallelDofMapping *colCoarseSpace = coarseSpaceMap[colComponent];
Thomas Witkowski's avatar
Thomas Witkowski committed
226

Thomas Witkowski's avatar
Thomas Witkowski committed
227 228
	std::set<DegreeOfFreedom> &dirichletRows = dofMat->getDirichletRows();

Thomas Witkowski's avatar
Thomas Witkowski committed
229 230
	traits::col<Matrix>::type col(dofMat->getBaseMatrix());
	traits::const_value<Matrix>::type value(dofMat->getBaseMatrix());
231

232
	// Traverse all rows.
233
	for (cursor_type cursor = begin<row>(dofMat->getBaseMatrix()),
Thomas Witkowski's avatar
Thomas Witkowski committed
234
	       cend = end<row>(dofMat->getBaseMatrix()); cursor != cend; ++cursor) {
235

236
	  bool isRowCoarse = isCoarseSpace(rowComponent, cursor.value());
Thomas Witkowski's avatar
Thomas Witkowski committed
237

238
	  // For the case, this is a dirichlet row we have to check whether the
Thomas Witkowski's avatar
Thomas Witkowski committed
239
	  // rank is also owner of this row DOF.
240 241 242
	  if (dirichletRows.count(cursor.value())) {
	    if ((!isRowCoarse && !(*interiorMap)[rowComponent].isRankDof(cursor.value())) ||
		(isRowCoarse && !(*rowCoarseSpace)[rowComponent].isRankDof(cursor.value())))
243
	      continue;
Thomas Witkowski's avatar
Thomas Witkowski committed
244
	  }
245

246 247
	  cols.clear();
	  colsOther.clear();
248
	  values.clear();
249 250 251
	  valuesOther.clear();

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

Thomas Witkowski's avatar
Thomas Witkowski committed
255
	    bool isColCoarse = isCoarseSpace(colComponent, col(*icursor));
256

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
257
	    if (isColCoarse == false)
258
	      if ((*interiorMap)[colComponent].isSet(col(*icursor)) == false)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
259 260
		continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
261 262 263
	    if (isColCoarse == isRowCoarse) {
	      cols.push_back(col(*icursor));
	      values.push_back(value(*icursor));
264
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
265 266
	      colsOther.push_back(col(*icursor));
	      valuesOther.push_back(value(*icursor));
267 268 269 270 271 272
	    }
	  }  // for each nnz in row


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

Thomas Witkowski's avatar
Thomas Witkowski committed
273
	  if (isRowCoarse) {
Thomas Witkowski's avatar
Thomas Witkowski committed
274 275
	    for (unsigned int i = 0; i < cols.size(); i++)
	      cols[i] = colCoarseSpace->getMatIndex(colComponent, cols[i]);
276

277
	    int rowIndex = rowCoarseSpace->getMatIndex(rowComponent, cursor.value());
Thomas Witkowski's avatar
Thomas Witkowski committed
278
	    MatSetValues(getMatCoarseByComponent(rowComponent, colComponent),
279 280
	    		 1, &rowIndex, cols.size(),
	    		 &(cols[0]), &(values[0]), ADD_VALUES);
281 282

	    if (colsOther.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
283
	      for (unsigned int i = 0; i < colsOther.size(); i++)
284 285 286
		colsOther[i] =
		  interiorMap->getMatIndex(colComponent, colsOther[i]) +
		  rStartInterior;
287

288
	      MatSetValues(getMatCoarseInteriorByComponent(rowComponent),
289 290
	       		   1, &rowIndex, colsOther.size(),
 	       		   &(colsOther[0]), &(valuesOther[0]), ADD_VALUES);
291 292
	    }
	  } else {
293
	    if ((*interiorMap)[rowComponent].isSet(cursor.value()) == false)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
294 295
	      continue;

296 297
	    int localRowIndex =
	      (localMatrix ?
298 299
	       interiorMap->getLocalMatIndex(rowComponent, cursor.value()) :
	       interiorMap->getMatIndex(rowComponent, cursor.value()));
300

Thomas Witkowski's avatar
Thomas Witkowski committed
301
	    for (unsigned int i = 0; i < cols.size(); i++) {
302
	      if (localMatrix)
Thomas Witkowski's avatar
Thomas Witkowski committed
303
		cols[i] = interiorMap->getLocalMatIndex(colComponent, cols[i]);
304
	      else
Thomas Witkowski's avatar
Thomas Witkowski committed
305
		cols[i] = interiorMap->getMatIndex(colComponent, cols[i]);
306
	    }
307

308 309
	    MatSetValues(getMatInterior(), 1, &localRowIndex, cols.size(),
  	     		 &(cols[0]), &(values[0]), ADD_VALUES);
310

311
	    if (colsOther.size()) {
312
	      int globalRowIndex =
313
		interiorMap->getMatIndex(rowComponent, cursor.value()) + rStartInterior;
314

Thomas Witkowski's avatar
Thomas Witkowski committed
315
	      for (unsigned int i = 0; i < colsOther.size(); i++)
316
		colsOther[i] =
Thomas Witkowski's avatar
Thomas Witkowski committed
317
		  colCoarseSpace->getMatIndex(colComponent, colsOther[i]);
318

319
	      MatSetValues(getMatInteriorCoarseByComponent(colComponent),
320 321
			   1, &globalRowIndex, colsOther.size(),
			   &(colsOther[0]), &(valuesOther[0]), ADD_VALUES);
322 323
	    }
	  }
324
	}
325 326 327
      }
    }

328
    matAssembly();
329

330 331
    // === Create solver for the non primal (thus local) variables. ===

332
    KSPCreate(domainComm, &kspInterior);
333
    petsc::ksp_set_operators(kspInterior, getMatInterior(), getMatInterior());
334 335 336
    KSPSetOptionsPrefix(kspInterior, "interior_");
    KSPSetType(kspInterior, KSPPREONLY);
    KSPGetPC(kspInterior, &pcInterior);
337 338
    if (isSymmetric) {
      PCSetType(pcInterior, PCCHOLESKY);
339
      PCFactorSetMatSolverPackage(pcInterior, MATSOLVERMUMPS);
340 341
    } else {
      PCSetType(pcInterior, PCLU);
342
      if (localMatrix)
343 344 345 346
	PCFactorSetMatSolverPackage(pcInterior, MATSOLVERUMFPACK);
      else
	PCFactorSetMatSolverPackage(pcInterior, MATSOLVERMUMPS);
    }
347
    KSPSetFromOptions(kspInterior);
348 349 350
  }


351 352
  void PetscSolverGlobalMatrix::fillPetscRhs(SystemVector *vec)
  {
353
    FUNCNAME_DBG("PetscSolverGlobalMatrix::fillPetscRhs()");
Thomas Witkowski's avatar
Thomas Witkowski committed
354

355
    TEST_EXIT_DBG(vec)("No DOF vector defined!\n");
356
    TEST_EXIT_DBG(interiorMap)("No parallel DOF map defined!\n");
357 358

    // === Transfer values from DOF vector to the PETSc vector. ===
Thomas Witkowski's avatar
Thomas Witkowski committed
359
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
360
      for (int i = 0; i < vec->getSize(); i++)
361
	setDofVector(getVecRhsInterior(),
Thomas Witkowski's avatar
Thomas Witkowski committed
362
		     getVecRhsCoarseByComponent(i), vec->getDOFVector(i), i);
Thomas Witkowski's avatar
Thomas Witkowski committed
363 364
    } else {
      for (int i = 0; i < vec->getSize(); i++)
365
	setDofVector(getVecRhsInterior(), vec->getDOFVector(i), i);
Thomas Witkowski's avatar
Thomas Witkowski committed
366 367
    }

368
    vecRhsAssembly();
369 370 371 372 373 374 375 376 377 378 379 380

    // === For debugging allow to write the rhs vector to a file. ===

    bool dbgWriteRhs = false;
    Parameters::get("parallel->debug->write rhs", dbgWriteRhs);
    if (dbgWriteRhs) {
      PetscViewer vecView;
      PetscViewerBinaryOpen(PETSC_COMM_WORLD, "mpi.vec",
			    FILE_MODE_WRITE, &vecView);
      VecView(getVecRhsInterior(), vecView);
      PetscViewerDestroy(&vecView);
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
381 382 383
  }


384 385 386 387 388
  /// 1.) set startsolution
  /// 2.) create null-space
  /// 3.) solve Ax=b
  /// 4.) destroy null-space
  /// 5.) transfer solution back to DOFVector
389
  void PetscSolverGlobalMatrix::solvePetscMatrix(SystemVector &vec,
390
						 AdaptInfo *adaptInfo)
Thomas Witkowski's avatar
Thomas Witkowski committed
391 392 393 394 395 396 397
  {
    FUNCNAME("PetscSolverGlobalMatrix::solvePetscMatrix()");

    int nComponents = vec.getSize();

    // === Set old solution to be initiual guess for PETSc solver. ===
    if (!zeroStartVector) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
398 399
      TEST_EXIT(coarseSpaceMap.size() == 0)("Not yet supported!\n");

400
      VecSet(getVecSolInterior(), 0.0);
401

Thomas Witkowski's avatar
Thomas Witkowski committed
402
      for (int i = 0; i < nComponents; i++)
403
	setDofVector(getVecSolInterior(), vec.getDOFVector(i), i, true);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
404

405
      vecSolAssembly();
Thomas Witkowski's avatar
Thomas Witkowski committed
406 407
    }

408

Thomas Witkowski's avatar
Thomas Witkowski committed
409 410
    MatNullSpace matNullspace;
    Vec nullspaceBasis;
411
    SystemVector *basisVec;
412
    if (nullspace.size() > 0 ||
413 414
	hasConstantNullspace ||
	constNullspaceComponent.size() > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
415
      TEST_EXIT_DBG(nullspace.size() <= 1)("Not yet implemented!\n");
416

417 418
      if (constNullspaceComponent.size() > 0) {
	nullspace.clear();
419
        basisVec = new SystemVector(vec);
420 421 422 423 424
	basisVec->set(0.0);
	for (unsigned int i = 0; i < constNullspaceComponent.size(); i++)
	  basisVec->getDOFVector(constNullspaceComponent[i])->set(1.0);

	nullspace.push_back(basisVec);
425
      }
426

Thomas Witkowski's avatar
Thomas Witkowski committed
427
      if (nullspace.size() > 0) {
428
	VecDuplicate(getVecSolInterior(), &nullspaceBasis);
429
	setDofVector(nullspaceBasis, *(nullspace[0]), true);
430

Thomas Witkowski's avatar
Thomas Witkowski committed
431 432
	VecAssemblyBegin(nullspaceBasis);
	VecAssemblyEnd(nullspaceBasis);
433 434

	VecNormalize(nullspaceBasis, PETSC_NULL);
435 436

	MatNullSpaceCreate(domainComm, (hasConstantNullspace ? PETSC_TRUE : PETSC_FALSE),
Thomas Witkowski's avatar
Thomas Witkowski committed
437 438
			   1, &nullspaceBasis, &matNullspace);

439
	MatMult(getMatInterior(), nullspaceBasis, getVecSolInterior());
Thomas Witkowski's avatar
Thomas Witkowski committed
440
	PetscReal n;
441
	VecNorm(getVecSolInterior(), NORM_2, &n);
Thomas Witkowski's avatar
Thomas Witkowski committed
442 443
	MSG("NORM IS: %e\n", n);
      } else {
444
	MatNullSpaceCreate(domainComm, PETSC_TRUE, 0, PETSC_NULL, &matNullspace);
Thomas Witkowski's avatar
Thomas Witkowski committed
445
      }
446

Thomas Witkowski's avatar
Thomas Witkowski committed
447 448 449
      MSG("NULLSPACE IS NOT REMOVED!\n");
      // MatSetNullSpace(getMatInterior(), matNullspace);
      // KSPSetNullSpace(kspInterior, matNullspace);
450

Thomas Witkowski's avatar
Thomas Witkowski committed
451
      // === Remove null space, if requested. ===
452

Thomas Witkowski's avatar
Thomas Witkowski committed
453
      if (removeRhsNullspace) {
Thomas Witkowski's avatar
Thomas Witkowski committed
454
	TEST_EXIT_DBG(coarseSpaceMap.empty())("Not supported!\n");
455 456

	MSG("Remove nullspace from rhs vector.\n");
457
	petsc::mat_nullspace_remove(matNullspace, getVecRhsInterior());
Thomas Witkowski's avatar
Thomas Witkowski committed
458 459 460 461
      }
    } else {
      TEST_EXIT(removeRhsNullspace == false)
	("No nullspace provided that should be removed from rhs!\n");
462 463
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
464
    // PETSc.
465
    solve(getVecRhsInterior(), getVecSolInterior());
Thomas Witkowski's avatar
Thomas Witkowski committed
466

467
    // delete PETSc stuff
468
    if (nullspace.size() > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
469
      MatNullSpaceDestroy(&matNullspace);
470 471
      VecDestroy(&nullspaceBasis);
    }
472 473 474 475 476
    
    // delete allocated memory
    if ((nullspace.size() > 0 || hasConstantNullspace) && constNullspaceComponent.size() > 0) {
	delete basisVec;
    }
477

Thomas Witkowski's avatar
Thomas Witkowski committed
478 479
    // === Transfere values from PETSc's solution vectors to the DOF vectors. ===
    PetscScalar *vecPointer;
480
    VecGetArray(getVecSolInterior(), &vecPointer);
Thomas Witkowski's avatar
Thomas Witkowski committed
481

482
    int c = 0;
483 484
    for (int component = 0; component < nComponents; component++) {
      DOFVector<double> &dv = *(vec.getDOFVector(component));
485

486
      DofMap& d = (*interiorMap)[component].getMap();
487 488 489
      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
490 491
    }

492
    VecRestoreArray(getVecSolInterior(), &vecPointer);
Thomas Witkowski's avatar
Thomas Witkowski committed
493

494 495
    // === Synchronize DOFs at common DOFs, i.e., DOFs that correspond to ===
    // === more than one partition.                                       ===
Thomas Witkowski's avatar
Thomas Witkowski committed
496
    meshDistributor->synchVector(vec);
497 498 499
  }


500 501 502
  void PetscSolverGlobalMatrix::solveGlobal(Vec &rhs, Vec &sol)
  {
    Vec tmp;
503
    if (domainComm.Get_size() == 1)
504
      createLocalVec(*interiorMap, tmp);
505
    else
506
      createVec(*interiorMap, tmp);
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

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

    KSPSolve(kspInterior, tmp, tmp);

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

523
    for (int i = 0; i < interiorMap->getRankDofs(); i++)
524 525 526 527 528 529 530 531 532
      rhsValues[i] = tmpValues[i];

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

    VecDestroy(&tmp);
  }


533 534
  void PetscSolverGlobalMatrix::destroyMatrixData()
  {
535
    matDestroy();
Thomas Witkowski's avatar
Thomas Witkowski committed
536

537 538
    exitPreconditioner(pcInterior);
    exitSolver(kspInterior);
Thomas Witkowski's avatar
Thomas Witkowski committed
539 540 541
  }


542 543
  void PetscSolverGlobalMatrix::destroyVectorData()
  {
544
    vecDestroy();
545 546 547
  }


548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
  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;
563
      Parameters::get("parallel->solver->is block " + boost::lexical_cast<string>(i),
564 565 566
		      blockComponents);
      int nComponents = static_cast<int>(blockComponents.size());

567
      TEST_EXIT(nComponents > 0)("No IS block for block %d defined!\n", i);
568 569 570 571

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

576
      createFieldSplit(pc, isNames[i].c_str(), blockComponents);
577 578 579 580
    }
  }


581 582
  void PetscSolverGlobalMatrix::createFieldSplit(PC pc,
						 const char* splitName,
583 584 585 586
						 vector<int> &components)
  {
    IS is;
    interiorMap->createIndexSet(is, components[0], components.size());
587
    PCFieldSplitSetIS(pc, splitName, is);
588 589
    ISDestroy(&is);
  }
590 591


592 593 594 595 596 597 598
  void PetscSolverGlobalMatrix::extractVectorComponent(Vec input, int i, Vec *output, int numberOfComponents)
  {
    IS is;
    interiorMap->createIndexSet(is, i, numberOfComponents);
    VecGetSubVector(input, is, output);
    ISDestroy(&is);
  }
599

600 601 602 603 604 605 606 607 608
  void PetscSolverGlobalMatrix::extractMatrixComponent(Mat input, int startRow, int numberOfRows, int startCol, int numberOfCols, Mat *output)
  {
    IS isrow, iscol;
    interiorMap->createIndexSet(isrow, startRow, numberOfRows);
    interiorMap->createIndexSet(iscol, startCol, numberOfCols);
    MatGetSubMatrix(input, isrow, iscol, MAT_INITIAL_MATRIX, output);
    ISDestroy(&iscol);
    ISDestroy(&isrow);
  }
609

610

611
  void PetscSolverGlobalMatrix::initSolver(KSP &ksp)
612
  {
613
    KSPCreate(domainComm, &ksp);
614
    petsc::ksp_set_operators(ksp, getMatInterior(), getMatInterior());
615 616 617
    KSPSetTolerances(ksp, 0.0, 1e-8, PETSC_DEFAULT, PETSC_DEFAULT);
    KSPSetType(ksp, KSPBCGS);
    KSPSetOptionsPrefix(ksp, kspPrefix.c_str());
618
    MatSetOptionsPrefix(getMatInterior(), kspPrefix.c_str());
619 620 621 622 623
    KSPSetFromOptions(ksp);

    // Do not delete the solution vector, use it for the initial guess.
    if (!zeroStartVector)
      KSPSetInitialGuessNonzero(ksp, PETSC_TRUE);
624
  }
625 626


627
  void PetscSolverGlobalMatrix::exitSolver(KSP &ksp)
628 629
  {
    KSPDestroy(&ksp);
630
  }
631 632


633 634 635 636 637 638
  void PetscSolverGlobalMatrix::initPreconditioner(PC pc)
  {
    PCSetFromOptions(pc);
    createFieldSplit(pc);
  }

639

640
  void PetscSolverGlobalMatrix::exitPreconditioner(PC pc)
641
  { }
642 643


644
  /// called by \ref fillPetscMatrix
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
645
  void PetscSolverGlobalMatrix::setDofMatrix(DOFMatrix* seqMat,
Thomas Witkowski's avatar
Thomas Witkowski committed
646
					     int rowComp, int colComp)
Thomas Witkowski's avatar
Thomas Witkowski committed
647 648 649
  {
    FUNCNAME("PetscSolverGlobalMatrix::setDofMatrix()");

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
650
    TEST_EXIT(seqMat)("No DOFMatrix!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
651 652

    using mtl::tag::row; using mtl::tag::nz; using mtl::begin; using mtl::end;
653
    namespace traits = mtl::traits;
Thomas Witkowski's avatar
Thomas Witkowski committed
654 655
    typedef DOFMatrix::base_matrix_type Matrix;

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
656 657
    traits::col<Matrix>::type col(seqMat->getBaseMatrix());
    traits::const_value<Matrix>::type value(seqMat->getBaseMatrix());
Thomas Witkowski's avatar
Thomas Witkowski committed
658 659 660 661 662 663 664 665

    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);
666

667 668
    // Get periodic mapping object
    PeriodicMap &perMap = meshDistributor->getPeriodicMap();
669
    std::map<int, int> associatedRows;
Thomas Witkowski's avatar
Thomas Witkowski committed
670
    std::set<DegreeOfFreedom> &dirichletRows = seqMat->getDirichletRows();
671

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
672 673
    const FiniteElemSpace *rowFe = seqMat->getRowFeSpace();
    const FiniteElemSpace *colFe = seqMat->getColFeSpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
674

675
    for (cursor_type cursor = begin<row>(seqMat->getBaseMatrix()),
676
         cend = end<row>(seqMat->getBaseMatrix()); cursor != cend; ++cursor) {
677
      MultiIndex rowMultiIndex;
678
      if ((*interiorMap)[rowComp].find(cursor.value(), rowMultiIndex) == false)
679
        continue;
680 681

      int globalRowDof = rowMultiIndex.global;
682

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

Thomas Witkowski's avatar
Thomas Witkowski committed
686
      // Dirichlet rows can be set only be the owner ranks.
687
      if (dirichletRows.count(cursor.value()) && !((*interiorMap)[rowComp].isRankDof(cursor.value())))
688
        continue;
Thomas Witkowski's avatar
Thomas Witkowski committed
689

690 691
      int rowIndex = interiorMap->getMatIndex(rowComp, globalRowDof);
      int assRowIndex = -1;
Thomas Witkowski's avatar
Thomas Witkowski committed
692

693 694 695 696
      if (periodicRow) {
        std::set<int> perAsc;
        perMap.fillAssociations(rowFe, globalRowDof,
                          meshDistributor->getElementObjectDb(), perAsc);
697

698 699
        vector<int> newRows;
        perMap.mapDof(rowFe, globalRowDof, perAsc, newRows);
Thomas Witkowski's avatar
Thomas Witkowski committed
700

701 702 703
        assRowIndex = interiorMap->getMatIndex(rowComp,
                                *std::max_element(newRows.begin(),newRows.end()));
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
704

705 706
      cols.clear();
      values.clear();
Thomas Witkowski's avatar
Thomas Witkowski committed
707

708 709
      for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor);
           icursor != icend; ++icursor) {
Thomas Witkowski's avatar
Thomas Witkowski committed
710

711 712 713 714
        // Global index of the current column index.
        MultiIndex colMultiIndex;
        if ((*interiorMap)[colComp].find(col(*icursor), colMultiIndex) == false)
          continue;
Thomas Witkowski's avatar
Thomas Witkowski committed
715

716 717 718
        int globalColDof = colMultiIndex.global;
        // Get PETSc's mat col index.
        int colIndex = interiorMap->getMatIndex(colComp, globalColDof);
Thomas Witkowski's avatar
Thomas Witkowski committed
719

720 721 722
        cols.push_back(colIndex);
        values.push_back(value(*icursor));
          }
Thomas Witkowski's avatar
Thomas Witkowski committed
723

724 725 726 727 728 729
          if (!periodicRow || assRowIndex == rowIndex)
        MatSetValues(getMatInterior(), 1, &rowIndex, cols.size(),
                &(cols[0]), &(values[0]), ADD_VALUES);
          else {
        MatSetValues(getMatInterior(), 1, &assRowIndex, cols.size(),
                &(cols[0]), &(values[0]), ADD_VALUES);
Thomas Witkowski's avatar
Thomas Witkowski committed
730

731 732 733
        associatedRows.insert(std::make_pair(rowIndex, assRowIndex));
          }
        }
Thomas Witkowski's avatar
Thomas Witkowski committed
734

735 736
        int globalPeriodic = associatedRows.size();
        Parallel::mpi::globalAdd(globalPeriodic);
Thomas Witkowski's avatar
Thomas Witkowski committed
737

738
        if (globalPeriodic) {
Thomas Witkowski's avatar
Thomas Witkowski committed
739

740 741
          MatAssemblyBegin(getMatInterior(), MAT_FLUSH_ASSEMBLY);
          MatAssemblyEnd(getMatInterior(), MAT_FLUSH_ASSEMBLY);
Thomas Witkowski's avatar
Thomas Witkowski committed
742

743
          int c[2]; double v[2] = {1.0, -1.0};
Thomas Witkowski's avatar
Thomas Witkowski committed
744

745 746 747 748
          std::map<int, int>::iterator mapIt = associatedRows.begin();
          for (;mapIt != associatedRows.end(); mapIt++) {
        c[0] = mapIt->first;
        c[1] = mapIt->second;
Thomas Witkowski's avatar
Thomas Witkowski committed
749

750
        MatSetValues(getMatInterior(), 1, &c[0], 2, c, v, INSERT_VALUES);
Thomas Witkowski's avatar
Thomas Witkowski committed
751
      }
752 753 754

      MatAssemblyBegin(getMatInterior(), MAT_FLUSH_ASSEMBLY);
      MatAssemblyEnd(getMatInterior(), MAT_FLUSH_ASSEMBLY);
Thomas Witkowski's avatar
Thomas Witkowski committed
755 756 757 758
    }
  }


759
  /// called by \ref fillPetscRhs
760
  void PetscSolverGlobalMatrix::setDofVector(Vec vecInterior,
Thomas Witkowski's avatar
Thomas Witkowski committed
761
					     Vec vecCoarse,
762 763
					     DOFVector<double>* vec,
					     int rowComp,
764
					     bool rankOnly)
Thomas Witkowski's avatar
Thomas Witkowski committed
765
  {
766
    FUNCNAME_DBG("PetscSolverGlobalMatrix::setDofVector()");
Thomas Witkowski's avatar
Thomas Witkowski committed
767

768
    const FiniteElemSpace *feSpace = vec->getFeSpace();
769
    PeriodicMap &perMap = meshDistributor->getPeriodicMap();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
770

771 772
    std::set<int> associatedRows;

773
    ParallelDofMapping *rowCoarseSpace =
774
      (coarseSpaceMap.size() ? coarseSpaceMap[rowComp] : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
775 776

    map<DegreeOfFreedom, double> &dirichletValues = vec->getDirichletValues();
777

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

Thomas Witkowski's avatar
Thomas Witkowski committed
782
      DegreeOfFreedom dof = dofIt.getDOFIndex();
783

Thomas Witkowski's avatar
Thomas Witkowski committed
784
      if (rankOnly && !(*interiorMap)[rowComp].isRankDof(dof))
785
        continue;
786

787 788
      bool isCoarseDof = isCoarseSpace(rowComp, dof);