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


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

namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
19 20
  using namespace std;

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  void PetscSolver::printSolutionInfo(AdaptInfo *adaptInfo,
				      bool iterationCounter, 
				      bool residual)
  {
    FUNCNAME("PetscSolver::printSolutionInfo()");

    if (iterationCounter) {
      int iterations = 0;
      KSPGetIterationNumber(solver, &iterations);
      MSG("  Number of iterations: %d\n", iterations);
      adaptInfo->setSolverIterations(iterations);
    }

    if (residual) {
      double norm = 0.0;
      MatMult(petscMatrix, petscSolVec, petscTmpVec);
      VecAXPY(petscTmpVec, -1.0, petscRhsVec);
      VecNorm(petscTmpVec, NORM_2, &norm);
      MSG("  Residual norm: %e\n", norm);
    }
  }

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

  void PetscSolver::copyVec(Vec& originVec, Vec& destVec, 
			    vector<int>& originIndex, vector<int>& destIndex)
  {
    FUNCNAME("PetscSolver::copyVec()");

    IS originIs, destIs;
    ISCreateGeneral(PETSC_COMM_WORLD, 
		    originIndex.size(), 
		    &(originIndex[0]),
		    PETSC_USE_POINTER,
		    &originIs);

    ISCreateGeneral(PETSC_COMM_WORLD, 
		    destIndex.size(), 
		    &(destIndex[0]),
		    PETSC_USE_POINTER,
		    &destIs);

    VecScatter scatter;
    VecScatterCreate(originVec, originIs, destVec, destIs, &scatter);
    VecScatterBegin(scatter, originVec, destVec,
		    INSERT_VALUES, SCATTER_FORWARD);
    VecScatterEnd(scatter, originVec, destVec,
		  INSERT_VALUES, SCATTER_FORWARD);

    ISDestroy(&originIs);
    ISDestroy(&destIs);    
    VecScatterDestroy(&scatter);
  }

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

  vector<const FiniteElemSpace*> PetscSolver::getFeSpaces(Matrix<DOFMatrix*> *mat)
  {
    FUNCNAME("PetscSolver::getFeSpaces()");

    int nComponents = mat->getNumRows();
    vector<const FiniteElemSpace*> result(nComponents);

    for (int i = 0; i < nComponents; i++) 
      for (int j = 0; j < nComponents; j++)
	if ((*mat)[i][j]) {
	  result[i] = (*mat)[i][j]->getRowFeSpace();
	  break;
	}

#if (DEBUG != 0)
    // === In debug mode, we test if all FE spaces of the matrix are also ===
    // === considered by the mesh distributor.                            ===
    checkFeSpaces(result);
#endif

    return result;
  }


  vector<const FiniteElemSpace*> PetscSolver::getFeSpaces(SystemVector *vec)
  {
    FUNCNAME("PetscSolver::getFeSpaces()");

    int nComponents = vec->getSize();
    vector<const FiniteElemSpace*> result(nComponents);

    for (int i = 0; i < nComponents; i++) 
      result[i] = vec->getFeSpace(i);

#if (DEBUG != 0)
    // === In debug mode, we test if all FE spaces of the matrix are also ===
    // === considered by the mesh distributor.                            ===
    checkFeSpaces(result);
#endif

    return result;
  }


  void PetscSolver::checkFeSpaces(vector<const FiniteElemSpace*>& feSpaces)
  {
    FUNCNAME("PetscSolver::checkFeSpaces()");

    for (unsigned int i = 0; i < feSpaces.size(); i++) {
      bool found = false;
      for (unsigned int j = 0; j < meshDistributor->getFeSpaces().size(); j++)
	if (feSpaces[i] == meshDistributor->getFeSpace(j)) {
	  found = true;
	  break;
	}
      TEST_EXIT(found)("FE spaces not found in mesh distributor!\n");
    }
  }

134
}