PetscSolver.cc 4.85 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.


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

18

19 20
namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
21 22
  using namespace std;

23
  PetscSolver::PetscSolver()
24 25 26 27
    : meshDistributor(NULL),      
      subdomainLevel(0),
      interiorMap(NULL),
      coarseSpaceMap(NULL),
28
      mpiRank(-1),
29
      kspPrefix(""),
Thomas Witkowski's avatar
Thomas Witkowski committed
30 31
      removeRhsNullspace(false),
      hasConstantNullspace(false)
32 33 34 35 36
  {
    string kspStr = "";
    Parameters::get("parallel->solver->petsc->ksp", kspStr);
    if (kspStr != "")
      PetscOptionsInsertString(kspStr.c_str());
37

Thomas Witkowski's avatar
Thomas Witkowski committed
38 39
    Parameters::get("parallel->remove rhs null space", removeRhsNullspace);
    Parameters::get("parallel->has constant null space", hasConstantNullspace);
40 41
    Parameters::get("parallel->nullspace->const in comp", 
		    constNullspaceComponent);
42 43 44
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
45 46
  void PetscSolver::setCoarseSpaceDofMapping(ParallelDofMapping *coarseDofs, 
					     int component = -1)
47
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
48 49 50 51 52 53 54 55 56 57 58 59 60
    FUNCNAME("PetscSolver::setCoarseSpaceDofMapping()");

    TEST_EXIT_DBG(coarseDofs)("Should not happen!\n");

    if (component == -1) {
      coarseSpaceMap.clear();

      int nComponents = coarseDofs->getNumberOfComponents();
      for (int i = 0; i < nComponents; i++)
	coarseSpaceMap[i] = coarseDofs;
    } else {
      coarseSpaceMap[component] = coarseDofs;
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
61 62
  }

63

Thomas Witkowski's avatar
Thomas Witkowski committed
64 65
  void PetscSolver::updateSubdomainData()
  {
66 67 68 69 70 71 72 73 74 75
    if (mpiCommLocal.Get_size() == 1) {
      rStartInterior = 0;
      nGlobalOverallInterior = interiorMap->getOverallDofs();
    } else {
      int groupRowsInterior = 0;
      if (mpiCommLocal.Get_rank() == 0)
	groupRowsInterior = interiorMap->getOverallDofs();

      mpi::getDofNumbering(mpiCommGlobal, groupRowsInterior,
			   rStartInterior, nGlobalOverallInterior);
76

77 78 79 80 81
      int tmp = 0;
      if (mpiCommLocal.Get_rank() == 0)
	tmp = rStartInterior;

      mpiCommLocal.Allreduce(&tmp, &rStartInterior, 1, MPI_INT, MPI_SUM);
82
    }
83 84
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
85
 
86 87 88
  void PetscSolver::solve(Vec &rhs, Vec &sol)
  {
    FUNCNAME("PetscSolver::solve()");
89

90 91 92 93
    PetscErrorCode solverError = KSPSolve(kspInterior, rhs, sol);
    if (solverError != 0) {
      AMDiS::finalize();
      exit(-1);
94 95 96
    }
  }

97

98 99 100 101 102 103 104 105
  void PetscSolver::solveGlobal(Vec &rhs, Vec &sol)
  {
    FUNCNAME("PetscSolver::solveGlobal()");

    ERROR_EXIT("Not implemented!\n");
  }


106 107 108 109 110 111
  void PetscSolver::copyVec(Vec& originVec, Vec& destVec, 
			    vector<int>& originIndex, vector<int>& destIndex)
  {
    FUNCNAME("PetscSolver::copyVec()");

    IS originIs, destIs;
112
    ISCreateGeneral(mpiCommGlobal, 
113 114 115 116 117
		    originIndex.size(), 
		    &(originIndex[0]),
		    PETSC_USE_POINTER,
		    &originIs);

118
    ISCreateGeneral(mpiCommGlobal, 
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		    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);
  }

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

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

196
}