PetscSolverGlobalMatrix.cc 30 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * 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.
 * 
 ******************************************************************************/
Thomas Witkowski's avatar
Thomas Witkowski committed
20

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

29
namespace AMDiS { namespace Parallel {
30
31
32
33
34
35

  PetscSolverGlobalMatrix::PetscSolverGlobalMatrix(string name)
    : PetscSolver(name),
      zeroStartVector(false),
      printMatInfo(false)
  {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    PetscParameters params;
    bool matSolverPackage = false;
    
    // set the solver
    std::string solverName = "petsc";
    Parameters::get(name, solverName);
    if (solverName == "petsc") 
      Parameters::get(name + "->ksp_type", solverName);
    
    std::string kspSolver = params.solverMap[solverName];
    
    if (params.matSolverPackage[kspSolver]) {
      // direct solvers
      PetscOptionsInsertString(("-" + kspPrefix + "ksp_type preonly").c_str());
      PetscOptionsInsertString(("-" + kspPrefix + "pc_type lu").c_str());
      PetscOptionsInsertString(("-" + kspPrefix + "pc_factor_mat_solver_package " + (kspSolver != "direct" ? kspSolver : "mumps")).c_str());
      setMaxIterations(1);
      zeroStartVector = true;
      matSolverPackage = true;
    } else if (!params.emptyParam[kspSolver]) {    
      // other solvers
      PetscOptionsInsertString(("-" + kspPrefix + "ksp_type " + kspSolver).c_str());
    }
    
    
    // set the preconditioner
    string precon = "";
    Parameters::get(name + "->pc_type", precon);
    if (!precon.size())
      Parameters::get(name + "->left precon", precon);
    if (!matSolverPackage && !params.emptyParam[precon]) {
      precon = (params.preconMap.find(precon) != params.preconMap.end() ? params.preconMap[precon] : precon);
      PetscOptionsInsertString(("-" + kspPrefix + "pc_type " + precon).c_str());
    }
    
    PetscOptionsInsertString(("-" + kspPrefix + "ksp_max_it " + boost::lexical_cast<std::string>(getMaxIterations())).c_str());
    PetscOptionsInsertString(("-" + kspPrefix + "ksp_rtol " + boost::lexical_cast<std::string>(getRelative())).c_str());    
    PetscOptionsInsertString(("-" + kspPrefix + "ksp_atol " + boost::lexical_cast<std::string>(getTolerance())).c_str());   
    
    if (getInfo() >= 20)
      PetscOptionsInsertString(("-" + kspPrefix + "ksp_monitor_true_residual").c_str());
    else if (getInfo() >= 10)
      PetscOptionsInsertString(("-" + kspPrefix + "ksp_monitor").c_str());
    
    if (!matSolverPackage) {
      Parameters::get("parallel->use zero start vector", zeroStartVector);
    }
83
84
85
86
    Parameters::get("parallel->print matrix info", printMatInfo);
  }


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
87
  void PetscSolverGlobalMatrix::fillPetscMatrix(Matrix<DOFMatrix*> *seqMat)
Thomas Witkowski's avatar
Thomas Witkowski committed
88
89
90
  {
    FUNCNAME("PetscSolverGlobalMatrix::fillPetscMatrix()");

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
91
92
93
94
    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");
    
95
#if (DEBUG != 0)
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
96
    double wtime = MPI::Wtime();
97
#endif
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
98

99
    createMatVec(*seqMat);
100

Thomas Witkowski's avatar
Thomas Witkowski committed
101
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
102
      fillPetscMatrixWithCoarseSpace(seqMat);
103
104
      return;
    }
105
    
106
107
    // === Create PETSc vector (solution and a temporary vector). ===

Thomas Witkowski's avatar
Thomas Witkowski committed
108
109
110
111
112
113
#if (DEBUG != 0)
    MSG("Fill petsc matrix 1 needed %.5f seconds\n", MPI::Wtime() - wtime);
#endif

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

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
114
    int nComponents = seqMat->getNumRows();
Thomas Witkowski's avatar
Thomas Witkowski committed
115
116
    for (int i = 0; i < nComponents; i++)
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
117
118
	if ((*seqMat)[i][j])
	  setDofMatrix((*seqMat)[i][j], i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
119
120
121
122
123

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

124
    matAssembly();
125

126
127
    if (printMatInfo) {
      MatInfo matInfo;
128
      MatGetInfo(getMatInterior(), MAT_GLOBAL_SUM, &matInfo);
129
130
131
132
133
134
135
      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));
    }
136
137


138
    // === Init PETSc solver and preconditioner objects. ===
139

140
141
    initSolver(kspInterior);
    KSPGetPC(kspInterior, &pcInterior);
142
    initPreconditioner(pcInterior);
143

144

Thomas Witkowski's avatar
Thomas Witkowski committed
145
146
147
#if (DEBUG != 0)
    MSG("Fill petsc matrix 3 needed %.5f seconds\n", MPI::Wtime() - wtime);
#endif
148
149


150
151
152
153
154
155
156
157
158
159
160
    // === 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);
    }
