PetscSolverGlobalMatrix.cc 29.9 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
  PetscSolverGlobalMatrix::PetscSolverGlobalMatrix(string name, bool setOptions)
32
33
34
    : PetscSolver(name),
      zeroStartVector(false),
      printMatInfo(false)
35
36
  { FUNCNAME_DBG("PetscSolverGlobalMatrix()");
  
37
    bool matSolverPackage = false;
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
    if (setOptions) {
      PetscParameters params;
      
      // 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.find(kspSolver) != params.matSolverPackage.end()) {
	// direct solvers
	PetscOptionsInsertString(("-" + kspPrefix + "ksp_type preonly").c_str());
	PetscOptionsInsertString(("-" + kspPrefix + "pc_type lu").c_str());
	PetscOptionsInsertString(("-" + kspPrefix + "pc_factor_mat_solver_package " + kspSolver).c_str());
	setMaxIterations(1);
	zeroStartVector = true;
	matSolverPackage = true;
      } else if (params.emptyParam.find(kspSolver) == params.emptyParam.end() && solverName != "petsc") {    
	// 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.find(precon) == params.emptyParam.end()) {
	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());
80
81
    }
    if (!matSolverPackage) {
82
      Parameters::get(name + "->use zero start vector", zeroStartVector);
83
    }
84
    Parameters::get("parallel->print matrix info", printMatInfo);
85
86
87
88
89
90
91
92
93
94
95
96
97
    
#if DEBUG != 0
    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);
    PetscOptionsView(viewer);
    PetscViewerDestroy(&viewer);
    }
#endif
98
99
100
  }


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
101
  void PetscSolverGlobalMatrix::fillPetscMatrix(Matrix<DOFMatrix*> *seqMat)
Thomas Witkowski's avatar
Thomas Witkowski committed
102
103
104
  {
    FUNCNAME("PetscSolverGlobalMatrix::fillPetscMatrix()");

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
105
106
107
108
    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");
    
109
#if (DEBUG != 0)
110
    Timer t;
111
#endif
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
112

113
    createMatVec(*seqMat);
114

Thomas Witkowski's avatar
Thomas Witkowski committed
115
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
116
      fillPetscMatrixWithCoarseSpace(seqMat);
117
118
      return;
    }
119
    
120
121
    // === Create PETSc vector (solution and a temporary vector). ===

Thomas Witkowski's avatar
Thomas Witkowski committed
122
#if (DEBUG != 0)
123
124
    MSG("Fill petsc matrix 1 needed %.5f seconds\n", t.elapsed());
    t.reset();
Thomas Witkowski's avatar
Thomas Witkowski committed
125
126
127
128
#endif

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

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
129
    int nComponents = seqMat->getNumRows();
Thomas Witkowski's avatar
Thomas Witkowski committed
130
131
    for (int i = 0; i < nComponents; i++)
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
132
133
	if ((*seqMat)[i][j])
	  setDofMatrix((*seqMat)[i][j], i, j);
Thomas Witkowski's avatar
Thomas Witkowski committed
134
135

#if (DEBUG != 0)
136
137
    MSG("Fill petsc matrix 2 needed %.5f seconds\n", t.elapsed());
    t.reset();
Thomas Witkowski's avatar
Thomas Witkowski committed
138
139
#endif

140
    matAssembly();
141

142
143
    if (printMatInfo) {
      MatInfo matInfo;
144
      MatGetInfo(getMatInterior(), MAT_GLOBAL_SUM, &matInfo);
145
146
147
148
149
150
151
      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));
    }
152
153


154
    // === Init PETSc solver and preconditioner objects. ===
155

156
157
    initSolver(kspInterior);
    KSPGetPC(kspInterior, &pcInterior);
158
    initPreconditioner(pcInterior);
159

160

Thomas Witkowski's avatar
Thomas Witkowski committed
161
#if (DEBUG != 0)
162
    MSG("Fill petsc matrix 3 needed %.5f seconds\n", t.elapsed());
Thomas Witkowski's avatar
Thomas Witkowski committed
163
#endif
164
165