161
162
163
  }


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
164
  void PetscSolverGlobalMatrix::fillPetscMatrixWithCoarseSpace(Matrix<DOFMatrix*> *seqMat)
165
166
  {
    FUNCNAME("PetscSolverGlobalMatrix::fillPetscMatrixWithCoarseSpace()");
167
168

    TEST_EXIT_DBG(interiorMap)("Should not happen!\n");
169
170
    TEST_EXIT_DBG(coarseSpaceMap.size() == seqMat->getSize())
      ("Wrong sizes %d %d\n", coarseSpaceMap.size(), seqMat->getSize());
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    // === 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);

188
189
190
    bool localMatrix = 
      (meshDistributor->getMeshLevelData().getMpiComm(meshLevel) == MPI::COMM_SELF);

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

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
194
    int nComponents = seqMat->getSize();
Thomas Witkowski's avatar
Thomas Witkowski committed
195
196
197
198
199
    for (int rowComponent = 0; rowComponent < nComponents; rowComponent++) {
      for (int colComponent = 0; colComponent < nComponents; colComponent++) {
	DOFMatrix* dofMat = (*seqMat)[rowComponent][colComponent];

	if (!dofMat)
200
201
	  continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
202
203
	ParallelDofMapping *rowCoarseSpace = coarseSpaceMap[rowComponent];
	ParallelDofMapping *colCoarseSpace = coarseSpaceMap[colComponent];
Thomas Witkowski's avatar
Thomas Witkowski committed
204

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

Thomas Witkowski's avatar
Thomas Witkowski committed
207
208
	traits::col<Matrix>::type col(dofMat->getBaseMatrix());
	traits::const_value<Matrix>::type value(dofMat->getBaseMatrix());
209
210
	
	// Traverse all rows.
Thomas Witkowski's avatar
Thomas Witkowski committed
211
212
	for (cursor_type cursor = begin<row>(dofMat->getBaseMatrix()), 
	       cend = end<row>(dofMat->getBaseMatrix()); cursor != cend; ++cursor) {
213

214
	  bool isRowCoarse = isCoarseSpace(rowComponent, cursor.value());
Thomas Witkowski's avatar
Thomas Witkowski committed
215
216
217

	  // For the case, this is a dirichlet row we have to check whether the 
	  // rank is also owner of this row DOF.
218
219
220
	  if (dirichletRows.count(cursor.value())) {
	    if ((!isRowCoarse && !(*interiorMap)[rowComponent].isRankDof(cursor.value())) ||
		(isRowCoarse && !(*rowCoarseSpace)[rowComponent].isRankDof(cursor.value())))
221
	      continue;    
Thomas Witkowski's avatar
Thomas Witkowski committed
222
	  }
223
224
225
226
227
228
229
230
231
232
  
	  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
233
	    bool isColCoarse = isCoarseSpace(colComponent, col(*icursor));
234

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
235
	    if (isColCoarse == false)
236
	      if ((*interiorMap)[colComponent].isSet(col(*icursor)) == false)
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
237
238
		continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
239
240
241
	    if (isColCoarse == isRowCoarse) {
	      cols.push_back(col(*icursor));
	      values.push_back(value(*icursor));
242
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
243
244
	      colsOther.push_back(col(*icursor));
	      valuesOther.push_back(value(*icursor));
245
246
247
248
249
250
	    }
	  }  // for each nnz in row


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

Thomas Witkowski's avatar
Thomas Witkowski committed
251
	  if (isRowCoarse) {
Thomas Witkowski's avatar
Thomas Witkowski committed
252
253
	    for (unsigned int i = 0; i < cols.size(); i++)
	      cols[i] = colCoarseSpace->getMatIndex(colComponent, cols[i]);
254

255
	    int rowIndex = rowCoarseSpace->getMatIndex(rowComponent, cursor.value());
Thomas Witkowski's avatar
Thomas Witkowski committed
256
	    MatSetValues(getMatCoarseByComponent(rowComponent, colComponent),
257
258
	    		 1, &rowIndex, cols.size(),
	    		 &(cols[0]), &(values[0]), ADD_VALUES);
259
260

	    if (colsOther.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
261
262
263
	      for (unsigned int i = 0; i < colsOther.size(); i++)
		colsOther[i] = 
		  interiorMap->getMatIndex(colComponent, colsOther[i]) + 
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
264
		  rStartInterior;	      
265

Thomas Witkowski's avatar
Thomas Witkowski committed
266
	      MatSetValues(getMatCoarseInteriorByComponent(rowComponent), 
267
268
	       		   1, &rowIndex, colsOther.size(),
 	       		   &(colsOther[0]), &(valuesOther[0]), ADD_VALUES);
269
270
	    }
	  } else {
271
	    if ((*interiorMap)[rowComponent].isSet(cursor.value()) == false)
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
272
273
	      continue;

274
	    int localRowIndex = 
275
	      (localMatrix ? 
276
277
	       interiorMap->getLocalMatIndex(rowComponent, cursor.value()) :
	       interiorMap->getMatIndex(rowComponent, cursor.value()));
278

Thomas Witkowski's avatar
Thomas Witkowski committed
279
	    for (unsigned int i = 0; i < cols.size(); i++) {
280
	      if (localMatrix)
Thomas Witkowski's avatar
Thomas Witkowski committed
281
		cols[i] = interiorMap->getLocalMatIndex(colComponent, cols[i]);
282
	      else
Thomas Witkowski's avatar
Thomas Witkowski committed
283
		cols[i] = interiorMap->getMatIndex(colComponent, cols[i]);
284
285
	    }
	    
286
287
	    MatSetValues(getMatInterior(), 1, &localRowIndex, cols.size(),
  	     		 &(cols[0]), &(values[0]), ADD_VALUES);
Thomas Witkowski's avatar
Thomas Witkowski committed
288
	    
289
	    if (colsOther.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
290
	      int globalRowIndex = 
291
		interiorMap->getMatIndex(rowComponent, cursor.value()) + rStartInterior;
Thomas Witkowski's avatar
Thomas Witkowski committed
292
      
Thomas Witkowski's avatar
Thomas Witkowski committed
293
294
295
	      for (unsigned int i = 0; i < colsOther.size(); i++)
		colsOther[i] = 
		  colCoarseSpace->getMatIndex(colComponent, colsOther[i]);
296

297
298
299
	      MatSetValues(getMatInteriorCoarseByComponent(colComponent), 
			   1, &globalRowIndex, colsOther.size(),
			   &(colsOther[0]), &(valuesOther[0]), ADD_VALUES);
300
301
302
303
304
305
	    }
	  }
	} 
      }
    }

306
    matAssembly();
307

308
309
    // === Create solver for the non primal (thus local) variables. ===

310
    KSPCreate(domainComm, &kspInterior);
311
    KSPSetOperators(kspInterior, getMatInterior(), getMatInterior(),
312
		    SAME_NONZERO_PATTERN);
313
314
315
    KSPSetOptionsPrefix(kspInterior, "interior_");
    KSPSetType(kspInterior, KSPPREONLY);
    KSPGetPC(kspInterior, &pcInterior);
316
317
    if (isSymmetric) {
      PCSetType(pcInterior, PCCHOLESKY);
318
      PCFactorSetMatSolverPackage(pcInterior, MATSOLVERMUMPS);
319
320
    } else {
      PCSetType(pcInterior, PCLU);
321
      if (localMatrix)
322
323
324
325
	PCFactorSetMatSolverPackage(pcInterior, MATSOLVERUMFPACK);
      else
	PCFactorSetMatSolverPackage(pcInterior, MATSOLVERMUMPS);
    }
326
    KSPSetFromOptions(kspInterior);
327
328
329
  }


330
331
332
  void PetscSolverGlobalMatrix::fillPetscRhs(SystemVector *vec)
  {
    FUNCNAME("PetscSolverGlobalMatrix::fillPetscRhs()");
Thomas Witkowski's avatar
Thomas Witkowski committed
333

334
    TEST_EXIT_DBG(vec)("No DOF vector defined!\n");
335
    TEST_EXIT_DBG(interiorMap)("No parallel DOF map defined!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
336
    
Thomas Witkowski's avatar
Thomas Witkowski committed
337
    // === Transfer values from DOF vector to the PETSc vector. === 
Thomas Witkowski's avatar
Thomas Witkowski committed
338
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
339
      for (int i = 0; i < vec->getSize(); i++)
340
	setDofVector(getVecRhsInterior(), 
Thomas Witkowski's avatar
Thomas Witkowski committed
341
		     getVecRhsCoarseByComponent(i), vec->getDOFVector(i), i);
Thomas Witkowski's avatar
Thomas Witkowski committed
342
343
    } else {
      for (int i = 0; i < vec->getSize(); i++)
344
	setDofVector(getVecRhsInterior(), vec->getDOFVector(i), i);
Thomas Witkowski's avatar
Thomas Witkowski committed
345
346
    }

347
    vecRhsAssembly();
348
349
350
351
352
353
354
355
356
357
358
359

    // === 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
360
361
362
  }