166
167
168
169
170
171
172
173
174
175
176
    // === 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);
    }
177
178
179
  }


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
180
  void PetscSolverGlobalMatrix::fillPetscMatrixWithCoarseSpace(Matrix<DOFMatrix*> *seqMat)
181
  {
182
    FUNCNAME_DBG("PetscSolverGlobalMatrix::fillPetscMatrixWithCoarseSpace()");
183

184
    TEST_EXIT_DBG(interiorMap)("No interiorMap! Should not happen!\n");
185
186
    TEST_EXIT_DBG(coarseSpaceMap.size() == seqMat->getSize())
      ("Wrong sizes %d %d\n", coarseSpaceMap.size(), seqMat->getSize());
187

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    // === 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);

204
205
206
    bool localMatrix = 
      (meshDistributor->getMeshLevelData().getMpiComm(meshLevel) == MPI::COMM_SELF);

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

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
210
    int nComponents = seqMat->getSize();
Thomas Witkowski's avatar
Thomas Witkowski committed
211
212
213
214
215
    for (int rowComponent = 0; rowComponent < nComponents; rowComponent++) {
      for (int colComponent = 0; colComponent < nComponents; colComponent++) {
	DOFMatrix* dofMat = (*seqMat)[rowComponent][colComponent];

	if (!dofMat)
216
217
	  continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
218
219
	ParallelDofMapping *rowCoarseSpace = coarseSpaceMap[rowComponent];
	ParallelDofMapping *colCoarseSpace = coarseSpaceMap[colComponent];
Thomas Witkowski's avatar
Thomas Witkowski committed
220

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

Thomas Witkowski's avatar
Thomas Witkowski committed
223
224
	traits::col<Matrix>::type col(dofMat->getBaseMatrix());
	traits::const_value<Matrix>::type value(dofMat->getBaseMatrix());
225
226
	
	// Traverse all rows.
Thomas Witkowski's avatar
Thomas Witkowski committed
227
228
	for (cursor_type cursor = begin<row>(dofMat->getBaseMatrix()), 
	       cend = end<row>(dofMat->getBaseMatrix()); cursor != cend; ++cursor) {
229

230
	  bool isRowCoarse = isCoarseSpace(rowComponent, cursor.value());
Thomas Witkowski's avatar
Thomas Witkowski committed
231
232
233

	  // For the case, this is a dirichlet row we have to check whether the 
	  // rank is also owner of this row DOF.
234
235
236
	  if (dirichletRows.count(cursor.value())) {
	    if ((!isRowCoarse && !(*interiorMap)[rowComponent].isRankDof(cursor.value())) ||
		(isRowCoarse && !(*rowCoarseSpace)[rowComponent].isRankDof(cursor.value())))
237
	      continue;    
Thomas Witkowski's avatar
Thomas Witkowski committed
238
	  }
239
240
241
242
243
244
245
246
247
248
  
	  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
249
	    bool isColCoarse = isCoarseSpace(colComponent, col(*icursor));
250

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
251
	    if (isColCoarse == false)
252
	      if ((*interiorMap)[colComponent].isSet(col(*icursor)) == false)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
253
254
		continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
255
256
257
	    if (isColCoarse == isRowCoarse) {
	      cols.push_back(col(*icursor));
	      values.push_back(value(*icursor));
258
	    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
259
260
	      colsOther.push_back(col(*icursor));
	      valuesOther.push_back(value(*icursor));
261
262
263
264
265
266
	    }
	  }  // for each nnz in row


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

Thomas Witkowski's avatar
Thomas Witkowski committed
267
	  if (isRowCoarse) {
Thomas Witkowski's avatar
Thomas Witkowski committed
268
269
	    for (unsigned int i = 0; i < cols.size(); i++)
	      cols[i] = colCoarseSpace->getMatIndex(colComponent, cols[i]);
270

271
	    int rowIndex = rowCoarseSpace->getMatIndex(rowComponent, cursor.value());
Thomas Witkowski's avatar
Thomas Witkowski committed
272
	    MatSetValues(getMatCoarseByComponent(rowComponent, colComponent),
273
274
	    		 1, &rowIndex, cols.size(),
	    		 &(cols[0]), &(values[0]), ADD_VALUES);
275
276

	    if (colsOther.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
277
278
279
	      for (unsigned int i = 0; i < colsOther.size(); i++)
		colsOther[i] = 
		  interiorMap->getMatIndex(colComponent, colsOther[i]) + 
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
280
		  rStartInterior;	      
281

Thomas Witkowski's avatar
Thomas Witkowski committed
282
	      MatSetValues(getMatCoarseInteriorByComponent(rowComponent), 
283
284
	       		   1, &rowIndex, colsOther.size(),
 	       		   &(colsOther[0]), &(valuesOther[0]), ADD_VALUES);
285
286
	    }
	  } else {
287
	    if ((*interiorMap)[rowComponent].isSet(cursor.value()) == false)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
288
289
	      continue;

290
	    int localRowIndex = 
291
	      (localMatrix ? 
292
293
	       interiorMap->getLocalMatIndex(rowComponent, cursor.value()) :
	       interiorMap->getMatIndex(rowComponent, cursor.value()));
294

Thomas Witkowski's avatar
Thomas Witkowski committed
295
	    for (unsigned int i = 0; i < cols.size(); i++) {
296
	      if (localMatrix)
Thomas Witkowski's avatar
Thomas Witkowski committed
297
		cols[i] = interiorMap->getLocalMatIndex(colComponent, cols[i]);
298
	      else
Thomas Witkowski's avatar
Thomas Witkowski committed
299
		cols[i] = interiorMap->getMatIndex(colComponent, cols[i]);
300
301
	    }
	    
302
303
	    MatSetValues(getMatInterior(), 1, &localRowIndex, cols.size(),
  	     		 &(cols[0]), &(values[0]), ADD_VALUES);
Thomas Witkowski's avatar
Thomas Witkowski committed
304
	    
305
	    if (colsOther.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
306
	      int globalRowIndex = 
307
		interiorMap->getMatIndex(rowComponent, cursor.value()) + rStartInterior;
Thomas Witkowski's avatar
Thomas Witkowski committed
308
      
Thomas Witkowski's avatar
Thomas Witkowski committed
309
310
311
	      for (unsigned int i = 0; i < colsOther.size(); i++)
		colsOther[i] = 
		  colCoarseSpace->getMatIndex(colComponent, colsOther[i]);
312

313
314
315
	      MatSetValues(getMatInteriorCoarseByComponent(colComponent), 
			   1, &globalRowIndex, colsOther.size(),
			   &(colsOther[0]), &(valuesOther[0]), ADD_VALUES);
316
317
318
319
320
321
	    }
	  }
	} 
      }
    }