363
364
  void PetscSolverGlobalMatrix::solvePetscMatrix(SystemVector &vec, 
						 AdaptInfo *adaptInfo)
Thomas Witkowski's avatar
Thomas Witkowski committed
365
366
367
368
369
370
371
  {
    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
372
373
      TEST_EXIT(coarseSpaceMap.size() == 0)("Not yet supported!\n");

374
      VecSet(getVecSolInterior(), 0.0);
Thomas Witkowski's avatar
Thomas Witkowski committed
375
376
      
      for (int i = 0; i < nComponents; i++)
377
	setDofVector(getVecSolInterior(), vec.getDOFVector(i), i, true);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
378

379
      vecSolAssembly();
Thomas Witkowski's avatar
Thomas Witkowski committed
380
381
    }

382

Thomas Witkowski's avatar
Thomas Witkowski committed
383
384
    MatNullSpace matNullspace;
    Vec nullspaceBasis;
385
386
387
    if (nullspace.size() > 0 || 
	hasConstantNullspace ||
	constNullspaceComponent.size() > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
388
      TEST_EXIT_DBG(nullspace.size() <= 1)("Not yet implemented!\n");
389

390
391
392
393
394
395
396
397
398
399
      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
400
      if (nullspace.size() > 0) {
401
	VecDuplicate(getVecSolInterior(), &nullspaceBasis);
402
	setDofVector(nullspaceBasis, *(nullspace[0]), true);
Thomas Witkowski's avatar
Thomas Witkowski committed
403
404
405
	
	VecAssemblyBegin(nullspaceBasis);
	VecAssemblyEnd(nullspaceBasis);
406
407

	VecNormalize(nullspaceBasis, PETSC_NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
408
	
409
	MatNullSpaceCreate(domainComm, (hasConstantNullspace ? PETSC_TRUE : PETSC_FALSE), 
Thomas Witkowski's avatar
Thomas Witkowski committed
410
411
			   1, &nullspaceBasis, &matNullspace);

412
	MatMult(getMatInterior(), nullspaceBasis, getVecSolInterior());
Thomas Witkowski's avatar
Thomas Witkowski committed
413
	PetscReal n;
414
	VecNorm(getVecSolInterior(), NORM_2, &n);
Thomas Witkowski's avatar
Thomas Witkowski committed
415
416
	MSG("NORM IS: %e\n", n);
      } else {
417
	MatNullSpaceCreate(domainComm, PETSC_TRUE, 0, PETSC_NULL, &matNullspace);
Thomas Witkowski's avatar
Thomas Witkowski committed
418
      }
419

Thomas Witkowski's avatar
Thomas Witkowski committed
420
421
422
      MSG("NULLSPACE IS NOT REMOVED!\n");
      // MatSetNullSpace(getMatInterior(), matNullspace);
      // KSPSetNullSpace(kspInterior, matNullspace);
423

Thomas Witkowski's avatar
Thomas Witkowski committed
424
      // === Remove null space, if requested. ===
425

Thomas Witkowski's avatar
Thomas Witkowski committed
426
      if (removeRhsNullspace) {
Thomas Witkowski's avatar
Thomas Witkowski committed
427
	TEST_EXIT_DBG(coarseSpaceMap.empty())("Not supported!\n");
428
429

	MSG("Remove nullspace from rhs vector.\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
430
	
431
	MatNullSpaceRemove(matNullspace, getVecRhsInterior(), PETSC_NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
432
433
434
435
      }
    } else {
      TEST_EXIT(removeRhsNullspace == false)
	("No nullspace provided that should be removed from rhs!\n");
436
437
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
438
    // PETSc.
439
    solve(getVecRhsInterior(), getVecSolInterior());
Thomas Witkowski's avatar
Thomas Witkowski committed
440

441
442

    if (nullspace.size() > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
443
      MatNullSpaceDestroy(&matNullspace);
444
445
446
447
      VecDestroy(&nullspaceBasis);
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
448
449
    // === Transfere values from PETSc's solution vectors to the DOF vectors. ===
    PetscScalar *vecPointer;
450
    VecGetArray(getVecSolInterior(), &vecPointer);    
Thomas Witkowski's avatar
Thomas Witkowski committed
451

452
    int c = 0;
453
454
    for (int component = 0; component < nComponents; component++) {
      DOFVector<double> &dv = *(vec.getDOFVector(component));
455

456
      DofMap& d = (*interiorMap)[component].getMap();
457
458
459
      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
460
461
    }

462
    VecRestoreArray(getVecSolInterior(), &vecPointer);
Thomas Witkowski's avatar
Thomas Witkowski committed
463
464
465
466

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


470
471
472
473
474
  void PetscSolverGlobalMatrix::solveGlobal(Vec &rhs, Vec &sol)
  {
    FUNCNAME("PetscSolverGlobalMatrix::solveGlobal()");

    Vec tmp;
475
    if (domainComm.Get_size() == 1)
476
      createLocalVec(*interiorMap, tmp);
477
    else
478
      createVec(*interiorMap, tmp);
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504

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

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

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

    VecDestroy(&tmp);
  }


505
506
507
508
  void PetscSolverGlobalMatrix::destroyMatrixData()
  {
    FUNCNAME("PetscSolverGlobalMatrix::destroyMatrixData()");

509
    matDestroy();
Thomas Witkowski's avatar
Thomas Witkowski committed
510

511
512
513
    exitPreconditioner(pcInterior);

    exitSolver(kspInterior);
Thomas Witkowski's avatar
Thomas Witkowski committed
514
515
516
  }


517
518
519
520
  void PetscSolverGlobalMatrix::destroyVectorData()
  {
    FUNCNAME("PetscSolverGlobalMatrix::destroyVectorData()");

521
    vecDestroy();
522
523
524
  }


525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
  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());

544
      TEST_EXIT(nComponents > 0)("No IS block for block %d defined!\n", i);
545
546
547
548
549
550
551
552

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

553
      createFieldSplit(pc, isNames[i].c_str(), blockComponents);
554
555
556
557
    }
  }


558
  void PetscSolverGlobalMatrix::createFieldSplit(PC pc, 
559
						 const char* splitName, 
560
561
562
563
564
565
						 vector<int> &components)
  {
    FUNCNAME("PetscSolverGlobalMatrix::createFieldSplit()");

    IS is;
    interiorMap->createIndexSet(is, components[0], components.size());
566
    PCFieldSplitSetIS(pc, splitName, is);
567
568
    ISDestroy(&is);
  }
569
570
571
572
573
574
575
576
577
578
  
  
  void PetscSolverGlobalMatrix::extractVectorComponent(Vec input, int i, Vec *output, int numberOfComponents)
  {
    FUNCNAME("PetscSolverGlobalMatrix::extractVectorComponent()");
    IS is;
    interiorMap->createIndexSet(is, i, numberOfComponents);
    VecGetSubVector(input, is, output);
    ISDestroy(&is);
  }
579

580
581
582
583
584
585
586
587
588
589
590
  void PetscSolverGlobalMatrix::extractMatrixComponent(Mat input, int startRow, int numberOfRows, int startCol, int numberOfCols, Mat *output)
  {
    FUNCNAME("PetscSolverGlobalMatrix::extractMatrixComponent()");
    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);
  }
  
591

592
  void PetscSolverGlobalMatrix::initSolver(KSP &ksp)
593
594
595
  {
    FUNCNAME("PetscSolverGlobalMatrix::initSolver()");

596
    KSPCreate(domainComm, &ksp);
597
598
599
600
601
602
603
604
605
606
607
608
609
    KSPSetOperators(ksp, getMatInterior(), getMatInterior(), 
		    SAME_NONZERO_PATTERN); 
    KSPSetTolerances(ksp, 0.0, 1e-8, PETSC_DEFAULT, PETSC_DEFAULT);
    KSPSetType(ksp, KSPBCGS);
    KSPSetOptionsPrefix(ksp, kspPrefix.c_str());
    KSPSetFromOptions(ksp);

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


610
  void PetscSolverGlobalMatrix::exitSolver(KSP &ksp)
611
612
613
614
615
616
617
  {
    FUNCNAME("PetscSolverGlobalMatrix::exitSolver()");

    KSPDestroy(&ksp);
  }  


618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  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
633
  void PetscSolverGlobalMatrix::setDofMatrix(DOFMatrix* seqMat,
Thomas Witkowski's avatar
Thomas Witkowski committed
634
					     int rowComp, int colComp)
Thomas Witkowski's avatar
Thomas Witkowski committed
635
636
637
  {
    FUNCNAME("PetscSolverGlobalMatrix::setDofMatrix()");

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
638
    TEST_EXIT(seqMat)("No DOFMatrix!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
639
640

    using mtl::tag::row; using mtl::tag::nz; using mtl::begin; using mtl::end;
641
    namespace traits = mtl::traits;
Thomas Witkowski's avatar
Thomas Witkowski committed
642
643
    typedef DOFMatrix::base_matrix_type Matrix;

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
644
645
    traits::col<Matrix>::type col(seqMat->getBaseMatrix());
    traits::const_value<Matrix>::type value(seqMat->getBaseMatrix());
Thomas Witkowski's avatar
Thomas Witkowski committed
646
647
648
649
650
651
652
653
654
655
656

    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;

657
658
    // Get periodic mapping object
    PeriodicMap &perMap = meshDistributor->getPeriodicMap();
Thomas Witkowski's avatar
Thomas Witkowski committed
659
660
    std::set<DegreeOfFreedom> &dirichletRows = seqMat->getDirichletRows();
      
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
661
662
    const FiniteElemSpace *rowFe = seqMat->getRowFeSpace();
    const FiniteElemSpace *colFe = seqMat->getColFeSpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
663
664

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

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
667
668
    for (cursor_type cursor = begin<row>(seqMat->getBaseMatrix()), 
	   cend = end<row>(seqMat->getBaseMatrix()); cursor != cend; ++cursor) {
Thomas Witkowski's avatar
Thomas Witkowski committed
669
      // Global index of the current row DOF.
670
      MultiIndex rowMultiIndex;
671
      if ((*interiorMap)[rowComp].find(cursor.value(), rowMultiIndex) == false)
672
673
674
	continue;

      int globalRowDof = rowMultiIndex.global;
675

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
683
684
685
      if (!periodicRow) {
	// === Row DOF index is not periodic. ===

686
	// Get PETSc's mat row index.
Thomas Witkowski's avatar
Thomas Witkowski committed
687
	int rowIndex = interiorMap->getMatIndex(rowComp, globalRowDof);
Thomas Witkowski's avatar
Thomas Witkowski committed
688
689
690
691
692
693
694
695

	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.
696
	  MultiIndex colMultiIndex;
Thomas Witkowski's avatar
Thomas Witkowski committed
697
	  if ((*interiorMap)[colComp].find(col(*icursor), colMultiIndex) == false)
698
699
700
	    continue;

	  int globalColDof = colMultiIndex.global;
Thomas Witkowski's avatar
Thomas Witkowski committed
701
	  // Test if the current col dof is a periodic dof.
702
	  bool periodicCol = perMap.isPeriodic(colFe, globalColDof);
703
	  // Get PETSc's mat col index.
Thomas Witkowski's avatar
Thomas Witkowski committed
704
	  int colIndex = interiorMap->getMatIndex(colComp, globalColDof);
Thomas Witkowski's avatar
Thomas Witkowski committed
705
706
707
708
709
710
711

	  // 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
712
713
 	    cols.push_back(colIndex);
 	    values.push_back(value(*icursor));
Thomas Witkowski's avatar
Thomas Witkowski committed
714
715
716
717
718
	  } else {
	    // === Row index is not periodic, but column index is. ===

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

Thomas Witkowski's avatar
Thomas Witkowski committed
722
723
724
725
726
727
728
729
730
	    // 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;
731
	    perMap.mapDof(colFe, globalColDof, perAsc, newCols);
Thomas Witkowski's avatar
Thomas Witkowski committed
732
	    for (unsigned int i = 0; i < newCols.size(); i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
733
	      cols.push_back(interiorMap->getMatIndex(colComp, newCols[i]));
Thomas Witkowski's avatar
Thomas Witkowski committed
734
735
736
737
738
	      values.push_back(scaledValue);	      
	    }
	  }
	}

739
  	MatSetValues(getMatInterior(), 1, &rowIndex, cols.size(), 
Thomas Witkowski's avatar
Thomas Witkowski committed
740
  		     &(cols[0]), &(values[0]), ADD_VALUES);	
Thomas Witkowski's avatar
Thomas Witkowski committed
741
742
743
744
745
746
747
748
749
750
751
752
753
754
      } 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.
Thomas Witkowski's avatar
Thomas Witkowski committed
755
	  int globalColDof = (*interiorMap)[colComp][col(*icursor)].global;
Thomas Witkowski's avatar
Thomas Witkowski committed
756
757
758
759
760
761
762
763
764

	  // 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;
765
766
767
768
	  perMap.fillAssociations(colFe, globalColDof, 
				  meshDistributor->getElementObjectDb(), perAsc);
	  perMap.fillAssociations(rowFe, globalRowDof, 
				  meshDistributor->getElementObjectDb(), perAsc);
Thomas Witkowski's avatar
Thomas Witkowski committed
769
770
771
772
773
774
775
776
777
778

	  // 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
779
780
	  perMap.mapDof(rowFe, colFe, make_pair(globalRowDof, globalColDof),
			perAsc, entry);
Thomas Witkowski's avatar
Thomas Witkowski committed
781
782
783

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

784
	  for (unsigned int i = 0; i < entry.size(); i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
785
786
	    int rowIdx = interiorMap->getMatIndex(rowComp, entry[i].first);
	    int colIdx = interiorMap->getMatIndex(colComp, entry[i].second);
Thomas Witkowski's avatar
Thomas Witkowski committed
787

788
789
	    colsMap[rowIdx].push_back(colIdx);
	    valsMap[rowIdx].push_back(scaledValue);
Thomas Witkowski's avatar
Thomas Witkowski committed
790
791
792
793
794
795
796
797
798
799
800
801
	  }
	}


	// === 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;
802

803
	  MatSetValues(getMatInterior(), 1, &rowIndex, rowIt->second.size(),
Thomas Witkowski's avatar
Thomas Witkowski committed
804
805
806
807
808
809
810
		       &(rowIt->second[0]), &(valsMap[rowIt->first][0]), ADD_VALUES);
	}
      }
    }
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
811
812
  void PetscSolverGlobalMatrix::setDofVector(Vec vecInterior, 
					     Vec vecCoarse,
813
					     DOFVector<double>* vec, 
Thomas Witkowski's avatar
Thomas Witkowski committed
814
					     int rowComp, 
815
					     bool rankOnly)