322
    matAssembly();
323

324
325
    // === Create solver for the non primal (thus local) variables. ===

326
    KSPCreate(domainComm, &kspInterior);
327
    KSPSetOperators(kspInterior, getMatInterior(), getMatInterior(),
328
		    SAME_NONZERO_PATTERN);
329
330
331
    KSPSetOptionsPrefix(kspInterior, "interior_");
    KSPSetType(kspInterior, KSPPREONLY);
    KSPGetPC(kspInterior, &pcInterior);
332
333
    if (isSymmetric) {
      PCSetType(pcInterior, PCCHOLESKY);
334
      PCFactorSetMatSolverPackage(pcInterior, MATSOLVERMUMPS);
335
336
    } else {
      PCSetType(pcInterior, PCLU);
337
      if (localMatrix)
338
339
340
341
	PCFactorSetMatSolverPackage(pcInterior, MATSOLVERUMFPACK);
      else
	PCFactorSetMatSolverPackage(pcInterior, MATSOLVERMUMPS);
    }
342
    KSPSetFromOptions(kspInterior);
343
344
345
  }


346
347
  void PetscSolverGlobalMatrix::fillPetscRhs(SystemVector *vec)
  {
348
    FUNCNAME_DBG("PetscSolverGlobalMatrix::fillPetscRhs()");
Thomas Witkowski's avatar
Thomas Witkowski committed
349

350
    TEST_EXIT_DBG(vec)("No DOF vector defined!\n");
351
    TEST_EXIT_DBG(interiorMap)("No parallel DOF map defined!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
352
    
Thomas Witkowski's avatar
Thomas Witkowski committed
353
    // === Transfer values from DOF vector to the PETSc vector. === 
Thomas Witkowski's avatar
Thomas Witkowski committed
354
    if (coarseSpaceMap.size()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
355
      for (int i = 0; i < vec->getSize(); i++)
356
	setDofVector(getVecRhsInterior(), 
Thomas Witkowski's avatar
Thomas Witkowski committed
357
		     getVecRhsCoarseByComponent(i), vec->getDOFVector(i), i);
Thomas Witkowski's avatar
Thomas Witkowski committed
358
359
    } else {
      for (int i = 0; i < vec->getSize(); i++)
360
	setDofVector(getVecRhsInterior(), vec->getDOFVector(i), i);
Thomas Witkowski's avatar
Thomas Witkowski committed
361
362
    }

363
    vecRhsAssembly();
364
365
366
367
368
369
370
371
372
373
374
375

    // === 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
376
377
378
  }


379
380
  void PetscSolverGlobalMatrix::solvePetscMatrix(SystemVector &vec, 
						 AdaptInfo *adaptInfo)
Thomas Witkowski's avatar
Thomas Witkowski committed
381
382
383
384
385
386
387
  {
    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
388
389
      TEST_EXIT(coarseSpaceMap.size() == 0)("Not yet supported!\n");

390
      VecSet(getVecSolInterior(), 0.0);
Thomas Witkowski's avatar
Thomas Witkowski committed
391
392
      
      for (int i = 0; i < nComponents; i++)
393
	setDofVector(getVecSolInterior(), vec.getDOFVector(i), i, true);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
394

395
      vecSolAssembly();
Thomas Witkowski's avatar
Thomas Witkowski committed
396
397
    }

398

Thomas Witkowski's avatar
Thomas Witkowski committed
399
400
    MatNullSpace matNullspace;
    Vec nullspaceBasis;
401
402
403
    if (nullspace.size() > 0 || 
	hasConstantNullspace ||
	constNullspaceComponent.size() > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
404
      TEST_EXIT_DBG(nullspace.size() <= 1)("Not yet implemented!\n");
405

406
407
408
409
410
411
412
413
414
415
      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
416
      if (nullspace.size() > 0) {
417
	VecDuplicate(getVecSolInterior(), &nullspaceBasis);
418
	setDofVector(nullspaceBasis, *(nullspace[0]), true);
Thomas Witkowski's avatar
Thomas Witkowski committed
419
420
421
	
	VecAssemblyBegin(nullspaceBasis);
	VecAssemblyEnd(nullspaceBasis);
422
423

	VecNormalize(nullspaceBasis, PETSC_NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
424
	
425
	MatNullSpaceCreate(domainComm, (hasConstantNullspace ? PETSC_TRUE : PETSC_FALSE), 
Thomas Witkowski's avatar
Thomas Witkowski committed
426
427
			   1, &nullspaceBasis, &matNullspace);

428
	MatMult(getMatInterior(), nullspaceBasis, getVecSolInterior());
Thomas Witkowski's avatar
Thomas Witkowski committed
429
	PetscReal n;
430
	VecNorm(getVecSolInterior(), NORM_2, &n);
Thomas Witkowski's avatar
Thomas Witkowski committed
431
432
	MSG("NORM IS: %e\n", n);
      } else {
433
	MatNullSpaceCreate(domainComm, PETSC_TRUE, 0, PETSC_NULL, &matNullspace);
Thomas Witkowski's avatar
Thomas Witkowski committed
434
      }
435

Thomas Witkowski's avatar
Thomas Witkowski committed
436
437
438
      MSG("NULLSPACE IS NOT REMOVED!\n");
      // MatSetNullSpace(getMatInterior(), matNullspace);
      // KSPSetNullSpace(kspInterior, matNullspace);
439

Thomas Witkowski's avatar
Thomas Witkowski committed
440
      // === Remove null space, if requested. ===
441

Thomas Witkowski's avatar
Thomas Witkowski committed
442
      if (removeRhsNullspace) {
Thomas Witkowski's avatar
Thomas Witkowski committed
443
	TEST_EXIT_DBG(coarseSpaceMap.empty())("Not supported!\n");
444
445

	MSG("Remove nullspace from rhs vector.\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
446
	
447
	MatNullSpaceRemove(matNullspace, getVecRhsInterior(), PETSC_NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
448
449
450
451
      }
    } else {
      TEST_EXIT(removeRhsNullspace == false)
	("No nullspace provided that should be removed from rhs!\n");
452
453
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
454
    // PETSc.
455
    solve(getVecRhsInterior(), getVecSolInterior());
Thomas Witkowski's avatar
Thomas Witkowski committed
456

457
458

    if (nullspace.size() > 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
459
      MatNullSpaceDestroy(&matNullspace);
460
461
462
463
      VecDestroy(&nullspaceBasis);
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
464
465
    // === Transfere values from PETSc's solution vectors to the DOF vectors. ===
    PetscScalar *vecPointer;
466
    VecGetArray(getVecSolInterior(), &vecPointer);    
Thomas Witkowski's avatar
Thomas Witkowski committed
467

468
    int c = 0;
469
470
    for (int component = 0; component < nComponents; component++) {
      DOFVector<double> &dv = *(vec.getDOFVector(component));
471

472
      DofMap& d = (*interiorMap)[component].getMap();
473
474
475
      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
476
477
    }

478
    VecRestoreArray(getVecSolInterior(), &vecPointer);
Thomas Witkowski's avatar
Thomas Witkowski committed
479
480
481
482

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


486
487
488
  void PetscSolverGlobalMatrix::solveGlobal(Vec &rhs, Vec &sol)
  {
    Vec tmp;
489
    if (domainComm.Get_size() == 1)
490
      createLocalVec(*interiorMap, tmp);
491
    else
492
      createVec(*interiorMap, tmp);
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518

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


519
520
  void PetscSolverGlobalMatrix::destroyMatrixData()
  {
521
    matDestroy();
Thomas Witkowski's avatar
Thomas Witkowski committed
522

523
524
    exitPreconditioner(pcInterior);
    exitSolver(kspInterior);
Thomas Witkowski's avatar
Thomas Witkowski committed
525
526
527
  }


528
529
  void PetscSolverGlobalMatrix::destroyVectorData()
  {
530
    vecDestroy();
531
532
533
  }


534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
  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());

553
      TEST_EXIT(nComponents > 0)("No IS block for block %d defined!\n", i);
554
555
556
557
558
559
560
561

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

562
      createFieldSplit(pc, isNames[i].c_str(), blockComponents);
563
564
565
566
    }
  }


567
  void PetscSolverGlobalMatrix::createFieldSplit(PC pc, 
568
						 const char* splitName, 
569
570
571
572
						 vector<int> &components)
  {
    IS is;
    interiorMap->createIndexSet(is, components[0], components.size());
573
    PCFieldSplitSetIS(pc, splitName, is);
574
575
    ISDestroy(&is);
  }
576
577
578
579
580
581
582
583
584
  
  
  void PetscSolverGlobalMatrix::extractVectorComponent(Vec input, int i, Vec *output, int numberOfComponents)
  {
    IS is;
    interiorMap->createIndexSet(is, i, numberOfComponents);
    VecGetSubVector(input, is, output);
    ISDestroy(&is);
  }
585

586
587
588
589
590
591
592
593
594
595
  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);
  }
  
596

597
  void PetscSolverGlobalMatrix::initSolver(KSP &ksp)
598
  {
599
    KSPCreate(domainComm, &ksp);
600
601
602
603
604
    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());
605
    MatSetOptionsPrefix(getMatInterior(), kspPrefix.c_str());
606
607
608
609
610
611
612
613
    KSPSetFromOptions(ksp);

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


614
  void PetscSolverGlobalMatrix::exitSolver(KSP &ksp)
615
616
617
618
619
  {
    KSPDestroy(&ksp);
  }  


620
621
622
623
624
625
626
627
  void PetscSolverGlobalMatrix::initPreconditioner(PC pc)
  {
    PCSetFromOptions(pc);
    createFieldSplit(pc);
  }

 
  void PetscSolverGlobalMatrix::exitPreconditioner(PC pc)
628
  { }
629
630


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
631
  void PetscSolverGlobalMatrix::setDofMatrix(DOFMatrix* seqMat,
Thomas Witkowski's avatar
Thomas Witkowski committed
632
					     int rowComp, int colComp)
Thomas Witkowski's avatar
Thomas Witkowski committed
633
634
635
  {
    FUNCNAME("PetscSolverGlobalMatrix::setDofMatrix()");

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

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

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

    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;

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

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

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

      int globalRowDof = rowMultiIndex.global;
673

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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


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

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

820
    ParallelDofMapping *rowCoarseSpace = 
821
      (coarseSpaceMap.size() ? coarseSpaceMap[rowComp] : nullptr);
Thomas Witkowski's avatar
Thomas Witkowski committed
822
823

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

Thomas Witkowski's avatar
Thomas Witkowski committed
825
826
827
    // 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
828
829
      
      DegreeOfFreedom dof = dofIt.getDOFIndex();
830

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

834
835
      bool isCoarseDof = isCoarseSpace(rowComp, dof);

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

843
      if (isCoarseDof) {
844
	TEST_EXIT_DBG(vecCoarse != PETSC_NULL)("vecCoarse not set! Should not happen!\n");
845

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
869
870
871
	  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
872
	    int mappedIndex = interiorMap->getMatIndex(rowComp, mappedDof);
873

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

884
885
886
887
888
889
890
891

  PetscSolver* PetscSolverGlobalMatrix::createSubSolver(int component,
							string kspPrefix)
  {
    vector<const FiniteElemSpace*> fe;
    fe.push_back(componentSpaces[component]);

    PetscSolver* subSolver = new PetscSolverGlobalMatrix("");
892
    subSolver->setKspPrefix(kspPrefix);
893
    subSolver->setMeshDistributor(meshDistributor, 0);
894
895
896
897
898
899
900
901
902
    subSolver->init(fe, fe);

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

    return subSolver;
  }

903
904
905
906

  void PetscSolverGlobalMatrix::setConstantNullSpace(KSP ksp,
						     int constFeSpace,
						     bool test)
Thomas Witkowski's avatar
blub  
Thomas Witkowski committed
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
  {
    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);
930
    }
Thomas Witkowski's avatar
blub  
Thomas Witkowski committed
931
932
933
    
    
    MatNullSpace matNullSpace;
934
    MatNullSpaceCreate(domainComm, PETSC_FALSE, 1, &nullSpaceBasis, &matNullSpace);
Thomas Witkowski's avatar
blub  
Thomas Witkowski committed
935
936
    KSPSetNullSpace(ksp, matNullSpace);
    MatNullSpaceDestroy(&matNullSpace);
937
938

    VecDestroy(&nullSpaceBasis);
Thomas Witkowski's avatar
blub  
Thomas Witkowski committed
939
940
  }

941

Thomas Witkowski's avatar
blub  
Thomas Witkowski committed
942
943
944
  void PetscSolverGlobalMatrix::setConstantNullSpace(KSP ksp)
  {
    MatNullSpace matNullSpace;
945
    MatNullSpaceCreate(domainComm, PETSC_TRUE, 0, PETSC_NULL, &matNullSpace);
Thomas Witkowski's avatar
blub  
Thomas Witkowski committed
946
947
948
    KSPSetNullSpace(ksp, matNullSpace);
    MatNullSpaceDestroy(&matNullSpace);
  }
949

950
} } // end namespace Parallel, AMDiS