Thomas Witkowski's avatar
Thomas Witkowski committed
816
817
818
  {
    FUNCNAME("PetscSolverGlobalMatrix::setDofVector()");

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

822
    ParallelDofMapping *rowCoarseSpace = 
Thomas Witkowski's avatar
Thomas Witkowski committed
823
824
825
      (coarseSpaceMap.size() ? coarseSpaceMap[rowComp] : NULL);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
827
828
829
    // Traverse all used DOFs in the dof vector.
    DOFVector<double>::Iterator dofIt(vec, USED_DOFS);
    for (dofIt.reset(); !dofIt.end(); ++dofIt) {
Thomas Witkowski's avatar
Thomas Witkowski committed
830
831
      
      DegreeOfFreedom dof = dofIt.getDOFIndex();
832

Thomas Witkowski's avatar
Thomas Witkowski committed
833
834
      if (rankOnly && !(*interiorMap)[rowComp].isRankDof(dof))
	continue;
835

836
837
      bool isCoarseDof = isCoarseSpace(rowComp, dof);

Thomas Witkowski's avatar
Thomas Witkowski committed
838
      // Dirichlet rows can be set only be the owner ranks.
839
840
841
842
843
      if (dirichletValues.count(dof)) {
	if ((!isCoarseDof && !((*interiorMap)[rowComp].isRankDof(dof))) ||
	    (isCoarseDof && !((*rowCoarseSpace)[rowComp].isRankDof(dof))))
	  continue;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
844

845
      if (isCoarseDof) {
846
847
	TEST_EXIT_DBG(vecCoarse != PETSC_NULL)("Should not happen!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
848
	int index = rowCoarseSpace->getMatIndex(rowComp, dof);
849
850
	VecSetValue(vecCoarse, index, *dofIt, ADD_VALUES);
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
851
	if ((*interiorMap)[rowComp].isSet(dof) == false)
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
852
853
	  continue;

854
	// Calculate global row index of the DOF.
Thomas Witkowski's avatar
Thomas Witkowski committed
855
	DegreeOfFreedom globalRowDof = (*interiorMap)[rowComp][dof].global;
856
857
858
859
860
	
	// Get PETSc's mat index of the row DOF.
	int index = 0;
	if (interiorMap->isMatIndexFromGlobal())
	  index = 
Thomas Witkowski's avatar
Thomas Witkowski committed
861
	    interiorMap->getMatIndex(rowComp, globalRowDof) + rStartInterior;
862
863
	else
	  index =
Thomas Witkowski's avatar
Thomas Witkowski committed
864
	    interiorMap->getMatIndex(rowComp, dof) + rStartInterior;
865

866
	if (perMap.isPeriodic(feSpace, globalRowDof)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
867
868
869
	  std::set<int>& perAsc = perMap.getAssociations(feSpace, globalRowDof);
	  double value = *dofIt / (perAsc.size() + 1.0);
	  VecSetValue(vecInterior, index, value, ADD_VALUES);
870

Thomas Witkowski's avatar
Thomas Witkowski committed
871
872
873
	  for (std::set<int>::iterator perIt = perAsc.begin(); 
	       perIt != perAsc.end(); ++perIt) {
	    int mappedDof = perMap.map(feSpace, *perIt, globalRowDof);
Thomas Witkowski's avatar
Thomas Witkowski committed
874
	    int mappedIndex = interiorMap->getMatIndex(rowComp, mappedDof);
875

Thomas Witkowski's avatar
Thomas Witkowski committed
876
	    VecSetValue(vecInterior, mappedIndex, value, ADD_VALUES);
877
878
879
	  }	  
	} else {	  
	  // The DOF index is not periodic.
Thomas Witkowski's avatar
Thomas Witkowski committed
880
881
	  VecSetValue(vecInterior, index, *dofIt, ADD_VALUES);
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
882
883
884
885
      }
    }
  }

886
887
888
889
890
891
892
893
894
895

  PetscSolver* PetscSolverGlobalMatrix::createSubSolver(int component,
							string kspPrefix)
  {
    FUNCNAME("PetscSolverGlobalMatrix::createSubSolver()");

    vector<const FiniteElemSpace*> fe;
    fe.push_back(componentSpaces[component]);

    PetscSolver* subSolver = new PetscSolverGlobalMatrix("");
896
    subSolver->setKspPrefix(kspPrefix);
897
    subSolver->setMeshDistributor(meshDistributor, 0);
898
899
900
901
902
903
904
905
906
    subSolver->init(fe, fe);

    ParallelDofMapping &subDofMap = subSolver->getDofMap();
    subDofMap[0] = dofMap[component];
    subDofMap.update();

    return subSolver;
  }

907
908
909
910

  void PetscSolverGlobalMatrix::setConstantNullSpace(KSP ksp,
						     int constFeSpace,
						     bool test)
Thomas Witkowski's avatar
blub    
Thomas Witkowski committed
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
  {
    FUNCNAME("PetscSolverGlobalMatrix::setConstantNullSpace()");

    Vec nullSpaceBasis;
    VecDuplicate(getVecSolInterior(), &nullSpaceBasis);
    
    SystemVector basisVec("tmp", componentSpaces, componentSpaces.size(), true);
    basisVec.set(0.0);
    basisVec.getDOFVector(constFeSpace)->set(1.0);
    
    setDofVector(nullSpaceBasis, basisVec, true);
    VecAssemblyBegin(nullSpaceBasis);
    VecAssemblyEnd(nullSpaceBasis);
    VecNormalize(nullSpaceBasis, PETSC_NULL);
    
    if (test) {
      Vec tmp;
      MatGetVecs(getMatInterior(), &tmp, PETSC_NULL);
      MatMult(getMatInterior(), nullSpaceBasis, tmp);
      PetscReal n;
      VecNorm(tmp, NORM_2, &n);
      MSG("NORM IS: %e\n", n);
      VecDestroy(&tmp);
934
    }
Thomas Witkowski's avatar
blub    
Thomas Witkowski committed
935
936
937
    
    
    MatNullSpace matNullSpace;
938
    MatNullSpaceCreate(domainComm, PETSC_FALSE, 1, &nullSpaceBasis, &matNullSpace);
Thomas Witkowski's avatar
blub    
Thomas Witkowski committed
939
940
    KSPSetNullSpace(ksp, matNullSpace);
    MatNullSpaceDestroy(&matNullSpace);
941
942

    VecDestroy(&nullSpaceBasis);
Thomas Witkowski's avatar
blub    
Thomas Witkowski committed
943
944
  }

945

Thomas Witkowski's avatar
blub    
Thomas Witkowski committed
946
947
948
949
950
  void PetscSolverGlobalMatrix::setConstantNullSpace(KSP ksp)
  {
    FUNCNAME("PetscSolverGlobalMatrix::setConstantNullSpace()");

    MatNullSpace matNullSpace;
951
    MatNullSpaceCreate(domainComm, PETSC_TRUE, 0, PETSC_NULL, &matNullSpace);
Thomas Witkowski's avatar
blub    
Thomas Witkowski committed
952
953
954
    KSPSetNullSpace(ksp, matNullSpace);
    MatNullSpaceDestroy(&matNullSpace);
  }
955

956
} } // end namespace Parallel, AMDiS